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