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/
40 #include "../lib/platform/threads.h"
41 #include "../lib/util/StdString.h"
46 #define CEC_TEST_CLIENT_VERSION 8
48 #include <cecloader.h>
49 int g_cecLogLevel
= CEC_LOG_ALL
;
51 inline bool HexStrToInt(const std::string
& data
, uint8_t& value
)
54 if (sscanf(data
.c_str(), "%x", &iTmp
) == 1)
61 value
= (uint8_t) iTmp
;
69 //get the first word (separated by whitespace) from string data and place that in word
70 //then remove that word from string data
71 bool GetWord(string
& data
, string
& word
)
73 stringstream
datastream(data
);
77 if (datastream
.fail())
83 size_t pos
= data
.find(word
) + word
.length();
85 if (pos
>= data
.length())
91 data
= data
.substr(pos
);
97 if (datastream
.fail())
103 void flush_log(ICECAdapter
*cecParser
)
105 cec_log_message message
;
106 while (cecParser
&& cecParser
->GetNextLogMessage(&message
))
108 if ((message
.level
& g_cecLogLevel
) == message
.level
)
110 switch (message
.level
)
115 case CEC_LOG_WARNING
:
121 case CEC_LOG_TRAFFIC
:
131 CStdString strMessageTmp
;
132 strMessageTmp
.Format("[%16lld]\t%s", message
.time
, message
.message
);
133 cout
<< strMessageTmp
.c_str() << endl
;
138 void list_devices(ICECAdapter
*parser
)
140 cec_adapter
*devices
= new cec_adapter
[10];
141 uint8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
142 if (iDevicesFound
<= 0)
144 cout
<< "Found devices: NONE" << endl
;
149 strLog
.Format("Found devices: %d", iDevicesFound
);
150 cout
<< strLog
.c_str() << endl
;
151 for (unsigned int iDevicePtr
= 0; iDevicePtr
< iDevicesFound
; iDevicePtr
++)
153 CStdString strDevice
;
154 strDevice
.Format("device: %d\npath: %s\ncom port: %s", iDevicePtr
+ 1, devices
[iDevicePtr
].path
, devices
[iDevicePtr
].comm
);
155 cout
<< endl
<< strDevice
.c_str() << endl
;
160 void show_help(const char* strExec
)
163 strExec
<< " {-h|--help|-l|--list-devices|[COM PORT]}" << endl
<<
165 "parameters:" << endl
<<
166 "\t-h --help Shows this help text" << endl
<<
167 "\t-l --list-devices List all devices on this system" << endl
<<
168 "\t[COM PORT] The com port to connect to. If no COM port is given, the client tries to connect to the first device that is detected" << endl
<<
170 "Type 'h' or 'help' and press enter after starting the client to display all available commands" << endl
;
173 void show_console_help(void)
176 "================================================================================" << endl
<<
177 "Available commands:" << endl
<<
179 "tx {bytes} transfer bytes over the CEC line." << endl
<<
180 "txn {bytes} transfer bytes and don't wait for an ACK reply." << endl
<<
181 "[tx 40 00 FF 11 22 33] sends bytes 0x40 0x00 0xFF 0x11 0x22 0x33" << endl
<<
183 "on {address} power on the device with the given logical address." << endl
<<
184 "[on 5] power on a connected audio system" << endl
<<
186 "standby {address} put the device with the given address in standby mode." << endl
<<
187 "[standby 0] powers off the TV" << endl
<<
189 "la {logical_address} change the logical address of the CEC adapter." << endl
<<
190 "[la 4] logical address 4" << endl
<<
192 "pa {physical_address} change the physical address of the CEC adapter." << endl
<<
193 "[pa 10 00] physical address 1.0.0.0" << endl
<<
195 "osd {addr} {string} set OSD message on the specified device." << endl
<<
196 "[osd 0 Test Message] displays 'Test Message' on the TV" << endl
<<
198 "[mon] {1|0} enable or disable CEC bus monitoring." << endl
<<
199 "[log] {1 - 31} change the log level. see cectypes.h for values." << endl
<<
200 "[ping] send a ping command to the CEC adapter." << endl
<<
201 "[bl] to let the adapter enter the bootloader, to upgrade" << endl
<<
202 " the flash rom." << endl
<<
203 "[r] reconnect to the CEC adapter." << endl
<<
204 "[h] or [help] show this help." << endl
<<
205 "[q] or [quit] to quit the CEC test client and switch off all" << endl
<<
206 " connected CEC devices." << endl
<<
207 "================================================================================" << endl
;
210 int main (int argc
, char *argv
[])
212 ICECAdapter
*parser
= LoadLibCec("CECTester");
213 if (!parser
|| parser
->GetMinVersion() > CEC_TEST_CLIENT_VERSION
)
216 cout
<< "Cannot load libcec.dll" << endl
;
218 cout
<< "Cannot load libcec.so" << endl
;
223 strLog
.Format("CEC Parser created - libcec version %d", parser
->GetLibVersion());
224 cout
<< strLog
.c_str() << endl
;
226 //make stdin non-blocking
228 int flags
= fcntl(0, F_GETFL
, 0);
230 fcntl(0, F_SETFL
, flags
);
236 cout
<< "no serial port given. trying autodetect: ";
237 cec_adapter devices
[10];
238 uint8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
239 if (iDevicesFound
<= 0)
241 cout
<< "FAILED" << endl
;
242 UnloadLibCec(parser
);
247 cout
<< endl
<< " path: " << devices
[0].path
<< endl
<<
248 " com port: " << devices
[0].comm
<< endl
<< endl
;
249 strPort
= devices
[0].comm
;
252 else if (!strcmp(argv
[1], "--list-devices") || !strcmp(argv
[1], "-l"))
254 list_devices(parser
);
255 UnloadLibCec(parser
);
258 else if (!strcmp(argv
[1], "--help") || !strcmp(argv
[1], "-h"))
268 if (!parser
->Open(strPort
.c_str()))
270 cout
<< "unable to open the device on port " << strPort
<< endl
;
272 UnloadLibCec(parser
);
276 cout
<< "cec device opened" << endl
;
278 parser
->PowerOnDevices(CECDEVICE_TV
);
281 parser
->SetActiveView();
284 bool bContinue(true);
285 cout
<< "waiting for input" << endl
;
297 if (GetWord(input
, command
))
299 if (command
== "tx" || command
== "txn")
306 while (GetWord(input
, strvalue
) && HexStrToInt(strvalue
, ivalue
))
307 bytes
.push_back(ivalue
);
309 parser
->Transmit(bytes
, command
== "tx");
311 else if (command
== "on")
315 if (GetWord(input
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
317 parser
->PowerOnDevices((cec_logical_address
) iValue
);
321 cout
<< "invalid destination" << endl
;
324 else if (command
== "standby")
328 if (GetWord(input
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
330 parser
->StandbyDevices((cec_logical_address
) iValue
);
334 cout
<< "invalid destination" << endl
;
337 else if (command
== "la")
340 if (GetWord(input
, strvalue
))
342 parser
->SetLogicalAddress((cec_logical_address
) atoi(strvalue
.c_str()));
345 else if (command
== "pa")
349 if (GetWord(input
, strB1
) && HexStrToInt(strB1
, iB1
) &&
350 GetWord(input
, strB2
) && HexStrToInt(strB2
, iB2
))
352 uint16_t iPhysicalAddress
= ((uint16_t)iB1
<< 8) + iB2
;
353 parser
->SetPhysicalAddress(iPhysicalAddress
);
356 else if (command
== "osd")
358 bool bFirstWord(false);
359 string strAddr
, strMessage
, strWord
;
361 if (GetWord(input
, strAddr
) && HexStrToInt(strAddr
, iAddr
) && iAddr
< 0xF)
363 while (GetWord(input
, strWord
))
368 strMessage
.append(" ");
370 strMessage
.append(strWord
);
372 parser
->SetOSDString((cec_logical_address
) iAddr
, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME
, strMessage
.c_str());
375 else if (command
== "ping")
377 parser
->PingAdapter();
379 else if (command
== "mon")
381 CStdString strEnable
;
382 if (GetWord(input
, strEnable
) && (strEnable
.Equals("0") || strEnable
.Equals("1")))
384 parser
->SwitchMonitoring(strEnable
.Equals("1"));
387 else if (command
== "bl")
389 parser
->StartBootloader();
391 else if (command
== "r")
393 cout
<< "closing the connection" << endl
;
397 cout
<< "opening a new connection" << endl
;
398 parser
->Open(strPort
.c_str());
401 cout
<< "setting active view" << endl
;
402 parser
->SetActiveView();
404 else if (command
== "h" || command
== "help")
408 else if (command
== "q" || command
== "quit")
412 else if (command
== "log")
415 if (GetWord(input
, strLevel
))
417 int iNewLevel
= atoi(strLevel
);
418 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
420 g_cecLogLevel
= iNewLevel
;
421 cout
<< "log level changed to " << strLevel
.c_str() << endl
;
427 cout
<< "waiting for input" << endl
;
429 CCondition::Sleep(50);
432 parser
->StandbyDevices(CECDEVICE_BROADCAST
);
435 UnloadLibCec(parser
);