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