38f6b5bd8eaaba8832890680da9e27d7325a827f
[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 "ven {addr} get the vendor ID of the specified device." << endl <<
225 "[ven 0] get the vendor ID of the TV" << endl <<
226 endl <<
227 "lang {addr} get the menu language of the specified device." << endl <<
228 "[lang 0] get the menu language of the TV" << endl <<
229 endl <<
230 "[mon] {1|0} enable or disable CEC bus monitoring." << endl <<
231 "[log] {1 - 31} change the log level. see cectypes.h for values." << endl <<
232 "[ping] send a ping command to the CEC adapter." << endl <<
233 "[bl] to let the adapter enter the bootloader, to upgrade" << endl <<
234 " the flash rom." << endl <<
235 "[r] reconnect to the CEC adapter." << endl <<
236 "[h] or [help] show this help." << endl <<
237 "[q] or [quit] to quit the CEC test client and switch off all" << endl <<
238 " connected CEC devices." << endl <<
239 "================================================================================" << endl;
240 }
241
242 int main (int argc, char *argv[])
243 {
244 ICECAdapter *parser = LoadLibCec("CECTester");
245 if (!parser || parser->GetMinVersion() > CEC_TEST_CLIENT_VERSION)
246 {
247 #ifdef __WINDOWS__
248 cout << "Cannot load libcec.dll" << endl;
249 #else
250 cout << "Cannot load libcec.so" << endl;
251 #endif
252 return 1;
253 }
254 CStdString strLog;
255 strLog.Format("CEC Parser created - libcec version %d", parser->GetLibVersion());
256 cout << strLog.c_str() << endl;
257
258 //make stdin non-blocking
259 #ifndef __WINDOWS__
260 int flags = fcntl(0, F_GETFL, 0);
261 flags |= O_NONBLOCK;
262 fcntl(0, F_SETFL, flags);
263 #endif
264
265 int iArgPtr = 1;
266 while (iArgPtr < argc)
267 {
268 if (argc >= iArgPtr + 1)
269 {
270 if (!strcmp(argv[iArgPtr], "-f") ||
271 !strcmp(argv[iArgPtr], "--log-file") ||
272 !strcmp(argv[iArgPtr], "-sf") ||
273 !strcmp(argv[iArgPtr], "--short-log-file"))
274 {
275 if (argc >= iArgPtr + 2)
276 {
277 g_logOutput.open(argv[iArgPtr + 1]);
278 g_bShortLog = (!strcmp(argv[iArgPtr], "-sf") || !strcmp(argv[iArgPtr], "--short-log-file"));
279 iArgPtr += 2;
280 }
281 else
282 {
283 cout << "== skipped log-file parameter: no file given ==" << endl;
284 ++iArgPtr;
285 }
286 }
287 else if (!strcmp(argv[iArgPtr], "-d") ||
288 !strcmp(argv[iArgPtr], "--log-level"))
289 {
290 if (argc >= iArgPtr + 2)
291 {
292 int iNewLevel = atoi(argv[iArgPtr + 1]);
293 if (iNewLevel >= CEC_LOG_ERROR && iNewLevel <= CEC_LOG_ALL)
294 {
295 g_cecLogLevel = iNewLevel;
296 cout << "log level set to " << argv[iArgPtr + 1] << endl;
297 }
298 else
299 {
300 cout << "== skipped log-level parameter: invalid level '" << argv[iArgPtr + 1] << "' ==" << endl;
301 }
302 iArgPtr += 2;
303 }
304 else
305 {
306 cout << "== skipped log-level parameter: no level given ==" << endl;
307 ++iArgPtr;
308 }
309 }
310 else if (!strcmp(argv[iArgPtr], "-la") ||
311 !strcmp(argv[iArgPtr], "--logical-address"))
312 {
313 if (argc >= iArgPtr + 2)
314 {
315 int iNewAddress = atoi(argv[iArgPtr + 1]);
316 if (iNewAddress >= 0 && iNewAddress <= 15)
317 {
318 g_iLogicalAddress = iNewAddress;
319 cout << "logical address set to " << argv[iArgPtr + 1] << endl;
320 }
321 else
322 {
323 cout << "== skipped logical-address parameter: invalid address '" << argv[iArgPtr + 1] << "' ==" << endl;
324 }
325 iArgPtr += 2;
326 }
327 else
328 {
329 cout << "== skipped logical-address parameter: no address given ==" << endl;
330 ++iArgPtr;
331 }
332 }
333 else if (!strcmp(argv[iArgPtr], "--list-devices") ||
334 !strcmp(argv[iArgPtr], "-l"))
335 {
336 list_devices(parser);
337 UnloadLibCec(parser);
338 return 0;
339 }
340 else if (!strcmp(argv[iArgPtr], "--help") ||
341 !strcmp(argv[iArgPtr], "-h"))
342 {
343 show_help(argv[0]);
344 UnloadLibCec(parser);
345 return 0;
346 }
347 else
348 {
349 g_strPort = argv[iArgPtr++];
350 }
351 }
352 }
353
354 if (g_strPort.IsEmpty())
355 {
356 cout << "no serial port given. trying autodetect: ";
357 cec_adapter devices[10];
358 uint8_t iDevicesFound = parser->FindAdapters(devices, 10, NULL);
359 if (iDevicesFound <= 0)
360 {
361 cout << "FAILED" << endl;
362 UnloadLibCec(parser);
363 return 1;
364 }
365 else
366 {
367 cout << endl << " path: " << devices[0].path << endl <<
368 " com port: " << devices[0].comm << endl << endl;
369 g_strPort = devices[0].comm;
370 }
371 }
372
373 parser->SetLogicalAddress((cec_logical_address) g_iLogicalAddress);
374
375 if (!parser->Open(g_strPort.c_str()))
376 {
377 cout << "unable to open the device on port " << g_strPort << endl;
378 flush_log(parser);
379 UnloadLibCec(parser);
380 return 1;
381 }
382
383 cout << "cec device opened" << endl;
384
385 parser->PowerOnDevices(CECDEVICE_TV);
386 flush_log(parser);
387
388 parser->SetActiveView();
389 flush_log(parser);
390
391 bool bContinue(true);
392 cout << "waiting for input" << endl;
393 while (bContinue)
394 {
395 flush_log(parser);
396
397 /* just ignore the command buffer and clear it */
398 cec_command dummy;
399 while (parser && parser->GetNextCommand(&dummy)) {}
400
401 string input;
402 getline(cin, input);
403 cin.clear();
404
405 if (!input.empty())
406 {
407 string command;
408 if (GetWord(input, command))
409 {
410 if (command == "tx" || command == "txn")
411 {
412 string strvalue;
413 uint8_t ivalue;
414 cec_command bytes;
415 bytes.clear();
416
417 while (GetWord(input, strvalue) && HexStrToInt(strvalue, ivalue))
418 bytes.push_back(ivalue);
419
420 if (command == "txn")
421 bytes.ack_timeout = 0;
422
423 parser->Transmit(bytes);
424 }
425 else if (command == "on")
426 {
427 string strValue;
428 uint8_t iValue = 0;
429 if (GetWord(input, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF)
430 {
431 parser->PowerOnDevices((cec_logical_address) iValue);
432 }
433 else
434 {
435 cout << "invalid destination" << endl;
436 }
437 }
438 else if (command == "standby")
439 {
440 string strValue;
441 uint8_t iValue = 0;
442 if (GetWord(input, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF)
443 {
444 parser->StandbyDevices((cec_logical_address) iValue);
445 }
446 else
447 {
448 cout << "invalid destination" << endl;
449 }
450 }
451 else if (command == "la")
452 {
453 string strvalue;
454 if (GetWord(input, strvalue))
455 {
456 parser->SetLogicalAddress((cec_logical_address) atoi(strvalue.c_str()));
457 }
458 }
459 else if (command == "pa")
460 {
461 string strB1, strB2;
462 uint8_t iB1, iB2;
463 if (GetWord(input, strB1) && HexStrToInt(strB1, iB1) &&
464 GetWord(input, strB2) && HexStrToInt(strB2, iB2))
465 {
466 uint16_t iPhysicalAddress = ((uint16_t)iB1 << 8) + iB2;
467 parser->SetPhysicalAddress(iPhysicalAddress);
468 }
469 }
470 else if (command == "osd")
471 {
472 bool bFirstWord(false);
473 string strAddr, strMessage, strWord;
474 uint8_t iAddr;
475 if (GetWord(input, strAddr) && HexStrToInt(strAddr, iAddr) && iAddr < 0xF)
476 {
477 while (GetWord(input, strWord))
478 {
479 if (bFirstWord)
480 {
481 bFirstWord = false;
482 strMessage.append(" ");
483 }
484 strMessage.append(strWord);
485 }
486 parser->SetOSDString((cec_logical_address) iAddr, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME, strMessage.c_str());
487 }
488 }
489 else if (command == "ping")
490 {
491 parser->PingAdapter();
492 }
493 else if (command == "mon")
494 {
495 CStdString strEnable;
496 if (GetWord(input, strEnable) && (strEnable.Equals("0") || strEnable.Equals("1")))
497 {
498 parser->SwitchMonitoring(strEnable.Equals("1"));
499 }
500 }
501 else if (command == "bl")
502 {
503 parser->StartBootloader();
504 }
505 else if (command == "lang")
506 {
507 CStdString strDev;
508 if (GetWord(input, strDev))
509 {
510 int iDev = atoi(strDev);
511 if (iDev >= 0 && iDev < 15)
512 {
513 CStdString strLog;
514 cec_menu_language language;
515 if (parser->GetDeviceMenuLanguage((cec_logical_address) iDev, &language))
516 strLog.Format("menu language '%s'", language.language);
517 else
518 strLog = "failed!";
519 cout << strLog.c_str() << endl;
520 }
521 }
522 }
523 else if (command == "ven")
524 {
525 CStdString strDev;
526 if (GetWord(input, strDev))
527 {
528 int iDev = atoi(strDev);
529 if (iDev >= 0 && iDev < 15)
530 {
531 uint64_t iVendor = parser->GetDeviceVendorId((cec_logical_address) iDev);
532 CStdString strLog;
533 strLog.Format("vendor id: %06x", iVendor);
534 cout << strLog.c_str() << endl;
535 }
536 }
537 }
538 else if (command == "ver")
539 {
540 CStdString strDev;
541 if (GetWord(input, strDev))
542 {
543 int iDev = atoi(strDev);
544 if (iDev >= 0 && iDev < 15)
545 {
546 cec_version iVersion = parser->GetDeviceCecVersion((cec_logical_address) iDev);
547 switch (iVersion)
548 {
549 case CEC_VERSION_1_2:
550 cout << "CEC version 1.2" << endl;
551 break;
552 case CEC_VERSION_1_2A:
553 cout << "CEC version 1.2a" << endl;
554 break;
555 case CEC_VERSION_1_3:
556 cout << "CEC version 1.3" << endl;
557 break;
558 case CEC_VERSION_1_3A:
559 cout << "CEC version 1.3a" << endl;
560 break;
561 default:
562 cout << "unknown CEC version" << endl;
563 break;
564 }
565 }
566 }
567 }
568 else if (command == "r")
569 {
570 cout << "closing the connection" << endl;
571 parser->Close();
572 flush_log(parser);
573
574 cout << "opening a new connection" << endl;
575 parser->Open(g_strPort.c_str());
576 flush_log(parser);
577
578 cout << "setting active view" << endl;
579 parser->SetActiveView();
580 }
581 else if (command == "h" || command == "help")
582 {
583 show_console_help();
584 }
585 else if (command == "q" || command == "quit")
586 {
587 bContinue = false;
588 }
589 else if (command == "log")
590 {
591 CStdString strLevel;
592 if (GetWord(input, strLevel))
593 {
594 int iNewLevel = atoi(strLevel);
595 if (iNewLevel >= CEC_LOG_ERROR && iNewLevel <= CEC_LOG_ALL)
596 {
597 g_cecLogLevel = iNewLevel;
598 cout << "log level changed to " << strLevel.c_str() << endl;
599 }
600 }
601 }
602 }
603 if (bContinue)
604 cout << "waiting for input" << endl;
605 }
606 CCondition::Sleep(50);
607 }
608
609 parser->StandbyDevices(CECDEVICE_BROADCAST);
610 parser->Close();
611 flush_log(parser);
612 UnloadLibCec(parser);
613
614 if (g_logOutput.is_open())
615 g_logOutput.close();
616
617 return 0;
618 }