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