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