cec: pass the menu request command to listeners when it's not handled
[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
d75be19b 47#define CEC_TEST_CLIENT_VERSION 1
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
d5f66c28 108void FlushLog(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
d5f66c28 152void ListDevices(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
d5f66c28 174void ShowHelpCommandLine(const char* strExec)
abbca718
LOK
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 <<
f8513317 182 " -t --type {p|r|t|a} The device type to use. More than one is possible." << endl <<
45de9d9f 183 " -p --physical {hex-address} The physical address to use for the primary logical device." << endl <<
8bc45476
LOK
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 <<
606034b6 187 " -d --log-level {level} Sets the log level. See cectypes.h for values." << endl <<
5ad0ea13
LOK
188 " -s --single-command Execute a single command and exit. Does not power" << endl <<
189 " on devices on startup and power them off on exit." << endl <<
8bc45476
LOK
190 " [COM PORT] The com port to connect to. If no COM" << endl <<
191 " port is given, the client tries to connect to the" << endl <<
192 " first device that is detected." << endl <<
825ddb96 193 endl <<
8bc45476
LOK
194 "Type 'h' or 'help' and press enter after starting the client to display all " << endl <<
195 "available commands" << endl;
825ddb96
LOK
196}
197
d5f66c28 198ICECAdapter *CreateParser(cec_device_type_list typeList)
f8513317
LOK
199{
200 ICECAdapter *parser = LibCecInit("CECTester", typeList);
201 if (!parser || parser->GetMinLibVersion() > CEC_TEST_CLIENT_VERSION)
202 {
203 #ifdef __WINDOWS__
204 cout << "Cannot load libcec.dll" << endl;
205 #else
206 cout << "Cannot load libcec.so" << endl;
207 #endif
208 return NULL;
209 }
210
211 CStdString strLog;
212 strLog.Format("CEC Parser created - libcec version %d.%d", parser->GetLibVersionMajor(), parser->GetLibVersionMinor());
213 cout << strLog.c_str() << endl;
214
215 return parser;
216}
217
d5f66c28 218void ShowHelpConsole(void)
825ddb96
LOK
219{
220 cout << endl <<
221 "================================================================================" << endl <<
222 "Available commands:" << endl <<
223 endl <<
224 "tx {bytes} transfer bytes over the CEC line." << endl <<
1f0e9e0f 225 "txn {bytes} transfer bytes but don't wait for transmission ACK." << endl <<
825ddb96
LOK
226 "[tx 40 00 FF 11 22 33] sends bytes 0x40 0x00 0xFF 0x11 0x22 0x33" << endl <<
227 endl <<
88f45c9b
LOK
228 "on {address} power on the device with the given logical address." << endl <<
229 "[on 5] power on a connected audio system" << endl <<
230 endl <<
231 "standby {address} put the device with the given address in standby mode." << endl <<
232 "[standby 0] powers off the TV" << endl <<
233 endl <<
825ddb96
LOK
234 "la {logical_address} change the logical address of the CEC adapter." << endl <<
235 "[la 4] logical address 4" << endl <<
236 endl <<
a424ebac
LOK
237 "pa {physical_address} change the physical address of the CEC adapter." << endl <<
238 "[pa 10 00] physical address 1.0.0.0" << endl <<
239 endl <<
1969b140
LOK
240 "osd {addr} {string} set OSD message on the specified device." << endl <<
241 "[osd 0 Test Message] displays 'Test Message' on the TV" << endl <<
242 endl <<
6a1c0009
LOK
243 "ver {addr} get the CEC version of the specified device." << endl <<
244 "[ver 0] get the CEC version of the TV" << endl <<
245 endl <<
44c74256
LOK
246 "ven {addr} get the vendor ID of the specified device." << endl <<
247 "[ven 0] get the vendor ID of the TV" << endl <<
248 endl <<
249 "lang {addr} get the menu language of the specified device." << endl <<
a3269a0a
LOK
250 "[lang 0] get the menu language of the TV" << endl <<
251 endl <<
e55f3f70
LOK
252 "pow {addr} get the power status of the specified device." << endl <<
253 "[pow 0] get the power status of the TV" << endl <<
254 endl <<
57f45e6c
LOK
255 "poll {addr} poll the specified device." << endl <<
256 "[poll 0] sends a poll message to the TV" << endl <<
257 endl <<
8b7e5ff6 258 "[mon] {1|0} enable or disable CEC bus monitoring." << endl <<
bdd433cb 259 "[log] {1 - 31} change the log level. see cectypes.h for values." << endl <<
825ddb96 260 "[ping] send a ping command to the CEC adapter." << endl <<
88f45c9b
LOK
261 "[bl] to let the adapter enter the bootloader, to upgrade" << endl <<
262 " the flash rom." << endl <<
263 "[r] reconnect to the CEC adapter." << endl <<
825ddb96 264 "[h] or [help] show this help." << endl <<
88f45c9b
LOK
265 "[q] or [quit] to quit the CEC test client and switch off all" << endl <<
266 " connected CEC devices." << endl <<
825ddb96 267 "================================================================================" << endl;
abbca718
LOK
268}
269
270int main (int argc, char *argv[])
271{
b1ee8e60 272 int16_t iPhysicalAddress = -1;
f8513317 273 cec_device_type_list typeList;
20b8870a 274 typeList.clear();
abbca718 275
3d0dca16 276 int iArgPtr = 1;
5ad0ea13 277 bool bSingleCommand(false);
3d0dca16 278 while (iArgPtr < argc)
e6d2161b 279 {
3d0dca16 280 if (argc >= iArgPtr + 1)
e6d2161b 281 {
3d0dca16
LOK
282 if (!strcmp(argv[iArgPtr], "-f") ||
283 !strcmp(argv[iArgPtr], "--log-file") ||
284 !strcmp(argv[iArgPtr], "-sf") ||
285 !strcmp(argv[iArgPtr], "--short-log-file"))
286 {
287 if (argc >= iArgPtr + 2)
288 {
289 g_logOutput.open(argv[iArgPtr + 1]);
290 g_bShortLog = (!strcmp(argv[iArgPtr], "-sf") || !strcmp(argv[iArgPtr], "--short-log-file"));
291 iArgPtr += 2;
292 }
293 else
294 {
606034b6
LOK
295 cout << "== skipped log-file parameter: no file given ==" << endl;
296 ++iArgPtr;
297 }
298 }
299 else if (!strcmp(argv[iArgPtr], "-d") ||
300 !strcmp(argv[iArgPtr], "--log-level"))
301 {
302 if (argc >= iArgPtr + 2)
303 {
304 int iNewLevel = atoi(argv[iArgPtr + 1]);
305 if (iNewLevel >= CEC_LOG_ERROR && iNewLevel <= CEC_LOG_ALL)
306 {
307 g_cecLogLevel = iNewLevel;
5ad0ea13
LOK
308 if (!bSingleCommand)
309 cout << "log level set to " << argv[iArgPtr + 1] << endl;
606034b6
LOK
310 }
311 else
312 {
313 cout << "== skipped log-level parameter: invalid level '" << argv[iArgPtr + 1] << "' ==" << endl;
314 }
315 iArgPtr += 2;
316 }
317 else
318 {
319 cout << "== skipped log-level parameter: no level given ==" << endl;
3d0dca16
LOK
320 ++iArgPtr;
321 }
322 }
f8513317
LOK
323 else if (!strcmp(argv[iArgPtr], "-t") ||
324 !strcmp(argv[iArgPtr], "--type"))
d6cc5f60
LOK
325 {
326 if (argc >= iArgPtr + 2)
327 {
f8513317
LOK
328 if (!strcmp(argv[iArgPtr + 1], "p"))
329 {
5ad0ea13
LOK
330 if (!bSingleCommand)
331 cout << "== using device type 'playback device'" << endl;
20b8870a 332 typeList.add(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
f8513317
LOK
333 }
334 else if (!strcmp(argv[iArgPtr + 1], "r"))
d6cc5f60 335 {
5ad0ea13
LOK
336 if (!bSingleCommand)
337 cout << "== using device type 'recording device'" << endl;
20b8870a 338 typeList.add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
f8513317
LOK
339 }
340 else if (!strcmp(argv[iArgPtr + 1], "t"))
341 {
5ad0ea13
LOK
342 if (!bSingleCommand)
343 cout << "== using device type 'tuner'" << endl;
20b8870a 344 typeList.add(CEC_DEVICE_TYPE_TUNER);
f8513317
LOK
345 }
346 else if (!strcmp(argv[iArgPtr + 1], "a"))
347 {
5ad0ea13
LOK
348 if (!bSingleCommand)
349 cout << "== using device type 'audio system'" << endl;
20b8870a 350 typeList.add(CEC_DEVICE_TYPE_AUDIO_SYSTEM);
d6cc5f60
LOK
351 }
352 else
353 {
f8513317 354 cout << "== skipped invalid device type '" << argv[iArgPtr + 1] << "'" << endl;
d6cc5f60 355 }
d6cc5f60
LOK
356 ++iArgPtr;
357 }
f8513317 358 ++iArgPtr;
d6cc5f60 359 }
3d0dca16
LOK
360 else if (!strcmp(argv[iArgPtr], "--list-devices") ||
361 !strcmp(argv[iArgPtr], "-l"))
362 {
d5f66c28 363 ICECAdapter *parser = CreateParser(typeList);
f8513317
LOK
364 if (parser)
365 {
d5f66c28 366 ListDevices(parser);
f8513317
LOK
367 UnloadLibCec(parser);
368 }
3d0dca16
LOK
369 return 0;
370 }
5ad0ea13
LOK
371 else if (!strcmp(argv[iArgPtr], "--single-command") ||
372 !strcmp(argv[iArgPtr], "-s"))
373 {
374 bSingleCommand = true;
375 ++iArgPtr;
376 }
3d0dca16
LOK
377 else if (!strcmp(argv[iArgPtr], "--help") ||
378 !strcmp(argv[iArgPtr], "-h"))
379 {
d5f66c28 380 ShowHelpCommandLine(argv[0]);
3d0dca16
LOK
381 return 0;
382 }
45de9d9f
LOK
383 else if (!strcmp(argv[iArgPtr], "-p") ||
384 !strcmp(argv[iArgPtr], "--physical"))
385 {
386 if (argc >= iArgPtr + 2)
387 {
388 if (sscanf(argv[iArgPtr + 1], "%x", &iPhysicalAddress))
389 cout << "using physical address '" << std::hex << iPhysicalAddress << "'" << endl;
390 else
391 cout << "== skipped physical address parameter: invalid physical address '" << argv[iArgPtr + 1] << "' ==" << endl;
392 ++iArgPtr;
393 }
394 ++iArgPtr;
395 }
3d0dca16
LOK
396 else
397 {
398 g_strPort = argv[iArgPtr++];
399 }
e6d2161b
LOK
400 }
401 }
402
ab1469a0 403 if (typeList.IsEmpty())
f8513317 404 {
5ad0ea13
LOK
405 if (!bSingleCommand)
406 cout << "No device type given. Using 'playback device'" << endl;
20b8870a 407 typeList.add(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
f8513317
LOK
408 }
409
410 ICECAdapter *parser = LibCecInit("CECTester", typeList);
411 if (!parser || parser->GetMinLibVersion() > CEC_TEST_CLIENT_VERSION)
412 {
413#ifdef __WINDOWS__
414 cout << "Cannot load libcec.dll" << endl;
415#else
416 cout << "Cannot load libcec.so" << endl;
417#endif
418 return 1;
419 }
f8513317 420
5ad0ea13
LOK
421 if (!bSingleCommand)
422 {
423 CStdString strLog;
424 strLog.Format("CEC Parser created - libcec version %d.%d", parser->GetLibVersionMajor(), parser->GetLibVersionMinor());
425 cout << strLog.c_str() << endl;
426
427 //make stdin non-blocking
428 #ifndef __WINDOWS__
429 int flags = fcntl(0, F_GETFL, 0);
430 flags |= O_NONBLOCK;
431 fcntl(0, F_SETFL, flags);
432 #endif
433 }
f8513317 434
3d0dca16 435 if (g_strPort.IsEmpty())
abbca718 436 {
5ad0ea13
LOK
437 if (!bSingleCommand)
438 cout << "no serial port given. trying autodetect: ";
25701fa6
LOK
439 cec_adapter devices[10];
440 uint8_t iDevicesFound = parser->FindAdapters(devices, 10, NULL);
abbca718
LOK
441 if (iDevicesFound <= 0)
442 {
5ad0ea13
LOK
443 if (bSingleCommand)
444 cout << "autodetect ";
abbca718
LOK
445 cout << "FAILED" << endl;
446 UnloadLibCec(parser);
447 return 1;
448 }
449 else
450 {
5ad0ea13
LOK
451 if (!bSingleCommand)
452 {
453 cout << endl << " path: " << devices[0].path << endl <<
454 " com port: " << devices[0].comm << endl << endl;
455 }
3d0dca16 456 g_strPort = devices[0].comm;
abbca718
LOK
457 }
458 }
e6d2161b 459
3d0dca16 460 if (!parser->Open(g_strPort.c_str()))
abbca718 461 {
3d0dca16 462 cout << "unable to open the device on port " << g_strPort << endl;
d5f66c28 463 FlushLog(parser);
abbca718
LOK
464 UnloadLibCec(parser);
465 return 1;
466 }
467
5ad0ea13
LOK
468 if (!bSingleCommand)
469 {
470 cout << "cec device opened" << endl;
abbca718 471
45de9d9f
LOK
472 if (-1 != iPhysicalAddress)
473 {
474 parser->SetPhysicalAddress(iPhysicalAddress);
475 FlushLog(parser);
476 }
477
5ad0ea13
LOK
478 parser->PowerOnDevices(CECDEVICE_TV);
479 FlushLog(parser);
abbca718 480
5ad0ea13
LOK
481 parser->SetActiveSource();
482 FlushLog(parser);
483
484 cout << "waiting for input" << endl;
485 }
abbca718
LOK
486
487 bool bContinue(true);
abbca718
LOK
488 while (bContinue)
489 {
5ad0ea13
LOK
490 if (bSingleCommand)
491 bContinue = false;
492
d5f66c28 493 FlushLog(parser);
abbca718 494
b5b53c7d
LOK
495 /* just ignore the command buffer and clear it */
496 cec_command dummy;
497 while (parser && parser->GetNextCommand(&dummy)) {}
498
abbca718
LOK
499 string input;
500 getline(cin, input);
501 cin.clear();
502
503 if (!input.empty())
504 {
505 string command;
506 if (GetWord(input, command))
507 {
faa6a23b 508 if (command == "tx" || command == "txn")
abbca718
LOK
509 {
510 string strvalue;
8bca69de 511 uint8_t ivalue;
9dee1670 512 cec_command bytes;
ab1469a0 513 bytes.Clear();
25701fa6 514
abbca718 515 while (GetWord(input, strvalue) && HexStrToInt(strvalue, ivalue))
ab1469a0 516 bytes.PushBack(ivalue);
abbca718 517
8d84e2c0 518 if (command == "txn")
1f0e9e0f 519 bytes.transmit_timeout = 0;
8d84e2c0
LOK
520
521 parser->Transmit(bytes);
abbca718 522 }
88f45c9b
LOK
523 else if (command == "on")
524 {
525 string strValue;
526 uint8_t iValue = 0;
527 if (GetWord(input, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF)
528 {
529 parser->PowerOnDevices((cec_logical_address) iValue);
530 }
531 else
532 {
533 cout << "invalid destination" << endl;
534 }
535 }
536 else if (command == "standby")
537 {
538 string strValue;
539 uint8_t iValue = 0;
540 if (GetWord(input, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF)
541 {
542 parser->StandbyDevices((cec_logical_address) iValue);
543 }
544 else
545 {
546 cout << "invalid destination" << endl;
547 }
548 }
57f45e6c
LOK
549 else if (command == "poll")
550 {
551 string strValue;
552 uint8_t iValue = 0;
553 if (GetWord(input, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF)
554 {
555 if (parser->PollDevice((cec_logical_address) iValue))
556 cout << "POLL message sent" << endl;
557 else
558 cout << "POLL message not sent" << endl;
559 }
560 else
561 {
562 cout << "invalid destination" << endl;
563 }
564 }
6dfe9213
LOK
565 else if (command == "la")
566 {
567 string strvalue;
6dfe9213
LOK
568 if (GetWord(input, strvalue))
569 {
570 parser->SetLogicalAddress((cec_logical_address) atoi(strvalue.c_str()));
abbca718
LOK
571 }
572 }
a424ebac
LOK
573 else if (command == "pa")
574 {
575 string strB1, strB2;
576 uint8_t iB1, iB2;
577 if (GetWord(input, strB1) && HexStrToInt(strB1, iB1) &&
578 GetWord(input, strB2) && HexStrToInt(strB2, iB2))
579 {
580 uint16_t iPhysicalAddress = ((uint16_t)iB1 << 8) + iB2;
581 parser->SetPhysicalAddress(iPhysicalAddress);
582 }
583 }
1969b140
LOK
584 else if (command == "osd")
585 {
586 bool bFirstWord(false);
587 string strAddr, strMessage, strWord;
588 uint8_t iAddr;
589 if (GetWord(input, strAddr) && HexStrToInt(strAddr, iAddr) && iAddr < 0xF)
590 {
591 while (GetWord(input, strWord))
592 {
593 if (bFirstWord)
594 {
595 bFirstWord = false;
596 strMessage.append(" ");
597 }
598 strMessage.append(strWord);
599 }
600 parser->SetOSDString((cec_logical_address) iAddr, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME, strMessage.c_str());
601 }
602 }
abbca718
LOK
603 else if (command == "ping")
604 {
2abe74eb 605 parser->PingAdapter();
abbca718 606 }
8b7e5ff6
LOK
607 else if (command == "mon")
608 {
609 CStdString strEnable;
610 if (GetWord(input, strEnable) && (strEnable.Equals("0") || strEnable.Equals("1")))
611 {
612 parser->SwitchMonitoring(strEnable.Equals("1"));
613 }
614 }
abbca718
LOK
615 else if (command == "bl")
616 {
617 parser->StartBootloader();
618 }
a3269a0a
LOK
619 else if (command == "lang")
620 {
621 CStdString strDev;
622 if (GetWord(input, strDev))
623 {
624 int iDev = atoi(strDev);
625 if (iDev >= 0 && iDev < 15)
626 {
627 CStdString strLog;
628 cec_menu_language language;
629 if (parser->GetDeviceMenuLanguage((cec_logical_address) iDev, &language))
630 strLog.Format("menu language '%s'", language.language);
631 else
632 strLog = "failed!";
633 cout << strLog.c_str() << endl;
634 }
635 }
636 }
44c74256
LOK
637 else if (command == "ven")
638 {
639 CStdString strDev;
640 if (GetWord(input, strDev))
641 {
642 int iDev = atoi(strDev);
643 if (iDev >= 0 && iDev < 15)
644 {
645 uint64_t iVendor = parser->GetDeviceVendorId((cec_logical_address) iDev);
646 CStdString strLog;
647 strLog.Format("vendor id: %06x", iVendor);
648 cout << strLog.c_str() << endl;
649 }
650 }
651 }
6a1c0009
LOK
652 else if (command == "ver")
653 {
654 CStdString strDev;
655 if (GetWord(input, strDev))
656 {
657 int iDev = atoi(strDev);
658 if (iDev >= 0 && iDev < 15)
659 {
660 cec_version iVersion = parser->GetDeviceCecVersion((cec_logical_address) iDev);
661 switch (iVersion)
662 {
663 case CEC_VERSION_1_2:
664 cout << "CEC version 1.2" << endl;
665 break;
666 case CEC_VERSION_1_2A:
667 cout << "CEC version 1.2a" << endl;
668 break;
669 case CEC_VERSION_1_3:
670 cout << "CEC version 1.3" << endl;
671 break;
672 case CEC_VERSION_1_3A:
673 cout << "CEC version 1.3a" << endl;
674 break;
675 default:
676 cout << "unknown CEC version" << endl;
677 break;
678 }
679 }
680 }
681 }
e55f3f70
LOK
682 else if (command == "pow")
683 {
684 CStdString strDev;
685 if (GetWord(input, strDev))
686 {
687 int iDev = atoi(strDev);
688 if (iDev >= 0 && iDev < 15)
689 {
690 cec_power_status iPower = parser->GetDevicePowerStatus((cec_logical_address) iDev);
691 switch (iPower)
692 {
693 case CEC_POWER_STATUS_ON:
694 cout << "powered on" << endl;
695 break;
696 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY:
697 cout << "on -> standby" << endl;
698 break;
699 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON:
700 cout << "standby -> on" << endl;
701 break;
702 case CEC_POWER_STATUS_STANDBY:
703 cout << "standby" << endl;
704 break;
705 default:
706 cout << "unknown power status" << endl;
707 break;
708 }
709 }
710 }
711 }
12027dbe
LOK
712 else if (command == "r")
713 {
25701fa6 714 cout << "closing the connection" << endl;
12027dbe 715 parser->Close();
d5f66c28 716 FlushLog(parser);
25701fa6
LOK
717
718 cout << "opening a new connection" << endl;
3d0dca16 719 parser->Open(g_strPort.c_str());
d5f66c28 720 FlushLog(parser);
25701fa6 721
18203d17
LOK
722 cout << "setting active source" << endl;
723 parser->SetActiveSource();
12027dbe 724 }
825ddb96
LOK
725 else if (command == "h" || command == "help")
726 {
d5f66c28 727 ShowHelpConsole();
825ddb96 728 }
abbca718
LOK
729 else if (command == "q" || command == "quit")
730 {
731 bContinue = false;
732 }
bdd433cb
LOK
733 else if (command == "log")
734 {
735 CStdString strLevel;
736 if (GetWord(input, strLevel))
737 {
738 int iNewLevel = atoi(strLevel);
739 if (iNewLevel >= CEC_LOG_ERROR && iNewLevel <= CEC_LOG_ALL)
740 {
741 g_cecLogLevel = iNewLevel;
742 cout << "log level changed to " << strLevel.c_str() << endl;
743 }
744 }
745 }
abbca718
LOK
746 }
747 if (bContinue)
748 cout << "waiting for input" << endl;
749 }
5ad0ea13
LOK
750
751 if (bContinue)
752 CCondition::Sleep(50);
abbca718
LOK
753 }
754
5ad0ea13
LOK
755 if (!bSingleCommand)
756 parser->StandbyDevices(CECDEVICE_BROADCAST);
757
5f39c4d8 758 parser->Close();
d5f66c28 759 FlushLog(parser);
abbca718 760 UnloadLibCec(parser);
e6d2161b
LOK
761
762 if (g_logOutput.is_open())
763 g_logOutput.close();
764
abbca718
LOK
765 return 0;
766}