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