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/
33 #include "../../include/CECExports.h"
34 #include "../lib/platform/threads.h"
35 #include "../lib/util/StdString.h"
45 #define CEC_TEST_CLIENT_VERSION 5
48 inline bool HexStrToInt(const std::string
& data
, uint8_t& value
)
51 if (sscanf(data
.c_str(), "%x", &iTmp
) == 1)
58 value
= (uint8_t) iTmp
;
66 //get the first word (separated by whitespace) from string data and place that in word
67 //then remove that word from string data
68 bool GetWord(string
& data
, string
& word
)
70 stringstream
datastream(data
);
74 if (datastream
.fail())
80 size_t pos
= data
.find(word
) + word
.length();
82 if (pos
>= data
.length())
88 data
= data
.substr(pos
);
94 if (datastream
.fail())
100 void flush_log(ICECAdapter
*cecParser
)
102 cec_log_message message
;
103 while (cecParser
&& cecParser
->GetNextLogMessage(&message
))
105 switch (message
.level
)
108 cout
<< "ERROR: " << message
.message
<< endl
;
110 case CEC_LOG_WARNING
:
111 cout
<< "WARNING: " << message
.message
<< endl
;
114 cout
<< "NOTICE: " << message
.message
<< endl
;
117 cout
<< "DEBUG: " << message
.message
<< endl
;
123 void list_devices(ICECAdapter
*parser
)
125 cec_adapter
*devices
= new cec_adapter
[10];
126 uint8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
127 if (iDevicesFound
<= 0)
129 cout
<< "Found devices: NONE" << endl
;
134 strLog
.Format("Found devices: %d", iDevicesFound
);
135 cout
<< strLog
.c_str() << endl
;
136 for (unsigned int iDevicePtr
= 0; iDevicePtr
< iDevicesFound
; iDevicePtr
++)
138 CStdString strDevice
;
139 strDevice
.Format("device: %d\npath: %s\ncom port: %s", iDevicePtr
+ 1, devices
[iDevicePtr
].path
, devices
[iDevicePtr
].comm
);
140 cout
<< endl
<< strDevice
.c_str() << endl
;
145 void show_help(const char* strExec
)
148 strExec
<< " {-h|--help|-l|--list-devices|[COM PORT]}" << endl
<<
150 "parameters:" << endl
<<
151 "\t-h --help Shows this help text" << endl
<<
152 "\t-l --list-devices List all devices on this system" << endl
<<
153 "\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
<<
155 "Type 'h' or 'help' and press enter after starting the client to display all available commands" << endl
;
158 void show_console_help(void)
161 "================================================================================" << endl
<<
162 "Available commands:" << endl
<<
164 "tx {bytes} transfer bytes over the CEC line." << endl
<<
165 "[tx 40 00 FF 11 22 33] sends bytes 0x40 0x00 0xFF 0x11 0x22 0x33" << endl
<<
167 "la {logical_address} change the logical address of the CEC adapter." << endl
<<
168 "[la 4] logical address 4" << endl
<<
170 "[ping] send a ping command to the CEC adapter." << endl
<<
171 "[bl] to let the adapter enter the bootloader, to upgrade the flash rom." << endl
<<
172 "[h] or [help] show this help." << endl
<<
173 "[q] or [quit] to quit the CEC test client and switch off all connected CEC devices." << endl
<<
174 "================================================================================" << endl
;
177 int main (int argc
, char *argv
[])
179 ICECAdapter
*parser
= LoadLibCec("CECTester");
180 if (!parser
&& parser
->GetMinVersion() > CEC_TEST_CLIENT_VERSION
)
182 cout
<< "Unable to create parser. Is libcec.dll present?" << endl
;
186 strLog
.Format("CEC Parser created - libcec version %d", parser
->GetLibVersion());
187 cout
<< strLog
.c_str() << endl
;
189 //make stdin non-blocking
191 int flags
= fcntl(0, F_GETFL
, 0);
193 fcntl(0, F_SETFL
, flags
);
199 cout
<< "no serial port given. trying autodetect: ";
200 cec_adapter devices
[10];
201 uint8_t iDevicesFound
= parser
->FindAdapters(devices
, 10, NULL
);
202 if (iDevicesFound
<= 0)
204 cout
<< "FAILED" << endl
;
205 UnloadLibCec(parser
);
210 cout
<< endl
<< " path: " << devices
[0].path
<< endl
<<
211 " com port: " << devices
[0].comm
<< endl
<< endl
;
212 strPort
= devices
[0].comm
;
215 else if (!strcmp(argv
[1], "--list-devices") || !strcmp(argv
[1], "-l"))
217 list_devices(parser
);
218 UnloadLibCec(parser
);
221 else if (!strcmp(argv
[1], "--help") || !strcmp(argv
[1], "-h"))
231 if (!parser
->Open(strPort
.c_str()))
233 cout
<< "unable to open the device on port " << strPort
<< endl
;
235 UnloadLibCec(parser
);
239 cout
<< "cec device opened" << endl
;
241 parser
->PowerOnDevices(CECDEVICE_TV
);
244 parser
->SetActiveView();
247 bool bContinue(true);
248 cout
<< "waiting for input" << endl
;
260 if (GetWord(input
, command
))
269 while (GetWord(input
, strvalue
) && HexStrToInt(strvalue
, ivalue
))
270 bytes
.push_back(ivalue
);
272 parser
->Transmit(bytes
);
274 else if (command
== "la")
277 if (GetWord(input
, strvalue
))
279 parser
->SetLogicalAddress((cec_logical_address
) atoi(strvalue
.c_str()));
282 else if (command
== "ping")
284 parser
->PingAdapter();
286 else if (command
== "bl")
288 parser
->StartBootloader();
290 else if (command
== "r")
292 cout
<< "closing the connection" << endl
;
296 cout
<< "opening a new connection" << endl
;
297 parser
->Open(strPort
.c_str());
300 cout
<< "setting active view" << endl
;
301 parser
->SetActiveView();
303 else if (command
== "h" || command
== "help")
307 else if (command
== "q" || command
== "quit")
313 cout
<< "waiting for input" << endl
;
315 CCondition::Sleep(50);
318 parser
->StandbyDevices(CECDEVICE_BROADCAST
);
321 UnloadLibCec(parser
);