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;
55 inline bool HexStrToInt(const std::string
& data
, uint8_t& value
)
58 if (sscanf(data
.c_str(), "%x", &iTmp
) == 1)
65 value
= (uint8_t) iTmp
;
73 //get the first word (separated by whitespace) from string data and place that in word
74 //then remove that word from string data
75 bool GetWord(string
& data
, string
& word
)
77 stringstream
datastream(data
);
81 if (datastream
.fail())
87 size_t pos
= data
.find(word
) + word
.length();
89 if (pos
>= data
.length())
95 data
= data
.substr(pos
);
101 if (datastream
.fail())
107 void flush_log(ICECAdapter
*cecParser
)
109 cec_log_message message
;
110 while (cecParser
&& cecParser
->GetNextLogMessage(&message
))
112 if ((message
.level
& g_cecLogLevel
) == message
.level
)
115 switch (message
.level
)
118 strLevel
= "ERROR: ";
120 case CEC_LOG_WARNING
:
121 strLevel
= "WARNING: ";
124 strLevel
= "NOTICE: ";
126 case CEC_LOG_TRAFFIC
:
127 strLevel
= "TRAFFIC: ";
130 strLevel
= "DEBUG: ";
136 CStdString strFullLog
;
137 strFullLog
.Format("%s[%16lld]\t%s", strLevel
.c_str(), message
.time
, message
.message
);
138 cout
<< strFullLog
.c_str() << endl
;
140 if (g_logOutput
.is_open())
143 g_logOutput
<< message
.message
<< endl
;
145 g_logOutput
<< strFullLog
.c_str() << endl
;
151 void list_devices(ICECAdapter
*parser
)
153 cec_adapter
*devices
= new cec_adapter
[10];
154 uint8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
155 if (iDevicesFound
<= 0)
157 cout
<< "Found devices: NONE" << endl
;
162 strLog
.Format("Found devices: %d", iDevicesFound
);
163 cout
<< strLog
.c_str() << endl
;
164 for (unsigned int iDevicePtr
= 0; iDevicePtr
< iDevicesFound
; iDevicePtr
++)
166 CStdString strDevice
;
167 strDevice
.Format("device: %d\npath: %s\ncom port: %s", iDevicePtr
+ 1, devices
[iDevicePtr
].path
, devices
[iDevicePtr
].comm
);
168 cout
<< endl
<< strDevice
.c_str() << endl
;
173 void show_help(const char* strExec
)
176 strExec
<< " {-h|--help|-l|--list-devices|[COM PORT]}" << endl
<<
178 "parameters:" << endl
<<
179 " -h --help Shows this help text" << endl
<<
180 " -l --list-devices List all devices on this system" << endl
<<
181 " -f --log-file {file} Writes all libCEC log message to a file" << endl
<<
182 " -sf --short-log-file {file} Writes all libCEC log message without timestamps" << endl
<<
183 " and log levels to a file." << endl
<<
184 " [COM PORT] The com port to connect to. If no COM" << endl
<<
185 " port is given, the client tries to connect to the" << endl
<<
186 " first device that is detected." << endl
<<
188 "Type 'h' or 'help' and press enter after starting the client to display all " << endl
<<
189 "available commands" << endl
;
192 void show_console_help(void)
195 "================================================================================" << endl
<<
196 "Available commands:" << endl
<<
198 "tx {bytes} transfer bytes over the CEC line." << endl
<<
199 "txn {bytes} transfer bytes and don't wait for an ACK reply." << endl
<<
200 "[tx 40 00 FF 11 22 33] sends bytes 0x40 0x00 0xFF 0x11 0x22 0x33" << endl
<<
202 "on {address} power on the device with the given logical address." << endl
<<
203 "[on 5] power on a connected audio system" << endl
<<
205 "standby {address} put the device with the given address in standby mode." << endl
<<
206 "[standby 0] powers off the TV" << endl
<<
208 "la {logical_address} change the logical address of the CEC adapter." << endl
<<
209 "[la 4] logical address 4" << endl
<<
211 "pa {physical_address} change the physical address of the CEC adapter." << endl
<<
212 "[pa 10 00] physical address 1.0.0.0" << endl
<<
214 "osd {addr} {string} set OSD message on the specified device." << endl
<<
215 "[osd 0 Test Message] displays 'Test Message' on the TV" << endl
<<
217 "[mon] {1|0} enable or disable CEC bus monitoring." << endl
<<
218 "[log] {1 - 31} change the log level. see cectypes.h for values." << endl
<<
219 "[ping] send a ping command to the CEC adapter." << endl
<<
220 "[bl] to let the adapter enter the bootloader, to upgrade" << endl
<<
221 " the flash rom." << endl
<<
222 "[r] reconnect to the CEC adapter." << endl
<<
223 "[h] or [help] show this help." << endl
<<
224 "[q] or [quit] to quit the CEC test client and switch off all" << endl
<<
225 " connected CEC devices." << endl
<<
226 "================================================================================" << endl
;
229 int main (int argc
, char *argv
[])
231 ICECAdapter
*parser
= LoadLibCec("CECTester");
232 if (!parser
|| parser
->GetMinVersion() > CEC_TEST_CLIENT_VERSION
)
235 cout
<< "Cannot load libcec.dll" << endl
;
237 cout
<< "Cannot load libcec.so" << endl
;
242 strLog
.Format("CEC Parser created - libcec version %d", parser
->GetLibVersion());
243 cout
<< strLog
.c_str() << endl
;
245 //make stdin non-blocking
247 int flags
= fcntl(0, F_GETFL
, 0);
249 fcntl(0, F_SETFL
, flags
);
256 if (!strcmp(argv
[1], "-f") ||
257 !strcmp(argv
[1], "--log-file") ||
258 !strcmp(argv
[1], "-sf") ||
259 !strcmp(argv
[1], "--short-log-file"))
261 g_logOutput
.open(argv
[2]);
262 g_bShortLog
= (!strcmp(argv
[1], "-sf") || !strcmp(argv
[1], "--short-log-file"));
267 if (argc
< 2 + iArgPtr
)
269 cout
<< "no serial port given. trying autodetect: ";
270 cec_adapter devices
[10];
271 uint8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
272 if (iDevicesFound
<= 0)
274 cout
<< "FAILED" << endl
;
275 UnloadLibCec(parser
);
280 cout
<< endl
<< " path: " << devices
[0].path
<< endl
<<
281 " com port: " << devices
[0].comm
<< endl
<< endl
;
282 strPort
= devices
[0].comm
;
285 else if (!strcmp(argv
[1 + iArgPtr
], "--list-devices") || !strcmp(argv
[1 + iArgPtr
], "-l"))
287 list_devices(parser
);
288 UnloadLibCec(parser
);
291 else if (!strcmp(argv
[1 + iArgPtr
], "--help") || !strcmp(argv
[1 + iArgPtr
], "-h"))
298 strPort
= argv
[1 + iArgPtr
];
302 if (!parser
->Open(strPort
.c_str()))
304 cout
<< "unable to open the device on port " << strPort
<< endl
;
306 UnloadLibCec(parser
);
310 cout
<< "cec device opened" << endl
;
312 parser
->PowerOnDevices(CECDEVICE_TV
);
315 parser
->SetActiveView();
318 bool bContinue(true);
319 cout
<< "waiting for input" << endl
;
331 if (GetWord(input
, command
))
333 if (command
== "tx" || command
== "txn")
340 while (GetWord(input
, strvalue
) && HexStrToInt(strvalue
, ivalue
))
341 bytes
.push_back(ivalue
);
343 parser
->Transmit(bytes
, command
== "tx");
345 else if (command
== "on")
349 if (GetWord(input
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
351 parser
->PowerOnDevices((cec_logical_address
) iValue
);
355 cout
<< "invalid destination" << endl
;
358 else if (command
== "standby")
362 if (GetWord(input
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
364 parser
->StandbyDevices((cec_logical_address
) iValue
);
368 cout
<< "invalid destination" << endl
;
371 else if (command
== "la")
374 if (GetWord(input
, strvalue
))
376 parser
->SetLogicalAddress((cec_logical_address
) atoi(strvalue
.c_str()));
379 else if (command
== "pa")
383 if (GetWord(input
, strB1
) && HexStrToInt(strB1
, iB1
) &&
384 GetWord(input
, strB2
) && HexStrToInt(strB2
, iB2
))
386 uint16_t iPhysicalAddress
= ((uint16_t)iB1
<< 8) + iB2
;
387 parser
->SetPhysicalAddress(iPhysicalAddress
);
390 else if (command
== "osd")
392 bool bFirstWord(false);
393 string strAddr
, strMessage
, strWord
;
395 if (GetWord(input
, strAddr
) && HexStrToInt(strAddr
, iAddr
) && iAddr
< 0xF)
397 while (GetWord(input
, strWord
))
402 strMessage
.append(" ");
404 strMessage
.append(strWord
);
406 parser
->SetOSDString((cec_logical_address
) iAddr
, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME
, strMessage
.c_str());
409 else if (command
== "ping")
411 parser
->PingAdapter();
413 else if (command
== "mon")
415 CStdString strEnable
;
416 if (GetWord(input
, strEnable
) && (strEnable
.Equals("0") || strEnable
.Equals("1")))
418 parser
->SwitchMonitoring(strEnable
.Equals("1"));
421 else if (command
== "bl")
423 parser
->StartBootloader();
425 else if (command
== "r")
427 cout
<< "closing the connection" << endl
;
431 cout
<< "opening a new connection" << endl
;
432 parser
->Open(strPort
.c_str());
435 cout
<< "setting active view" << endl
;
436 parser
->SetActiveView();
438 else if (command
== "h" || command
== "help")
442 else if (command
== "q" || command
== "quit")
446 else if (command
== "log")
449 if (GetWord(input
, strLevel
))
451 int iNewLevel
= atoi(strLevel
);
452 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
454 g_cecLogLevel
= iNewLevel
;
455 cout
<< "log level changed to " << strLevel
.c_str() << endl
;
461 cout
<< "waiting for input" << endl
;
463 CCondition::Sleep(50);
466 parser
->StandbyDevices(CECDEVICE_BROADCAST
);
469 UnloadLibCec(parser
);
471 if (g_logOutput
.is_open())