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 g_callbacks
.CBCecAlert
= NULL
;
185 adapter
->EnableCallbacks(NULL
, &g_callbacks
);
188 void ListDevices(ICECAdapter
*parser
)
190 cec_adapter
*devices
= new cec_adapter
[10];
191 int8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
192 if (iDevicesFound
<= 0)
194 PrintToStdOut("Found devices: NONE");
198 CStdString strDeviceInfo
;
199 strDeviceInfo
.Format("Found devices: %d\n\n", iDevicesFound
);
201 for (int8_t iDevicePtr
= 0; iDevicePtr
< iDevicesFound
; iDevicePtr
++)
203 strDeviceInfo
.AppendFormat("device: %d\ncom port: %s\n", iDevicePtr
+ 1, devices
[iDevicePtr
].comm
);
204 libcec_configuration config
;
207 if (!parser
->GetDeviceInformation(devices
[iDevicePtr
].comm
, &config
))
208 PrintToStdOut("WARNING: unable to open the device on port %s", devices
[iDevicePtr
].comm
);
210 strDeviceInfo
.AppendFormat("firmware version: %d\n", config
.iFirmwareVersion
);
211 strDeviceInfo
.append("\n");
213 PrintToStdOut(strDeviceInfo
.c_str());
217 void ShowHelpCommandLine(const char* strExec
)
219 CLockObject
lock(g_outputMutex
);
221 strExec
<< " {-h|--help|-l|--list-devices|[COM PORT]}" << endl
<<
223 "parameters:" << endl
<<
224 " -h --help Shows this help text" << endl
<<
225 " -l --list-devices List all devices on this system" << endl
<<
226 " -t --type {p|r|t|a} The device type to use. More than one is possible." << endl
<<
227 " -p --port {int} The HDMI port to use as active source." << endl
<<
228 " -b --base {int} The logical address of the device to with this " << endl
<<
229 " adapter is connected." << endl
<<
230 " -f --log-file {file} Writes all libCEC log message to a file" << endl
<<
231 " -r --rom Read persisted settings from the EEPROM" << endl
<<
232 " -sf --short-log-file {file} Writes all libCEC log message without timestamps" << endl
<<
233 " and log levels to a file." << endl
<<
234 " -d --log-level {level} Sets the log level. See cectypes.h for values." << endl
<<
235 " -s --single-command Execute a single command and exit. Does not power" << endl
<<
236 " on devices on startup and power them off on exit." << endl
<<
237 " -o --osd-name {osd name} Use a custom osd name." << endl
<<
238 " [COM PORT] The com port to connect to. If no COM" << endl
<<
239 " port is given, the client tries to connect to the" << endl
<<
240 " first device that is detected." << endl
<<
242 "Type 'h' or 'help' and press enter after starting the client to display all " << endl
<<
243 "available commands" << endl
;
246 void ShowHelpConsole(void)
248 CLockObject
lock(g_outputMutex
);
250 "================================================================================" << endl
<<
251 "Available commands:" << endl
<<
253 "[tx] {bytes} transfer bytes over the CEC line." << endl
<<
254 "[txn] {bytes} transfer bytes but don't wait for transmission ACK." << endl
<<
255 "[on] {address} power on the device with the given logical address." << endl
<<
256 "[standby] {address} put the device with the given address in standby mode." << endl
<<
257 "[la] {logical address} change the logical address of the CEC adapter." << endl
<<
258 "[p] {device} {port} change the HDMI port number of the CEC adapter." << endl
<<
259 "[pa] {physical address} change the physical address of the CEC adapter." << endl
<<
260 "[as] make the CEC adapter the active source." << endl
<<
261 "[osd] {addr} {string} set OSD message on the specified device." << endl
<<
262 "[ver] {addr} get the CEC version of the specified device." << endl
<<
263 "[ven] {addr} get the vendor ID of the specified device." << endl
<<
264 "[lang] {addr} get the menu language of the specified device." << endl
<<
265 "[pow] {addr} get the power status of the specified device." << endl
<<
266 "[name] {addr} get the OSD name of the specified device." << endl
<<
267 "[poll] {addr} poll the specified device." << endl
<<
268 "[lad] lists active devices on the bus" << endl
<<
269 "[ad] {addr} checks whether the specified device is active." << endl
<<
270 "[at] {type} checks whether the specified device type is active." << endl
<<
271 "[sp] {addr} makes the specified physical address active." << endl
<<
272 "[spl] {addr} makes the specified logical address active." << endl
<<
273 "[volup] send a volume up command to the amp if present" << endl
<<
274 "[voldown] send a volume down command to the amp if present" << endl
<<
275 "[mute] send a mute/unmute command to the amp if present" << endl
<<
276 "[self] show the list of addresses controlled by libCEC" << endl
<<
277 "[scan] scan the CEC bus and display device info" << endl
<<
278 "[mon] {1|0} enable or disable CEC bus monitoring." << endl
<<
279 "[log] {1 - 31} change the log level. see cectypes.h for values." << endl
<<
280 "[ping] send a ping command to the CEC adapter." << endl
<<
281 "[bl] to let the adapter enter the bootloader, to upgrade" << endl
<<
282 " the flash rom." << endl
<<
283 "[r] reconnect to the CEC adapter." << endl
<<
284 "[h] or [help] show this help." << endl
<<
285 "[q] or [quit] to quit the CEC test client and switch off all" << endl
<<
286 " connected CEC devices." << endl
<<
287 "================================================================================" << endl
;
290 bool ProcessCommandSELF(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
292 if (command
== "self")
294 cec_logical_addresses addr
= parser
->GetLogicalAddresses();
295 CStdString strOut
= "Addresses controlled by libCEC: ";
297 for (uint8_t iPtr
= 0; iPtr
<= 15; iPtr
++)
301 strOut
.AppendFormat((bFirst
? "%d%s" : ", %d%s"), iPtr
, parser
->IsActiveSource((cec_logical_address
)iPtr
) ? "*" : "");
305 PrintToStdOut(strOut
.c_str());
312 bool ProcessCommandSP(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
318 if (GetWord(arguments
, strAddress
))
320 sscanf(strAddress
.c_str(), "%x", &iAddress
);
321 if (iAddress
>= 0 && iAddress
<= 0xFFFF)
322 parser
->SetStreamPath((uint16_t)iAddress
);
330 bool ProcessCommandSPL(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
332 if (command
== "spl")
335 cec_logical_address iAddress
;
336 if (GetWord(arguments
, strAddress
))
338 iAddress
= (cec_logical_address
)atoi(strAddress
.c_str());
339 if (iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
)
340 parser
->SetStreamPath(iAddress
);
348 bool ProcessCommandTX(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
350 if (command
== "tx" || command
== "txn")
357 while (GetWord(arguments
, strvalue
) && HexStrToInt(strvalue
, ivalue
))
358 bytes
.PushBack(ivalue
);
360 if (command
== "txn")
361 bytes
.transmit_timeout
= 0;
363 parser
->Transmit(bytes
);
371 bool ProcessCommandON(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
377 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
379 parser
->PowerOnDevices((cec_logical_address
) iValue
);
384 PrintToStdOut("invalid destination");
391 bool ProcessCommandSTANDBY(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
393 if (command
== "standby")
397 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
399 parser
->StandbyDevices((cec_logical_address
) iValue
);
404 PrintToStdOut("invalid destination");
411 bool ProcessCommandPOLL(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
413 if (command
== "poll")
417 if (GetWord(arguments
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
419 if (parser
->PollDevice((cec_logical_address
) iValue
))
420 PrintToStdOut("POLL message sent");
422 PrintToStdOut("POLL message not sent");
427 PrintToStdOut("invalid destination");
434 bool ProcessCommandLA(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
439 if (GetWord(arguments
, strvalue
))
441 parser
->SetLogicalAddress((cec_logical_address
) atoi(strvalue
.c_str()));
449 bool ProcessCommandP(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
453 string strPort
, strDevice
;
454 if (GetWord(arguments
, strDevice
) && GetWord(arguments
, strPort
))
456 parser
->SetHDMIPort((cec_logical_address
)atoi(strDevice
.c_str()), (uint8_t)atoi(strPort
.c_str()));
464 bool ProcessCommandPA(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
470 if (GetWord(arguments
, strB1
) && HexStrToInt(strB1
, iB1
) &&
471 GetWord(arguments
, strB2
) && HexStrToInt(strB2
, iB2
))
473 uint16_t iPhysicalAddress
= ((uint16_t)iB1
<< 8) + iB2
;
474 parser
->SetPhysicalAddress(iPhysicalAddress
);
482 bool ProcessCommandOSD(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
484 if (command
== "osd")
486 bool bFirstWord(false);
487 string strAddr
, strMessage
, strWord
;
489 if (GetWord(arguments
, strAddr
) && HexStrToInt(strAddr
, iAddr
) && iAddr
< 0xF)
491 while (GetWord(arguments
, strWord
))
496 strMessage
.append(" ");
498 strMessage
.append(strWord
);
500 parser
->SetOSDString((cec_logical_address
) iAddr
, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME
, strMessage
.c_str());
508 bool ProcessCommandAS(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
512 parser
->SetActiveView();
520 bool ProcessCommandPING(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
522 if (command
== "ping")
524 parser
->PingAdapter();
531 bool ProcessCommandVOLUP(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
533 if (command
== "volup")
535 PrintToStdOut("volume up: %2X", parser
->VolumeUp());
542 bool ProcessCommandVOLDOWN(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
544 if (command
== "voldown")
546 PrintToStdOut("volume down: %2X", parser
->VolumeDown());
553 bool ProcessCommandMUTE(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
555 if (command
== "mute")
557 PrintToStdOut("mute: %2X", parser
->MuteAudio());
564 bool ProcessCommandMON(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
566 if (command
== "mon")
568 CStdString strEnable
;
569 if (GetWord(arguments
, strEnable
) && (strEnable
.Equals("0") || strEnable
.Equals("1")))
571 parser
->SwitchMonitoring(strEnable
.Equals("1"));
579 bool ProcessCommandBL(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
583 if (parser
->StartBootloader())
585 PrintToStdOut("entered bootloader mode. exiting cec-client");
595 bool ProcessCommandLANG(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
597 if (command
== "lang")
600 if (GetWord(arguments
, strDev
))
602 int iDev
= atoi(strDev
);
603 if (iDev
>= 0 && iDev
< 15)
606 cec_menu_language language
;
607 if (parser
->GetDeviceMenuLanguage((cec_logical_address
) iDev
, &language
))
608 strLog
.Format("menu language '%s'", language
.language
);
611 PrintToStdOut(strLog
);
620 bool ProcessCommandVEN(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
622 if (command
== "ven")
625 if (GetWord(arguments
, strDev
))
627 int iDev
= atoi(strDev
);
628 if (iDev
>= 0 && iDev
< 15)
630 uint64_t iVendor
= parser
->GetDeviceVendorId((cec_logical_address
) iDev
);
631 PrintToStdOut("vendor id: %06x", iVendor
);
640 bool ProcessCommandVER(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
642 if (command
== "ver")
645 if (GetWord(arguments
, strDev
))
647 int iDev
= atoi(strDev
);
648 if (iDev
>= 0 && iDev
< 15)
650 cec_version iVersion
= parser
->GetDeviceCecVersion((cec_logical_address
) iDev
);
651 PrintToStdOut("CEC version %s", parser
->ToString(iVersion
));
660 bool ProcessCommandPOW(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
662 if (command
== "pow")
665 if (GetWord(arguments
, strDev
))
667 int iDev
= atoi(strDev
);
668 if (iDev
>= 0 && iDev
< 15)
670 cec_power_status iPower
= parser
->GetDevicePowerStatus((cec_logical_address
) iDev
);
671 PrintToStdOut("power status: %s", parser
->ToString(iPower
));
680 bool ProcessCommandNAME(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
682 if (command
== "name")
685 if (GetWord(arguments
, strDev
))
687 int iDev
= atoi(strDev
);
688 if (iDev
>= 0 && iDev
< 15)
690 cec_osd_name name
= parser
->GetDeviceOSDName((cec_logical_address
)iDev
);
691 PrintToStdOut("OSD name of device %d is '%s'", iDev
, name
.name
);
700 bool ProcessCommandLAD(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
702 if (command
== "lad")
704 PrintToStdOut("listing active devices:");
705 cec_logical_addresses addresses
= parser
->GetActiveDevices();
706 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
709 PrintToStdOut("logical address %X", (int)iPtr
);
717 bool ProcessCommandAD(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
722 if (GetWord(arguments
, strDev
))
724 int iDev
= atoi(strDev
);
725 if (iDev
>= 0 && iDev
< 15)
726 PrintToStdOut("logical address %X is %s", iDev
, (parser
->IsActiveDevice((cec_logical_address
)iDev
) ? "active" : "not active"));
733 bool ProcessCommandAT(ICECAdapter
*parser
, const string
&command
, string
&arguments
)
738 if (GetWord(arguments
, strType
))
740 cec_device_type type
= CEC_DEVICE_TYPE_TV
;
741 if (strType
.Equals("a"))
742 type
= CEC_DEVICE_TYPE_AUDIO_SYSTEM
;
743 else if (strType
.Equals("p"))
744 type
= CEC_DEVICE_TYPE_PLAYBACK_DEVICE
;
745 else if (strType
.Equals("r"))
746 type
= CEC_DEVICE_TYPE_RECORDING_DEVICE
;
747 else if (strType
.Equals("t"))
748 type
= CEC_DEVICE_TYPE_TUNER
;
750 PrintToStdOut("device %d is %s", type
, (parser
->IsActiveDeviceType(type
) ? "active" : "not active"));
758 bool ProcessCommandR(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
762 PrintToStdOut("closing the connection");
765 PrintToStdOut("opening a new connection");
766 parser
->Open(g_strPort
.c_str());
768 PrintToStdOut("setting active source");
769 parser
->SetActiveSource();
776 bool ProcessCommandH(ICECAdapter
* UNUSED(parser
), const string
&command
, string
& UNUSED(arguments
))
778 if (command
== "h" || command
== "help")
787 bool ProcessCommandLOG(ICECAdapter
* UNUSED(parser
), const string
&command
, string
&arguments
)
789 if (command
== "log")
792 if (GetWord(arguments
, strLevel
))
794 int iNewLevel
= atoi(strLevel
);
795 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
797 g_cecLogLevel
= iNewLevel
;
799 PrintToStdOut("log level changed to %s", strLevel
.c_str());
808 bool ProcessCommandSCAN(ICECAdapter
*parser
, const string
&command
, string
& UNUSED(arguments
))
810 if (command
== "scan")
812 PrintToStdOut("CEC bus information");
813 PrintToStdOut("===================");
814 cec_logical_addresses addresses
= parser
->GetActiveDevices();
815 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
820 uint64_t iVendorId
= parser
->GetDeviceVendorId((cec_logical_address
)iPtr
);
821 bool bActive
= parser
->IsActiveSource((cec_logical_address
)iPtr
);
822 uint16_t iPhysicalAddress
= parser
->GetDevicePhysicalAddress((cec_logical_address
)iPtr
);
823 cec_version iCecVersion
= parser
->GetDeviceCecVersion((cec_logical_address
)iPtr
);
824 cec_power_status power
= parser
->GetDevicePowerStatus((cec_logical_address
)iPtr
);
825 cec_osd_name osdName
= parser
->GetDeviceOSDName((cec_logical_address
)iPtr
);
827 strAddr
.Format("%04x", iPhysicalAddress
);
828 cec_menu_language lang
;
829 lang
.device
= CECDEVICE_UNKNOWN
;
830 parser
->GetDeviceMenuLanguage((cec_logical_address
)iPtr
, &lang
);
832 strLog
.AppendFormat("device #%X: %s\n", (int)iPtr
, parser
->ToString((cec_logical_address
)iPtr
));
833 strLog
.AppendFormat("address: %s\n", strAddr
.c_str());
834 strLog
.AppendFormat("active source: %s\n", (bActive
? "yes" : "no"));
835 strLog
.AppendFormat("vendor: %s\n", parser
->ToString((cec_vendor_id
)iVendorId
));
836 strLog
.AppendFormat("osd string: %s\n", osdName
.name
);
837 strLog
.AppendFormat("CEC version: %s\n", parser
->ToString(iCecVersion
));
838 strLog
.AppendFormat("power status: %s\n", parser
->ToString(power
));
839 if ((uint8_t)lang
.device
== iPtr
)
840 strLog
.AppendFormat("language: %s\n", lang
.language
);
842 PrintToStdOut(strLog
);
851 bool ProcessConsoleCommand(ICECAdapter
*parser
, string
&input
)
856 if (GetWord(input
, command
))
858 if (command
== "q" || command
== "quit")
861 ProcessCommandTX(parser
, command
, input
) ||
862 ProcessCommandON(parser
, command
, input
) ||
863 ProcessCommandSTANDBY(parser
, command
, input
) ||
864 ProcessCommandPOLL(parser
, command
, input
) ||
865 ProcessCommandLA(parser
, command
, input
) ||
866 ProcessCommandP(parser
, command
, input
) ||
867 ProcessCommandPA(parser
, command
, input
) ||
868 ProcessCommandAS(parser
, command
, input
) ||
869 ProcessCommandOSD(parser
, command
, input
) ||
870 ProcessCommandPING(parser
, command
, input
) ||
871 ProcessCommandVOLUP(parser
, command
, input
) ||
872 ProcessCommandVOLDOWN(parser
, command
, input
) ||
873 ProcessCommandMUTE(parser
, command
, input
) ||
874 ProcessCommandMON(parser
, command
, input
) ||
875 ProcessCommandBL(parser
, command
, input
) ||
876 ProcessCommandLANG(parser
, command
, input
) ||
877 ProcessCommandVEN(parser
, command
, input
) ||
878 ProcessCommandVER(parser
, command
, input
) ||
879 ProcessCommandPOW(parser
, command
, input
) ||
880 ProcessCommandNAME(parser
, command
, input
) ||
881 ProcessCommandLAD(parser
, command
, input
) ||
882 ProcessCommandAD(parser
, command
, input
) ||
883 ProcessCommandAT(parser
, command
, input
) ||
884 ProcessCommandR(parser
, command
, input
) ||
885 ProcessCommandH(parser
, command
, input
) ||
886 ProcessCommandLOG(parser
, command
, input
) ||
887 ProcessCommandSCAN(parser
, command
, input
) ||
888 ProcessCommandSP(parser
, command
, input
) ||
889 ProcessCommandSPL(parser
, command
, input
) ||
890 ProcessCommandSELF(parser
, command
, input
);
896 bool ProcessCommandLineArguments(int argc
, char *argv
[])
900 while (iArgPtr
< argc
&& bReturn
)
902 if (argc
>= iArgPtr
+ 1)
904 if (!strcmp(argv
[iArgPtr
], "-f") ||
905 !strcmp(argv
[iArgPtr
], "--log-file") ||
906 !strcmp(argv
[iArgPtr
], "-sf") ||
907 !strcmp(argv
[iArgPtr
], "--short-log-file"))
909 if (argc
>= iArgPtr
+ 2)
911 g_logOutput
.open(argv
[iArgPtr
+ 1]);
912 g_bShortLog
= (!strcmp(argv
[iArgPtr
], "-sf") || !strcmp(argv
[iArgPtr
], "--short-log-file"));
917 cout
<< "== skipped log-file parameter: no file given ==" << endl
;
921 else if (!strcmp(argv
[iArgPtr
], "-d") ||
922 !strcmp(argv
[iArgPtr
], "--log-level"))
924 if (argc
>= iArgPtr
+ 2)
926 int iNewLevel
= atoi(argv
[iArgPtr
+ 1]);
927 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
929 g_cecLogLevel
= iNewLevel
;
930 if (!g_bSingleCommand
)
931 cout
<< "log level set to " << argv
[iArgPtr
+ 1] << endl
;
935 cout
<< "== skipped log-level parameter: invalid level '" << argv
[iArgPtr
+ 1] << "' ==" << endl
;
941 cout
<< "== skipped log-level parameter: no level given ==" << endl
;
945 else if (!strcmp(argv
[iArgPtr
], "-t") ||
946 !strcmp(argv
[iArgPtr
], "--type"))
948 if (argc
>= iArgPtr
+ 2)
950 if (!strcmp(argv
[iArgPtr
+ 1], "p"))
952 if (!g_bSingleCommand
)
953 cout
<< "== using device type 'playback device'" << endl
;
954 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
956 else if (!strcmp(argv
[iArgPtr
+ 1], "r"))
958 if (!g_bSingleCommand
)
959 cout
<< "== using device type 'recording device'" << endl
;
960 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
962 else if (!strcmp(argv
[iArgPtr
+ 1], "t"))
964 if (!g_bSingleCommand
)
965 cout
<< "== using device type 'tuner'" << endl
;
966 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_TUNER
);
968 else if (!strcmp(argv
[iArgPtr
+ 1], "a"))
970 if (!g_bSingleCommand
)
971 cout
<< "== using device type 'audio system'" << endl
;
972 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_AUDIO_SYSTEM
);
976 cout
<< "== skipped invalid device type '" << argv
[iArgPtr
+ 1] << "'" << endl
;
982 else if (!strcmp(argv
[iArgPtr
], "--list-devices") ||
983 !strcmp(argv
[iArgPtr
], "-l"))
985 ICECAdapter
*parser
= LibCecInitialise(&g_config
);
989 UnloadLibCec(parser
);
994 else if (!strcmp(argv
[iArgPtr
], "--bootloader"))
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_config
.baseDevice
= (cec_logical_address
)atoi(argv
[iArgPtr
+ 1]);
1017 cout
<< "using base device '" << (int)g_config
.baseDevice
<< "'" << endl
;
1022 else if (!strcmp(argv
[iArgPtr
], "-p") ||
1023 !strcmp(argv
[iArgPtr
], "--port"))
1025 if (argc
>= iArgPtr
+ 2)
1027 uint8_t hdmiport
= (int8_t)atoi(argv
[iArgPtr
+ 1]);
1032 g_config
.iHDMIPort
= hdmiport
;
1033 cout
<< "using HDMI port '" << (int)g_config
.iHDMIPort
<< "'" << endl
;
1038 else if (!strcmp(argv
[iArgPtr
], "-r") ||
1039 !strcmp(argv
[iArgPtr
], "--rom"))
1041 cout
<< "using settings from EEPROM" << endl
;
1042 g_config
.bGetSettingsFromROM
= 1;
1045 else if (!strcmp(argv
[iArgPtr
], "-o") ||
1046 !strcmp(argv
[iArgPtr
], "--osd-name"))
1048 if (argc
>= iArgPtr
+ 2)
1050 snprintf(g_config
.strDeviceName
, 13, "%s", argv
[iArgPtr
+ 1]);
1051 cout
<< "using osd name " << g_config
.strDeviceName
<< endl
;
1058 g_strPort
= argv
[iArgPtr
++];
1066 int main (int argc
, char *argv
[])
1069 snprintf(g_config
.strDeviceName
, 13, "CECTester");
1070 g_config
.callbackParam
= NULL
;
1071 g_config
.clientVersion
= CEC_CLIENT_VERSION_1_5_0
;
1072 g_callbacks
.CBCecLogMessage
= &CecLogMessage
;
1073 g_callbacks
.CBCecKeyPress
= &CecKeyPress
;
1074 g_callbacks
.CBCecCommand
= &CecCommand
;
1075 g_config
.callbacks
= &g_callbacks
;
1077 if (!ProcessCommandLineArguments(argc
, argv
))
1080 if (g_config
.deviceTypes
.IsEmpty())
1082 if (!g_bSingleCommand
)
1083 cout
<< "No device type given. Using 'recording device'" << endl
;
1084 g_config
.deviceTypes
.add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
1087 ICECAdapter
*parser
= LibCecInitialise(&g_config
);
1091 cout
<< "Cannot load libcec.dll" << endl
;
1093 cout
<< "Cannot load libcec.so" << endl
;
1097 UnloadLibCec(parser
);
1102 if (!g_bSingleCommand
)
1105 strLog
.Format("CEC Parser created - libCEC version %s", parser
->ToString((cec_server_version
)g_config
.serverVersion
));
1106 cout
<< strLog
.c_str() << endl
;
1108 //make stdin non-blocking
1110 int flags
= fcntl(0, F_GETFL
, 0);
1111 flags
|= O_NONBLOCK
;
1112 fcntl(0, F_SETFL
, flags
);
1116 if (g_strPort
.IsEmpty())
1118 if (!g_bSingleCommand
)
1119 cout
<< "no serial port given. trying autodetect: ";
1120 cec_adapter devices
[10];
1121 uint8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
1122 if (iDevicesFound
<= 0)
1124 if (g_bSingleCommand
)
1125 cout
<< "autodetect ";
1126 cout
<< "FAILED" << endl
;
1127 UnloadLibCec(parser
);
1132 if (!g_bSingleCommand
)
1134 cout
<< endl
<< " path: " << devices
[0].path
<< endl
<<
1135 " com port: " << devices
[0].comm
<< endl
<< endl
;
1137 g_strPort
= devices
[0].comm
;
1141 PrintToStdOut("opening a connection to the CEC adapter...");
1143 if (!parser
->Open(g_strPort
.c_str()))
1145 PrintToStdOut("unable to open the device on port %s", g_strPort
.c_str());
1146 UnloadLibCec(parser
);
1150 if (!g_bSingleCommand
)
1151 PrintToStdOut("waiting for input");
1153 while (!g_bExit
&& !g_bHardExit
)
1156 getline(cin
, input
);
1159 if (ProcessConsoleCommand(parser
, input
) && !g_bSingleCommand
&& !g_bExit
&& !g_bHardExit
)
1162 PrintToStdOut("waiting for input");
1167 if (!g_bExit
&& !g_bHardExit
)
1172 UnloadLibCec(parser
);
1174 if (g_logOutput
.is_open())
1175 g_logOutput
.close();