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