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 "[poll] {addr} poll the specified device." << endl
<<
237 "[lad] lists active devices on the bus" << endl
<<
238 "[ad] {addr} checks whether the specified device is active." << endl
<<
239 "[at] {type} checks whether the specified device type is active." << endl
<<
240 "[volup] send a volume up command to the amp if present" << endl
<<
241 "[voldown] send a volume down command to the amp if present" << endl
<<
242 "[mute] send a mute/unmute command to the amp if present" << endl
<<
243 "[mon] {1|0} enable or disable CEC bus monitoring." << endl
<<
244 "[log] {1 - 31} change the log level. see cectypes.h for values." << endl
<<
245 "[ping] send a ping command to the CEC adapter." << endl
<<
246 "[bl] to let the adapter enter the bootloader, to upgrade" << endl
<<
247 " the flash rom." << endl
<<
248 "[r] reconnect to the CEC adapter." << endl
<<
249 "[h] or [help] show this help." << endl
<<
250 "[q] or [quit] to quit the CEC test client and switch off all" << endl
<<
251 " connected CEC devices." << endl
<<
252 "================================================================================" << endl
;
255 int main (int argc
, char *argv
[])
257 int8_t iHDMIPort(-1);
258 cec_device_type_list typeList
;
262 bool bSingleCommand(false);
263 while (iArgPtr
< argc
)
265 if (argc
>= iArgPtr
+ 1)
267 if (!strcmp(argv
[iArgPtr
], "-f") ||
268 !strcmp(argv
[iArgPtr
], "--log-file") ||
269 !strcmp(argv
[iArgPtr
], "-sf") ||
270 !strcmp(argv
[iArgPtr
], "--short-log-file"))
272 if (argc
>= iArgPtr
+ 2)
274 g_logOutput
.open(argv
[iArgPtr
+ 1]);
275 g_bShortLog
= (!strcmp(argv
[iArgPtr
], "-sf") || !strcmp(argv
[iArgPtr
], "--short-log-file"));
280 cout
<< "== skipped log-file parameter: no file given ==" << endl
;
284 else if (!strcmp(argv
[iArgPtr
], "-d") ||
285 !strcmp(argv
[iArgPtr
], "--log-level"))
287 if (argc
>= iArgPtr
+ 2)
289 int iNewLevel
= atoi(argv
[iArgPtr
+ 1]);
290 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
292 g_cecLogLevel
= iNewLevel
;
294 cout
<< "log level set to " << argv
[iArgPtr
+ 1] << endl
;
298 cout
<< "== skipped log-level parameter: invalid level '" << argv
[iArgPtr
+ 1] << "' ==" << endl
;
304 cout
<< "== skipped log-level parameter: no level given ==" << endl
;
308 else if (!strcmp(argv
[iArgPtr
], "-t") ||
309 !strcmp(argv
[iArgPtr
], "--type"))
311 if (argc
>= iArgPtr
+ 2)
313 if (!strcmp(argv
[iArgPtr
+ 1], "p"))
316 cout
<< "== using device type 'playback device'" << endl
;
317 typeList
.add(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
319 else if (!strcmp(argv
[iArgPtr
+ 1], "r"))
322 cout
<< "== using device type 'recording device'" << endl
;
323 typeList
.add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
325 else if (!strcmp(argv
[iArgPtr
+ 1], "t"))
328 cout
<< "== using device type 'tuner'" << endl
;
329 typeList
.add(CEC_DEVICE_TYPE_TUNER
);
331 else if (!strcmp(argv
[iArgPtr
+ 1], "a"))
334 cout
<< "== using device type 'audio system'" << endl
;
335 typeList
.add(CEC_DEVICE_TYPE_AUDIO_SYSTEM
);
339 cout
<< "== skipped invalid device type '" << argv
[iArgPtr
+ 1] << "'" << endl
;
345 else if (!strcmp(argv
[iArgPtr
], "--list-devices") ||
346 !strcmp(argv
[iArgPtr
], "-l"))
348 ICECAdapter
*parser
= CreateParser(typeList
);
352 UnloadLibCec(parser
);
356 else if (!strcmp(argv
[iArgPtr
], "--single-command") ||
357 !strcmp(argv
[iArgPtr
], "-s"))
359 bSingleCommand
= true;
362 else if (!strcmp(argv
[iArgPtr
], "--help") ||
363 !strcmp(argv
[iArgPtr
], "-h"))
365 ShowHelpCommandLine(argv
[0]);
368 else if (!strcmp(argv
[iArgPtr
], "-p") ||
369 !strcmp(argv
[iArgPtr
], "--port"))
371 if (argc
>= iArgPtr
+ 2)
373 iHDMIPort
= atoi(argv
[iArgPtr
+ 1]);
374 cout
<< "using HDMI port '" << iHDMIPort
<< "'" << endl
;
381 g_strPort
= argv
[iArgPtr
++];
386 if (typeList
.IsEmpty())
389 cout
<< "No device type given. Using 'recording device'" << endl
;
390 typeList
.add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
393 ICECAdapter
*parser
= LibCecInit("CECTester", typeList
);
394 if (!parser
|| parser
->GetMinLibVersion() > CEC_TEST_CLIENT_VERSION
)
397 cout
<< "Cannot load libcec.dll" << endl
;
399 cout
<< "Cannot load libcec.so" << endl
;
407 strLog
.Format("CEC Parser created - libcec version %d.%d", parser
->GetLibVersionMajor(), parser
->GetLibVersionMinor());
408 cout
<< strLog
.c_str() << endl
;
410 //make stdin non-blocking
412 int flags
= fcntl(0, F_GETFL
, 0);
414 fcntl(0, F_SETFL
, flags
);
418 if (g_strPort
.IsEmpty())
421 cout
<< "no serial port given. trying autodetect: ";
422 cec_adapter devices
[10];
423 uint8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
424 if (iDevicesFound
<= 0)
427 cout
<< "autodetect ";
428 cout
<< "FAILED" << endl
;
429 UnloadLibCec(parser
);
436 cout
<< endl
<< " path: " << devices
[0].path
<< endl
<<
437 " com port: " << devices
[0].comm
<< endl
<< endl
;
439 g_strPort
= devices
[0].comm
;
445 parser
->SetHDMIPort((uint8_t)iHDMIPort
);
449 cout
<< "scanning the CEC bus..." << endl
;
451 if (!parser
->Open(g_strPort
.c_str()))
453 cout
<< "unable to open the device on port " << g_strPort
<< endl
;
455 UnloadLibCec(parser
);
461 cout
<< "cec device opened" << endl
;
463 parser
->PowerOnDevices(CECDEVICE_TV
);
466 parser
->SetActiveSource();
469 cout
<< "waiting for input" << endl
;
472 bool bContinue(true);
480 /* just ignore the command buffer and clear it */
482 while (parser
&& parser
->GetNextCommand(&dummy
)) {}
491 if (GetWord(input
, command
))
493 if (command
== "tx" || command
== "txn")
500 while (GetWord(input
, strvalue
) && HexStrToInt(strvalue
, ivalue
))
501 bytes
.PushBack(ivalue
);
503 if (command
== "txn")
504 bytes
.transmit_timeout
= 0;
506 parser
->Transmit(bytes
);
508 else if (command
== "on")
512 if (GetWord(input
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
514 parser
->PowerOnDevices((cec_logical_address
) iValue
);
518 cout
<< "invalid destination" << endl
;
521 else if (command
== "standby")
525 if (GetWord(input
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
527 parser
->StandbyDevices((cec_logical_address
) iValue
);
531 cout
<< "invalid destination" << endl
;
534 else if (command
== "poll")
538 if (GetWord(input
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
540 if (parser
->PollDevice((cec_logical_address
) iValue
))
541 cout
<< "POLL message sent" << endl
;
543 cout
<< "POLL message not sent" << endl
;
547 cout
<< "invalid destination" << endl
;
550 else if (command
== "la")
553 if (GetWord(input
, strvalue
))
555 parser
->SetLogicalAddress((cec_logical_address
) atoi(strvalue
.c_str()));
558 else if (command
== "p")
561 if (GetWord(input
, strvalue
))
563 parser
->SetHDMIPort(atoi(strvalue
.c_str()));
566 else if (command
== "pa")
570 if (GetWord(input
, strB1
) && HexStrToInt(strB1
, iB1
) &&
571 GetWord(input
, strB2
) && HexStrToInt(strB2
, iB2
))
573 uint16_t iPhysicalAddress
= ((uint16_t)iB1
<< 8) + iB2
;
574 parser
->SetPhysicalAddress(iPhysicalAddress
);
577 else if (command
== "osd")
579 bool bFirstWord(false);
580 string strAddr
, strMessage
, strWord
;
582 if (GetWord(input
, strAddr
) && HexStrToInt(strAddr
, iAddr
) && iAddr
< 0xF)
584 while (GetWord(input
, strWord
))
589 strMessage
.append(" ");
591 strMessage
.append(strWord
);
593 parser
->SetOSDString((cec_logical_address
) iAddr
, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME
, strMessage
.c_str());
596 else if (command
== "ping")
598 parser
->PingAdapter();
600 else if (command
== "volup")
604 else if (command
== "voldown")
606 parser
->VolumeDown();
608 else if (command
== "mute")
612 else if (command
== "mon")
614 CStdString strEnable
;
615 if (GetWord(input
, strEnable
) && (strEnable
.Equals("0") || strEnable
.Equals("1")))
617 parser
->SwitchMonitoring(strEnable
.Equals("1"));
620 else if (command
== "bl")
622 parser
->StartBootloader();
624 else if (command
== "lang")
627 if (GetWord(input
, strDev
))
629 int iDev
= atoi(strDev
);
630 if (iDev
>= 0 && iDev
< 15)
633 cec_menu_language language
;
634 if (parser
->GetDeviceMenuLanguage((cec_logical_address
) iDev
, &language
))
635 strLog
.Format("menu language '%s'", language
.language
);
638 cout
<< strLog
.c_str() << endl
;
642 else if (command
== "ven")
645 if (GetWord(input
, strDev
))
647 int iDev
= atoi(strDev
);
648 if (iDev
>= 0 && iDev
< 15)
650 uint64_t iVendor
= parser
->GetDeviceVendorId((cec_logical_address
) iDev
);
652 strLog
.Format("vendor id: %06x", iVendor
);
653 cout
<< strLog
.c_str() << endl
;
657 else if (command
== "ver")
660 if (GetWord(input
, strDev
))
662 int iDev
= atoi(strDev
);
663 if (iDev
>= 0 && iDev
< 15)
665 cec_version iVersion
= parser
->GetDeviceCecVersion((cec_logical_address
) iDev
);
668 case CEC_VERSION_1_2
:
669 cout
<< "CEC version 1.2" << endl
;
671 case CEC_VERSION_1_2A
:
672 cout
<< "CEC version 1.2a" << endl
;
674 case CEC_VERSION_1_3
:
675 cout
<< "CEC version 1.3" << endl
;
677 case CEC_VERSION_1_3A
:
678 cout
<< "CEC version 1.3a" << endl
;
681 cout
<< "unknown CEC version" << endl
;
687 else if (command
== "pow")
690 if (GetWord(input
, strDev
))
692 int iDev
= atoi(strDev
);
693 if (iDev
>= 0 && iDev
< 15)
695 cec_power_status iPower
= parser
->GetDevicePowerStatus((cec_logical_address
) iDev
);
698 case CEC_POWER_STATUS_ON
:
699 cout
<< "powered on" << endl
;
701 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
:
702 cout
<< "on -> standby" << endl
;
704 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
:
705 cout
<< "standby -> on" << endl
;
707 case CEC_POWER_STATUS_STANDBY
:
708 cout
<< "standby" << endl
;
711 cout
<< "unknown power status" << endl
;
717 else if (command
== "lad")
719 cout
<< "listing active devices:" << endl
;
720 cec_logical_addresses addresses
= parser
->GetActiveDevices();
721 for (unsigned iPtr
= 0; iPtr
< 16; iPtr
++)
723 cout
<< "logical address " << iPtr
<< endl
;
725 else if (command
== "ad")
728 if (GetWord(input
, strDev
))
730 int iDev
= atoi(strDev
);
731 if (iDev
>= 0 && iDev
< 15)
732 cout
<< "logical address " << iDev
<< " is " << (parser
->IsActiveDevice((cec_logical_address
)iDev
) ? "active" : "not active") << endl
;
735 else if (command
== "at")
738 if (GetWord(input
, 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
;
749 cout
<< "device " << type
<< " is " << (parser
->IsActiveDeviceType(type
) ? "active" : "not active") << endl
;
752 else if (command
== "r")
754 cout
<< "closing the connection" << endl
;
758 cout
<< "opening a new connection" << endl
;
759 parser
->Open(g_strPort
.c_str());
762 cout
<< "setting active source" << endl
;
763 parser
->SetActiveSource();
765 else if (command
== "h" || command
== "help")
769 else if (command
== "q" || command
== "quit")
773 else if (command
== "log")
776 if (GetWord(input
, strLevel
))
778 int iNewLevel
= atoi(strLevel
);
779 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
781 g_cecLogLevel
= iNewLevel
;
782 cout
<< "log level changed to " << strLevel
.c_str() << endl
;
788 cout
<< "waiting for input" << endl
;
792 CCondition::Sleep(50);
796 parser
->StandbyDevices(CECDEVICE_BROADCAST
);
800 UnloadLibCec(parser
);
802 if (g_logOutput
.is_open())