cec: use the local cec.h/cectypes.h instead of system wide when compiling libCEC...
[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_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 "[self] show the list of addresses controlled by libCEC" << endl <<
282 "[scan] scan the CEC bus and display device info" << endl <<
283 "[mon] {1|0} enable or disable CEC bus monitoring." << endl <<
284 "[log] {1 - 31} change the log level. see cectypes.h for values." << endl <<
285 "[ping] send a ping command to the CEC adapter." << endl <<
286 "[bl] to let the adapter enter the bootloader, to upgrade" << endl <<
287 " the flash rom." << endl <<
288 "[r] reconnect to the CEC adapter." << endl <<
289 "[h] or [help] show this help." << endl <<
290 "[q] or [quit] to quit the CEC test client and switch off all" << endl <<
291 " connected CEC devices." << endl <<
292 "================================================================================" << endl;
293 }
294
295 bool ProcessCommandSELF(ICECAdapter *parser, const string &command, string & UNUSED(arguments))
296 {
297 if (command == "self")
298 {
299 cec_logical_addresses addr = parser->GetLogicalAddresses();
300 CStdString strOut = "Addresses controlled by libCEC: ";
301 bool bFirst(true);
302 for (uint8_t iPtr = 0; iPtr <= 15; iPtr++)
303 {
304 if (addr[iPtr])
305 {
306 strOut.AppendFormat((bFirst ? "%d%s" : ", %d%s"), iPtr, parser->IsActiveSource((cec_logical_address)iPtr) ? "*" : "");
307 bFirst = false;
308 }
309 }
310 PrintToStdOut(strOut.c_str());
311 return true;
312 }
313
314 return false;
315 }
316
317 bool ProcessCommandSP(ICECAdapter *parser, const string &command, string &arguments)
318 {
319 if (command == "sp")
320 {
321 string strAddress;
322 int iAddress;
323 if (GetWord(arguments, strAddress))
324 {
325 sscanf(strAddress.c_str(), "%x", &iAddress);
326 if (iAddress >= 0 && iAddress <= 0xFFFF)
327 parser->SetStreamPath((uint16_t)iAddress);
328 return true;
329 }
330 }
331
332 return false;
333 }
334
335 bool ProcessCommandSPL(ICECAdapter *parser, const string &command, string &arguments)
336 {
337 if (command == "spl")
338 {
339 string strAddress;
340 cec_logical_address iAddress;
341 if (GetWord(arguments, strAddress))
342 {
343 iAddress = (cec_logical_address)atoi(strAddress.c_str());
344 if (iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
345 parser->SetStreamPath(iAddress);
346 return true;
347 }
348 }
349
350 return false;
351 }
352
353 bool ProcessCommandTX(ICECAdapter *parser, const string &command, string &arguments)
354 {
355 if (command == "tx" || command == "txn")
356 {
357 string strvalue;
358 uint8_t ivalue;
359 cec_command bytes;
360 bytes.Clear();
361
362 while (GetWord(arguments, strvalue) && HexStrToInt(strvalue, ivalue))
363 bytes.PushBack(ivalue);
364
365 if (command == "txn")
366 bytes.transmit_timeout = 0;
367
368 parser->Transmit(bytes);
369
370 return true;
371 }
372
373 return false;
374 }
375
376 bool ProcessCommandON(ICECAdapter *parser, const string &command, string &arguments)
377 {
378 if (command == "on")
379 {
380 string strValue;
381 uint8_t iValue = 0;
382 if (GetWord(arguments, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF)
383 {
384 parser->PowerOnDevices((cec_logical_address) iValue);
385 return true;
386 }
387 else
388 {
389 PrintToStdOut("invalid destination");
390 }
391 }
392
393 return false;
394 }
395
396 bool ProcessCommandSTANDBY(ICECAdapter *parser, const string &command, string &arguments)
397 {
398 if (command == "standby")
399 {
400 string strValue;
401 uint8_t iValue = 0;
402 if (GetWord(arguments, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF)
403 {
404 parser->StandbyDevices((cec_logical_address) iValue);
405 return true;
406 }
407 else
408 {
409 PrintToStdOut("invalid destination");
410 }
411 }
412
413 return false;
414 }
415
416 bool ProcessCommandPOLL(ICECAdapter *parser, const string &command, string &arguments)
417 {
418 if (command == "poll")
419 {
420 string strValue;
421 uint8_t iValue = 0;
422 if (GetWord(arguments, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF)
423 {
424 if (parser->PollDevice((cec_logical_address) iValue))
425 PrintToStdOut("POLL message sent");
426 else
427 PrintToStdOut("POLL message not sent");
428 return true;
429 }
430 else
431 {
432 PrintToStdOut("invalid destination");
433 }
434 }
435
436 return false;
437 }
438
439 bool ProcessCommandLA(ICECAdapter *parser, const string &command, string &arguments)
440 {
441 if (command == "la")
442 {
443 string strvalue;
444 if (GetWord(arguments, strvalue))
445 {
446 parser->SetLogicalAddress((cec_logical_address) atoi(strvalue.c_str()));
447 return true;
448 }
449 }
450
451 return false;
452 }
453
454 bool ProcessCommandP(ICECAdapter *parser, const string &command, string &arguments)
455 {
456 if (command == "p")
457 {
458 string strPort, strDevice;
459 if (GetWord(arguments, strDevice) && GetWord(arguments, strPort))
460 {
461 parser->SetHDMIPort((cec_logical_address)atoi(strDevice.c_str()), (uint8_t)atoi(strPort.c_str()));
462 return true;
463 }
464 }
465
466 return false;
467 }
468
469 bool ProcessCommandPA(ICECAdapter *parser, const string &command, string &arguments)
470 {
471 if (command == "pa")
472 {
473 string strB1, strB2;
474 uint8_t iB1, iB2;
475 if (GetWord(arguments, strB1) && HexStrToInt(strB1, iB1) &&
476 GetWord(arguments, strB2) && HexStrToInt(strB2, iB2))
477 {
478 uint16_t iPhysicalAddress = ((uint16_t)iB1 << 8) + iB2;
479 parser->SetPhysicalAddress(iPhysicalAddress);
480 return true;
481 }
482 }
483
484 return false;
485 }
486
487 bool ProcessCommandOSD(ICECAdapter *parser, const string &command, string &arguments)
488 {
489 if (command == "osd")
490 {
491 bool bFirstWord(false);
492 string strAddr, strMessage, strWord;
493 uint8_t iAddr;
494 if (GetWord(arguments, strAddr) && HexStrToInt(strAddr, iAddr) && iAddr < 0xF)
495 {
496 while (GetWord(arguments, strWord))
497 {
498 if (bFirstWord)
499 {
500 bFirstWord = false;
501 strMessage.append(" ");
502 }
503 strMessage.append(strWord);
504 }
505 parser->SetOSDString((cec_logical_address) iAddr, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME, strMessage.c_str());
506 return true;
507 }
508 }
509
510 return false;
511 }
512
513 bool ProcessCommandAS(ICECAdapter *parser, const string &command, string & UNUSED(arguments))
514 {
515 if (command == "as")
516 {
517 parser->SetActiveView();
518 return true;
519 }
520
521 return false;
522 }
523
524
525 bool ProcessCommandPING(ICECAdapter *parser, const string &command, string & UNUSED(arguments))
526 {
527 if (command == "ping")
528 {
529 parser->PingAdapter();
530 return true;
531 }
532
533 return false;
534 }
535
536 bool ProcessCommandVOLUP(ICECAdapter *parser, const string &command, string & UNUSED(arguments))
537 {
538 if (command == "volup")
539 {
540 PrintToStdOut("volume up: %2X", parser->VolumeUp());
541 return true;
542 }
543
544 return false;
545 }
546
547 bool ProcessCommandVOLDOWN(ICECAdapter *parser, const string &command, string & UNUSED(arguments))
548 {
549 if (command == "voldown")
550 {
551 PrintToStdOut("volume down: %2X", parser->VolumeDown());
552 return true;
553 }
554
555 return false;
556 }
557
558 bool ProcessCommandMUTE(ICECAdapter *parser, const string &command, string & UNUSED(arguments))
559 {
560 if (command == "mute")
561 {
562 PrintToStdOut("mute: %2X", parser->MuteAudio());
563 return true;
564 }
565
566 return false;
567 }
568
569 bool ProcessCommandMON(ICECAdapter *parser, const string &command, string &arguments)
570 {
571 if (command == "mon")
572 {
573 CStdString strEnable;
574 if (GetWord(arguments, strEnable) && (strEnable.Equals("0") || strEnable.Equals("1")))
575 {
576 parser->SwitchMonitoring(strEnable.Equals("1"));
577 return true;
578 }
579 }
580
581 return false;
582 }
583
584 bool ProcessCommandBL(ICECAdapter *parser, const string &command, string & UNUSED(arguments))
585 {
586 if (command == "bl")
587 {
588 if (parser->StartBootloader())
589 {
590 PrintToStdOut("entered bootloader mode. exiting cec-client");
591 g_bExit = true;
592 g_bHardExit = true;
593 }
594 return true;
595 }
596
597 return false;
598 }
599
600 bool ProcessCommandLANG(ICECAdapter *parser, const string &command, string &arguments)
601 {
602 if (command == "lang")
603 {
604 CStdString strDev;
605 if (GetWord(arguments, strDev))
606 {
607 int iDev = atoi(strDev);
608 if (iDev >= 0 && iDev < 15)
609 {
610 CStdString strLog;
611 cec_menu_language language;
612 if (parser->GetDeviceMenuLanguage((cec_logical_address) iDev, &language))
613 strLog.Format("menu language '%s'", language.language);
614 else
615 strLog = "failed!";
616 PrintToStdOut(strLog);
617 return true;
618 }
619 }
620 }
621
622 return false;
623 }
624
625 bool ProcessCommandVEN(ICECAdapter *parser, const string &command, string &arguments)
626 {
627 if (command == "ven")
628 {
629 CStdString strDev;
630 if (GetWord(arguments, strDev))
631 {
632 int iDev = atoi(strDev);
633 if (iDev >= 0 && iDev < 15)
634 {
635 uint64_t iVendor = parser->GetDeviceVendorId((cec_logical_address) iDev);
636 PrintToStdOut("vendor id: %06x", iVendor);
637 return true;
638 }
639 }
640 }
641
642 return false;
643 }
644
645 bool ProcessCommandVER(ICECAdapter *parser, const string &command, string &arguments)
646 {
647 if (command == "ver")
648 {
649 CStdString strDev;
650 if (GetWord(arguments, strDev))
651 {
652 int iDev = atoi(strDev);
653 if (iDev >= 0 && iDev < 15)
654 {
655 cec_version iVersion = parser->GetDeviceCecVersion((cec_logical_address) iDev);
656 PrintToStdOut("CEC version %s", parser->ToString(iVersion));
657 return true;
658 }
659 }
660 }
661
662 return false;
663 }
664
665 bool ProcessCommandPOW(ICECAdapter *parser, const string &command, string &arguments)
666 {
667 if (command == "pow")
668 {
669 CStdString strDev;
670 if (GetWord(arguments, strDev))
671 {
672 int iDev = atoi(strDev);
673 if (iDev >= 0 && iDev < 15)
674 {
675 cec_power_status iPower = parser->GetDevicePowerStatus((cec_logical_address) iDev);
676 PrintToStdOut("power status: %s", parser->ToString(iPower));
677 return true;
678 }
679 }
680 }
681
682 return false;
683 }
684
685 bool ProcessCommandNAME(ICECAdapter *parser, const string &command, string &arguments)
686 {
687 if (command == "name")
688 {
689 CStdString strDev;
690 if (GetWord(arguments, strDev))
691 {
692 int iDev = atoi(strDev);
693 if (iDev >= 0 && iDev < 15)
694 {
695 cec_osd_name name = parser->GetDeviceOSDName((cec_logical_address)iDev);
696 PrintToStdOut("OSD name of device %d is '%s'", iDev, name.name);
697 }
698 return true;
699 }
700 }
701
702 return false;
703 }
704
705 bool ProcessCommandLAD(ICECAdapter *parser, const string &command, string & UNUSED(arguments))
706 {
707 if (command == "lad")
708 {
709 PrintToStdOut("listing active devices:");
710 cec_logical_addresses addresses = parser->GetActiveDevices();
711 for (uint8_t iPtr = 0; iPtr <= 11; iPtr++)
712 if (addresses[iPtr])
713 {
714 PrintToStdOut("logical address %X", (int)iPtr);
715 }
716 return true;
717 }
718
719 return false;
720 }
721
722 bool ProcessCommandAD(ICECAdapter *parser, const string &command, string &arguments)
723 {
724 if (command == "ad")
725 {
726 CStdString strDev;
727 if (GetWord(arguments, strDev))
728 {
729 int iDev = atoi(strDev);
730 if (iDev >= 0 && iDev < 15)
731 PrintToStdOut("logical address %X is %s", iDev, (parser->IsActiveDevice((cec_logical_address)iDev) ? "active" : "not active"));
732 }
733 }
734
735 return false;
736 }
737
738 bool ProcessCommandAT(ICECAdapter *parser, const string &command, string &arguments)
739 {
740 if (command == "at")
741 {
742 CStdString strType;
743 if (GetWord(arguments, strType))
744 {
745 cec_device_type type = CEC_DEVICE_TYPE_TV;
746 if (strType.Equals("a"))
747 type = CEC_DEVICE_TYPE_AUDIO_SYSTEM;
748 else if (strType.Equals("p"))
749 type = CEC_DEVICE_TYPE_PLAYBACK_DEVICE;
750 else if (strType.Equals("r"))
751 type = CEC_DEVICE_TYPE_RECORDING_DEVICE;
752 else if (strType.Equals("t"))
753 type = CEC_DEVICE_TYPE_TUNER;
754
755 PrintToStdOut("device %d is %s", type, (parser->IsActiveDeviceType(type) ? "active" : "not active"));
756 return true;
757 }
758 }
759
760 return false;
761 }
762
763 bool ProcessCommandR(ICECAdapter *parser, const string &command, string & UNUSED(arguments))
764 {
765 if (command == "r")
766 {
767 PrintToStdOut("closing the connection");
768 parser->Close();
769
770 PrintToStdOut("opening a new connection");
771 parser->Open(g_strPort.c_str());
772
773 PrintToStdOut("setting active source");
774 parser->SetActiveSource();
775 return true;
776 }
777
778 return false;
779 }
780
781 bool ProcessCommandH(ICECAdapter * UNUSED(parser), const string &command, string & UNUSED(arguments))
782 {
783 if (command == "h" || command == "help")
784 {
785 ShowHelpConsole();
786 return true;
787 }
788
789 return false;
790 }
791
792 bool ProcessCommandLOG(ICECAdapter * UNUSED(parser), const string &command, string &arguments)
793 {
794 if (command == "log")
795 {
796 CStdString strLevel;
797 if (GetWord(arguments, strLevel))
798 {
799 int iNewLevel = atoi(strLevel);
800 if (iNewLevel >= CEC_LOG_ERROR && iNewLevel <= CEC_LOG_ALL)
801 {
802 g_cecLogLevel = iNewLevel;
803
804 PrintToStdOut("log level changed to %s", strLevel.c_str());
805 return true;
806 }
807 }
808 }
809
810 return false;
811 }
812
813 bool ProcessCommandSCAN(ICECAdapter *parser, const string &command, string & UNUSED(arguments))
814 {
815 if (command == "scan")
816 {
817 PrintToStdOut("CEC bus information");
818 PrintToStdOut("===================");
819 cec_logical_addresses addresses = parser->GetActiveDevices();
820 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
821 {
822 if (addresses[iPtr])
823 {
824 CStdString strLog;
825 uint64_t iVendorId = parser->GetDeviceVendorId((cec_logical_address)iPtr);
826 bool bActive = parser->IsActiveSource((cec_logical_address)iPtr);
827 uint16_t iPhysicalAddress = parser->GetDevicePhysicalAddress((cec_logical_address)iPtr);
828 cec_version iCecVersion = parser->GetDeviceCecVersion((cec_logical_address)iPtr);
829 cec_power_status power = parser->GetDevicePowerStatus((cec_logical_address)iPtr);
830 cec_osd_name osdName = parser->GetDeviceOSDName((cec_logical_address)iPtr);
831 CStdString strAddr;
832 strAddr.Format("%04x", iPhysicalAddress);
833 cec_menu_language lang;
834 lang.device = CECDEVICE_UNKNOWN;
835 parser->GetDeviceMenuLanguage((cec_logical_address)iPtr, &lang);
836
837 strLog.AppendFormat("device #%X: %s\n", (int)iPtr, parser->ToString((cec_logical_address)iPtr));
838 strLog.AppendFormat("address: %s\n", strAddr.c_str());
839 strLog.AppendFormat("active source: %s\n", (bActive ? "yes" : "no"));
840 strLog.AppendFormat("vendor: %s\n", parser->ToString((cec_vendor_id)iVendorId));
841 strLog.AppendFormat("osd string: %s\n", osdName.name);
842 strLog.AppendFormat("CEC version: %s\n", parser->ToString(iCecVersion));
843 strLog.AppendFormat("power status: %s\n", parser->ToString(power));
844 if ((uint8_t)lang.device == iPtr)
845 strLog.AppendFormat("language: %s\n", lang.language);
846 strLog.append("\n");
847 PrintToStdOut(strLog);
848 }
849 }
850 return true;
851 }
852
853 return false;
854 }
855
856 bool ProcessConsoleCommand(ICECAdapter *parser, string &input)
857 {
858 if (!input.empty())
859 {
860 string command;
861 if (GetWord(input, command))
862 {
863 if (command == "q" || command == "quit")
864 return false;
865
866 ProcessCommandTX(parser, command, input) ||
867 ProcessCommandON(parser, command, input) ||
868 ProcessCommandSTANDBY(parser, command, input) ||
869 ProcessCommandPOLL(parser, command, input) ||
870 ProcessCommandLA(parser, command, input) ||
871 ProcessCommandP(parser, command, input) ||
872 ProcessCommandPA(parser, command, input) ||
873 ProcessCommandAS(parser, command, input) ||
874 ProcessCommandOSD(parser, command, input) ||
875 ProcessCommandPING(parser, command, input) ||
876 ProcessCommandVOLUP(parser, command, input) ||
877 ProcessCommandVOLDOWN(parser, command, input) ||
878 ProcessCommandMUTE(parser, command, input) ||
879 ProcessCommandMON(parser, command, input) ||
880 ProcessCommandBL(parser, command, input) ||
881 ProcessCommandLANG(parser, command, input) ||
882 ProcessCommandVEN(parser, command, input) ||
883 ProcessCommandVER(parser, command, input) ||
884 ProcessCommandPOW(parser, command, input) ||
885 ProcessCommandNAME(parser, command, input) ||
886 ProcessCommandLAD(parser, command, input) ||
887 ProcessCommandAD(parser, command, input) ||
888 ProcessCommandAT(parser, command, input) ||
889 ProcessCommandR(parser, command, input) ||
890 ProcessCommandH(parser, command, input) ||
891 ProcessCommandLOG(parser, command, input) ||
892 ProcessCommandSCAN(parser, command, input) ||
893 ProcessCommandSP(parser, command, input) ||
894 ProcessCommandSPL(parser, command, input) ||
895 ProcessCommandSELF(parser, command, input);
896 }
897 }
898 return true;
899 }
900
901 bool ProcessCommandLineArguments(int argc, char *argv[])
902 {
903 bool bReturn(true);
904 int iArgPtr = 1;
905 while (iArgPtr < argc && bReturn)
906 {
907 if (argc >= iArgPtr + 1)
908 {
909 if (!strcmp(argv[iArgPtr], "-f") ||
910 !strcmp(argv[iArgPtr], "--log-file") ||
911 !strcmp(argv[iArgPtr], "-sf") ||
912 !strcmp(argv[iArgPtr], "--short-log-file"))
913 {
914 if (argc >= iArgPtr + 2)
915 {
916 g_logOutput.open(argv[iArgPtr + 1]);
917 g_bShortLog = (!strcmp(argv[iArgPtr], "-sf") || !strcmp(argv[iArgPtr], "--short-log-file"));
918 iArgPtr += 2;
919 }
920 else
921 {
922 cout << "== skipped log-file parameter: no file given ==" << endl;
923 ++iArgPtr;
924 }
925 }
926 else if (!strcmp(argv[iArgPtr], "-d") ||
927 !strcmp(argv[iArgPtr], "--log-level"))
928 {
929 if (argc >= iArgPtr + 2)
930 {
931 int iNewLevel = atoi(argv[iArgPtr + 1]);
932 if (iNewLevel >= CEC_LOG_ERROR && iNewLevel <= CEC_LOG_ALL)
933 {
934 g_cecLogLevel = iNewLevel;
935 if (!g_bSingleCommand)
936 cout << "log level set to " << argv[iArgPtr + 1] << endl;
937 }
938 else
939 {
940 cout << "== skipped log-level parameter: invalid level '" << argv[iArgPtr + 1] << "' ==" << endl;
941 }
942 iArgPtr += 2;
943 }
944 else
945 {
946 cout << "== skipped log-level parameter: no level given ==" << endl;
947 ++iArgPtr;
948 }
949 }
950 else if (!strcmp(argv[iArgPtr], "-t") ||
951 !strcmp(argv[iArgPtr], "--type"))
952 {
953 if (argc >= iArgPtr + 2)
954 {
955 if (!strcmp(argv[iArgPtr + 1], "p"))
956 {
957 if (!g_bSingleCommand)
958 cout << "== using device type 'playback device'" << endl;
959 g_typeList.add(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
960 }
961 else if (!strcmp(argv[iArgPtr + 1], "r"))
962 {
963 if (!g_bSingleCommand)
964 cout << "== using device type 'recording device'" << endl;
965 g_typeList.add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
966 }
967 else if (!strcmp(argv[iArgPtr + 1], "t"))
968 {
969 if (!g_bSingleCommand)
970 cout << "== using device type 'tuner'" << endl;
971 g_typeList.add(CEC_DEVICE_TYPE_TUNER);
972 }
973 else if (!strcmp(argv[iArgPtr + 1], "a"))
974 {
975 if (!g_bSingleCommand)
976 cout << "== using device type 'audio system'" << endl;
977 g_typeList.add(CEC_DEVICE_TYPE_AUDIO_SYSTEM);
978 }
979 else
980 {
981 cout << "== skipped invalid device type '" << argv[iArgPtr + 1] << "'" << endl;
982 }
983 ++iArgPtr;
984 }
985 ++iArgPtr;
986 }
987 else if (!strcmp(argv[iArgPtr], "--list-devices") ||
988 !strcmp(argv[iArgPtr], "-l"))
989 {
990 ICECAdapter *parser = CreateParser(g_typeList);
991 if (parser)
992 {
993 ListDevices(parser);
994 UnloadLibCec(parser);
995 parser = NULL;
996 }
997 bReturn = false;
998 }
999 else if (!strcmp(argv[iArgPtr], "--single-command") ||
1000 !strcmp(argv[iArgPtr], "-s"))
1001 {
1002 g_bSingleCommand = true;
1003 ++iArgPtr;
1004 }
1005 else if (!strcmp(argv[iArgPtr], "--help") ||
1006 !strcmp(argv[iArgPtr], "-h"))
1007 {
1008 ShowHelpCommandLine(argv[0]);
1009 return 0;
1010 }
1011 else if (!strcmp(argv[iArgPtr], "-b") ||
1012 !strcmp(argv[iArgPtr], "--base"))
1013 {
1014 if (argc >= iArgPtr + 2)
1015 {
1016 g_iBaseDevice = (cec_logical_address)atoi(argv[iArgPtr + 1]);
1017 cout << "using base device '" << (int)g_iBaseDevice << "'" << endl;
1018 ++iArgPtr;
1019 }
1020 ++iArgPtr;
1021 }
1022 else if (!strcmp(argv[iArgPtr], "-p") ||
1023 !strcmp(argv[iArgPtr], "--port"))
1024 {
1025 if (argc >= iArgPtr + 2)
1026 {
1027 g_iHDMIPort = (int8_t)atoi(argv[iArgPtr + 1]);
1028 cout << "using HDMI port '" << (int)g_iHDMIPort << "'" << endl;
1029 ++iArgPtr;
1030 }
1031 ++iArgPtr;
1032 }
1033 else
1034 {
1035 g_strPort = argv[iArgPtr++];
1036 }
1037 }
1038 }
1039
1040 return bReturn;
1041 }
1042
1043 int main (int argc, char *argv[])
1044 {
1045 g_typeList.clear();
1046
1047 if (!ProcessCommandLineArguments(argc, argv))
1048 return 0;
1049
1050 if (g_typeList.IsEmpty())
1051 {
1052 if (!g_bSingleCommand)
1053 cout << "No device type given. Using 'recording device'" << endl;
1054 g_typeList.add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
1055 }
1056
1057 ICECAdapter *parser = LibCecInit("CECTester", g_typeList);
1058 if (!parser || parser->GetMinLibVersion() > CEC_TEST_CLIENT_VERSION)
1059 {
1060 #ifdef __WINDOWS__
1061 cout << "Cannot load libcec.dll" << endl;
1062 #else
1063 cout << "Cannot load libcec.so" << endl;
1064 #endif
1065
1066 if (parser)
1067 UnloadLibCec(parser);
1068
1069 return 1;
1070 }
1071
1072 if (!g_bSingleCommand)
1073 {
1074 CStdString strLog;
1075 strLog.Format("CEC Parser created - libcec version %d.%d", parser->GetLibVersionMajor(), parser->GetLibVersionMinor());
1076 cout << strLog.c_str() << endl;
1077
1078 //make stdin non-blocking
1079 #ifndef __WINDOWS__
1080 int flags = fcntl(0, F_GETFL, 0);
1081 flags |= O_NONBLOCK;
1082 fcntl(0, F_SETFL, flags);
1083 #endif
1084 }
1085
1086 if (g_strPort.IsEmpty())
1087 {
1088 if (!g_bSingleCommand)
1089 cout << "no serial port given. trying autodetect: ";
1090 cec_adapter devices[10];
1091 uint8_t iDevicesFound = parser->FindAdapters(devices, 10, NULL);
1092 if (iDevicesFound <= 0)
1093 {
1094 if (g_bSingleCommand)
1095 cout << "autodetect ";
1096 cout << "FAILED" << endl;
1097 UnloadLibCec(parser);
1098 return 1;
1099 }
1100 else
1101 {
1102 if (!g_bSingleCommand)
1103 {
1104 cout << endl << " path: " << devices[0].path << endl <<
1105 " com port: " << devices[0].comm << endl << endl;
1106 }
1107 g_strPort = devices[0].comm;
1108 }
1109 }
1110
1111 EnableCallbacks(parser);
1112
1113 parser->SetHDMIPort(g_iBaseDevice, g_iHDMIPort);
1114 PrintToStdOut("opening a connection to the CEC adapter...");
1115
1116 if (!parser->Open(g_strPort.c_str()))
1117 {
1118 PrintToStdOut("unable to open the device on port %s", g_strPort.c_str());
1119 UnloadLibCec(parser);
1120 return 1;
1121 }
1122
1123 if (!g_bSingleCommand)
1124 {
1125 PrintToStdOut("cec device opened");
1126
1127 parser->PowerOnDevices(CECDEVICE_TV);
1128 parser->SetActiveSource();
1129
1130 PrintToStdOut("waiting for input");
1131 }
1132
1133 while (!g_bExit && !g_bHardExit)
1134 {
1135 string input;
1136 getline(cin, input);
1137 cin.clear();
1138
1139 if (ProcessConsoleCommand(parser, input) && !g_bSingleCommand && !g_bExit && !g_bHardExit)
1140 {
1141 if (!input.empty())
1142 PrintToStdOut("waiting for input");
1143 }
1144 else
1145 g_bExit = true;
1146
1147 if (!g_bExit && !g_bHardExit)
1148 CCondition::Sleep(50);
1149 }
1150
1151 if (!g_bSingleCommand && !g_bHardExit)
1152 parser->StandbyDevices(CECDEVICE_BROADCAST);
1153
1154 parser->Close();
1155 UnloadLibCec(parser);
1156
1157 if (g_logOutput.is_open())
1158 g_logOutput.close();
1159
1160 return 0;
1161 }