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