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>
50 inline bool HexStrToInt(const std::string
& data
, uint8_t& value
)
53 if (sscanf(data
.c_str(), "%x", &iTmp
) == 1)
60 value
= (uint8_t) iTmp
;
68 //get the first word (separated by whitespace) from string data and place that in word
69 //then remove that word from string data
70 bool GetWord(string
& data
, string
& word
)
72 stringstream
datastream(data
);
76 if (datastream
.fail())
82 size_t pos
= data
.find(word
) + word
.length();
84 if (pos
>= data
.length())
90 data
= data
.substr(pos
);
96 if (datastream
.fail())
102 void flush_log(ICECAdapter
*cecParser
)
104 cec_log_message message
;
105 while (cecParser
&& cecParser
->GetNextLogMessage(&message
))
107 switch (message
.level
)
112 case CEC_LOG_WARNING
:
118 case CEC_LOG_TRAFFIC
:
126 CStdString strMessageTmp
;
127 strMessageTmp
.Format("[%16lld]\t%s", message
.time
, message
.message
);
128 cout
<< strMessageTmp
.c_str() << endl
;
132 void list_devices(ICECAdapter
*parser
)
134 cec_adapter
*devices
= new cec_adapter
[10];
135 uint8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
136 if (iDevicesFound
<= 0)
138 cout
<< "Found devices: NONE" << endl
;
143 strLog
.Format("Found devices: %d", iDevicesFound
);
144 cout
<< strLog
.c_str() << endl
;
145 for (unsigned int iDevicePtr
= 0; iDevicePtr
< iDevicesFound
; iDevicePtr
++)
147 CStdString strDevice
;
148 strDevice
.Format("device: %d\npath: %s\ncom port: %s", iDevicePtr
+ 1, devices
[iDevicePtr
].path
, devices
[iDevicePtr
].comm
);
149 cout
<< endl
<< strDevice
.c_str() << endl
;
154 void show_help(const char* strExec
)
157 strExec
<< " {-h|--help|-l|--list-devices|[COM PORT]}" << endl
<<
159 "parameters:" << endl
<<
160 "\t-h --help Shows this help text" << endl
<<
161 "\t-l --list-devices List all devices on this system" << endl
<<
162 "\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
<<
164 "Type 'h' or 'help' and press enter after starting the client to display all available commands" << endl
;
167 void show_console_help(void)
170 "================================================================================" << endl
<<
171 "Available commands:" << endl
<<
173 "tx {bytes} transfer bytes over the CEC line." << endl
<<
174 "txn {bytes} transfer bytes and don't wait for an ACK reply." << endl
<<
175 "[tx 40 00 FF 11 22 33] sends bytes 0x40 0x00 0xFF 0x11 0x22 0x33" << endl
<<
177 "on {address} power on the device with the given logical address." << endl
<<
178 "[on 5] power on a connected audio system" << endl
<<
180 "standby {address} put the device with the given address in standby mode." << endl
<<
181 "[standby 0] powers off the TV" << endl
<<
183 "la {logical_address} change the logical address of the CEC adapter." << endl
<<
184 "[la 4] logical address 4" << endl
<<
186 "pa {physical_address} change the physical address of the CEC adapter." << endl
<<
187 "[pa 10 00] physical address 1.0.0.0" << endl
<<
189 "osd {addr} {string} set OSD message on the specified device." << endl
<<
190 "[osd 0 Test Message] displays 'Test Message' on the TV" << endl
<<
192 "[mon] {1|0} enable or disable CEC bus monitoring." << endl
<<
193 "[ping] send a ping command to the CEC adapter." << endl
<<
194 "[bl] to let the adapter enter the bootloader, to upgrade" << endl
<<
195 " the flash rom." << endl
<<
196 "[r] reconnect to the CEC adapter." << endl
<<
197 "[h] or [help] show this help." << endl
<<
198 "[q] or [quit] to quit the CEC test client and switch off all" << endl
<<
199 " connected CEC devices." << endl
<<
200 "================================================================================" << endl
;
203 int main (int argc
, char *argv
[])
205 ICECAdapter
*parser
= LoadLibCec("CECTester");
206 if (!parser
|| parser
->GetMinVersion() > CEC_TEST_CLIENT_VERSION
)
209 cout
<< "Cannot load libcec.dll" << endl
;
211 cout
<< "Cannot load libcec.so" << endl
;
216 strLog
.Format("CEC Parser created - libcec version %d", parser
->GetLibVersion());
217 cout
<< strLog
.c_str() << endl
;
219 //make stdin non-blocking
221 int flags
= fcntl(0, F_GETFL
, 0);
223 fcntl(0, F_SETFL
, flags
);
229 cout
<< "no serial port given. trying autodetect: ";
230 cec_adapter devices
[10];
231 uint8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
232 if (iDevicesFound
<= 0)
234 cout
<< "FAILED" << endl
;
235 UnloadLibCec(parser
);
240 cout
<< endl
<< " path: " << devices
[0].path
<< endl
<<
241 " com port: " << devices
[0].comm
<< endl
<< endl
;
242 strPort
= devices
[0].comm
;
245 else if (!strcmp(argv
[1], "--list-devices") || !strcmp(argv
[1], "-l"))
247 list_devices(parser
);
248 UnloadLibCec(parser
);
251 else if (!strcmp(argv
[1], "--help") || !strcmp(argv
[1], "-h"))
261 if (!parser
->Open(strPort
.c_str()))
263 cout
<< "unable to open the device on port " << strPort
<< endl
;
265 UnloadLibCec(parser
);
269 cout
<< "cec device opened" << endl
;
271 parser
->PowerOnDevices(CECDEVICE_TV
);
274 parser
->SetActiveView();
277 bool bContinue(true);
278 cout
<< "waiting for input" << endl
;
290 if (GetWord(input
, command
))
292 if (command
== "tx" || command
== "txn")
299 while (GetWord(input
, strvalue
) && HexStrToInt(strvalue
, ivalue
))
300 bytes
.push_back(ivalue
);
302 parser
->Transmit(bytes
, command
== "tx");
304 else if (command
== "on")
308 if (GetWord(input
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
310 parser
->PowerOnDevices((cec_logical_address
) iValue
);
314 cout
<< "invalid destination" << endl
;
317 else if (command
== "standby")
321 if (GetWord(input
, strValue
) && HexStrToInt(strValue
, iValue
) && iValue
<= 0xF)
323 parser
->StandbyDevices((cec_logical_address
) iValue
);
327 cout
<< "invalid destination" << endl
;
330 else if (command
== "la")
333 if (GetWord(input
, strvalue
))
335 parser
->SetLogicalAddress((cec_logical_address
) atoi(strvalue
.c_str()));
338 else if (command
== "pa")
342 if (GetWord(input
, strB1
) && HexStrToInt(strB1
, iB1
) &&
343 GetWord(input
, strB2
) && HexStrToInt(strB2
, iB2
))
345 uint16_t iPhysicalAddress
= ((uint16_t)iB1
<< 8) + iB2
;
346 parser
->SetPhysicalAddress(iPhysicalAddress
);
349 else if (command
== "osd")
351 bool bFirstWord(false);
352 string strAddr
, strMessage
, strWord
;
354 if (GetWord(input
, strAddr
) && HexStrToInt(strAddr
, iAddr
) && iAddr
< 0xF)
356 while (GetWord(input
, strWord
))
361 strMessage
.append(" ");
363 strMessage
.append(strWord
);
365 parser
->SetOSDString((cec_logical_address
) iAddr
, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME
, strMessage
.c_str());
368 else if (command
== "ping")
370 parser
->PingAdapter();
372 else if (command
== "mon")
374 CStdString strEnable
;
375 if (GetWord(input
, strEnable
) && (strEnable
.Equals("0") || strEnable
.Equals("1")))
377 parser
->SwitchMonitoring(strEnable
.Equals("1"));
380 else if (command
== "bl")
382 parser
->StartBootloader();
384 else if (command
== "r")
386 cout
<< "closing the connection" << endl
;
390 cout
<< "opening a new connection" << endl
;
391 parser
->Open(strPort
.c_str());
394 cout
<< "setting active view" << endl
;
395 parser
->SetActiveView();
397 else if (command
== "h" || command
== "help")
401 else if (command
== "q" || command
== "quit")
407 cout
<< "waiting for input" << endl
;
409 CCondition::Sleep(50);
412 parser
->StandbyDevices(CECDEVICE_BROADCAST
);
415 UnloadLibCec(parser
);