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 " -sf --short-log-file {file} Writes all libCEC log message without timestamps" << endl
<<
218 " and log levels to a file." << endl
<<
219 " -d --log-level {level} Sets the log level. See cectypes.h for values." << endl
<<
220 " -s --single-command Execute a single command and exit. Does not power" << endl
<<
221 " on devices on startup and power them off on exit." << endl
<<
222 " [COM PORT] The com port to connect to. If no COM" << endl
<<
223 " port is given, the client tries to connect to the" << endl
<<
224 " first device that is detected." << endl
<<
226 "Type 'h' or 'help' and press enter after starting the client to display all " << endl
<<
227 "available commands" << endl
;
230 void ShowHelpConsole(void)
232 CLockObject
lock(g_outputMutex
);
234 "================================================================================" << endl
<<
235 "Available commands:" << endl
<<
237 "[tx] {bytes} transfer bytes over the CEC line." << endl
<<
238 "[txn] {bytes} transfer bytes but don't wait for transmission ACK." << endl
<<
239 "[on] {address} power on the device with the given logical address." << endl
<<
240 "[standby] {address} put the device with the given address in standby mode." << endl
<<
241 "[la] {logical address} change the logical address of the CEC adapter." << endl
<<
242 "[p] {device} {port} change the HDMI port number of the CEC adapter." << endl
<<
243 "[pa] {physical address} change the physical address of the CEC adapter." << endl
<<
244 "[as] make the CEC adapter the active source." << endl
<<
245 "[osd] {addr} {string} set OSD message on the specified device." << endl
<<
246 "[ver] {addr} get the CEC version of the specified device." << endl
<<
247 "[ven] {addr} get the vendor ID of the specified device." << endl
<<
248 "[lang] {addr} get the menu language of the specified device." << endl
<<
249 "[pow] {addr} get the power status of the specified device." << endl
<<
250 "[name] {addr} get the OSD name of the specified device." << endl
<<
251 "[poll] {addr} poll the specified device." << endl
<<
252 "[lad] lists active devices on the bus" << endl
<<
253 "[ad] {addr} checks whether the specified device is active." << endl
<<
254 "[at] {type} checks whether the specified device type is active." << endl
<<
255 "[sp] {addr} makes the specified physical address active." << endl
<<
256 "[spl] {addr} makes the specified logical address active." << endl
<<
257 "[volup] send a volume up command to the amp if present" << endl
<<
258 "[voldown] send a volume down command to the amp if present" << endl
<<
259 "[mute] send a mute/unmute command to the amp if present" << endl
<<
260 "[self] show the list of addresses controlled by libCEC" << endl
<<
261 "[scan] scan the CEC bus and display device info" << endl
<<
262 "[mon] {1|0} enable or disable CEC bus monitoring." << endl
<<
263 "[log] {1 - 31} change the log level. see cectypes.h for values." << endl
<<
264 "[ping] send a ping command to the CEC adapter." << endl
<<
265 "[bl] to let the adapter enter the bootloader, to upgrade" << endl
<<
266 " the flash rom." << endl
<<
267 "[r] reconnect to the CEC adapter." << endl
<<
268 "[h] or [help] show this help." << endl
<<
269 "[q] or [quit] to quit the CEC test client and switch off all" << endl
<<
270 " connected CEC devices." << endl
<<
271 "================================================================================" << endl
;
274 bool ProcessCommandSELF(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
276 if (command
== "self")
278 cec_logical_addresses addr
= parser
->GetLogicalAddresses();
279 CStdString strOut
= "Addresses controlled by libCEC: ";
281 for (uint8_t iPtr
= 0; iPtr
<= 15; iPtr
++)
285 strOut
.AppendFormat((bFirst
? "%d%s" : ", %d%s"), iPtr
, parser
->IsActiveSource((cec_logical_address
)iPtr
) ? "*" : "");
289 PrintToStdOut(strOut
.c_str());
296 bool ProcessCommandSP(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
302 if (GetWord(arguments
, strAddress
))
304 sscanf(strAddress
.c_str(), "%x", &iAddress
);
305 if (iAddress
>= 0 && iAddress
<= 0xFFFF)
306 parser
->SetStreamPath((uint16_t)iAddress
);
314 bool ProcessCommandSPL(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
316 if (command
== "spl")
319 cec_logical_address iAddress
;
320 if (GetWord(arguments
, strAddress
))
322 iAddress
= (cec_logical_address
)atoi(strAddress
.c_str());
323 if (iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
324 parser
->SetStreamPath(iAddress
);
332 bool ProcessCommandTX(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
334 if (command
== "tx" || command
== "txn")
341 while (GetWord(arguments
, strvalue
) && HexStrToInt(strvalue
, ivalue
))
342 bytes
.PushBack(ivalue
);
344 if (command
== "txn")
345 bytes
.transmit_timeout
= 0;
347 parser
->Transmit(bytes
);
355 bool ProcessCommandON(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
361 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
363 parser
->PowerOnDevices((cec_logical_address
) iValue
);
368 PrintToStdOut("invalid destination");
375 bool ProcessCommandSTANDBY(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
377 if (command
== "standby")
381 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
383 parser
->StandbyDevices((cec_logical_address
) iValue
);
388 PrintToStdOut("invalid destination");
395 bool ProcessCommandPOLL(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
397 if (command
== "poll")
401 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
403 if (parser
->PollDevice((cec_logical_address
) iValue
))
404 PrintToStdOut("POLL message sent");
406 PrintToStdOut("POLL message not sent");
411 PrintToStdOut("invalid destination");
418 bool ProcessCommandLA(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
423 if (GetWord(arguments
, strvalue
))
425 parser
->SetLogicalAddress((cec_logical_address
) atoi(strvalue
.c_str()));
433 bool ProcessCommandP(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
437 string strPort
, strDevice
;
438 if (GetWord(arguments
, strDevice
) && GetWord(arguments
, strPort
))
440 parser
->SetHDMIPort((cec_logical_address
)atoi(strDevice
.c_str()), (uint8_t)atoi(strPort
.c_str()));
448 bool ProcessCommandPA(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
454 if (GetWord(arguments
, strB1
) && HexStrToInt(strB1
, iB1
) &&
455 GetWord(arguments
, strB2
) && HexStrToInt(strB2
, iB2
))
457 uint16_t iPhysicalAddress
= ((uint16_t)iB1
<< 8) + iB2
;
458 parser
->SetPhysicalAddress(iPhysicalAddress
);
466 bool ProcessCommandOSD(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
468 if (command
== "osd")
470 bool bFirstWord(false);
471 string strAddr
, strMessage
, strWord
;
473 if (GetWord(arguments
, strAddr
) && HexStrToInt(strAddr
, iAddr
) && iAddr
< 0xF)
475 while (GetWord(arguments
, strWord
))
480 strMessage
.append(" ");
482 strMessage
.append(strWord
);
484 parser
->SetOSDString((cec_logical_address
) iAddr
, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME
, strMessage
.c_str());
492 bool ProcessCommandAS(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
496 parser
->SetActiveView();
504 bool ProcessCommandPING(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
506 if (command
== "ping")
508 parser
->PingAdapter();
515 bool ProcessCommandVOLUP(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
517 if (command
== "volup")
519 PrintToStdOut("volume up: %2X", parser
->VolumeUp());
526 bool ProcessCommandVOLDOWN(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
528 if (command
== "voldown")
530 PrintToStdOut("volume down: %2X", parser
->VolumeDown());
537 bool ProcessCommandMUTE(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
539 if (command
== "mute")
541 PrintToStdOut("mute: %2X", parser
->MuteAudio());
548 bool ProcessCommandMON(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
550 if (command
== "mon")
552 CStdString strEnable
;
553 if (GetWord(arguments
, strEnable
) && (strEnable
.Equals("0") || strEnable
.Equals("1")))
555 parser
->SwitchMonitoring(strEnable
.Equals("1"));
563 bool ProcessCommandBL(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
567 if (parser
->StartBootloader())
569 PrintToStdOut("entered bootloader mode. exiting cec-client");
579 bool ProcessCommandLANG(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
581 if (command
== "lang")
584 if (GetWord(arguments
, strDev
))
586 int iDev
= atoi(strDev
);
587 if (iDev
>= 0 && iDev
< 15)
590 cec_menu_language language
;
591 if (parser
->GetDeviceMenuLanguage((cec_logical_address
) iDev
, &language
))
592 strLog
.Format("menu language '%s'", language
.language
);
595 PrintToStdOut(strLog
);
604 bool ProcessCommandVEN(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
606 if (command
== "ven")
609 if (GetWord(arguments
, strDev
))
611 int iDev
= atoi(strDev
);
612 if (iDev
>= 0 && iDev
< 15)
614 uint64_t iVendor
= parser
->GetDeviceVendorId((cec_logical_address
) iDev
);
615 PrintToStdOut("vendor id: %06x", iVendor
);
624 bool ProcessCommandVER(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
626 if (command
== "ver")
629 if (GetWord(arguments
, strDev
))
631 int iDev
= atoi(strDev
);
632 if (iDev
>= 0 && iDev
< 15)
634 cec_version iVersion
= parser
->GetDeviceCecVersion((cec_logical_address
) iDev
);
635 PrintToStdOut("CEC version %s", parser
->ToString(iVersion
));
644 bool ProcessCommandPOW(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
646 if (command
== "pow")
649 if (GetWord(arguments
, strDev
))
651 int iDev
= atoi(strDev
);
652 if (iDev
>= 0 && iDev
< 15)
654 cec_power_status iPower
= parser
->GetDevicePowerStatus((cec_logical_address
) iDev
);
655 PrintToStdOut("power status: %s", parser
->ToString(iPower
));
664 bool ProcessCommandNAME(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
666 if (command
== "name")
669 if (GetWord(arguments
, strDev
))
671 int iDev
= atoi(strDev
);
672 if (iDev
>= 0 && iDev
< 15)
674 cec_osd_name name
= parser
->GetDeviceOSDName((cec_logical_address
)iDev
);
675 PrintToStdOut("OSD name of device %d is '%s'", iDev
, name
.name
);
684 bool ProcessCommandLAD(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
686 if (command
== "lad")
688 PrintToStdOut("listing active devices:");
689 cec_logical_addresses addresses
= parser
->GetActiveDevices();
690 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
693 PrintToStdOut("logical address %X", (int)iPtr
);
701 bool ProcessCommandAD(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
706 if (GetWord(arguments
, strDev
))
708 int iDev
= atoi(strDev
);
709 if (iDev
>= 0 && iDev
< 15)
710 PrintToStdOut("logical address %X is %s", iDev
, (parser
->IsActiveDevice((cec_logical_address
)iDev
) ? "active" : "not active"));
717 bool ProcessCommandAT(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
722 if (GetWord(arguments
, strType
))
724 cec_device_type type
= CEC_DEVICE_TYPE_TV
;
725 if (strType
.Equals("a"))
726 type
= CEC_DEVICE_TYPE_AUDIO_SYSTEM
;
727 else if (strType
.Equals("p"))
728 type
= CEC_DEVICE_TYPE_PLAYBACK_DEVICE
;
729 else if (strType
.Equals("r"))
730 type
= CEC_DEVICE_TYPE_RECORDING_DEVICE
;
731 else if (strType
.Equals("t"))
732 type
= CEC_DEVICE_TYPE_TUNER
;
734 PrintToStdOut("device %d is %s", type
, (parser
->IsActiveDeviceType(type
) ? "active" : "not active"));
742 bool ProcessCommandR(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
746 PrintToStdOut("closing the connection");
749 PrintToStdOut("opening a new connection");
750 parser
->Open(g_strPort
.c_str());
752 PrintToStdOut("setting active source");
753 parser
->SetActiveSource();
760 bool ProcessCommandH(ICECAdapter
* UNUSED(parser
), const string
&command
, string
& UNUSED(arguments
))
762 if (command
== "h" || command
== "help")
771 bool ProcessCommandLOG(ICECAdapter
* UNUSED(parser
), const string
&command
, string
&arguments
)
773 if (command
== "log")
776 if (GetWord(arguments
, strLevel
))
778 int iNewLevel
= atoi(strLevel
);
779 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
781 g_cecLogLevel
= iNewLevel
;
783 PrintToStdOut("log level changed to %s", strLevel
.c_str());
792 bool ProcessCommandSCAN(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
794 if (command
== "scan")
796 PrintToStdOut("CEC bus information");
797 PrintToStdOut("===================");
798 cec_logical_addresses addresses
= parser
->GetActiveDevices();
799 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
804 uint64_t iVendorId
= parser
->GetDeviceVendorId((cec_logical_address
)iPtr
);
805 bool bActive
= parser
->IsActiveSource((cec_logical_address
)iPtr
);
806 uint16_t iPhysicalAddress
= parser
->GetDevicePhysicalAddress((cec_logical_address
)iPtr
);
807 cec_version iCecVersion
= parser
->GetDeviceCecVersion((cec_logical_address
)iPtr
);
808 cec_power_status power
= parser
->GetDevicePowerStatus((cec_logical_address
)iPtr
);
809 cec_osd_name osdName
= parser
->GetDeviceOSDName((cec_logical_address
)iPtr
);
811 strAddr
.Format("%04x", iPhysicalAddress
);
812 cec_menu_language lang
;
813 lang
.device
= CECDEVICE_UNKNOWN
;
814 parser
->GetDeviceMenuLanguage((cec_logical_address
)iPtr
, &lang
);
816 strLog
.AppendFormat("device #%X: %s\n", (int)iPtr
, parser
->ToString((cec_logical_address
)iPtr
));
817 strLog
.AppendFormat("address: %s\n", strAddr
.c_str());
818 strLog
.AppendFormat("active source: %s\n", (bActive
? "yes" : "no"));
819 strLog
.AppendFormat("vendor: %s\n", parser
->ToString((cec_vendor_id
)iVendorId
));
820 strLog
.AppendFormat("osd string: %s\n", osdName
.name
);
821 strLog
.AppendFormat("CEC version: %s\n", parser
->ToString(iCecVersion
));
822 strLog
.AppendFormat("power status: %s\n", parser
->ToString(power
));
823 if ((uint8_t)lang
.device
== iPtr
)
824 strLog
.AppendFormat("language: %s\n", lang
.language
);
826 PrintToStdOut(strLog
);
835 bool ProcessConsoleCommand(ICECAdapter
*parser
, string
&input
)
840 if (GetWord(input
, command
))
842 if (command
== "q" || command
== "quit")
845 ProcessCommandTX(parser
, command
, input
) ||
846 ProcessCommandON(parser
, command
, input
) ||
847 ProcessCommandSTANDBY(parser
, command
, input
) ||
848 ProcessCommandPOLL(parser
, command
, input
) ||
849 ProcessCommandLA(parser
, command
, input
) ||
850 ProcessCommandP(parser
, command
, input
) ||
851 ProcessCommandPA(parser
, command
, input
) ||
852 ProcessCommandAS(parser
, command
, input
) ||
853 ProcessCommandOSD(parser
, command
, input
) ||
854 ProcessCommandPING(parser
, command
, input
) ||
855 ProcessCommandVOLUP(parser
, command
, input
) ||
856 ProcessCommandVOLDOWN(parser
, command
, input
) ||
857 ProcessCommandMUTE(parser
, command
, input
) ||
858 ProcessCommandMON(parser
, command
, input
) ||
859 ProcessCommandBL(parser
, command
, input
) ||
860 ProcessCommandLANG(parser
, command
, input
) ||
861 ProcessCommandVEN(parser
, command
, input
) ||
862 ProcessCommandVER(parser
, command
, input
) ||
863 ProcessCommandPOW(parser
, command
, input
) ||
864 ProcessCommandNAME(parser
, command
, input
) ||
865 ProcessCommandLAD(parser
, command
, input
) ||
866 ProcessCommandAD(parser
, command
, input
) ||
867 ProcessCommandAT(parser
, command
, input
) ||
868 ProcessCommandR(parser
, command
, input
) ||
869 ProcessCommandH(parser
, command
, input
) ||
870 ProcessCommandLOG(parser
, command
, input
) ||
871 ProcessCommandSCAN(parser
, command
, input
) ||
872 ProcessCommandSP(parser
, command
, input
) ||
873 ProcessCommandSPL(parser
, command
, input
) ||
874 ProcessCommandSELF(parser
, command
, input
);
880 bool ProcessCommandLineArguments(int argc
, char *argv
[])
884 while (iArgPtr
< argc
&& bReturn
)
886 if (argc
>= iArgPtr
+ 1)
888 if (!strcmp(argv
[iArgPtr
], "-f") ||
889 !strcmp(argv
[iArgPtr
], "--log-file") ||
890 !strcmp(argv
[iArgPtr
], "-sf") ||
891 !strcmp(argv
[iArgPtr
], "--short-log-file"))
893 if (argc
>= iArgPtr
+ 2)
895 g_logOutput
.open(argv
[iArgPtr
+ 1]);
896 g_bShortLog
= (!strcmp(argv
[iArgPtr
], "-sf") || !strcmp(argv
[iArgPtr
], "--short-log-file"));
901 cout
<< "== skipped log-file parameter: no file given ==" << endl
;
905 else if (!strcmp(argv
[iArgPtr
], "-d") ||
906 !strcmp(argv
[iArgPtr
], "--log-level"))
908 if (argc
>= iArgPtr
+ 2)
910 int iNewLevel
= atoi(argv
[iArgPtr
+ 1]);
911 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
913 g_cecLogLevel
= iNewLevel
;
914 if (!g_bSingleCommand
)
915 cout
<< "log level set to " << argv
[iArgPtr
+ 1] << endl
;
919 cout
<< "== skipped log-level parameter: invalid level '" << argv
[iArgPtr
+ 1] << "' ==" << endl
;
925 cout
<< "== skipped log-level parameter: no level given ==" << endl
;
929 else if (!strcmp(argv
[iArgPtr
], "-t") ||
930 !strcmp(argv
[iArgPtr
], "--type"))
932 if (argc
>= iArgPtr
+ 2)
934 if (!strcmp(argv
[iArgPtr
+ 1], "p"))
936 if (!g_bSingleCommand
)
937 cout
<< "== using device type 'playback device'" << endl
;
938 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
940 else if (!strcmp(argv
[iArgPtr
+ 1], "r"))
942 if (!g_bSingleCommand
)
943 cout
<< "== using device type 'recording device'" << endl
;
944 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
946 else if (!strcmp(argv
[iArgPtr
+ 1], "t"))
948 if (!g_bSingleCommand
)
949 cout
<< "== using device type 'tuner'" << endl
;
950 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_TUNER
);
952 else if (!strcmp(argv
[iArgPtr
+ 1], "a"))
954 if (!g_bSingleCommand
)
955 cout
<< "== using device type 'audio system'" << endl
;
956 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_AUDIO_SYSTEM
);
960 cout
<< "== skipped invalid device type '" << argv
[iArgPtr
+ 1] << "'" << endl
;
966 else if (!strcmp(argv
[iArgPtr
], "--list-devices") ||
967 !strcmp(argv
[iArgPtr
], "-l"))
969 ICECAdapter
*parser
= LibCecInitialise(&g_config
);
973 UnloadLibCec(parser
);
978 else if (!strcmp(argv
[iArgPtr
], "--single-command") ||
979 !strcmp(argv
[iArgPtr
], "-s"))
981 g_bSingleCommand
= true;
984 else if (!strcmp(argv
[iArgPtr
], "--help") ||
985 !strcmp(argv
[iArgPtr
], "-h"))
987 ShowHelpCommandLine(argv
[0]);
990 else if (!strcmp(argv
[iArgPtr
], "-b") ||
991 !strcmp(argv
[iArgPtr
], "--base"))
993 if (argc
>= iArgPtr
+ 2)
995 g_config
.baseDevice
= (cec_logical_address
)atoi(argv
[iArgPtr
+ 1]);
996 cout
<< "using base device '" << (int)g_config
.baseDevice
<< "'" << endl
;
1001 else if (!strcmp(argv
[iArgPtr
], "-p") ||
1002 !strcmp(argv
[iArgPtr
], "--port"))
1004 if (argc
>= iArgPtr
+ 2)
1006 g_config
.iHDMIPort
= (int8_t)atoi(argv
[iArgPtr
+ 1]);
1007 cout
<< "using HDMI port '" << (int)g_config
.iHDMIPort
<< "'" << endl
;
1014 g_strPort
= argv
[iArgPtr
++];
1022 int main (int argc
, char *argv
[])
1025 snprintf(g_config
.strDeviceName
, 13, "CECTester");
1026 g_config
.callbackParam
= NULL
;
1027 g_config
.clientVersion
= CEC_CLIENT_VERSION_1_5_0
;
1028 g_callbacks
.CBCecLogMessage
= &CecLogMessage
;
1029 g_callbacks
.CBCecKeyPress
= &CecKeyPress
;
1030 g_callbacks
.CBCecCommand
= &CecCommand
;
1031 g_config
.callbacks
= &g_callbacks
;
1033 if (!ProcessCommandLineArguments(argc
, argv
))
1036 if (g_config
.deviceTypes
.IsEmpty())
1038 if (!g_bSingleCommand
)
1039 cout
<< "No device type given. Using 'recording device'" << endl
;
1040 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
1043 ICECAdapter
*parser
= LibCecInitialise(&g_config
);
1047 cout
<< "Cannot load libcec.dll" << endl
;
1049 cout
<< "Cannot load libcec.so" << endl
;
1053 UnloadLibCec(parser
);
1058 if (!g_bSingleCommand
)
1061 strLog
.Format("CEC Parser created - libCEC version %s", parser
->ToString((cec_server_version
)g_config
.serverVersion
));
1062 cout
<< strLog
.c_str() << endl
;
1064 //make stdin non-blocking
1066 int flags
= fcntl(0, F_GETFL
, 0);
1067 flags
|= O_NONBLOCK
;
1068 fcntl(0, F_SETFL
, flags
);
1072 if (g_strPort
.IsEmpty())
1074 if (!g_bSingleCommand
)
1075 cout
<< "no serial port given. trying autodetect: ";
1076 cec_adapter devices
[10];
1077 uint8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
1078 if (iDevicesFound
<= 0)
1080 if (g_bSingleCommand
)
1081 cout
<< "autodetect ";
1082 cout
<< "FAILED" << endl
;
1083 UnloadLibCec(parser
);
1088 if (!g_bSingleCommand
)
1090 cout
<< endl
<< " path: " << devices
[0].path
<< endl
<<
1091 " com port: " << devices
[0].comm
<< endl
<< endl
;
1093 g_strPort
= devices
[0].comm
;
1097 PrintToStdOut("opening a connection to the CEC adapter...");
1099 if (!parser
->Open(g_strPort
.c_str()))
1101 PrintToStdOut("unable to open the device on port %s", g_strPort
.c_str());
1102 UnloadLibCec(parser
);
1106 if (!g_bSingleCommand
)
1107 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
)
1128 UnloadLibCec(parser
);
1130 if (g_logOutput
.is_open())
1131 g_logOutput
.close();