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/
41 #include "../lib/platform/os.h"
42 #include "../lib/implementations/CECCommandHandler.h"
46 using namespace PLATFORM
;
48 #define CEC_TEST_CLIENT_VERSION 1
50 #include <cecloader.h>
52 int g_cecLogLevel(CEC_LOG_ALL
);
54 bool g_bShortLog(false);
56 uint8_t g_iHDMIPort(CEC_DEFAULT_HDMI_PORT
);
57 cec_logical_address
g_iBaseDevice((cec_logical_address
)CEC_DEFAULT_BASE_DEVICE
);
58 cec_device_type_list g_typeList
;
59 bool g_bSingleCommand(false);
61 bool g_bHardExit(false);
63 ICECCallbacks g_callbacks
;
65 inline void PrintToStdOut(const char *strFormat
, ...)
70 va_start(argList
, strFormat
);
71 strLog
.FormatV(strFormat
, argList
);
74 CLockObject
lock(g_outputMutex
);
75 cout
<< strLog
<< endl
;
78 inline bool HexStrToInt(const std::string
& data
, uint8_t& value
)
81 if (sscanf(data
.c_str(), "%x", &iTmp
) == 1)
88 value
= (uint8_t) iTmp
;
96 //get the first word (separated by whitespace) from string data and place that in word
97 //then remove that word from string data
98 bool GetWord(string
& data
, string
& word
)
100 stringstream
datastream(data
);
104 if (datastream
.fail())
110 size_t pos
= data
.find(word
) + word
.length();
112 if (pos
>= data
.length())
118 data
= data
.substr(pos
);
121 datastream
.str(data
);
124 if (datastream
.fail())
130 int CecLogMessage(void *UNUSED(cbParam
), const cec_log_message
&message
)
132 if ((message
.level
& g_cecLogLevel
) == message
.level
)
135 switch (message
.level
)
138 strLevel
= "ERROR: ";
140 case CEC_LOG_WARNING
:
141 strLevel
= "WARNING: ";
144 strLevel
= "NOTICE: ";
146 case CEC_LOG_TRAFFIC
:
147 strLevel
= "TRAFFIC: ";
150 strLevel
= "DEBUG: ";
156 CStdString strFullLog
;
157 strFullLog
.Format("%s[%16lld]\t%s", strLevel
.c_str(), message
.time
, message
.message
);
158 PrintToStdOut(strFullLog
.c_str());
160 if (g_logOutput
.is_open())
163 g_logOutput
<< message
.message
<< endl
;
165 g_logOutput
<< strFullLog
.c_str() << endl
;
172 int CecKeyPress(void *UNUSED(cbParam
), const cec_keypress
&UNUSED(key
))
177 int CecCommand(void *UNUSED(cbParam
), const cec_command
&UNUSED(command
))
182 void EnableCallbacks(ICECAdapter
*adapter
)
184 g_callbacks
.CBCecLogMessage
= &CecLogMessage
;
185 g_callbacks
.CBCecKeyPress
= &CecKeyPress
;
186 g_callbacks
.CBCecCommand
= &CecCommand
;
187 adapter
->EnableCallbacks(NULL
, &g_callbacks
);
190 void ListDevices(ICECAdapter
*parser
)
192 cec_adapter
*devices
= new cec_adapter
[10];
193 int8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
194 if (iDevicesFound
<= 0)
196 PrintToStdOut("Found devices: NONE");
200 PrintToStdOut("Found devices: %d\n", iDevicesFound
);
201 for (int8_t iDevicePtr
= 0; iDevicePtr
< iDevicesFound
; iDevicePtr
++)
202 PrintToStdOut("device: %d\npath: %s\ncom port: %s\n", iDevicePtr
+ 1, devices
[iDevicePtr
].path
, devices
[iDevicePtr
].comm
);
206 void ShowHelpCommandLine(const char* strExec
)
208 CLockObject
lock(g_outputMutex
);
210 strExec
<< " {-h|--help|-l|--list-devices|[COM PORT]}" << endl
<<
212 "parameters:" << endl
<<
213 " -h --help Shows this help text" << endl
<<
214 " -l --list-devices List all devices on this system" << endl
<<
215 " -t --type {p|r|t|a} The device type to use. More than one is possible." << endl
<<
216 " -p --port {int} The HDMI port to use as active source." << endl
<<
217 " -b --base {int} The logical address of the device to with this " << endl
<<
218 " adapter is connected." << endl
<<
219 " -f --log-file {file} Writes all libCEC log message to a file" << endl
<<
220 " -sf --short-log-file {file} Writes all libCEC log message without timestamps" << endl
<<
221 " and log levels to a file." << endl
<<
222 " -d --log-level {level} Sets the log level. See cectypes.h for values." << endl
<<
223 " -s --single-command Execute a single command and exit. Does not power" << endl
<<
224 " on devices on startup and power them off on exit." << endl
<<
225 " [COM PORT] The com port to connect to. If no COM" << endl
<<
226 " port is given, the client tries to connect to the" << endl
<<
227 " first device that is detected." << endl
<<
229 "Type 'h' or 'help' and press enter after starting the client to display all " << endl
<<
230 "available commands" << endl
;
233 ICECAdapter
*CreateParser(cec_device_type_list typeList
)
235 ICECAdapter
*parser
= LibCecInit("CECTester", typeList
);
236 if (!parser
|| parser
->GetMinLibVersion() > CEC_TEST_CLIENT_VERSION
)
239 PrintToStdOut("Cannot load libcec.dll");
241 PrintToStdOut("Cannot load libcec.so");
246 PrintToStdOut("CEC Parser created - libcec version %d.%d", parser
->GetLibVersionMajor(), parser
->GetLibVersionMinor());
251 void ShowHelpConsole(void)
253 CLockObject
lock(g_outputMutex
);
255 "================================================================================" << endl
<<
256 "Available commands:" << endl
<<
258 "[tx] {bytes} transfer bytes over the CEC line." << endl
<<
259 "[txn] {bytes} transfer bytes but don't wait for transmission ACK." << endl
<<
260 "[on] {address} power on the device with the given logical address." << endl
<<
261 "[standby] {address} put the device with the given address in standby mode." << endl
<<
262 "[la] {logical address} change the logical address of the CEC adapter." << endl
<<
263 "[p] {device} {port} change the HDMI port number of the CEC adapter." << endl
<<
264 "[pa] {physical address} change the physical address of the CEC adapter." << endl
<<
265 "[as] make the CEC adapter the active source." << endl
<<
266 "[osd] {addr} {string} set OSD message on the specified device." << endl
<<
267 "[ver] {addr} get the CEC version of the specified device." << endl
<<
268 "[ven] {addr} get the vendor ID of the specified device." << endl
<<
269 "[lang] {addr} get the menu language of the specified device." << endl
<<
270 "[pow] {addr} get the power status of the specified device." << endl
<<
271 "[name] {addr} get the OSD name of the specified device." << endl
<<
272 "[poll] {addr} poll the specified device." << endl
<<
273 "[lad] lists active devices on the bus" << endl
<<
274 "[ad] {addr} checks whether the specified device is active." << endl
<<
275 "[at] {type} checks whether the specified device type is active." << endl
<<
276 "[sp] {addr} makes the specified physical address active." << endl
<<
277 "[spl] {addr} makes the specified logical address active." << endl
<<
278 "[volup] send a volume up command to the amp if present" << endl
<<
279 "[voldown] send a volume down command to the amp if present" << endl
<<
280 "[mute] send a mute/unmute command to the amp if present" << endl
<<
281 "[scan] scan the CEC bus and display device info" << endl
<<
282 "[mon] {1|0} enable or disable CEC bus monitoring." << endl
<<
283 "[log] {1 - 31} change the log level. see cectypes.h for values." << endl
<<
284 "[ping] send a ping command to the CEC adapter." << endl
<<
285 "[bl] to let the adapter enter the bootloader, to upgrade" << endl
<<
286 " the flash rom." << endl
<<
287 "[r] reconnect to the CEC adapter." << endl
<<
288 "[h] or [help] show this help." << endl
<<
289 "[q] or [quit] to quit the CEC test client and switch off all" << endl
<<
290 " connected CEC devices." << endl
<<
291 "================================================================================" << endl
;
294 bool ProcessCommandSP(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
300 if (GetWord(arguments
, strAddress
))
302 sscanf(strAddress
.c_str(), "%x", &iAddress
);
303 if (iAddress
>= 0 && iAddress
<= 0xFFFF)
304 parser
->SetStreamPath((uint16_t)iAddress
);
312 bool ProcessCommandSPL(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
314 if (command
== "spl")
317 cec_logical_address iAddress
;
318 if (GetWord(arguments
, strAddress
))
320 iAddress
= (cec_logical_address
)atoi(strAddress
.c_str());
321 if (iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
322 parser
->SetStreamPath(iAddress
);
330 bool ProcessCommandTX(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
332 if (command
== "tx" || command
== "txn")
339 while (GetWord(arguments
, strvalue
) && HexStrToInt(strvalue
, ivalue
))
340 bytes
.PushBack(ivalue
);
342 if (command
== "txn")
343 bytes
.transmit_timeout
= 0;
345 parser
->Transmit(bytes
);
353 bool ProcessCommandON(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
359 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
361 parser
->PowerOnDevices((cec_logical_address
) iValue
);
366 PrintToStdOut("invalid destination");
373 bool ProcessCommandSTANDBY(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
375 if (command
== "standby")
379 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
381 parser
->StandbyDevices((cec_logical_address
) iValue
);
386 PrintToStdOut("invalid destination");
393 bool ProcessCommandPOLL(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
395 if (command
== "poll")
399 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
401 if (parser
->PollDevice((cec_logical_address
) iValue
))
402 PrintToStdOut("POLL message sent");
404 PrintToStdOut("POLL message not sent");
409 PrintToStdOut("invalid destination");
416 bool ProcessCommandLA(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
421 if (GetWord(arguments
, strvalue
))
423 parser
->SetLogicalAddress((cec_logical_address
) atoi(strvalue
.c_str()));
431 bool ProcessCommandP(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
435 string strPort
, strDevice
;
436 if (GetWord(arguments
, strDevice
) && GetWord(arguments
, strPort
))
438 parser
->SetHDMIPort((cec_logical_address
)atoi(strDevice
.c_str()), (uint8_t)atoi(strPort
.c_str()));
446 bool ProcessCommandPA(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
452 if (GetWord(arguments
, strB1
) && HexStrToInt(strB1
, iB1
) &&
453 GetWord(arguments
, strB2
) && HexStrToInt(strB2
, iB2
))
455 uint16_t iPhysicalAddress
= ((uint16_t)iB1
<< 8) + iB2
;
456 parser
->SetPhysicalAddress(iPhysicalAddress
);
464 bool ProcessCommandOSD(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
466 if (command
== "osd")
468 bool bFirstWord(false);
469 string strAddr
, strMessage
, strWord
;
471 if (GetWord(arguments
, strAddr
) && HexStrToInt(strAddr
, iAddr
) && iAddr
< 0xF)
473 while (GetWord(arguments
, strWord
))
478 strMessage
.append(" ");
480 strMessage
.append(strWord
);
482 parser
->SetOSDString((cec_logical_address
) iAddr
, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME
, strMessage
.c_str());
490 bool ProcessCommandAS(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
494 parser
->SetActiveView();
502 bool ProcessCommandPING(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
504 if (command
== "ping")
506 parser
->PingAdapter();
513 bool ProcessCommandVOLUP(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
515 if (command
== "volup")
517 PrintToStdOut("volume up: %2X", parser
->VolumeUp());
524 bool ProcessCommandVOLDOWN(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
526 if (command
== "voldown")
528 PrintToStdOut("volume down: %2X", parser
->VolumeDown());
535 bool ProcessCommandMUTE(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
537 if (command
== "mute")
539 PrintToStdOut("mute: %2X", parser
->MuteAudio());
546 bool ProcessCommandMON(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
548 if (command
== "mon")
550 CStdString strEnable
;
551 if (GetWord(arguments
, strEnable
) && (strEnable
.Equals("0") || strEnable
.Equals("1")))
553 parser
->SwitchMonitoring(strEnable
.Equals("1"));
561 bool ProcessCommandBL(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
565 if (parser
->StartBootloader())
567 PrintToStdOut("entered bootloader mode. exiting cec-client");
577 bool ProcessCommandLANG(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
579 if (command
== "lang")
582 if (GetWord(arguments
, strDev
))
584 int iDev
= atoi(strDev
);
585 if (iDev
>= 0 && iDev
< 15)
588 cec_menu_language language
;
589 if (parser
->GetDeviceMenuLanguage((cec_logical_address
) iDev
, &language
))
590 strLog
.Format("menu language '%s'", language
.language
);
593 PrintToStdOut(strLog
);
602 bool ProcessCommandVEN(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
604 if (command
== "ven")
607 if (GetWord(arguments
, strDev
))
609 int iDev
= atoi(strDev
);
610 if (iDev
>= 0 && iDev
< 15)
612 uint64_t iVendor
= parser
->GetDeviceVendorId((cec_logical_address
) iDev
);
613 PrintToStdOut("vendor id: %06x", iVendor
);
622 bool ProcessCommandVER(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
624 if (command
== "ver")
627 if (GetWord(arguments
, strDev
))
629 int iDev
= atoi(strDev
);
630 if (iDev
>= 0 && iDev
< 15)
632 cec_version iVersion
= parser
->GetDeviceCecVersion((cec_logical_address
) iDev
);
633 PrintToStdOut("CEC version %s", parser
->ToString(iVersion
));
642 bool ProcessCommandPOW(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
644 if (command
== "pow")
647 if (GetWord(arguments
, strDev
))
649 int iDev
= atoi(strDev
);
650 if (iDev
>= 0 && iDev
< 15)
652 cec_power_status iPower
= parser
->GetDevicePowerStatus((cec_logical_address
) iDev
);
653 PrintToStdOut("power status: %s", parser
->ToString(iPower
));
662 bool ProcessCommandNAME(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
664 if (command
== "name")
667 if (GetWord(arguments
, strDev
))
669 int iDev
= atoi(strDev
);
670 if (iDev
>= 0 && iDev
< 15)
672 cec_osd_name name
= parser
->GetDeviceOSDName((cec_logical_address
)iDev
);
673 PrintToStdOut("OSD name of device %d is '%s'", iDev
, name
.name
);
682 bool ProcessCommandLAD(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
684 if (command
== "lad")
686 PrintToStdOut("listing active devices:");
687 cec_logical_addresses addresses
= parser
->GetActiveDevices();
688 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
691 PrintToStdOut("logical address %X", (int)iPtr
);
699 bool ProcessCommandAD(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
704 if (GetWord(arguments
, strDev
))
706 int iDev
= atoi(strDev
);
707 if (iDev
>= 0 && iDev
< 15)
708 PrintToStdOut("logical address %X is %s", iDev
, (parser
->IsActiveDevice((cec_logical_address
)iDev
) ? "active" : "not active"));
715 bool ProcessCommandAT(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
720 if (GetWord(arguments
, strType
))
722 cec_device_type type
= CEC_DEVICE_TYPE_TV
;
723 if (strType
.Equals("a"))
724 type
= CEC_DEVICE_TYPE_AUDIO_SYSTEM
;
725 else if (strType
.Equals("p"))
726 type
= CEC_DEVICE_TYPE_PLAYBACK_DEVICE
;
727 else if (strType
.Equals("r"))
728 type
= CEC_DEVICE_TYPE_RECORDING_DEVICE
;
729 else if (strType
.Equals("t"))
730 type
= CEC_DEVICE_TYPE_TUNER
;
732 PrintToStdOut("device %d is %s", type
, (parser
->IsActiveDeviceType(type
) ? "active" : "not active"));
740 bool ProcessCommandR(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
744 PrintToStdOut("closing the connection");
747 PrintToStdOut("opening a new connection");
748 parser
->Open(g_strPort
.c_str());
750 PrintToStdOut("setting active source");
751 parser
->SetActiveSource();
758 bool ProcessCommandH(ICECAdapter
* UNUSED(parser
), const string
&command
, string
& UNUSED(arguments
))
760 if (command
== "h" || command
== "help")
769 bool ProcessCommandLOG(ICECAdapter
* UNUSED(parser
), const string
&command
, string
&arguments
)
771 if (command
== "log")
774 if (GetWord(arguments
, strLevel
))
776 int iNewLevel
= atoi(strLevel
);
777 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
779 g_cecLogLevel
= iNewLevel
;
781 PrintToStdOut("log level changed to %s", strLevel
.c_str());
790 bool ProcessCommandSCAN(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
792 if (command
== "scan")
794 PrintToStdOut("CEC bus information");
795 PrintToStdOut("===================");
796 cec_logical_addresses addresses
= parser
->GetActiveDevices();
797 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
802 uint64_t iVendorId
= parser
->GetDeviceVendorId((cec_logical_address
)iPtr
);
803 bool bActive
= parser
->IsActiveSource((cec_logical_address
)iPtr
);
804 uint16_t iPhysicalAddress
= parser
->GetDevicePhysicalAddress((cec_logical_address
)iPtr
);
805 cec_version iCecVersion
= parser
->GetDeviceCecVersion((cec_logical_address
)iPtr
);
806 cec_power_status power
= parser
->GetDevicePowerStatus((cec_logical_address
)iPtr
);
807 cec_osd_name osdName
= parser
->GetDeviceOSDName((cec_logical_address
)iPtr
);
809 strAddr
.Format("%04x", iPhysicalAddress
);
810 cec_menu_language lang
;
811 lang
.device
= CECDEVICE_UNKNOWN
;
812 parser
->GetDeviceMenuLanguage((cec_logical_address
)iPtr
, &lang
);
814 strLog
.AppendFormat("device #%X: %s\n", (int)iPtr
, parser
->ToString((cec_logical_address
)iPtr
));
815 strLog
.AppendFormat("address: %s\n", strAddr
.c_str());
816 strLog
.AppendFormat("active source: %s\n", (bActive
? "yes" : "no"));
817 strLog
.AppendFormat("vendor: %s\n", parser
->ToString((cec_vendor_id
)iVendorId
));
818 strLog
.AppendFormat("osd string: %s\n", osdName
.name
);
819 strLog
.AppendFormat("CEC version: %s\n", parser
->ToString(iCecVersion
));
820 strLog
.AppendFormat("power status: %s\n", parser
->ToString(power
));
821 if ((uint8_t)lang
.device
== iPtr
)
822 strLog
.AppendFormat("language: %s\n", lang
.language
);
824 PrintToStdOut(strLog
);
833 bool ProcessConsoleCommand(ICECAdapter
*parser
, string
&input
)
838 if (GetWord(input
, command
))
840 if (command
== "q" || command
== "quit")
843 ProcessCommandTX(parser
, command
, input
) ||
844 ProcessCommandON(parser
, command
, input
) ||
845 ProcessCommandSTANDBY(parser
, command
, input
) ||
846 ProcessCommandPOLL(parser
, command
, input
) ||
847 ProcessCommandLA(parser
, command
, input
) ||
848 ProcessCommandP(parser
, command
, input
) ||
849 ProcessCommandPA(parser
, command
, input
) ||
850 ProcessCommandAS(parser
, command
, input
) ||
851 ProcessCommandOSD(parser
, command
, input
) ||
852 ProcessCommandPING(parser
, command
, input
) ||
853 ProcessCommandVOLUP(parser
, command
, input
) ||
854 ProcessCommandVOLDOWN(parser
, command
, input
) ||
855 ProcessCommandMUTE(parser
, command
, input
) ||
856 ProcessCommandMON(parser
, command
, input
) ||
857 ProcessCommandBL(parser
, command
, input
) ||
858 ProcessCommandLANG(parser
, command
, input
) ||
859 ProcessCommandVEN(parser
, command
, input
) ||
860 ProcessCommandVER(parser
, command
, input
) ||
861 ProcessCommandPOW(parser
, command
, input
) ||
862 ProcessCommandNAME(parser
, command
, input
) ||
863 ProcessCommandLAD(parser
, command
, input
) ||
864 ProcessCommandAD(parser
, command
, input
) ||
865 ProcessCommandAT(parser
, command
, input
) ||
866 ProcessCommandR(parser
, command
, input
) ||
867 ProcessCommandH(parser
, command
, input
) ||
868 ProcessCommandLOG(parser
, command
, input
) ||
869 ProcessCommandSCAN(parser
, command
, input
) ||
870 ProcessCommandSP(parser
, command
, input
) ||
871 ProcessCommandSPL(parser
, command
, input
);
877 bool ProcessCommandLineArguments(int argc
, char *argv
[])
881 while (iArgPtr
< argc
&& bReturn
)
883 if (argc
>= iArgPtr
+ 1)
885 if (!strcmp(argv
[iArgPtr
], "-f") ||
886 !strcmp(argv
[iArgPtr
], "--log-file") ||
887 !strcmp(argv
[iArgPtr
], "-sf") ||
888 !strcmp(argv
[iArgPtr
], "--short-log-file"))
890 if (argc
>= iArgPtr
+ 2)
892 g_logOutput
.open(argv
[iArgPtr
+ 1]);
893 g_bShortLog
= (!strcmp(argv
[iArgPtr
], "-sf") || !strcmp(argv
[iArgPtr
], "--short-log-file"));
898 cout
<< "== skipped log-file parameter: no file given ==" << endl
;
902 else if (!strcmp(argv
[iArgPtr
], "-d") ||
903 !strcmp(argv
[iArgPtr
], "--log-level"))
905 if (argc
>= iArgPtr
+ 2)
907 int iNewLevel
= atoi(argv
[iArgPtr
+ 1]);
908 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
910 g_cecLogLevel
= iNewLevel
;
911 if (!g_bSingleCommand
)
912 cout
<< "log level set to " << argv
[iArgPtr
+ 1] << endl
;
916 cout
<< "== skipped log-level parameter: invalid level '" << argv
[iArgPtr
+ 1] << "' ==" << endl
;
922 cout
<< "== skipped log-level parameter: no level given ==" << endl
;
926 else if (!strcmp(argv
[iArgPtr
], "-t") ||
927 !strcmp(argv
[iArgPtr
], "--type"))
929 if (argc
>= iArgPtr
+ 2)
931 if (!strcmp(argv
[iArgPtr
+ 1], "p"))
933 if (!g_bSingleCommand
)
934 cout
<< "== using device type 'playback device'" << endl
;
935 g_typeList
.add(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
937 else if (!strcmp(argv
[iArgPtr
+ 1], "r"))
939 if (!g_bSingleCommand
)
940 cout
<< "== using device type 'recording device'" << endl
;
941 g_typeList
.add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
943 else if (!strcmp(argv
[iArgPtr
+ 1], "t"))
945 if (!g_bSingleCommand
)
946 cout
<< "== using device type 'tuner'" << endl
;
947 g_typeList
.add(CEC_DEVICE_TYPE_TUNER
);
949 else if (!strcmp(argv
[iArgPtr
+ 1], "a"))
951 if (!g_bSingleCommand
)
952 cout
<< "== using device type 'audio system'" << endl
;
953 g_typeList
.add(CEC_DEVICE_TYPE_AUDIO_SYSTEM
);
957 cout
<< "== skipped invalid device type '" << argv
[iArgPtr
+ 1] << "'" << endl
;
963 else if (!strcmp(argv
[iArgPtr
], "--list-devices") ||
964 !strcmp(argv
[iArgPtr
], "-l"))
966 ICECAdapter
*parser
= CreateParser(g_typeList
);
970 UnloadLibCec(parser
);
975 else if (!strcmp(argv
[iArgPtr
], "--single-command") ||
976 !strcmp(argv
[iArgPtr
], "-s"))
978 g_bSingleCommand
= true;
981 else if (!strcmp(argv
[iArgPtr
], "--help") ||
982 !strcmp(argv
[iArgPtr
], "-h"))
984 ShowHelpCommandLine(argv
[0]);
987 else if (!strcmp(argv
[iArgPtr
], "-b") ||
988 !strcmp(argv
[iArgPtr
], "--base"))
990 if (argc
>= iArgPtr
+ 2)
992 g_iBaseDevice
= (cec_logical_address
)atoi(argv
[iArgPtr
+ 1]);
993 cout
<< "using base device '" << (int)g_iBaseDevice
<< "'" << endl
;
998 else if (!strcmp(argv
[iArgPtr
], "-p") ||
999 !strcmp(argv
[iArgPtr
], "--port"))
1001 if (argc
>= iArgPtr
+ 2)
1003 g_iHDMIPort
= (int8_t)atoi(argv
[iArgPtr
+ 1]);
1004 cout
<< "using HDMI port '" << (int)g_iHDMIPort
<< "'" << endl
;
1011 g_strPort
= argv
[iArgPtr
++];
1019 int main (int argc
, char *argv
[])
1023 if (!ProcessCommandLineArguments(argc
, argv
))
1026 if (g_typeList
.IsEmpty())
1028 if (!g_bSingleCommand
)
1029 cout
<< "No device type given. Using 'recording device'" << endl
;
1030 g_typeList
.add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
1033 ICECAdapter
*parser
= LibCecInit("CECTester", g_typeList
);
1034 if (!parser
|| parser
->GetMinLibVersion() > CEC_TEST_CLIENT_VERSION
)
1037 cout
<< "Cannot load libcec.dll" << endl
;
1039 cout
<< "Cannot load libcec.so" << endl
;
1043 UnloadLibCec(parser
);
1048 if (!g_bSingleCommand
)
1051 strLog
.Format("CEC Parser created - libcec version %d.%d", parser
->GetLibVersionMajor(), parser
->GetLibVersionMinor());
1052 cout
<< strLog
.c_str() << endl
;
1054 //make stdin non-blocking
1056 int flags
= fcntl(0, F_GETFL
, 0);
1057 flags
|= O_NONBLOCK
;
1058 fcntl(0, F_SETFL
, flags
);
1062 if (g_strPort
.IsEmpty())
1064 if (!g_bSingleCommand
)
1065 cout
<< "no serial port given. trying autodetect: ";
1066 cec_adapter devices
[10];
1067 uint8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
1068 if (iDevicesFound
<= 0)
1070 if (g_bSingleCommand
)
1071 cout
<< "autodetect ";
1072 cout
<< "FAILED" << endl
;
1073 UnloadLibCec(parser
);
1078 if (!g_bSingleCommand
)
1080 cout
<< endl
<< " path: " << devices
[0].path
<< endl
<<
1081 " com port: " << devices
[0].comm
<< endl
<< endl
;
1083 g_strPort
= devices
[0].comm
;
1087 EnableCallbacks(parser
);
1089 parser
->SetHDMIPort(g_iBaseDevice
, g_iHDMIPort
);
1090 PrintToStdOut("opening a connection to the CEC adapter...");
1092 if (!parser
->Open(g_strPort
.c_str()))
1094 PrintToStdOut("unable to open the device on port %s", g_strPort
.c_str());
1095 UnloadLibCec(parser
);
1099 if (!g_bSingleCommand
)
1101 PrintToStdOut("cec device opened");
1103 parser
->PowerOnDevices(CECDEVICE_TV
);
1104 parser
->SetActiveSource();
1106 PrintToStdOut("waiting for input");
1109 while (!g_bExit
&& !g_bHardExit
)
1112 getline(cin
, input
);
1115 if (ProcessConsoleCommand(parser
, input
) && !g_bSingleCommand
&& !g_bExit
&& !g_bHardExit
)
1118 PrintToStdOut("waiting for input");
1123 if (!g_bExit
&& !g_bHardExit
)
1124 CCondition::Sleep(50);
1127 if (!g_bSingleCommand
&& !g_bHardExit
)
1128 parser
->StandbyDevices(CECDEVICE_BROADCAST
);
1131 UnloadLibCec(parser
);
1133 if (g_logOutput
.is_open())
1134 g_logOutput
.close();