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