cec: added vendor ids for Akai, AOC, Benq, Daewoo, Grundig, Medion, Sharp and Vizio
[deb_libcec.git] / src / testclient / main.cpp
1 /*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
31 */
32
33 #include "../../include/cec.h"
34
35 #include <cstdio>
36 #include <fcntl.h>
37 #include <iostream>
38 #include <fstream>
39 #include <string>
40 #include <sstream>
41 #include "../lib/platform/os.h"
42 #include "../lib/implementations/CECCommandHandler.h"
43
44 using namespace CEC;
45 using namespace std;
46 using namespace PLATFORM;
47
48 #define CEC_CONFIG_VERSION CEC_CLIENT_VERSION_1_7_1;
49
50 #include <cecloader.h>
51
52 ICECCallbacks g_callbacks;
53 libcec_configuration g_config;
54 int g_cecLogLevel(-1);
55 int g_cecDefaultLogLevel(CEC_LOG_ALL);
56 ofstream g_logOutput;
57 bool g_bShortLog(false);
58 CStdString g_strPort;
59 bool g_bSingleCommand(false);
60 bool g_bExit(false);
61 bool g_bHardExit(false);
62 CMutex g_outputMutex;
63
64 inline void PrintToStdOut(const char *strFormat, ...)
65 {
66 CStdString strLog;
67
68 va_list argList;
69 va_start(argList, strFormat);
70 strLog.FormatV(strFormat, argList);
71 va_end(argList);
72
73 CLockObject lock(g_outputMutex);
74 cout << strLog << endl;
75 }
76
77 inline bool HexStrToInt(const std::string& data, uint8_t& value)
78 {
79 int iTmp(0);
80 if (sscanf(data.c_str(), "%x", &iTmp) == 1)
81 {
82 if (iTmp > 256)
83 value = 255;
84 else if (iTmp < 0)
85 value = 0;
86 else
87 value = (uint8_t) iTmp;
88
89 return true;
90 }
91
92 return false;
93 }
94
95 //get the first word (separated by whitespace) from string data and place that in word
96 //then remove that word from string data
97 bool GetWord(string& data, string& word)
98 {
99 stringstream datastream(data);
100 string end;
101
102 datastream >> word;
103 if (datastream.fail())
104 {
105 data.clear();
106 return false;
107 }
108
109 size_t pos = data.find(word) + word.length();
110
111 if (pos >= data.length())
112 {
113 data.clear();
114 return true;
115 }
116
117 data = data.substr(pos);
118
119 datastream.clear();
120 datastream.str(data);
121
122 datastream >> end;
123 if (datastream.fail())
124 data.clear();
125
126 return true;
127 }
128
129 int CecLogMessage(void *UNUSED(cbParam), const cec_log_message &message)
130 {
131 if ((message.level & g_cecLogLevel) == message.level)
132 {
133 CStdString strLevel;
134 switch (message.level)
135 {
136 case CEC_LOG_ERROR:
137 strLevel = "ERROR: ";
138 break;
139 case CEC_LOG_WARNING:
140 strLevel = "WARNING: ";
141 break;
142 case CEC_LOG_NOTICE:
143 strLevel = "NOTICE: ";
144 break;
145 case CEC_LOG_TRAFFIC:
146 strLevel = "TRAFFIC: ";
147 break;
148 case CEC_LOG_DEBUG:
149 strLevel = "DEBUG: ";
150 break;
151 default:
152 break;
153 }
154
155 CStdString strFullLog;
156 strFullLog.Format("%s[%16lld]\t%s", strLevel.c_str(), message.time, message.message);
157 PrintToStdOut(strFullLog.c_str());
158
159 if (g_logOutput.is_open())
160 {
161 if (g_bShortLog)
162 g_logOutput << message.message << endl;
163 else
164 g_logOutput << strFullLog.c_str() << endl;
165 }
166 }
167
168 return 0;
169 }
170
171 int CecKeyPress(void *UNUSED(cbParam), const cec_keypress &UNUSED(key))
172 {
173 return 0;
174 }
175
176 int CecCommand(void *UNUSED(cbParam), const cec_command &UNUSED(command))
177 {
178 return 0;
179 }
180
181 int CecAlert(void *UNUSED(cbParam), const libcec_alert type, const libcec_parameter &UNUSED(param))
182 {
183 switch (type)
184 {
185 case CEC_ALERT_CONNECTION_LOST:
186 PrintToStdOut("Connection lost - exiting\n");
187 g_bExit = true;
188 break;
189 default:
190 break;
191 }
192 return 0;
193 }
194
195 void ListDevices(ICECAdapter *parser)
196 {
197 cec_adapter *devices = new cec_adapter[10];
198 int8_t iDevicesFound = parser->FindAdapters(devices, 10, NULL);
199 if (iDevicesFound <= 0)
200 {
201 PrintToStdOut("Found devices: NONE");
202 }
203 else
204 {
205 CStdString strDeviceInfo;
206 strDeviceInfo.Format("Found devices: %d\n\n", iDevicesFound);
207
208 for (int8_t iDevicePtr = 0; iDevicePtr < iDevicesFound; iDevicePtr++)
209 {
210 strDeviceInfo.AppendFormat("device: %d\ncom port: %s\n", iDevicePtr + 1, devices[iDevicePtr].comm);
211 libcec_configuration config;
212 config.Clear();
213
214 if (!parser->GetDeviceInformation(devices[iDevicePtr].comm, &config))
215 PrintToStdOut("WARNING: unable to open the device on port %s", devices[iDevicePtr].comm);
216 else
217 {
218 strDeviceInfo.AppendFormat("firmware version: %d\n", config.iFirmwareVersion);
219
220 if (config.iFirmwareBuildDate != CEC_FW_BUILD_UNKNOWN)
221 {
222 time_t buildTime = (time_t)config.iFirmwareBuildDate;
223 strDeviceInfo.AppendFormat("firmware build date: %s", asctime(gmtime(&buildTime)));
224 strDeviceInfo = strDeviceInfo.Left((int)strDeviceInfo.length() - 1); // strip \n added by asctime
225 strDeviceInfo.append(" +0000");
226 }
227 }
228 strDeviceInfo.append("\n");
229 }
230 PrintToStdOut(strDeviceInfo.c_str());
231 }
232 }
233
234 void ShowHelpCommandLine(const char* strExec)
235 {
236 CLockObject lock(g_outputMutex);
237 cout << endl <<
238 strExec << " {-h|--help|-l|--list-devices|[COM PORT]}" << endl <<
239 endl <<
240 "parameters:" << endl <<
241 " -h --help Shows this help text" << endl <<
242 " -l --list-devices List all devices on this system" << endl <<
243 " -t --type {p|r|t|a} The device type to use. More than one is possible." << endl <<
244 " -p --port {int} The HDMI port to use as active source." << endl <<
245 " -b --base {int} The logical address of the device to with this " << endl <<
246 " adapter is connected." << endl <<
247 " -f --log-file {file} Writes all libCEC log message to a file" << endl <<
248 " -r --rom Read persisted settings from the EEPROM" << endl <<
249 " -sf --short-log-file {file} Writes all libCEC log message without timestamps" << endl <<
250 " and log levels to a file." << endl <<
251 " -d --log-level {level} Sets the log level. See cectypes.h for values." << endl <<
252 " -s --single-command Execute a single command and exit. Does not power" << endl <<
253 " on devices on startup and power them off on exit." << endl <<
254 " -o --osd-name {osd name} Use a custom osd name." << endl <<
255 " -m --monitor Start a monitor-only client." << endl <<
256 " [COM PORT] The com port to connect to. If no COM" << endl <<
257 " port is given, the client tries to connect to the" << endl <<
258 " first device that is detected." << endl <<
259 endl <<
260 "Type 'h' or 'help' and press enter after starting the client to display all " << endl <<
261 "available commands" << endl;
262 }
263
264 void ShowHelpConsole(void)
265 {
266 CLockObject lock(g_outputMutex);
267 cout << endl <<
268 "================================================================================" << endl <<
269 "Available commands:" << endl <<
270 endl <<
271 "[tx] {bytes} transfer bytes over the CEC line." << endl <<
272 "[txn] {bytes} transfer bytes but don't wait for transmission ACK." << endl <<
273 "[on] {address} power on the device with the given logical address." << endl <<
274 "[standby] {address} put the device with the given address in standby mode." << endl <<
275 "[la] {logical address} change the logical address of the CEC adapter." << endl <<
276 "[p] {device} {port} change the HDMI port number of the CEC adapter." << endl <<
277 "[pa] {physical address} change the physical address of the CEC adapter." << endl <<
278 "[as] make the CEC adapter the active source." << endl <<
279 "[osd] {addr} {string} set OSD message on the specified device." << endl <<
280 "[ver] {addr} get the CEC version of the specified device." << endl <<
281 "[ven] {addr} get the vendor ID of the specified device." << endl <<
282 "[lang] {addr} get the menu language of the specified device." << endl <<
283 "[pow] {addr} get the power status of the specified device." << endl <<
284 "[name] {addr} get the OSD name of the specified device." << endl <<
285 "[poll] {addr} poll the specified device." << endl <<
286 "[lad] lists active devices on the bus" << endl <<
287 "[ad] {addr} checks whether the specified device is active." << endl <<
288 "[at] {type} checks whether the specified device type is active." << endl <<
289 "[sp] {addr} makes the specified physical address active." << endl <<
290 "[spl] {addr} makes the specified logical address active." << endl <<
291 "[volup] send a volume up command to the amp if present" << endl <<
292 "[voldown] send a volume down command to the amp if present" << endl <<
293 "[mute] send a mute/unmute command to the amp if present" << endl <<
294 "[self] show the list of addresses controlled by libCEC" << endl <<
295 "[scan] scan the CEC bus and display device info" << endl <<
296 "[mon] {1|0} enable or disable CEC bus monitoring." << endl <<
297 "[log] {1 - 31} change the log level. see cectypes.h for values." << endl <<
298 "[ping] send a ping command to the CEC adapter." << endl <<
299 "[bl] to let the adapter enter the bootloader, to upgrade" << endl <<
300 " the flash rom." << endl <<
301 "[r] reconnect to the CEC adapter." << endl <<
302 "[h] or [help] show this help." << endl <<
303 "[q] or [quit] to quit the CEC test client and switch off all" << endl <<
304 " connected CEC devices." << endl <<
305 "================================================================================" << endl;
306 }
307
308 bool ProcessCommandSELF(ICECAdapter *parser, const string &command, string & UNUSED(arguments))
309 {
310 if (command == "self")
311 {
312 cec_logical_addresses addr = parser->GetLogicalAddresses();
313 CStdString strOut = "Addresses controlled by libCEC: ";
314 bool bFirst(true);
315 for (uint8_t iPtr = 0; iPtr <= 15; iPtr++)
316 {
317 if (addr[iPtr])
318 {
319 strOut.AppendFormat((bFirst ? "%d%s" : ", %d%s"), iPtr, parser->IsActiveSource((cec_logical_address)iPtr) ? "*" : "");
320 bFirst = false;
321 }
322 }
323 PrintToStdOut(strOut.c_str());
324 return true;
325 }
326
327 return false;
328 }
329
330 bool ProcessCommandSP(ICECAdapter *parser, const string &command, string &arguments)
331 {
332 if (command == "sp")
333 {
334 string strAddress;
335 int iAddress;
336 if (GetWord(arguments, strAddress))
337 {
338 sscanf(strAddress.c_str(), "%x", &iAddress);
339 if (iAddress >= 0 && iAddress <= CEC_INVALID_PHYSICAL_ADDRESS)
340 parser->SetStreamPath((uint16_t)iAddress);
341 return true;
342 }
343 }
344
345 return false;
346 }
347
348 bool ProcessCommandSPL(ICECAdapter *parser, const string &command, string &arguments)
349 {
350 if (command == "spl")
351 {
352 string strAddress;
353 cec_logical_address iAddress;
354 if (GetWord(arguments, strAddress))
355 {
356 iAddress = (cec_logical_address)atoi(strAddress.c_str());
357 if (iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
358 parser->SetStreamPath(iAddress);
359 return true;
360 }
361 }
362
363 return false;
364 }
365
366 bool ProcessCommandTX(ICECAdapter *parser, const string &command, string &arguments)
367 {
368 if (command == "tx" || command == "txn")
369 {
370 string strvalue;
371 uint8_t ivalue;
372 cec_command bytes;
373 bytes.Clear();
374
375 while (GetWord(arguments, strvalue) && HexStrToInt(strvalue, ivalue))
376 bytes.PushBack(ivalue);
377
378 if (command == "txn")
379 bytes.transmit_timeout = 0;
380
381 parser->Transmit(bytes);
382
383 return true;
384 }
385
386 return false;
387 }
388
389 bool ProcessCommandON(ICECAdapter *parser, const string &command, string &arguments)
390 {
391 if (command == "on")
392 {
393 string strValue;
394 uint8_t iValue = 0;
395 if (GetWord(arguments, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF)
396 {
397 parser->PowerOnDevices((cec_logical_address) iValue);
398 return true;
399 }
400 else
401 {
402 PrintToStdOut("invalid destination");
403 }
404 }
405
406 return false;
407 }
408
409 bool ProcessCommandSTANDBY(ICECAdapter *parser, const string &command, string &arguments)
410 {
411 if (command == "standby")
412 {
413 string strValue;
414 uint8_t iValue = 0;
415 if (GetWord(arguments, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF)
416 {
417 parser->StandbyDevices((cec_logical_address) iValue);
418 return true;
419 }
420 else
421 {
422 PrintToStdOut("invalid destination");
423 }
424 }
425
426 return false;
427 }
428
429 bool ProcessCommandPOLL(ICECAdapter *parser, const string &command, string &arguments)
430 {
431 if (command == "poll")
432 {
433 string strValue;
434 uint8_t iValue = 0;
435 if (GetWord(arguments, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF)
436 {
437 if (parser->PollDevice((cec_logical_address) iValue))
438 PrintToStdOut("POLL message sent");
439 else
440 PrintToStdOut("POLL message not sent");
441 return true;
442 }
443 else
444 {
445 PrintToStdOut("invalid destination");
446 }
447 }
448
449 return false;
450 }
451
452 bool ProcessCommandLA(ICECAdapter *parser, const string &command, string &arguments)
453 {
454 if (command == "la")
455 {
456 string strvalue;
457 if (GetWord(arguments, strvalue))
458 {
459 parser->SetLogicalAddress((cec_logical_address) atoi(strvalue.c_str()));
460 return true;
461 }
462 }
463
464 return false;
465 }
466
467 bool ProcessCommandP(ICECAdapter *parser, const string &command, string &arguments)
468 {
469 if (command == "p")
470 {
471 string strPort, strDevice;
472 if (GetWord(arguments, strDevice) && GetWord(arguments, strPort))
473 {
474 parser->SetHDMIPort((cec_logical_address)atoi(strDevice.c_str()), (uint8_t)atoi(strPort.c_str()));
475 return true;
476 }
477 }
478
479 return false;
480 }
481
482 bool ProcessCommandPA(ICECAdapter *parser, const string &command, string &arguments)
483 {
484 if (command == "pa")
485 {
486 string strB1, strB2;
487 uint8_t iB1, iB2;
488 if (GetWord(arguments, strB1) && HexStrToInt(strB1, iB1) &&
489 GetWord(arguments, strB2) && HexStrToInt(strB2, iB2))
490 {
491 uint16_t iPhysicalAddress = ((uint16_t)iB1 << 8) + iB2;
492 parser->SetPhysicalAddress(iPhysicalAddress);
493 return true;
494 }
495 }
496
497 return false;
498 }
499
500 bool ProcessCommandOSD(ICECAdapter *parser, const string &command, string &arguments)
501 {
502 if (command == "osd")
503 {
504 bool bFirstWord(false);
505 string strAddr, strMessage, strWord;
506 uint8_t iAddr;
507 if (GetWord(arguments, strAddr) && HexStrToInt(strAddr, iAddr) && iAddr < 0xF)
508 {
509 while (GetWord(arguments, strWord))
510 {
511 if (bFirstWord)
512 {
513 bFirstWord = false;
514 strMessage.append(" ");
515 }
516 strMessage.append(strWord);
517 }
518 parser->SetOSDString((cec_logical_address) iAddr, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME, strMessage.c_str());
519 return true;
520 }
521 }
522
523 return false;
524 }
525
526 bool ProcessCommandAS(ICECAdapter *parser, const string &command, string & UNUSED(arguments))
527 {
528 if (command == "as")
529 {
530 parser->SetActiveSource();
531 return true;
532 }
533
534 return false;
535 }
536
537
538 bool ProcessCommandPING(ICECAdapter *parser, const string &command, string & UNUSED(arguments))
539 {
540 if (command == "ping")
541 {
542 parser->PingAdapter();
543 return true;
544 }
545
546 return false;
547 }
548
549 bool ProcessCommandVOLUP(ICECAdapter *parser, const string &command, string & UNUSED(arguments))
550 {
551 if (command == "volup")
552 {
553 PrintToStdOut("volume up: %2X", parser->VolumeUp());
554 return true;
555 }
556
557 return false;
558 }
559
560 bool ProcessCommandVOLDOWN(ICECAdapter *parser, const string &command, string & UNUSED(arguments))
561 {
562 if (command == "voldown")
563 {
564 PrintToStdOut("volume down: %2X", parser->VolumeDown());
565 return true;
566 }
567
568 return false;
569 }
570
571 bool ProcessCommandMUTE(ICECAdapter *parser, const string &command, string & UNUSED(arguments))
572 {
573 if (command == "mute")
574 {
575 PrintToStdOut("mute: %2X", parser->MuteAudio());
576 return true;
577 }
578
579 return false;
580 }
581
582 bool ProcessCommandMON(ICECAdapter *parser, const string &command, string &arguments)
583 {
584 if (command == "mon")
585 {
586 CStdString strEnable;
587 if (GetWord(arguments, strEnable) && (strEnable.Equals("0") || strEnable.Equals("1")))
588 {
589 parser->SwitchMonitoring(strEnable.Equals("1"));
590 return true;
591 }
592 }
593
594 return false;
595 }
596
597 bool ProcessCommandBL(ICECAdapter *parser, const string &command, string & UNUSED(arguments))
598 {
599 if (command == "bl")
600 {
601 if (parser->StartBootloader())
602 {
603 PrintToStdOut("entered bootloader mode. exiting cec-client");
604 g_bExit = true;
605 g_bHardExit = true;
606 }
607 return true;
608 }
609
610 return false;
611 }
612
613 bool ProcessCommandLANG(ICECAdapter *parser, const string &command, string &arguments)
614 {
615 if (command == "lang")
616 {
617 CStdString strDev;
618 if (GetWord(arguments, strDev))
619 {
620 int iDev = atoi(strDev);
621 if (iDev >= 0 && iDev < 15)
622 {
623 CStdString strLog;
624 cec_menu_language language;
625 if (parser->GetDeviceMenuLanguage((cec_logical_address) iDev, &language))
626 strLog.Format("menu language '%s'", language.language);
627 else
628 strLog = "failed!";
629 PrintToStdOut(strLog);
630 return true;
631 }
632 }
633 }
634
635 return false;
636 }
637
638 bool ProcessCommandVEN(ICECAdapter *parser, const string &command, string &arguments)
639 {
640 if (command == "ven")
641 {
642 CStdString strDev;
643 if (GetWord(arguments, strDev))
644 {
645 int iDev = atoi(strDev);
646 if (iDev >= 0 && iDev < 15)
647 {
648 uint64_t iVendor = parser->GetDeviceVendorId((cec_logical_address) iDev);
649 PrintToStdOut("vendor id: %06x", iVendor);
650 return true;
651 }
652 }
653 }
654
655 return false;
656 }
657
658 bool ProcessCommandVER(ICECAdapter *parser, const string &command, string &arguments)
659 {
660 if (command == "ver")
661 {
662 CStdString strDev;
663 if (GetWord(arguments, strDev))
664 {
665 int iDev = atoi(strDev);
666 if (iDev >= 0 && iDev < 15)
667 {
668 cec_version iVersion = parser->GetDeviceCecVersion((cec_logical_address) iDev);
669 PrintToStdOut("CEC version %s", parser->ToString(iVersion));
670 return true;
671 }
672 }
673 }
674
675 return false;
676 }
677
678 bool ProcessCommandPOW(ICECAdapter *parser, const string &command, string &arguments)
679 {
680 if (command == "pow")
681 {
682 CStdString strDev;
683 if (GetWord(arguments, strDev))
684 {
685 int iDev = atoi(strDev);
686 if (iDev >= 0 && iDev < 15)
687 {
688 cec_power_status iPower = parser->GetDevicePowerStatus((cec_logical_address) iDev);
689 PrintToStdOut("power status: %s", parser->ToString(iPower));
690 return true;
691 }
692 }
693 }
694
695 return false;
696 }
697
698 bool ProcessCommandNAME(ICECAdapter *parser, const string &command, string &arguments)
699 {
700 if (command == "name")
701 {
702 CStdString strDev;
703 if (GetWord(arguments, strDev))
704 {
705 int iDev = atoi(strDev);
706 if (iDev >= 0 && iDev < 15)
707 {
708 cec_osd_name name = parser->GetDeviceOSDName((cec_logical_address)iDev);
709 PrintToStdOut("OSD name of device %d is '%s'", iDev, name.name);
710 }
711 return true;
712 }
713 }
714
715 return false;
716 }
717
718 bool ProcessCommandLAD(ICECAdapter *parser, const string &command, string & UNUSED(arguments))
719 {
720 if (command == "lad")
721 {
722 PrintToStdOut("listing active devices:");
723 cec_logical_addresses addresses = parser->GetActiveDevices();
724 for (uint8_t iPtr = 0; iPtr <= 11; iPtr++)
725 if (addresses[iPtr])
726 {
727 PrintToStdOut("logical address %X", (int)iPtr);
728 }
729 return true;
730 }
731
732 return false;
733 }
734
735 bool ProcessCommandAD(ICECAdapter *parser, const string &command, string &arguments)
736 {
737 if (command == "ad")
738 {
739 CStdString strDev;
740 if (GetWord(arguments, strDev))
741 {
742 int iDev = atoi(strDev);
743 if (iDev >= 0 && iDev < 15)
744 PrintToStdOut("logical address %X is %s", iDev, (parser->IsActiveDevice((cec_logical_address)iDev) ? "active" : "not active"));
745 }
746 }
747
748 return false;
749 }
750
751 bool ProcessCommandAT(ICECAdapter *parser, const string &command, string &arguments)
752 {
753 if (command == "at")
754 {
755 CStdString strType;
756 if (GetWord(arguments, strType))
757 {
758 cec_device_type type = CEC_DEVICE_TYPE_TV;
759 if (strType.Equals("a"))
760 type = CEC_DEVICE_TYPE_AUDIO_SYSTEM;
761 else if (strType.Equals("p"))
762 type = CEC_DEVICE_TYPE_PLAYBACK_DEVICE;
763 else if (strType.Equals("r"))
764 type = CEC_DEVICE_TYPE_RECORDING_DEVICE;
765 else if (strType.Equals("t"))
766 type = CEC_DEVICE_TYPE_TUNER;
767
768 PrintToStdOut("device %d is %s", type, (parser->IsActiveDeviceType(type) ? "active" : "not active"));
769 return true;
770 }
771 }
772
773 return false;
774 }
775
776 bool ProcessCommandR(ICECAdapter *parser, const string &command, string & UNUSED(arguments))
777 {
778 if (command == "r")
779 {
780 bool bReactivate = parser->IsLibCECActiveSource();
781
782 PrintToStdOut("closing the connection");
783 parser->Close();
784
785 PrintToStdOut("opening a new connection");
786 parser->Open(g_strPort.c_str());
787
788 if (bReactivate)
789 {
790 PrintToStdOut("setting active source");
791 parser->SetActiveSource();
792 }
793 return true;
794 }
795
796 return false;
797 }
798
799 bool ProcessCommandH(ICECAdapter * UNUSED(parser), const string &command, string & UNUSED(arguments))
800 {
801 if (command == "h" || command == "help")
802 {
803 ShowHelpConsole();
804 return true;
805 }
806
807 return false;
808 }
809
810 bool ProcessCommandLOG(ICECAdapter * UNUSED(parser), const string &command, string &arguments)
811 {
812 if (command == "log")
813 {
814 CStdString strLevel;
815 if (GetWord(arguments, strLevel))
816 {
817 int iNewLevel = atoi(strLevel);
818 if (iNewLevel >= CEC_LOG_ERROR && iNewLevel <= CEC_LOG_ALL)
819 {
820 g_cecLogLevel = iNewLevel;
821
822 PrintToStdOut("log level changed to %s", strLevel.c_str());
823 return true;
824 }
825 }
826 }
827
828 return false;
829 }
830
831 bool ProcessCommandSCAN(ICECAdapter *parser, const string &command, string & UNUSED(arguments))
832 {
833 if (command == "scan")
834 {
835 CStdString strLog;
836 PrintToStdOut("requesting CEC bus information ...");
837
838 strLog.append("CEC bus information\n===================\n");
839 cec_logical_addresses addresses = parser->GetActiveDevices();
840 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
841 {
842 if (addresses[iPtr])
843 {
844 uint64_t iVendorId = parser->GetDeviceVendorId((cec_logical_address)iPtr);
845 bool bActive = parser->IsActiveSource((cec_logical_address)iPtr);
846 uint16_t iPhysicalAddress = parser->GetDevicePhysicalAddress((cec_logical_address)iPtr);
847 cec_version iCecVersion = parser->GetDeviceCecVersion((cec_logical_address)iPtr);
848 cec_power_status power = parser->GetDevicePowerStatus((cec_logical_address)iPtr);
849 cec_osd_name osdName = parser->GetDeviceOSDName((cec_logical_address)iPtr);
850 CStdString strAddr;
851 strAddr.Format("%04x", iPhysicalAddress);
852 cec_menu_language lang;
853 lang.device = CECDEVICE_UNKNOWN;
854 parser->GetDeviceMenuLanguage((cec_logical_address)iPtr, &lang);
855
856 strLog.AppendFormat("device #%X: %s\n", (int)iPtr, parser->ToString((cec_logical_address)iPtr));
857 strLog.AppendFormat("address: %s\n", strAddr.c_str());
858 strLog.AppendFormat("active source: %s\n", (bActive ? "yes" : "no"));
859 strLog.AppendFormat("vendor: %s\n", parser->ToString((cec_vendor_id)iVendorId));
860 strLog.AppendFormat("osd string: %s\n", osdName.name);
861 strLog.AppendFormat("CEC version: %s\n", parser->ToString(iCecVersion));
862 strLog.AppendFormat("power status: %s\n", parser->ToString(power));
863 if ((uint8_t)lang.device == iPtr)
864 strLog.AppendFormat("language: %s\n", lang.language);
865 strLog.append("\n\n");
866 }
867 }
868
869 cec_logical_address activeSource = parser->GetActiveSource();
870 strLog.AppendFormat("currently active source: %s (%d)", parser->ToString(activeSource), (int)activeSource);
871
872 PrintToStdOut(strLog);
873 return true;
874 }
875
876 return false;
877 }
878
879 bool ProcessConsoleCommand(ICECAdapter *parser, string &input)
880 {
881 if (!input.empty())
882 {
883 string command;
884 if (GetWord(input, command))
885 {
886 if (command == "q" || command == "quit")
887 return false;
888
889 ProcessCommandTX(parser, command, input) ||
890 ProcessCommandON(parser, command, input) ||
891 ProcessCommandSTANDBY(parser, command, input) ||
892 ProcessCommandPOLL(parser, command, input) ||
893 ProcessCommandLA(parser, command, input) ||
894 ProcessCommandP(parser, command, input) ||
895 ProcessCommandPA(parser, command, input) ||
896 ProcessCommandAS(parser, command, input) ||
897 ProcessCommandOSD(parser, command, input) ||
898 ProcessCommandPING(parser, command, input) ||
899 ProcessCommandVOLUP(parser, command, input) ||
900 ProcessCommandVOLDOWN(parser, command, input) ||
901 ProcessCommandMUTE(parser, command, input) ||
902 ProcessCommandMON(parser, command, input) ||
903 ProcessCommandBL(parser, command, input) ||
904 ProcessCommandLANG(parser, command, input) ||
905 ProcessCommandVEN(parser, command, input) ||
906 ProcessCommandVER(parser, command, input) ||
907 ProcessCommandPOW(parser, command, input) ||
908 ProcessCommandNAME(parser, command, input) ||
909 ProcessCommandLAD(parser, command, input) ||
910 ProcessCommandAD(parser, command, input) ||
911 ProcessCommandAT(parser, command, input) ||
912 ProcessCommandR(parser, command, input) ||
913 ProcessCommandH(parser, command, input) ||
914 ProcessCommandLOG(parser, command, input) ||
915 ProcessCommandSCAN(parser, command, input) ||
916 ProcessCommandSP(parser, command, input) ||
917 ProcessCommandSPL(parser, command, input) ||
918 ProcessCommandSELF(parser, command, input);
919 }
920 }
921 return true;
922 }
923
924 bool ProcessCommandLineArguments(int argc, char *argv[])
925 {
926 bool bReturn(true);
927 int iArgPtr = 1;
928 while (iArgPtr < argc && bReturn)
929 {
930 if (argc >= iArgPtr + 1)
931 {
932 if (!strcmp(argv[iArgPtr], "-f") ||
933 !strcmp(argv[iArgPtr], "--log-file") ||
934 !strcmp(argv[iArgPtr], "-sf") ||
935 !strcmp(argv[iArgPtr], "--short-log-file"))
936 {
937 if (argc >= iArgPtr + 2)
938 {
939 g_logOutput.open(argv[iArgPtr + 1]);
940 g_bShortLog = (!strcmp(argv[iArgPtr], "-sf") || !strcmp(argv[iArgPtr], "--short-log-file"));
941 iArgPtr += 2;
942 }
943 else
944 {
945 cout << "== skipped log-file parameter: no file given ==" << endl;
946 ++iArgPtr;
947 }
948 }
949 else if (!strcmp(argv[iArgPtr], "-d") ||
950 !strcmp(argv[iArgPtr], "--log-level"))
951 {
952 if (argc >= iArgPtr + 2)
953 {
954 int iNewLevel = atoi(argv[iArgPtr + 1]);
955 if (iNewLevel >= CEC_LOG_ERROR && iNewLevel <= CEC_LOG_ALL)
956 {
957 g_cecLogLevel = iNewLevel;
958 if (!g_bSingleCommand)
959 cout << "log level set to " << argv[iArgPtr + 1] << endl;
960 }
961 else
962 {
963 cout << "== skipped log-level parameter: invalid level '" << argv[iArgPtr + 1] << "' ==" << endl;
964 }
965 iArgPtr += 2;
966 }
967 else
968 {
969 cout << "== skipped log-level parameter: no level given ==" << endl;
970 ++iArgPtr;
971 }
972 }
973 else if (!strcmp(argv[iArgPtr], "-t") ||
974 !strcmp(argv[iArgPtr], "--type"))
975 {
976 if (argc >= iArgPtr + 2)
977 {
978 if (!strcmp(argv[iArgPtr + 1], "p"))
979 {
980 if (!g_bSingleCommand)
981 cout << "== using device type 'playback device'" << endl;
982 g_config.deviceTypes.add(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
983 }
984 else if (!strcmp(argv[iArgPtr + 1], "r"))
985 {
986 if (!g_bSingleCommand)
987 cout << "== using device type 'recording device'" << endl;
988 g_config.deviceTypes.add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
989 }
990 else if (!strcmp(argv[iArgPtr + 1], "t"))
991 {
992 if (!g_bSingleCommand)
993 cout << "== using device type 'tuner'" << endl;
994 g_config.deviceTypes.add(CEC_DEVICE_TYPE_TUNER);
995 }
996 else if (!strcmp(argv[iArgPtr + 1], "a"))
997 {
998 if (!g_bSingleCommand)
999 cout << "== using device type 'audio system'" << endl;
1000 g_config.deviceTypes.add(CEC_DEVICE_TYPE_AUDIO_SYSTEM);
1001 }
1002 else
1003 {
1004 cout << "== skipped invalid device type '" << argv[iArgPtr + 1] << "'" << endl;
1005 }
1006 ++iArgPtr;
1007 }
1008 ++iArgPtr;
1009 }
1010 else if (!strcmp(argv[iArgPtr], "--list-devices") ||
1011 !strcmp(argv[iArgPtr], "-l"))
1012 {
1013 if (g_cecLogLevel == -1)
1014 g_cecLogLevel = CEC_LOG_WARNING + CEC_LOG_ERROR;
1015 ICECAdapter *parser = LibCecInitialise(&g_config);
1016 if (parser)
1017 {
1018 ListDevices(parser);
1019 UnloadLibCec(parser);
1020 parser = NULL;
1021 }
1022 bReturn = false;
1023 }
1024 else if (!strcmp(argv[iArgPtr], "--bootloader"))
1025 {
1026 LibCecBootloader();
1027 bReturn = false;
1028 }
1029 else if (!strcmp(argv[iArgPtr], "--single-command") ||
1030 !strcmp(argv[iArgPtr], "-s"))
1031 {
1032 g_bSingleCommand = true;
1033 ++iArgPtr;
1034 }
1035 else if (!strcmp(argv[iArgPtr], "--help") ||
1036 !strcmp(argv[iArgPtr], "-h"))
1037 {
1038 if (g_cecLogLevel == -1)
1039 g_cecLogLevel = CEC_LOG_WARNING + CEC_LOG_ERROR;
1040
1041 ShowHelpCommandLine(argv[0]);
1042 return 0;
1043 }
1044 else if (!strcmp(argv[iArgPtr], "-b") ||
1045 !strcmp(argv[iArgPtr], "--base"))
1046 {
1047 if (argc >= iArgPtr + 2)
1048 {
1049 g_config.baseDevice = (cec_logical_address)atoi(argv[iArgPtr + 1]);
1050 cout << "using base device '" << (int)g_config.baseDevice << "'" << endl;
1051 ++iArgPtr;
1052 }
1053 ++iArgPtr;
1054 }
1055 else if (!strcmp(argv[iArgPtr], "-p") ||
1056 !strcmp(argv[iArgPtr], "--port"))
1057 {
1058 if (argc >= iArgPtr + 2)
1059 {
1060 uint8_t hdmiport = (int8_t)atoi(argv[iArgPtr + 1]);
1061 if (hdmiport < 1)
1062 hdmiport = 1;
1063 if (hdmiport > 15)
1064 hdmiport = 15;
1065 g_config.iHDMIPort = hdmiport;
1066 cout << "using HDMI port '" << (int)g_config.iHDMIPort << "'" << endl;
1067 ++iArgPtr;
1068 }
1069 ++iArgPtr;
1070 }
1071 else if (!strcmp(argv[iArgPtr], "-r") ||
1072 !strcmp(argv[iArgPtr], "--rom"))
1073 {
1074 cout << "using settings from EEPROM" << endl;
1075 g_config.bGetSettingsFromROM = 1;
1076 ++iArgPtr;
1077 }
1078 else if (!strcmp(argv[iArgPtr], "-o") ||
1079 !strcmp(argv[iArgPtr], "--osd-name"))
1080 {
1081 if (argc >= iArgPtr + 2)
1082 {
1083 snprintf(g_config.strDeviceName, 13, "%s", argv[iArgPtr + 1]);
1084 cout << "using osd name " << g_config.strDeviceName << endl;
1085 ++iArgPtr;
1086 }
1087 ++iArgPtr;
1088 }
1089 else if (!strcmp(argv[iArgPtr], "-m") ||
1090 !strcmp(argv[iArgPtr], "--monitor"))
1091 {
1092 cout << "starting a monitor-only client. use 'mon 0' to switch to normal mode" << endl;
1093 g_config.bMonitorOnly = 1;
1094 ++iArgPtr;
1095 }
1096 else
1097 {
1098 g_strPort = argv[iArgPtr++];
1099 }
1100 }
1101 }
1102
1103 return bReturn;
1104 }
1105
1106 int main (int argc, char *argv[])
1107 {
1108 g_config.Clear();
1109 snprintf(g_config.strDeviceName, 13, "CECTester");
1110 g_config.clientVersion = CEC_CONFIG_VERSION;
1111 g_config.bActivateSource = 0;
1112 g_callbacks.CBCecLogMessage = &CecLogMessage;
1113 g_callbacks.CBCecKeyPress = &CecKeyPress;
1114 g_callbacks.CBCecCommand = &CecCommand;
1115 g_callbacks.CBCecAlert = &CecAlert;
1116 g_config.callbacks = &g_callbacks;
1117
1118 if (!ProcessCommandLineArguments(argc, argv))
1119 return 0;
1120
1121 if (g_cecLogLevel == -1)
1122 g_cecLogLevel = g_cecDefaultLogLevel;
1123
1124 if (g_config.deviceTypes.IsEmpty())
1125 {
1126 if (!g_bSingleCommand)
1127 cout << "No device type given. Using 'recording device'" << endl;
1128 g_config.deviceTypes.add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
1129 }
1130
1131 ICECAdapter *parser = LibCecInitialise(&g_config);
1132 if (!parser)
1133 {
1134 #ifdef __WINDOWS__
1135 cout << "Cannot load libcec.dll" << endl;
1136 #else
1137 cout << "Cannot load libcec.so" << endl;
1138 #endif
1139
1140 if (parser)
1141 UnloadLibCec(parser);
1142
1143 return 1;
1144 }
1145
1146 if (!g_bSingleCommand)
1147 {
1148 CStdString strLog;
1149 strLog.Format("CEC Parser created - libCEC version %s", parser->ToString((cec_server_version)g_config.serverVersion));
1150 cout << strLog.c_str() << endl;
1151
1152 //make stdin non-blocking
1153 #ifndef __WINDOWS__
1154 int flags = fcntl(0, F_GETFL, 0);
1155 flags |= O_NONBLOCK;
1156 fcntl(0, F_SETFL, flags);
1157 #endif
1158 }
1159
1160 if (g_strPort.IsEmpty())
1161 {
1162 if (!g_bSingleCommand)
1163 cout << "no serial port given. trying autodetect: ";
1164 cec_adapter devices[10];
1165 uint8_t iDevicesFound = parser->FindAdapters(devices, 10, NULL);
1166 if (iDevicesFound <= 0)
1167 {
1168 if (g_bSingleCommand)
1169 cout << "autodetect ";
1170 cout << "FAILED" << endl;
1171 UnloadLibCec(parser);
1172 return 1;
1173 }
1174 else
1175 {
1176 if (!g_bSingleCommand)
1177 {
1178 cout << endl << " path: " << devices[0].path << endl <<
1179 " com port: " << devices[0].comm << endl << endl;
1180 }
1181 g_strPort = devices[0].comm;
1182 }
1183 }
1184
1185 PrintToStdOut("opening a connection to the CEC adapter...");
1186
1187 if (!parser->Open(g_strPort.c_str()))
1188 {
1189 PrintToStdOut("unable to open the device on port %s", g_strPort.c_str());
1190 UnloadLibCec(parser);
1191 return 1;
1192 }
1193
1194 if (!g_bSingleCommand)
1195 PrintToStdOut("waiting for input");
1196
1197 while (!g_bExit && !g_bHardExit)
1198 {
1199 string input;
1200 getline(cin, input);
1201 cin.clear();
1202
1203 if (ProcessConsoleCommand(parser, input) && !g_bSingleCommand && !g_bExit && !g_bHardExit)
1204 {
1205 if (!input.empty())
1206 PrintToStdOut("waiting for input");
1207 }
1208 else
1209 g_bExit = true;
1210
1211 if (!g_bExit && !g_bHardExit)
1212 CEvent::Sleep(50);
1213 }
1214
1215 parser->Close();
1216 UnloadLibCec(parser);
1217
1218 if (g_logOutput.is_open())
1219 g_logOutput.close();
1220
1221 return 0;
1222 }