cec: added GetActiveSource()/cec_get_active_source() and IsActiveSource()/cec_is_acti...
[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 <= 11; 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 bool bActive = parser->IsActiveSource((cec_logical_address)iPtr);
724 uint16_t iPhysicalAddress = parser->GetDevicePhysicalAddress((cec_logical_address)iPtr);
725 uint64_t iVendorId = parser->GetDeviceVendorId((cec_logical_address)iPtr);
726 cec_version iCecVersion = parser->GetDeviceCecVersion((cec_logical_address)iPtr);
727 cec_power_status power = parser->GetDevicePowerStatus((cec_logical_address)iPtr);
728 cec_osd_name osdName = parser->GetOSDName((cec_logical_address)iPtr);
729 CStdString strAddr;
730 strAddr.Format("%04x", iPhysicalAddress);
731 cec_menu_language lang;
732 lang.device = CECDEVICE_UNKNOWN;
733 parser->GetDeviceMenuLanguage((cec_logical_address)iPtr, &lang);
734
735 cout << "device #" << (int)iPtr << ": " << parser->ToString((cec_logical_address)iPtr) << endl;
736 cout << "address: " << strAddr.c_str() << endl;
737 cout << "active source: " << (bActive ? "yes" : "no") << endl;
738 cout << "vendor: " << parser->ToString((cec_vendor_id)iVendorId) << endl;
739 cout << "osd string: " << osdName.name << endl;
740 cout << "CEC version: " << parser->ToString(iCecVersion) << endl;
741 cout << "power status: " << parser->ToString(power) << endl;
742 if ((uint8_t)lang.device == iPtr)
743 cout << "language: " << lang.language << endl;
744 cout << endl;
745 }
746 }
747 return true;
748 }
749
750 return false;
751 }
752
753 bool ProcessConsoleCommand(ICECAdapter *parser, string &input)
754 {
755 if (!input.empty())
756 {
757 string command;
758 if (GetWord(input, command))
759 {
760 if (command == "q" || command == "quit")
761 return false;
762
763 ProcessCommandTX(parser, command, input) ||
764 ProcessCommandON(parser, command, input) ||
765 ProcessCommandSTANDBY(parser, command, input) ||
766 ProcessCommandPOLL(parser, command, input) ||
767 ProcessCommandLA(parser, command, input) ||
768 ProcessCommandP(parser, command, input) ||
769 ProcessCommandPA(parser, command, input) ||
770 ProcessCommandOSD(parser, command, input) ||
771 ProcessCommandPING(parser, command, input) ||
772 ProcessCommandVOLUP(parser, command, input) ||
773 ProcessCommandVOLDOWN(parser, command, input) ||
774 ProcessCommandMUTE(parser, command, input) ||
775 ProcessCommandMON(parser, command, input) ||
776 ProcessCommandBL(parser, command, input) ||
777 ProcessCommandLANG(parser, command, input) ||
778 ProcessCommandVEN(parser, command, input) ||
779 ProcessCommandVER(parser, command, input) ||
780 ProcessCommandPOW(parser, command, input) ||
781 ProcessCommandNAME(parser, command, input) ||
782 ProcessCommandLAD(parser, command, input) ||
783 ProcessCommandAD(parser, command, input) ||
784 ProcessCommandAT(parser, command, input) ||
785 ProcessCommandR(parser, command, input) ||
786 ProcessCommandH(parser, command, input) ||
787 ProcessCommandLOG(parser, command, input) ||
788 ProcessCommandSCAN(parser, command, input);
789 }
790 }
791 return true;
792 }
793
794 bool ProcessCommandLineArguments(int argc, char *argv[])
795 {
796 bool bReturn(true);
797 int iArgPtr = 1;
798 while (iArgPtr < argc)
799 {
800 if (argc >= iArgPtr + 1)
801 {
802 if (!strcmp(argv[iArgPtr], "-f") ||
803 !strcmp(argv[iArgPtr], "--log-file") ||
804 !strcmp(argv[iArgPtr], "-sf") ||
805 !strcmp(argv[iArgPtr], "--short-log-file"))
806 {
807 if (argc >= iArgPtr + 2)
808 {
809 g_logOutput.open(argv[iArgPtr + 1]);
810 g_bShortLog = (!strcmp(argv[iArgPtr], "-sf") || !strcmp(argv[iArgPtr], "--short-log-file"));
811 iArgPtr += 2;
812 }
813 else
814 {
815 cout << "== skipped log-file parameter: no file given ==" << endl;
816 ++iArgPtr;
817 }
818 }
819 else if (!strcmp(argv[iArgPtr], "-d") ||
820 !strcmp(argv[iArgPtr], "--log-level"))
821 {
822 if (argc >= iArgPtr + 2)
823 {
824 int iNewLevel = atoi(argv[iArgPtr + 1]);
825 if (iNewLevel >= CEC_LOG_ERROR && iNewLevel <= CEC_LOG_ALL)
826 {
827 g_cecLogLevel = iNewLevel;
828 if (!g_bSingleCommand)
829 cout << "log level set to " << argv[iArgPtr + 1] << endl;
830 }
831 else
832 {
833 cout << "== skipped log-level parameter: invalid level '" << argv[iArgPtr + 1] << "' ==" << endl;
834 }
835 iArgPtr += 2;
836 }
837 else
838 {
839 cout << "== skipped log-level parameter: no level given ==" << endl;
840 ++iArgPtr;
841 }
842 }
843 else if (!strcmp(argv[iArgPtr], "-t") ||
844 !strcmp(argv[iArgPtr], "--type"))
845 {
846 if (argc >= iArgPtr + 2)
847 {
848 if (!strcmp(argv[iArgPtr + 1], "p"))
849 {
850 if (!g_bSingleCommand)
851 cout << "== using device type 'playback device'" << endl;
852 g_typeList.add(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
853 }
854 else if (!strcmp(argv[iArgPtr + 1], "r"))
855 {
856 if (!g_bSingleCommand)
857 cout << "== using device type 'recording device'" << endl;
858 g_typeList.add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
859 }
860 else if (!strcmp(argv[iArgPtr + 1], "t"))
861 {
862 if (!g_bSingleCommand)
863 cout << "== using device type 'tuner'" << endl;
864 g_typeList.add(CEC_DEVICE_TYPE_TUNER);
865 }
866 else if (!strcmp(argv[iArgPtr + 1], "a"))
867 {
868 if (!g_bSingleCommand)
869 cout << "== using device type 'audio system'" << endl;
870 g_typeList.add(CEC_DEVICE_TYPE_AUDIO_SYSTEM);
871 }
872 else
873 {
874 cout << "== skipped invalid device type '" << argv[iArgPtr + 1] << "'" << endl;
875 }
876 ++iArgPtr;
877 }
878 ++iArgPtr;
879 }
880 else if (!strcmp(argv[iArgPtr], "--list-devices") ||
881 !strcmp(argv[iArgPtr], "-l"))
882 {
883 ICECAdapter *parser = CreateParser(g_typeList);
884 if (parser)
885 {
886 ListDevices(parser);
887 UnloadLibCec(parser);
888 }
889 bReturn = false;
890 }
891 else if (!strcmp(argv[iArgPtr], "--single-command") ||
892 !strcmp(argv[iArgPtr], "-s"))
893 {
894 g_bSingleCommand = true;
895 ++iArgPtr;
896 }
897 else if (!strcmp(argv[iArgPtr], "--help") ||
898 !strcmp(argv[iArgPtr], "-h"))
899 {
900 ShowHelpCommandLine(argv[0]);
901 return 0;
902 }
903 else if (!strcmp(argv[iArgPtr], "-p") ||
904 !strcmp(argv[iArgPtr], "--port"))
905 {
906 if (argc >= iArgPtr + 2)
907 {
908 g_iHDMIPort = (int8_t)atoi(argv[iArgPtr + 1]);
909 cout << "using HDMI port '" << g_iHDMIPort << "'" << endl;
910 ++iArgPtr;
911 }
912 ++iArgPtr;
913 }
914 else
915 {
916 g_strPort = argv[iArgPtr++];
917 }
918 }
919 }
920
921 return bReturn;
922 }
923
924 int main (int argc, char *argv[])
925 {
926 g_typeList.clear();
927
928 if (!ProcessCommandLineArguments(argc, argv))
929 return 0;
930
931 if (g_typeList.IsEmpty())
932 {
933 if (!g_bSingleCommand)
934 cout << "No device type given. Using 'recording device'" << endl;
935 g_typeList.add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
936 }
937
938 ICECAdapter *parser = LibCecInit("CECTester", g_typeList);
939 if (!parser || parser->GetMinLibVersion() > CEC_TEST_CLIENT_VERSION)
940 {
941 #ifdef __WINDOWS__
942 cout << "Cannot load libcec.dll" << endl;
943 #else
944 cout << "Cannot load libcec.so" << endl;
945 #endif
946 return 1;
947 }
948
949 if (!g_bSingleCommand)
950 {
951 CStdString strLog;
952 strLog.Format("CEC Parser created - libcec version %d.%d", parser->GetLibVersionMajor(), parser->GetLibVersionMinor());
953 cout << strLog.c_str() << endl;
954
955 //make stdin non-blocking
956 #ifndef __WINDOWS__
957 int flags = fcntl(0, F_GETFL, 0);
958 flags |= O_NONBLOCK;
959 fcntl(0, F_SETFL, flags);
960 #endif
961 }
962
963 if (g_strPort.IsEmpty())
964 {
965 if (!g_bSingleCommand)
966 cout << "no serial port given. trying autodetect: ";
967 cec_adapter devices[10];
968 uint8_t iDevicesFound = parser->FindAdapters(devices, 10, NULL);
969 if (iDevicesFound <= 0)
970 {
971 if (g_bSingleCommand)
972 cout << "autodetect ";
973 cout << "FAILED" << endl;
974 UnloadLibCec(parser);
975 return 1;
976 }
977 else
978 {
979 if (!g_bSingleCommand)
980 {
981 cout << endl << " path: " << devices[0].path << endl <<
982 " com port: " << devices[0].comm << endl << endl;
983 }
984 g_strPort = devices[0].comm;
985 }
986 }
987
988 if (g_iHDMIPort > 0)
989 {
990 parser->SetHDMIPort((cec_logical_address)CEC_DEFAULT_BASE_DEVICE, (uint8_t)g_iHDMIPort);
991 FlushLog(parser);
992 }
993
994 cout << "scanning the CEC bus..." << endl;
995
996 if (!parser->Open(g_strPort.c_str()))
997 {
998 cout << "unable to open the device on port " << g_strPort << endl;
999 FlushLog(parser);
1000 UnloadLibCec(parser);
1001 return 1;
1002 }
1003
1004 if (!g_bSingleCommand)
1005 {
1006 FlushLog(parser);
1007 cout << "cec device opened" << endl;
1008
1009 parser->PowerOnDevices(CECDEVICE_TV);
1010 FlushLog(parser);
1011
1012 parser->SetActiveSource();
1013 FlushLog(parser);
1014
1015 cout << "waiting for input" << endl;
1016 }
1017
1018 bool bContinue(true);
1019 while (bContinue)
1020 {
1021 FlushLog(parser);
1022
1023 /* just ignore the command buffer and clear it */
1024 cec_command dummy;
1025 while (parser && parser->GetNextCommand(&dummy)) {}
1026
1027 string input;
1028 getline(cin, input);
1029 cin.clear();
1030
1031 if (ProcessConsoleCommand(parser, input) && !g_bSingleCommand)
1032 {
1033 if (!input.empty())
1034 cout << "waiting for input" << endl;
1035 }
1036 else
1037 bContinue = false;
1038
1039 if (bContinue)
1040 CCondition::Sleep(50);
1041 }
1042
1043 if (!g_bSingleCommand)
1044 parser->StandbyDevices(CECDEVICE_BROADCAST);
1045
1046 parser->Close();
1047 FlushLog(parser);
1048 UnloadLibCec(parser);
1049
1050 if (g_logOutput.is_open())
1051 g_logOutput.close();
1052
1053 return 0;
1054 }