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"
49 using namespace PLATFORM
;
51 #define CEC_CONFIG_VERSION CEC_CLIENT_VERSION_CURRENT;
53 #include "../../include/cecloader.h"
55 ICECCallbacks g_callbacks
;
56 libcec_configuration g_config
;
57 int g_cecLogLevel(-1);
58 int g_cecDefaultLogLevel(CEC_LOG_ALL
);
60 bool g_bShortLog(false);
62 bool g_bSingleCommand(false);
64 bool g_bHardExit(false);
67 inline void PrintToStdOut(const char *strFormat
, ...)
72 va_start(argList
, strFormat
);
73 strLog
.FormatV(strFormat
, argList
);
76 CLockObject
lock(g_outputMutex
);
77 cout
<< strLog
<< endl
;
80 inline bool HexStrToInt(const std::string
& data
, uint8_t& value
)
83 if (sscanf(data
.c_str(), "%x", &iTmp
) == 1)
90 value
= (uint8_t) iTmp
;
98 //get the first word (separated by whitespace) from string data and place that in word
99 //then remove that word from string data
100 bool GetWord(string
& data
, string
& word
)
102 stringstream
datastream(data
);
106 if (datastream
.fail())
112 size_t pos
= data
.find(word
) + word
.length();
114 if (pos
>= data
.length())
120 data
= data
.substr(pos
);
123 datastream
.str(data
);
126 if (datastream
.fail())
132 int CecLogMessage(void *UNUSED(cbParam
), const cec_log_message message
)
134 if ((message
.level
& g_cecLogLevel
) == message
.level
)
137 switch (message
.level
)
140 strLevel
= "ERROR: ";
142 case CEC_LOG_WARNING
:
143 strLevel
= "WARNING: ";
146 strLevel
= "NOTICE: ";
148 case CEC_LOG_TRAFFIC
:
149 strLevel
= "TRAFFIC: ";
152 strLevel
= "DEBUG: ";
158 CStdString strFullLog
;
159 strFullLog
.Format("%s[%16lld]\t%s", strLevel
.c_str(), message
.time
, message
.message
);
160 PrintToStdOut(strFullLog
.c_str());
162 if (g_logOutput
.is_open())
165 g_logOutput
<< message
.message
<< endl
;
167 g_logOutput
<< strFullLog
.c_str() << endl
;
174 int CecKeyPress(void *UNUSED(cbParam
), const cec_keypress
UNUSED(key
))
179 int CecCommand(void *UNUSED(cbParam
), const cec_command
UNUSED(command
))
184 int CecAlert(void *UNUSED(cbParam
), const libcec_alert type
, const libcec_parameter
UNUSED(param
))
188 case CEC_ALERT_CONNECTION_LOST
:
189 PrintToStdOut("Connection lost - exiting\n");
198 void ListDevices(ICECAdapter
*parser
)
200 cec_adapter_descriptor devices
[10];
201 int8_t iDevicesFound
= parser
->DetectAdapters(devices
, 10, NULL
);
202 if (iDevicesFound
<= 0)
204 PrintToStdOut("Found devices: NONE");
208 PrintToStdOut("Found devices: %d\n", iDevicesFound
);
210 for (int8_t iDevicePtr
= 0; iDevicePtr
< iDevicesFound
; iDevicePtr
++)
212 PrintToStdOut("device: %d", iDevicePtr
+ 1);
213 PrintToStdOut("com port: %s", devices
[iDevicePtr
].strComName
);
214 PrintToStdOut("vendor id: %04x", devices
[iDevicePtr
].iVendorId
);
215 PrintToStdOut("product id: %04x", devices
[iDevicePtr
].iProductId
);
216 PrintToStdOut("firmware version: %d", devices
[iDevicePtr
].iFirmwareVersion
);
218 if (devices
[iDevicePtr
].iFirmwareBuildDate
!= CEC_FW_BUILD_UNKNOWN
)
220 time_t buildTime
= (time_t)devices
[iDevicePtr
].iFirmwareBuildDate
;
221 CStdString strDeviceInfo
;
222 strDeviceInfo
.AppendFormat("firmware build date: %s", asctime(gmtime(&buildTime
)));
223 strDeviceInfo
= strDeviceInfo
.Left(strDeviceInfo
.length() > 1 ? (unsigned)(strDeviceInfo
.length() - 1) : 0); // strip \n added by asctime
224 strDeviceInfo
.append(" +0000");
225 PrintToStdOut(strDeviceInfo
.c_str());
228 if (devices
[iDevicePtr
].adapterType
!= ADAPTERTYPE_UNKNOWN
)
230 PrintToStdOut("type: %s", parser
->ToString(devices
[iDevicePtr
].adapterType
));
238 void ShowHelpCommandLine(const char* strExec
)
240 CLockObject
lock(g_outputMutex
);
242 strExec
<< " {-h|--help|-l|--list-devices|[COM PORT]}" << endl
<<
244 "parameters:" << endl
<<
245 " -h --help Shows this help text" << endl
<<
246 " -l --list-devices List all devices on this system" << endl
<<
247 " -t --type {p|r|t|a} The device type to use. More than one is possible." << endl
<<
248 " -p --port {int} The HDMI port to use as active source." << endl
<<
249 " -b --base {int} The logical address of the device to with this " << endl
<<
250 " adapter is connected." << endl
<<
251 " -f --log-file {file} Writes all libCEC log message to a file" << endl
<<
252 " -r --rom Read persisted settings from the EEPROM" << endl
<<
253 " -sf --short-log-file {file} Writes all libCEC log message without timestamps" << endl
<<
254 " and log levels to a file." << endl
<<
255 " -d --log-level {level} Sets the log level. See cectypes.h for values." << endl
<<
256 " -s --single-command Execute a single command and exit. Does not power" << endl
<<
257 " on devices on startup and power them off on exit." << endl
<<
258 " -o --osd-name {osd name} Use a custom osd name." << endl
<<
259 " -m --monitor Start a monitor-only client." << endl
<<
260 " -i --info Shows information about how libCEC was compiled." << endl
<<
261 " [COM PORT] The com port to connect to. If no COM" << endl
<<
262 " port is given, the client tries to connect to the" << endl
<<
263 " first device that is detected." << endl
<<
265 "Type 'h' or 'help' and press enter after starting the client to display all " << endl
<<
266 "available commands" << endl
;
269 void ShowHelpConsole(void)
271 CLockObject
lock(g_outputMutex
);
273 "================================================================================" << endl
<<
274 "Available commands:" << endl
<<
276 "[tx] {bytes} transfer bytes over the CEC line." << endl
<<
277 "[txn] {bytes} transfer bytes but don't wait for transmission ACK." << endl
<<
278 "[on] {address} power on the device with the given logical address." << endl
<<
279 "[standby] {address} put the device with the given address in standby mode." << endl
<<
280 "[la] {logical address} change the logical address of the CEC adapter." << endl
<<
281 "[p] {device} {port} change the HDMI port number of the CEC adapter." << endl
<<
282 "[pa] {physical address} change the physical address of the CEC adapter." << endl
<<
283 "[as] make the CEC adapter the active source." << endl
<<
284 "[is] mark the CEC adapter as inactive source." << endl
<<
285 "[osd] {addr} {string} set OSD message on the specified device." << endl
<<
286 "[ver] {addr} get the CEC version of the specified device." << endl
<<
287 "[ven] {addr} get the vendor ID of the specified device." << endl
<<
288 "[lang] {addr} get the menu language of the specified device." << endl
<<
289 "[pow] {addr} get the power status of the specified device." << endl
<<
290 "[name] {addr} get the OSD name of the specified device." << endl
<<
291 "[poll] {addr} poll the specified device." << endl
<<
292 "[lad] lists active devices on the bus" << endl
<<
293 "[ad] {addr} checks whether the specified device is active." << endl
<<
294 "[at] {type} checks whether the specified device type is active." << endl
<<
295 "[sp] {addr} makes the specified physical address active." << endl
<<
296 "[spl] {addr} makes the specified logical address active." << endl
<<
297 "[volup] send a volume up command to the amp if present" << endl
<<
298 "[voldown] send a volume down command to the amp if present" << endl
<<
299 "[mute] send a mute/unmute command to the amp if present" << endl
<<
300 "[self] show the list of addresses controlled by libCEC" << endl
<<
301 "[scan] scan the CEC bus and display device info" << endl
<<
302 "[mon] {1|0} enable or disable CEC bus monitoring." << endl
<<
303 "[log] {1 - 31} change the log level. see cectypes.h for values." << endl
<<
304 "[ping] send a ping command to the CEC adapter." << endl
<<
305 "[bl] to let the adapter enter the bootloader, to upgrade" << endl
<<
306 " the flash rom." << endl
<<
307 "[r] reconnect to the CEC adapter." << endl
<<
308 "[h] or [help] show this help." << endl
<<
309 "[q] or [quit] to quit the CEC test client and switch off all" << endl
<<
310 " connected CEC devices." << endl
<<
311 "================================================================================" << endl
;
314 bool ProcessCommandSELF(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
316 if (command
== "self")
318 cec_logical_addresses addr
= parser
->GetLogicalAddresses();
319 CStdString strOut
= "Addresses controlled by libCEC: ";
321 for (uint8_t iPtr
= 0; iPtr
<= 15; iPtr
++)
325 strOut
.AppendFormat((bFirst
? "%d%s" : ", %d%s"), iPtr
, parser
->IsActiveSource((cec_logical_address
)iPtr
) ? "*" : "");
329 PrintToStdOut(strOut
.c_str());
336 bool ProcessCommandSP(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
342 if (GetWord(arguments
, strAddress
))
344 sscanf(strAddress
.c_str(), "%x", &iAddress
);
345 if (iAddress
>= 0 && iAddress
<= CEC_INVALID_PHYSICAL_ADDRESS
)
346 parser
->SetStreamPath((uint16_t)iAddress
);
354 bool ProcessCommandSPL(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
356 if (command
== "spl")
359 cec_logical_address iAddress
;
360 if (GetWord(arguments
, strAddress
))
362 iAddress
= (cec_logical_address
)atoi(strAddress
.c_str());
363 if (iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
364 parser
->SetStreamPath(iAddress
);
372 bool ProcessCommandTX(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
374 if (command
== "tx" || command
== "txn")
381 CStdString
strArguments(arguments
);
382 strArguments
.Replace(':', ' ');
383 arguments
= strArguments
;
385 while (GetWord(arguments
, strvalue
) && HexStrToInt(strvalue
, ivalue
))
386 bytes
.PushBack(ivalue
);
388 if (command
== "txn")
389 bytes
.transmit_timeout
= 0;
391 parser
->Transmit(bytes
);
399 bool ProcessCommandON(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
405 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
407 parser
->PowerOnDevices((cec_logical_address
) iValue
);
412 PrintToStdOut("invalid destination");
419 bool ProcessCommandSTANDBY(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
421 if (command
== "standby")
425 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
427 parser
->StandbyDevices((cec_logical_address
) iValue
);
432 PrintToStdOut("invalid destination");
439 bool ProcessCommandPOLL(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
441 if (command
== "poll")
445 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
447 if (parser
->PollDevice((cec_logical_address
) iValue
))
448 PrintToStdOut("POLL message sent");
450 PrintToStdOut("POLL message not sent");
455 PrintToStdOut("invalid destination");
462 bool ProcessCommandLA(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
467 if (GetWord(arguments
, strvalue
))
469 parser
->SetLogicalAddress((cec_logical_address
) atoi(strvalue
.c_str()));
477 bool ProcessCommandP(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
481 string strPort
, strDevice
;
482 if (GetWord(arguments
, strDevice
) && GetWord(arguments
, strPort
))
484 parser
->SetHDMIPort((cec_logical_address
)atoi(strDevice
.c_str()), (uint8_t)atoi(strPort
.c_str()));
492 bool ProcessCommandPA(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
498 if (GetWord(arguments
, strB1
) && HexStrToInt(strB1
, iB1
) &&
499 GetWord(arguments
, strB2
) && HexStrToInt(strB2
, iB2
))
501 uint16_t iPhysicalAddress
= ((uint16_t)iB1
<< 8) + iB2
;
502 parser
->SetPhysicalAddress(iPhysicalAddress
);
510 bool ProcessCommandOSD(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
512 if (command
== "osd")
514 bool bFirstWord(false);
515 string strAddr
, strMessage
, strWord
;
517 if (GetWord(arguments
, strAddr
) && HexStrToInt(strAddr
, iAddr
) && iAddr
< 0xF)
519 while (GetWord(arguments
, strWord
))
524 strMessage
.append(" ");
526 strMessage
.append(strWord
);
528 parser
->SetOSDString((cec_logical_address
) iAddr
, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME
, strMessage
.c_str());
536 bool ProcessCommandAS(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
540 parser
->SetActiveSource();
541 // wait for the source switch to finish for 15 seconds tops
542 if (g_bSingleCommand
)
544 CTimeout
timeout(15000);
545 bool bActiveSource(false);
546 while (timeout
.TimeLeft() > 0 && !bActiveSource
)
548 bActiveSource
= parser
->IsLibCECActiveSource();
559 bool ProcessCommandIS(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
562 return parser
->SetInactiveView();
567 bool ProcessCommandPING(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
569 if (command
== "ping")
571 parser
->PingAdapter();
578 bool ProcessCommandVOLUP(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
580 if (command
== "volup")
582 PrintToStdOut("volume up: %2X", parser
->VolumeUp());
589 bool ProcessCommandVOLDOWN(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
591 if (command
== "voldown")
593 PrintToStdOut("volume down: %2X", parser
->VolumeDown());
600 bool ProcessCommandMUTE(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
602 if (command
== "mute")
604 PrintToStdOut("mute: %2X", parser
->MuteAudio());
611 bool ProcessCommandMON(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
613 if (command
== "mon")
615 CStdString strEnable
;
616 if (GetWord(arguments
, strEnable
) && (strEnable
.Equals("0") || strEnable
.Equals("1")))
618 parser
->SwitchMonitoring(strEnable
.Equals("1"));
626 bool ProcessCommandBL(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
630 if (parser
->StartBootloader())
632 PrintToStdOut("entered bootloader mode. exiting cec-client");
642 bool ProcessCommandLANG(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
644 if (command
== "lang")
647 if (GetWord(arguments
, strDev
))
649 int iDev
= atoi(strDev
);
650 if (iDev
>= 0 && iDev
< 15)
653 cec_menu_language language
;
654 if (parser
->GetDeviceMenuLanguage((cec_logical_address
) iDev
, &language
))
655 strLog
.Format("menu language '%s'", language
.language
);
658 PrintToStdOut(strLog
);
667 bool ProcessCommandVEN(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
669 if (command
== "ven")
672 if (GetWord(arguments
, strDev
))
674 int iDev
= atoi(strDev
);
675 if (iDev
>= 0 && iDev
< 15)
677 uint64_t iVendor
= parser
->GetDeviceVendorId((cec_logical_address
) iDev
);
678 PrintToStdOut("vendor id: %06llx", iVendor
);
687 bool ProcessCommandVER(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
689 if (command
== "ver")
692 if (GetWord(arguments
, strDev
))
694 int iDev
= atoi(strDev
);
695 if (iDev
>= 0 && iDev
< 15)
697 cec_version iVersion
= parser
->GetDeviceCecVersion((cec_logical_address
) iDev
);
698 PrintToStdOut("CEC version %s", parser
->ToString(iVersion
));
707 bool ProcessCommandPOW(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
709 if (command
== "pow")
712 if (GetWord(arguments
, strDev
))
714 int iDev
= atoi(strDev
);
715 if (iDev
>= 0 && iDev
< 15)
717 cec_power_status iPower
= parser
->GetDevicePowerStatus((cec_logical_address
) iDev
);
718 PrintToStdOut("power status: %s", parser
->ToString(iPower
));
727 bool ProcessCommandNAME(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
729 if (command
== "name")
732 if (GetWord(arguments
, strDev
))
734 int iDev
= atoi(strDev
);
735 if (iDev
>= 0 && iDev
< 15)
737 cec_osd_name name
= parser
->GetDeviceOSDName((cec_logical_address
)iDev
);
738 PrintToStdOut("OSD name of device %d is '%s'", iDev
, name
.name
);
747 bool ProcessCommandLAD(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
749 if (command
== "lad")
751 PrintToStdOut("listing active devices:");
752 cec_logical_addresses addresses
= parser
->GetActiveDevices();
753 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
756 PrintToStdOut("logical address %X", (int)iPtr
);
764 bool ProcessCommandAD(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
769 if (GetWord(arguments
, strDev
))
771 int iDev
= atoi(strDev
);
772 if (iDev
>= 0 && iDev
< 15)
773 PrintToStdOut("logical address %X is %s", iDev
, (parser
->IsActiveDevice((cec_logical_address
)iDev
) ? "active" : "not active"));
780 bool ProcessCommandAT(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
785 if (GetWord(arguments
, strType
))
787 cec_device_type type
= CEC_DEVICE_TYPE_TV
;
788 if (strType
.Equals("a"))
789 type
= CEC_DEVICE_TYPE_AUDIO_SYSTEM
;
790 else if (strType
.Equals("p"))
791 type
= CEC_DEVICE_TYPE_PLAYBACK_DEVICE
;
792 else if (strType
.Equals("r"))
793 type
= CEC_DEVICE_TYPE_RECORDING_DEVICE
;
794 else if (strType
.Equals("t"))
795 type
= CEC_DEVICE_TYPE_TUNER
;
797 PrintToStdOut("device %d is %s", type
, (parser
->IsActiveDeviceType(type
) ? "active" : "not active"));
805 bool ProcessCommandR(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
809 bool bReactivate
= parser
->IsLibCECActiveSource();
811 PrintToStdOut("closing the connection");
814 PrintToStdOut("opening a new connection");
815 parser
->Open(g_strPort
.c_str());
819 PrintToStdOut("setting active source");
820 parser
->SetActiveSource();
828 bool ProcessCommandH(ICECAdapter
* UNUSED(parser
), const string
&command
, string
& UNUSED(arguments
))
830 if (command
== "h" || command
== "help")
839 bool ProcessCommandLOG(ICECAdapter
* UNUSED(parser
), const string
&command
, string
&arguments
)
841 if (command
== "log")
844 if (GetWord(arguments
, strLevel
))
846 int iNewLevel
= atoi(strLevel
);
847 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
849 g_cecLogLevel
= iNewLevel
;
851 PrintToStdOut("log level changed to %s", strLevel
.c_str());
860 bool ProcessCommandSCAN(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
862 if (command
== "scan")
865 PrintToStdOut("requesting CEC bus information ...");
867 strLog
.append("CEC bus information\n===================\n");
868 cec_logical_addresses addresses
= parser
->GetActiveDevices();
869 cec_logical_address activeSource
= parser
->GetActiveSource();
870 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
874 uint64_t iVendorId
= parser
->GetDeviceVendorId((cec_logical_address
)iPtr
);
875 uint16_t iPhysicalAddress
= parser
->GetDevicePhysicalAddress((cec_logical_address
)iPtr
);
876 bool bActive
= parser
->IsActiveSource((cec_logical_address
)iPtr
);
877 cec_version iCecVersion
= parser
->GetDeviceCecVersion((cec_logical_address
)iPtr
);
878 cec_power_status power
= parser
->GetDevicePowerStatus((cec_logical_address
)iPtr
);
879 cec_osd_name osdName
= parser
->GetDeviceOSDName((cec_logical_address
)iPtr
);
881 strAddr
.Format("%x.%x.%x.%x", (iPhysicalAddress
>> 12) & 0xF, (iPhysicalAddress
>> 8) & 0xF, (iPhysicalAddress
>> 4) & 0xF, iPhysicalAddress
& 0xF);
882 cec_menu_language lang
;
883 lang
.device
= CECDEVICE_UNKNOWN
;
884 parser
->GetDeviceMenuLanguage((cec_logical_address
)iPtr
, &lang
);
886 strLog
.AppendFormat("device #%X: %s\n", (int)iPtr
, parser
->ToString((cec_logical_address
)iPtr
));
887 strLog
.AppendFormat("address: %s\n", strAddr
.c_str());
888 strLog
.AppendFormat("active source: %s\n", (bActive
? "yes" : "no"));
889 strLog
.AppendFormat("vendor: %s\n", parser
->ToString((cec_vendor_id
)iVendorId
));
890 strLog
.AppendFormat("osd string: %s\n", osdName
.name
);
891 strLog
.AppendFormat("CEC version: %s\n", parser
->ToString(iCecVersion
));
892 strLog
.AppendFormat("power status: %s\n", parser
->ToString(power
));
893 if ((uint8_t)lang
.device
== iPtr
)
894 strLog
.AppendFormat("language: %s\n", lang
.language
);
895 strLog
.append("\n\n");
899 activeSource
= parser
->GetActiveSource();
900 strLog
.AppendFormat("currently active source: %s (%d)", parser
->ToString(activeSource
), (int)activeSource
);
902 PrintToStdOut(strLog
);
909 bool ProcessConsoleCommand(ICECAdapter
*parser
, string
&input
)
914 if (GetWord(input
, command
))
916 if (command
== "q" || command
== "quit")
919 ProcessCommandTX(parser
, command
, input
) ||
920 ProcessCommandON(parser
, command
, input
) ||
921 ProcessCommandSTANDBY(parser
, command
, input
) ||
922 ProcessCommandPOLL(parser
, command
, input
) ||
923 ProcessCommandLA(parser
, command
, input
) ||
924 ProcessCommandP(parser
, command
, input
) ||
925 ProcessCommandPA(parser
, command
, input
) ||
926 ProcessCommandAS(parser
, command
, input
) ||
927 ProcessCommandIS(parser
, command
, input
) ||
928 ProcessCommandOSD(parser
, command
, input
) ||
929 ProcessCommandPING(parser
, command
, input
) ||
930 ProcessCommandVOLUP(parser
, command
, input
) ||
931 ProcessCommandVOLDOWN(parser
, command
, input
) ||
932 ProcessCommandMUTE(parser
, command
, input
) ||
933 ProcessCommandMON(parser
, command
, input
) ||
934 ProcessCommandBL(parser
, command
, input
) ||
935 ProcessCommandLANG(parser
, command
, input
) ||
936 ProcessCommandVEN(parser
, command
, input
) ||
937 ProcessCommandVER(parser
, command
, input
) ||
938 ProcessCommandPOW(parser
, command
, input
) ||
939 ProcessCommandNAME(parser
, command
, input
) ||
940 ProcessCommandLAD(parser
, command
, input
) ||
941 ProcessCommandAD(parser
, command
, input
) ||
942 ProcessCommandAT(parser
, command
, input
) ||
943 ProcessCommandR(parser
, command
, input
) ||
944 ProcessCommandH(parser
, command
, input
) ||
945 ProcessCommandLOG(parser
, command
, input
) ||
946 ProcessCommandSCAN(parser
, command
, input
) ||
947 ProcessCommandSP(parser
, command
, input
) ||
948 ProcessCommandSPL(parser
, command
, input
) ||
949 ProcessCommandSELF(parser
, command
, input
);
955 bool ProcessCommandLineArguments(int argc
, char *argv
[])
959 while (iArgPtr
< argc
&& bReturn
)
961 if (argc
>= iArgPtr
+ 1)
963 if (!strcmp(argv
[iArgPtr
], "-f") ||
964 !strcmp(argv
[iArgPtr
], "--log-file") ||
965 !strcmp(argv
[iArgPtr
], "-sf") ||
966 !strcmp(argv
[iArgPtr
], "--short-log-file"))
968 if (argc
>= iArgPtr
+ 2)
970 g_logOutput
.open(argv
[iArgPtr
+ 1]);
971 g_bShortLog
= (!strcmp(argv
[iArgPtr
], "-sf") || !strcmp(argv
[iArgPtr
], "--short-log-file"));
976 cout
<< "== skipped log-file parameter: no file given ==" << endl
;
980 else if (!strcmp(argv
[iArgPtr
], "-d") ||
981 !strcmp(argv
[iArgPtr
], "--log-level"))
983 if (argc
>= iArgPtr
+ 2)
985 int iNewLevel
= atoi(argv
[iArgPtr
+ 1]);
986 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
988 g_cecLogLevel
= iNewLevel
;
989 if (!g_bSingleCommand
)
990 cout
<< "log level set to " << argv
[iArgPtr
+ 1] << endl
;
994 cout
<< "== skipped log-level parameter: invalid level '" << argv
[iArgPtr
+ 1] << "' ==" << endl
;
1000 cout
<< "== skipped log-level parameter: no level given ==" << endl
;
1004 else if (!strcmp(argv
[iArgPtr
], "-t") ||
1005 !strcmp(argv
[iArgPtr
], "--type"))
1007 if (argc
>= iArgPtr
+ 2)
1009 if (!strcmp(argv
[iArgPtr
+ 1], "p"))
1011 if (!g_bSingleCommand
)
1012 cout
<< "== using device type 'playback device'" << endl
;
1013 g_config
.deviceTypes
.Add(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
1015 else if (!strcmp(argv
[iArgPtr
+ 1], "r"))
1017 if (!g_bSingleCommand
)
1018 cout
<< "== using device type 'recording device'" << endl
;
1019 g_config
.deviceTypes
.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
1021 else if (!strcmp(argv
[iArgPtr
+ 1], "t"))
1023 if (!g_bSingleCommand
)
1024 cout
<< "== using device type 'tuner'" << endl
;
1025 g_config
.deviceTypes
.Add(CEC_DEVICE_TYPE_TUNER
);
1027 else if (!strcmp(argv
[iArgPtr
+ 1], "a"))
1029 if (!g_bSingleCommand
)
1030 cout
<< "== using device type 'audio system'" << endl
;
1031 g_config
.deviceTypes
.Add(CEC_DEVICE_TYPE_AUDIO_SYSTEM
);
1035 cout
<< "== skipped invalid device type '" << argv
[iArgPtr
+ 1] << "'" << endl
;
1041 else if (!strcmp(argv
[iArgPtr
], "--info") ||
1042 !strcmp(argv
[iArgPtr
], "-i"))
1044 if (g_cecLogLevel
== -1)
1045 g_cecLogLevel
= CEC_LOG_WARNING
+ CEC_LOG_ERROR
;
1046 ICECAdapter
*parser
= LibCecInitialise(&g_config
);
1049 CStdString strMessage
;
1050 strMessage
.Format("libCEC version: %s", parser
->ToString((cec_server_version
)g_config
.serverVersion
));
1051 if (g_config
.serverVersion
>= CEC_SERVER_VERSION_1_7_2
)
1052 strMessage
.AppendFormat(", %s", parser
->GetLibInfo());
1053 PrintToStdOut(strMessage
.c_str());
1054 UnloadLibCec(parser
);
1059 else if (!strcmp(argv
[iArgPtr
], "--list-devices") ||
1060 !strcmp(argv
[iArgPtr
], "-l"))
1062 if (g_cecLogLevel
== -1)
1063 g_cecLogLevel
= CEC_LOG_WARNING
+ CEC_LOG_ERROR
;
1064 ICECAdapter
*parser
= LibCecInitialise(&g_config
);
1067 ListDevices(parser
);
1068 UnloadLibCec(parser
);
1073 else if (!strcmp(argv
[iArgPtr
], "--bootloader"))
1078 else if (!strcmp(argv
[iArgPtr
], "--single-command") ||
1079 !strcmp(argv
[iArgPtr
], "-s"))
1081 g_bSingleCommand
= true;
1084 else if (!strcmp(argv
[iArgPtr
], "--help") ||
1085 !strcmp(argv
[iArgPtr
], "-h"))
1087 if (g_cecLogLevel
== -1)
1088 g_cecLogLevel
= CEC_LOG_WARNING
+ CEC_LOG_ERROR
;
1090 ShowHelpCommandLine(argv
[0]);
1093 else if (!strcmp(argv
[iArgPtr
], "-b") ||
1094 !strcmp(argv
[iArgPtr
], "--base"))
1096 if (argc
>= iArgPtr
+ 2)
1098 g_config
.baseDevice
= (cec_logical_address
)atoi(argv
[iArgPtr
+ 1]);
1099 cout
<< "using base device '" << (int)g_config
.baseDevice
<< "'" << endl
;
1104 else if (!strcmp(argv
[iArgPtr
], "-p") ||
1105 !strcmp(argv
[iArgPtr
], "--port"))
1107 if (argc
>= iArgPtr
+ 2)
1109 uint8_t hdmiport
= (int8_t)atoi(argv
[iArgPtr
+ 1]);
1114 g_config
.iHDMIPort
= hdmiport
;
1115 cout
<< "using HDMI port '" << (int)g_config
.iHDMIPort
<< "'" << endl
;
1120 else if (!strcmp(argv
[iArgPtr
], "-r") ||
1121 !strcmp(argv
[iArgPtr
], "--rom"))
1123 cout
<< "using settings from EEPROM" << endl
;
1124 g_config
.bGetSettingsFromROM
= 1;
1127 else if (!strcmp(argv
[iArgPtr
], "-o") ||
1128 !strcmp(argv
[iArgPtr
], "--osd-name"))
1130 if (argc
>= iArgPtr
+ 2)
1132 snprintf(g_config
.strDeviceName
, 13, "%s", argv
[iArgPtr
+ 1]);
1133 cout
<< "using osd name " << g_config
.strDeviceName
<< endl
;
1138 else if (!strcmp(argv
[iArgPtr
], "-m") ||
1139 !strcmp(argv
[iArgPtr
], "--monitor"))
1141 cout
<< "starting a monitor-only client. use 'mon 0' to switch to normal mode" << endl
;
1142 g_config
.bMonitorOnly
= 1;
1147 g_strPort
= argv
[iArgPtr
++];
1155 void sighandler(int iSignal
)
1157 PrintToStdOut("signal caught: %d - exiting", iSignal
);
1161 int main (int argc
, char *argv
[])
1163 if (signal(SIGINT
, sighandler
) == SIG_ERR
)
1165 PrintToStdOut("can't register sighandler");
1170 g_callbacks
.Clear();
1171 snprintf(g_config
.strDeviceName
, 13, "CECTester");
1172 g_config
.clientVersion
= CEC_CONFIG_VERSION
;
1173 g_config
.bActivateSource
= 0;
1174 g_callbacks
.CBCecLogMessage
= &CecLogMessage
;
1175 g_callbacks
.CBCecKeyPress
= &CecKeyPress
;
1176 g_callbacks
.CBCecCommand
= &CecCommand
;
1177 g_callbacks
.CBCecAlert
= &CecAlert
;
1178 g_config
.callbacks
= &g_callbacks
;
1180 if (!ProcessCommandLineArguments(argc
, argv
))
1183 if (g_cecLogLevel
== -1)
1184 g_cecLogLevel
= g_cecDefaultLogLevel
;
1186 if (g_config
.deviceTypes
.IsEmpty())
1188 if (!g_bSingleCommand
)
1189 cout
<< "No device type given. Using 'recording device'" << endl
;
1190 g_config
.deviceTypes
.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
1193 ICECAdapter
*parser
= LibCecInitialise(&g_config
);
1197 cout
<< "Cannot load libcec.dll" << endl
;
1199 cout
<< "Cannot load libcec.so" << endl
;
1203 UnloadLibCec(parser
);
1208 // init video on targets that need this
1209 parser
->InitVideoStandalone();
1211 if (!g_bSingleCommand
)
1214 strLog
.Format("CEC Parser created - libCEC version %s", parser
->ToString((cec_server_version
)g_config
.serverVersion
));
1215 cout
<< strLog
.c_str() << endl
;
1217 //make stdin non-blocking
1219 int flags
= fcntl(0, F_GETFL
, 0);
1220 flags
|= O_NONBLOCK
;
1221 fcntl(0, F_SETFL
, flags
);
1225 if (g_strPort
.IsEmpty())
1227 if (!g_bSingleCommand
)
1228 cout
<< "no serial port given. trying autodetect: ";
1229 cec_adapter devices
[10];
1230 uint8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
1231 if (iDevicesFound
<= 0)
1233 if (g_bSingleCommand
)
1234 cout
<< "autodetect ";
1235 cout
<< "FAILED" << endl
;
1236 UnloadLibCec(parser
);
1241 if (!g_bSingleCommand
)
1243 cout
<< endl
<< " path: " << devices
[0].path
<< endl
<<
1244 " com port: " << devices
[0].comm
<< endl
<< endl
;
1246 g_strPort
= devices
[0].comm
;
1250 PrintToStdOut("opening a connection to the CEC adapter...");
1252 if (!parser
->Open(g_strPort
.c_str()))
1254 PrintToStdOut("unable to open the device on port %s", g_strPort
.c_str());
1255 UnloadLibCec(parser
);
1259 if (!g_bSingleCommand
)
1260 PrintToStdOut("waiting for input");
1262 while (!g_bExit
&& !g_bHardExit
)
1265 getline(cin
, input
);
1268 if (ProcessConsoleCommand(parser
, input
) && !g_bSingleCommand
&& !g_bExit
&& !g_bHardExit
)
1271 PrintToStdOut("waiting for input");
1276 if (!g_bExit
&& !g_bHardExit
)
1281 UnloadLibCec(parser
);
1283 if (g_logOutput
.is_open())
1284 g_logOutput
.close();