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