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/
34 #include "../include/cec.h"
42 #include "../lib/platform/os.h"
43 #include "../lib/implementations/CECCommandHandler.h"
44 #include "../lib/platform/util/StdString.h"
48 using namespace PLATFORM
;
50 #define CEC_CONFIG_VERSION CEC_CLIENT_VERSION_1_8_0;
52 #include <cecloader.h>
54 ICECCallbacks g_callbacks
;
55 libcec_configuration g_config
;
56 int g_cecLogLevel(-1);
57 int g_cecDefaultLogLevel(CEC_LOG_ALL
);
59 bool g_bShortLog(false);
61 bool g_bSingleCommand(false);
63 bool g_bHardExit(false);
66 inline void PrintToStdOut(const char *strFormat
, ...)
71 va_start(argList
, strFormat
);
72 strLog
.FormatV(strFormat
, argList
);
75 CLockObject
lock(g_outputMutex
);
76 cout
<< strLog
<< endl
;
79 inline bool HexStrToInt(const std::string
& data
, uint8_t& value
)
82 if (sscanf(data
.c_str(), "%x", &iTmp
) == 1)
89 value
= (uint8_t) iTmp
;
97 //get the first word (separated by whitespace) from string data and place that in word
98 //then remove that word from string data
99 bool GetWord(string
& data
, string
& word
)
101 stringstream
datastream(data
);
105 if (datastream
.fail())
111 size_t pos
= data
.find(word
) + word
.length();
113 if (pos
>= data
.length())
119 data
= data
.substr(pos
);
122 datastream
.str(data
);
125 if (datastream
.fail())
131 int CecLogMessage(void *UNUSED(cbParam
), const cec_log_message
&message
)
133 if ((message
.level
& g_cecLogLevel
) == message
.level
)
136 switch (message
.level
)
139 strLevel
= "ERROR: ";
141 case CEC_LOG_WARNING
:
142 strLevel
= "WARNING: ";
145 strLevel
= "NOTICE: ";
147 case CEC_LOG_TRAFFIC
:
148 strLevel
= "TRAFFIC: ";
151 strLevel
= "DEBUG: ";
157 CStdString strFullLog
;
158 strFullLog
.Format("%s[%16lld]\t%s", strLevel
.c_str(), message
.time
, message
.message
);
159 PrintToStdOut(strFullLog
.c_str());
161 if (g_logOutput
.is_open())
164 g_logOutput
<< message
.message
<< endl
;
166 g_logOutput
<< strFullLog
.c_str() << endl
;
173 int CecKeyPress(void *UNUSED(cbParam
), const cec_keypress
&UNUSED(key
))
178 int CecCommand(void *UNUSED(cbParam
), const cec_command
&UNUSED(command
))
183 int CecAlert(void *UNUSED(cbParam
), const libcec_alert type
, const libcec_parameter
&UNUSED(param
))
187 case CEC_ALERT_CONNECTION_LOST
:
188 PrintToStdOut("Connection lost - exiting\n");
197 void ListDevices(ICECAdapter
*parser
)
199 cec_adapter
*devices
= new cec_adapter
[10];
200 int8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
201 if (iDevicesFound
<= 0)
203 PrintToStdOut("Found devices: NONE");
207 CStdString strDeviceInfo
;
208 strDeviceInfo
.Format("Found devices: %d\n\n", iDevicesFound
);
210 for (int8_t iDevicePtr
= 0; iDevicePtr
< iDevicesFound
; iDevicePtr
++)
212 strDeviceInfo
.AppendFormat("device: %d\ncom port: %s\n", iDevicePtr
+ 1, devices
[iDevicePtr
].comm
);
213 libcec_configuration config
;
216 if (!parser
->GetDeviceInformation(devices
[iDevicePtr
].comm
, &config
))
217 PrintToStdOut("WARNING: unable to open the device on port %s", devices
[iDevicePtr
].comm
);
220 strDeviceInfo
.AppendFormat("firmware version: %d\n", config
.iFirmwareVersion
);
222 if (config
.iFirmwareBuildDate
!= CEC_FW_BUILD_UNKNOWN
)
224 time_t buildTime
= (time_t)config
.iFirmwareBuildDate
;
225 strDeviceInfo
.AppendFormat("firmware build date: %s", asctime(gmtime(&buildTime
)));
226 strDeviceInfo
= strDeviceInfo
.Left(strDeviceInfo
.length() > 1 ? (unsigned)(strDeviceInfo
.length() - 1) : 0); // strip \n added by asctime
227 strDeviceInfo
.append(" +0000");
230 strDeviceInfo
.append("\n");
232 PrintToStdOut(strDeviceInfo
.c_str());
236 void ShowHelpCommandLine(const char* strExec
)
238 CLockObject
lock(g_outputMutex
);
240 strExec
<< " {-h|--help|-l|--list-devices|[COM PORT]}" << endl
<<
242 "parameters:" << endl
<<
243 " -h --help Shows this help text" << endl
<<
244 " -l --list-devices List all devices on this system" << endl
<<
245 " -t --type {p|r|t|a} The device type to use. More than one is possible." << endl
<<
246 " -p --port {int} The HDMI port to use as active source." << endl
<<
247 " -b --base {int} The logical address of the device to with this " << endl
<<
248 " adapter is connected." << endl
<<
249 " -f --log-file {file} Writes all libCEC log message to a file" << endl
<<
250 " -r --rom Read persisted settings from the EEPROM" << endl
<<
251 " -sf --short-log-file {file} Writes all libCEC log message without timestamps" << endl
<<
252 " and log levels to a file." << endl
<<
253 " -d --log-level {level} Sets the log level. See cectypes.h for values." << endl
<<
254 " -s --single-command Execute a single command and exit. Does not power" << endl
<<
255 " on devices on startup and power them off on exit." << endl
<<
256 " -o --osd-name {osd name} Use a custom osd name." << endl
<<
257 " -m --monitor Start a monitor-only client." << endl
<<
258 " -i --info Shows information about how libCEC was compiled." << endl
<<
259 " [COM PORT] The com port to connect to. If no COM" << endl
<<
260 " port is given, the client tries to connect to the" << endl
<<
261 " first device that is detected." << endl
<<
263 "Type 'h' or 'help' and press enter after starting the client to display all " << endl
<<
264 "available commands" << endl
;
267 void ShowHelpConsole(void)
269 CLockObject
lock(g_outputMutex
);
271 "================================================================================" << endl
<<
272 "Available commands:" << endl
<<
274 "[tx] {bytes} transfer bytes over the CEC line." << endl
<<
275 "[txn] {bytes} transfer bytes but don't wait for transmission ACK." << endl
<<
276 "[on] {address} power on the device with the given logical address." << endl
<<
277 "[standby] {address} put the device with the given address in standby mode." << endl
<<
278 "[la] {logical address} change the logical address of the CEC adapter." << endl
<<
279 "[p] {device} {port} change the HDMI port number of the CEC adapter." << endl
<<
280 "[pa] {physical address} change the physical address of the CEC adapter." << endl
<<
281 "[as] make the CEC adapter the active source." << endl
<<
282 "[osd] {addr} {string} set OSD message on the specified device." << endl
<<
283 "[ver] {addr} get the CEC version of the specified device." << endl
<<
284 "[ven] {addr} get the vendor ID of the specified device." << endl
<<
285 "[lang] {addr} get the menu language of the specified device." << endl
<<
286 "[pow] {addr} get the power status of the specified device." << endl
<<
287 "[name] {addr} get the OSD name of the specified device." << endl
<<
288 "[poll] {addr} poll the specified device." << endl
<<
289 "[lad] lists active devices on the bus" << endl
<<
290 "[ad] {addr} checks whether the specified device is active." << endl
<<
291 "[at] {type} checks whether the specified device type is active." << endl
<<
292 "[sp] {addr} makes the specified physical address active." << endl
<<
293 "[spl] {addr} makes the specified logical address active." << endl
<<
294 "[volup] send a volume up command to the amp if present" << endl
<<
295 "[voldown] send a volume down command to the amp if present" << endl
<<
296 "[mute] send a mute/unmute command to the amp if present" << endl
<<
297 "[self] show the list of addresses controlled by libCEC" << endl
<<
298 "[scan] scan the CEC bus and display device info" << endl
<<
299 "[mon] {1|0} enable or disable CEC bus monitoring." << endl
<<
300 "[log] {1 - 31} change the log level. see cectypes.h for values." << endl
<<
301 "[ping] send a ping command to the CEC adapter." << endl
<<
302 "[bl] to let the adapter enter the bootloader, to upgrade" << endl
<<
303 " the flash rom." << endl
<<
304 "[r] reconnect to the CEC adapter." << endl
<<
305 "[h] or [help] show this help." << endl
<<
306 "[q] or [quit] to quit the CEC test client and switch off all" << endl
<<
307 " connected CEC devices." << endl
<<
308 "================================================================================" << endl
;
311 bool ProcessCommandSELF(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
313 if (command
== "self")
315 cec_logical_addresses addr
= parser
->GetLogicalAddresses();
316 CStdString strOut
= "Addresses controlled by libCEC: ";
318 for (uint8_t iPtr
= 0; iPtr
<= 15; iPtr
++)
322 strOut
.AppendFormat((bFirst
? "%d%s" : ", %d%s"), iPtr
, parser
->IsActiveSource((cec_logical_address
)iPtr
) ? "*" : "");
326 PrintToStdOut(strOut
.c_str());
333 bool ProcessCommandSP(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
339 if (GetWord(arguments
, strAddress
))
341 sscanf(strAddress
.c_str(), "%x", &iAddress
);
342 if (iAddress
>= 0 && iAddress
<= CEC_INVALID_PHYSICAL_ADDRESS
)
343 parser
->SetStreamPath((uint16_t)iAddress
);
351 bool ProcessCommandSPL(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
353 if (command
== "spl")
356 cec_logical_address iAddress
;
357 if (GetWord(arguments
, strAddress
))
359 iAddress
= (cec_logical_address
)atoi(strAddress
.c_str());
360 if (iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
361 parser
->SetStreamPath(iAddress
);
369 bool ProcessCommandTX(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
371 if (command
== "tx" || command
== "txn")
378 while (GetWord(arguments
, strvalue
) && HexStrToInt(strvalue
, ivalue
))
379 bytes
.PushBack(ivalue
);
381 if (command
== "txn")
382 bytes
.transmit_timeout
= 0;
384 parser
->Transmit(bytes
);
392 bool ProcessCommandON(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
398 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
400 parser
->PowerOnDevices((cec_logical_address
) iValue
);
405 PrintToStdOut("invalid destination");
412 bool ProcessCommandSTANDBY(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
414 if (command
== "standby")
418 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
420 parser
->StandbyDevices((cec_logical_address
) iValue
);
425 PrintToStdOut("invalid destination");
432 bool ProcessCommandPOLL(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
434 if (command
== "poll")
438 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
440 if (parser
->PollDevice((cec_logical_address
) iValue
))
441 PrintToStdOut("POLL message sent");
443 PrintToStdOut("POLL message not sent");
448 PrintToStdOut("invalid destination");
455 bool ProcessCommandLA(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
460 if (GetWord(arguments
, strvalue
))
462 parser
->SetLogicalAddress((cec_logical_address
) atoi(strvalue
.c_str()));
470 bool ProcessCommandP(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
474 string strPort
, strDevice
;
475 if (GetWord(arguments
, strDevice
) && GetWord(arguments
, strPort
))
477 parser
->SetHDMIPort((cec_logical_address
)atoi(strDevice
.c_str()), (uint8_t)atoi(strPort
.c_str()));
485 bool ProcessCommandPA(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
491 if (GetWord(arguments
, strB1
) && HexStrToInt(strB1
, iB1
) &&
492 GetWord(arguments
, strB2
) && HexStrToInt(strB2
, iB2
))
494 uint16_t iPhysicalAddress
= ((uint16_t)iB1
<< 8) + iB2
;
495 parser
->SetPhysicalAddress(iPhysicalAddress
);
503 bool ProcessCommandOSD(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
505 if (command
== "osd")
507 bool bFirstWord(false);
508 string strAddr
, strMessage
, strWord
;
510 if (GetWord(arguments
, strAddr
) && HexStrToInt(strAddr
, iAddr
) && iAddr
< 0xF)
512 while (GetWord(arguments
, strWord
))
517 strMessage
.append(" ");
519 strMessage
.append(strWord
);
521 parser
->SetOSDString((cec_logical_address
) iAddr
, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME
, strMessage
.c_str());
529 bool ProcessCommandAS(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
533 parser
->SetActiveSource();
541 bool ProcessCommandPING(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
543 if (command
== "ping")
545 parser
->PingAdapter();
552 bool ProcessCommandVOLUP(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
554 if (command
== "volup")
556 PrintToStdOut("volume up: %2X", parser
->VolumeUp());
563 bool ProcessCommandVOLDOWN(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
565 if (command
== "voldown")
567 PrintToStdOut("volume down: %2X", parser
->VolumeDown());
574 bool ProcessCommandMUTE(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
576 if (command
== "mute")
578 PrintToStdOut("mute: %2X", parser
->MuteAudio());
585 bool ProcessCommandMON(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
587 if (command
== "mon")
589 CStdString strEnable
;
590 if (GetWord(arguments
, strEnable
) && (strEnable
.Equals("0") || strEnable
.Equals("1")))
592 parser
->SwitchMonitoring(strEnable
.Equals("1"));
600 bool ProcessCommandBL(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
604 if (parser
->StartBootloader())
606 PrintToStdOut("entered bootloader mode. exiting cec-client");
616 bool ProcessCommandLANG(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
618 if (command
== "lang")
621 if (GetWord(arguments
, strDev
))
623 int iDev
= atoi(strDev
);
624 if (iDev
>= 0 && iDev
< 15)
627 cec_menu_language language
;
628 if (parser
->GetDeviceMenuLanguage((cec_logical_address
) iDev
, &language
))
629 strLog
.Format("menu language '%s'", language
.language
);
632 PrintToStdOut(strLog
);
641 bool ProcessCommandVEN(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
643 if (command
== "ven")
646 if (GetWord(arguments
, strDev
))
648 int iDev
= atoi(strDev
);
649 if (iDev
>= 0 && iDev
< 15)
651 uint64_t iVendor
= parser
->GetDeviceVendorId((cec_logical_address
) iDev
);
652 PrintToStdOut("vendor id: %06x", iVendor
);
661 bool ProcessCommandVER(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
663 if (command
== "ver")
666 if (GetWord(arguments
, strDev
))
668 int iDev
= atoi(strDev
);
669 if (iDev
>= 0 && iDev
< 15)
671 cec_version iVersion
= parser
->GetDeviceCecVersion((cec_logical_address
) iDev
);
672 PrintToStdOut("CEC version %s", parser
->ToString(iVersion
));
681 bool ProcessCommandPOW(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
683 if (command
== "pow")
686 if (GetWord(arguments
, strDev
))
688 int iDev
= atoi(strDev
);
689 if (iDev
>= 0 && iDev
< 15)
691 cec_power_status iPower
= parser
->GetDevicePowerStatus((cec_logical_address
) iDev
);
692 PrintToStdOut("power status: %s", parser
->ToString(iPower
));
701 bool ProcessCommandNAME(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
703 if (command
== "name")
706 if (GetWord(arguments
, strDev
))
708 int iDev
= atoi(strDev
);
709 if (iDev
>= 0 && iDev
< 15)
711 cec_osd_name name
= parser
->GetDeviceOSDName((cec_logical_address
)iDev
);
712 PrintToStdOut("OSD name of device %d is '%s'", iDev
, name
.name
);
721 bool ProcessCommandLAD(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
723 if (command
== "lad")
725 PrintToStdOut("listing active devices:");
726 cec_logical_addresses addresses
= parser
->GetActiveDevices();
727 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
730 PrintToStdOut("logical address %X", (int)iPtr
);
738 bool ProcessCommandAD(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
743 if (GetWord(arguments
, strDev
))
745 int iDev
= atoi(strDev
);
746 if (iDev
>= 0 && iDev
< 15)
747 PrintToStdOut("logical address %X is %s", iDev
, (parser
->IsActiveDevice((cec_logical_address
)iDev
) ? "active" : "not active"));
754 bool ProcessCommandAT(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
759 if (GetWord(arguments
, strType
))
761 cec_device_type type
= CEC_DEVICE_TYPE_TV
;
762 if (strType
.Equals("a"))
763 type
= CEC_DEVICE_TYPE_AUDIO_SYSTEM
;
764 else if (strType
.Equals("p"))
765 type
= CEC_DEVICE_TYPE_PLAYBACK_DEVICE
;
766 else if (strType
.Equals("r"))
767 type
= CEC_DEVICE_TYPE_RECORDING_DEVICE
;
768 else if (strType
.Equals("t"))
769 type
= CEC_DEVICE_TYPE_TUNER
;
771 PrintToStdOut("device %d is %s", type
, (parser
->IsActiveDeviceType(type
) ? "active" : "not active"));
779 bool ProcessCommandR(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
783 bool bReactivate
= parser
->IsLibCECActiveSource();
785 PrintToStdOut("closing the connection");
788 PrintToStdOut("opening a new connection");
789 parser
->Open(g_strPort
.c_str());
793 PrintToStdOut("setting active source");
794 parser
->SetActiveSource();
802 bool ProcessCommandH(ICECAdapter
* UNUSED(parser
), const string
&command
, string
& UNUSED(arguments
))
804 if (command
== "h" || command
== "help")
813 bool ProcessCommandLOG(ICECAdapter
* UNUSED(parser
), const string
&command
, string
&arguments
)
815 if (command
== "log")
818 if (GetWord(arguments
, strLevel
))
820 int iNewLevel
= atoi(strLevel
);
821 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
823 g_cecLogLevel
= iNewLevel
;
825 PrintToStdOut("log level changed to %s", strLevel
.c_str());
834 bool ProcessCommandSCAN(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
836 if (command
== "scan")
839 PrintToStdOut("requesting CEC bus information ...");
841 strLog
.append("CEC bus information\n===================\n");
842 cec_logical_addresses addresses
= parser
->GetActiveDevices();
843 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
847 uint64_t iVendorId
= parser
->GetDeviceVendorId((cec_logical_address
)iPtr
);
848 bool bActive
= parser
->IsActiveSource((cec_logical_address
)iPtr
);
849 uint16_t iPhysicalAddress
= parser
->GetDevicePhysicalAddress((cec_logical_address
)iPtr
);
850 cec_version iCecVersion
= parser
->GetDeviceCecVersion((cec_logical_address
)iPtr
);
851 cec_power_status power
= parser
->GetDevicePowerStatus((cec_logical_address
)iPtr
);
852 cec_osd_name osdName
= parser
->GetDeviceOSDName((cec_logical_address
)iPtr
);
854 strAddr
.Format("%04x", iPhysicalAddress
);
855 cec_menu_language lang
;
856 lang
.device
= CECDEVICE_UNKNOWN
;
857 parser
->GetDeviceMenuLanguage((cec_logical_address
)iPtr
, &lang
);
859 strLog
.AppendFormat("device #%X: %s\n", (int)iPtr
, parser
->ToString((cec_logical_address
)iPtr
));
860 strLog
.AppendFormat("address: %s\n", strAddr
.c_str());
861 strLog
.AppendFormat("active source: %s\n", (bActive
? "yes" : "no"));
862 strLog
.AppendFormat("vendor: %s\n", parser
->ToString((cec_vendor_id
)iVendorId
));
863 strLog
.AppendFormat("osd string: %s\n", osdName
.name
);
864 strLog
.AppendFormat("CEC version: %s\n", parser
->ToString(iCecVersion
));
865 strLog
.AppendFormat("power status: %s\n", parser
->ToString(power
));
866 if ((uint8_t)lang
.device
== iPtr
)
867 strLog
.AppendFormat("language: %s\n", lang
.language
);
868 strLog
.append("\n\n");
872 cec_logical_address activeSource
= parser
->GetActiveSource();
873 strLog
.AppendFormat("currently active source: %s (%d)", parser
->ToString(activeSource
), (int)activeSource
);
875 PrintToStdOut(strLog
);
882 bool ProcessConsoleCommand(ICECAdapter
*parser
, string
&input
)
887 if (GetWord(input
, command
))
889 if (command
== "q" || command
== "quit")
892 ProcessCommandTX(parser
, command
, input
) ||
893 ProcessCommandON(parser
, command
, input
) ||
894 ProcessCommandSTANDBY(parser
, command
, input
) ||
895 ProcessCommandPOLL(parser
, command
, input
) ||
896 ProcessCommandLA(parser
, command
, input
) ||
897 ProcessCommandP(parser
, command
, input
) ||
898 ProcessCommandPA(parser
, command
, input
) ||
899 ProcessCommandAS(parser
, command
, input
) ||
900 ProcessCommandOSD(parser
, command
, input
) ||
901 ProcessCommandPING(parser
, command
, input
) ||
902 ProcessCommandVOLUP(parser
, command
, input
) ||
903 ProcessCommandVOLDOWN(parser
, command
, input
) ||
904 ProcessCommandMUTE(parser
, command
, input
) ||
905 ProcessCommandMON(parser
, command
, input
) ||
906 ProcessCommandBL(parser
, command
, input
) ||
907 ProcessCommandLANG(parser
, command
, input
) ||
908 ProcessCommandVEN(parser
, command
, input
) ||
909 ProcessCommandVER(parser
, command
, input
) ||
910 ProcessCommandPOW(parser
, command
, input
) ||
911 ProcessCommandNAME(parser
, command
, input
) ||
912 ProcessCommandLAD(parser
, command
, input
) ||
913 ProcessCommandAD(parser
, command
, input
) ||
914 ProcessCommandAT(parser
, command
, input
) ||
915 ProcessCommandR(parser
, command
, input
) ||
916 ProcessCommandH(parser
, command
, input
) ||
917 ProcessCommandLOG(parser
, command
, input
) ||
918 ProcessCommandSCAN(parser
, command
, input
) ||
919 ProcessCommandSP(parser
, command
, input
) ||
920 ProcessCommandSPL(parser
, command
, input
) ||
921 ProcessCommandSELF(parser
, command
, input
);
927 bool ProcessCommandLineArguments(int argc
, char *argv
[])
931 while (iArgPtr
< argc
&& bReturn
)
933 if (argc
>= iArgPtr
+ 1)
935 if (!strcmp(argv
[iArgPtr
], "-f") ||
936 !strcmp(argv
[iArgPtr
], "--log-file") ||
937 !strcmp(argv
[iArgPtr
], "-sf") ||
938 !strcmp(argv
[iArgPtr
], "--short-log-file"))
940 if (argc
>= iArgPtr
+ 2)
942 g_logOutput
.open(argv
[iArgPtr
+ 1]);
943 g_bShortLog
= (!strcmp(argv
[iArgPtr
], "-sf") || !strcmp(argv
[iArgPtr
], "--short-log-file"));
948 cout
<< "== skipped log-file parameter: no file given ==" << endl
;
952 else if (!strcmp(argv
[iArgPtr
], "-d") ||
953 !strcmp(argv
[iArgPtr
], "--log-level"))
955 if (argc
>= iArgPtr
+ 2)
957 int iNewLevel
= atoi(argv
[iArgPtr
+ 1]);
958 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
960 g_cecLogLevel
= iNewLevel
;
961 if (!g_bSingleCommand
)
962 cout
<< "log level set to " << argv
[iArgPtr
+ 1] << endl
;
966 cout
<< "== skipped log-level parameter: invalid level '" << argv
[iArgPtr
+ 1] << "' ==" << endl
;
972 cout
<< "== skipped log-level parameter: no level given ==" << endl
;
976 else if (!strcmp(argv
[iArgPtr
], "-t") ||
977 !strcmp(argv
[iArgPtr
], "--type"))
979 if (argc
>= iArgPtr
+ 2)
981 if (!strcmp(argv
[iArgPtr
+ 1], "p"))
983 if (!g_bSingleCommand
)
984 cout
<< "== using device type 'playback device'" << endl
;
985 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
987 else if (!strcmp(argv
[iArgPtr
+ 1], "r"))
989 if (!g_bSingleCommand
)
990 cout
<< "== using device type 'recording device'" << endl
;
991 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
993 else if (!strcmp(argv
[iArgPtr
+ 1], "t"))
995 if (!g_bSingleCommand
)
996 cout
<< "== using device type 'tuner'" << endl
;
997 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_TUNER
);
999 else if (!strcmp(argv
[iArgPtr
+ 1], "a"))
1001 if (!g_bSingleCommand
)
1002 cout
<< "== using device type 'audio system'" << endl
;
1003 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_AUDIO_SYSTEM
);
1007 cout
<< "== skipped invalid device type '" << argv
[iArgPtr
+ 1] << "'" << endl
;
1013 else if (!strcmp(argv
[iArgPtr
], "--info") ||
1014 !strcmp(argv
[iArgPtr
], "-i"))
1016 if (g_cecLogLevel
== -1)
1017 g_cecLogLevel
= CEC_LOG_WARNING
+ CEC_LOG_ERROR
;
1018 ICECAdapter
*parser
= LibCecInitialise(&g_config
);
1021 CStdString strMessage
;
1022 strMessage
.Format("libCEC version: %s", parser
->ToString((cec_server_version
)g_config
.serverVersion
));
1023 if (g_config
.serverVersion
>= CEC_SERVER_VERSION_1_7_2
)
1024 strMessage
.AppendFormat(", %s", parser
->GetLibInfo());
1025 PrintToStdOut(strMessage
.c_str());
1026 UnloadLibCec(parser
);
1031 else if (!strcmp(argv
[iArgPtr
], "--list-devices") ||
1032 !strcmp(argv
[iArgPtr
], "-l"))
1034 if (g_cecLogLevel
== -1)
1035 g_cecLogLevel
= CEC_LOG_WARNING
+ CEC_LOG_ERROR
;
1036 ICECAdapter
*parser
= LibCecInitialise(&g_config
);
1039 ListDevices(parser
);
1040 UnloadLibCec(parser
);
1045 else if (!strcmp(argv
[iArgPtr
], "--bootloader"))
1050 else if (!strcmp(argv
[iArgPtr
], "--single-command") ||
1051 !strcmp(argv
[iArgPtr
], "-s"))
1053 g_bSingleCommand
= true;
1056 else if (!strcmp(argv
[iArgPtr
], "--help") ||
1057 !strcmp(argv
[iArgPtr
], "-h"))
1059 if (g_cecLogLevel
== -1)
1060 g_cecLogLevel
= CEC_LOG_WARNING
+ CEC_LOG_ERROR
;
1062 ShowHelpCommandLine(argv
[0]);
1065 else if (!strcmp(argv
[iArgPtr
], "-b") ||
1066 !strcmp(argv
[iArgPtr
], "--base"))
1068 if (argc
>= iArgPtr
+ 2)
1070 g_config
.baseDevice
= (cec_logical_address
)atoi(argv
[iArgPtr
+ 1]);
1071 cout
<< "using base device '" << (int)g_config
.baseDevice
<< "'" << endl
;
1076 else if (!strcmp(argv
[iArgPtr
], "-p") ||
1077 !strcmp(argv
[iArgPtr
], "--port"))
1079 if (argc
>= iArgPtr
+ 2)
1081 uint8_t hdmiport
= (int8_t)atoi(argv
[iArgPtr
+ 1]);
1086 g_config
.iHDMIPort
= hdmiport
;
1087 cout
<< "using HDMI port '" << (int)g_config
.iHDMIPort
<< "'" << endl
;
1092 else if (!strcmp(argv
[iArgPtr
], "-r") ||
1093 !strcmp(argv
[iArgPtr
], "--rom"))
1095 cout
<< "using settings from EEPROM" << endl
;
1096 g_config
.bGetSettingsFromROM
= 1;
1099 else if (!strcmp(argv
[iArgPtr
], "-o") ||
1100 !strcmp(argv
[iArgPtr
], "--osd-name"))
1102 if (argc
>= iArgPtr
+ 2)
1104 snprintf(g_config
.strDeviceName
, 13, "%s", argv
[iArgPtr
+ 1]);
1105 cout
<< "using osd name " << g_config
.strDeviceName
<< endl
;
1110 else if (!strcmp(argv
[iArgPtr
], "-m") ||
1111 !strcmp(argv
[iArgPtr
], "--monitor"))
1113 cout
<< "starting a monitor-only client. use 'mon 0' to switch to normal mode" << endl
;
1114 g_config
.bMonitorOnly
= 1;
1119 g_strPort
= argv
[iArgPtr
++];
1127 int main (int argc
, char *argv
[])
1130 g_callbacks
.Clear();
1131 snprintf(g_config
.strDeviceName
, 13, "CECTester");
1132 g_config
.clientVersion
= CEC_CONFIG_VERSION
;
1133 g_config
.bActivateSource
= 0;
1134 g_callbacks
.CBCecLogMessage
= &CecLogMessage
;
1135 g_callbacks
.CBCecKeyPress
= &CecKeyPress
;
1136 g_callbacks
.CBCecCommand
= &CecCommand
;
1137 g_callbacks
.CBCecAlert
= &CecAlert
;
1138 g_config
.callbacks
= &g_callbacks
;
1140 if (!ProcessCommandLineArguments(argc
, argv
))
1143 if (g_cecLogLevel
== -1)
1144 g_cecLogLevel
= g_cecDefaultLogLevel
;
1146 if (g_config
.deviceTypes
.IsEmpty())
1148 if (!g_bSingleCommand
)
1149 cout
<< "No device type given. Using 'recording device'" << endl
;
1150 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
1153 ICECAdapter
*parser
= LibCecInitialise(&g_config
);
1157 cout
<< "Cannot load libcec.dll" << endl
;
1159 cout
<< "Cannot load libcec.so" << endl
;
1163 UnloadLibCec(parser
);
1168 // init video on targets that need this
1169 parser
->InitVideoStandalone();
1171 if (!g_bSingleCommand
)
1174 strLog
.Format("CEC Parser created - libCEC version %s", parser
->ToString((cec_server_version
)g_config
.serverVersion
));
1175 cout
<< strLog
.c_str() << endl
;
1177 //make stdin non-blocking
1179 int flags
= fcntl(0, F_GETFL
, 0);
1180 flags
|= O_NONBLOCK
;
1181 fcntl(0, F_SETFL
, flags
);
1185 if (g_strPort
.IsEmpty())
1187 if (!g_bSingleCommand
)
1188 cout
<< "no serial port given. trying autodetect: ";
1189 cec_adapter devices
[10];
1190 uint8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
1191 if (iDevicesFound
<= 0)
1193 if (g_bSingleCommand
)
1194 cout
<< "autodetect ";
1195 cout
<< "FAILED" << endl
;
1196 UnloadLibCec(parser
);
1201 if (!g_bSingleCommand
)
1203 cout
<< endl
<< " path: " << devices
[0].path
<< endl
<<
1204 " com port: " << devices
[0].comm
<< endl
<< endl
;
1206 g_strPort
= devices
[0].comm
;
1210 PrintToStdOut("opening a connection to the CEC adapter...");
1212 if (!parser
->Open(g_strPort
.c_str()))
1214 PrintToStdOut("unable to open the device on port %s", g_strPort
.c_str());
1215 UnloadLibCec(parser
);
1219 if (!g_bSingleCommand
)
1220 PrintToStdOut("waiting for input");
1222 while (!g_bExit
&& !g_bHardExit
)
1225 getline(cin
, input
);
1228 if (ProcessConsoleCommand(parser
, input
) && !g_bSingleCommand
&& !g_bExit
&& !g_bHardExit
)
1231 PrintToStdOut("waiting for input");
1236 if (!g_bExit
&& !g_bHardExit
)
1241 UnloadLibCec(parser
);
1243 if (g_logOutput
.is_open())
1244 g_logOutput
.close();