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