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_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 "[self] show the list of addresses controlled by libCEC" << endl
<<
282 "[scan] scan the CEC bus and display device info" << endl
<<
283 "[mon] {1|0} enable or disable CEC bus monitoring." << endl
<<
284 "[log] {1 - 31} change the log level. see cectypes.h for values." << endl
<<
285 "[ping] send a ping command to the CEC adapter." << endl
<<
286 "[bl] to let the adapter enter the bootloader, to upgrade" << endl
<<
287 " the flash rom." << endl
<<
288 "[r] reconnect to the CEC adapter." << endl
<<
289 "[h] or [help] show this help." << endl
<<
290 "[q] or [quit] to quit the CEC test client and switch off all" << endl
<<
291 " connected CEC devices." << endl
<<
292 "================================================================================" << endl
;
295 bool ProcessCommandSELF(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
297 if (command
== "self")
299 cec_logical_addresses addr
= parser
->GetLogicalAddresses();
300 CStdString strOut
= "Addresses controlled by libCEC: ";
302 for (uint8_t iPtr
= 0; iPtr
<= 15; iPtr
++)
306 strOut
.AppendFormat((bFirst
? "%d%s" : ", %d%s"), iPtr
, parser
->IsActiveSource((cec_logical_address
)iPtr
) ? "*" : "");
310 PrintToStdOut(strOut
.c_str());
317 bool ProcessCommandSP(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
323 if (GetWord(arguments
, strAddress
))
325 sscanf(strAddress
.c_str(), "%x", &iAddress
);
326 if (iAddress
>= 0 && iAddress
<= 0xFFFF)
327 parser
->SetStreamPath((uint16_t)iAddress
);
335 bool ProcessCommandSPL(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
337 if (command
== "spl")
340 cec_logical_address iAddress
;
341 if (GetWord(arguments
, strAddress
))
343 iAddress
= (cec_logical_address
)atoi(strAddress
.c_str());
344 if (iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
345 parser
->SetStreamPath(iAddress
);
353 bool ProcessCommandTX(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
355 if (command
== "tx" || command
== "txn")
362 while (GetWord(arguments
, strvalue
) && HexStrToInt(strvalue
, ivalue
))
363 bytes
.PushBack(ivalue
);
365 if (command
== "txn")
366 bytes
.transmit_timeout
= 0;
368 parser
->Transmit(bytes
);
376 bool ProcessCommandON(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
382 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
384 parser
->PowerOnDevices((cec_logical_address
) iValue
);
389 PrintToStdOut("invalid destination");
396 bool ProcessCommandSTANDBY(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
398 if (command
== "standby")
402 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
404 parser
->StandbyDevices((cec_logical_address
) iValue
);
409 PrintToStdOut("invalid destination");
416 bool ProcessCommandPOLL(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
418 if (command
== "poll")
422 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
424 if (parser
->PollDevice((cec_logical_address
) iValue
))
425 PrintToStdOut("POLL message sent");
427 PrintToStdOut("POLL message not sent");
432 PrintToStdOut("invalid destination");
439 bool ProcessCommandLA(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
444 if (GetWord(arguments
, strvalue
))
446 parser
->SetLogicalAddress((cec_logical_address
) atoi(strvalue
.c_str()));
454 bool ProcessCommandP(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
458 string strPort
, strDevice
;
459 if (GetWord(arguments
, strDevice
) && GetWord(arguments
, strPort
))
461 parser
->SetHDMIPort((cec_logical_address
)atoi(strDevice
.c_str()), (uint8_t)atoi(strPort
.c_str()));
469 bool ProcessCommandPA(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
475 if (GetWord(arguments
, strB1
) && HexStrToInt(strB1
, iB1
) &&
476 GetWord(arguments
, strB2
) && HexStrToInt(strB2
, iB2
))
478 uint16_t iPhysicalAddress
= ((uint16_t)iB1
<< 8) + iB2
;
479 parser
->SetPhysicalAddress(iPhysicalAddress
);
487 bool ProcessCommandOSD(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
489 if (command
== "osd")
491 bool bFirstWord(false);
492 string strAddr
, strMessage
, strWord
;
494 if (GetWord(arguments
, strAddr
) && HexStrToInt(strAddr
, iAddr
) && iAddr
< 0xF)
496 while (GetWord(arguments
, strWord
))
501 strMessage
.append(" ");
503 strMessage
.append(strWord
);
505 parser
->SetOSDString((cec_logical_address
) iAddr
, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME
, strMessage
.c_str());
513 bool ProcessCommandAS(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
517 parser
->SetActiveView();
525 bool ProcessCommandPING(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
527 if (command
== "ping")
529 parser
->PingAdapter();
536 bool ProcessCommandVOLUP(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
538 if (command
== "volup")
540 PrintToStdOut("volume up: %2X", parser
->VolumeUp());
547 bool ProcessCommandVOLDOWN(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
549 if (command
== "voldown")
551 PrintToStdOut("volume down: %2X", parser
->VolumeDown());
558 bool ProcessCommandMUTE(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
560 if (command
== "mute")
562 PrintToStdOut("mute: %2X", parser
->MuteAudio());
569 bool ProcessCommandMON(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
571 if (command
== "mon")
573 CStdString strEnable
;
574 if (GetWord(arguments
, strEnable
) && (strEnable
.Equals("0") || strEnable
.Equals("1")))
576 parser
->SwitchMonitoring(strEnable
.Equals("1"));
584 bool ProcessCommandBL(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
588 if (parser
->StartBootloader())
590 PrintToStdOut("entered bootloader mode. exiting cec-client");
600 bool ProcessCommandLANG(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
602 if (command
== "lang")
605 if (GetWord(arguments
, strDev
))
607 int iDev
= atoi(strDev
);
608 if (iDev
>= 0 && iDev
< 15)
611 cec_menu_language language
;
612 if (parser
->GetDeviceMenuLanguage((cec_logical_address
) iDev
, &language
))
613 strLog
.Format("menu language '%s'", language
.language
);
616 PrintToStdOut(strLog
);
625 bool ProcessCommandVEN(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
627 if (command
== "ven")
630 if (GetWord(arguments
, strDev
))
632 int iDev
= atoi(strDev
);
633 if (iDev
>= 0 && iDev
< 15)
635 uint64_t iVendor
= parser
->GetDeviceVendorId((cec_logical_address
) iDev
);
636 PrintToStdOut("vendor id: %06x", iVendor
);
645 bool ProcessCommandVER(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
647 if (command
== "ver")
650 if (GetWord(arguments
, strDev
))
652 int iDev
= atoi(strDev
);
653 if (iDev
>= 0 && iDev
< 15)
655 cec_version iVersion
= parser
->GetDeviceCecVersion((cec_logical_address
) iDev
);
656 PrintToStdOut("CEC version %s", parser
->ToString(iVersion
));
665 bool ProcessCommandPOW(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
667 if (command
== "pow")
670 if (GetWord(arguments
, strDev
))
672 int iDev
= atoi(strDev
);
673 if (iDev
>= 0 && iDev
< 15)
675 cec_power_status iPower
= parser
->GetDevicePowerStatus((cec_logical_address
) iDev
);
676 PrintToStdOut("power status: %s", parser
->ToString(iPower
));
685 bool ProcessCommandNAME(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
687 if (command
== "name")
690 if (GetWord(arguments
, strDev
))
692 int iDev
= atoi(strDev
);
693 if (iDev
>= 0 && iDev
< 15)
695 cec_osd_name name
= parser
->GetDeviceOSDName((cec_logical_address
)iDev
);
696 PrintToStdOut("OSD name of device %d is '%s'", iDev
, name
.name
);
705 bool ProcessCommandLAD(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
707 if (command
== "lad")
709 PrintToStdOut("listing active devices:");
710 cec_logical_addresses addresses
= parser
->GetActiveDevices();
711 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
714 PrintToStdOut("logical address %X", (int)iPtr
);
722 bool ProcessCommandAD(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
727 if (GetWord(arguments
, strDev
))
729 int iDev
= atoi(strDev
);
730 if (iDev
>= 0 && iDev
< 15)
731 PrintToStdOut("logical address %X is %s", iDev
, (parser
->IsActiveDevice((cec_logical_address
)iDev
) ? "active" : "not active"));
738 bool ProcessCommandAT(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
743 if (GetWord(arguments
, strType
))
745 cec_device_type type
= CEC_DEVICE_TYPE_TV
;
746 if (strType
.Equals("a"))
747 type
= CEC_DEVICE_TYPE_AUDIO_SYSTEM
;
748 else if (strType
.Equals("p"))
749 type
= CEC_DEVICE_TYPE_PLAYBACK_DEVICE
;
750 else if (strType
.Equals("r"))
751 type
= CEC_DEVICE_TYPE_RECORDING_DEVICE
;
752 else if (strType
.Equals("t"))
753 type
= CEC_DEVICE_TYPE_TUNER
;
755 PrintToStdOut("device %d is %s", type
, (parser
->IsActiveDeviceType(type
) ? "active" : "not active"));
763 bool ProcessCommandR(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
767 PrintToStdOut("closing the connection");
770 PrintToStdOut("opening a new connection");
771 parser
->Open(g_strPort
.c_str());
773 PrintToStdOut("setting active source");
774 parser
->SetActiveSource();
781 bool ProcessCommandH(ICECAdapter
* UNUSED(parser
), const string
&command
, string
& UNUSED(arguments
))
783 if (command
== "h" || command
== "help")
792 bool ProcessCommandLOG(ICECAdapter
* UNUSED(parser
), const string
&command
, string
&arguments
)
794 if (command
== "log")
797 if (GetWord(arguments
, strLevel
))
799 int iNewLevel
= atoi(strLevel
);
800 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
802 g_cecLogLevel
= iNewLevel
;
804 PrintToStdOut("log level changed to %s", strLevel
.c_str());
813 bool ProcessCommandSCAN(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
815 if (command
== "scan")
817 PrintToStdOut("CEC bus information");
818 PrintToStdOut("===================");
819 cec_logical_addresses addresses
= parser
->GetActiveDevices();
820 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
825 uint64_t iVendorId
= parser
->GetDeviceVendorId((cec_logical_address
)iPtr
);
826 bool bActive
= parser
->IsActiveSource((cec_logical_address
)iPtr
);
827 uint16_t iPhysicalAddress
= parser
->GetDevicePhysicalAddress((cec_logical_address
)iPtr
);
828 cec_version iCecVersion
= parser
->GetDeviceCecVersion((cec_logical_address
)iPtr
);
829 cec_power_status power
= parser
->GetDevicePowerStatus((cec_logical_address
)iPtr
);
830 cec_osd_name osdName
= parser
->GetDeviceOSDName((cec_logical_address
)iPtr
);
832 strAddr
.Format("%04x", iPhysicalAddress
);
833 cec_menu_language lang
;
834 lang
.device
= CECDEVICE_UNKNOWN
;
835 parser
->GetDeviceMenuLanguage((cec_logical_address
)iPtr
, &lang
);
837 strLog
.AppendFormat("device #%X: %s\n", (int)iPtr
, parser
->ToString((cec_logical_address
)iPtr
));
838 strLog
.AppendFormat("address: %s\n", strAddr
.c_str());
839 strLog
.AppendFormat("active source: %s\n", (bActive
? "yes" : "no"));
840 strLog
.AppendFormat("vendor: %s\n", parser
->ToString((cec_vendor_id
)iVendorId
));
841 strLog
.AppendFormat("osd string: %s\n", osdName
.name
);
842 strLog
.AppendFormat("CEC version: %s\n", parser
->ToString(iCecVersion
));
843 strLog
.AppendFormat("power status: %s\n", parser
->ToString(power
));
844 if ((uint8_t)lang
.device
== iPtr
)
845 strLog
.AppendFormat("language: %s\n", lang
.language
);
847 PrintToStdOut(strLog
);
856 bool ProcessConsoleCommand(ICECAdapter
*parser
, string
&input
)
861 if (GetWord(input
, command
))
863 if (command
== "q" || command
== "quit")
866 ProcessCommandTX(parser
, command
, input
) ||
867 ProcessCommandON(parser
, command
, input
) ||
868 ProcessCommandSTANDBY(parser
, command
, input
) ||
869 ProcessCommandPOLL(parser
, command
, input
) ||
870 ProcessCommandLA(parser
, command
, input
) ||
871 ProcessCommandP(parser
, command
, input
) ||
872 ProcessCommandPA(parser
, command
, input
) ||
873 ProcessCommandAS(parser
, command
, input
) ||
874 ProcessCommandOSD(parser
, command
, input
) ||
875 ProcessCommandPING(parser
, command
, input
) ||
876 ProcessCommandVOLUP(parser
, command
, input
) ||
877 ProcessCommandVOLDOWN(parser
, command
, input
) ||
878 ProcessCommandMUTE(parser
, command
, input
) ||
879 ProcessCommandMON(parser
, command
, input
) ||
880 ProcessCommandBL(parser
, command
, input
) ||
881 ProcessCommandLANG(parser
, command
, input
) ||
882 ProcessCommandVEN(parser
, command
, input
) ||
883 ProcessCommandVER(parser
, command
, input
) ||
884 ProcessCommandPOW(parser
, command
, input
) ||
885 ProcessCommandNAME(parser
, command
, input
) ||
886 ProcessCommandLAD(parser
, command
, input
) ||
887 ProcessCommandAD(parser
, command
, input
) ||
888 ProcessCommandAT(parser
, command
, input
) ||
889 ProcessCommandR(parser
, command
, input
) ||
890 ProcessCommandH(parser
, command
, input
) ||
891 ProcessCommandLOG(parser
, command
, input
) ||
892 ProcessCommandSCAN(parser
, command
, input
) ||
893 ProcessCommandSP(parser
, command
, input
) ||
894 ProcessCommandSPL(parser
, command
, input
) ||
895 ProcessCommandSELF(parser
, command
, input
);
901 bool ProcessCommandLineArguments(int argc
, char *argv
[])
905 while (iArgPtr
< argc
&& bReturn
)
907 if (argc
>= iArgPtr
+ 1)
909 if (!strcmp(argv
[iArgPtr
], "-f") ||
910 !strcmp(argv
[iArgPtr
], "--log-file") ||
911 !strcmp(argv
[iArgPtr
], "-sf") ||
912 !strcmp(argv
[iArgPtr
], "--short-log-file"))
914 if (argc
>= iArgPtr
+ 2)
916 g_logOutput
.open(argv
[iArgPtr
+ 1]);
917 g_bShortLog
= (!strcmp(argv
[iArgPtr
], "-sf") || !strcmp(argv
[iArgPtr
], "--short-log-file"));
922 cout
<< "== skipped log-file parameter: no file given ==" << endl
;
926 else if (!strcmp(argv
[iArgPtr
], "-d") ||
927 !strcmp(argv
[iArgPtr
], "--log-level"))
929 if (argc
>= iArgPtr
+ 2)
931 int iNewLevel
= atoi(argv
[iArgPtr
+ 1]);
932 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
934 g_cecLogLevel
= iNewLevel
;
935 if (!g_bSingleCommand
)
936 cout
<< "log level set to " << argv
[iArgPtr
+ 1] << endl
;
940 cout
<< "== skipped log-level parameter: invalid level '" << argv
[iArgPtr
+ 1] << "' ==" << endl
;
946 cout
<< "== skipped log-level parameter: no level given ==" << endl
;
950 else if (!strcmp(argv
[iArgPtr
], "-t") ||
951 !strcmp(argv
[iArgPtr
], "--type"))
953 if (argc
>= iArgPtr
+ 2)
955 if (!strcmp(argv
[iArgPtr
+ 1], "p"))
957 if (!g_bSingleCommand
)
958 cout
<< "== using device type 'playback device'" << endl
;
959 g_typeList
.add(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
961 else if (!strcmp(argv
[iArgPtr
+ 1], "r"))
963 if (!g_bSingleCommand
)
964 cout
<< "== using device type 'recording device'" << endl
;
965 g_typeList
.add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
967 else if (!strcmp(argv
[iArgPtr
+ 1], "t"))
969 if (!g_bSingleCommand
)
970 cout
<< "== using device type 'tuner'" << endl
;
971 g_typeList
.add(CEC_DEVICE_TYPE_TUNER
);
973 else if (!strcmp(argv
[iArgPtr
+ 1], "a"))
975 if (!g_bSingleCommand
)
976 cout
<< "== using device type 'audio system'" << endl
;
977 g_typeList
.add(CEC_DEVICE_TYPE_AUDIO_SYSTEM
);
981 cout
<< "== skipped invalid device type '" << argv
[iArgPtr
+ 1] << "'" << endl
;
987 else if (!strcmp(argv
[iArgPtr
], "--list-devices") ||
988 !strcmp(argv
[iArgPtr
], "-l"))
990 ICECAdapter
*parser
= CreateParser(g_typeList
);
994 UnloadLibCec(parser
);
999 else if (!strcmp(argv
[iArgPtr
], "--single-command") ||
1000 !strcmp(argv
[iArgPtr
], "-s"))
1002 g_bSingleCommand
= true;
1005 else if (!strcmp(argv
[iArgPtr
], "--help") ||
1006 !strcmp(argv
[iArgPtr
], "-h"))
1008 ShowHelpCommandLine(argv
[0]);
1011 else if (!strcmp(argv
[iArgPtr
], "-b") ||
1012 !strcmp(argv
[iArgPtr
], "--base"))
1014 if (argc
>= iArgPtr
+ 2)
1016 g_iBaseDevice
= (cec_logical_address
)atoi(argv
[iArgPtr
+ 1]);
1017 cout
<< "using base device '" << (int)g_iBaseDevice
<< "'" << endl
;
1022 else if (!strcmp(argv
[iArgPtr
], "-p") ||
1023 !strcmp(argv
[iArgPtr
], "--port"))
1025 if (argc
>= iArgPtr
+ 2)
1027 g_iHDMIPort
= (int8_t)atoi(argv
[iArgPtr
+ 1]);
1028 cout
<< "using HDMI port '" << (int)g_iHDMIPort
<< "'" << endl
;
1035 g_strPort
= argv
[iArgPtr
++];
1043 int main (int argc
, char *argv
[])
1047 if (!ProcessCommandLineArguments(argc
, argv
))
1050 if (g_typeList
.IsEmpty())
1052 if (!g_bSingleCommand
)
1053 cout
<< "No device type given. Using 'recording device'" << endl
;
1054 g_typeList
.add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
1057 ICECAdapter
*parser
= LibCecInit("CECTester", g_typeList
);
1058 if (!parser
|| parser
->GetMinLibVersion() > CEC_TEST_CLIENT_VERSION
)
1061 cout
<< "Cannot load libcec.dll" << endl
;
1063 cout
<< "Cannot load libcec.so" << endl
;
1067 UnloadLibCec(parser
);
1072 if (!g_bSingleCommand
)
1075 strLog
.Format("CEC Parser created - libcec version %d.%d", parser
->GetLibVersionMajor(), parser
->GetLibVersionMinor());
1076 cout
<< strLog
.c_str() << endl
;
1078 //make stdin non-blocking
1080 int flags
= fcntl(0, F_GETFL
, 0);
1081 flags
|= O_NONBLOCK
;
1082 fcntl(0, F_SETFL
, flags
);
1086 if (g_strPort
.IsEmpty())
1088 if (!g_bSingleCommand
)
1089 cout
<< "no serial port given. trying autodetect: ";
1090 cec_adapter devices
[10];
1091 uint8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
1092 if (iDevicesFound
<= 0)
1094 if (g_bSingleCommand
)
1095 cout
<< "autodetect ";
1096 cout
<< "FAILED" << endl
;
1097 UnloadLibCec(parser
);
1102 if (!g_bSingleCommand
)
1104 cout
<< endl
<< " path: " << devices
[0].path
<< endl
<<
1105 " com port: " << devices
[0].comm
<< endl
<< endl
;
1107 g_strPort
= devices
[0].comm
;
1111 EnableCallbacks(parser
);
1113 parser
->SetHDMIPort(g_iBaseDevice
, g_iHDMIPort
);
1114 PrintToStdOut("opening a connection to the CEC adapter...");
1116 if (!parser
->Open(g_strPort
.c_str()))
1118 PrintToStdOut("unable to open the device on port %s", g_strPort
.c_str());
1119 UnloadLibCec(parser
);
1123 if (!g_bSingleCommand
)
1125 PrintToStdOut("cec device opened");
1127 parser
->PowerOnDevices(CECDEVICE_TV
);
1128 parser
->SetActiveSource();
1130 PrintToStdOut("waiting for input");
1133 while (!g_bExit
&& !g_bHardExit
)
1136 getline(cin
, input
);
1139 if (ProcessConsoleCommand(parser
, input
) && !g_bSingleCommand
&& !g_bExit
&& !g_bHardExit
)
1142 PrintToStdOut("waiting for input");
1147 if (!g_bExit
&& !g_bHardExit
)
1148 CCondition::Sleep(50);
1151 if (!g_bSingleCommand
&& !g_bHardExit
)
1152 parser
->StandbyDevices(CECDEVICE_BROADCAST
);
1155 UnloadLibCec(parser
);
1157 if (g_logOutput
.is_open())
1158 g_logOutput
.close();