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"
47 #define CEC_TEST_CLIENT_VERSION 1
49 #include <cecloader.h>
51 int g_cecLogLevel
= CEC_LOG_ALL
;
53 bool g_bShortLog
= false;
56 inline bool HexStrToInt(const std::string
& data
, uint8_t& value
)
59 if (sscanf(data
.c_str(), "%x", &iTmp
) == 1)
66 value
= (uint8_t) iTmp
;
74 //get the first word (separated by whitespace) from string data and place that in word
75 //then remove that word from string data
76 bool GetWord(string
& data
, string
& word
)
78 stringstream
datastream(data
);
82 if (datastream
.fail())
88 size_t pos
= data
.find(word
) + word
.length();
90 if (pos
>= data
.length())
96 data
= data
.substr(pos
);
102 if (datastream
.fail())
108 void FlushLog(ICECAdapter
*cecParser
)
110 cec_log_message message
;
111 while (cecParser
&& cecParser
->GetNextLogMessage(&message
))
113 if ((message
.level
& g_cecLogLevel
) == message
.level
)
116 switch (message
.level
)
119 strLevel
= "ERROR: ";
121 case CEC_LOG_WARNING
:
122 strLevel
= "WARNING: ";
125 strLevel
= "NOTICE: ";
127 case CEC_LOG_TRAFFIC
:
128 strLevel
= "TRAFFIC: ";
131 strLevel
= "DEBUG: ";
137 CStdString strFullLog
;
138 strFullLog
.Format("%s[%16lld]\t%s", strLevel
.c_str(), message
.time
, message
.message
);
139 cout
<< strFullLog
.c_str() << endl
;
141 if (g_logOutput
.is_open())
144 g_logOutput
<< message
.message
<< endl
;
146 g_logOutput
<< strFullLog
.c_str() << endl
;
152 void ListDevices(ICECAdapter
*parser
)
154 cec_adapter
*devices
= new cec_adapter
[10];
155 uint8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
156 if (iDevicesFound
<= 0)
158 cout
<< "Found devices: NONE" << endl
;
163 strLog
.Format("Found devices: %d", iDevicesFound
);
164 cout
<< strLog
.c_str() << endl
;
165 for (unsigned int iDevicePtr
= 0; iDevicePtr
< iDevicesFound
; iDevicePtr
++)
167 CStdString strDevice
;
168 strDevice
.Format("device: %d\npath: %s\ncom port: %s", iDevicePtr
+ 1, devices
[iDevicePtr
].path
, devices
[iDevicePtr
].comm
);
169 cout
<< endl
<< strDevice
.c_str() << endl
;
174 void ShowHelpCommandLine(const char* strExec
)
177 strExec
<< " {-h|--help|-l|--list-devices|[COM PORT]}" << endl
<<
179 "parameters:" << endl
<<
180 " -h --help Shows this help text" << endl
<<
181 " -l --list-devices List all devices on this system" << endl
<<
182 " -t --type {p|r|t|a} The device type to use. More than one is possible." << endl
<<
183 " -p --port {int} The HDMI port to use as active source." << endl
<<
184 " -f --log-file {file} Writes all libCEC log message to a file" << endl
<<
185 " -sf --short-log-file {file} Writes all libCEC log message without timestamps" << endl
<<
186 " and log levels to a file." << endl
<<
187 " -d --log-level {level} Sets the log level. See cectypes.h for values." << endl
<<
188 " -s --single-command Execute a single command and exit. Does not power" << endl
<<
189 " on devices on startup and power them off on exit." << endl
<<
190 " [COM PORT] The com port to connect to. If no COM" << endl
<<
191 " port is given, the client tries to connect to the" << endl
<<
192 " first device that is detected." << endl
<<
194 "Type 'h' or 'help' and press enter after starting the client to display all " << endl
<<
195 "available commands" << endl
;
198 ICECAdapter
*CreateParser(cec_device_type_list typeList
)
200 ICECAdapter
*parser
= LibCecInit("CECTester", typeList
);
201 if (!parser
|| parser
->GetMinLibVersion() > CEC_TEST_CLIENT_VERSION
)
204 cout
<< "Cannot load libcec.dll" << endl
;
206 cout
<< "Cannot load libcec.so" << endl
;
212 strLog
.Format("CEC Parser created - libcec version %d.%d", parser
->GetLibVersionMajor(), parser
->GetLibVersionMinor());
213 cout
<< strLog
.c_str() << endl
;
218 void ShowHelpConsole(void)
221 "================================================================================" << endl
<<
222 "Available commands:" << endl
<<
224 "[tx] {bytes} transfer bytes over the CEC line." << endl
<<
225 "[txn] {bytes} transfer bytes but don't wait for transmission ACK." << endl
<<
226 "[on] {address} power on the device with the given logical address." << endl
<<
227 "[standby] {address} put the device with the given address in standby mode." << endl
<<
228 "[la] {logical address} change the logical address of the CEC adapter." << endl
<<
229 "[p] {port number} change the HDMI port number of the CEC adapter." << endl
<<
230 "[pa] {physical address} change the physical address of the CEC adapter." << endl
<<
231 "[osd] {addr} {string} set OSD message on the specified device." << endl
<<
232 "[ver] {addr} get the CEC version of the specified device." << endl
<<
233 "[ven] {addr} get the vendor ID of the specified device." << endl
<<
234 "[lang] {addr} get the menu language of the specified device." << endl
<<
235 "[pow] {addr} get the power status of the specified device." << endl
<<
236 "[name] {addr} get the OSD name of the specified device." << endl
<<
237 "[poll] {addr} poll the specified device." << endl
<<
238 "[lad] lists active devices on the bus" << endl
<<
239 "[ad] {addr} checks whether the specified device is active." << endl
<<
240 "[at] {type} checks whether the specified device type is active." << endl
<<
241 "[volup] send a volume up command to the amp if present" << endl
<<
242 "[voldown] send a volume down command to the amp if present" << endl
<<
243 "[mute] send a mute/unmute command to the amp if present" << endl
<<
244 "[mon] {1|0} enable or disable CEC bus monitoring." << endl
<<
245 "[log] {1 - 31} change the log level. see cectypes.h for values." << endl
<<
246 "[ping] send a ping command to the CEC adapter." << endl
<<
247 "[bl] to let the adapter enter the bootloader, to upgrade" << endl
<<
248 " the flash rom." << endl
<<
249 "[r] reconnect to the CEC adapter." << endl
<<
250 "[h] or [help] show this help." << endl
<<
251 "[q] or [quit] to quit the CEC test client and switch off all" << endl
<<
252 " connected CEC devices." << endl
<<
253 "================================================================================" << endl
;
256 int main (int argc
, char *argv
[])
258 int8_t iHDMIPort(-1);
259 cec_device_type_list typeList
;
263 bool bSingleCommand(false);
264 while (iArgPtr
< argc
)
266 if (argc
>= iArgPtr
+ 1)
268 if (!strcmp(argv
[iArgPtr
], "-f") ||
269 !strcmp(argv
[iArgPtr
], "--log-file") ||
270 !strcmp(argv
[iArgPtr
], "-sf") ||
271 !strcmp(argv
[iArgPtr
], "--short-log-file"))
273 if (argc
>= iArgPtr
+ 2)
275 g_logOutput
.open(argv
[iArgPtr
+ 1]);
276 g_bShortLog
= (!strcmp(argv
[iArgPtr
], "-sf") || !strcmp(argv
[iArgPtr
], "--short-log-file"));
281 cout
<< "== skipped log-file parameter: no file given ==" << endl
;
285 else if (!strcmp(argv
[iArgPtr
], "-d") ||
286 !strcmp(argv
[iArgPtr
], "--log-level"))
288 if (argc
>= iArgPtr
+ 2)
290 int iNewLevel
= atoi(argv
[iArgPtr
+ 1]);
291 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
293 g_cecLogLevel
= iNewLevel
;
295 cout
<< "log level set to " << argv
[iArgPtr
+ 1] << endl
;
299 cout
<< "== skipped log-level parameter: invalid level '" << argv
[iArgPtr
+ 1] << "' ==" << endl
;
305 cout
<< "== skipped log-level parameter: no level given ==" << endl
;
309 else if (!strcmp(argv
[iArgPtr
], "-t") ||
310 !strcmp(argv
[iArgPtr
], "--type"))
312 if (argc
>= iArgPtr
+ 2)
314 if (!strcmp(argv
[iArgPtr
+ 1], "p"))
317 cout
<< "== using device type 'playback device'" << endl
;
318 typeList
.add(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
320 else if (!strcmp(argv
[iArgPtr
+ 1], "r"))
323 cout
<< "== using device type 'recording device'" << endl
;
324 typeList
.add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
326 else if (!strcmp(argv
[iArgPtr
+ 1], "t"))
329 cout
<< "== using device type 'tuner'" << endl
;
330 typeList
.add(CEC_DEVICE_TYPE_TUNER
);
332 else if (!strcmp(argv
[iArgPtr
+ 1], "a"))
335 cout
<< "== using device type 'audio system'" << endl
;
336 typeList
.add(CEC_DEVICE_TYPE_AUDIO_SYSTEM
);
340 cout
<< "== skipped invalid device type '" << argv
[iArgPtr
+ 1] << "'" << endl
;
346 else if (!strcmp(argv
[iArgPtr
], "--list-devices") ||
347 !strcmp(argv
[iArgPtr
], "-l"))
349 ICECAdapter
*parser
= CreateParser(typeList
);
353 UnloadLibCec(parser
);
357 else if (!strcmp(argv
[iArgPtr
], "--single-command") ||
358 !strcmp(argv
[iArgPtr
], "-s"))
360 bSingleCommand
= true;
363 else if (!strcmp(argv
[iArgPtr
], "--help") ||
364 !strcmp(argv
[iArgPtr
], "-h"))
366 ShowHelpCommandLine(argv
[0]);
369 else if (!strcmp(argv
[iArgPtr
], "-p") ||
370 !strcmp(argv
[iArgPtr
], "--port"))
372 if (argc
>= iArgPtr
+ 2)
374 iHDMIPort
= (int8_t)atoi(argv
[iArgPtr
+ 1]);
375 cout
<< "using HDMI port '" << iHDMIPort
<< "'" << endl
;
382 g_strPort
= argv
[iArgPtr
++];
387 if (typeList
.IsEmpty())
390 cout
<< "No device type given. Using 'recording device'" << endl
;
391 typeList
.add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
394 ICECAdapter
*parser
= LibCecInit("CECTester", typeList
);
395 if (!parser
|| parser
->GetMinLibVersion() > CEC_TEST_CLIENT_VERSION
)
398 cout
<< "Cannot load libcec.dll" << endl
;
400 cout
<< "Cannot load libcec.so" << endl
;
408 strLog
.Format("CEC Parser created - libcec version %d.%d", parser
->GetLibVersionMajor(), parser
->GetLibVersionMinor());
409 cout
<< strLog
.c_str() << endl
;
411 //make stdin non-blocking
413 int flags
= fcntl(0, F_GETFL
, 0);
415 fcntl(0, F_SETFL
, flags
);
419 if (g_strPort
.IsEmpty())
422 cout
<< "no serial port given. trying autodetect: ";
423 cec_adapter devices
[10];
424 uint8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
425 if (iDevicesFound
<= 0)
428 cout
<< "autodetect ";
429 cout
<< "FAILED" << endl
;
430 UnloadLibCec(parser
);
437 cout
<< endl
<< " path: " << devices
[0].path
<< endl
<<
438 " com port: " << devices
[0].comm
<< endl
<< endl
;
440 g_strPort
= devices
[0].comm
;
446 parser
->SetHDMIPort((uint8_t)iHDMIPort
);
450 cout
<< "scanning the CEC bus..." << endl
;
452 if (!parser
->Open(g_strPort
.c_str()))
454 cout
<< "unable to open the device on port " << g_strPort
<< endl
;
456 UnloadLibCec(parser
);
462 cout
<< "cec device opened" << endl
;
464 parser
->PowerOnDevices(CECDEVICE_TV
);
467 parser
->SetActiveSource();
470 cout
<< "waiting for input" << endl
;
473 bool bContinue(true);
481 /* just ignore the command buffer and clear it */
483 while (parser
&& parser
->GetNextCommand(&dummy
)) {}
492 if (GetWord(input
, command
))
494 if (command
== "tx" || command
== "txn")
501 while (GetWord(input
, strvalue
) && HexStrToInt(strvalue
, ivalue
))
502 bytes
.PushBack(ivalue
);
504 if (command
== "txn")
505 bytes
.transmit_timeout
= 0;
507 parser
->Transmit(bytes
);
509 else if (command
== "on")
513 if (GetWord(input
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
515 parser
->PowerOnDevices((cec_logical_address
) iValue
);
519 cout
<< "invalid destination" << endl
;
522 else if (command
== "standby")
526 if (GetWord(input
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
528 parser
->StandbyDevices((cec_logical_address
) iValue
);
532 cout
<< "invalid destination" << endl
;
535 else if (command
== "poll")
539 if (GetWord(input
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
541 if (parser
->PollDevice((cec_logical_address
) iValue
))
542 cout
<< "POLL message sent" << endl
;
544 cout
<< "POLL message not sent" << endl
;
548 cout
<< "invalid destination" << endl
;
551 else if (command
== "la")
554 if (GetWord(input
, strvalue
))
556 parser
->SetLogicalAddress((cec_logical_address
) atoi(strvalue
.c_str()));
559 else if (command
== "p")
562 if (GetWord(input
, strvalue
))
564 parser
->SetHDMIPort((uint8_t)atoi(strvalue
.c_str()));
567 else if (command
== "pa")
571 if (GetWord(input
, strB1
) && HexStrToInt(strB1
, iB1
) &&
572 GetWord(input
, strB2
) && HexStrToInt(strB2
, iB2
))
574 uint16_t iPhysicalAddress
= ((uint16_t)iB1
<< 8) + iB2
;
575 parser
->SetPhysicalAddress(iPhysicalAddress
);
578 else if (command
== "osd")
580 bool bFirstWord(false);
581 string strAddr
, strMessage
, strWord
;
583 if (GetWord(input
, strAddr
) && HexStrToInt(strAddr
, iAddr
) && iAddr
< 0xF)
585 while (GetWord(input
, strWord
))
590 strMessage
.append(" ");
592 strMessage
.append(strWord
);
594 parser
->SetOSDString((cec_logical_address
) iAddr
, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME
, strMessage
.c_str());
597 else if (command
== "ping")
599 parser
->PingAdapter();
601 else if (command
== "volup")
604 strLog
.Format("volume up: %2X", parser
->VolumeUp());
605 cout
<< strLog
.c_str() << endl
;
607 else if (command
== "voldown")
610 strLog
.Format("volume up: %2X", parser
->VolumeDown());
611 cout
<< strLog
.c_str() << endl
;
613 else if (command
== "mute")
616 strLog
.Format("mute: %2X", parser
->MuteAudio());
617 cout
<< strLog
.c_str() << endl
;
619 else if (command
== "mon")
621 CStdString strEnable
;
622 if (GetWord(input
, strEnable
) && (strEnable
.Equals("0") || strEnable
.Equals("1")))
624 parser
->SwitchMonitoring(strEnable
.Equals("1"));
627 else if (command
== "bl")
629 parser
->StartBootloader();
631 else if (command
== "lang")
634 if (GetWord(input
, strDev
))
636 int iDev
= atoi(strDev
);
637 if (iDev
>= 0 && iDev
< 15)
640 cec_menu_language language
;
641 if (parser
->GetDeviceMenuLanguage((cec_logical_address
) iDev
, &language
))
642 strLog
.Format("menu language '%s'", language
.language
);
645 cout
<< strLog
.c_str() << endl
;
649 else if (command
== "ven")
652 if (GetWord(input
, strDev
))
654 int iDev
= atoi(strDev
);
655 if (iDev
>= 0 && iDev
< 15)
657 uint64_t iVendor
= parser
->GetDeviceVendorId((cec_logical_address
) iDev
);
659 strLog
.Format("vendor id: %06x", iVendor
);
660 cout
<< strLog
.c_str() << endl
;
664 else if (command
== "ver")
667 if (GetWord(input
, strDev
))
669 int iDev
= atoi(strDev
);
670 if (iDev
>= 0 && iDev
< 15)
672 cec_version iVersion
= parser
->GetDeviceCecVersion((cec_logical_address
) iDev
);
675 case CEC_VERSION_1_2
:
676 cout
<< "CEC version 1.2" << endl
;
678 case CEC_VERSION_1_2A
:
679 cout
<< "CEC version 1.2a" << endl
;
681 case CEC_VERSION_1_3
:
682 cout
<< "CEC version 1.3" << endl
;
684 case CEC_VERSION_1_3A
:
685 cout
<< "CEC version 1.3a" << endl
;
688 cout
<< "unknown CEC version" << endl
;
694 else if (command
== "pow")
697 if (GetWord(input
, strDev
))
699 int iDev
= atoi(strDev
);
700 if (iDev
>= 0 && iDev
< 15)
702 cec_power_status iPower
= parser
->GetDevicePowerStatus((cec_logical_address
) iDev
);
705 case CEC_POWER_STATUS_ON
:
706 cout
<< "powered on" << endl
;
708 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
:
709 cout
<< "on -> standby" << endl
;
711 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
:
712 cout
<< "standby -> on" << endl
;
714 case CEC_POWER_STATUS_STANDBY
:
715 cout
<< "standby" << endl
;
718 cout
<< "unknown power status" << endl
;
724 else if (command
== "name")
727 if (GetWord(input
, strDev
))
729 int iDev
= atoi(strDev
);
730 if (iDev
>= 0 && iDev
< 15)
732 cec_osd_name name
= parser
->GetOSDName((cec_logical_address
)iDev
);
733 cout
<< "OSD name of device " << iDev
<< " is '" << name
.name
<< "'" << endl
;
737 else if (command
== "lad")
739 cout
<< "listing active devices:" << endl
;
740 cec_logical_addresses addresses
= parser
->GetActiveDevices();
741 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
743 cout
<< "logical address " << iPtr
<< endl
;
745 else if (command
== "ad")
748 if (GetWord(input
, strDev
))
750 int iDev
= atoi(strDev
);
751 if (iDev
>= 0 && iDev
< 15)
752 cout
<< "logical address " << iDev
<< " is " << (parser
->IsActiveDevice((cec_logical_address
)iDev
) ? "active" : "not active") << endl
;
755 else if (command
== "at")
758 if (GetWord(input
, strType
))
760 cec_device_type type
= CEC_DEVICE_TYPE_TV
;
761 if (strType
.Equals("a"))
762 type
= CEC_DEVICE_TYPE_AUDIO_SYSTEM
;
763 else if (strType
.Equals("p"))
764 type
= CEC_DEVICE_TYPE_PLAYBACK_DEVICE
;
765 else if (strType
.Equals("r"))
766 type
= CEC_DEVICE_TYPE_RECORDING_DEVICE
;
767 else if (strType
.Equals("t"))
768 type
= CEC_DEVICE_TYPE_TUNER
;
769 cout
<< "device " << type
<< " is " << (parser
->IsActiveDeviceType(type
) ? "active" : "not active") << endl
;
772 else if (command
== "r")
774 cout
<< "closing the connection" << endl
;
778 cout
<< "opening a new connection" << endl
;
779 parser
->Open(g_strPort
.c_str());
782 cout
<< "setting active source" << endl
;
783 parser
->SetActiveSource();
785 else if (command
== "h" || command
== "help")
789 else if (command
== "q" || command
== "quit")
793 else if (command
== "log")
796 if (GetWord(input
, strLevel
))
798 int iNewLevel
= atoi(strLevel
);
799 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
801 g_cecLogLevel
= iNewLevel
;
802 cout
<< "log level changed to " << strLevel
.c_str() << endl
;
808 cout
<< "waiting for input" << endl
;
812 CCondition::Sleep(50);
816 parser
->StandbyDevices(CECDEVICE_BROADCAST
);
820 UnloadLibCec(parser
);
822 if (g_logOutput
.is_open())