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 #include <cecloader.h>
50 ICECCallbacks g_callbacks
;
51 libcec_configuration g_config
;
52 int g_cecLogLevel(CEC_LOG_ALL
);
54 bool g_bShortLog(false);
56 bool g_bSingleCommand(false);
58 bool g_bHardExit(false);
61 inline void PrintToStdOut(const char *strFormat
, ...)
66 va_start(argList
, strFormat
);
67 strLog
.FormatV(strFormat
, argList
);
70 CLockObject
lock(g_outputMutex
);
71 cout
<< strLog
<< endl
;
74 inline bool HexStrToInt(const std::string
& data
, uint8_t& value
)
77 if (sscanf(data
.c_str(), "%x", &iTmp
) == 1)
84 value
= (uint8_t) iTmp
;
92 //get the first word (separated by whitespace) from string data and place that in word
93 //then remove that word from string data
94 bool GetWord(string
& data
, string
& word
)
96 stringstream
datastream(data
);
100 if (datastream
.fail())
106 size_t pos
= data
.find(word
) + word
.length();
108 if (pos
>= data
.length())
114 data
= data
.substr(pos
);
117 datastream
.str(data
);
120 if (datastream
.fail())
126 int CecLogMessage(void *UNUSED(cbParam
), const cec_log_message
&message
)
128 if ((message
.level
& g_cecLogLevel
) == message
.level
)
131 switch (message
.level
)
134 strLevel
= "ERROR: ";
136 case CEC_LOG_WARNING
:
137 strLevel
= "WARNING: ";
140 strLevel
= "NOTICE: ";
142 case CEC_LOG_TRAFFIC
:
143 strLevel
= "TRAFFIC: ";
146 strLevel
= "DEBUG: ";
152 CStdString strFullLog
;
153 strFullLog
.Format("%s[%16lld]\t%s", strLevel
.c_str(), message
.time
, message
.message
);
154 PrintToStdOut(strFullLog
.c_str());
156 if (g_logOutput
.is_open())
159 g_logOutput
<< message
.message
<< endl
;
161 g_logOutput
<< strFullLog
.c_str() << endl
;
168 int CecKeyPress(void *UNUSED(cbParam
), const cec_keypress
&UNUSED(key
))
173 int CecCommand(void *UNUSED(cbParam
), const cec_command
&UNUSED(command
))
178 void EnableCallbacks(ICECAdapter
*adapter
)
180 g_callbacks
.CBCecLogMessage
= &CecLogMessage
;
181 g_callbacks
.CBCecKeyPress
= &CecKeyPress
;
182 g_callbacks
.CBCecCommand
= &CecCommand
;
183 g_callbacks
.CBCecConfigurationChanged
= NULL
;
184 adapter
->EnableCallbacks(NULL
, &g_callbacks
);
187 void ListDevices(ICECAdapter
*parser
)
189 cec_adapter
*devices
= new cec_adapter
[10];
190 int8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
191 if (iDevicesFound
<= 0)
193 PrintToStdOut("Found devices: NONE");
197 PrintToStdOut("Found devices: %d\n", iDevicesFound
);
198 for (int8_t iDevicePtr
= 0; iDevicePtr
< iDevicesFound
; iDevicePtr
++)
199 PrintToStdOut("device: %d\npath: %s\ncom port: %s\n", iDevicePtr
+ 1, devices
[iDevicePtr
].path
, devices
[iDevicePtr
].comm
);
203 void ShowHelpCommandLine(const char* strExec
)
205 CLockObject
lock(g_outputMutex
);
207 strExec
<< " {-h|--help|-l|--list-devices|[COM PORT]}" << endl
<<
209 "parameters:" << endl
<<
210 " -h --help Shows this help text" << endl
<<
211 " -l --list-devices List all devices on this system" << endl
<<
212 " -t --type {p|r|t|a} The device type to use. More than one is possible." << endl
<<
213 " -p --port {int} The HDMI port to use as active source." << endl
<<
214 " -b --base {int} The logical address of the device to with this " << endl
<<
215 " adapter is connected." << endl
<<
216 " -f --log-file {file} Writes all libCEC log message to a file" << endl
<<
217 " -r --rom Read persisted settings from the EEPROM" << endl
<<
218 " -sf --short-log-file {file} Writes all libCEC log message without timestamps" << endl
<<
219 " and log levels to a file." << endl
<<
220 " -d --log-level {level} Sets the log level. See cectypes.h for values." << endl
<<
221 " -s --single-command Execute a single command and exit. Does not power" << endl
<<
222 " on devices on startup and power them off on exit." << endl
<<
223 " -o --osd-name {osd name} Use a custom osd name." << endl
<<
224 " [COM PORT] The com port to connect to. If no COM" << endl
<<
225 " port is given, the client tries to connect to the" << endl
<<
226 " first device that is detected." << endl
<<
228 "Type 'h' or 'help' and press enter after starting the client to display all " << endl
<<
229 "available commands" << endl
;
232 void ShowHelpConsole(void)
234 CLockObject
lock(g_outputMutex
);
236 "================================================================================" << endl
<<
237 "Available commands:" << endl
<<
239 "[tx] {bytes} transfer bytes over the CEC line." << endl
<<
240 "[txn] {bytes} transfer bytes but don't wait for transmission ACK." << endl
<<
241 "[on] {address} power on the device with the given logical address." << endl
<<
242 "[standby] {address} put the device with the given address in standby mode." << endl
<<
243 "[la] {logical address} change the logical address of the CEC adapter." << endl
<<
244 "[p] {device} {port} change the HDMI port number of the CEC adapter." << endl
<<
245 "[pa] {physical address} change the physical address of the CEC adapter." << endl
<<
246 "[as] make the CEC adapter the active source." << endl
<<
247 "[osd] {addr} {string} set OSD message on the specified device." << endl
<<
248 "[ver] {addr} get the CEC version of the specified device." << endl
<<
249 "[ven] {addr} get the vendor ID of the specified device." << endl
<<
250 "[lang] {addr} get the menu language of the specified device." << endl
<<
251 "[pow] {addr} get the power status of the specified device." << endl
<<
252 "[name] {addr} get the OSD name of the specified device." << endl
<<
253 "[poll] {addr} poll the specified device." << endl
<<
254 "[lad] lists active devices on the bus" << endl
<<
255 "[ad] {addr} checks whether the specified device is active." << endl
<<
256 "[at] {type} checks whether the specified device type is active." << endl
<<
257 "[sp] {addr} makes the specified physical address active." << endl
<<
258 "[spl] {addr} makes the specified logical address active." << endl
<<
259 "[volup] send a volume up command to the amp if present" << endl
<<
260 "[voldown] send a volume down command to the amp if present" << endl
<<
261 "[mute] send a mute/unmute command to the amp if present" << endl
<<
262 "[self] show the list of addresses controlled by libCEC" << endl
<<
263 "[scan] scan the CEC bus and display device info" << endl
<<
264 "[mon] {1|0} enable or disable CEC bus monitoring." << endl
<<
265 "[log] {1 - 31} change the log level. see cectypes.h for values." << endl
<<
266 "[ping] send a ping command to the CEC adapter." << endl
<<
267 "[bl] to let the adapter enter the bootloader, to upgrade" << endl
<<
268 " the flash rom." << endl
<<
269 "[r] reconnect to the CEC adapter." << endl
<<
270 "[h] or [help] show this help." << endl
<<
271 "[q] or [quit] to quit the CEC test client and switch off all" << endl
<<
272 " connected CEC devices." << endl
<<
273 "================================================================================" << endl
;
276 bool ProcessCommandSELF(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
278 if (command
== "self")
280 cec_logical_addresses addr
= parser
->GetLogicalAddresses();
281 CStdString strOut
= "Addresses controlled by libCEC: ";
283 for (uint8_t iPtr
= 0; iPtr
<= 15; iPtr
++)
287 strOut
.AppendFormat((bFirst
? "%d%s" : ", %d%s"), iPtr
, parser
->IsActiveSource((cec_logical_address
)iPtr
) ? "*" : "");
291 PrintToStdOut(strOut
.c_str());
298 bool ProcessCommandSP(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
304 if (GetWord(arguments
, strAddress
))
306 sscanf(strAddress
.c_str(), "%x", &iAddress
);
307 if (iAddress
>= 0 && iAddress
<= 0xFFFF)
308 parser
->SetStreamPath((uint16_t)iAddress
);
316 bool ProcessCommandSPL(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
318 if (command
== "spl")
321 cec_logical_address iAddress
;
322 if (GetWord(arguments
, strAddress
))
324 iAddress
= (cec_logical_address
)atoi(strAddress
.c_str());
325 if (iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
326 parser
->SetStreamPath(iAddress
);
334 bool ProcessCommandTX(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
336 if (command
== "tx" || command
== "txn")
343 while (GetWord(arguments
, strvalue
) && HexStrToInt(strvalue
, ivalue
))
344 bytes
.PushBack(ivalue
);
346 if (command
== "txn")
347 bytes
.transmit_timeout
= 0;
349 parser
->Transmit(bytes
);
357 bool ProcessCommandON(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
363 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
365 parser
->PowerOnDevices((cec_logical_address
) iValue
);
370 PrintToStdOut("invalid destination");
377 bool ProcessCommandSTANDBY(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
379 if (command
== "standby")
383 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
385 parser
->StandbyDevices((cec_logical_address
) iValue
);
390 PrintToStdOut("invalid destination");
397 bool ProcessCommandPOLL(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
399 if (command
== "poll")
403 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
405 if (parser
->PollDevice((cec_logical_address
) iValue
))
406 PrintToStdOut("POLL message sent");
408 PrintToStdOut("POLL message not sent");
413 PrintToStdOut("invalid destination");
420 bool ProcessCommandLA(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
425 if (GetWord(arguments
, strvalue
))
427 parser
->SetLogicalAddress((cec_logical_address
) atoi(strvalue
.c_str()));
435 bool ProcessCommandP(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
439 string strPort
, strDevice
;
440 if (GetWord(arguments
, strDevice
) && GetWord(arguments
, strPort
))
442 parser
->SetHDMIPort((cec_logical_address
)atoi(strDevice
.c_str()), (uint8_t)atoi(strPort
.c_str()));
450 bool ProcessCommandPA(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
456 if (GetWord(arguments
, strB1
) && HexStrToInt(strB1
, iB1
) &&
457 GetWord(arguments
, strB2
) && HexStrToInt(strB2
, iB2
))
459 uint16_t iPhysicalAddress
= ((uint16_t)iB1
<< 8) + iB2
;
460 parser
->SetPhysicalAddress(iPhysicalAddress
);
468 bool ProcessCommandOSD(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
470 if (command
== "osd")
472 bool bFirstWord(false);
473 string strAddr
, strMessage
, strWord
;
475 if (GetWord(arguments
, strAddr
) && HexStrToInt(strAddr
, iAddr
) && iAddr
< 0xF)
477 while (GetWord(arguments
, strWord
))
482 strMessage
.append(" ");
484 strMessage
.append(strWord
);
486 parser
->SetOSDString((cec_logical_address
) iAddr
, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME
, strMessage
.c_str());
494 bool ProcessCommandAS(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
498 parser
->SetActiveView();
506 bool ProcessCommandPING(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
508 if (command
== "ping")
510 parser
->PingAdapter();
517 bool ProcessCommandVOLUP(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
519 if (command
== "volup")
521 PrintToStdOut("volume up: %2X", parser
->VolumeUp());
528 bool ProcessCommandVOLDOWN(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
530 if (command
== "voldown")
532 PrintToStdOut("volume down: %2X", parser
->VolumeDown());
539 bool ProcessCommandMUTE(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
541 if (command
== "mute")
543 PrintToStdOut("mute: %2X", parser
->MuteAudio());
550 bool ProcessCommandMON(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
552 if (command
== "mon")
554 CStdString strEnable
;
555 if (GetWord(arguments
, strEnable
) && (strEnable
.Equals("0") || strEnable
.Equals("1")))
557 parser
->SwitchMonitoring(strEnable
.Equals("1"));
565 bool ProcessCommandBL(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
569 if (parser
->StartBootloader())
571 PrintToStdOut("entered bootloader mode. exiting cec-client");
581 bool ProcessCommandLANG(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
583 if (command
== "lang")
586 if (GetWord(arguments
, strDev
))
588 int iDev
= atoi(strDev
);
589 if (iDev
>= 0 && iDev
< 15)
592 cec_menu_language language
;
593 if (parser
->GetDeviceMenuLanguage((cec_logical_address
) iDev
, &language
))
594 strLog
.Format("menu language '%s'", language
.language
);
597 PrintToStdOut(strLog
);
606 bool ProcessCommandVEN(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
608 if (command
== "ven")
611 if (GetWord(arguments
, strDev
))
613 int iDev
= atoi(strDev
);
614 if (iDev
>= 0 && iDev
< 15)
616 uint64_t iVendor
= parser
->GetDeviceVendorId((cec_logical_address
) iDev
);
617 PrintToStdOut("vendor id: %06x", iVendor
);
626 bool ProcessCommandVER(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
628 if (command
== "ver")
631 if (GetWord(arguments
, strDev
))
633 int iDev
= atoi(strDev
);
634 if (iDev
>= 0 && iDev
< 15)
636 cec_version iVersion
= parser
->GetDeviceCecVersion((cec_logical_address
) iDev
);
637 PrintToStdOut("CEC version %s", parser
->ToString(iVersion
));
646 bool ProcessCommandPOW(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
648 if (command
== "pow")
651 if (GetWord(arguments
, strDev
))
653 int iDev
= atoi(strDev
);
654 if (iDev
>= 0 && iDev
< 15)
656 cec_power_status iPower
= parser
->GetDevicePowerStatus((cec_logical_address
) iDev
);
657 PrintToStdOut("power status: %s", parser
->ToString(iPower
));
666 bool ProcessCommandNAME(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
668 if (command
== "name")
671 if (GetWord(arguments
, strDev
))
673 int iDev
= atoi(strDev
);
674 if (iDev
>= 0 && iDev
< 15)
676 cec_osd_name name
= parser
->GetDeviceOSDName((cec_logical_address
)iDev
);
677 PrintToStdOut("OSD name of device %d is '%s'", iDev
, name
.name
);
686 bool ProcessCommandLAD(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
688 if (command
== "lad")
690 PrintToStdOut("listing active devices:");
691 cec_logical_addresses addresses
= parser
->GetActiveDevices();
692 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
695 PrintToStdOut("logical address %X", (int)iPtr
);
703 bool ProcessCommandAD(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
708 if (GetWord(arguments
, strDev
))
710 int iDev
= atoi(strDev
);
711 if (iDev
>= 0 && iDev
< 15)
712 PrintToStdOut("logical address %X is %s", iDev
, (parser
->IsActiveDevice((cec_logical_address
)iDev
) ? "active" : "not active"));
719 bool ProcessCommandAT(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
724 if (GetWord(arguments
, strType
))
726 cec_device_type type
= CEC_DEVICE_TYPE_TV
;
727 if (strType
.Equals("a"))
728 type
= CEC_DEVICE_TYPE_AUDIO_SYSTEM
;
729 else if (strType
.Equals("p"))
730 type
= CEC_DEVICE_TYPE_PLAYBACK_DEVICE
;
731 else if (strType
.Equals("r"))
732 type
= CEC_DEVICE_TYPE_RECORDING_DEVICE
;
733 else if (strType
.Equals("t"))
734 type
= CEC_DEVICE_TYPE_TUNER
;
736 PrintToStdOut("device %d is %s", type
, (parser
->IsActiveDeviceType(type
) ? "active" : "not active"));
744 bool ProcessCommandR(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
748 PrintToStdOut("closing the connection");
751 PrintToStdOut("opening a new connection");
752 parser
->Open(g_strPort
.c_str());
754 PrintToStdOut("setting active source");
755 parser
->SetActiveSource();
762 bool ProcessCommandH(ICECAdapter
* UNUSED(parser
), const string
&command
, string
& UNUSED(arguments
))
764 if (command
== "h" || command
== "help")
773 bool ProcessCommandLOG(ICECAdapter
* UNUSED(parser
), const string
&command
, string
&arguments
)
775 if (command
== "log")
778 if (GetWord(arguments
, strLevel
))
780 int iNewLevel
= atoi(strLevel
);
781 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
783 g_cecLogLevel
= iNewLevel
;
785 PrintToStdOut("log level changed to %s", strLevel
.c_str());
794 bool ProcessCommandSCAN(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
796 if (command
== "scan")
798 PrintToStdOut("CEC bus information");
799 PrintToStdOut("===================");
800 cec_logical_addresses addresses
= parser
->GetActiveDevices();
801 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
806 uint64_t iVendorId
= parser
->GetDeviceVendorId((cec_logical_address
)iPtr
);
807 bool bActive
= parser
->IsActiveSource((cec_logical_address
)iPtr
);
808 uint16_t iPhysicalAddress
= parser
->GetDevicePhysicalAddress((cec_logical_address
)iPtr
);
809 cec_version iCecVersion
= parser
->GetDeviceCecVersion((cec_logical_address
)iPtr
);
810 cec_power_status power
= parser
->GetDevicePowerStatus((cec_logical_address
)iPtr
);
811 cec_osd_name osdName
= parser
->GetDeviceOSDName((cec_logical_address
)iPtr
);
813 strAddr
.Format("%04x", iPhysicalAddress
);
814 cec_menu_language lang
;
815 lang
.device
= CECDEVICE_UNKNOWN
;
816 parser
->GetDeviceMenuLanguage((cec_logical_address
)iPtr
, &lang
);
818 strLog
.AppendFormat("device #%X: %s\n", (int)iPtr
, parser
->ToString((cec_logical_address
)iPtr
));
819 strLog
.AppendFormat("address: %s\n", strAddr
.c_str());
820 strLog
.AppendFormat("active source: %s\n", (bActive
? "yes" : "no"));
821 strLog
.AppendFormat("vendor: %s\n", parser
->ToString((cec_vendor_id
)iVendorId
));
822 strLog
.AppendFormat("osd string: %s\n", osdName
.name
);
823 strLog
.AppendFormat("CEC version: %s\n", parser
->ToString(iCecVersion
));
824 strLog
.AppendFormat("power status: %s\n", parser
->ToString(power
));
825 if ((uint8_t)lang
.device
== iPtr
)
826 strLog
.AppendFormat("language: %s\n", lang
.language
);
828 PrintToStdOut(strLog
);
837 bool ProcessConsoleCommand(ICECAdapter
*parser
, string
&input
)
842 if (GetWord(input
, command
))
844 if (command
== "q" || command
== "quit")
847 ProcessCommandTX(parser
, command
, input
) ||
848 ProcessCommandON(parser
, command
, input
) ||
849 ProcessCommandSTANDBY(parser
, command
, input
) ||
850 ProcessCommandPOLL(parser
, command
, input
) ||
851 ProcessCommandLA(parser
, command
, input
) ||
852 ProcessCommandP(parser
, command
, input
) ||
853 ProcessCommandPA(parser
, command
, input
) ||
854 ProcessCommandAS(parser
, command
, input
) ||
855 ProcessCommandOSD(parser
, command
, input
) ||
856 ProcessCommandPING(parser
, command
, input
) ||
857 ProcessCommandVOLUP(parser
, command
, input
) ||
858 ProcessCommandVOLDOWN(parser
, command
, input
) ||
859 ProcessCommandMUTE(parser
, command
, input
) ||
860 ProcessCommandMON(parser
, command
, input
) ||
861 ProcessCommandBL(parser
, command
, input
) ||
862 ProcessCommandLANG(parser
, command
, input
) ||
863 ProcessCommandVEN(parser
, command
, input
) ||
864 ProcessCommandVER(parser
, command
, input
) ||
865 ProcessCommandPOW(parser
, command
, input
) ||
866 ProcessCommandNAME(parser
, command
, input
) ||
867 ProcessCommandLAD(parser
, command
, input
) ||
868 ProcessCommandAD(parser
, command
, input
) ||
869 ProcessCommandAT(parser
, command
, input
) ||
870 ProcessCommandR(parser
, command
, input
) ||
871 ProcessCommandH(parser
, command
, input
) ||
872 ProcessCommandLOG(parser
, command
, input
) ||
873 ProcessCommandSCAN(parser
, command
, input
) ||
874 ProcessCommandSP(parser
, command
, input
) ||
875 ProcessCommandSPL(parser
, command
, input
) ||
876 ProcessCommandSELF(parser
, command
, input
);
882 bool ProcessCommandLineArguments(int argc
, char *argv
[])
886 while (iArgPtr
< argc
&& bReturn
)
888 if (argc
>= iArgPtr
+ 1)
890 if (!strcmp(argv
[iArgPtr
], "-f") ||
891 !strcmp(argv
[iArgPtr
], "--log-file") ||
892 !strcmp(argv
[iArgPtr
], "-sf") ||
893 !strcmp(argv
[iArgPtr
], "--short-log-file"))
895 if (argc
>= iArgPtr
+ 2)
897 g_logOutput
.open(argv
[iArgPtr
+ 1]);
898 g_bShortLog
= (!strcmp(argv
[iArgPtr
], "-sf") || !strcmp(argv
[iArgPtr
], "--short-log-file"));
903 cout
<< "== skipped log-file parameter: no file given ==" << endl
;
907 else if (!strcmp(argv
[iArgPtr
], "-d") ||
908 !strcmp(argv
[iArgPtr
], "--log-level"))
910 if (argc
>= iArgPtr
+ 2)
912 int iNewLevel
= atoi(argv
[iArgPtr
+ 1]);
913 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
915 g_cecLogLevel
= iNewLevel
;
916 if (!g_bSingleCommand
)
917 cout
<< "log level set to " << argv
[iArgPtr
+ 1] << endl
;
921 cout
<< "== skipped log-level parameter: invalid level '" << argv
[iArgPtr
+ 1] << "' ==" << endl
;
927 cout
<< "== skipped log-level parameter: no level given ==" << endl
;
931 else if (!strcmp(argv
[iArgPtr
], "-t") ||
932 !strcmp(argv
[iArgPtr
], "--type"))
934 if (argc
>= iArgPtr
+ 2)
936 if (!strcmp(argv
[iArgPtr
+ 1], "p"))
938 if (!g_bSingleCommand
)
939 cout
<< "== using device type 'playback device'" << endl
;
940 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
942 else if (!strcmp(argv
[iArgPtr
+ 1], "r"))
944 if (!g_bSingleCommand
)
945 cout
<< "== using device type 'recording device'" << endl
;
946 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
948 else if (!strcmp(argv
[iArgPtr
+ 1], "t"))
950 if (!g_bSingleCommand
)
951 cout
<< "== using device type 'tuner'" << endl
;
952 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_TUNER
);
954 else if (!strcmp(argv
[iArgPtr
+ 1], "a"))
956 if (!g_bSingleCommand
)
957 cout
<< "== using device type 'audio system'" << endl
;
958 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_AUDIO_SYSTEM
);
962 cout
<< "== skipped invalid device type '" << argv
[iArgPtr
+ 1] << "'" << endl
;
968 else if (!strcmp(argv
[iArgPtr
], "--list-devices") ||
969 !strcmp(argv
[iArgPtr
], "-l"))
971 ICECAdapter
*parser
= LibCecInitialise(&g_config
);
975 UnloadLibCec(parser
);
980 else if (!strcmp(argv
[iArgPtr
], "--bootloader"))
985 else if (!strcmp(argv
[iArgPtr
], "--single-command") ||
986 !strcmp(argv
[iArgPtr
], "-s"))
988 g_bSingleCommand
= true;
991 else if (!strcmp(argv
[iArgPtr
], "--help") ||
992 !strcmp(argv
[iArgPtr
], "-h"))
994 ShowHelpCommandLine(argv
[0]);
997 else if (!strcmp(argv
[iArgPtr
], "-b") ||
998 !strcmp(argv
[iArgPtr
], "--base"))
1000 if (argc
>= iArgPtr
+ 2)
1002 g_config
.baseDevice
= (cec_logical_address
)atoi(argv
[iArgPtr
+ 1]);
1003 cout
<< "using base device '" << (int)g_config
.baseDevice
<< "'" << endl
;
1008 else if (!strcmp(argv
[iArgPtr
], "-p") ||
1009 !strcmp(argv
[iArgPtr
], "--port"))
1011 if (argc
>= iArgPtr
+ 2)
1013 g_config
.iHDMIPort
= (int8_t)atoi(argv
[iArgPtr
+ 1]);
1014 cout
<< "using HDMI port '" << (int)g_config
.iHDMIPort
<< "'" << endl
;
1019 else if (!strcmp(argv
[iArgPtr
], "-r") ||
1020 !strcmp(argv
[iArgPtr
], "--rom"))
1022 cout
<< "using settings from EEPROM" << endl
;
1023 g_config
.bGetSettingsFromROM
= 1;
1026 else if (!strcmp(argv
[iArgPtr
], "-o") ||
1027 !strcmp(argv
[iArgPtr
], "--osd-name"))
1029 if (argc
>= iArgPtr
+ 2)
1031 snprintf(g_config
.strDeviceName
, 13, argv
[iArgPtr
+ 1]);
1032 cout
<< "using osd name " << g_config
.strDeviceName
<< endl
;
1039 g_strPort
= argv
[iArgPtr
++];
1047 int main (int argc
, char *argv
[])
1050 snprintf(g_config
.strDeviceName
, 13, "CECTester");
1051 g_config
.callbackParam
= NULL
;
1052 g_config
.clientVersion
= CEC_CLIENT_VERSION_1_5_0
;
1053 g_callbacks
.CBCecLogMessage
= &CecLogMessage
;
1054 g_callbacks
.CBCecKeyPress
= &CecKeyPress
;
1055 g_callbacks
.CBCecCommand
= &CecCommand
;
1056 g_config
.callbacks
= &g_callbacks
;
1058 if (!ProcessCommandLineArguments(argc
, argv
))
1061 if (g_config
.deviceTypes
.IsEmpty())
1063 if (!g_bSingleCommand
)
1064 cout
<< "No device type given. Using 'recording device'" << endl
;
1065 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
1068 ICECAdapter
*parser
= LibCecInitialise(&g_config
);
1072 cout
<< "Cannot load libcec.dll" << endl
;
1074 cout
<< "Cannot load libcec.so" << endl
;
1078 UnloadLibCec(parser
);
1083 if (!g_bSingleCommand
)
1086 strLog
.Format("CEC Parser created - libCEC version %s", parser
->ToString((cec_server_version
)g_config
.serverVersion
));
1087 cout
<< strLog
.c_str() << endl
;
1089 //make stdin non-blocking
1091 int flags
= fcntl(0, F_GETFL
, 0);
1092 flags
|= O_NONBLOCK
;
1093 fcntl(0, F_SETFL
, flags
);
1097 if (g_strPort
.IsEmpty())
1099 if (!g_bSingleCommand
)
1100 cout
<< "no serial port given. trying autodetect: ";
1101 cec_adapter devices
[10];
1102 uint8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
1103 if (iDevicesFound
<= 0)
1105 if (g_bSingleCommand
)
1106 cout
<< "autodetect ";
1107 cout
<< "FAILED" << endl
;
1108 UnloadLibCec(parser
);
1113 if (!g_bSingleCommand
)
1115 cout
<< endl
<< " path: " << devices
[0].path
<< endl
<<
1116 " com port: " << devices
[0].comm
<< endl
<< endl
;
1118 g_strPort
= devices
[0].comm
;
1122 PrintToStdOut("opening a connection to the CEC adapter...");
1124 if (!parser
->Open(g_strPort
.c_str()))
1126 PrintToStdOut("unable to open the device on port %s", g_strPort
.c_str());
1127 UnloadLibCec(parser
);
1131 if (!g_bSingleCommand
)
1132 PrintToStdOut("waiting for input");
1134 while (!g_bExit
&& !g_bHardExit
)
1137 getline(cin
, input
);
1140 if (ProcessConsoleCommand(parser
, input
) && !g_bSingleCommand
&& !g_bExit
&& !g_bHardExit
)
1143 PrintToStdOut("waiting for input");
1148 if (!g_bExit
&& !g_bHardExit
)
1153 UnloadLibCec(parser
);
1155 if (g_logOutput
.is_open())
1156 g_logOutput
.close();