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>
50 int g_cecLogLevel
= CEC_LOG_ALL
;
53 inline bool HexStrToInt(const std::string
& data
, uint8_t& value
)
56 if (sscanf(data
.c_str(), "%x", &iTmp
) == 1)
63 value
= (uint8_t) iTmp
;
71 //get the first word (separated by whitespace) from string data and place that in word
72 //then remove that word from string data
73 bool GetWord(string
& data
, string
& word
)
75 stringstream
datastream(data
);
79 if (datastream
.fail())
85 size_t pos
= data
.find(word
) + word
.length();
87 if (pos
>= data
.length())
93 data
= data
.substr(pos
);
99 if (datastream
.fail())
105 void flush_log(ICECAdapter
*cecParser
)
107 cec_log_message message
;
108 while (cecParser
&& cecParser
->GetNextLogMessage(&message
))
110 if ((message
.level
& g_cecLogLevel
) == message
.level
)
113 switch (message
.level
)
116 strLevel
= "ERROR: ";
118 case CEC_LOG_WARNING
:
119 strLevel
= "WARNING: ";
122 strLevel
= "NOTICE: ";
124 case CEC_LOG_TRAFFIC
:
125 strLevel
= "TRAFFIC: ";
128 strLevel
= "DEBUG: ";
134 CStdString strMessageTmp
;
135 strMessageTmp
.Format("[%16lld]\t%s", message
.time
, message
.message
);
136 cout
<< strLevel
.c_str() << strMessageTmp
.c_str() << endl
;
138 if (g_logOutput
.is_open())
139 g_logOutput
<< strLevel
.c_str() << strMessageTmp
.c_str() << endl
;
144 void list_devices(ICECAdapter
*parser
)
146 cec_adapter
*devices
= new cec_adapter
[10];
147 uint8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
148 if (iDevicesFound
<= 0)
150 cout
<< "Found devices: NONE" << endl
;
155 strLog
.Format("Found devices: %d", iDevicesFound
);
156 cout
<< strLog
.c_str() << endl
;
157 for (unsigned int iDevicePtr
= 0; iDevicePtr
< iDevicesFound
; iDevicePtr
++)
159 CStdString strDevice
;
160 strDevice
.Format("device: %d\npath: %s\ncom port: %s", iDevicePtr
+ 1, devices
[iDevicePtr
].path
, devices
[iDevicePtr
].comm
);
161 cout
<< endl
<< strDevice
.c_str() << endl
;
166 void show_help(const char* strExec
)
169 strExec
<< " {-h|--help|-l|--list-devices|[COM PORT]}" << endl
<<
171 "parameters:" << endl
<<
172 "\t-h --help Shows this help text" << endl
<<
173 "\t-l --list-devices List all devices on this system" << endl
<<
174 "\t-f --log-file {file} Writes all libCEC log message to a file" << endl
<<
175 "\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
<<
177 "Type 'h' or 'help' and press enter after starting the client to display all available commands" << endl
;
180 void show_console_help(void)
183 "================================================================================" << endl
<<
184 "Available commands:" << endl
<<
186 "tx {bytes} transfer bytes over the CEC line." << endl
<<
187 "txn {bytes} transfer bytes and don't wait for an ACK reply." << endl
<<
188 "[tx 40 00 FF 11 22 33] sends bytes 0x40 0x00 0xFF 0x11 0x22 0x33" << endl
<<
190 "on {address} power on the device with the given logical address." << endl
<<
191 "[on 5] power on a connected audio system" << endl
<<
193 "standby {address} put the device with the given address in standby mode." << endl
<<
194 "[standby 0] powers off the TV" << endl
<<
196 "la {logical_address} change the logical address of the CEC adapter." << endl
<<
197 "[la 4] logical address 4" << endl
<<
199 "pa {physical_address} change the physical address of the CEC adapter." << endl
<<
200 "[pa 10 00] physical address 1.0.0.0" << endl
<<
202 "osd {addr} {string} set OSD message on the specified device." << endl
<<
203 "[osd 0 Test Message] displays 'Test Message' on the TV" << endl
<<
205 "[mon] {1|0} enable or disable CEC bus monitoring." << endl
<<
206 "[log] {1 - 31} change the log level. see cectypes.h for values." << endl
<<
207 "[ping] send a ping command to the CEC adapter." << endl
<<
208 "[bl] to let the adapter enter the bootloader, to upgrade" << endl
<<
209 " the flash rom." << endl
<<
210 "[r] reconnect to the CEC adapter." << endl
<<
211 "[h] or [help] show this help." << endl
<<
212 "[q] or [quit] to quit the CEC test client and switch off all" << endl
<<
213 " connected CEC devices." << endl
<<
214 "================================================================================" << endl
;
217 int main (int argc
, char *argv
[])
219 ICECAdapter
*parser
= LoadLibCec("CECTester");
220 if (!parser
|| parser
->GetMinVersion() > CEC_TEST_CLIENT_VERSION
)
223 cout
<< "Cannot load libcec.dll" << endl
;
225 cout
<< "Cannot load libcec.so" << endl
;
230 strLog
.Format("CEC Parser created - libcec version %d", parser
->GetLibVersion());
231 cout
<< strLog
.c_str() << endl
;
233 //make stdin non-blocking
235 int flags
= fcntl(0, F_GETFL
, 0);
237 fcntl(0, F_SETFL
, flags
);
244 if (!strcmp(argv
[1], "-f"))
246 g_logOutput
.open(argv
[2]);
249 else if (!strcmp(argv
[1], "--file"))
251 g_logOutput
.open(argv
[2]);
256 if (argc
< 2 + iArgPtr
)
258 cout
<< "no serial port given. trying autodetect: ";
259 cec_adapter devices
[10];
260 uint8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
261 if (iDevicesFound
<= 0)
263 cout
<< "FAILED" << endl
;
264 UnloadLibCec(parser
);
269 cout
<< endl
<< " path: " << devices
[0].path
<< endl
<<
270 " com port: " << devices
[0].comm
<< endl
<< endl
;
271 strPort
= devices
[0].comm
;
274 else if (!strcmp(argv
[1 + iArgPtr
], "--list-devices") || !strcmp(argv
[1 + iArgPtr
], "-l"))
276 list_devices(parser
);
277 UnloadLibCec(parser
);
280 else if (!strcmp(argv
[1 + iArgPtr
], "--help") || !strcmp(argv
[1 + iArgPtr
], "-h"))
287 strPort
= argv
[1 + iArgPtr
];
291 if (!parser
->Open(strPort
.c_str()))
293 cout
<< "unable to open the device on port " << strPort
<< endl
;
295 UnloadLibCec(parser
);
299 cout
<< "cec device opened" << endl
;
301 parser
->PowerOnDevices(CECDEVICE_TV
);
304 parser
->SetActiveView();
307 bool bContinue(true);
308 cout
<< "waiting for input" << endl
;
320 if (GetWord(input
, command
))
322 if (command
== "tx" || command
== "txn")
329 while (GetWord(input
, strvalue
) && HexStrToInt(strvalue
, ivalue
))
330 bytes
.push_back(ivalue
);
332 parser
->Transmit(bytes
, command
== "tx");
334 else if (command
== "on")
338 if (GetWord(input
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
340 parser
->PowerOnDevices((cec_logical_address
) iValue
);
344 cout
<< "invalid destination" << endl
;
347 else if (command
== "standby")
351 if (GetWord(input
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
353 parser
->StandbyDevices((cec_logical_address
) iValue
);
357 cout
<< "invalid destination" << endl
;
360 else if (command
== "la")
363 if (GetWord(input
, strvalue
))
365 parser
->SetLogicalAddress((cec_logical_address
) atoi(strvalue
.c_str()));
368 else if (command
== "pa")
372 if (GetWord(input
, strB1
) && HexStrToInt(strB1
, iB1
) &&
373 GetWord(input
, strB2
) && HexStrToInt(strB2
, iB2
))
375 uint16_t iPhysicalAddress
= ((uint16_t)iB1
<< 8) + iB2
;
376 parser
->SetPhysicalAddress(iPhysicalAddress
);
379 else if (command
== "osd")
381 bool bFirstWord(false);
382 string strAddr
, strMessage
, strWord
;
384 if (GetWord(input
, strAddr
) && HexStrToInt(strAddr
, iAddr
) && iAddr
< 0xF)
386 while (GetWord(input
, strWord
))
391 strMessage
.append(" ");
393 strMessage
.append(strWord
);
395 parser
->SetOSDString((cec_logical_address
) iAddr
, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME
, strMessage
.c_str());
398 else if (command
== "ping")
400 parser
->PingAdapter();
402 else if (command
== "mon")
404 CStdString strEnable
;
405 if (GetWord(input
, strEnable
) && (strEnable
.Equals("0") || strEnable
.Equals("1")))
407 parser
->SwitchMonitoring(strEnable
.Equals("1"));
410 else if (command
== "bl")
412 parser
->StartBootloader();
414 else if (command
== "r")
416 cout
<< "closing the connection" << endl
;
420 cout
<< "opening a new connection" << endl
;
421 parser
->Open(strPort
.c_str());
424 cout
<< "setting active view" << endl
;
425 parser
->SetActiveView();
427 else if (command
== "h" || command
== "help")
431 else if (command
== "q" || command
== "quit")
435 else if (command
== "log")
438 if (GetWord(input
, strLevel
))
440 int iNewLevel
= atoi(strLevel
);
441 if (iNewLevel
>= CEC_LOG_ERROR
&& iNewLevel
<= CEC_LOG_ALL
)
443 g_cecLogLevel
= iNewLevel
;
444 cout
<< "log level changed to " << strLevel
.c_str() << endl
;
450 cout
<< "waiting for input" << endl
;
452 CCondition::Sleep(50);
455 parser
->StandbyDevices(CECDEVICE_BROADCAST
);
458 UnloadLibCec(parser
);
460 if (g_logOutput
.is_open())