2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
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.
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.
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.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
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/
34 #include "../include/cec.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"
50 using namespace PLATFORM
;
52 #define CEC_CONFIG_VERSION CEC_CLIENT_VERSION_CURRENT;
54 #include "../../include/cecloader.h"
56 static void PrintToStdOut(const char *strFormat
, ...);
58 ICECCallbacks g_callbacks
;
59 libcec_configuration g_config
;
60 int g_cecLogLevel(-1);
61 int g_cecDefaultLogLevel(CEC_LOG_ALL
);
63 bool g_bShortLog(false);
65 bool g_bSingleCommand(false);
67 bool g_bHardExit(false);
69 ICECAdapter
* g_parser
;
71 class CReconnect
: public PLATFORM::CThread
74 static CReconnect
& Get(void)
76 static CReconnect _instance
;
80 virtual ~CReconnect(void) {}
87 if (!g_parser
->Open(g_strPort
.c_str()))
89 PrintToStdOut("Failed to reconnect\n");
100 static void PrintToStdOut(const char *strFormat
, ...)
105 va_start(argList
, strFormat
);
106 strLog
.FormatV(strFormat
, argList
);
109 CLockObject
lock(g_outputMutex
);
110 cout
<< strLog
<< endl
;
113 inline bool HexStrToInt(const std::string
& data
, uint8_t& value
)
116 if (sscanf(data
.c_str(), "%x", &iTmp
) == 1)
123 value
= (uint8_t) iTmp
;
131 //get the first word (separated by whitespace) from string data and place that in word
132 //then remove that word from string data
133 bool GetWord(string
& data
, string
& word
)
135 stringstream
datastream(data
);
139 if (datastream
.fail())
145 size_t pos
= data
.find(word
) + word
.length();
147 if (pos
>= data
.length())
153 data
= data
.substr(pos
);
156 datastream
.str(data
);
159 if (datastream
.fail())
165 int CecLogMessage(void *UNUSED(cbParam
), const cec_log_message message
)
167 if ((message
.level
& g_cecLogLevel
) == message
.level
)
170 switch (message
.level
)
173 strLevel
= "ERROR: ";
175 case CEC_LOG_WARNING
:
176 strLevel
= "WARNING: ";
179 strLevel
= "NOTICE: ";
181 case CEC_LOG_TRAFFIC
:
182 strLevel
= "TRAFFIC: ";
185 strLevel
= "DEBUG: ";
191 CStdString strFullLog
;
192 strFullLog
.Format("%s[%16lld]\t%s", strLevel
.c_str(), message
.time
, message
.message
);
193 PrintToStdOut(strFullLog
.c_str());
195 if (g_logOutput
.is_open())
198 g_logOutput
<< message
.message
<< endl
;
200 g_logOutput
<< strFullLog
.c_str() << endl
;
207 int CecKeyPress(void *UNUSED(cbParam
), const cec_keypress
UNUSED(key
))
212 int CecCommand(void *UNUSED(cbParam
), const cec_command
UNUSED(command
))
217 int CecAlert(void *UNUSED(cbParam
), const libcec_alert type
, const libcec_parameter
UNUSED(param
))
221 case CEC_ALERT_CONNECTION_LOST
:
222 if (!CReconnect::Get().IsRunning())
224 PrintToStdOut("Connection lost - trying to reconnect\n");
225 CReconnect::Get().CreateThread(false);
234 void ListDevices(ICECAdapter
*parser
)
236 cec_adapter_descriptor devices
[10];
237 int8_t iDevicesFound
= parser
->DetectAdapters(devices
, 10, NULL
);
238 if (iDevicesFound
<= 0)
240 PrintToStdOut("Found devices: NONE");
244 PrintToStdOut("Found devices: %d\n", iDevicesFound
);
246 for (int8_t iDevicePtr
= 0; iDevicePtr
< iDevicesFound
; iDevicePtr
++)
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
);
254 if (devices
[iDevicePtr
].iFirmwareBuildDate
!= CEC_FW_BUILD_UNKNOWN
)
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());
264 if (devices
[iDevicePtr
].adapterType
!= ADAPTERTYPE_UNKNOWN
)
266 PrintToStdOut("type: %s", parser
->ToString(devices
[iDevicePtr
].adapterType
));
274 void ShowHelpCommandLine(const char* strExec
)
276 CLockObject
lock(g_outputMutex
);
278 strExec
<< " {-h|--help|-l|--list-devices|[COM PORT]}" << 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
<<
301 "Type 'h' or 'help' and press enter after starting the client to display all " << endl
<<
302 "available commands" << endl
;
305 void ShowHelpConsole(void)
307 CLockObject
lock(g_outputMutex
);
309 "================================================================================" << endl
<<
310 "Available commands:" << 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
;
350 bool ProcessCommandSELF(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
352 if (command
== "self")
354 cec_logical_addresses addr
= parser
->GetLogicalAddresses();
355 CStdString strOut
= "Addresses controlled by libCEC: ";
357 for (uint8_t iPtr
= 0; iPtr
<= 15; iPtr
++)
361 strOut
.AppendFormat((bFirst
? "%d%s" : ", %d%s"), iPtr
, parser
->IsActiveSource((cec_logical_address
)iPtr
) ? "*" : "");
365 PrintToStdOut(strOut
.c_str());
372 bool ProcessCommandSP(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
378 if (GetWord(arguments
, strAddress
))
380 sscanf(strAddress
.c_str(), "%x", &iAddress
);
381 if (iAddress
>= 0 && iAddress
<= CEC_INVALID_PHYSICAL_ADDRESS
)
382 parser
->SetStreamPath((uint16_t)iAddress
);
390 bool ProcessCommandSPL(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
392 if (command
== "spl")
395 cec_logical_address iAddress
;
396 if (GetWord(arguments
, strAddress
))
398 iAddress
= (cec_logical_address
)atoi(strAddress
.c_str());
399 if (iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
400 parser
->SetStreamPath(iAddress
);
408 bool ProcessCommandTX(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
410 if (command
== "tx" || command
== "txn")
417 CStdString
strArguments(arguments
);
418 strArguments
.Replace(':', ' ');
419 arguments
= strArguments
;
421 while (GetWord(arguments
, strvalue
) && HexStrToInt(strvalue
, ivalue
))
422 bytes
.PushBack(ivalue
);
424 if (command
== "txn")
425 bytes
.transmit_timeout
= 0;
427 parser
->Transmit(bytes
);
435 bool ProcessCommandON(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
441 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
443 parser
->PowerOnDevices((cec_logical_address
) iValue
);
448 PrintToStdOut("invalid destination");
455 bool ProcessCommandSTANDBY(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
457 if (command
== "standby")
461 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
463 parser
->StandbyDevices((cec_logical_address
) iValue
);
468 PrintToStdOut("invalid destination");
475 bool ProcessCommandPOLL(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
477 if (command
== "poll")
481 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
483 if (parser
->PollDevice((cec_logical_address
) iValue
))
484 PrintToStdOut("POLL message sent");
486 PrintToStdOut("POLL message not sent");
491 PrintToStdOut("invalid destination");
498 bool ProcessCommandLA(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
503 if (GetWord(arguments
, strvalue
))
505 parser
->SetLogicalAddress((cec_logical_address
) atoi(strvalue
.c_str()));
513 bool ProcessCommandP(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
517 string strPort
, strDevice
;
518 if (GetWord(arguments
, strDevice
) && GetWord(arguments
, strPort
))
520 parser
->SetHDMIPort((cec_logical_address
)atoi(strDevice
.c_str()), (uint8_t)atoi(strPort
.c_str()));
528 bool ProcessCommandPA(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
534 if (GetWord(arguments
, strB1
) && HexStrToInt(strB1
, iB1
) &&
535 GetWord(arguments
, strB2
) && HexStrToInt(strB2
, iB2
))
537 uint16_t iPhysicalAddress
= ((uint16_t)iB1
<< 8) + iB2
;
538 parser
->SetPhysicalAddress(iPhysicalAddress
);
546 bool ProcessCommandOSD(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
548 if (command
== "osd")
550 bool bFirstWord(false);
551 string strAddr
, strMessage
, strWord
;
553 if (GetWord(arguments
, strAddr
) && HexStrToInt(strAddr
, iAddr
) && iAddr
< 0xF)
555 while (GetWord(arguments
, strWord
))
560 strMessage
.append(" ");
562 strMessage
.append(strWord
);
564 parser
->SetOSDString((cec_logical_address
) iAddr
, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME
, strMessage
.c_str());
572 bool ProcessCommandAS(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
576 parser
->SetActiveSource();
577 // wait for the source switch to finish for 15 seconds tops
578 if (g_bSingleCommand
)
580 CTimeout
timeout(15000);
581 bool bActiveSource(false);
582 while (timeout
.TimeLeft() > 0 && !bActiveSource
)
584 bActiveSource
= parser
->IsLibCECActiveSource();
595 bool ProcessCommandIS(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
598 return parser
->SetInactiveView();
603 bool ProcessCommandPING(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
605 if (command
== "ping")
607 parser
->PingAdapter();
614 bool ProcessCommandVOLUP(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
616 if (command
== "volup")
618 PrintToStdOut("volume up: %2X", parser
->VolumeUp());
625 bool ProcessCommandVOLDOWN(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
627 if (command
== "voldown")
629 PrintToStdOut("volume down: %2X", parser
->VolumeDown());
636 bool ProcessCommandMUTE(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
638 if (command
== "mute")
640 PrintToStdOut("mute: %2X", parser
->MuteAudio());
647 bool ProcessCommandMON(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
649 if (command
== "mon")
651 CStdString strEnable
;
652 if (GetWord(arguments
, strEnable
) && (strEnable
.Equals("0") || strEnable
.Equals("1")))
654 parser
->SwitchMonitoring(strEnable
.Equals("1"));
662 bool ProcessCommandBL(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
666 if (parser
->StartBootloader())
668 PrintToStdOut("entered bootloader mode. exiting cec-client");
678 bool ProcessCommandLANG(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
680 if (command
== "lang")
683 if (GetWord(arguments
, strDev
))
685 int iDev
= atoi(strDev
);
686 if (iDev
>= 0 && iDev
< 15)
689 cec_menu_language language
;
690 if (parser
->GetDeviceMenuLanguage((cec_logical_address
) iDev
, &language
))
691 strLog
.Format("menu language '%s'", language
.language
);
694 PrintToStdOut(strLog
);
703 bool ProcessCommandVEN(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
705 if (command
== "ven")
708 if (GetWord(arguments
, strDev
))
710 int iDev
= atoi(strDev
);
711 if (iDev
>= 0 && iDev
< 15)
713 uint64_t iVendor
= parser
->GetDeviceVendorId((cec_logical_address
) iDev
);
714 PrintToStdOut("vendor id: %06llx", iVendor
);
723 bool ProcessCommandVER(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
725 if (command
== "ver")
728 if (GetWord(arguments
, strDev
))
730 int iDev
= atoi(strDev
);
731 if (iDev
>= 0 && iDev
< 15)
733 cec_version iVersion
= parser
->GetDeviceCecVersion((cec_logical_address
) iDev
);
734 PrintToStdOut("CEC version %s", parser
->ToString(iVersion
));
743 bool ProcessCommandPOW(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
745 if (command
== "pow")
748 if (GetWord(arguments
, strDev
))
750 int iDev
= atoi(strDev
);
751 if (iDev
>= 0 && iDev
< 15)
753 cec_power_status iPower
= parser
->GetDevicePowerStatus((cec_logical_address
) iDev
);
754 PrintToStdOut("power status: %s", parser
->ToString(iPower
));
763 bool ProcessCommandNAME(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
765 if (command
== "name")
768 if (GetWord(arguments
, strDev
))
770 int iDev
= atoi(strDev
);
771 if (iDev
>= 0 && iDev
< 15)
773 cec_osd_name name
= parser
->GetDeviceOSDName((cec_logical_address
)iDev
);
774 PrintToStdOut("OSD name of device %d is '%s'", iDev
, name
.name
);
783 bool ProcessCommandLAD(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
785 if (command
== "lad")
787 PrintToStdOut("listing active devices:");
788 cec_logical_addresses addresses
= parser
->GetActiveDevices();
789 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
792 PrintToStdOut("logical address %X", (int)iPtr
);
800 bool ProcessCommandAD(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
805 if (GetWord(arguments
, strDev
))
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"));
816 bool ProcessCommandAT(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
821 if (GetWord(arguments
, strType
))
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
;
833 PrintToStdOut("device %d is %s", type
, (parser
->IsActiveDeviceType(type
) ? "active" : "not active"));
841 bool ProcessCommandR(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
845 bool bReactivate
= parser
->IsLibCECActiveSource();
847 PrintToStdOut("closing the connection");
850 PrintToStdOut("opening a new connection");
851 parser
->Open(g_strPort
.c_str());
855 PrintToStdOut("setting active source");
856 parser
->SetActiveSource();
864 bool ProcessCommandH(ICECAdapter
* UNUSED(parser
), const string
&command
, string
& UNUSED(arguments
))
866 if (command
== "h" || command
== "help")
875 bool ProcessCommandLOG(ICECAdapter
* UNUSED(parser
), const string
&command
, string
&arguments
)
877 if (command
== "log")
880 if (GetWord(arguments
, strLevel
))
882 int iNewLevel
= atoi(strLevel
);
883 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
885 g_cecLogLevel
= iNewLevel
;
887 PrintToStdOut("log level changed to %s", strLevel
.c_str());
896 bool ProcessCommandSCAN(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
898 if (command
== "scan")
901 PrintToStdOut("requesting CEC bus information ...");
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
++)
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
);
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
);
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");
935 activeSource
= parser
->GetActiveSource();
936 strLog
.AppendFormat("currently active source: %s (%d)", parser
->ToString(activeSource
), (int)activeSource
);
938 PrintToStdOut(strLog
);
945 bool ProcessConsoleCommand(ICECAdapter
*parser
, string
&input
)
950 if (GetWord(input
, command
))
952 if (command
== "q" || command
== "quit")
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
);
991 bool ProcessCommandLineArguments(int argc
, char *argv
[])
995 while (iArgPtr
< argc
&& bReturn
)
997 if (argc
>= iArgPtr
+ 1)
999 if (!strcmp(argv
[iArgPtr
], "-f") ||
1000 !strcmp(argv
[iArgPtr
], "--log-file") ||
1001 !strcmp(argv
[iArgPtr
], "-sf") ||
1002 !strcmp(argv
[iArgPtr
], "--short-log-file"))
1004 if (argc
>= iArgPtr
+ 2)
1006 g_logOutput
.open(argv
[iArgPtr
+ 1]);
1007 g_bShortLog
= (!strcmp(argv
[iArgPtr
], "-sf") || !strcmp(argv
[iArgPtr
], "--short-log-file"));
1012 cout
<< "== skipped log-file parameter: no file given ==" << endl
;
1016 else if (!strcmp(argv
[iArgPtr
], "-d") ||
1017 !strcmp(argv
[iArgPtr
], "--log-level"))
1019 if (argc
>= iArgPtr
+ 2)
1021 int iNewLevel
= atoi(argv
[iArgPtr
+ 1]);
1022 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
1024 g_cecLogLevel
= iNewLevel
;
1025 if (!g_bSingleCommand
)
1026 cout
<< "log level set to " << argv
[iArgPtr
+ 1] << endl
;
1030 cout
<< "== skipped log-level parameter: invalid level '" << argv
[iArgPtr
+ 1] << "' ==" << endl
;
1036 cout
<< "== skipped log-level parameter: no level given ==" << endl
;
1040 else if (!strcmp(argv
[iArgPtr
], "-t") ||
1041 !strcmp(argv
[iArgPtr
], "--type"))
1043 if (argc
>= iArgPtr
+ 2)
1045 if (!strcmp(argv
[iArgPtr
+ 1], "p"))
1047 if (!g_bSingleCommand
)
1048 cout
<< "== using device type 'playback device'" << endl
;
1049 g_config
.deviceTypes
.Add(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
1051 else if (!strcmp(argv
[iArgPtr
+ 1], "r"))
1053 if (!g_bSingleCommand
)
1054 cout
<< "== using device type 'recording device'" << endl
;
1055 g_config
.deviceTypes
.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
1057 else if (!strcmp(argv
[iArgPtr
+ 1], "t"))
1059 if (!g_bSingleCommand
)
1060 cout
<< "== using device type 'tuner'" << endl
;
1061 g_config
.deviceTypes
.Add(CEC_DEVICE_TYPE_TUNER
);
1063 else if (!strcmp(argv
[iArgPtr
+ 1], "a"))
1065 if (!g_bSingleCommand
)
1066 cout
<< "== using device type 'audio system'" << endl
;
1067 g_config
.deviceTypes
.Add(CEC_DEVICE_TYPE_AUDIO_SYSTEM
);
1071 cout
<< "== skipped invalid device type '" << argv
[iArgPtr
+ 1] << "'" << endl
;
1077 else if (!strcmp(argv
[iArgPtr
], "--info") ||
1078 !strcmp(argv
[iArgPtr
], "-i"))
1080 if (g_cecLogLevel
== -1)
1081 g_cecLogLevel
= CEC_LOG_WARNING
+ CEC_LOG_ERROR
;
1082 ICECAdapter
*parser
= LibCecInitialise(&g_config
);
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
);
1095 else if (!strcmp(argv
[iArgPtr
], "--list-devices") ||
1096 !strcmp(argv
[iArgPtr
], "-l"))
1098 if (g_cecLogLevel
== -1)
1099 g_cecLogLevel
= CEC_LOG_WARNING
+ CEC_LOG_ERROR
;
1100 ICECAdapter
*parser
= LibCecInitialise(&g_config
);
1103 ListDevices(parser
);
1104 UnloadLibCec(parser
);
1109 else if (!strcmp(argv
[iArgPtr
], "--bootloader"))
1114 else if (!strcmp(argv
[iArgPtr
], "--single-command") ||
1115 !strcmp(argv
[iArgPtr
], "-s"))
1117 g_bSingleCommand
= true;
1120 else if (!strcmp(argv
[iArgPtr
], "--help") ||
1121 !strcmp(argv
[iArgPtr
], "-h"))
1123 if (g_cecLogLevel
== -1)
1124 g_cecLogLevel
= CEC_LOG_WARNING
+ CEC_LOG_ERROR
;
1126 ShowHelpCommandLine(argv
[0]);
1129 else if (!strcmp(argv
[iArgPtr
], "-b") ||
1130 !strcmp(argv
[iArgPtr
], "--base"))
1132 if (argc
>= iArgPtr
+ 2)
1134 g_config
.baseDevice
= (cec_logical_address
)atoi(argv
[iArgPtr
+ 1]);
1135 cout
<< "using base device '" << (int)g_config
.baseDevice
<< "'" << endl
;
1140 else if (!strcmp(argv
[iArgPtr
], "-p") ||
1141 !strcmp(argv
[iArgPtr
], "--port"))
1143 if (argc
>= iArgPtr
+ 2)
1145 uint8_t hdmiport
= (int8_t)atoi(argv
[iArgPtr
+ 1]);
1150 g_config
.iHDMIPort
= hdmiport
;
1151 cout
<< "using HDMI port '" << (int)g_config
.iHDMIPort
<< "'" << endl
;
1156 else if (!strcmp(argv
[iArgPtr
], "-r") ||
1157 !strcmp(argv
[iArgPtr
], "--rom"))
1159 cout
<< "using settings from EEPROM" << endl
;
1160 g_config
.bGetSettingsFromROM
= 1;
1163 else if (!strcmp(argv
[iArgPtr
], "-o") ||
1164 !strcmp(argv
[iArgPtr
], "--osd-name"))
1166 if (argc
>= iArgPtr
+ 2)
1168 snprintf(g_config
.strDeviceName
, 13, "%s", argv
[iArgPtr
+ 1]);
1169 cout
<< "using osd name " << g_config
.strDeviceName
<< endl
;
1174 else if (!strcmp(argv
[iArgPtr
], "-m") ||
1175 !strcmp(argv
[iArgPtr
], "--monitor"))
1177 cout
<< "starting a monitor-only client. use 'mon 0' to switch to normal mode" << endl
;
1178 g_config
.bMonitorOnly
= 1;
1183 g_strPort
= argv
[iArgPtr
++];
1191 void sighandler(int iSignal
)
1193 PrintToStdOut("signal caught: %d - exiting", iSignal
);
1197 int main (int argc
, char *argv
[])
1199 if (signal(SIGINT
, sighandler
) == SIG_ERR
)
1201 PrintToStdOut("can't register sighandler");
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
;
1216 if (!ProcessCommandLineArguments(argc
, argv
))
1219 if (g_cecLogLevel
== -1)
1220 g_cecLogLevel
= g_cecDefaultLogLevel
;
1222 if (g_config
.deviceTypes
.IsEmpty())
1224 if (!g_bSingleCommand
)
1225 cout
<< "No device type given. Using 'recording device'" << endl
;
1226 g_config
.deviceTypes
.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
1229 g_parser
= LibCecInitialise(&g_config
);
1233 cout
<< "Cannot load libcec.dll" << endl
;
1235 cout
<< "Cannot load libcec.so" << endl
;
1239 UnloadLibCec(g_parser
);
1244 // init video on targets that need this
1245 g_parser
->InitVideoStandalone();
1247 if (!g_bSingleCommand
)
1250 strLog
.Format("CEC Parser created - libCEC version %s", g_parser
->ToString((cec_server_version
)g_config
.serverVersion
));
1251 cout
<< strLog
.c_str() << endl
;
1253 //make stdin non-blocking
1255 int flags
= fcntl(0, F_GETFL
, 0);
1256 flags
|= O_NONBLOCK
;
1257 fcntl(0, F_SETFL
, flags
);
1261 if (g_strPort
.IsEmpty())
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)
1269 if (g_bSingleCommand
)
1270 cout
<< "autodetect ";
1271 cout
<< "FAILED" << endl
;
1272 UnloadLibCec(g_parser
);
1277 if (!g_bSingleCommand
)
1279 cout
<< endl
<< " path: " << devices
[0].path
<< endl
<<
1280 " com port: " << devices
[0].comm
<< endl
<< endl
;
1282 g_strPort
= devices
[0].comm
;
1286 PrintToStdOut("opening a connection to the CEC adapter...");
1288 if (!g_parser
->Open(g_strPort
.c_str()))
1290 PrintToStdOut("unable to open the device on port %s", g_strPort
.c_str());
1291 UnloadLibCec(g_parser
);
1295 if (!g_bSingleCommand
)
1296 PrintToStdOut("waiting for input");
1298 while (!g_bExit
&& !g_bHardExit
)
1301 getline(cin
, input
);
1304 if (ProcessConsoleCommand(g_parser
, input
) && !g_bSingleCommand
&& !g_bExit
&& !g_bHardExit
)
1307 PrintToStdOut("waiting for input");
1312 if (!g_bExit
&& !g_bHardExit
)
1317 UnloadLibCec(g_parser
);
1319 if (g_logOutput
.is_open())
1320 g_logOutput
.close();