cec: fix reconnect
[deb_libcec.git] / src / testclient / main.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
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.
13 *
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.
18 *
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.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
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/
31 */
32
33#include "../../include/CECExports.h"
34#include "../lib/platform/threads.h"
35#include "../lib/util/StdString.h"
36#include <cstdio>
37#include <fcntl.h>
38#include <iostream>
39#include <string>
40#include <sstream>
41
42using namespace CEC;
43using namespace std;
44
45#define CEC_TEST_CLIENT_VERSION 3
46
47
48inline bool HexStrToInt(const std::string& data, uint8_t& value)
49{
50 int iTmp(0);
51 if (sscanf(data.c_str(), "%x", &iTmp) == 1)
52 {
53 if (iTmp > 256)
54 value = 255;
55 else if (iTmp < 0)
56 value = 0;
57 else
58 value = (uint8_t) iTmp;
59
60 return true;
61 }
62
63 return false;
64}
65
66//get the first word (separated by whitespace) from string data and place that in word
67//then remove that word from string data
68bool GetWord(string& data, string& word)
69{
70 stringstream datastream(data);
71 string end;
72
73 datastream >> word;
74 if (datastream.fail())
75 {
76 data.clear();
77 return false;
78 }
79
80 size_t pos = data.find(word) + word.length();
81
82 if (pos >= data.length())
83 {
84 data.clear();
85 return true;
86 }
87
88 data = data.substr(pos);
89
90 datastream.clear();
91 datastream.str(data);
92
93 datastream >> end;
94 if (datastream.fail())
95 data.clear();
96
97 return true;
98}
99
100void flush_log(ICECAdapter *cecParser)
101{
102 cec_log_message message;
103 while (cecParser && cecParser->GetNextLogMessage(&message))
104 {
105 switch (message.level)
106 {
107 case CEC_LOG_ERROR:
108 cout << "ERROR: " << message.message.c_str() << endl;
109 break;
110 case CEC_LOG_WARNING:
111 cout << "WARNING: " << message.message.c_str() << endl;
112 break;
113 case CEC_LOG_NOTICE:
114 cout << "NOTICE: " << message.message.c_str() << endl;
115 break;
116 case CEC_LOG_DEBUG:
117 cout << "DEBUG: " << message.message.c_str() << endl;
118 break;
119 }
120 }
121}
122
123void list_devices(ICECAdapter *parser)
124{
125 cout << "Found devices: ";
126 vector<cec_adapter> devices;
127 int iDevicesFound = parser->FindAdapters(devices);
128 if (iDevicesFound <= 0)
129 {
130#ifdef __WINDOWS__
131 cout << "Not supported yet, sorry!" << endl;
132#else
133 cout << "NONE" << endl;
134#endif
135 }
136 else
137 {
138 cout << devices.size() << endl;
139 for (unsigned int iDevicePtr = 0; iDevicePtr < devices.size(); iDevicePtr++)
140 {
141 CStdString strDevice;
142 strDevice.Format("device: %d\npath: %s\ncom port: %s", iDevicePtr, devices[iDevicePtr].path.c_str(), devices[0].comm.c_str());
143 cout << endl << strDevice.c_str() << endl;
144 }
145 }
146}
147
148void show_help(const char* strExec)
149{
150 cout << endl <<
151 strExec << " {-h|--help|-l|--list-devices|[COM PORT]}" << endl <<
152 endl <<
153 "parameters:" << endl <<
154 "\t-h --help Shows this help text" << endl <<
155 "\t-l --list-devices List all devices on this system" << endl <<
156 "\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 <<
157 endl <<
158 "Type 'h' or 'help' and press enter after starting the client to display all available commands" << endl;
159}
160
161void show_console_help(void)
162{
163 cout << endl <<
164 "================================================================================" << endl <<
165 "Available commands:" << endl <<
166 endl <<
167 "tx {bytes} transfer bytes over the CEC line." << endl <<
168 "[tx 40 00 FF 11 22 33] sends bytes 0x40 0x00 0xFF 0x11 0x22 0x33" << endl <<
169 endl <<
170 "la {logical_address} change the logical address of the CEC adapter." << endl <<
171 "[la 4] logical address 4" << endl <<
172 endl <<
173 "[ping] send a ping command to the CEC adapter." << endl <<
174 "[bl] to let the adapter enter the bootloader, to upgrade the flash rom." << endl <<
175 "[h] or [help] show this help." << endl <<
176 "[q] or [quit] to quit the CEC test client and switch off all connected CEC devices." << endl <<
177 "================================================================================" << endl;
178}
179
180int main (int argc, char *argv[])
181{
182 ICECAdapter *parser = LoadLibCec("CEC Tester");
183 if (!parser && parser->GetMinVersion() > CEC_TEST_CLIENT_VERSION)
184 {
185 cout << "Unable to create parser. Is libcec.dll present?" << endl;
186 return 1;
187 }
188 CStdString strLog;
189 strLog.Format("CEC Parser created - libcec version %d", parser->GetLibVersion());
190 cout << strLog.c_str() << endl;
191
192 //make stdin non-blocking
193#ifndef __WINDOWS__
194 int flags = fcntl(0, F_GETFL, 0);
195 flags |= O_NONBLOCK;
196 fcntl(0, F_SETFL, flags);
197#endif
198
199 string strPort;
200 if (argc < 2)
201 {
202 cout << "no serial port given. trying autodetect: ";
203 vector<cec_adapter> devices;
204 int iDevicesFound = parser->FindAdapters(devices);
205 if (iDevicesFound <= 0)
206 {
207 cout << "FAILED" << endl;
208 UnloadLibCec(parser);
209 return 1;
210 }
211 else
212 {
213 cout << endl << " path: " << devices[0].path << endl <<
214 " com port: " << devices[0].comm << endl << endl;
215 strPort = devices[0].comm;
216 }
217 }
218 else if (!strcmp(argv[1], "--list-devices") || !strcmp(argv[1], "-l"))
219 {
220 list_devices(parser);
221 UnloadLibCec(parser);
222 return 0;
223 }
224 else if (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h"))
225 {
226 show_help(argv[0]);
227 return 0;
228 }
229 else
230 {
231 strPort = argv[1];
232 }
233
234 if (!parser->Open(strPort.c_str()))
235 {
236 cout << "unable to open the device on port " << strPort << endl;
237 flush_log(parser);
238 UnloadLibCec(parser);
239 return 1;
240 }
241
242 cout << "cec device opened" << endl;
243
244 parser->PowerOnDevices(CECDEVICE_TV);
245 flush_log(parser);
246
247 parser->SetActiveView();
248 flush_log(parser);
249
250 bool bContinue(true);
251 cout << "waiting for input" << endl;
252 while (bContinue)
253 {
254 flush_log(parser);
255
256 string input;
257 getline(cin, input);
258 cin.clear();
259
260 if (!input.empty())
261 {
262 string command;
263 if (GetWord(input, command))
264 {
265 if (command == "tx")
266 {
267 string strvalue;
268 uint8_t ivalue;
269 vector<uint8_t> bytes;
270 while (GetWord(input, strvalue) && HexStrToInt(strvalue, ivalue))
271 bytes.push_back(ivalue);
272
273 parser->Transmit(bytes);
274 }
275 else if (command == "la")
276 {
277 string strvalue;
278 if (GetWord(input, strvalue))
279 {
280 parser->SetLogicalAddress((cec_logical_address) atoi(strvalue.c_str()));
281 }
282 }
283 else if (command == "ping")
284 {
285 parser->PingAdapter();
286 }
287 else if (command == "bl")
288 {
289 parser->StartBootloader();
290 }
291 else if (command == "r")
292 {
293 parser->Close();
294 parser->Open(strPort.c_str());
295 parser->SetActiveView();
296 }
297 else if (command == "h" || command == "help")
298 {
299 show_console_help();
300 }
301 else if (command == "q" || command == "quit")
302 {
303 bContinue = false;
304 }
305 }
306 if (bContinue)
307 cout << "waiting for input" << endl;
308 }
309 CCondition::Sleep(50);
310 }
311
312 parser->StandbyDevices(CECDEVICE_BROADCAST);
313 parser->Close();
314 flush_log(parser);
315 UnloadLibCec(parser);
316 return 0;
317}