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