2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2012 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/
33 #include "../../include/cec.h"
41 #include "../lib/platform/os.h"
42 #include "../lib/implementations/CECCommandHandler.h"
46 using namespace PLATFORM
;
48 #define CEC_CONFIG_VERSION CEC_CLIENT_VERSION_1_7_0;
50 #include <cecloader.h>
52 ICECCallbacks g_callbacks
;
53 libcec_configuration g_config
;
54 int g_cecLogLevel(CEC_LOG_ALL
);
56 bool g_bShortLog(false);
58 bool g_bSingleCommand(false);
60 bool g_bHardExit(false);
63 inline void PrintToStdOut(const char *strFormat
, ...)
68 va_start(argList
, strFormat
);
69 strLog
.FormatV(strFormat
, argList
);
72 CLockObject
lock(g_outputMutex
);
73 cout
<< strLog
<< endl
;
76 inline bool HexStrToInt(const std::string
& data
, uint8_t& value
)
79 if (sscanf(data
.c_str(), "%x", &iTmp
) == 1)
86 value
= (uint8_t) iTmp
;
94 //get the first word (separated by whitespace) from string data and place that in word
95 //then remove that word from string data
96 bool GetWord(string
& data
, string
& word
)
98 stringstream
datastream(data
);
102 if (datastream
.fail())
108 size_t pos
= data
.find(word
) + word
.length();
110 if (pos
>= data
.length())
116 data
= data
.substr(pos
);
119 datastream
.str(data
);
122 if (datastream
.fail())
128 int CecLogMessage(void *UNUSED(cbParam
), const cec_log_message
&message
)
130 if ((message
.level
& g_cecLogLevel
) == message
.level
)
133 switch (message
.level
)
136 strLevel
= "ERROR: ";
138 case CEC_LOG_WARNING
:
139 strLevel
= "WARNING: ";
142 strLevel
= "NOTICE: ";
144 case CEC_LOG_TRAFFIC
:
145 strLevel
= "TRAFFIC: ";
148 strLevel
= "DEBUG: ";
154 CStdString strFullLog
;
155 strFullLog
.Format("%s[%16lld]\t%s", strLevel
.c_str(), message
.time
, message
.message
);
156 PrintToStdOut(strFullLog
.c_str());
158 if (g_logOutput
.is_open())
161 g_logOutput
<< message
.message
<< endl
;
163 g_logOutput
<< strFullLog
.c_str() << endl
;
170 int CecKeyPress(void *UNUSED(cbParam
), const cec_keypress
&UNUSED(key
))
175 int CecCommand(void *UNUSED(cbParam
), const cec_command
&UNUSED(command
))
180 int CecAlert(void *UNUSED(cbParam
), const libcec_alert type
, const libcec_parameter
&UNUSED(param
))
184 case CEC_ALERT_CONNECTION_LOST
:
185 PrintToStdOut("Connection lost - exiting\n");
194 void ListDevices(ICECAdapter
*parser
)
196 cec_adapter
*devices
= new cec_adapter
[10];
197 int8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
198 if (iDevicesFound
<= 0)
200 PrintToStdOut("Found devices: NONE");
204 CStdString strDeviceInfo
;
205 strDeviceInfo
.Format("Found devices: %d\n\n", iDevicesFound
);
207 for (int8_t iDevicePtr
= 0; iDevicePtr
< iDevicesFound
; iDevicePtr
++)
209 strDeviceInfo
.AppendFormat("device: %d\ncom port: %s\n", iDevicePtr
+ 1, devices
[iDevicePtr
].comm
);
210 libcec_configuration config
;
213 if (!parser
->GetDeviceInformation(devices
[iDevicePtr
].comm
, &config
))
214 PrintToStdOut("WARNING: unable to open the device on port %s", devices
[iDevicePtr
].comm
);
217 strDeviceInfo
.AppendFormat("firmware version: %d\n", config
.iFirmwareVersion
);
219 if (config
.iFirmwareBuildDate
!= CEC_FW_BUILD_UNKNOWN
)
221 time_t buildTime
= (time_t)config
.iFirmwareBuildDate
;
222 strDeviceInfo
.AppendFormat("firmware build date: %s", asctime(gmtime(&buildTime
)));
223 strDeviceInfo
= strDeviceInfo
.Left((int)strDeviceInfo
.length() - 1); // strip \n added by asctime
224 strDeviceInfo
.append(" +0000");
227 strDeviceInfo
.append("\n");
229 PrintToStdOut(strDeviceInfo
.c_str());
233 void ShowHelpCommandLine(const char* strExec
)
235 CLockObject
lock(g_outputMutex
);
237 strExec
<< " {-h|--help|-l|--list-devices|[COM PORT]}" << endl
<<
239 "parameters:" << endl
<<
240 " -h --help Shows this help text" << endl
<<
241 " -l --list-devices List all devices on this system" << endl
<<
242 " -t --type {p|r|t|a} The device type to use. More than one is possible." << endl
<<
243 " -p --port {int} The HDMI port to use as active source." << endl
<<
244 " -b --base {int} The logical address of the device to with this " << endl
<<
245 " adapter is connected." << endl
<<
246 " -f --log-file {file} Writes all libCEC log message to a file" << endl
<<
247 " -r --rom Read persisted settings from the EEPROM" << endl
<<
248 " -sf --short-log-file {file} Writes all libCEC log message without timestamps" << endl
<<
249 " and log levels to a file." << endl
<<
250 " -d --log-level {level} Sets the log level. See cectypes.h for values." << endl
<<
251 " -s --single-command Execute a single command and exit. Does not power" << endl
<<
252 " on devices on startup and power them off on exit." << endl
<<
253 " -o --osd-name {osd name} Use a custom osd name." << endl
<<
254 " -m --monitor Start a monitor-only client." << endl
<<
255 " [COM PORT] The com port to connect to. If no COM" << endl
<<
256 " port is given, the client tries to connect to the" << endl
<<
257 " first device that is detected." << endl
<<
259 "Type 'h' or 'help' and press enter after starting the client to display all " << endl
<<
260 "available commands" << endl
;
263 void ShowHelpConsole(void)
265 CLockObject
lock(g_outputMutex
);
267 "================================================================================" << endl
<<
268 "Available commands:" << endl
<<
270 "[tx] {bytes} transfer bytes over the CEC line." << endl
<<
271 "[txn] {bytes} transfer bytes but don't wait for transmission ACK." << endl
<<
272 "[on] {address} power on the device with the given logical address." << endl
<<
273 "[standby] {address} put the device with the given address in standby mode." << endl
<<
274 "[la] {logical address} change the logical address of the CEC adapter." << endl
<<
275 "[p] {device} {port} change the HDMI port number of the CEC adapter." << endl
<<
276 "[pa] {physical address} change the physical address of the CEC adapter." << endl
<<
277 "[as] make the CEC adapter the active source." << endl
<<
278 "[osd] {addr} {string} set OSD message on the specified device." << endl
<<
279 "[ver] {addr} get the CEC version of the specified device." << endl
<<
280 "[ven] {addr} get the vendor ID of the specified device." << endl
<<
281 "[lang] {addr} get the menu language of the specified device." << endl
<<
282 "[pow] {addr} get the power status of the specified device." << endl
<<
283 "[name] {addr} get the OSD name of the specified device." << endl
<<
284 "[poll] {addr} poll the specified device." << endl
<<
285 "[lad] lists active devices on the bus" << endl
<<
286 "[ad] {addr} checks whether the specified device is active." << endl
<<
287 "[at] {type} checks whether the specified device type is active." << endl
<<
288 "[sp] {addr} makes the specified physical address active." << endl
<<
289 "[spl] {addr} makes the specified logical address active." << endl
<<
290 "[volup] send a volume up command to the amp if present" << endl
<<
291 "[voldown] send a volume down command to the amp if present" << endl
<<
292 "[mute] send a mute/unmute command to the amp if present" << endl
<<
293 "[self] show the list of addresses controlled by libCEC" << endl
<<
294 "[scan] scan the CEC bus and display device info" << endl
<<
295 "[mon] {1|0} enable or disable CEC bus monitoring." << endl
<<
296 "[log] {1 - 31} change the log level. see cectypes.h for values." << endl
<<
297 "[ping] send a ping command to the CEC adapter." << endl
<<
298 "[bl] to let the adapter enter the bootloader, to upgrade" << endl
<<
299 " the flash rom." << endl
<<
300 "[r] reconnect to the CEC adapter." << endl
<<
301 "[h] or [help] show this help." << endl
<<
302 "[q] or [quit] to quit the CEC test client and switch off all" << endl
<<
303 " connected CEC devices." << endl
<<
304 "================================================================================" << endl
;
307 bool ProcessCommandSELF(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
309 if (command
== "self")
311 cec_logical_addresses addr
= parser
->GetLogicalAddresses();
312 CStdString strOut
= "Addresses controlled by libCEC: ";
314 for (uint8_t iPtr
= 0; iPtr
<= 15; iPtr
++)
318 strOut
.AppendFormat((bFirst
? "%d%s" : ", %d%s"), iPtr
, parser
->IsActiveSource((cec_logical_address
)iPtr
) ? "*" : "");
322 PrintToStdOut(strOut
.c_str());
329 bool ProcessCommandSP(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
335 if (GetWord(arguments
, strAddress
))
337 sscanf(strAddress
.c_str(), "%x", &iAddress
);
338 if (iAddress
>= 0 && iAddress
<= CEC_INVALID_PHYSICAL_ADDRESS
)
339 parser
->SetStreamPath((uint16_t)iAddress
);
347 bool ProcessCommandSPL(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
349 if (command
== "spl")
352 cec_logical_address iAddress
;
353 if (GetWord(arguments
, strAddress
))
355 iAddress
= (cec_logical_address
)atoi(strAddress
.c_str());
356 if (iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
357 parser
->SetStreamPath(iAddress
);
365 bool ProcessCommandTX(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
367 if (command
== "tx" || command
== "txn")
374 while (GetWord(arguments
, strvalue
) && HexStrToInt(strvalue
, ivalue
))
375 bytes
.PushBack(ivalue
);
377 if (command
== "txn")
378 bytes
.transmit_timeout
= 0;
380 parser
->Transmit(bytes
);
388 bool ProcessCommandON(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
394 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
396 parser
->PowerOnDevices((cec_logical_address
) iValue
);
401 PrintToStdOut("invalid destination");
408 bool ProcessCommandSTANDBY(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
410 if (command
== "standby")
414 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
416 parser
->StandbyDevices((cec_logical_address
) iValue
);
421 PrintToStdOut("invalid destination");
428 bool ProcessCommandPOLL(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
430 if (command
== "poll")
434 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
436 if (parser
->PollDevice((cec_logical_address
) iValue
))
437 PrintToStdOut("POLL message sent");
439 PrintToStdOut("POLL message not sent");
444 PrintToStdOut("invalid destination");
451 bool ProcessCommandLA(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
456 if (GetWord(arguments
, strvalue
))
458 parser
->SetLogicalAddress((cec_logical_address
) atoi(strvalue
.c_str()));
466 bool ProcessCommandP(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
470 string strPort
, strDevice
;
471 if (GetWord(arguments
, strDevice
) && GetWord(arguments
, strPort
))
473 parser
->SetHDMIPort((cec_logical_address
)atoi(strDevice
.c_str()), (uint8_t)atoi(strPort
.c_str()));
481 bool ProcessCommandPA(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
487 if (GetWord(arguments
, strB1
) && HexStrToInt(strB1
, iB1
) &&
488 GetWord(arguments
, strB2
) && HexStrToInt(strB2
, iB2
))
490 uint16_t iPhysicalAddress
= ((uint16_t)iB1
<< 8) + iB2
;
491 parser
->SetPhysicalAddress(iPhysicalAddress
);
499 bool ProcessCommandOSD(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
501 if (command
== "osd")
503 bool bFirstWord(false);
504 string strAddr
, strMessage
, strWord
;
506 if (GetWord(arguments
, strAddr
) && HexStrToInt(strAddr
, iAddr
) && iAddr
< 0xF)
508 while (GetWord(arguments
, strWord
))
513 strMessage
.append(" ");
515 strMessage
.append(strWord
);
517 parser
->SetOSDString((cec_logical_address
) iAddr
, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME
, strMessage
.c_str());
525 bool ProcessCommandAS(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
529 parser
->SetActiveSource();
537 bool ProcessCommandPING(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
539 if (command
== "ping")
541 parser
->PingAdapter();
548 bool ProcessCommandVOLUP(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
550 if (command
== "volup")
552 PrintToStdOut("volume up: %2X", parser
->VolumeUp());
559 bool ProcessCommandVOLDOWN(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
561 if (command
== "voldown")
563 PrintToStdOut("volume down: %2X", parser
->VolumeDown());
570 bool ProcessCommandMUTE(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
572 if (command
== "mute")
574 PrintToStdOut("mute: %2X", parser
->MuteAudio());
581 bool ProcessCommandMON(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
583 if (command
== "mon")
585 CStdString strEnable
;
586 if (GetWord(arguments
, strEnable
) && (strEnable
.Equals("0") || strEnable
.Equals("1")))
588 parser
->SwitchMonitoring(strEnable
.Equals("1"));
596 bool ProcessCommandBL(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
600 if (parser
->StartBootloader())
602 PrintToStdOut("entered bootloader mode. exiting cec-client");
612 bool ProcessCommandLANG(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
614 if (command
== "lang")
617 if (GetWord(arguments
, strDev
))
619 int iDev
= atoi(strDev
);
620 if (iDev
>= 0 && iDev
< 15)
623 cec_menu_language language
;
624 if (parser
->GetDeviceMenuLanguage((cec_logical_address
) iDev
, &language
))
625 strLog
.Format("menu language '%s'", language
.language
);
628 PrintToStdOut(strLog
);
637 bool ProcessCommandVEN(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
639 if (command
== "ven")
642 if (GetWord(arguments
, strDev
))
644 int iDev
= atoi(strDev
);
645 if (iDev
>= 0 && iDev
< 15)
647 uint64_t iVendor
= parser
->GetDeviceVendorId((cec_logical_address
) iDev
);
648 PrintToStdOut("vendor id: %06x", iVendor
);
657 bool ProcessCommandVER(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
659 if (command
== "ver")
662 if (GetWord(arguments
, strDev
))
664 int iDev
= atoi(strDev
);
665 if (iDev
>= 0 && iDev
< 15)
667 cec_version iVersion
= parser
->GetDeviceCecVersion((cec_logical_address
) iDev
);
668 PrintToStdOut("CEC version %s", parser
->ToString(iVersion
));
677 bool ProcessCommandPOW(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
679 if (command
== "pow")
682 if (GetWord(arguments
, strDev
))
684 int iDev
= atoi(strDev
);
685 if (iDev
>= 0 && iDev
< 15)
687 cec_power_status iPower
= parser
->GetDevicePowerStatus((cec_logical_address
) iDev
);
688 PrintToStdOut("power status: %s", parser
->ToString(iPower
));
697 bool ProcessCommandNAME(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
699 if (command
== "name")
702 if (GetWord(arguments
, strDev
))
704 int iDev
= atoi(strDev
);
705 if (iDev
>= 0 && iDev
< 15)
707 cec_osd_name name
= parser
->GetDeviceOSDName((cec_logical_address
)iDev
);
708 PrintToStdOut("OSD name of device %d is '%s'", iDev
, name
.name
);
717 bool ProcessCommandLAD(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
719 if (command
== "lad")
721 PrintToStdOut("listing active devices:");
722 cec_logical_addresses addresses
= parser
->GetActiveDevices();
723 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
726 PrintToStdOut("logical address %X", (int)iPtr
);
734 bool ProcessCommandAD(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
739 if (GetWord(arguments
, strDev
))
741 int iDev
= atoi(strDev
);
742 if (iDev
>= 0 && iDev
< 15)
743 PrintToStdOut("logical address %X is %s", iDev
, (parser
->IsActiveDevice((cec_logical_address
)iDev
) ? "active" : "not active"));
750 bool ProcessCommandAT(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
755 if (GetWord(arguments
, strType
))
757 cec_device_type type
= CEC_DEVICE_TYPE_TV
;
758 if (strType
.Equals("a"))
759 type
= CEC_DEVICE_TYPE_AUDIO_SYSTEM
;
760 else if (strType
.Equals("p"))
761 type
= CEC_DEVICE_TYPE_PLAYBACK_DEVICE
;
762 else if (strType
.Equals("r"))
763 type
= CEC_DEVICE_TYPE_RECORDING_DEVICE
;
764 else if (strType
.Equals("t"))
765 type
= CEC_DEVICE_TYPE_TUNER
;
767 PrintToStdOut("device %d is %s", type
, (parser
->IsActiveDeviceType(type
) ? "active" : "not active"));
775 bool ProcessCommandR(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
779 bool bReactivate
= parser
->IsLibCECActiveSource();
781 PrintToStdOut("closing the connection");
784 PrintToStdOut("opening a new connection");
785 parser
->Open(g_strPort
.c_str());
789 PrintToStdOut("setting active source");
790 parser
->SetActiveSource();
798 bool ProcessCommandH(ICECAdapter
* UNUSED(parser
), const string
&command
, string
& UNUSED(arguments
))
800 if (command
== "h" || command
== "help")
809 bool ProcessCommandLOG(ICECAdapter
* UNUSED(parser
), const string
&command
, string
&arguments
)
811 if (command
== "log")
814 if (GetWord(arguments
, strLevel
))
816 int iNewLevel
= atoi(strLevel
);
817 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
819 g_cecLogLevel
= iNewLevel
;
821 PrintToStdOut("log level changed to %s", strLevel
.c_str());
830 bool ProcessCommandSCAN(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
832 if (command
== "scan")
835 PrintToStdOut("requesting CEC bus information ...");
837 strLog
.append("CEC bus information\n===================\n");
838 cec_logical_addresses addresses
= parser
->GetActiveDevices();
839 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
843 uint64_t iVendorId
= parser
->GetDeviceVendorId((cec_logical_address
)iPtr
);
844 bool bActive
= parser
->IsActiveSource((cec_logical_address
)iPtr
);
845 uint16_t iPhysicalAddress
= parser
->GetDevicePhysicalAddress((cec_logical_address
)iPtr
);
846 cec_version iCecVersion
= parser
->GetDeviceCecVersion((cec_logical_address
)iPtr
);
847 cec_power_status power
= parser
->GetDevicePowerStatus((cec_logical_address
)iPtr
);
848 cec_osd_name osdName
= parser
->GetDeviceOSDName((cec_logical_address
)iPtr
);
850 strAddr
.Format("%04x", iPhysicalAddress
);
851 cec_menu_language lang
;
852 lang
.device
= CECDEVICE_UNKNOWN
;
853 parser
->GetDeviceMenuLanguage((cec_logical_address
)iPtr
, &lang
);
855 strLog
.AppendFormat("device #%X: %s\n", (int)iPtr
, parser
->ToString((cec_logical_address
)iPtr
));
856 strLog
.AppendFormat("address: %s\n", strAddr
.c_str());
857 strLog
.AppendFormat("active source: %s\n", (bActive
? "yes" : "no"));
858 strLog
.AppendFormat("vendor: %s\n", parser
->ToString((cec_vendor_id
)iVendorId
));
859 strLog
.AppendFormat("osd string: %s\n", osdName
.name
);
860 strLog
.AppendFormat("CEC version: %s\n", parser
->ToString(iCecVersion
));
861 strLog
.AppendFormat("power status: %s\n", parser
->ToString(power
));
862 if ((uint8_t)lang
.device
== iPtr
)
863 strLog
.AppendFormat("language: %s\n", lang
.language
);
864 strLog
.append("\n\n");
868 cec_logical_address activeSource
= parser
->GetActiveSource();
869 strLog
.AppendFormat("currently active source: %s (%d)", parser
->ToString(activeSource
), (int)activeSource
);
871 PrintToStdOut(strLog
);
878 bool ProcessConsoleCommand(ICECAdapter
*parser
, string
&input
)
883 if (GetWord(input
, command
))
885 if (command
== "q" || command
== "quit")
888 ProcessCommandTX(parser
, command
, input
) ||
889 ProcessCommandON(parser
, command
, input
) ||
890 ProcessCommandSTANDBY(parser
, command
, input
) ||
891 ProcessCommandPOLL(parser
, command
, input
) ||
892 ProcessCommandLA(parser
, command
, input
) ||
893 ProcessCommandP(parser
, command
, input
) ||
894 ProcessCommandPA(parser
, command
, input
) ||
895 ProcessCommandAS(parser
, command
, input
) ||
896 ProcessCommandOSD(parser
, command
, input
) ||
897 ProcessCommandPING(parser
, command
, input
) ||
898 ProcessCommandVOLUP(parser
, command
, input
) ||
899 ProcessCommandVOLDOWN(parser
, command
, input
) ||
900 ProcessCommandMUTE(parser
, command
, input
) ||
901 ProcessCommandMON(parser
, command
, input
) ||
902 ProcessCommandBL(parser
, command
, input
) ||
903 ProcessCommandLANG(parser
, command
, input
) ||
904 ProcessCommandVEN(parser
, command
, input
) ||
905 ProcessCommandVER(parser
, command
, input
) ||
906 ProcessCommandPOW(parser
, command
, input
) ||
907 ProcessCommandNAME(parser
, command
, input
) ||
908 ProcessCommandLAD(parser
, command
, input
) ||
909 ProcessCommandAD(parser
, command
, input
) ||
910 ProcessCommandAT(parser
, command
, input
) ||
911 ProcessCommandR(parser
, command
, input
) ||
912 ProcessCommandH(parser
, command
, input
) ||
913 ProcessCommandLOG(parser
, command
, input
) ||
914 ProcessCommandSCAN(parser
, command
, input
) ||
915 ProcessCommandSP(parser
, command
, input
) ||
916 ProcessCommandSPL(parser
, command
, input
) ||
917 ProcessCommandSELF(parser
, command
, input
);
923 bool ProcessCommandLineArguments(int argc
, char *argv
[])
927 while (iArgPtr
< argc
&& bReturn
)
929 if (argc
>= iArgPtr
+ 1)
931 if (!strcmp(argv
[iArgPtr
], "-f") ||
932 !strcmp(argv
[iArgPtr
], "--log-file") ||
933 !strcmp(argv
[iArgPtr
], "-sf") ||
934 !strcmp(argv
[iArgPtr
], "--short-log-file"))
936 if (argc
>= iArgPtr
+ 2)
938 g_logOutput
.open(argv
[iArgPtr
+ 1]);
939 g_bShortLog
= (!strcmp(argv
[iArgPtr
], "-sf") || !strcmp(argv
[iArgPtr
], "--short-log-file"));
944 cout
<< "== skipped log-file parameter: no file given ==" << endl
;
948 else if (!strcmp(argv
[iArgPtr
], "-d") ||
949 !strcmp(argv
[iArgPtr
], "--log-level"))
951 if (argc
>= iArgPtr
+ 2)
953 int iNewLevel
= atoi(argv
[iArgPtr
+ 1]);
954 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
956 g_cecLogLevel
= iNewLevel
;
957 if (!g_bSingleCommand
)
958 cout
<< "log level set to " << argv
[iArgPtr
+ 1] << endl
;
962 cout
<< "== skipped log-level parameter: invalid level '" << argv
[iArgPtr
+ 1] << "' ==" << endl
;
968 cout
<< "== skipped log-level parameter: no level given ==" << endl
;
972 else if (!strcmp(argv
[iArgPtr
], "-t") ||
973 !strcmp(argv
[iArgPtr
], "--type"))
975 if (argc
>= iArgPtr
+ 2)
977 if (!strcmp(argv
[iArgPtr
+ 1], "p"))
979 if (!g_bSingleCommand
)
980 cout
<< "== using device type 'playback device'" << endl
;
981 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
983 else if (!strcmp(argv
[iArgPtr
+ 1], "r"))
985 if (!g_bSingleCommand
)
986 cout
<< "== using device type 'recording device'" << endl
;
987 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
989 else if (!strcmp(argv
[iArgPtr
+ 1], "t"))
991 if (!g_bSingleCommand
)
992 cout
<< "== using device type 'tuner'" << endl
;
993 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_TUNER
);
995 else if (!strcmp(argv
[iArgPtr
+ 1], "a"))
997 if (!g_bSingleCommand
)
998 cout
<< "== using device type 'audio system'" << endl
;
999 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_AUDIO_SYSTEM
);
1003 cout
<< "== skipped invalid device type '" << argv
[iArgPtr
+ 1] << "'" << endl
;
1009 else if (!strcmp(argv
[iArgPtr
], "--list-devices") ||
1010 !strcmp(argv
[iArgPtr
], "-l"))
1012 ICECAdapter
*parser
= LibCecInitialise(&g_config
);
1015 ListDevices(parser
);
1016 UnloadLibCec(parser
);
1021 else if (!strcmp(argv
[iArgPtr
], "--bootloader"))
1026 else if (!strcmp(argv
[iArgPtr
], "--single-command") ||
1027 !strcmp(argv
[iArgPtr
], "-s"))
1029 g_bSingleCommand
= true;
1032 else if (!strcmp(argv
[iArgPtr
], "--help") ||
1033 !strcmp(argv
[iArgPtr
], "-h"))
1035 ShowHelpCommandLine(argv
[0]);
1038 else if (!strcmp(argv
[iArgPtr
], "-b") ||
1039 !strcmp(argv
[iArgPtr
], "--base"))
1041 if (argc
>= iArgPtr
+ 2)
1043 g_config
.baseDevice
= (cec_logical_address
)atoi(argv
[iArgPtr
+ 1]);
1044 cout
<< "using base device '" << (int)g_config
.baseDevice
<< "'" << endl
;
1049 else if (!strcmp(argv
[iArgPtr
], "-p") ||
1050 !strcmp(argv
[iArgPtr
], "--port"))
1052 if (argc
>= iArgPtr
+ 2)
1054 uint8_t hdmiport
= (int8_t)atoi(argv
[iArgPtr
+ 1]);
1059 g_config
.iHDMIPort
= hdmiport
;
1060 cout
<< "using HDMI port '" << (int)g_config
.iHDMIPort
<< "'" << endl
;
1065 else if (!strcmp(argv
[iArgPtr
], "-r") ||
1066 !strcmp(argv
[iArgPtr
], "--rom"))
1068 cout
<< "using settings from EEPROM" << endl
;
1069 g_config
.bGetSettingsFromROM
= 1;
1072 else if (!strcmp(argv
[iArgPtr
], "-o") ||
1073 !strcmp(argv
[iArgPtr
], "--osd-name"))
1075 if (argc
>= iArgPtr
+ 2)
1077 snprintf(g_config
.strDeviceName
, 13, "%s", argv
[iArgPtr
+ 1]);
1078 cout
<< "using osd name " << g_config
.strDeviceName
<< endl
;
1083 else if (!strcmp(argv
[iArgPtr
], "-m") ||
1084 !strcmp(argv
[iArgPtr
], "--monitor"))
1086 cout
<< "starting a monitor-only client. use 'mon 0' to switch to normal mode" << endl
;
1087 g_config
.bMonitorOnly
= 1;
1092 g_strPort
= argv
[iArgPtr
++];
1100 int main (int argc
, char *argv
[])
1103 snprintf(g_config
.strDeviceName
, 13, "CECTester");
1104 g_config
.clientVersion
= CEC_CONFIG_VERSION
;
1105 g_config
.bActivateSource
= 0;
1106 g_callbacks
.CBCecLogMessage
= &CecLogMessage
;
1107 g_callbacks
.CBCecKeyPress
= &CecKeyPress
;
1108 g_callbacks
.CBCecCommand
= &CecCommand
;
1109 g_callbacks
.CBCecAlert
= &CecAlert
;
1110 g_config
.callbacks
= &g_callbacks
;
1112 if (!ProcessCommandLineArguments(argc
, argv
))
1115 if (g_config
.deviceTypes
.IsEmpty())
1117 if (!g_bSingleCommand
)
1118 cout
<< "No device type given. Using 'recording device'" << endl
;
1119 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
1122 ICECAdapter
*parser
= LibCecInitialise(&g_config
);
1126 cout
<< "Cannot load libcec.dll" << endl
;
1128 cout
<< "Cannot load libcec.so" << endl
;
1132 UnloadLibCec(parser
);
1137 if (!g_bSingleCommand
)
1140 strLog
.Format("CEC Parser created - libCEC version %s", parser
->ToString((cec_server_version
)g_config
.serverVersion
));
1141 cout
<< strLog
.c_str() << endl
;
1143 //make stdin non-blocking
1145 int flags
= fcntl(0, F_GETFL
, 0);
1146 flags
|= O_NONBLOCK
;
1147 fcntl(0, F_SETFL
, flags
);
1151 if (g_strPort
.IsEmpty())
1153 if (!g_bSingleCommand
)
1154 cout
<< "no serial port given. trying autodetect: ";
1155 cec_adapter devices
[10];
1156 uint8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
1157 if (iDevicesFound
<= 0)
1159 if (g_bSingleCommand
)
1160 cout
<< "autodetect ";
1161 cout
<< "FAILED" << endl
;
1162 UnloadLibCec(parser
);
1167 if (!g_bSingleCommand
)
1169 cout
<< endl
<< " path: " << devices
[0].path
<< endl
<<
1170 " com port: " << devices
[0].comm
<< endl
<< endl
;
1172 g_strPort
= devices
[0].comm
;
1176 PrintToStdOut("opening a connection to the CEC adapter...");
1178 if (!parser
->Open(g_strPort
.c_str()))
1180 PrintToStdOut("unable to open the device on port %s", g_strPort
.c_str());
1181 UnloadLibCec(parser
);
1185 if (!g_bSingleCommand
)
1186 PrintToStdOut("waiting for input");
1188 while (!g_bExit
&& !g_bHardExit
)
1191 getline(cin
, input
);
1194 if (ProcessConsoleCommand(parser
, input
) && !g_bSingleCommand
&& !g_bExit
&& !g_bHardExit
)
1197 PrintToStdOut("waiting for input");
1202 if (!g_bExit
&& !g_bHardExit
)
1207 UnloadLibCec(parser
);
1209 if (g_logOutput
.is_open())
1210 g_logOutput
.close();