2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011 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/threads.h"
42 #include "../lib/util/StdString.h"
43 #include "../lib/implementations/CECCommandHandler.h"
48 #define CEC_TEST_CLIENT_VERSION 1
50 #include <cecloader.h>
52 int g_cecLogLevel
= CEC_LOG_ALL
;
54 bool g_bShortLog
= false;
57 inline bool HexStrToInt(const std::string
& data
, uint8_t& value
)
60 if (sscanf(data
.c_str(), "%x", &iTmp
) == 1)
67 value
= (uint8_t) iTmp
;
75 //get the first word (separated by whitespace) from string data and place that in word
76 //then remove that word from string data
77 bool GetWord(string
& data
, string
& word
)
79 stringstream
datastream(data
);
83 if (datastream
.fail())
89 size_t pos
= data
.find(word
) + word
.length();
91 if (pos
>= data
.length())
97 data
= data
.substr(pos
);
100 datastream
.str(data
);
103 if (datastream
.fail())
109 void FlushLog(ICECAdapter
*cecParser
)
111 cec_log_message message
;
112 while (cecParser
&& cecParser
->GetNextLogMessage(&message
))
114 if ((message
.level
& g_cecLogLevel
) == message
.level
)
117 switch (message
.level
)
120 strLevel
= "ERROR: ";
122 case CEC_LOG_WARNING
:
123 strLevel
= "WARNING: ";
126 strLevel
= "NOTICE: ";
128 case CEC_LOG_TRAFFIC
:
129 strLevel
= "TRAFFIC: ";
132 strLevel
= "DEBUG: ";
138 CStdString strFullLog
;
139 strFullLog
.Format("%s[%16lld]\t%s", strLevel
.c_str(), message
.time
, message
.message
);
140 cout
<< strFullLog
.c_str() << endl
;
142 if (g_logOutput
.is_open())
145 g_logOutput
<< message
.message
<< endl
;
147 g_logOutput
<< strFullLog
.c_str() << endl
;
153 void ListDevices(ICECAdapter
*parser
)
155 cec_adapter
*devices
= new cec_adapter
[10];
156 uint8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
157 if (iDevicesFound
<= 0)
159 cout
<< "Found devices: NONE" << endl
;
164 strLog
.Format("Found devices: %d", iDevicesFound
);
165 cout
<< strLog
.c_str() << endl
;
166 for (unsigned int iDevicePtr
= 0; iDevicePtr
< iDevicesFound
; iDevicePtr
++)
168 CStdString strDevice
;
169 strDevice
.Format("device: %d\npath: %s\ncom port: %s", iDevicePtr
+ 1, devices
[iDevicePtr
].path
, devices
[iDevicePtr
].comm
);
170 cout
<< endl
<< strDevice
.c_str() << endl
;
175 void ShowHelpCommandLine(const char* strExec
)
178 strExec
<< " {-h|--help|-l|--list-devices|[COM PORT]}" << endl
<<
180 "parameters:" << endl
<<
181 " -h --help Shows this help text" << endl
<<
182 " -l --list-devices List all devices on this system" << endl
<<
183 " -t --type {p|r|t|a} The device type to use. More than one is possible." << endl
<<
184 " -p --port {int} The HDMI port to use as active source." << endl
<<
185 " -f --log-file {file} Writes all libCEC log message to a file" << endl
<<
186 " -sf --short-log-file {file} Writes all libCEC log message without timestamps" << endl
<<
187 " and log levels to a file." << endl
<<
188 " -d --log-level {level} Sets the log level. See cectypes.h for values." << endl
<<
189 " -s --single-command Execute a single command and exit. Does not power" << endl
<<
190 " on devices on startup and power them off on exit." << endl
<<
191 " [COM PORT] The com port to connect to. If no COM" << endl
<<
192 " port is given, the client tries to connect to the" << endl
<<
193 " first device that is detected." << endl
<<
195 "Type 'h' or 'help' and press enter after starting the client to display all " << endl
<<
196 "available commands" << endl
;
199 ICECAdapter
*CreateParser(cec_device_type_list typeList
)
201 ICECAdapter
*parser
= LibCecInit("CECTester", typeList
);
202 if (!parser
|| parser
->GetMinLibVersion() > CEC_TEST_CLIENT_VERSION
)
205 cout
<< "Cannot load libcec.dll" << endl
;
207 cout
<< "Cannot load libcec.so" << endl
;
213 strLog
.Format("CEC Parser created - libcec version %d.%d", parser
->GetLibVersionMajor(), parser
->GetLibVersionMinor());
214 cout
<< strLog
.c_str() << endl
;
219 void ShowHelpConsole(void)
222 "================================================================================" << endl
<<
223 "Available commands:" << endl
<<
225 "[tx] {bytes} transfer bytes over the CEC line." << endl
<<
226 "[txn] {bytes} transfer bytes but don't wait for transmission ACK." << endl
<<
227 "[on] {address} power on the device with the given logical address." << endl
<<
228 "[standby] {address} put the device with the given address in standby mode." << endl
<<
229 "[la] {logical address} change the logical address of the CEC adapter." << endl
<<
230 "[p] {device} {port} change the HDMI port number of the CEC adapter." << endl
<<
231 "[pa] {physical address} change the physical address of the CEC adapter." << endl
<<
232 "[osd] {addr} {string} set OSD message on the specified device." << endl
<<
233 "[ver] {addr} get the CEC version of the specified device." << endl
<<
234 "[ven] {addr} get the vendor ID of the specified device." << endl
<<
235 "[lang] {addr} get the menu language of the specified device." << endl
<<
236 "[pow] {addr} get the power status of the specified device." << endl
<<
237 "[name] {addr} get the OSD name of the specified device." << endl
<<
238 "[poll] {addr} poll the specified device." << endl
<<
239 "[lad] lists active devices on the bus" << endl
<<
240 "[ad] {addr} checks whether the specified device is active." << endl
<<
241 "[at] {type} checks whether the specified device type is active." << endl
<<
242 "[volup] send a volume up command to the amp if present" << endl
<<
243 "[voldown] send a volume down command to the amp if present" << endl
<<
244 "[mute] send a mute/unmute command to the amp if present" << endl
<<
245 "[scan] scan the CEC bus and display device info" << endl
<<
246 "[mon] {1|0} enable or disable CEC bus monitoring." << endl
<<
247 "[log] {1 - 31} change the log level. see cectypes.h for values." << endl
<<
248 "[ping] send a ping command to the CEC adapter." << endl
<<
249 "[bl] to let the adapter enter the bootloader, to upgrade" << endl
<<
250 " the flash rom." << endl
<<
251 "[r] reconnect to the CEC adapter." << endl
<<
252 "[h] or [help] show this help." << endl
<<
253 "[q] or [quit] to quit the CEC test client and switch off all" << endl
<<
254 " connected CEC devices." << endl
<<
255 "================================================================================" << endl
;
258 int main (int argc
, char *argv
[])
260 int8_t iHDMIPort(-1);
261 cec_device_type_list typeList
;
265 bool bSingleCommand(false);
266 while (iArgPtr
< argc
)
268 if (argc
>= iArgPtr
+ 1)
270 if (!strcmp(argv
[iArgPtr
], "-f") ||
271 !strcmp(argv
[iArgPtr
], "--log-file") ||
272 !strcmp(argv
[iArgPtr
], "-sf") ||
273 !strcmp(argv
[iArgPtr
], "--short-log-file"))
275 if (argc
>= iArgPtr
+ 2)
277 g_logOutput
.open(argv
[iArgPtr
+ 1]);
278 g_bShortLog
= (!strcmp(argv
[iArgPtr
], "-sf") || !strcmp(argv
[iArgPtr
], "--short-log-file"));
283 cout
<< "== skipped log-file parameter: no file given ==" << endl
;
287 else if (!strcmp(argv
[iArgPtr
], "-d") ||
288 !strcmp(argv
[iArgPtr
], "--log-level"))
290 if (argc
>= iArgPtr
+ 2)
292 int iNewLevel
= atoi(argv
[iArgPtr
+ 1]);
293 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
295 g_cecLogLevel
= iNewLevel
;
297 cout
<< "log level set to " << argv
[iArgPtr
+ 1] << endl
;
301 cout
<< "== skipped log-level parameter: invalid level '" << argv
[iArgPtr
+ 1] << "' ==" << endl
;
307 cout
<< "== skipped log-level parameter: no level given ==" << endl
;
311 else if (!strcmp(argv
[iArgPtr
], "-t") ||
312 !strcmp(argv
[iArgPtr
], "--type"))
314 if (argc
>= iArgPtr
+ 2)
316 if (!strcmp(argv
[iArgPtr
+ 1], "p"))
319 cout
<< "== using device type 'playback device'" << endl
;
320 typeList
.add(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
322 else if (!strcmp(argv
[iArgPtr
+ 1], "r"))
325 cout
<< "== using device type 'recording device'" << endl
;
326 typeList
.add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
328 else if (!strcmp(argv
[iArgPtr
+ 1], "t"))
331 cout
<< "== using device type 'tuner'" << endl
;
332 typeList
.add(CEC_DEVICE_TYPE_TUNER
);
334 else if (!strcmp(argv
[iArgPtr
+ 1], "a"))
337 cout
<< "== using device type 'audio system'" << endl
;
338 typeList
.add(CEC_DEVICE_TYPE_AUDIO_SYSTEM
);
342 cout
<< "== skipped invalid device type '" << argv
[iArgPtr
+ 1] << "'" << endl
;
348 else if (!strcmp(argv
[iArgPtr
], "--list-devices") ||
349 !strcmp(argv
[iArgPtr
], "-l"))
351 ICECAdapter
*parser
= CreateParser(typeList
);
355 UnloadLibCec(parser
);
359 else if (!strcmp(argv
[iArgPtr
], "--single-command") ||
360 !strcmp(argv
[iArgPtr
], "-s"))
362 bSingleCommand
= true;
365 else if (!strcmp(argv
[iArgPtr
], "--help") ||
366 !strcmp(argv
[iArgPtr
], "-h"))
368 ShowHelpCommandLine(argv
[0]);
371 else if (!strcmp(argv
[iArgPtr
], "-p") ||
372 !strcmp(argv
[iArgPtr
], "--port"))
374 if (argc
>= iArgPtr
+ 2)
376 iHDMIPort
= (int8_t)atoi(argv
[iArgPtr
+ 1]);
377 cout
<< "using HDMI port '" << iHDMIPort
<< "'" << endl
;
384 g_strPort
= argv
[iArgPtr
++];
389 if (typeList
.IsEmpty())
392 cout
<< "No device type given. Using 'recording device'" << endl
;
393 typeList
.add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
396 ICECAdapter
*parser
= LibCecInit("CECTester", typeList
);
397 if (!parser
|| parser
->GetMinLibVersion() > CEC_TEST_CLIENT_VERSION
)
400 cout
<< "Cannot load libcec.dll" << endl
;
402 cout
<< "Cannot load libcec.so" << endl
;
410 strLog
.Format("CEC Parser created - libcec version %d.%d", parser
->GetLibVersionMajor(), parser
->GetLibVersionMinor());
411 cout
<< strLog
.c_str() << endl
;
413 //make stdin non-blocking
415 int flags
= fcntl(0, F_GETFL
, 0);
417 fcntl(0, F_SETFL
, flags
);
421 if (g_strPort
.IsEmpty())
424 cout
<< "no serial port given. trying autodetect: ";
425 cec_adapter devices
[10];
426 uint8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
427 if (iDevicesFound
<= 0)
430 cout
<< "autodetect ";
431 cout
<< "FAILED" << endl
;
432 UnloadLibCec(parser
);
439 cout
<< endl
<< " path: " << devices
[0].path
<< endl
<<
440 " com port: " << devices
[0].comm
<< endl
<< endl
;
442 g_strPort
= devices
[0].comm
;
448 parser
->SetHDMIPort((cec_logical_address
)CEC_DEFAULT_BASE_DEVICE
, (uint8_t)iHDMIPort
);
452 cout
<< "scanning the CEC bus..." << endl
;
454 if (!parser
->Open(g_strPort
.c_str()))
456 cout
<< "unable to open the device on port " << g_strPort
<< endl
;
458 UnloadLibCec(parser
);
464 cout
<< "cec device opened" << endl
;
466 parser
->PowerOnDevices(CECDEVICE_TV
);
469 parser
->SetActiveSource();
472 cout
<< "waiting for input" << endl
;
475 bool bContinue(true);
483 /* just ignore the command buffer and clear it */
485 while (parser
&& parser
->GetNextCommand(&dummy
)) {}
494 if (GetWord(input
, command
))
496 if (command
== "tx" || command
== "txn")
503 while (GetWord(input
, strvalue
) && HexStrToInt(strvalue
, ivalue
))
504 bytes
.PushBack(ivalue
);
506 if (command
== "txn")
507 bytes
.transmit_timeout
= 0;
509 parser
->Transmit(bytes
);
511 else if (command
== "on")
515 if (GetWord(input
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
517 parser
->PowerOnDevices((cec_logical_address
) iValue
);
521 cout
<< "invalid destination" << endl
;
524 else if (command
== "standby")
528 if (GetWord(input
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
530 parser
->StandbyDevices((cec_logical_address
) iValue
);
534 cout
<< "invalid destination" << endl
;
537 else if (command
== "poll")
541 if (GetWord(input
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
543 if (parser
->PollDevice((cec_logical_address
) iValue
))
544 cout
<< "POLL message sent" << endl
;
546 cout
<< "POLL message not sent" << endl
;
550 cout
<< "invalid destination" << endl
;
553 else if (command
== "la")
556 if (GetWord(input
, strvalue
))
558 parser
->SetLogicalAddress((cec_logical_address
) atoi(strvalue
.c_str()));
561 else if (command
== "p")
563 string strPort
, strDevice
;
564 if (GetWord(input
, strDevice
) && GetWord(input
, strPort
))
566 parser
->SetHDMIPort((cec_logical_address
)atoi(strDevice
.c_str()), (uint8_t)atoi(strPort
.c_str()));
569 else if (command
== "pa")
573 if (GetWord(input
, strB1
) && HexStrToInt(strB1
, iB1
) &&
574 GetWord(input
, strB2
) && HexStrToInt(strB2
, iB2
))
576 uint16_t iPhysicalAddress
= ((uint16_t)iB1
<< 8) + iB2
;
577 parser
->SetPhysicalAddress(iPhysicalAddress
);
580 else if (command
== "osd")
582 bool bFirstWord(false);
583 string strAddr
, strMessage
, strWord
;
585 if (GetWord(input
, strAddr
) && HexStrToInt(strAddr
, iAddr
) && iAddr
< 0xF)
587 while (GetWord(input
, strWord
))
592 strMessage
.append(" ");
594 strMessage
.append(strWord
);
596 parser
->SetOSDString((cec_logical_address
) iAddr
, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME
, strMessage
.c_str());
599 else if (command
== "ping")
601 parser
->PingAdapter();
603 else if (command
== "volup")
606 strLog
.Format("volume up: %2X", parser
->VolumeUp());
607 cout
<< strLog
.c_str() << endl
;
609 else if (command
== "voldown")
612 strLog
.Format("volume up: %2X", parser
->VolumeDown());
613 cout
<< strLog
.c_str() << endl
;
615 else if (command
== "mute")
618 strLog
.Format("mute: %2X", parser
->MuteAudio());
619 cout
<< strLog
.c_str() << endl
;
621 else if (command
== "mon")
623 CStdString strEnable
;
624 if (GetWord(input
, strEnable
) && (strEnable
.Equals("0") || strEnable
.Equals("1")))
626 parser
->SwitchMonitoring(strEnable
.Equals("1"));
629 else if (command
== "bl")
631 parser
->StartBootloader();
633 else if (command
== "lang")
636 if (GetWord(input
, strDev
))
638 int iDev
= atoi(strDev
);
639 if (iDev
>= 0 && iDev
< 15)
642 cec_menu_language language
;
643 if (parser
->GetDeviceMenuLanguage((cec_logical_address
) iDev
, &language
))
644 strLog
.Format("menu language '%s'", language
.language
);
647 cout
<< strLog
.c_str() << endl
;
651 else if (command
== "ven")
654 if (GetWord(input
, strDev
))
656 int iDev
= atoi(strDev
);
657 if (iDev
>= 0 && iDev
< 15)
659 uint64_t iVendor
= parser
->GetDeviceVendorId((cec_logical_address
) iDev
);
661 strLog
.Format("vendor id: %06x", iVendor
);
662 cout
<< strLog
.c_str() << endl
;
666 else if (command
== "ver")
669 if (GetWord(input
, strDev
))
671 int iDev
= atoi(strDev
);
672 if (iDev
>= 0 && iDev
< 15)
674 cec_version iVersion
= parser
->GetDeviceCecVersion((cec_logical_address
) iDev
);
677 case CEC_VERSION_1_2
:
678 cout
<< "CEC version 1.2" << endl
;
680 case CEC_VERSION_1_2A
:
681 cout
<< "CEC version 1.2a" << endl
;
683 case CEC_VERSION_1_3
:
684 cout
<< "CEC version 1.3" << endl
;
686 case CEC_VERSION_1_3A
:
687 cout
<< "CEC version 1.3a" << endl
;
690 cout
<< "unknown CEC version" << endl
;
696 else if (command
== "pow")
699 if (GetWord(input
, strDev
))
701 int iDev
= atoi(strDev
);
702 if (iDev
>= 0 && iDev
< 15)
704 cec_power_status iPower
= parser
->GetDevicePowerStatus((cec_logical_address
) iDev
);
707 case CEC_POWER_STATUS_ON
:
708 cout
<< "powered on" << endl
;
710 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
:
711 cout
<< "on -> standby" << endl
;
713 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
:
714 cout
<< "standby -> on" << endl
;
716 case CEC_POWER_STATUS_STANDBY
:
717 cout
<< "standby" << endl
;
720 cout
<< "unknown power status" << endl
;
726 else if (command
== "name")
729 if (GetWord(input
, strDev
))
731 int iDev
= atoi(strDev
);
732 if (iDev
>= 0 && iDev
< 15)
734 cec_osd_name name
= parser
->GetOSDName((cec_logical_address
)iDev
);
735 cout
<< "OSD name of device " << iDev
<< " is '" << name
.name
<< "'" << endl
;
739 else if (command
== "lad")
741 cout
<< "listing active devices:" << endl
;
742 cec_logical_addresses addresses
= parser
->GetActiveDevices();
743 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
745 cout
<< "logical address " << (int)iPtr
<< endl
;
747 else if (command
== "scan")
749 cout
<< "CEC bus information" << endl
;
750 cout
<< "===================" << endl
;
751 cec_logical_addresses addresses
= parser
->GetActiveDevices();
752 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
755 uint64_t iVendorId
= parser
->GetDeviceVendorId((cec_logical_address
)iPtr
);
756 cec_version iCecVersion
= parser
->GetDeviceCecVersion((cec_logical_address
)iPtr
);
757 cec_power_status power
= parser
->GetDevicePowerStatus((cec_logical_address
)iPtr
);
758 cec_osd_name osdName
= parser
->GetOSDName((cec_logical_address
)iPtr
);
759 cec_menu_language lang
;
760 lang
.device
= CECDEVICE_UNKNOWN
;
761 parser
->GetDeviceMenuLanguage((cec_logical_address
)iPtr
, &lang
);
763 cout
<< "device #" << (int)iPtr
<< ": " << parser
->ToString((cec_logical_address
)iPtr
) << endl
;
764 cout
<< "vendor: " << parser
->ToString((cec_vendor_id
)iVendorId
) << endl
;
765 cout
<< "osd string: " << osdName
.name
<< endl
;
766 cout
<< "CEC version: " << parser
->ToString(iCecVersion
) << endl
;
767 cout
<< "power status: " << parser
->ToString(power
) << endl
;
768 if ((uint8_t)lang
.device
== iPtr
)
769 cout
<< "language: " << lang
.language
<< endl
;
773 else if (command
== "ad")
776 if (GetWord(input
, strDev
))
778 int iDev
= atoi(strDev
);
779 if (iDev
>= 0 && iDev
< 15)
780 cout
<< "logical address " << iDev
<< " is " << (parser
->IsActiveDevice((cec_logical_address
)iDev
) ? "active" : "not active") << endl
;
783 else if (command
== "at")
786 if (GetWord(input
, strType
))
788 cec_device_type type
= CEC_DEVICE_TYPE_TV
;
789 if (strType
.Equals("a"))
790 type
= CEC_DEVICE_TYPE_AUDIO_SYSTEM
;
791 else if (strType
.Equals("p"))
792 type
= CEC_DEVICE_TYPE_PLAYBACK_DEVICE
;
793 else if (strType
.Equals("r"))
794 type
= CEC_DEVICE_TYPE_RECORDING_DEVICE
;
795 else if (strType
.Equals("t"))
796 type
= CEC_DEVICE_TYPE_TUNER
;
797 cout
<< "device " << type
<< " is " << (parser
->IsActiveDeviceType(type
) ? "active" : "not active") << endl
;
800 else if (command
== "r")
802 cout
<< "closing the connection" << endl
;
806 cout
<< "opening a new connection" << endl
;
807 parser
->Open(g_strPort
.c_str());
810 cout
<< "setting active source" << endl
;
811 parser
->SetActiveSource();
813 else if (command
== "h" || command
== "help")
817 else if (command
== "q" || command
== "quit")
821 else if (command
== "log")
824 if (GetWord(input
, strLevel
))
826 int iNewLevel
= atoi(strLevel
);
827 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
829 g_cecLogLevel
= iNewLevel
;
830 cout
<< "log level changed to " << strLevel
.c_str() << endl
;
836 cout
<< "waiting for input" << endl
;
840 CCondition::Sleep(50);
844 parser
->StandbyDevices(CECDEVICE_BROADCAST
);
848 UnloadLibCec(parser
);
850 if (g_logOutput
.is_open())