cec: added -d and --log-level params to cec-client
[deb_libcec.git] / src / testclient / main.cpp
CommitLineData
abbca718
LOK
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
761dcc45 33#include <cec.h>
acec5f48 34
abbca718
LOK
35#include <cstdio>
36#include <fcntl.h>
37#include <iostream>
e6d2161b 38#include <fstream>
abbca718 39#include <string>
b9187cc6 40#include <sstream>
acec5f48
LOK
41#include "../lib/platform/threads.h"
42#include "../lib/util/StdString.h"
abbca718
LOK
43
44using namespace CEC;
45using namespace std;
46
8b7e5ff6 47#define CEC_TEST_CLIENT_VERSION 8
abbca718 48
761dcc45 49#include <cecloader.h>
8bc45476 50
3d0dca16
LOK
51int g_cecLogLevel = CEC_LOG_ALL;
52ofstream g_logOutput;
53bool g_bShortLog = false;
54CStdString g_strPort;
b9187cc6 55
8bca69de 56inline bool HexStrToInt(const std::string& data, uint8_t& value)
b9187cc6 57{
8bca69de
LOK
58 int iTmp(0);
59 if (sscanf(data.c_str(), "%x", &iTmp) == 1)
60 {
61 if (iTmp > 256)
62 value = 255;
63 else if (iTmp < 0)
64 value = 0;
65 else
66 value = (uint8_t) iTmp;
b9187cc6 67
8bca69de
LOK
68 return true;
69 }
70
71 return false;
72}
b9187cc6
LOK
73
74//get the first word (separated by whitespace) from string data and place that in word
75//then remove that word from string data
76bool GetWord(string& data, string& word)
77{
78 stringstream datastream(data);
79 string end;
80
81 datastream >> word;
82 if (datastream.fail())
83 {
84 data.clear();
85 return false;
86 }
87
88 size_t pos = data.find(word) + word.length();
89
90 if (pos >= data.length())
91 {
92 data.clear();
93 return true;
94 }
95
96 data = data.substr(pos);
97
98 datastream.clear();
99 datastream.str(data);
100
101 datastream >> end;
102 if (datastream.fail())
103 data.clear();
104
105 return true;
106}
107
2abe74eb 108void flush_log(ICECAdapter *cecParser)
abbca718
LOK
109{
110 cec_log_message message;
111 while (cecParser && cecParser->GetNextLogMessage(&message))
112 {
bdd433cb 113 if ((message.level & g_cecLogLevel) == message.level)
abbca718 114 {
e6d2161b 115 CStdString strLevel;
bdd433cb
LOK
116 switch (message.level)
117 {
118 case CEC_LOG_ERROR:
e6d2161b 119 strLevel = "ERROR: ";
bdd433cb
LOK
120 break;
121 case CEC_LOG_WARNING:
e6d2161b 122 strLevel = "WARNING: ";
bdd433cb
LOK
123 break;
124 case CEC_LOG_NOTICE:
e6d2161b 125 strLevel = "NOTICE: ";
bdd433cb
LOK
126 break;
127 case CEC_LOG_TRAFFIC:
e6d2161b 128 strLevel = "TRAFFIC: ";
bdd433cb
LOK
129 break;
130 case CEC_LOG_DEBUG:
e6d2161b 131 strLevel = "DEBUG: ";
bdd433cb
LOK
132 break;
133 default:
134 break;
135 }
40339852 136
8bc45476
LOK
137 CStdString strFullLog;
138 strFullLog.Format("%s[%16lld]\t%s", strLevel.c_str(), message.time, message.message);
139 cout << strFullLog.c_str() << endl;
e6d2161b
LOK
140
141 if (g_logOutput.is_open())
8bc45476
LOK
142 {
143 if (g_bShortLog)
144 g_logOutput << message.message << endl;
145 else
146 g_logOutput << strFullLog.c_str() << endl;
147 }
bdd433cb 148 }
abbca718
LOK
149 }
150}
151
2abe74eb 152void list_devices(ICECAdapter *parser)
abbca718 153{
25701fa6
LOK
154 cec_adapter *devices = new cec_adapter[10];
155 uint8_t iDevicesFound = parser->FindAdapters(devices, 10, NULL);
abbca718
LOK
156 if (iDevicesFound <= 0)
157 {
25701fa6 158 cout << "Found devices: NONE" << endl;
abbca718
LOK
159 }
160 else
161 {
25701fa6
LOK
162 CStdString strLog;
163 strLog.Format("Found devices: %d", iDevicesFound);
164 cout << strLog.c_str() << endl;
165 for (unsigned int iDevicePtr = 0; iDevicePtr < iDevicesFound; iDevicePtr++)
abbca718
LOK
166 {
167 CStdString strDevice;
25701fa6 168 strDevice.Format("device: %d\npath: %s\ncom port: %s", iDevicePtr + 1, devices[iDevicePtr].path, devices[iDevicePtr].comm);
abbca718
LOK
169 cout << endl << strDevice.c_str() << endl;
170 }
171 }
172}
173
174void show_help(const char* strExec)
175{
176 cout << endl <<
177 strExec << " {-h|--help|-l|--list-devices|[COM PORT]}" << endl <<
178 endl <<
179 "parameters:" << endl <<
8bc45476
LOK
180 " -h --help Shows this help text" << endl <<
181 " -l --list-devices List all devices on this system" << endl <<
182 " -f --log-file {file} Writes all libCEC log message to a file" << endl <<
183 " -sf --short-log-file {file} Writes all libCEC log message without timestamps" << endl <<
184 " and log levels to a file." << endl <<
606034b6 185 " -d --log-level {level} Sets the log level. See cectypes.h for values." << endl <<
8bc45476
LOK
186 " [COM PORT] The com port to connect to. If no COM" << endl <<
187 " port is given, the client tries to connect to the" << endl <<
188 " first device that is detected." << endl <<
825ddb96 189 endl <<
8bc45476
LOK
190 "Type 'h' or 'help' and press enter after starting the client to display all " << endl <<
191 "available commands" << endl;
825ddb96
LOK
192}
193
194void show_console_help(void)
195{
196 cout << endl <<
197 "================================================================================" << endl <<
198 "Available commands:" << endl <<
199 endl <<
200 "tx {bytes} transfer bytes over the CEC line." << endl <<
faa6a23b 201 "txn {bytes} transfer bytes and don't wait for an ACK reply." << endl <<
825ddb96
LOK
202 "[tx 40 00 FF 11 22 33] sends bytes 0x40 0x00 0xFF 0x11 0x22 0x33" << endl <<
203 endl <<
88f45c9b
LOK
204 "on {address} power on the device with the given logical address." << endl <<
205 "[on 5] power on a connected audio system" << endl <<
206 endl <<
207 "standby {address} put the device with the given address in standby mode." << endl <<
208 "[standby 0] powers off the TV" << endl <<
209 endl <<
825ddb96
LOK
210 "la {logical_address} change the logical address of the CEC adapter." << endl <<
211 "[la 4] logical address 4" << endl <<
212 endl <<
a424ebac
LOK
213 "pa {physical_address} change the physical address of the CEC adapter." << endl <<
214 "[pa 10 00] physical address 1.0.0.0" << endl <<
215 endl <<
1969b140
LOK
216 "osd {addr} {string} set OSD message on the specified device." << endl <<
217 "[osd 0 Test Message] displays 'Test Message' on the TV" << endl <<
218 endl <<
8b7e5ff6 219 "[mon] {1|0} enable or disable CEC bus monitoring." << endl <<
bdd433cb 220 "[log] {1 - 31} change the log level. see cectypes.h for values." << endl <<
825ddb96 221 "[ping] send a ping command to the CEC adapter." << endl <<
88f45c9b
LOK
222 "[bl] to let the adapter enter the bootloader, to upgrade" << endl <<
223 " the flash rom." << endl <<
224 "[r] reconnect to the CEC adapter." << endl <<
825ddb96 225 "[h] or [help] show this help." << endl <<
88f45c9b
LOK
226 "[q] or [quit] to quit the CEC test client and switch off all" << endl <<
227 " connected CEC devices." << endl <<
825ddb96 228 "================================================================================" << endl;
abbca718
LOK
229}
230
231int main (int argc, char *argv[])
232{
d54c6c91 233 ICECAdapter *parser = LoadLibCec("CECTester");
acec5f48 234 if (!parser || parser->GetMinVersion() > CEC_TEST_CLIENT_VERSION)
abbca718 235 {
acec5f48
LOK
236#ifdef __WINDOWS__
237 cout << "Cannot load libcec.dll" << endl;
238#else
239 cout << "Cannot load libcec.so" << endl;
240#endif
abbca718
LOK
241 return 1;
242 }
243 CStdString strLog;
244 strLog.Format("CEC Parser created - libcec version %d", parser->GetLibVersion());
245 cout << strLog.c_str() << endl;
246
247 //make stdin non-blocking
248#ifndef __WINDOWS__
249 int flags = fcntl(0, F_GETFL, 0);
250 flags |= O_NONBLOCK;
251 fcntl(0, F_SETFL, flags);
252#endif
253
3d0dca16
LOK
254 int iArgPtr = 1;
255 while (iArgPtr < argc)
e6d2161b 256 {
3d0dca16 257 if (argc >= iArgPtr + 1)
e6d2161b 258 {
3d0dca16
LOK
259 if (!strcmp(argv[iArgPtr], "-f") ||
260 !strcmp(argv[iArgPtr], "--log-file") ||
261 !strcmp(argv[iArgPtr], "-sf") ||
262 !strcmp(argv[iArgPtr], "--short-log-file"))
263 {
264 if (argc >= iArgPtr + 2)
265 {
266 g_logOutput.open(argv[iArgPtr + 1]);
267 g_bShortLog = (!strcmp(argv[iArgPtr], "-sf") || !strcmp(argv[iArgPtr], "--short-log-file"));
268 iArgPtr += 2;
269 }
270 else
271 {
606034b6
LOK
272 cout << "== skipped log-file parameter: no file given ==" << endl;
273 ++iArgPtr;
274 }
275 }
276 else if (!strcmp(argv[iArgPtr], "-d") ||
277 !strcmp(argv[iArgPtr], "--log-level"))
278 {
279 if (argc >= iArgPtr + 2)
280 {
281 int iNewLevel = atoi(argv[iArgPtr + 1]);
282 if (iNewLevel >= CEC_LOG_ERROR && iNewLevel <= CEC_LOG_ALL)
283 {
284 g_cecLogLevel = iNewLevel;
285 cout << "log level set to " << argv[iArgPtr + 1] << endl;
286 }
287 else
288 {
289 cout << "== skipped log-level parameter: invalid level '" << argv[iArgPtr + 1] << "' ==" << endl;
290 }
291 iArgPtr += 2;
292 }
293 else
294 {
295 cout << "== skipped log-level parameter: no level given ==" << endl;
3d0dca16
LOK
296 ++iArgPtr;
297 }
298 }
299 else if (!strcmp(argv[iArgPtr], "--list-devices") ||
300 !strcmp(argv[iArgPtr], "-l"))
301 {
302 list_devices(parser);
303 UnloadLibCec(parser);
304 return 0;
305 }
306 else if (!strcmp(argv[iArgPtr], "--help") ||
307 !strcmp(argv[iArgPtr], "-h"))
308 {
309 show_help(argv[0]);
310 UnloadLibCec(parser);
311 return 0;
312 }
313 else
314 {
315 g_strPort = argv[iArgPtr++];
316 }
e6d2161b
LOK
317 }
318 }
319
3d0dca16 320 if (g_strPort.IsEmpty())
abbca718
LOK
321 {
322 cout << "no serial port given. trying autodetect: ";
25701fa6
LOK
323 cec_adapter devices[10];
324 uint8_t iDevicesFound = parser->FindAdapters(devices, 10, NULL);
abbca718
LOK
325 if (iDevicesFound <= 0)
326 {
327 cout << "FAILED" << endl;
328 UnloadLibCec(parser);
329 return 1;
330 }
331 else
332 {
333 cout << endl << " path: " << devices[0].path << endl <<
3d0dca16
LOK
334 " com port: " << devices[0].comm << endl << endl;
335 g_strPort = devices[0].comm;
abbca718
LOK
336 }
337 }
e6d2161b 338
3d0dca16 339 if (!parser->Open(g_strPort.c_str()))
abbca718 340 {
3d0dca16 341 cout << "unable to open the device on port " << g_strPort << endl;
abbca718
LOK
342 flush_log(parser);
343 UnloadLibCec(parser);
344 return 1;
345 }
346
347 cout << "cec device opened" << endl;
abbca718 348
8829d291 349 parser->PowerOnDevices(CECDEVICE_TV);
abbca718
LOK
350 flush_log(parser);
351
352 parser->SetActiveView();
353 flush_log(parser);
354
355 bool bContinue(true);
356 cout << "waiting for input" << endl;
357 while (bContinue)
358 {
359 flush_log(parser);
360
361 string input;
362 getline(cin, input);
363 cin.clear();
364
365 if (!input.empty())
366 {
367 string command;
368 if (GetWord(input, command))
369 {
faa6a23b 370 if (command == "tx" || command == "txn")
abbca718
LOK
371 {
372 string strvalue;
8bca69de 373 uint8_t ivalue;
9dee1670 374 cec_command bytes;
25701fa6
LOK
375 bytes.clear();
376
abbca718 377 while (GetWord(input, strvalue) && HexStrToInt(strvalue, ivalue))
b2da2768 378 bytes.push_back(ivalue);
abbca718 379
faa6a23b 380 parser->Transmit(bytes, command == "tx");
abbca718 381 }
88f45c9b
LOK
382 else if (command == "on")
383 {
384 string strValue;
385 uint8_t iValue = 0;
386 if (GetWord(input, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF)
387 {
388 parser->PowerOnDevices((cec_logical_address) iValue);
389 }
390 else
391 {
392 cout << "invalid destination" << endl;
393 }
394 }
395 else if (command == "standby")
396 {
397 string strValue;
398 uint8_t iValue = 0;
399 if (GetWord(input, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF)
400 {
401 parser->StandbyDevices((cec_logical_address) iValue);
402 }
403 else
404 {
405 cout << "invalid destination" << endl;
406 }
407 }
6dfe9213
LOK
408 else if (command == "la")
409 {
410 string strvalue;
6dfe9213
LOK
411 if (GetWord(input, strvalue))
412 {
413 parser->SetLogicalAddress((cec_logical_address) atoi(strvalue.c_str()));
abbca718
LOK
414 }
415 }
a424ebac
LOK
416 else if (command == "pa")
417 {
418 string strB1, strB2;
419 uint8_t iB1, iB2;
420 if (GetWord(input, strB1) && HexStrToInt(strB1, iB1) &&
421 GetWord(input, strB2) && HexStrToInt(strB2, iB2))
422 {
423 uint16_t iPhysicalAddress = ((uint16_t)iB1 << 8) + iB2;
424 parser->SetPhysicalAddress(iPhysicalAddress);
425 }
426 }
1969b140
LOK
427 else if (command == "osd")
428 {
429 bool bFirstWord(false);
430 string strAddr, strMessage, strWord;
431 uint8_t iAddr;
432 if (GetWord(input, strAddr) && HexStrToInt(strAddr, iAddr) && iAddr < 0xF)
433 {
434 while (GetWord(input, strWord))
435 {
436 if (bFirstWord)
437 {
438 bFirstWord = false;
439 strMessage.append(" ");
440 }
441 strMessage.append(strWord);
442 }
443 parser->SetOSDString((cec_logical_address) iAddr, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME, strMessage.c_str());
444 }
445 }
abbca718
LOK
446 else if (command == "ping")
447 {
2abe74eb 448 parser->PingAdapter();
abbca718 449 }
8b7e5ff6
LOK
450 else if (command == "mon")
451 {
452 CStdString strEnable;
453 if (GetWord(input, strEnable) && (strEnable.Equals("0") || strEnable.Equals("1")))
454 {
455 parser->SwitchMonitoring(strEnable.Equals("1"));
456 }
457 }
abbca718
LOK
458 else if (command == "bl")
459 {
460 parser->StartBootloader();
461 }
12027dbe
LOK
462 else if (command == "r")
463 {
25701fa6 464 cout << "closing the connection" << endl;
12027dbe 465 parser->Close();
25701fa6
LOK
466 flush_log(parser);
467
468 cout << "opening a new connection" << endl;
3d0dca16 469 parser->Open(g_strPort.c_str());
25701fa6
LOK
470 flush_log(parser);
471
472 cout << "setting active view" << endl;
12027dbe
LOK
473 parser->SetActiveView();
474 }
825ddb96
LOK
475 else if (command == "h" || command == "help")
476 {
477 show_console_help();
478 }
abbca718
LOK
479 else if (command == "q" || command == "quit")
480 {
481 bContinue = false;
482 }
bdd433cb
LOK
483 else if (command == "log")
484 {
485 CStdString strLevel;
486 if (GetWord(input, strLevel))
487 {
488 int iNewLevel = atoi(strLevel);
489 if (iNewLevel >= CEC_LOG_ERROR && iNewLevel <= CEC_LOG_ALL)
490 {
491 g_cecLogLevel = iNewLevel;
492 cout << "log level changed to " << strLevel.c_str() << endl;
493 }
494 }
495 }
abbca718
LOK
496 }
497 if (bContinue)
498 cout << "waiting for input" << endl;
499 }
500 CCondition::Sleep(50);
501 }
502
2abe74eb 503 parser->StandbyDevices(CECDEVICE_BROADCAST);
5f39c4d8 504 parser->Close();
abbca718
LOK
505 flush_log(parser);
506 UnloadLibCec(parser);
e6d2161b
LOK
507
508 if (g_logOutput.is_open())
509 g_logOutput.close();
510
abbca718
LOK
511 return 0;
512}