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