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