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
;
52 int g_iLogicalAddress
= CECDEVICE_PLAYBACKDEVICE1
;
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 flush_log(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 list_devices(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 show_help(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 " -la --logical-address {a} The logical address to use." << 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 " [COM PORT] The com port to connect to. If no COM" << endl
<<
189 " port is given, the client tries to connect to the" << endl
<<
190 " first device that is detected." << endl
<<
192 "Type 'h' or 'help' and press enter after starting the client to display all " << endl
<<
193 "available commands" << endl
;
196 void show_console_help(void)
199 "================================================================================" << endl
<<
200 "Available commands:" << endl
<<
202 "tx {bytes} transfer bytes over the CEC line." << endl
<<
203 "txn {bytes} transfer bytes but don't wait for transmission ACK." << endl
<<
204 "[tx 40 00 FF 11 22 33] sends bytes 0x40 0x00 0xFF 0x11 0x22 0x33" << endl
<<
206 "on {address} power on the device with the given logical address." << endl
<<
207 "[on 5] power on a connected audio system" << endl
<<
209 "standby {address} put the device with the given address in standby mode." << endl
<<
210 "[standby 0] powers off the TV" << endl
<<
212 "la {logical_address} change the logical address of the CEC adapter." << endl
<<
213 "[la 4] logical address 4" << endl
<<
215 "pa {physical_address} change the physical address of the CEC adapter." << endl
<<
216 "[pa 10 00] physical address 1.0.0.0" << endl
<<
218 "osd {addr} {string} set OSD message on the specified device." << endl
<<
219 "[osd 0 Test Message] displays 'Test Message' on the TV" << endl
<<
221 "ver {addr} get the CEC version of the specified device." << endl
<<
222 "[ver 0] get the CEC version of the TV" << endl
<<
224 "ven {addr} get the vendor ID of the specified device." << endl
<<
225 "[ven 0] get the vendor ID of the TV" << endl
<<
227 "lang {addr} get the menu language of the specified device." << endl
<<
228 "[lang 0] get the menu language of the TV" << endl
<<
230 "pow {addr} get the power status of the specified device." << endl
<<
231 "[pow 0] get the power status of the TV" << endl
<<
233 "poll {addr} poll the specified device." << endl
<<
234 "[poll 0] sends a poll message to the TV" << endl
<<
236 "[mon] {1|0} enable or disable CEC bus monitoring." << endl
<<
237 "[log] {1 - 31} change the log level. see cectypes.h for values." << endl
<<
238 "[ping] send a ping command to the CEC adapter." << endl
<<
239 "[bl] to let the adapter enter the bootloader, to upgrade" << endl
<<
240 " the flash rom." << endl
<<
241 "[r] reconnect to the CEC adapter." << endl
<<
242 "[h] or [help] show this help." << endl
<<
243 "[q] or [quit] to quit the CEC test client and switch off all" << endl
<<
244 " connected CEC devices." << endl
<<
245 "================================================================================" << endl
;
248 int main (int argc
, char *argv
[])
250 ICECAdapter
*parser
= LoadLibCec("CECTester");
251 if (!parser
|| parser
->GetMinLibVersion() > CEC_TEST_CLIENT_VERSION
)
254 cout
<< "Cannot load libcec.dll" << endl
;
256 cout
<< "Cannot load libcec.so" << endl
;
261 strLog
.Format("CEC Parser created - libcec version %d.%d", parser
->GetLibVersionMajor(), parser
->GetLibVersionMinor());
262 cout
<< strLog
.c_str() << endl
;
264 //make stdin non-blocking
266 int flags
= fcntl(0, F_GETFL
, 0);
268 fcntl(0, F_SETFL
, flags
);
272 while (iArgPtr
< argc
)
274 if (argc
>= iArgPtr
+ 1)
276 if (!strcmp(argv
[iArgPtr
], "-f") ||
277 !strcmp(argv
[iArgPtr
], "--log-file") ||
278 !strcmp(argv
[iArgPtr
], "-sf") ||
279 !strcmp(argv
[iArgPtr
], "--short-log-file"))
281 if (argc
>= iArgPtr
+ 2)
283 g_logOutput
.open(argv
[iArgPtr
+ 1]);
284 g_bShortLog
= (!strcmp(argv
[iArgPtr
], "-sf") || !strcmp(argv
[iArgPtr
], "--short-log-file"));
289 cout
<< "== skipped log-file parameter: no file given ==" << endl
;
293 else if (!strcmp(argv
[iArgPtr
], "-d") ||
294 !strcmp(argv
[iArgPtr
], "--log-level"))
296 if (argc
>= iArgPtr
+ 2)
298 int iNewLevel
= atoi(argv
[iArgPtr
+ 1]);
299 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
301 g_cecLogLevel
= iNewLevel
;
302 cout
<< "log level set to " << argv
[iArgPtr
+ 1] << endl
;
306 cout
<< "== skipped log-level parameter: invalid level '" << argv
[iArgPtr
+ 1] << "' ==" << endl
;
312 cout
<< "== skipped log-level parameter: no level given ==" << endl
;
316 else if (!strcmp(argv
[iArgPtr
], "-la") ||
317 !strcmp(argv
[iArgPtr
], "--logical-address"))
319 if (argc
>= iArgPtr
+ 2)
321 int iNewAddress
= atoi(argv
[iArgPtr
+ 1]);
322 if (iNewAddress
>= 0 && iNewAddress
<= 15)
324 g_iLogicalAddress
= iNewAddress
;
325 cout
<< "logical address set to " << argv
[iArgPtr
+ 1] << endl
;
329 cout
<< "== skipped logical-address parameter: invalid address '" << argv
[iArgPtr
+ 1] << "' ==" << endl
;
335 cout
<< "== skipped logical-address parameter: no address given ==" << endl
;
339 else if (!strcmp(argv
[iArgPtr
], "--list-devices") ||
340 !strcmp(argv
[iArgPtr
], "-l"))
342 list_devices(parser
);
343 UnloadLibCec(parser
);
346 else if (!strcmp(argv
[iArgPtr
], "--help") ||
347 !strcmp(argv
[iArgPtr
], "-h"))
350 UnloadLibCec(parser
);
355 g_strPort
= argv
[iArgPtr
++];
360 if (g_strPort
.IsEmpty())
362 cout
<< "no serial port given. trying autodetect: ";
363 cec_adapter devices
[10];
364 uint8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
365 if (iDevicesFound
<= 0)
367 cout
<< "FAILED" << endl
;
368 UnloadLibCec(parser
);
373 cout
<< endl
<< " path: " << devices
[0].path
<< endl
<<
374 " com port: " << devices
[0].comm
<< endl
<< endl
;
375 g_strPort
= devices
[0].comm
;
379 parser
->SetLogicalAddress((cec_logical_address
) g_iLogicalAddress
);
381 if (!parser
->Open(g_strPort
.c_str()))
383 cout
<< "unable to open the device on port " << g_strPort
<< endl
;
385 UnloadLibCec(parser
);
389 cout
<< "cec device opened" << endl
;
391 parser
->PowerOnDevices(CECDEVICE_TV
);
394 parser
->SetActiveView();
397 bool bContinue(true);
398 cout
<< "waiting for input" << endl
;
403 /* just ignore the command buffer and clear it */
405 while (parser
&& parser
->GetNextCommand(&dummy
)) {}
414 if (GetWord(input
, command
))
416 if (command
== "tx" || command
== "txn")
423 while (GetWord(input
, strvalue
) && HexStrToInt(strvalue
, ivalue
))
424 bytes
.push_back(ivalue
);
426 if (command
== "txn")
427 bytes
.transmit_timeout
= 0;
429 parser
->Transmit(bytes
);
431 else if (command
== "on")
435 if (GetWord(input
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
437 parser
->PowerOnDevices((cec_logical_address
) iValue
);
441 cout
<< "invalid destination" << endl
;
444 else if (command
== "standby")
448 if (GetWord(input
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
450 parser
->StandbyDevices((cec_logical_address
) iValue
);
454 cout
<< "invalid destination" << endl
;
457 else if (command
== "poll")
461 if (GetWord(input
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
463 if (parser
->PollDevice((cec_logical_address
) iValue
))
464 cout
<< "POLL message sent" << endl
;
466 cout
<< "POLL message not sent" << endl
;
470 cout
<< "invalid destination" << endl
;
473 else if (command
== "la")
476 if (GetWord(input
, strvalue
))
478 parser
->SetLogicalAddress((cec_logical_address
) atoi(strvalue
.c_str()));
481 else if (command
== "pa")
485 if (GetWord(input
, strB1
) && HexStrToInt(strB1
, iB1
) &&
486 GetWord(input
, strB2
) && HexStrToInt(strB2
, iB2
))
488 uint16_t iPhysicalAddress
= ((uint16_t)iB1
<< 8) + iB2
;
489 parser
->SetPhysicalAddress(iPhysicalAddress
);
492 else if (command
== "osd")
494 bool bFirstWord(false);
495 string strAddr
, strMessage
, strWord
;
497 if (GetWord(input
, strAddr
) && HexStrToInt(strAddr
, iAddr
) && iAddr
< 0xF)
499 while (GetWord(input
, strWord
))
504 strMessage
.append(" ");
506 strMessage
.append(strWord
);
508 parser
->SetOSDString((cec_logical_address
) iAddr
, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME
, strMessage
.c_str());
511 else if (command
== "ping")
513 parser
->PingAdapter();
515 else if (command
== "mon")
517 CStdString strEnable
;
518 if (GetWord(input
, strEnable
) && (strEnable
.Equals("0") || strEnable
.Equals("1")))
520 parser
->SwitchMonitoring(strEnable
.Equals("1"));
523 else if (command
== "bl")
525 parser
->StartBootloader();
527 else if (command
== "lang")
530 if (GetWord(input
, strDev
))
532 int iDev
= atoi(strDev
);
533 if (iDev
>= 0 && iDev
< 15)
536 cec_menu_language language
;
537 if (parser
->GetDeviceMenuLanguage((cec_logical_address
) iDev
, &language
))
538 strLog
.Format("menu language '%s'", language
.language
);
541 cout
<< strLog
.c_str() << endl
;
545 else if (command
== "ven")
548 if (GetWord(input
, strDev
))
550 int iDev
= atoi(strDev
);
551 if (iDev
>= 0 && iDev
< 15)
553 uint64_t iVendor
= parser
->GetDeviceVendorId((cec_logical_address
) iDev
);
555 strLog
.Format("vendor id: %06x", iVendor
);
556 cout
<< strLog
.c_str() << endl
;
560 else if (command
== "ver")
563 if (GetWord(input
, strDev
))
565 int iDev
= atoi(strDev
);
566 if (iDev
>= 0 && iDev
< 15)
568 cec_version iVersion
= parser
->GetDeviceCecVersion((cec_logical_address
) iDev
);
571 case CEC_VERSION_1_2
:
572 cout
<< "CEC version 1.2" << endl
;
574 case CEC_VERSION_1_2A
:
575 cout
<< "CEC version 1.2a" << endl
;
577 case CEC_VERSION_1_3
:
578 cout
<< "CEC version 1.3" << endl
;
580 case CEC_VERSION_1_3A
:
581 cout
<< "CEC version 1.3a" << endl
;
584 cout
<< "unknown CEC version" << endl
;
590 else if (command
== "pow")
593 if (GetWord(input
, strDev
))
595 int iDev
= atoi(strDev
);
596 if (iDev
>= 0 && iDev
< 15)
598 cec_power_status iPower
= parser
->GetDevicePowerStatus((cec_logical_address
) iDev
);
601 case CEC_POWER_STATUS_ON
:
602 cout
<< "powered on" << endl
;
604 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
:
605 cout
<< "on -> standby" << endl
;
607 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
:
608 cout
<< "standby -> on" << endl
;
610 case CEC_POWER_STATUS_STANDBY
:
611 cout
<< "standby" << endl
;
614 cout
<< "unknown power status" << endl
;
620 else if (command
== "r")
622 cout
<< "closing the connection" << endl
;
626 cout
<< "opening a new connection" << endl
;
627 parser
->Open(g_strPort
.c_str());
630 cout
<< "setting active view" << endl
;
631 parser
->SetActiveView();
633 else if (command
== "h" || command
== "help")
637 else if (command
== "q" || command
== "quit")
641 else if (command
== "log")
644 if (GetWord(input
, strLevel
))
646 int iNewLevel
= atoi(strLevel
);
647 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
649 g_cecLogLevel
= iNewLevel
;
650 cout
<< "log level changed to " << strLevel
.c_str() << endl
;
656 cout
<< "waiting for input" << endl
;
658 CCondition::Sleep(50);
661 parser
->StandbyDevices(CECDEVICE_BROADCAST
);
664 UnloadLibCec(parser
);
666 if (g_logOutput
.is_open())