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 8
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 flush_log(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 list_devices(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 show_help(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 " -f --log-file {file} Writes all libCEC log message to a file" << endl
<<
183 " -sf --short-log-file {file} Writes all libCEC log message without timestamps" << endl
<<
184 " and log levels to a file." << endl
<<
185 " -d --log-level {level} Sets the log level. See cectypes.h for values." << endl
<<
186 " [COM PORT] The com port to connect to. If no COM" << endl
<<
187 " port is given, the client tries to connect to the" << endl
<<
188 " first device that is detected." << endl
<<
190 "Type 'h' or 'help' and press enter after starting the client to display all " << endl
<<
191 "available commands" << endl
;
194 void show_console_help(void)
197 "================================================================================" << endl
<<
198 "Available commands:" << endl
<<
200 "tx {bytes} transfer bytes over the CEC line." << endl
<<
201 "txn {bytes} transfer bytes and don't wait for an ACK reply." << endl
<<
202 "[tx 40 00 FF 11 22 33] sends bytes 0x40 0x00 0xFF 0x11 0x22 0x33" << endl
<<
204 "on {address} power on the device with the given logical address." << endl
<<
205 "[on 5] power on a connected audio system" << endl
<<
207 "standby {address} put the device with the given address in standby mode." << endl
<<
208 "[standby 0] powers off the TV" << endl
<<
210 "la {logical_address} change the logical address of the CEC adapter." << endl
<<
211 "[la 4] logical address 4" << endl
<<
213 "pa {physical_address} change the physical address of the CEC adapter." << endl
<<
214 "[pa 10 00] physical address 1.0.0.0" << endl
<<
216 "osd {addr} {string} set OSD message on the specified device." << endl
<<
217 "[osd 0 Test Message] displays 'Test Message' on the TV" << endl
<<
219 "[mon] {1|0} enable or disable CEC bus monitoring." << endl
<<
220 "[log] {1 - 31} change the log level. see cectypes.h for values." << endl
<<
221 "[ping] send a ping command to the CEC adapter." << endl
<<
222 "[bl] to let the adapter enter the bootloader, to upgrade" << endl
<<
223 " the flash rom." << endl
<<
224 "[r] reconnect to the CEC adapter." << endl
<<
225 "[h] or [help] show this help." << endl
<<
226 "[q] or [quit] to quit the CEC test client and switch off all" << endl
<<
227 " connected CEC devices." << endl
<<
228 "================================================================================" << endl
;
231 int main (int argc
, char *argv
[])
233 ICECAdapter
*parser
= LoadLibCec("CECTester");
234 if (!parser
|| parser
->GetMinVersion() > CEC_TEST_CLIENT_VERSION
)
237 cout
<< "Cannot load libcec.dll" << endl
;
239 cout
<< "Cannot load libcec.so" << endl
;
244 strLog
.Format("CEC Parser created - libcec version %d", parser
->GetLibVersion());
245 cout
<< strLog
.c_str() << endl
;
247 //make stdin non-blocking
249 int flags
= fcntl(0, F_GETFL
, 0);
251 fcntl(0, F_SETFL
, flags
);
255 while (iArgPtr
< argc
)
257 if (argc
>= iArgPtr
+ 1)
259 if (!strcmp(argv
[iArgPtr
], "-f") ||
260 !strcmp(argv
[iArgPtr
], "--log-file") ||
261 !strcmp(argv
[iArgPtr
], "-sf") ||
262 !strcmp(argv
[iArgPtr
], "--short-log-file"))
264 if (argc
>= iArgPtr
+ 2)
266 g_logOutput
.open(argv
[iArgPtr
+ 1]);
267 g_bShortLog
= (!strcmp(argv
[iArgPtr
], "-sf") || !strcmp(argv
[iArgPtr
], "--short-log-file"));
272 cout
<< "== skipped log-file parameter: no file given ==" << endl
;
276 else if (!strcmp(argv
[iArgPtr
], "-d") ||
277 !strcmp(argv
[iArgPtr
], "--log-level"))
279 if (argc
>= iArgPtr
+ 2)
281 int iNewLevel
= atoi(argv
[iArgPtr
+ 1]);
282 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
284 g_cecLogLevel
= iNewLevel
;
285 cout
<< "log level set to " << argv
[iArgPtr
+ 1] << endl
;
289 cout
<< "== skipped log-level parameter: invalid level '" << argv
[iArgPtr
+ 1] << "' ==" << endl
;
295 cout
<< "== skipped log-level parameter: no level given ==" << endl
;
299 else if (!strcmp(argv
[iArgPtr
], "--list-devices") ||
300 !strcmp(argv
[iArgPtr
], "-l"))
302 list_devices(parser
);
303 UnloadLibCec(parser
);
306 else if (!strcmp(argv
[iArgPtr
], "--help") ||
307 !strcmp(argv
[iArgPtr
], "-h"))
310 UnloadLibCec(parser
);
315 g_strPort
= argv
[iArgPtr
++];
320 if (g_strPort
.IsEmpty())
322 cout
<< "no serial port given. trying autodetect: ";
323 cec_adapter devices
[10];
324 uint8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
325 if (iDevicesFound
<= 0)
327 cout
<< "FAILED" << endl
;
328 UnloadLibCec(parser
);
333 cout
<< endl
<< " path: " << devices
[0].path
<< endl
<<
334 " com port: " << devices
[0].comm
<< endl
<< endl
;
335 g_strPort
= devices
[0].comm
;
339 if (!parser
->Open(g_strPort
.c_str()))
341 cout
<< "unable to open the device on port " << g_strPort
<< endl
;
343 UnloadLibCec(parser
);
347 cout
<< "cec device opened" << endl
;
349 parser
->PowerOnDevices(CECDEVICE_TV
);
352 parser
->SetActiveView();
355 bool bContinue(true);
356 cout
<< "waiting for input" << endl
;
368 if (GetWord(input
, command
))
370 if (command
== "tx" || command
== "txn")
377 while (GetWord(input
, strvalue
) && HexStrToInt(strvalue
, ivalue
))
378 bytes
.push_back(ivalue
);
380 parser
->Transmit(bytes
, command
== "tx");
382 else if (command
== "on")
386 if (GetWord(input
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
388 parser
->PowerOnDevices((cec_logical_address
) iValue
);
392 cout
<< "invalid destination" << endl
;
395 else if (command
== "standby")
399 if (GetWord(input
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
401 parser
->StandbyDevices((cec_logical_address
) iValue
);
405 cout
<< "invalid destination" << endl
;
408 else if (command
== "la")
411 if (GetWord(input
, strvalue
))
413 parser
->SetLogicalAddress((cec_logical_address
) atoi(strvalue
.c_str()));
416 else if (command
== "pa")
420 if (GetWord(input
, strB1
) && HexStrToInt(strB1
, iB1
) &&
421 GetWord(input
, strB2
) && HexStrToInt(strB2
, iB2
))
423 uint16_t iPhysicalAddress
= ((uint16_t)iB1
<< 8) + iB2
;
424 parser
->SetPhysicalAddress(iPhysicalAddress
);
427 else if (command
== "osd")
429 bool bFirstWord(false);
430 string strAddr
, strMessage
, strWord
;
432 if (GetWord(input
, strAddr
) && HexStrToInt(strAddr
, iAddr
) && iAddr
< 0xF)
434 while (GetWord(input
, strWord
))
439 strMessage
.append(" ");
441 strMessage
.append(strWord
);
443 parser
->SetOSDString((cec_logical_address
) iAddr
, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME
, strMessage
.c_str());
446 else if (command
== "ping")
448 parser
->PingAdapter();
450 else if (command
== "mon")
452 CStdString strEnable
;
453 if (GetWord(input
, strEnable
) && (strEnable
.Equals("0") || strEnable
.Equals("1")))
455 parser
->SwitchMonitoring(strEnable
.Equals("1"));
458 else if (command
== "bl")
460 parser
->StartBootloader();
462 else if (command
== "r")
464 cout
<< "closing the connection" << endl
;
468 cout
<< "opening a new connection" << endl
;
469 parser
->Open(g_strPort
.c_str());
472 cout
<< "setting active view" << endl
;
473 parser
->SetActiveView();
475 else if (command
== "h" || command
== "help")
479 else if (command
== "q" || command
== "quit")
483 else if (command
== "log")
486 if (GetWord(input
, strLevel
))
488 int iNewLevel
= atoi(strLevel
);
489 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
491 g_cecLogLevel
= iNewLevel
;
492 cout
<< "log level changed to " << strLevel
.c_str() << endl
;
498 cout
<< "waiting for input" << endl
;
500 CCondition::Sleep(50);
503 parser
->StandbyDevices(CECDEVICE_BROADCAST
);
506 UnloadLibCec(parser
);
508 if (g_logOutput
.is_open())