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_1;
50 #include <cecloader.h>
52 ICECCallbacks g_callbacks
;
53 libcec_configuration g_config
;
54 int g_cecLogLevel(-1);
55 int g_cecDefaultLogLevel(CEC_LOG_ALL
);
57 bool g_bShortLog(false);
59 bool g_bSingleCommand(false);
61 bool g_bHardExit(false);
64 inline void PrintToStdOut(const char *strFormat
, ...)
69 va_start(argList
, strFormat
);
70 strLog
.FormatV(strFormat
, argList
);
73 CLockObject
lock(g_outputMutex
);
74 cout
<< strLog
<< endl
;
77 inline bool HexStrToInt(const std::string
& data
, uint8_t& value
)
80 if (sscanf(data
.c_str(), "%x", &iTmp
) == 1)
87 value
= (uint8_t) iTmp
;
95 //get the first word (separated by whitespace) from string data and place that in word
96 //then remove that word from string data
97 bool GetWord(string
& data
, string
& word
)
99 stringstream
datastream(data
);
103 if (datastream
.fail())
109 size_t pos
= data
.find(word
) + word
.length();
111 if (pos
>= data
.length())
117 data
= data
.substr(pos
);
120 datastream
.str(data
);
123 if (datastream
.fail())
129 int CecLogMessage(void *UNUSED(cbParam
), const cec_log_message
&message
)
131 if ((message
.level
& g_cecLogLevel
) == message
.level
)
134 switch (message
.level
)
137 strLevel
= "ERROR: ";
139 case CEC_LOG_WARNING
:
140 strLevel
= "WARNING: ";
143 strLevel
= "NOTICE: ";
145 case CEC_LOG_TRAFFIC
:
146 strLevel
= "TRAFFIC: ";
149 strLevel
= "DEBUG: ";
155 CStdString strFullLog
;
156 strFullLog
.Format("%s[%16lld]\t%s", strLevel
.c_str(), message
.time
, message
.message
);
157 PrintToStdOut(strFullLog
.c_str());
159 if (g_logOutput
.is_open())
162 g_logOutput
<< message
.message
<< endl
;
164 g_logOutput
<< strFullLog
.c_str() << endl
;
171 int CecKeyPress(void *UNUSED(cbParam
), const cec_keypress
&UNUSED(key
))
176 int CecCommand(void *UNUSED(cbParam
), const cec_command
&UNUSED(command
))
181 int CecAlert(void *UNUSED(cbParam
), const libcec_alert type
, const libcec_parameter
&UNUSED(param
))
185 case CEC_ALERT_CONNECTION_LOST
:
186 PrintToStdOut("Connection lost - exiting\n");
195 void ListDevices(ICECAdapter
*parser
)
197 cec_adapter
*devices
= new cec_adapter
[10];
198 int8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
199 if (iDevicesFound
<= 0)
201 PrintToStdOut("Found devices: NONE");
205 CStdString strDeviceInfo
;
206 strDeviceInfo
.Format("Found devices: %d\n\n", iDevicesFound
);
208 for (int8_t iDevicePtr
= 0; iDevicePtr
< iDevicesFound
; iDevicePtr
++)
210 strDeviceInfo
.AppendFormat("device: %d\ncom port: %s\n", iDevicePtr
+ 1, devices
[iDevicePtr
].comm
);
211 libcec_configuration config
;
214 if (!parser
->GetDeviceInformation(devices
[iDevicePtr
].comm
, &config
))
215 PrintToStdOut("WARNING: unable to open the device on port %s", devices
[iDevicePtr
].comm
);
218 strDeviceInfo
.AppendFormat("firmware version: %d\n", config
.iFirmwareVersion
);
220 if (config
.iFirmwareBuildDate
!= CEC_FW_BUILD_UNKNOWN
)
222 time_t buildTime
= (time_t)config
.iFirmwareBuildDate
;
223 strDeviceInfo
.AppendFormat("firmware build date: %s", asctime(gmtime(&buildTime
)));
224 strDeviceInfo
= strDeviceInfo
.Left((int)strDeviceInfo
.length() - 1); // strip \n added by asctime
225 strDeviceInfo
.append(" +0000");
228 strDeviceInfo
.append("\n");
230 PrintToStdOut(strDeviceInfo
.c_str());
234 void ShowHelpCommandLine(const char* strExec
)
236 CLockObject
lock(g_outputMutex
);
238 strExec
<< " {-h|--help|-l|--list-devices|[COM PORT]}" << endl
<<
240 "parameters:" << endl
<<
241 " -h --help Shows this help text" << endl
<<
242 " -l --list-devices List all devices on this system" << endl
<<
243 " -t --type {p|r|t|a} The device type to use. More than one is possible." << endl
<<
244 " -p --port {int} The HDMI port to use as active source." << endl
<<
245 " -b --base {int} The logical address of the device to with this " << endl
<<
246 " adapter is connected." << endl
<<
247 " -f --log-file {file} Writes all libCEC log message to a file" << endl
<<
248 " -r --rom Read persisted settings from the EEPROM" << endl
<<
249 " -sf --short-log-file {file} Writes all libCEC log message without timestamps" << endl
<<
250 " and log levels to a file." << endl
<<
251 " -d --log-level {level} Sets the log level. See cectypes.h for values." << endl
<<
252 " -s --single-command Execute a single command and exit. Does not power" << endl
<<
253 " on devices on startup and power them off on exit." << endl
<<
254 " -o --osd-name {osd name} Use a custom osd name." << endl
<<
255 " -m --monitor Start a monitor-only client." << endl
<<
256 " [COM PORT] The com port to connect to. If no COM" << endl
<<
257 " port is given, the client tries to connect to the" << endl
<<
258 " first device that is detected." << endl
<<
260 "Type 'h' or 'help' and press enter after starting the client to display all " << endl
<<
261 "available commands" << endl
;
264 void ShowHelpConsole(void)
266 CLockObject
lock(g_outputMutex
);
268 "================================================================================" << endl
<<
269 "Available commands:" << endl
<<
271 "[tx] {bytes} transfer bytes over the CEC line." << endl
<<
272 "[txn] {bytes} transfer bytes but don't wait for transmission ACK." << endl
<<
273 "[on] {address} power on the device with the given logical address." << endl
<<
274 "[standby] {address} put the device with the given address in standby mode." << endl
<<
275 "[la] {logical address} change the logical address of the CEC adapter." << endl
<<
276 "[p] {device} {port} change the HDMI port number of the CEC adapter." << endl
<<
277 "[pa] {physical address} change the physical address of the CEC adapter." << endl
<<
278 "[as] make the CEC adapter the active source." << endl
<<
279 "[osd] {addr} {string} set OSD message on the specified device." << endl
<<
280 "[ver] {addr} get the CEC version of the specified device." << endl
<<
281 "[ven] {addr} get the vendor ID of the specified device." << endl
<<
282 "[lang] {addr} get the menu language of the specified device." << endl
<<
283 "[pow] {addr} get the power status of the specified device." << endl
<<
284 "[name] {addr} get the OSD name of the specified device." << endl
<<
285 "[poll] {addr} poll the specified device." << endl
<<
286 "[lad] lists active devices on the bus" << endl
<<
287 "[ad] {addr} checks whether the specified device is active." << endl
<<
288 "[at] {type} checks whether the specified device type is active." << endl
<<
289 "[sp] {addr} makes the specified physical address active." << endl
<<
290 "[spl] {addr} makes the specified logical address active." << endl
<<
291 "[volup] send a volume up command to the amp if present" << endl
<<
292 "[voldown] send a volume down command to the amp if present" << endl
<<
293 "[mute] send a mute/unmute command to the amp if present" << endl
<<
294 "[self] show the list of addresses controlled by libCEC" << endl
<<
295 "[scan] scan the CEC bus and display device info" << endl
<<
296 "[mon] {1|0} enable or disable CEC bus monitoring." << endl
<<
297 "[log] {1 - 31} change the log level. see cectypes.h for values." << endl
<<
298 "[ping] send a ping command to the CEC adapter." << endl
<<
299 "[bl] to let the adapter enter the bootloader, to upgrade" << endl
<<
300 " the flash rom." << endl
<<
301 "[r] reconnect to the CEC adapter." << endl
<<
302 "[h] or [help] show this help." << endl
<<
303 "[q] or [quit] to quit the CEC test client and switch off all" << endl
<<
304 " connected CEC devices." << endl
<<
305 "================================================================================" << endl
;
308 bool ProcessCommandSELF(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
310 if (command
== "self")
312 cec_logical_addresses addr
= parser
->GetLogicalAddresses();
313 CStdString strOut
= "Addresses controlled by libCEC: ";
315 for (uint8_t iPtr
= 0; iPtr
<= 15; iPtr
++)
319 strOut
.AppendFormat((bFirst
? "%d%s" : ", %d%s"), iPtr
, parser
->IsActiveSource((cec_logical_address
)iPtr
) ? "*" : "");
323 PrintToStdOut(strOut
.c_str());
330 bool ProcessCommandSP(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
336 if (GetWord(arguments
, strAddress
))
338 sscanf(strAddress
.c_str(), "%x", &iAddress
);
339 if (iAddress
>= 0 && iAddress
<= CEC_INVALID_PHYSICAL_ADDRESS
)
340 parser
->SetStreamPath((uint16_t)iAddress
);
348 bool ProcessCommandSPL(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
350 if (command
== "spl")
353 cec_logical_address iAddress
;
354 if (GetWord(arguments
, strAddress
))
356 iAddress
= (cec_logical_address
)atoi(strAddress
.c_str());
357 if (iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
358 parser
->SetStreamPath(iAddress
);
366 bool ProcessCommandTX(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
368 if (command
== "tx" || command
== "txn")
375 while (GetWord(arguments
, strvalue
) && HexStrToInt(strvalue
, ivalue
))
376 bytes
.PushBack(ivalue
);
378 if (command
== "txn")
379 bytes
.transmit_timeout
= 0;
381 parser
->Transmit(bytes
);
389 bool ProcessCommandON(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
395 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
397 parser
->PowerOnDevices((cec_logical_address
) iValue
);
402 PrintToStdOut("invalid destination");
409 bool ProcessCommandSTANDBY(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
411 if (command
== "standby")
415 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
417 parser
->StandbyDevices((cec_logical_address
) iValue
);
422 PrintToStdOut("invalid destination");
429 bool ProcessCommandPOLL(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
431 if (command
== "poll")
435 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
437 if (parser
->PollDevice((cec_logical_address
) iValue
))
438 PrintToStdOut("POLL message sent");
440 PrintToStdOut("POLL message not sent");
445 PrintToStdOut("invalid destination");
452 bool ProcessCommandLA(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
457 if (GetWord(arguments
, strvalue
))
459 parser
->SetLogicalAddress((cec_logical_address
) atoi(strvalue
.c_str()));
467 bool ProcessCommandP(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
471 string strPort
, strDevice
;
472 if (GetWord(arguments
, strDevice
) && GetWord(arguments
, strPort
))
474 parser
->SetHDMIPort((cec_logical_address
)atoi(strDevice
.c_str()), (uint8_t)atoi(strPort
.c_str()));
482 bool ProcessCommandPA(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
488 if (GetWord(arguments
, strB1
) && HexStrToInt(strB1
, iB1
) &&
489 GetWord(arguments
, strB2
) && HexStrToInt(strB2
, iB2
))
491 uint16_t iPhysicalAddress
= ((uint16_t)iB1
<< 8) + iB2
;
492 parser
->SetPhysicalAddress(iPhysicalAddress
);
500 bool ProcessCommandOSD(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
502 if (command
== "osd")
504 bool bFirstWord(false);
505 string strAddr
, strMessage
, strWord
;
507 if (GetWord(arguments
, strAddr
) && HexStrToInt(strAddr
, iAddr
) && iAddr
< 0xF)
509 while (GetWord(arguments
, strWord
))
514 strMessage
.append(" ");
516 strMessage
.append(strWord
);
518 parser
->SetOSDString((cec_logical_address
) iAddr
, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME
, strMessage
.c_str());
526 bool ProcessCommandAS(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
530 parser
->SetActiveSource();
538 bool ProcessCommandPING(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
540 if (command
== "ping")
542 parser
->PingAdapter();
549 bool ProcessCommandVOLUP(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
551 if (command
== "volup")
553 PrintToStdOut("volume up: %2X", parser
->VolumeUp());
560 bool ProcessCommandVOLDOWN(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
562 if (command
== "voldown")
564 PrintToStdOut("volume down: %2X", parser
->VolumeDown());
571 bool ProcessCommandMUTE(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
573 if (command
== "mute")
575 PrintToStdOut("mute: %2X", parser
->MuteAudio());
582 bool ProcessCommandMON(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
584 if (command
== "mon")
586 CStdString strEnable
;
587 if (GetWord(arguments
, strEnable
) && (strEnable
.Equals("0") || strEnable
.Equals("1")))
589 parser
->SwitchMonitoring(strEnable
.Equals("1"));
597 bool ProcessCommandBL(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
601 if (parser
->StartBootloader())
603 PrintToStdOut("entered bootloader mode. exiting cec-client");
613 bool ProcessCommandLANG(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
615 if (command
== "lang")
618 if (GetWord(arguments
, strDev
))
620 int iDev
= atoi(strDev
);
621 if (iDev
>= 0 && iDev
< 15)
624 cec_menu_language language
;
625 if (parser
->GetDeviceMenuLanguage((cec_logical_address
) iDev
, &language
))
626 strLog
.Format("menu language '%s'", language
.language
);
629 PrintToStdOut(strLog
);
638 bool ProcessCommandVEN(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
640 if (command
== "ven")
643 if (GetWord(arguments
, strDev
))
645 int iDev
= atoi(strDev
);
646 if (iDev
>= 0 && iDev
< 15)
648 uint64_t iVendor
= parser
->GetDeviceVendorId((cec_logical_address
) iDev
);
649 PrintToStdOut("vendor id: %06x", iVendor
);
658 bool ProcessCommandVER(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
660 if (command
== "ver")
663 if (GetWord(arguments
, strDev
))
665 int iDev
= atoi(strDev
);
666 if (iDev
>= 0 && iDev
< 15)
668 cec_version iVersion
= parser
->GetDeviceCecVersion((cec_logical_address
) iDev
);
669 PrintToStdOut("CEC version %s", parser
->ToString(iVersion
));
678 bool ProcessCommandPOW(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
680 if (command
== "pow")
683 if (GetWord(arguments
, strDev
))
685 int iDev
= atoi(strDev
);
686 if (iDev
>= 0 && iDev
< 15)
688 cec_power_status iPower
= parser
->GetDevicePowerStatus((cec_logical_address
) iDev
);
689 PrintToStdOut("power status: %s", parser
->ToString(iPower
));
698 bool ProcessCommandNAME(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
700 if (command
== "name")
703 if (GetWord(arguments
, strDev
))
705 int iDev
= atoi(strDev
);
706 if (iDev
>= 0 && iDev
< 15)
708 cec_osd_name name
= parser
->GetDeviceOSDName((cec_logical_address
)iDev
);
709 PrintToStdOut("OSD name of device %d is '%s'", iDev
, name
.name
);
718 bool ProcessCommandLAD(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
720 if (command
== "lad")
722 PrintToStdOut("listing active devices:");
723 cec_logical_addresses addresses
= parser
->GetActiveDevices();
724 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
727 PrintToStdOut("logical address %X", (int)iPtr
);
735 bool ProcessCommandAD(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
740 if (GetWord(arguments
, strDev
))
742 int iDev
= atoi(strDev
);
743 if (iDev
>= 0 && iDev
< 15)
744 PrintToStdOut("logical address %X is %s", iDev
, (parser
->IsActiveDevice((cec_logical_address
)iDev
) ? "active" : "not active"));
751 bool ProcessCommandAT(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
756 if (GetWord(arguments
, strType
))
758 cec_device_type type
= CEC_DEVICE_TYPE_TV
;
759 if (strType
.Equals("a"))
760 type
= CEC_DEVICE_TYPE_AUDIO_SYSTEM
;
761 else if (strType
.Equals("p"))
762 type
= CEC_DEVICE_TYPE_PLAYBACK_DEVICE
;
763 else if (strType
.Equals("r"))
764 type
= CEC_DEVICE_TYPE_RECORDING_DEVICE
;
765 else if (strType
.Equals("t"))
766 type
= CEC_DEVICE_TYPE_TUNER
;
768 PrintToStdOut("device %d is %s", type
, (parser
->IsActiveDeviceType(type
) ? "active" : "not active"));
776 bool ProcessCommandR(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
780 bool bReactivate
= parser
->IsLibCECActiveSource();
782 PrintToStdOut("closing the connection");
785 PrintToStdOut("opening a new connection");
786 parser
->Open(g_strPort
.c_str());
790 PrintToStdOut("setting active source");
791 parser
->SetActiveSource();
799 bool ProcessCommandH(ICECAdapter
* UNUSED(parser
), const string
&command
, string
& UNUSED(arguments
))
801 if (command
== "h" || command
== "help")
810 bool ProcessCommandLOG(ICECAdapter
* UNUSED(parser
), const string
&command
, string
&arguments
)
812 if (command
== "log")
815 if (GetWord(arguments
, strLevel
))
817 int iNewLevel
= atoi(strLevel
);
818 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
820 g_cecLogLevel
= iNewLevel
;
822 PrintToStdOut("log level changed to %s", strLevel
.c_str());
831 bool ProcessCommandSCAN(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
833 if (command
== "scan")
836 PrintToStdOut("requesting CEC bus information ...");
838 strLog
.append("CEC bus information\n===================\n");
839 cec_logical_addresses addresses
= parser
->GetActiveDevices();
840 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
844 uint64_t iVendorId
= parser
->GetDeviceVendorId((cec_logical_address
)iPtr
);
845 bool bActive
= parser
->IsActiveSource((cec_logical_address
)iPtr
);
846 uint16_t iPhysicalAddress
= parser
->GetDevicePhysicalAddress((cec_logical_address
)iPtr
);
847 cec_version iCecVersion
= parser
->GetDeviceCecVersion((cec_logical_address
)iPtr
);
848 cec_power_status power
= parser
->GetDevicePowerStatus((cec_logical_address
)iPtr
);
849 cec_osd_name osdName
= parser
->GetDeviceOSDName((cec_logical_address
)iPtr
);
851 strAddr
.Format("%04x", iPhysicalAddress
);
852 cec_menu_language lang
;
853 lang
.device
= CECDEVICE_UNKNOWN
;
854 parser
->GetDeviceMenuLanguage((cec_logical_address
)iPtr
, &lang
);
856 strLog
.AppendFormat("device #%X: %s\n", (int)iPtr
, parser
->ToString((cec_logical_address
)iPtr
));
857 strLog
.AppendFormat("address: %s\n", strAddr
.c_str());
858 strLog
.AppendFormat("active source: %s\n", (bActive
? "yes" : "no"));
859 strLog
.AppendFormat("vendor: %s\n", parser
->ToString((cec_vendor_id
)iVendorId
));
860 strLog
.AppendFormat("osd string: %s\n", osdName
.name
);
861 strLog
.AppendFormat("CEC version: %s\n", parser
->ToString(iCecVersion
));
862 strLog
.AppendFormat("power status: %s\n", parser
->ToString(power
));
863 if ((uint8_t)lang
.device
== iPtr
)
864 strLog
.AppendFormat("language: %s\n", lang
.language
);
865 strLog
.append("\n\n");
869 cec_logical_address activeSource
= parser
->GetActiveSource();
870 strLog
.AppendFormat("currently active source: %s (%d)", parser
->ToString(activeSource
), (int)activeSource
);
872 PrintToStdOut(strLog
);
879 bool ProcessConsoleCommand(ICECAdapter
*parser
, string
&input
)
884 if (GetWord(input
, command
))
886 if (command
== "q" || command
== "quit")
889 ProcessCommandTX(parser
, command
, input
) ||
890 ProcessCommandON(parser
, command
, input
) ||
891 ProcessCommandSTANDBY(parser
, command
, input
) ||
892 ProcessCommandPOLL(parser
, command
, input
) ||
893 ProcessCommandLA(parser
, command
, input
) ||
894 ProcessCommandP(parser
, command
, input
) ||
895 ProcessCommandPA(parser
, command
, input
) ||
896 ProcessCommandAS(parser
, command
, input
) ||
897 ProcessCommandOSD(parser
, command
, input
) ||
898 ProcessCommandPING(parser
, command
, input
) ||
899 ProcessCommandVOLUP(parser
, command
, input
) ||
900 ProcessCommandVOLDOWN(parser
, command
, input
) ||
901 ProcessCommandMUTE(parser
, command
, input
) ||
902 ProcessCommandMON(parser
, command
, input
) ||
903 ProcessCommandBL(parser
, command
, input
) ||
904 ProcessCommandLANG(parser
, command
, input
) ||
905 ProcessCommandVEN(parser
, command
, input
) ||
906 ProcessCommandVER(parser
, command
, input
) ||
907 ProcessCommandPOW(parser
, command
, input
) ||
908 ProcessCommandNAME(parser
, command
, input
) ||
909 ProcessCommandLAD(parser
, command
, input
) ||
910 ProcessCommandAD(parser
, command
, input
) ||
911 ProcessCommandAT(parser
, command
, input
) ||
912 ProcessCommandR(parser
, command
, input
) ||
913 ProcessCommandH(parser
, command
, input
) ||
914 ProcessCommandLOG(parser
, command
, input
) ||
915 ProcessCommandSCAN(parser
, command
, input
) ||
916 ProcessCommandSP(parser
, command
, input
) ||
917 ProcessCommandSPL(parser
, command
, input
) ||
918 ProcessCommandSELF(parser
, command
, input
);
924 bool ProcessCommandLineArguments(int argc
, char *argv
[])
928 while (iArgPtr
< argc
&& bReturn
)
930 if (argc
>= iArgPtr
+ 1)
932 if (!strcmp(argv
[iArgPtr
], "-f") ||
933 !strcmp(argv
[iArgPtr
], "--log-file") ||
934 !strcmp(argv
[iArgPtr
], "-sf") ||
935 !strcmp(argv
[iArgPtr
], "--short-log-file"))
937 if (argc
>= iArgPtr
+ 2)
939 g_logOutput
.open(argv
[iArgPtr
+ 1]);
940 g_bShortLog
= (!strcmp(argv
[iArgPtr
], "-sf") || !strcmp(argv
[iArgPtr
], "--short-log-file"));
945 cout
<< "== skipped log-file parameter: no file given ==" << endl
;
949 else if (!strcmp(argv
[iArgPtr
], "-d") ||
950 !strcmp(argv
[iArgPtr
], "--log-level"))
952 if (argc
>= iArgPtr
+ 2)
954 int iNewLevel
= atoi(argv
[iArgPtr
+ 1]);
955 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
957 g_cecLogLevel
= iNewLevel
;
958 if (!g_bSingleCommand
)
959 cout
<< "log level set to " << argv
[iArgPtr
+ 1] << endl
;
963 cout
<< "== skipped log-level parameter: invalid level '" << argv
[iArgPtr
+ 1] << "' ==" << endl
;
969 cout
<< "== skipped log-level parameter: no level given ==" << endl
;
973 else if (!strcmp(argv
[iArgPtr
], "-t") ||
974 !strcmp(argv
[iArgPtr
], "--type"))
976 if (argc
>= iArgPtr
+ 2)
978 if (!strcmp(argv
[iArgPtr
+ 1], "p"))
980 if (!g_bSingleCommand
)
981 cout
<< "== using device type 'playback device'" << endl
;
982 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
984 else if (!strcmp(argv
[iArgPtr
+ 1], "r"))
986 if (!g_bSingleCommand
)
987 cout
<< "== using device type 'recording device'" << endl
;
988 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
990 else if (!strcmp(argv
[iArgPtr
+ 1], "t"))
992 if (!g_bSingleCommand
)
993 cout
<< "== using device type 'tuner'" << endl
;
994 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_TUNER
);
996 else if (!strcmp(argv
[iArgPtr
+ 1], "a"))
998 if (!g_bSingleCommand
)
999 cout
<< "== using device type 'audio system'" << endl
;
1000 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_AUDIO_SYSTEM
);
1004 cout
<< "== skipped invalid device type '" << argv
[iArgPtr
+ 1] << "'" << endl
;
1010 else if (!strcmp(argv
[iArgPtr
], "--list-devices") ||
1011 !strcmp(argv
[iArgPtr
], "-l"))
1013 if (g_cecLogLevel
== -1)
1014 g_cecLogLevel
= CEC_LOG_WARNING
+ CEC_LOG_ERROR
;
1015 ICECAdapter
*parser
= LibCecInitialise(&g_config
);
1018 ListDevices(parser
);
1019 UnloadLibCec(parser
);
1024 else if (!strcmp(argv
[iArgPtr
], "--bootloader"))
1029 else if (!strcmp(argv
[iArgPtr
], "--single-command") ||
1030 !strcmp(argv
[iArgPtr
], "-s"))
1032 g_bSingleCommand
= true;
1035 else if (!strcmp(argv
[iArgPtr
], "--help") ||
1036 !strcmp(argv
[iArgPtr
], "-h"))
1038 if (g_cecLogLevel
== -1)
1039 g_cecLogLevel
= CEC_LOG_WARNING
+ CEC_LOG_ERROR
;
1041 ShowHelpCommandLine(argv
[0]);
1044 else if (!strcmp(argv
[iArgPtr
], "-b") ||
1045 !strcmp(argv
[iArgPtr
], "--base"))
1047 if (argc
>= iArgPtr
+ 2)
1049 g_config
.baseDevice
= (cec_logical_address
)atoi(argv
[iArgPtr
+ 1]);
1050 cout
<< "using base device '" << (int)g_config
.baseDevice
<< "'" << endl
;
1055 else if (!strcmp(argv
[iArgPtr
], "-p") ||
1056 !strcmp(argv
[iArgPtr
], "--port"))
1058 if (argc
>= iArgPtr
+ 2)
1060 uint8_t hdmiport
= (int8_t)atoi(argv
[iArgPtr
+ 1]);
1065 g_config
.iHDMIPort
= hdmiport
;
1066 cout
<< "using HDMI port '" << (int)g_config
.iHDMIPort
<< "'" << endl
;
1071 else if (!strcmp(argv
[iArgPtr
], "-r") ||
1072 !strcmp(argv
[iArgPtr
], "--rom"))
1074 cout
<< "using settings from EEPROM" << endl
;
1075 g_config
.bGetSettingsFromROM
= 1;
1078 else if (!strcmp(argv
[iArgPtr
], "-o") ||
1079 !strcmp(argv
[iArgPtr
], "--osd-name"))
1081 if (argc
>= iArgPtr
+ 2)
1083 snprintf(g_config
.strDeviceName
, 13, "%s", argv
[iArgPtr
+ 1]);
1084 cout
<< "using osd name " << g_config
.strDeviceName
<< endl
;
1089 else if (!strcmp(argv
[iArgPtr
], "-m") ||
1090 !strcmp(argv
[iArgPtr
], "--monitor"))
1092 cout
<< "starting a monitor-only client. use 'mon 0' to switch to normal mode" << endl
;
1093 g_config
.bMonitorOnly
= 1;
1098 g_strPort
= argv
[iArgPtr
++];
1106 int main (int argc
, char *argv
[])
1109 snprintf(g_config
.strDeviceName
, 13, "CECTester");
1110 g_config
.clientVersion
= CEC_CONFIG_VERSION
;
1111 g_config
.bActivateSource
= 0;
1112 g_callbacks
.CBCecLogMessage
= &CecLogMessage
;
1113 g_callbacks
.CBCecKeyPress
= &CecKeyPress
;
1114 g_callbacks
.CBCecCommand
= &CecCommand
;
1115 g_callbacks
.CBCecAlert
= &CecAlert
;
1116 g_config
.callbacks
= &g_callbacks
;
1118 if (!ProcessCommandLineArguments(argc
, argv
))
1121 if (g_cecLogLevel
== -1)
1122 g_cecLogLevel
= g_cecDefaultLogLevel
;
1124 if (g_config
.deviceTypes
.IsEmpty())
1126 if (!g_bSingleCommand
)
1127 cout
<< "No device type given. Using 'recording device'" << endl
;
1128 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
1131 ICECAdapter
*parser
= LibCecInitialise(&g_config
);
1135 cout
<< "Cannot load libcec.dll" << endl
;
1137 cout
<< "Cannot load libcec.so" << endl
;
1141 UnloadLibCec(parser
);
1146 if (!g_bSingleCommand
)
1149 strLog
.Format("CEC Parser created - libCEC version %s", parser
->ToString((cec_server_version
)g_config
.serverVersion
));
1150 cout
<< strLog
.c_str() << endl
;
1152 //make stdin non-blocking
1154 int flags
= fcntl(0, F_GETFL
, 0);
1155 flags
|= O_NONBLOCK
;
1156 fcntl(0, F_SETFL
, flags
);
1160 if (g_strPort
.IsEmpty())
1162 if (!g_bSingleCommand
)
1163 cout
<< "no serial port given. trying autodetect: ";
1164 cec_adapter devices
[10];
1165 uint8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
1166 if (iDevicesFound
<= 0)
1168 if (g_bSingleCommand
)
1169 cout
<< "autodetect ";
1170 cout
<< "FAILED" << endl
;
1171 UnloadLibCec(parser
);
1176 if (!g_bSingleCommand
)
1178 cout
<< endl
<< " path: " << devices
[0].path
<< endl
<<
1179 " com port: " << devices
[0].comm
<< endl
<< endl
;
1181 g_strPort
= devices
[0].comm
;
1185 PrintToStdOut("opening a connection to the CEC adapter...");
1187 if (!parser
->Open(g_strPort
.c_str()))
1189 PrintToStdOut("unable to open the device on port %s", g_strPort
.c_str());
1190 UnloadLibCec(parser
);
1194 if (!g_bSingleCommand
)
1195 PrintToStdOut("waiting for input");
1197 while (!g_bExit
&& !g_bHardExit
)
1200 getline(cin
, input
);
1203 if (ProcessConsoleCommand(parser
, input
) && !g_bSingleCommand
&& !g_bExit
&& !g_bHardExit
)
1206 PrintToStdOut("waiting for input");
1211 if (!g_bExit
&& !g_bHardExit
)
1216 UnloadLibCec(parser
);
1218 if (g_logOutput
.is_open())
1219 g_logOutput
.close();