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 --physical {hex-address} The physical address to use for the primary logical device." << 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 "pa {physical_address} change the physical address of the CEC adapter." << endl
<<
230 "osd {addr} {string} set OSD message on the specified device." << endl
<<
231 "ver {addr} get the CEC version of the specified device." << endl
<<
232 "ven {addr} get the vendor ID of the specified device." << endl
<<
233 "lang {addr} get the menu language of the specified device." << endl
<<
234 "pow {addr} get the power status of the specified device." << endl
<<
235 "poll {addr} poll the specified device." << endl
<<
236 "lad lists active devices on the bus" << endl
<<
237 "ad {addr} checks whether the specified device is active." << endl
<<
238 "at {type} checks whether the specified device type is active." << endl
<<
239 "[mon] {1|0} enable or disable CEC bus monitoring." << endl
<<
240 "[log] {1 - 31} change the log level. see cectypes.h for values." << endl
<<
241 "[ping] send a ping command to the CEC adapter." << endl
<<
242 "[bl] to let the adapter enter the bootloader, to upgrade" << endl
<<
243 " the flash rom." << endl
<<
244 "[r] reconnect to the CEC adapter." << endl
<<
245 "[h] or [help] show this help." << endl
<<
246 "[q] or [quit] to quit the CEC test client and switch off all" << endl
<<
247 " connected CEC devices." << endl
<<
248 "================================================================================" << endl
;
251 int main (int argc
, char *argv
[])
253 int16_t iPhysicalAddress
= -1;
254 cec_device_type_list typeList
;
258 bool bSingleCommand(false);
259 while (iArgPtr
< argc
)
261 if (argc
>= iArgPtr
+ 1)
263 if (!strcmp(argv
[iArgPtr
], "-f") ||
264 !strcmp(argv
[iArgPtr
], "--log-file") ||
265 !strcmp(argv
[iArgPtr
], "-sf") ||
266 !strcmp(argv
[iArgPtr
], "--short-log-file"))
268 if (argc
>= iArgPtr
+ 2)
270 g_logOutput
.open(argv
[iArgPtr
+ 1]);
271 g_bShortLog
= (!strcmp(argv
[iArgPtr
], "-sf") || !strcmp(argv
[iArgPtr
], "--short-log-file"));
276 cout
<< "== skipped log-file parameter: no file given ==" << endl
;
280 else if (!strcmp(argv
[iArgPtr
], "-d") ||
281 !strcmp(argv
[iArgPtr
], "--log-level"))
283 if (argc
>= iArgPtr
+ 2)
285 int iNewLevel
= atoi(argv
[iArgPtr
+ 1]);
286 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
288 g_cecLogLevel
= iNewLevel
;
290 cout
<< "log level set to " << argv
[iArgPtr
+ 1] << endl
;
294 cout
<< "== skipped log-level parameter: invalid level '" << argv
[iArgPtr
+ 1] << "' ==" << endl
;
300 cout
<< "== skipped log-level parameter: no level given ==" << endl
;
304 else if (!strcmp(argv
[iArgPtr
], "-t") ||
305 !strcmp(argv
[iArgPtr
], "--type"))
307 if (argc
>= iArgPtr
+ 2)
309 if (!strcmp(argv
[iArgPtr
+ 1], "p"))
312 cout
<< "== using device type 'playback device'" << endl
;
313 typeList
.add(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
315 else if (!strcmp(argv
[iArgPtr
+ 1], "r"))
318 cout
<< "== using device type 'recording device'" << endl
;
319 typeList
.add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
321 else if (!strcmp(argv
[iArgPtr
+ 1], "t"))
324 cout
<< "== using device type 'tuner'" << endl
;
325 typeList
.add(CEC_DEVICE_TYPE_TUNER
);
327 else if (!strcmp(argv
[iArgPtr
+ 1], "a"))
330 cout
<< "== using device type 'audio system'" << endl
;
331 typeList
.add(CEC_DEVICE_TYPE_AUDIO_SYSTEM
);
335 cout
<< "== skipped invalid device type '" << argv
[iArgPtr
+ 1] << "'" << endl
;
341 else if (!strcmp(argv
[iArgPtr
], "--list-devices") ||
342 !strcmp(argv
[iArgPtr
], "-l"))
344 ICECAdapter
*parser
= CreateParser(typeList
);
348 UnloadLibCec(parser
);
352 else if (!strcmp(argv
[iArgPtr
], "--single-command") ||
353 !strcmp(argv
[iArgPtr
], "-s"))
355 bSingleCommand
= true;
358 else if (!strcmp(argv
[iArgPtr
], "--help") ||
359 !strcmp(argv
[iArgPtr
], "-h"))
361 ShowHelpCommandLine(argv
[0]);
364 else if (!strcmp(argv
[iArgPtr
], "-p") ||
365 !strcmp(argv
[iArgPtr
], "--physical"))
367 if (argc
>= iArgPtr
+ 2)
369 if (sscanf(argv
[iArgPtr
+ 1], "%x", &iPhysicalAddress
))
370 cout
<< "using physical address '" << std::hex
<< iPhysicalAddress
<< "'" << endl
;
372 cout
<< "== skipped physical address parameter: invalid physical address '" << argv
[iArgPtr
+ 1] << "' ==" << endl
;
379 g_strPort
= argv
[iArgPtr
++];
384 if (typeList
.IsEmpty())
387 cout
<< "No device type given. Using 'recording device'" << endl
;
388 typeList
.add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
391 ICECAdapter
*parser
= LibCecInit("CECTester", typeList
);
392 if (!parser
|| parser
->GetMinLibVersion() > CEC_TEST_CLIENT_VERSION
)
395 cout
<< "Cannot load libcec.dll" << endl
;
397 cout
<< "Cannot load libcec.so" << endl
;
405 strLog
.Format("CEC Parser created - libcec version %d.%d", parser
->GetLibVersionMajor(), parser
->GetLibVersionMinor());
406 cout
<< strLog
.c_str() << endl
;
408 //make stdin non-blocking
410 int flags
= fcntl(0, F_GETFL
, 0);
412 fcntl(0, F_SETFL
, flags
);
416 if (g_strPort
.IsEmpty())
419 cout
<< "no serial port given. trying autodetect: ";
420 cec_adapter devices
[10];
421 uint8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
422 if (iDevicesFound
<= 0)
425 cout
<< "autodetect ";
426 cout
<< "FAILED" << endl
;
427 UnloadLibCec(parser
);
434 cout
<< endl
<< " path: " << devices
[0].path
<< endl
<<
435 " com port: " << devices
[0].comm
<< endl
<< endl
;
437 g_strPort
= devices
[0].comm
;
441 if (!parser
->Open(g_strPort
.c_str()))
443 cout
<< "unable to open the device on port " << g_strPort
<< endl
;
445 UnloadLibCec(parser
);
451 cout
<< "cec device opened" << endl
;
453 if (-1 != iPhysicalAddress
)
455 parser
->SetPhysicalAddress(iPhysicalAddress
);
459 parser
->PowerOnDevices(CECDEVICE_TV
);
462 parser
->SetActiveSource();
465 cout
<< "waiting for input" << endl
;
468 bool bContinue(true);
476 /* just ignore the command buffer and clear it */
478 while (parser
&& parser
->GetNextCommand(&dummy
)) {}
487 if (GetWord(input
, command
))
489 if (command
== "tx" || command
== "txn")
496 while (GetWord(input
, strvalue
) && HexStrToInt(strvalue
, ivalue
))
497 bytes
.PushBack(ivalue
);
499 if (command
== "txn")
500 bytes
.transmit_timeout
= 0;
502 parser
->Transmit(bytes
);
504 else if (command
== "on")
508 if (GetWord(input
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
510 parser
->PowerOnDevices((cec_logical_address
) iValue
);
514 cout
<< "invalid destination" << endl
;
517 else if (command
== "standby")
521 if (GetWord(input
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
523 parser
->StandbyDevices((cec_logical_address
) iValue
);
527 cout
<< "invalid destination" << endl
;
530 else if (command
== "poll")
534 if (GetWord(input
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
536 if (parser
->PollDevice((cec_logical_address
) iValue
))
537 cout
<< "POLL message sent" << endl
;
539 cout
<< "POLL message not sent" << endl
;
543 cout
<< "invalid destination" << endl
;
546 else if (command
== "la")
549 if (GetWord(input
, strvalue
))
551 parser
->SetLogicalAddress((cec_logical_address
) atoi(strvalue
.c_str()));
554 else if (command
== "pa")
558 if (GetWord(input
, strB1
) && HexStrToInt(strB1
, iB1
) &&
559 GetWord(input
, strB2
) && HexStrToInt(strB2
, iB2
))
561 uint16_t iPhysicalAddress
= ((uint16_t)iB1
<< 8) + iB2
;
562 parser
->SetPhysicalAddress(iPhysicalAddress
);
565 else if (command
== "osd")
567 bool bFirstWord(false);
568 string strAddr
, strMessage
, strWord
;
570 if (GetWord(input
, strAddr
) && HexStrToInt(strAddr
, iAddr
) && iAddr
< 0xF)
572 while (GetWord(input
, strWord
))
577 strMessage
.append(" ");
579 strMessage
.append(strWord
);
581 parser
->SetOSDString((cec_logical_address
) iAddr
, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME
, strMessage
.c_str());
584 else if (command
== "ping")
586 parser
->PingAdapter();
588 else if (command
== "mon")
590 CStdString strEnable
;
591 if (GetWord(input
, strEnable
) && (strEnable
.Equals("0") || strEnable
.Equals("1")))
593 parser
->SwitchMonitoring(strEnable
.Equals("1"));
596 else if (command
== "bl")
598 parser
->StartBootloader();
600 else if (command
== "lang")
603 if (GetWord(input
, strDev
))
605 int iDev
= atoi(strDev
);
606 if (iDev
>= 0 && iDev
< 15)
609 cec_menu_language language
;
610 if (parser
->GetDeviceMenuLanguage((cec_logical_address
) iDev
, &language
))
611 strLog
.Format("menu language '%s'", language
.language
);
614 cout
<< strLog
.c_str() << endl
;
618 else if (command
== "ven")
621 if (GetWord(input
, strDev
))
623 int iDev
= atoi(strDev
);
624 if (iDev
>= 0 && iDev
< 15)
626 uint64_t iVendor
= parser
->GetDeviceVendorId((cec_logical_address
) iDev
);
628 strLog
.Format("vendor id: %06x", iVendor
);
629 cout
<< strLog
.c_str() << endl
;
633 else if (command
== "ver")
636 if (GetWord(input
, strDev
))
638 int iDev
= atoi(strDev
);
639 if (iDev
>= 0 && iDev
< 15)
641 cec_version iVersion
= parser
->GetDeviceCecVersion((cec_logical_address
) iDev
);
644 case CEC_VERSION_1_2
:
645 cout
<< "CEC version 1.2" << endl
;
647 case CEC_VERSION_1_2A
:
648 cout
<< "CEC version 1.2a" << endl
;
650 case CEC_VERSION_1_3
:
651 cout
<< "CEC version 1.3" << endl
;
653 case CEC_VERSION_1_3A
:
654 cout
<< "CEC version 1.3a" << endl
;
657 cout
<< "unknown CEC version" << endl
;
663 else if (command
== "pow")
666 if (GetWord(input
, strDev
))
668 int iDev
= atoi(strDev
);
669 if (iDev
>= 0 && iDev
< 15)
671 cec_power_status iPower
= parser
->GetDevicePowerStatus((cec_logical_address
) iDev
);
674 case CEC_POWER_STATUS_ON
:
675 cout
<< "powered on" << endl
;
677 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
:
678 cout
<< "on -> standby" << endl
;
680 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
:
681 cout
<< "standby -> on" << endl
;
683 case CEC_POWER_STATUS_STANDBY
:
684 cout
<< "standby" << endl
;
687 cout
<< "unknown power status" << endl
;
693 else if (command
== "lad")
695 cout
<< "listing active devices:" << endl
;
696 cec_logical_addresses addresses
= parser
->GetActiveDevices();
697 for (unsigned iPtr
= 0; iPtr
< 16; iPtr
++)
699 cout
<< "logical address " << iPtr
<< endl
;
701 else if (command
== "ad")
704 if (GetWord(input
, strDev
))
706 int iDev
= atoi(strDev
);
707 if (iDev
>= 0 && iDev
< 15)
708 cout
<< "logical address " << iDev
<< " is " << (parser
->IsActiveDevice((cec_logical_address
)iDev
) ? "active" : "not active") << endl
;
711 else if (command
== "at")
714 if (GetWord(input
, strType
))
716 cec_device_type type
= CEC_DEVICE_TYPE_TV
;
717 if (strType
.Equals("a"))
718 type
= CEC_DEVICE_TYPE_AUDIO_SYSTEM
;
719 else if (strType
.Equals("p"))
720 type
= CEC_DEVICE_TYPE_PLAYBACK_DEVICE
;
721 else if (strType
.Equals("r"))
722 type
= CEC_DEVICE_TYPE_RECORDING_DEVICE
;
723 else if (strType
.Equals("t"))
724 type
= CEC_DEVICE_TYPE_TUNER
;
725 cout
<< "device " << type
<< " is " << (parser
->IsActiveDeviceType(type
) ? "active" : "not active") << endl
;
728 else if (command
== "r")
730 cout
<< "closing the connection" << endl
;
734 cout
<< "opening a new connection" << endl
;
735 parser
->Open(g_strPort
.c_str());
738 cout
<< "setting active source" << endl
;
739 parser
->SetActiveSource();
741 else if (command
== "h" || command
== "help")
745 else if (command
== "q" || command
== "quit")
749 else if (command
== "log")
752 if (GetWord(input
, strLevel
))
754 int iNewLevel
= atoi(strLevel
);
755 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
757 g_cecLogLevel
= iNewLevel
;
758 cout
<< "log level changed to " << strLevel
.c_str() << endl
;
764 cout
<< "waiting for input" << endl
;
768 CCondition::Sleep(50);
772 parser
->StandbyDevices(CECDEVICE_BROADCAST
);
776 UnloadLibCec(parser
);
778 if (g_logOutput
.is_open())