cec: added -b/--base to cec-client's parameters, so a base device can be set when...
[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"
03ae897d 43#include "../lib/implementations/CECCommandHandler.h"
abbca718
LOK
44
45using namespace CEC;
46using namespace std;
47
d75be19b 48#define CEC_TEST_CLIENT_VERSION 1
abbca718 49
761dcc45 50#include <cecloader.h>
8bc45476 51
8354f747
LOK
52int g_cecLogLevel(CEC_LOG_ALL);
53ofstream g_logOutput;
54bool g_bShortLog(false);
55CStdString g_strPort;
aa52c2e0
LOK
56uint8_t g_iHDMIPort(CEC_DEFAULT_HDMI_PORT);
57cec_logical_address g_iBaseDevice((cec_logical_address)CEC_DEFAULT_BASE_DEVICE);
8354f747
LOK
58cec_device_type_list g_typeList;
59bool g_bSingleCommand(false);
60
b9187cc6 61
8bca69de 62inline bool HexStrToInt(const std::string& data, uint8_t& value)
b9187cc6 63{
8bca69de
LOK
64 int iTmp(0);
65 if (sscanf(data.c_str(), "%x", &iTmp) == 1)
66 {
67 if (iTmp > 256)
68 value = 255;
69 else if (iTmp < 0)
70 value = 0;
71 else
72 value = (uint8_t) iTmp;
b9187cc6 73
8bca69de
LOK
74 return true;
75 }
76
77 return false;
78}
b9187cc6
LOK
79
80//get the first word (separated by whitespace) from string data and place that in word
81//then remove that word from string data
82bool GetWord(string& data, string& word)
83{
84 stringstream datastream(data);
85 string end;
86
87 datastream >> word;
88 if (datastream.fail())
89 {
90 data.clear();
91 return false;
92 }
93
94 size_t pos = data.find(word) + word.length();
95
96 if (pos >= data.length())
97 {
98 data.clear();
99 return true;
100 }
101
102 data = data.substr(pos);
103
104 datastream.clear();
105 datastream.str(data);
106
107 datastream >> end;
108 if (datastream.fail())
109 data.clear();
110
111 return true;
112}
113
d5f66c28 114void FlushLog(ICECAdapter *cecParser)
abbca718
LOK
115{
116 cec_log_message message;
117 while (cecParser && cecParser->GetNextLogMessage(&message))
118 {
bdd433cb 119 if ((message.level & g_cecLogLevel) == message.level)
abbca718 120 {
e6d2161b 121 CStdString strLevel;
bdd433cb
LOK
122 switch (message.level)
123 {
124 case CEC_LOG_ERROR:
e6d2161b 125 strLevel = "ERROR: ";
bdd433cb
LOK
126 break;
127 case CEC_LOG_WARNING:
e6d2161b 128 strLevel = "WARNING: ";
bdd433cb
LOK
129 break;
130 case CEC_LOG_NOTICE:
e6d2161b 131 strLevel = "NOTICE: ";
bdd433cb
LOK
132 break;
133 case CEC_LOG_TRAFFIC:
e6d2161b 134 strLevel = "TRAFFIC: ";
bdd433cb
LOK
135 break;
136 case CEC_LOG_DEBUG:
e6d2161b 137 strLevel = "DEBUG: ";
bdd433cb
LOK
138 break;
139 default:
140 break;
141 }
40339852 142
8bc45476
LOK
143 CStdString strFullLog;
144 strFullLog.Format("%s[%16lld]\t%s", strLevel.c_str(), message.time, message.message);
145 cout << strFullLog.c_str() << endl;
e6d2161b
LOK
146
147 if (g_logOutput.is_open())
8bc45476
LOK
148 {
149 if (g_bShortLog)
150 g_logOutput << message.message << endl;
151 else
152 g_logOutput << strFullLog.c_str() << endl;
153 }
bdd433cb 154 }
abbca718
LOK
155 }
156}
157
d5f66c28 158void ListDevices(ICECAdapter *parser)
abbca718 159{
25701fa6
LOK
160 cec_adapter *devices = new cec_adapter[10];
161 uint8_t iDevicesFound = parser->FindAdapters(devices, 10, NULL);
abbca718
LOK
162 if (iDevicesFound <= 0)
163 {
25701fa6 164 cout << "Found devices: NONE" << endl;
abbca718
LOK
165 }
166 else
167 {
25701fa6
LOK
168 CStdString strLog;
169 strLog.Format("Found devices: %d", iDevicesFound);
170 cout << strLog.c_str() << endl;
171 for (unsigned int iDevicePtr = 0; iDevicePtr < iDevicesFound; iDevicePtr++)
abbca718
LOK
172 {
173 CStdString strDevice;
25701fa6 174 strDevice.Format("device: %d\npath: %s\ncom port: %s", iDevicePtr + 1, devices[iDevicePtr].path, devices[iDevicePtr].comm);
abbca718
LOK
175 cout << endl << strDevice.c_str() << endl;
176 }
177 }
178}
179
d5f66c28 180void ShowHelpCommandLine(const char* strExec)
abbca718
LOK
181{
182 cout << endl <<
183 strExec << " {-h|--help|-l|--list-devices|[COM PORT]}" << endl <<
184 endl <<
185 "parameters:" << endl <<
8bc45476
LOK
186 " -h --help Shows this help text" << endl <<
187 " -l --list-devices List all devices on this system" << endl <<
f8513317 188 " -t --type {p|r|t|a} The device type to use. More than one is possible." << endl <<
16b1e052 189 " -p --port {int} The HDMI port to use as active source." << endl <<
aa52c2e0
LOK
190 " -b --base {int} The logical address of the device to with this " << endl <<
191 " adapter is connected." << endl <<
8bc45476
LOK
192 " -f --log-file {file} Writes all libCEC log message to a file" << endl <<
193 " -sf --short-log-file {file} Writes all libCEC log message without timestamps" << endl <<
194 " and log levels to a file." << endl <<
606034b6 195 " -d --log-level {level} Sets the log level. See cectypes.h for values." << endl <<
5ad0ea13
LOK
196 " -s --single-command Execute a single command and exit. Does not power" << endl <<
197 " on devices on startup and power them off on exit." << endl <<
8bc45476
LOK
198 " [COM PORT] The com port to connect to. If no COM" << endl <<
199 " port is given, the client tries to connect to the" << endl <<
200 " first device that is detected." << endl <<
825ddb96 201 endl <<
8bc45476
LOK
202 "Type 'h' or 'help' and press enter after starting the client to display all " << endl <<
203 "available commands" << endl;
825ddb96
LOK
204}
205
d5f66c28 206ICECAdapter *CreateParser(cec_device_type_list typeList)
f8513317
LOK
207{
208 ICECAdapter *parser = LibCecInit("CECTester", typeList);
209 if (!parser || parser->GetMinLibVersion() > CEC_TEST_CLIENT_VERSION)
210 {
211 #ifdef __WINDOWS__
212 cout << "Cannot load libcec.dll" << endl;
213 #else
214 cout << "Cannot load libcec.so" << endl;
215 #endif
216 return NULL;
217 }
218
219 CStdString strLog;
220 strLog.Format("CEC Parser created - libcec version %d.%d", parser->GetLibVersionMajor(), parser->GetLibVersionMinor());
221 cout << strLog.c_str() << endl;
222
223 return parser;
224}
225
d5f66c28 226void ShowHelpConsole(void)
825ddb96
LOK
227{
228 cout << endl <<
229 "================================================================================" << endl <<
230 "Available commands:" << endl <<
231 endl <<
7e793bdd
LOK
232 "[tx] {bytes} transfer bytes over the CEC line." << endl <<
233 "[txn] {bytes} transfer bytes but don't wait for transmission ACK." << endl <<
234 "[on] {address} power on the device with the given logical address." << endl <<
235 "[standby] {address} put the device with the given address in standby mode." << endl <<
236 "[la] {logical address} change the logical address of the CEC adapter." << endl <<
d2f1c157 237 "[p] {device} {port} change the HDMI port number of the CEC adapter." << endl <<
7e793bdd
LOK
238 "[pa] {physical address} change the physical address of the CEC adapter." << endl <<
239 "[osd] {addr} {string} set OSD message on the specified device." << endl <<
240 "[ver] {addr} get the CEC version of the specified device." << endl <<
241 "[ven] {addr} get the vendor ID of the specified device." << endl <<
242 "[lang] {addr} get the menu language of the specified device." << endl <<
243 "[pow] {addr} get the power status of the specified device." << endl <<
ed21be2a 244 "[name] {addr} get the OSD name of the specified device." << endl <<
7e793bdd
LOK
245 "[poll] {addr} poll the specified device." << endl <<
246 "[lad] lists active devices on the bus" << endl <<
247 "[ad] {addr} checks whether the specified device is active." << endl <<
248 "[at] {type} checks whether the specified device type is active." << endl <<
249 "[volup] send a volume up command to the amp if present" << endl <<
250 "[voldown] send a volume down command to the amp if present" << endl <<
251 "[mute] send a mute/unmute command to the amp if present" << endl <<
03ae897d 252 "[scan] scan the CEC bus and display device info" << endl <<
8b7e5ff6 253 "[mon] {1|0} enable or disable CEC bus monitoring." << endl <<
bdd433cb 254 "[log] {1 - 31} change the log level. see cectypes.h for values." << endl <<
825ddb96 255 "[ping] send a ping command to the CEC adapter." << endl <<
88f45c9b
LOK
256 "[bl] to let the adapter enter the bootloader, to upgrade" << endl <<
257 " the flash rom." << endl <<
258 "[r] reconnect to the CEC adapter." << endl <<
825ddb96 259 "[h] or [help] show this help." << endl <<
88f45c9b
LOK
260 "[q] or [quit] to quit the CEC test client and switch off all" << endl <<
261 " connected CEC devices." << endl <<
825ddb96 262 "================================================================================" << endl;
abbca718
LOK
263}
264
8354f747
LOK
265bool ProcessCommandTX(ICECAdapter *parser, const string &command, string &arguments)
266{
267 if (command == "tx" || command == "txn")
268 {
269 string strvalue;
270 uint8_t ivalue;
271 cec_command bytes;
272 bytes.Clear();
273
274 while (GetWord(arguments, strvalue) && HexStrToInt(strvalue, ivalue))
275 bytes.PushBack(ivalue);
276
277 if (command == "txn")
278 bytes.transmit_timeout = 0;
279
280 parser->Transmit(bytes);
281
282 return true;
283 }
284
285 return false;
286}
287
288bool ProcessCommandON(ICECAdapter *parser, const string &command, string &arguments)
289{
290 if (command == "on")
291 {
292 string strValue;
293 uint8_t iValue = 0;
294 if (GetWord(arguments, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF)
295 {
296 parser->PowerOnDevices((cec_logical_address) iValue);
297 return true;
298 }
299 else
300 {
301 cout << "invalid destination" << endl;
302 }
303 }
304
305 return false;
306}
307
308bool ProcessCommandSTANDBY(ICECAdapter *parser, const string &command, string &arguments)
309{
310 if (command == "standby")
311 {
312 string strValue;
313 uint8_t iValue = 0;
314 if (GetWord(arguments, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF)
315 {
316 parser->StandbyDevices((cec_logical_address) iValue);
317 return true;
318 }
319 else
320 {
321 cout << "invalid destination" << endl;
322 }
323 }
324
325 return false;
326}
327
328bool ProcessCommandPOLL(ICECAdapter *parser, const string &command, string &arguments)
329{
330 if (command == "poll")
331 {
332 string strValue;
333 uint8_t iValue = 0;
334 if (GetWord(arguments, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF)
335 {
336 if (parser->PollDevice((cec_logical_address) iValue))
337 cout << "POLL message sent" << endl;
338 else
339 cout << "POLL message not sent" << endl;
340 return true;
341 }
342 else
343 {
344 cout << "invalid destination" << endl;
345 }
346 }
347
348 return false;
349}
350
351bool ProcessCommandLA(ICECAdapter *parser, const string &command, string &arguments)
352{
353 if (command == "la")
354 {
355 string strvalue;
356 if (GetWord(arguments, strvalue))
357 {
358 parser->SetLogicalAddress((cec_logical_address) atoi(strvalue.c_str()));
359 return true;
360 }
361 }
362
363 return false;
364}
365
366bool ProcessCommandP(ICECAdapter *parser, const string &command, string &arguments)
367{
368 if (command == "p")
369 {
370 string strPort, strDevice;
371 if (GetWord(arguments, strDevice) && GetWord(arguments, strPort))
372 {
373 parser->SetHDMIPort((cec_logical_address)atoi(strDevice.c_str()), (uint8_t)atoi(strPort.c_str()));
374 return true;
375 }
376 }
377
378 return false;
379}
380
381bool ProcessCommandPA(ICECAdapter *parser, const string &command, string &arguments)
382{
383 if (command == "pa")
384 {
385 string strB1, strB2;
386 uint8_t iB1, iB2;
387 if (GetWord(arguments, strB1) && HexStrToInt(strB1, iB1) &&
388 GetWord(arguments, strB2) && HexStrToInt(strB2, iB2))
389 {
390 uint16_t iPhysicalAddress = ((uint16_t)iB1 << 8) + iB2;
391 parser->SetPhysicalAddress(iPhysicalAddress);
392 return true;
393 }
394 }
395
396 return false;
397}
398
399bool ProcessCommandOSD(ICECAdapter *parser, const string &command, string &arguments)
400{
401 if (command == "osd")
402 {
403 bool bFirstWord(false);
404 string strAddr, strMessage, strWord;
405 uint8_t iAddr;
406 if (GetWord(arguments, strAddr) && HexStrToInt(strAddr, iAddr) && iAddr < 0xF)
407 {
408 while (GetWord(arguments, strWord))
409 {
410 if (bFirstWord)
411 {
412 bFirstWord = false;
413 strMessage.append(" ");
414 }
415 strMessage.append(strWord);
416 }
417 parser->SetOSDString((cec_logical_address) iAddr, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME, strMessage.c_str());
418 return true;
419 }
420 }
421
422 return false;
423}
424
425bool ProcessCommandPING(ICECAdapter *parser, const string &command, string &arguments)
426{
427 if (command == "ping")
428 {
429 parser->PingAdapter();
430 return true;
431 }
432
433 return false;
434}
435
436bool ProcessCommandVOLUP(ICECAdapter *parser, const string &command, string &arguments)
437{
438 if (command == "volup")
439 {
440 CStdString strLog;
441 strLog.Format("volume up: %2X", parser->VolumeUp());
442 cout << strLog.c_str() << endl;
443 return true;
444 }
445
446 return false;
447}
448
449bool ProcessCommandVOLDOWN(ICECAdapter *parser, const string &command, string &arguments)
450{
451 if (command == "voldown")
452 {
453 CStdString strLog;
454 strLog.Format("volume up: %2X", parser->VolumeDown());
455 cout << strLog.c_str() << endl;
456 return true;
457 }
458
459 return false;
460}
461
462bool ProcessCommandMUTE(ICECAdapter *parser, const string &command, string &arguments)
463{
464 if (command == "mute")
465 {
466 CStdString strLog;
467 strLog.Format("mute: %2X", parser->MuteAudio());
468 cout << strLog.c_str() << endl;
469 return true;
470 }
471
472 return false;
473}
474
475bool ProcessCommandMON(ICECAdapter *parser, const string &command, string &arguments)
476{
477 if (command == "mon")
478 {
479 CStdString strEnable;
480 if (GetWord(arguments, strEnable) && (strEnable.Equals("0") || strEnable.Equals("1")))
481 {
482 parser->SwitchMonitoring(strEnable.Equals("1"));
483 return true;
484 }
485 }
486
487 return false;
488}
489
490bool ProcessCommandBL(ICECAdapter *parser, const string &command, string &arguments)
491{
492 if (command == "bl")
493 {
494 parser->StartBootloader();
495 return true;
496 }
497
498 return false;
499}
500
501bool ProcessCommandLANG(ICECAdapter *parser, const string &command, string &arguments)
502{
503 if (command == "lang")
504 {
505 CStdString strDev;
506 if (GetWord(arguments, strDev))
507 {
508 int iDev = atoi(strDev);
509 if (iDev >= 0 && iDev < 15)
510 {
511 CStdString strLog;
512 cec_menu_language language;
513 if (parser->GetDeviceMenuLanguage((cec_logical_address) iDev, &language))
514 strLog.Format("menu language '%s'", language.language);
515 else
516 strLog = "failed!";
517 cout << strLog.c_str() << endl;
518 return true;
519 }
520 }
521 }
522
523 return false;
524}
525
526bool ProcessCommandVEN(ICECAdapter *parser, const string &command, string &arguments)
527{
528 if (command == "ven")
529 {
530 CStdString strDev;
531 if (GetWord(arguments, strDev))
532 {
533 int iDev = atoi(strDev);
534 if (iDev >= 0 && iDev < 15)
535 {
536 uint64_t iVendor = parser->GetDeviceVendorId((cec_logical_address) iDev);
537 CStdString strLog;
538 strLog.Format("vendor id: %06x", iVendor);
539 cout << strLog.c_str() << endl;
540 return true;
541 }
542 }
543 }
544
545 return false;
546}
547
548bool ProcessCommandVER(ICECAdapter *parser, const string &command, string &arguments)
549{
550 if (command == "ver")
551 {
552 CStdString strDev;
553 if (GetWord(arguments, strDev))
554 {
555 int iDev = atoi(strDev);
556 if (iDev >= 0 && iDev < 15)
557 {
558 cec_version iVersion = parser->GetDeviceCecVersion((cec_logical_address) iDev);
559 cout << "CEC version " << parser->ToString(iVersion) << endl;
560 return true;
561 }
562 }
563 }
564
565 return false;
566}
567
568bool ProcessCommandPOW(ICECAdapter *parser, const string &command, string &arguments)
569{
570 if (command == "pow")
571 {
572 CStdString strDev;
573 if (GetWord(arguments, strDev))
574 {
575 int iDev = atoi(strDev);
576 if (iDev >= 0 && iDev < 15)
577 {
578 cec_power_status iPower = parser->GetDevicePowerStatus((cec_logical_address) iDev);
579 cout << "power status: " << parser->ToString(iPower) << endl;
580
581 return true;
582 }
583 }
584 }
585
586 return false;
587}
588
589bool ProcessCommandNAME(ICECAdapter *parser, const string &command, string &arguments)
590{
591 if (command == "name")
592 {
593 CStdString strDev;
594 if (GetWord(arguments, strDev))
595 {
596 int iDev = atoi(strDev);
597 if (iDev >= 0 && iDev < 15)
598 {
f71a1df9 599 cec_osd_name name = parser->GetDeviceOSDName((cec_logical_address)iDev);
8354f747
LOK
600 cout << "OSD name of device " << iDev << " is '" << name.name << "'" << endl;
601 }
602 return true;
603 }
604 }
605
606 return false;
607}
608
609bool ProcessCommandLAD(ICECAdapter *parser, const string &command, string &arguments)
610{
611 if (command == "lad")
612 {
613 cout << "listing active devices:" << endl;
614 cec_logical_addresses addresses = parser->GetActiveDevices();
90ea9066 615 for (uint8_t iPtr = 0; iPtr <= 11; iPtr++)
8354f747
LOK
616 if (addresses[iPtr])
617 cout << "logical address " << (int)iPtr << endl;
618 return true;
619 }
620
621 return false;
622}
623
624bool ProcessCommandAD(ICECAdapter *parser, const string &command, string &arguments)
625{
626 if (command == "ad")
627 {
628 CStdString strDev;
629 if (GetWord(arguments, strDev))
630 {
631 int iDev = atoi(strDev);
632 if (iDev >= 0 && iDev < 15)
633 cout << "logical address " << iDev << " is " << (parser->IsActiveDevice((cec_logical_address)iDev) ? "active" : "not active") << endl;
634 }
635 }
636
637 return false;
638}
639
640bool ProcessCommandAT(ICECAdapter *parser, const string &command, string &arguments)
641{
642 if (command == "at")
643 {
644 CStdString strType;
645 if (GetWord(arguments, strType))
646 {
647 cec_device_type type = CEC_DEVICE_TYPE_TV;
648 if (strType.Equals("a"))
649 type = CEC_DEVICE_TYPE_AUDIO_SYSTEM;
650 else if (strType.Equals("p"))
651 type = CEC_DEVICE_TYPE_PLAYBACK_DEVICE;
652 else if (strType.Equals("r"))
653 type = CEC_DEVICE_TYPE_RECORDING_DEVICE;
654 else if (strType.Equals("t"))
655 type = CEC_DEVICE_TYPE_TUNER;
656 cout << "device " << type << " is " << (parser->IsActiveDeviceType(type) ? "active" : "not active") << endl;
657 return true;
658 }
659 }
660
661 return false;
662}
663
664bool ProcessCommandR(ICECAdapter *parser, const string &command, string &arguments)
abbca718 665{
8354f747
LOK
666 if (command == "r")
667 {
668 cout << "closing the connection" << endl;
669 parser->Close();
670 FlushLog(parser);
671
672 cout << "opening a new connection" << endl;
673 parser->Open(g_strPort.c_str());
674 FlushLog(parser);
675
676 cout << "setting active source" << endl;
677 parser->SetActiveSource();
678 return true;
679 }
680
681 return false;
682}
683
684bool ProcessCommandH(ICECAdapter *parser, const string &command, string &arguments)
685{
686 if (command == "h" || command == "help")
687 {
688 ShowHelpConsole();
689 return true;
690 }
691
692 return false;
693}
abbca718 694
8354f747
LOK
695bool ProcessCommandLOG(ICECAdapter *parser, const string &command, string &arguments)
696{
697 if (command == "log")
698 {
699 CStdString strLevel;
700 if (GetWord(arguments, strLevel))
701 {
702 int iNewLevel = atoi(strLevel);
703 if (iNewLevel >= CEC_LOG_ERROR && iNewLevel <= CEC_LOG_ALL)
704 {
705 g_cecLogLevel = iNewLevel;
706 cout << "log level changed to " << strLevel.c_str() << endl;
707 return true;
708 }
709 }
710 }
711
712 return false;
713}
714
715bool ProcessCommandSCAN(ICECAdapter *parser, const string &command, string &arguments)
716{
717 if (command == "scan")
718 {
719 cout << "CEC bus information" << endl;
720 cout << "===================" << endl;
721 cec_logical_addresses addresses = parser->GetActiveDevices();
722 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
723 {
724 if (addresses[iPtr])
725 {
a98b2f2e 726 uint64_t iVendorId = parser->GetDeviceVendorId((cec_logical_address)iPtr);
b4b1b49b 727 bool bActive = parser->IsActiveSource((cec_logical_address)iPtr);
eab72c40 728 uint16_t iPhysicalAddress = parser->GetDevicePhysicalAddress((cec_logical_address)iPtr);
eab72c40
LOK
729 cec_version iCecVersion = parser->GetDeviceCecVersion((cec_logical_address)iPtr);
730 cec_power_status power = parser->GetDevicePowerStatus((cec_logical_address)iPtr);
f71a1df9 731 cec_osd_name osdName = parser->GetDeviceOSDName((cec_logical_address)iPtr);
eab72c40
LOK
732 CStdString strAddr;
733 strAddr.Format("%04x", iPhysicalAddress);
8354f747
LOK
734 cec_menu_language lang;
735 lang.device = CECDEVICE_UNKNOWN;
736 parser->GetDeviceMenuLanguage((cec_logical_address)iPtr, &lang);
737
738 cout << "device #" << (int)iPtr << ": " << parser->ToString((cec_logical_address)iPtr) << endl;
b4b1b49b
LOK
739 cout << "address: " << strAddr.c_str() << endl;
740 cout << "active source: " << (bActive ? "yes" : "no") << endl;
741 cout << "vendor: " << parser->ToString((cec_vendor_id)iVendorId) << endl;
742 cout << "osd string: " << osdName.name << endl;
743 cout << "CEC version: " << parser->ToString(iCecVersion) << endl;
744 cout << "power status: " << parser->ToString(power) << endl;
8354f747 745 if ((uint8_t)lang.device == iPtr)
b4b1b49b 746 cout << "language: " << lang.language << endl;
8354f747
LOK
747 cout << endl;
748 }
749 }
750 return true;
751 }
752
753 return false;
754}
755
756bool ProcessConsoleCommand(ICECAdapter *parser, string &input)
757{
758 if (!input.empty())
759 {
760 string command;
761 if (GetWord(input, command))
762 {
763 if (command == "q" || command == "quit")
764 return false;
765
766 ProcessCommandTX(parser, command, input) ||
767 ProcessCommandON(parser, command, input) ||
768 ProcessCommandSTANDBY(parser, command, input) ||
769 ProcessCommandPOLL(parser, command, input) ||
770 ProcessCommandLA(parser, command, input) ||
771 ProcessCommandP(parser, command, input) ||
772 ProcessCommandPA(parser, command, input) ||
773 ProcessCommandOSD(parser, command, input) ||
774 ProcessCommandPING(parser, command, input) ||
775 ProcessCommandVOLUP(parser, command, input) ||
776 ProcessCommandVOLDOWN(parser, command, input) ||
777 ProcessCommandMUTE(parser, command, input) ||
778 ProcessCommandMON(parser, command, input) ||
779 ProcessCommandBL(parser, command, input) ||
780 ProcessCommandLANG(parser, command, input) ||
781 ProcessCommandVEN(parser, command, input) ||
782 ProcessCommandVER(parser, command, input) ||
783 ProcessCommandPOW(parser, command, input) ||
784 ProcessCommandNAME(parser, command, input) ||
785 ProcessCommandLAD(parser, command, input) ||
786 ProcessCommandAD(parser, command, input) ||
787 ProcessCommandAT(parser, command, input) ||
788 ProcessCommandR(parser, command, input) ||
789 ProcessCommandH(parser, command, input) ||
790 ProcessCommandLOG(parser, command, input) ||
791 ProcessCommandSCAN(parser, command, input);
792 }
793 }
794 return true;
795}
796
797bool ProcessCommandLineArguments(int argc, char *argv[])
798{
799 bool bReturn(true);
3d0dca16 800 int iArgPtr = 1;
d9c13c6d 801 while (iArgPtr < argc && bReturn)
e6d2161b 802 {
3d0dca16 803 if (argc >= iArgPtr + 1)
e6d2161b 804 {
3d0dca16
LOK
805 if (!strcmp(argv[iArgPtr], "-f") ||
806 !strcmp(argv[iArgPtr], "--log-file") ||
807 !strcmp(argv[iArgPtr], "-sf") ||
808 !strcmp(argv[iArgPtr], "--short-log-file"))
809 {
810 if (argc >= iArgPtr + 2)
811 {
812 g_logOutput.open(argv[iArgPtr + 1]);
813 g_bShortLog = (!strcmp(argv[iArgPtr], "-sf") || !strcmp(argv[iArgPtr], "--short-log-file"));
814 iArgPtr += 2;
815 }
816 else
817 {
606034b6
LOK
818 cout << "== skipped log-file parameter: no file given ==" << endl;
819 ++iArgPtr;
820 }
821 }
822 else if (!strcmp(argv[iArgPtr], "-d") ||
823 !strcmp(argv[iArgPtr], "--log-level"))
824 {
825 if (argc >= iArgPtr + 2)
826 {
827 int iNewLevel = atoi(argv[iArgPtr + 1]);
828 if (iNewLevel >= CEC_LOG_ERROR && iNewLevel <= CEC_LOG_ALL)
829 {
830 g_cecLogLevel = iNewLevel;
8354f747 831 if (!g_bSingleCommand)
5ad0ea13 832 cout << "log level set to " << argv[iArgPtr + 1] << endl;
606034b6
LOK
833 }
834 else
835 {
836 cout << "== skipped log-level parameter: invalid level '" << argv[iArgPtr + 1] << "' ==" << endl;
837 }
838 iArgPtr += 2;
839 }
840 else
841 {
842 cout << "== skipped log-level parameter: no level given ==" << endl;
3d0dca16
LOK
843 ++iArgPtr;
844 }
845 }
f8513317
LOK
846 else if (!strcmp(argv[iArgPtr], "-t") ||
847 !strcmp(argv[iArgPtr], "--type"))
d6cc5f60
LOK
848 {
849 if (argc >= iArgPtr + 2)
850 {
f8513317
LOK
851 if (!strcmp(argv[iArgPtr + 1], "p"))
852 {
8354f747 853 if (!g_bSingleCommand)
5ad0ea13 854 cout << "== using device type 'playback device'" << endl;
8354f747 855 g_typeList.add(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
f8513317
LOK
856 }
857 else if (!strcmp(argv[iArgPtr + 1], "r"))
d6cc5f60 858 {
8354f747 859 if (!g_bSingleCommand)
5ad0ea13 860 cout << "== using device type 'recording device'" << endl;
8354f747 861 g_typeList.add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
f8513317
LOK
862 }
863 else if (!strcmp(argv[iArgPtr + 1], "t"))
864 {
8354f747 865 if (!g_bSingleCommand)
5ad0ea13 866 cout << "== using device type 'tuner'" << endl;
8354f747 867 g_typeList.add(CEC_DEVICE_TYPE_TUNER);
f8513317
LOK
868 }
869 else if (!strcmp(argv[iArgPtr + 1], "a"))
870 {
8354f747 871 if (!g_bSingleCommand)
5ad0ea13 872 cout << "== using device type 'audio system'" << endl;
8354f747 873 g_typeList.add(CEC_DEVICE_TYPE_AUDIO_SYSTEM);
d6cc5f60
LOK
874 }
875 else
876 {
f8513317 877 cout << "== skipped invalid device type '" << argv[iArgPtr + 1] << "'" << endl;
d6cc5f60 878 }
d6cc5f60
LOK
879 ++iArgPtr;
880 }
f8513317 881 ++iArgPtr;
d6cc5f60 882 }
3d0dca16
LOK
883 else if (!strcmp(argv[iArgPtr], "--list-devices") ||
884 !strcmp(argv[iArgPtr], "-l"))
885 {
8354f747 886 ICECAdapter *parser = CreateParser(g_typeList);
f8513317
LOK
887 if (parser)
888 {
d5f66c28 889 ListDevices(parser);
f8513317 890 UnloadLibCec(parser);
d9c13c6d 891 parser = NULL;
f8513317 892 }
8354f747 893 bReturn = false;
3d0dca16 894 }
5ad0ea13
LOK
895 else if (!strcmp(argv[iArgPtr], "--single-command") ||
896 !strcmp(argv[iArgPtr], "-s"))
897 {
8354f747 898 g_bSingleCommand = true;
5ad0ea13
LOK
899 ++iArgPtr;
900 }
3d0dca16
LOK
901 else if (!strcmp(argv[iArgPtr], "--help") ||
902 !strcmp(argv[iArgPtr], "-h"))
903 {
d5f66c28 904 ShowHelpCommandLine(argv[0]);
3d0dca16
LOK
905 return 0;
906 }
aa52c2e0
LOK
907 else if (!strcmp(argv[iArgPtr], "-b") ||
908 !strcmp(argv[iArgPtr], "--base"))
909 {
910 if (argc >= iArgPtr + 2)
911 {
912 g_iBaseDevice = (cec_logical_address)atoi(argv[iArgPtr + 1]);
913 cout << "using base device '" << (int)g_iBaseDevice << "'" << endl;
914 ++iArgPtr;
915 }
916 ++iArgPtr;
917 }
45de9d9f 918 else if (!strcmp(argv[iArgPtr], "-p") ||
16b1e052 919 !strcmp(argv[iArgPtr], "--port"))
45de9d9f
LOK
920 {
921 if (argc >= iArgPtr + 2)
922 {
8354f747 923 g_iHDMIPort = (int8_t)atoi(argv[iArgPtr + 1]);
aa52c2e0 924 cout << "using HDMI port '" << (int)g_iHDMIPort << "'" << endl;
45de9d9f
LOK
925 ++iArgPtr;
926 }
927 ++iArgPtr;
928 }
3d0dca16
LOK
929 else
930 {
931 g_strPort = argv[iArgPtr++];
932 }
e6d2161b
LOK
933 }
934 }
935
8354f747
LOK
936 return bReturn;
937}
938
939int main (int argc, char *argv[])
940{
941 g_typeList.clear();
942
943 if (!ProcessCommandLineArguments(argc, argv))
944 return 0;
945
946 if (g_typeList.IsEmpty())
f8513317 947 {
8354f747 948 if (!g_bSingleCommand)
88e5de6f 949 cout << "No device type given. Using 'recording device'" << endl;
8354f747 950 g_typeList.add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
f8513317
LOK
951 }
952
8354f747 953 ICECAdapter *parser = LibCecInit("CECTester", g_typeList);
f8513317
LOK
954 if (!parser || parser->GetMinLibVersion() > CEC_TEST_CLIENT_VERSION)
955 {
956#ifdef __WINDOWS__
957 cout << "Cannot load libcec.dll" << endl;
958#else
959 cout << "Cannot load libcec.so" << endl;
960#endif
961 return 1;
962 }
f8513317 963
8354f747 964 if (!g_bSingleCommand)
5ad0ea13
LOK
965 {
966 CStdString strLog;
967 strLog.Format("CEC Parser created - libcec version %d.%d", parser->GetLibVersionMajor(), parser->GetLibVersionMinor());
968 cout << strLog.c_str() << endl;
969
970 //make stdin non-blocking
971 #ifndef __WINDOWS__
972 int flags = fcntl(0, F_GETFL, 0);
973 flags |= O_NONBLOCK;
974 fcntl(0, F_SETFL, flags);
975 #endif
976 }
f8513317 977
3d0dca16 978 if (g_strPort.IsEmpty())
abbca718 979 {
8354f747 980 if (!g_bSingleCommand)
5ad0ea13 981 cout << "no serial port given. trying autodetect: ";
25701fa6
LOK
982 cec_adapter devices[10];
983 uint8_t iDevicesFound = parser->FindAdapters(devices, 10, NULL);
abbca718
LOK
984 if (iDevicesFound <= 0)
985 {
8354f747 986 if (g_bSingleCommand)
5ad0ea13 987 cout << "autodetect ";
abbca718
LOK
988 cout << "FAILED" << endl;
989 UnloadLibCec(parser);
990 return 1;
991 }
992 else
993 {
8354f747 994 if (!g_bSingleCommand)
5ad0ea13
LOK
995 {
996 cout << endl << " path: " << devices[0].path << endl <<
997 " com port: " << devices[0].comm << endl << endl;
998 }
3d0dca16 999 g_strPort = devices[0].comm;
abbca718
LOK
1000 }
1001 }
e6d2161b 1002
aa52c2e0
LOK
1003 parser->SetHDMIPort(g_iBaseDevice, g_iHDMIPort);
1004 cout << "opening a connection to the CEC adapter..." << endl;
16b1e052 1005
3d0dca16 1006 if (!parser->Open(g_strPort.c_str()))
abbca718 1007 {
3d0dca16 1008 cout << "unable to open the device on port " << g_strPort << endl;
d5f66c28 1009 FlushLog(parser);
abbca718
LOK
1010 UnloadLibCec(parser);
1011 return 1;
1012 }
1013
8354f747 1014 if (!g_bSingleCommand)
5ad0ea13 1015 {
45b97de7 1016 FlushLog(parser);
5ad0ea13 1017 cout << "cec device opened" << endl;
abbca718 1018
5ad0ea13
LOK
1019 parser->PowerOnDevices(CECDEVICE_TV);
1020 FlushLog(parser);
abbca718 1021
5ad0ea13
LOK
1022 parser->SetActiveSource();
1023 FlushLog(parser);
1024
1025 cout << "waiting for input" << endl;
1026 }
abbca718
LOK
1027
1028 bool bContinue(true);
abbca718
LOK
1029 while (bContinue)
1030 {
d5f66c28 1031 FlushLog(parser);
abbca718 1032
b5b53c7d
LOK
1033 /* just ignore the command buffer and clear it */
1034 cec_command dummy;
1035 while (parser && parser->GetNextCommand(&dummy)) {}
1036
abbca718
LOK
1037 string input;
1038 getline(cin, input);
1039 cin.clear();
1040
8354f747 1041 if (ProcessConsoleCommand(parser, input) && !g_bSingleCommand)
abbca718 1042 {
8354f747 1043 if (!input.empty())
abbca718
LOK
1044 cout << "waiting for input" << endl;
1045 }
8354f747
LOK
1046 else
1047 bContinue = false;
5ad0ea13
LOK
1048
1049 if (bContinue)
1050 CCondition::Sleep(50);
abbca718
LOK
1051 }
1052
8354f747 1053 if (!g_bSingleCommand)
5ad0ea13
LOK
1054 parser->StandbyDevices(CECDEVICE_BROADCAST);
1055
5f39c4d8 1056 parser->Close();
d5f66c28 1057 FlushLog(parser);
abbca718 1058 UnloadLibCec(parser);
e6d2161b
LOK
1059
1060 if (g_logOutput.is_open())
1061 g_logOutput.close();
1062
abbca718
LOK
1063 return 0;
1064}