cec: fixed - c+p'd copyright contained #pragma once
[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 <cec.h>
34
35#include <cstdio>
36#include <fcntl.h>
37#include <iostream>
38#include <string>
39#include <sstream>
40#include "../lib/platform/threads.h"
41#include "../lib/util/StdString.h"
42
43using namespace CEC;
44using namespace std;
45
46#define CEC_TEST_CLIENT_VERSION 8
47
48#include <cecloader.h>
49
50inline bool HexStrToInt(const std::string& data, uint8_t& value)
51{
52 int iTmp(0);
53 if (sscanf(data.c_str(), "%x", &iTmp) == 1)
54 {
55 if (iTmp > 256)
56 value = 255;
57 else if (iTmp < 0)
58 value = 0;
59 else
60 value = (uint8_t) iTmp;
61
62 return true;
63 }
64
65 return false;
66}
67
68//get the first word (separated by whitespace) from string data and place that in word
69//then remove that word from string data
70bool GetWord(string& data, string& word)
71{
72 stringstream datastream(data);
73 string end;
74
75 datastream >> word;
76 if (datastream.fail())
77 {
78 data.clear();
79 return false;
80 }
81
82 size_t pos = data.find(word) + word.length();
83
84 if (pos >= data.length())
85 {
86 data.clear();
87 return true;
88 }
89
90 data = data.substr(pos);
91
92 datastream.clear();
93 datastream.str(data);
94
95 datastream >> end;
96 if (datastream.fail())
97 data.clear();
98
99 return true;
100}
101
102void flush_log(ICECAdapter *cecParser)
103{
104 cec_log_message message;
105 while (cecParser && cecParser->GetNextLogMessage(&message))
106 {
107 switch (message.level)
108 {
109 case CEC_LOG_ERROR:
110 cout << "ERROR: ";
111 break;
112 case CEC_LOG_WARNING:
113 cout << "WARNING: ";
114 break;
115 case CEC_LOG_NOTICE:
116 cout << "NOTICE: ";
117 break;
118 case CEC_LOG_DEBUG:
119 cout << "DEBUG: ";
120 break;
121 }
122
123 CStdString strMessageTmp;
124 strMessageTmp.Format("[%16lld]\t%s", message.time, message.message);
125 cout << strMessageTmp.c_str() << endl;
126 }
127}
128
129void list_devices(ICECAdapter *parser)
130{
131 cec_adapter *devices = new cec_adapter[10];
132 uint8_t iDevicesFound = parser->FindAdapters(devices, 10, NULL);
133 if (iDevicesFound <= 0)
134 {
135 cout << "Found devices: NONE" << endl;
136 }
137 else
138 {
139 CStdString strLog;
140 strLog.Format("Found devices: %d", iDevicesFound);
141 cout << strLog.c_str() << endl;
142 for (unsigned int iDevicePtr = 0; iDevicePtr < iDevicesFound; iDevicePtr++)
143 {
144 CStdString strDevice;
145 strDevice.Format("device: %d\npath: %s\ncom port: %s", iDevicePtr + 1, devices[iDevicePtr].path, devices[iDevicePtr].comm);
146 cout << endl << strDevice.c_str() << endl;
147 }
148 }
149}
150
151void show_help(const char* strExec)
152{
153 cout << endl <<
154 strExec << " {-h|--help|-l|--list-devices|[COM PORT]}" << endl <<
155 endl <<
156 "parameters:" << endl <<
157 "\t-h --help Shows this help text" << endl <<
158 "\t-l --list-devices List all devices on this system" << endl <<
159 "\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 <<
160 endl <<
161 "Type 'h' or 'help' and press enter after starting the client to display all available commands" << endl;
162}
163
164void show_console_help(void)
165{
166 cout << endl <<
167 "================================================================================" << endl <<
168 "Available commands:" << endl <<
169 endl <<
170 "tx {bytes} transfer bytes over the CEC line." << endl <<
171 "txn {bytes} transfer bytes and don't wait for an ACK reply." << endl <<
172 "[tx 40 00 FF 11 22 33] sends bytes 0x40 0x00 0xFF 0x11 0x22 0x33" << endl <<
173 endl <<
174 "on {address} power on the device with the given logical address." << endl <<
175 "[on 5] power on a connected audio system" << endl <<
176 endl <<
177 "standby {address} put the device with the given address in standby mode." << endl <<
178 "[standby 0] powers off the TV" << endl <<
179 endl <<
180 "la {logical_address} change the logical address of the CEC adapter." << endl <<
181 "[la 4] logical address 4" << endl <<
182 endl <<
183 "pa {physical_address} change the physical address of the CEC adapter." << endl <<
184 "[pa 10 00] physical address 1.0.0.0" << endl <<
185 endl <<
186 "osd {addr} {string} set OSD message on the specified device." << endl <<
187 "[osd 0 Test Message] displays 'Test Message' on the TV" << endl <<
188 endl <<
189 "[mon] {1|0} enable or disable CEC bus monitoring." << endl <<
190 "[ping] send a ping command to the CEC adapter." << endl <<
191 "[bl] to let the adapter enter the bootloader, to upgrade" << endl <<
192 " the flash rom." << endl <<
193 "[r] reconnect to the CEC adapter." << endl <<
194 "[h] or [help] show this help." << endl <<
195 "[q] or [quit] to quit the CEC test client and switch off all" << endl <<
196 " connected CEC devices." << endl <<
197 "================================================================================" << endl;
198}
199
200int main (int argc, char *argv[])
201{
202 ICECAdapter *parser = LoadLibCec("CECTester");
203 if (!parser || parser->GetMinVersion() > CEC_TEST_CLIENT_VERSION)
204 {
205#ifdef __WINDOWS__
206 cout << "Cannot load libcec.dll" << endl;
207#else
208 cout << "Cannot load libcec.so" << endl;
209#endif
210 return 1;
211 }
212 CStdString strLog;
213 strLog.Format("CEC Parser created - libcec version %d", parser->GetLibVersion());
214 cout << strLog.c_str() << endl;
215
216 //make stdin non-blocking
217#ifndef __WINDOWS__
218 int flags = fcntl(0, F_GETFL, 0);
219 flags |= O_NONBLOCK;
220 fcntl(0, F_SETFL, flags);
221#endif
222
223 string strPort;
224 if (argc < 2)
225 {
226 cout << "no serial port given. trying autodetect: ";
227 cec_adapter devices[10];
228 uint8_t iDevicesFound = parser->FindAdapters(devices, 10, NULL);
229 if (iDevicesFound <= 0)
230 {
231 cout << "FAILED" << endl;
232 UnloadLibCec(parser);
233 return 1;
234 }
235 else
236 {
237 cout << endl << " path: " << devices[0].path << endl <<
238 " com port: " << devices[0].comm << endl << endl;
239 strPort = devices[0].comm;
240 }
241 }
242 else if (!strcmp(argv[1], "--list-devices") || !strcmp(argv[1], "-l"))
243 {
244 list_devices(parser);
245 UnloadLibCec(parser);
246 return 0;
247 }
248 else if (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h"))
249 {
250 show_help(argv[0]);
251 return 0;
252 }
253 else
254 {
255 strPort = argv[1];
256 }
257
258 if (!parser->Open(strPort.c_str()))
259 {
260 cout << "unable to open the device on port " << strPort << endl;
261 flush_log(parser);
262 UnloadLibCec(parser);
263 return 1;
264 }
265
266 cout << "cec device opened" << endl;
267
268 parser->PowerOnDevices(CECDEVICE_TV);
269 flush_log(parser);
270
271 parser->SetActiveView();
272 flush_log(parser);
273
274 bool bContinue(true);
275 cout << "waiting for input" << endl;
276 while (bContinue)
277 {
278 flush_log(parser);
279
280 string input;
281 getline(cin, input);
282 cin.clear();
283
284 if (!input.empty())
285 {
286 string command;
287 if (GetWord(input, command))
288 {
289 if (command == "tx" || command == "txn")
290 {
291 string strvalue;
292 uint8_t ivalue;
293 cec_command bytes;
294 bytes.clear();
295
296 while (GetWord(input, strvalue) && HexStrToInt(strvalue, ivalue))
297 bytes.push_back(ivalue);
298
299 parser->Transmit(bytes, command == "tx");
300 }
301 else if (command == "on")
302 {
303 string strValue;
304 uint8_t iValue = 0;
305 if (GetWord(input, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF)
306 {
307 parser->PowerOnDevices((cec_logical_address) iValue);
308 }
309 else
310 {
311 cout << "invalid destination" << endl;
312 }
313 }
314 else if (command == "standby")
315 {
316 string strValue;
317 uint8_t iValue = 0;
318 if (GetWord(input, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF)
319 {
320 parser->StandbyDevices((cec_logical_address) iValue);
321 }
322 else
323 {
324 cout << "invalid destination" << endl;
325 }
326 }
327 else if (command == "la")
328 {
329 string strvalue;
330 if (GetWord(input, strvalue))
331 {
332 parser->SetLogicalAddress((cec_logical_address) atoi(strvalue.c_str()));
333 }
334 }
335 else if (command == "pa")
336 {
337 string strB1, strB2;
338 uint8_t iB1, iB2;
339 if (GetWord(input, strB1) && HexStrToInt(strB1, iB1) &&
340 GetWord(input, strB2) && HexStrToInt(strB2, iB2))
341 {
342 uint16_t iPhysicalAddress = ((uint16_t)iB1 << 8) + iB2;
343 parser->SetPhysicalAddress(iPhysicalAddress);
344 }
345 }
346 else if (command == "osd")
347 {
348 bool bFirstWord(false);
349 string strAddr, strMessage, strWord;
350 uint8_t iAddr;
351 if (GetWord(input, strAddr) && HexStrToInt(strAddr, iAddr) && iAddr < 0xF)
352 {
353 while (GetWord(input, strWord))
354 {
355 if (bFirstWord)
356 {
357 bFirstWord = false;
358 strMessage.append(" ");
359 }
360 strMessage.append(strWord);
361 }
362 parser->SetOSDString((cec_logical_address) iAddr, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME, strMessage.c_str());
363 }
364 }
365 else if (command == "ping")
366 {
367 parser->PingAdapter();
368 }
369 else if (command == "mon")
370 {
371 CStdString strEnable;
372 if (GetWord(input, strEnable) && (strEnable.Equals("0") || strEnable.Equals("1")))
373 {
374 parser->SwitchMonitoring(strEnable.Equals("1"));
375 }
376 }
377 else if (command == "bl")
378 {
379 parser->StartBootloader();
380 }
381 else if (command == "r")
382 {
383 cout << "closing the connection" << endl;
384 parser->Close();
385 flush_log(parser);
386
387 cout << "opening a new connection" << endl;
388 parser->Open(strPort.c_str());
389 flush_log(parser);
390
391 cout << "setting active view" << endl;
392 parser->SetActiveView();
393 }
394 else if (command == "h" || command == "help")
395 {
396 show_console_help();
397 }
398 else if (command == "q" || command == "quit")
399 {
400 bContinue = false;
401 }
402 }
403 if (bContinue)
404 cout << "waiting for input" << endl;
405 }
406 CCondition::Sleep(50);
407 }
408
409 parser->StandbyDevices(CECDEVICE_BROADCAST);
410 parser->Close();
411 flush_log(parser);
412 UnloadLibCec(parser);
413 return 0;
414}