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 " -f --log-file {file} Writes all libCEC log message to a file" << endl
<<
184 " -sf --short-log-file {file} Writes all libCEC log message without timestamps" << endl
<<
185 " and log levels to a file." << endl
<<
186 " -d --log-level {level} Sets the log level. See cectypes.h for values." << endl
<<
187 " -s --single-command Execute a single command and exit. Does not power" << endl
<<
188 " on devices on startup and power them off on exit." << endl
<<
189 " [COM PORT] The com port to connect to. If no COM" << endl
<<
190 " port is given, the client tries to connect to the" << endl
<<
191 " first device that is detected." << endl
<<
193 "Type 'h' or 'help' and press enter after starting the client to display all " << endl
<<
194 "available commands" << endl
;
197 ICECAdapter
*CreateParser(cec_device_type_list typeList
)
199 ICECAdapter
*parser
= LibCecInit("CECTester", typeList
);
200 if (!parser
|| parser
->GetMinLibVersion() > CEC_TEST_CLIENT_VERSION
)
203 cout
<< "Cannot load libcec.dll" << endl
;
205 cout
<< "Cannot load libcec.so" << endl
;
211 strLog
.Format("CEC Parser created - libcec version %d.%d", parser
->GetLibVersionMajor(), parser
->GetLibVersionMinor());
212 cout
<< strLog
.c_str() << endl
;
217 void ShowHelpConsole(void)
220 "================================================================================" << endl
<<
221 "Available commands:" << endl
<<
223 "tx {bytes} transfer bytes over the CEC line." << endl
<<
224 "txn {bytes} transfer bytes but don't wait for transmission ACK." << endl
<<
225 "[tx 40 00 FF 11 22 33] sends bytes 0x40 0x00 0xFF 0x11 0x22 0x33" << endl
<<
227 "on {address} power on the device with the given logical address." << endl
<<
228 "[on 5] power on a connected audio system" << endl
<<
230 "standby {address} put the device with the given address in standby mode." << endl
<<
231 "[standby 0] powers off the TV" << endl
<<
233 "la {logical_address} change the logical address of the CEC adapter." << endl
<<
234 "[la 4] logical address 4" << endl
<<
236 "pa {physical_address} change the physical address of the CEC adapter." << endl
<<
237 "[pa 10 00] physical address 1.0.0.0" << endl
<<
239 "osd {addr} {string} set OSD message on the specified device." << endl
<<
240 "[osd 0 Test Message] displays 'Test Message' on the TV" << endl
<<
242 "ver {addr} get the CEC version of the specified device." << endl
<<
243 "[ver 0] get the CEC version of the TV" << endl
<<
245 "ven {addr} get the vendor ID of the specified device." << endl
<<
246 "[ven 0] get the vendor ID of the TV" << endl
<<
248 "lang {addr} get the menu language of the specified device." << endl
<<
249 "[lang 0] get the menu language of the TV" << endl
<<
251 "pow {addr} get the power status of the specified device." << endl
<<
252 "[pow 0] get the power status of the TV" << endl
<<
254 "poll {addr} poll the specified device." << endl
<<
255 "[poll 0] sends a poll message to the TV" << endl
<<
257 "[mon] {1|0} enable or disable CEC bus monitoring." << endl
<<
258 "[log] {1 - 31} change the log level. see cectypes.h for values." << endl
<<
259 "[ping] send a ping command to the CEC adapter." << endl
<<
260 "[bl] to let the adapter enter the bootloader, to upgrade" << endl
<<
261 " the flash rom." << endl
<<
262 "[r] reconnect to the CEC adapter." << endl
<<
263 "[h] or [help] show this help." << endl
<<
264 "[q] or [quit] to quit the CEC test client and switch off all" << endl
<<
265 " connected CEC devices." << endl
<<
266 "================================================================================" << endl
;
269 int main (int argc
, char *argv
[])
271 cec_device_type_list typeList
;
275 bool bSingleCommand(false);
276 while (iArgPtr
< argc
)
278 if (argc
>= iArgPtr
+ 1)
280 if (!strcmp(argv
[iArgPtr
], "-f") ||
281 !strcmp(argv
[iArgPtr
], "--log-file") ||
282 !strcmp(argv
[iArgPtr
], "-sf") ||
283 !strcmp(argv
[iArgPtr
], "--short-log-file"))
285 if (argc
>= iArgPtr
+ 2)
287 g_logOutput
.open(argv
[iArgPtr
+ 1]);
288 g_bShortLog
= (!strcmp(argv
[iArgPtr
], "-sf") || !strcmp(argv
[iArgPtr
], "--short-log-file"));
293 cout
<< "== skipped log-file parameter: no file given ==" << endl
;
297 else if (!strcmp(argv
[iArgPtr
], "-d") ||
298 !strcmp(argv
[iArgPtr
], "--log-level"))
300 if (argc
>= iArgPtr
+ 2)
302 int iNewLevel
= atoi(argv
[iArgPtr
+ 1]);
303 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
305 g_cecLogLevel
= iNewLevel
;
307 cout
<< "log level set to " << argv
[iArgPtr
+ 1] << endl
;
311 cout
<< "== skipped log-level parameter: invalid level '" << argv
[iArgPtr
+ 1] << "' ==" << endl
;
317 cout
<< "== skipped log-level parameter: no level given ==" << endl
;
321 else if (!strcmp(argv
[iArgPtr
], "-t") ||
322 !strcmp(argv
[iArgPtr
], "--type"))
324 if (argc
>= iArgPtr
+ 2)
326 if (!strcmp(argv
[iArgPtr
+ 1], "p"))
329 cout
<< "== using device type 'playback device'" << endl
;
330 typeList
.Add(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
332 else if (!strcmp(argv
[iArgPtr
+ 1], "r"))
335 cout
<< "== using device type 'recording device'" << endl
;
336 typeList
.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
338 else if (!strcmp(argv
[iArgPtr
+ 1], "t"))
341 cout
<< "== using device type 'tuner'" << endl
;
342 typeList
.Add(CEC_DEVICE_TYPE_TUNER
);
344 else if (!strcmp(argv
[iArgPtr
+ 1], "a"))
347 cout
<< "== using device type 'audio system'" << endl
;
348 typeList
.Add(CEC_DEVICE_TYPE_AUDIO_SYSTEM
);
352 cout
<< "== skipped invalid device type '" << argv
[iArgPtr
+ 1] << "'" << endl
;
358 else if (!strcmp(argv
[iArgPtr
], "--list-devices") ||
359 !strcmp(argv
[iArgPtr
], "-l"))
361 ICECAdapter
*parser
= CreateParser(typeList
);
365 UnloadLibCec(parser
);
369 else if (!strcmp(argv
[iArgPtr
], "--single-command") ||
370 !strcmp(argv
[iArgPtr
], "-s"))
372 bSingleCommand
= true;
375 else if (!strcmp(argv
[iArgPtr
], "--help") ||
376 !strcmp(argv
[iArgPtr
], "-h"))
378 ShowHelpCommandLine(argv
[0]);
383 g_strPort
= argv
[iArgPtr
++];
388 if (typeList
.IsEmpty())
391 cout
<< "No device type given. Using 'playback device'" << endl
;
392 typeList
.Add(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
395 ICECAdapter
*parser
= LibCecInit("CECTester", typeList
);
396 if (!parser
|| parser
->GetMinLibVersion() > CEC_TEST_CLIENT_VERSION
)
399 cout
<< "Cannot load libcec.dll" << endl
;
401 cout
<< "Cannot load libcec.so" << endl
;
409 strLog
.Format("CEC Parser created - libcec version %d.%d", parser
->GetLibVersionMajor(), parser
->GetLibVersionMinor());
410 cout
<< strLog
.c_str() << endl
;
412 //make stdin non-blocking
414 int flags
= fcntl(0, F_GETFL
, 0);
416 fcntl(0, F_SETFL
, flags
);
420 if (g_strPort
.IsEmpty())
423 cout
<< "no serial port given. trying autodetect: ";
424 cec_adapter devices
[10];
425 uint8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
426 if (iDevicesFound
<= 0)
429 cout
<< "autodetect ";
430 cout
<< "FAILED" << endl
;
431 UnloadLibCec(parser
);
438 cout
<< endl
<< " path: " << devices
[0].path
<< endl
<<
439 " com port: " << devices
[0].comm
<< endl
<< endl
;
441 g_strPort
= devices
[0].comm
;
445 if (!parser
->Open(g_strPort
.c_str()))
447 cout
<< "unable to open the device on port " << g_strPort
<< endl
;
449 UnloadLibCec(parser
);
455 cout
<< "cec device opened" << endl
;
457 parser
->PowerOnDevices(CECDEVICE_TV
);
460 parser
->SetActiveSource();
463 cout
<< "waiting for input" << endl
;
466 bool bContinue(true);
474 /* just ignore the command buffer and clear it */
476 while (parser
&& parser
->GetNextCommand(&dummy
)) {}
485 if (GetWord(input
, command
))
487 if (command
== "tx" || command
== "txn")
494 while (GetWord(input
, strvalue
) && HexStrToInt(strvalue
, ivalue
))
495 bytes
.PushBack(ivalue
);
497 if (command
== "txn")
498 bytes
.transmit_timeout
= 0;
500 parser
->Transmit(bytes
);
502 else if (command
== "on")
506 if (GetWord(input
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
508 parser
->PowerOnDevices((cec_logical_address
) iValue
);
512 cout
<< "invalid destination" << endl
;
515 else if (command
== "standby")
519 if (GetWord(input
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
521 parser
->StandbyDevices((cec_logical_address
) iValue
);
525 cout
<< "invalid destination" << endl
;
528 else if (command
== "poll")
532 if (GetWord(input
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
534 if (parser
->PollDevice((cec_logical_address
) iValue
))
535 cout
<< "POLL message sent" << endl
;
537 cout
<< "POLL message not sent" << endl
;
541 cout
<< "invalid destination" << endl
;
544 else if (command
== "la")
547 if (GetWord(input
, strvalue
))
549 parser
->SetLogicalAddress((cec_logical_address
) atoi(strvalue
.c_str()));
552 else if (command
== "pa")
556 if (GetWord(input
, strB1
) && HexStrToInt(strB1
, iB1
) &&
557 GetWord(input
, strB2
) && HexStrToInt(strB2
, iB2
))
559 uint16_t iPhysicalAddress
= ((uint16_t)iB1
<< 8) + iB2
;
560 parser
->SetPhysicalAddress(iPhysicalAddress
);
563 else if (command
== "osd")
565 bool bFirstWord(false);
566 string strAddr
, strMessage
, strWord
;
568 if (GetWord(input
, strAddr
) && HexStrToInt(strAddr
, iAddr
) && iAddr
< 0xF)
570 while (GetWord(input
, strWord
))
575 strMessage
.append(" ");
577 strMessage
.append(strWord
);
579 parser
->SetOSDString((cec_logical_address
) iAddr
, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME
, strMessage
.c_str());
582 else if (command
== "ping")
584 parser
->PingAdapter();
586 else if (command
== "mon")
588 CStdString strEnable
;
589 if (GetWord(input
, strEnable
) && (strEnable
.Equals("0") || strEnable
.Equals("1")))
591 parser
->SwitchMonitoring(strEnable
.Equals("1"));
594 else if (command
== "bl")
596 parser
->StartBootloader();
598 else if (command
== "lang")
601 if (GetWord(input
, strDev
))
603 int iDev
= atoi(strDev
);
604 if (iDev
>= 0 && iDev
< 15)
607 cec_menu_language language
;
608 if (parser
->GetDeviceMenuLanguage((cec_logical_address
) iDev
, &language
))
609 strLog
.Format("menu language '%s'", language
.language
);
612 cout
<< strLog
.c_str() << endl
;
616 else if (command
== "ven")
619 if (GetWord(input
, strDev
))
621 int iDev
= atoi(strDev
);
622 if (iDev
>= 0 && iDev
< 15)
624 uint64_t iVendor
= parser
->GetDeviceVendorId((cec_logical_address
) iDev
);
626 strLog
.Format("vendor id: %06x", iVendor
);
627 cout
<< strLog
.c_str() << endl
;
631 else if (command
== "ver")
634 if (GetWord(input
, strDev
))
636 int iDev
= atoi(strDev
);
637 if (iDev
>= 0 && iDev
< 15)
639 cec_version iVersion
= parser
->GetDeviceCecVersion((cec_logical_address
) iDev
);
642 case CEC_VERSION_1_2
:
643 cout
<< "CEC version 1.2" << endl
;
645 case CEC_VERSION_1_2A
:
646 cout
<< "CEC version 1.2a" << endl
;
648 case CEC_VERSION_1_3
:
649 cout
<< "CEC version 1.3" << endl
;
651 case CEC_VERSION_1_3A
:
652 cout
<< "CEC version 1.3a" << endl
;
655 cout
<< "unknown CEC version" << endl
;
661 else if (command
== "pow")
664 if (GetWord(input
, strDev
))
666 int iDev
= atoi(strDev
);
667 if (iDev
>= 0 && iDev
< 15)
669 cec_power_status iPower
= parser
->GetDevicePowerStatus((cec_logical_address
) iDev
);
672 case CEC_POWER_STATUS_ON
:
673 cout
<< "powered on" << endl
;
675 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
:
676 cout
<< "on -> standby" << endl
;
678 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
:
679 cout
<< "standby -> on" << endl
;
681 case CEC_POWER_STATUS_STANDBY
:
682 cout
<< "standby" << endl
;
685 cout
<< "unknown power status" << endl
;
691 else if (command
== "r")
693 cout
<< "closing the connection" << endl
;
697 cout
<< "opening a new connection" << endl
;
698 parser
->Open(g_strPort
.c_str());
701 cout
<< "setting active source" << endl
;
702 parser
->SetActiveSource();
704 else if (command
== "h" || command
== "help")
708 else if (command
== "q" || command
== "quit")
712 else if (command
== "log")
715 if (GetWord(input
, strLevel
))
717 int iNewLevel
= atoi(strLevel
);
718 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
720 g_cecLogLevel
= iNewLevel
;
721 cout
<< "log level changed to " << strLevel
.c_str() << endl
;
727 cout
<< "waiting for input" << endl
;
731 CCondition::Sleep(50);
735 parser
->StandbyDevices(CECDEVICE_BROADCAST
);
739 UnloadLibCec(parser
);
741 if (g_logOutput
.is_open())