cec: don't unlock the transmit mutex in CCECProcessor while waiting for an answer
[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();
612 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
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 {
723 uint64_t iVendorId = parser->GetDeviceVendorId((cec_logical_address)iPtr);
724 cec_version iCecVersion = parser->GetDeviceCecVersion((cec_logical_address)iPtr);
725 cec_power_status power = parser->GetDevicePowerStatus((cec_logical_address)iPtr);
726 cec_osd_name osdName = parser->GetOSDName((cec_logical_address)iPtr);
727 cec_menu_language lang;
728 lang.device = CECDEVICE_UNKNOWN;
729 parser->GetDeviceMenuLanguage((cec_logical_address)iPtr, &lang);
730
731 cout << "device #" << (int)iPtr << ": " << parser->ToString((cec_logical_address)iPtr) << endl;
732 cout << "vendor: " << parser->ToString((cec_vendor_id)iVendorId) << endl;
733 cout << "osd string: " << osdName.name << endl;
734 cout << "CEC version: " << parser->ToString(iCecVersion) << endl;
735 cout << "power status: " << parser->ToString(power) << endl;
736 if ((uint8_t)lang.device == iPtr)
737 cout << "language: " << lang.language << endl;
738 cout << endl;
739 }
740 }
741 return true;
742 }
743
744 return false;
745}
746
747bool ProcessConsoleCommand(ICECAdapter *parser, string &input)
748{
749 if (!input.empty())
750 {
751 string command;
752 if (GetWord(input, command))
753 {
754 if (command == "q" || command == "quit")
755 return false;
756
757 ProcessCommandTX(parser, command, input) ||
758 ProcessCommandON(parser, command, input) ||
759 ProcessCommandSTANDBY(parser, command, input) ||
760 ProcessCommandPOLL(parser, command, input) ||
761 ProcessCommandLA(parser, command, input) ||
762 ProcessCommandP(parser, command, input) ||
763 ProcessCommandPA(parser, command, input) ||
764 ProcessCommandOSD(parser, command, input) ||
765 ProcessCommandPING(parser, command, input) ||
766 ProcessCommandVOLUP(parser, command, input) ||
767 ProcessCommandVOLDOWN(parser, command, input) ||
768 ProcessCommandMUTE(parser, command, input) ||
769 ProcessCommandMON(parser, command, input) ||
770 ProcessCommandBL(parser, command, input) ||
771 ProcessCommandLANG(parser, command, input) ||
772 ProcessCommandVEN(parser, command, input) ||
773 ProcessCommandVER(parser, command, input) ||
774 ProcessCommandPOW(parser, command, input) ||
775 ProcessCommandNAME(parser, command, input) ||
776 ProcessCommandLAD(parser, command, input) ||
777 ProcessCommandAD(parser, command, input) ||
778 ProcessCommandAT(parser, command, input) ||
779 ProcessCommandR(parser, command, input) ||
780 ProcessCommandH(parser, command, input) ||
781 ProcessCommandLOG(parser, command, input) ||
782 ProcessCommandSCAN(parser, command, input);
783 }
784 }
785 return true;
786}
787
788bool ProcessCommandLineArguments(int argc, char *argv[])
789{
790 bool bReturn(true);
3d0dca16
LOK
791 int iArgPtr = 1;
792 while (iArgPtr < argc)
e6d2161b 793 {
3d0dca16 794 if (argc >= iArgPtr + 1)
e6d2161b 795 {
3d0dca16
LOK
796 if (!strcmp(argv[iArgPtr], "-f") ||
797 !strcmp(argv[iArgPtr], "--log-file") ||
798 !strcmp(argv[iArgPtr], "-sf") ||
799 !strcmp(argv[iArgPtr], "--short-log-file"))
800 {
801 if (argc >= iArgPtr + 2)
802 {
803 g_logOutput.open(argv[iArgPtr + 1]);
804 g_bShortLog = (!strcmp(argv[iArgPtr], "-sf") || !strcmp(argv[iArgPtr], "--short-log-file"));
805 iArgPtr += 2;
806 }
807 else
808 {
606034b6
LOK
809 cout << "== skipped log-file parameter: no file given ==" << endl;
810 ++iArgPtr;
811 }
812 }
813 else if (!strcmp(argv[iArgPtr], "-d") ||
814 !strcmp(argv[iArgPtr], "--log-level"))
815 {
816 if (argc >= iArgPtr + 2)
817 {
818 int iNewLevel = atoi(argv[iArgPtr + 1]);
819 if (iNewLevel >= CEC_LOG_ERROR && iNewLevel <= CEC_LOG_ALL)
820 {
821 g_cecLogLevel = iNewLevel;
8354f747 822 if (!g_bSingleCommand)
5ad0ea13 823 cout << "log level set to " << argv[iArgPtr + 1] << endl;
606034b6
LOK
824 }
825 else
826 {
827 cout << "== skipped log-level parameter: invalid level '" << argv[iArgPtr + 1] << "' ==" << endl;
828 }
829 iArgPtr += 2;
830 }
831 else
832 {
833 cout << "== skipped log-level parameter: no level given ==" << endl;
3d0dca16
LOK
834 ++iArgPtr;
835 }
836 }
f8513317
LOK
837 else if (!strcmp(argv[iArgPtr], "-t") ||
838 !strcmp(argv[iArgPtr], "--type"))
d6cc5f60
LOK
839 {
840 if (argc >= iArgPtr + 2)
841 {
f8513317
LOK
842 if (!strcmp(argv[iArgPtr + 1], "p"))
843 {
8354f747 844 if (!g_bSingleCommand)
5ad0ea13 845 cout << "== using device type 'playback device'" << endl;
8354f747 846 g_typeList.add(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
f8513317
LOK
847 }
848 else if (!strcmp(argv[iArgPtr + 1], "r"))
d6cc5f60 849 {
8354f747 850 if (!g_bSingleCommand)
5ad0ea13 851 cout << "== using device type 'recording device'" << endl;
8354f747 852 g_typeList.add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
f8513317
LOK
853 }
854 else if (!strcmp(argv[iArgPtr + 1], "t"))
855 {
8354f747 856 if (!g_bSingleCommand)
5ad0ea13 857 cout << "== using device type 'tuner'" << endl;
8354f747 858 g_typeList.add(CEC_DEVICE_TYPE_TUNER);
f8513317
LOK
859 }
860 else if (!strcmp(argv[iArgPtr + 1], "a"))
861 {
8354f747 862 if (!g_bSingleCommand)
5ad0ea13 863 cout << "== using device type 'audio system'" << endl;
8354f747 864 g_typeList.add(CEC_DEVICE_TYPE_AUDIO_SYSTEM);
d6cc5f60
LOK
865 }
866 else
867 {
f8513317 868 cout << "== skipped invalid device type '" << argv[iArgPtr + 1] << "'" << endl;
d6cc5f60 869 }
d6cc5f60
LOK
870 ++iArgPtr;
871 }
f8513317 872 ++iArgPtr;
d6cc5f60 873 }
3d0dca16
LOK
874 else if (!strcmp(argv[iArgPtr], "--list-devices") ||
875 !strcmp(argv[iArgPtr], "-l"))
876 {
8354f747 877 ICECAdapter *parser = CreateParser(g_typeList);
f8513317
LOK
878 if (parser)
879 {
d5f66c28 880 ListDevices(parser);
f8513317
LOK
881 UnloadLibCec(parser);
882 }
8354f747 883 bReturn = false;
3d0dca16 884 }
5ad0ea13
LOK
885 else if (!strcmp(argv[iArgPtr], "--single-command") ||
886 !strcmp(argv[iArgPtr], "-s"))
887 {
8354f747 888 g_bSingleCommand = true;
5ad0ea13
LOK
889 ++iArgPtr;
890 }
3d0dca16
LOK
891 else if (!strcmp(argv[iArgPtr], "--help") ||
892 !strcmp(argv[iArgPtr], "-h"))
893 {
d5f66c28 894 ShowHelpCommandLine(argv[0]);
3d0dca16
LOK
895 return 0;
896 }
45de9d9f 897 else if (!strcmp(argv[iArgPtr], "-p") ||
16b1e052 898 !strcmp(argv[iArgPtr], "--port"))
45de9d9f
LOK
899 {
900 if (argc >= iArgPtr + 2)
901 {
8354f747
LOK
902 g_iHDMIPort = (int8_t)atoi(argv[iArgPtr + 1]);
903 cout << "using HDMI port '" << g_iHDMIPort << "'" << endl;
45de9d9f
LOK
904 ++iArgPtr;
905 }
906 ++iArgPtr;
907 }
3d0dca16
LOK
908 else
909 {
910 g_strPort = argv[iArgPtr++];
911 }
e6d2161b
LOK
912 }
913 }
914
8354f747
LOK
915 return bReturn;
916}
917
918int main (int argc, char *argv[])
919{
920 g_typeList.clear();
921
922 if (!ProcessCommandLineArguments(argc, argv))
923 return 0;
924
925 if (g_typeList.IsEmpty())
f8513317 926 {
8354f747 927 if (!g_bSingleCommand)
88e5de6f 928 cout << "No device type given. Using 'recording device'" << endl;
8354f747 929 g_typeList.add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
f8513317
LOK
930 }
931
8354f747 932 ICECAdapter *parser = LibCecInit("CECTester", g_typeList);
f8513317
LOK
933 if (!parser || parser->GetMinLibVersion() > CEC_TEST_CLIENT_VERSION)
934 {
935#ifdef __WINDOWS__
936 cout << "Cannot load libcec.dll" << endl;
937#else
938 cout << "Cannot load libcec.so" << endl;
939#endif
940 return 1;
941 }
f8513317 942
8354f747 943 if (!g_bSingleCommand)
5ad0ea13
LOK
944 {
945 CStdString strLog;
946 strLog.Format("CEC Parser created - libcec version %d.%d", parser->GetLibVersionMajor(), parser->GetLibVersionMinor());
947 cout << strLog.c_str() << endl;
948
949 //make stdin non-blocking
950 #ifndef __WINDOWS__
951 int flags = fcntl(0, F_GETFL, 0);
952 flags |= O_NONBLOCK;
953 fcntl(0, F_SETFL, flags);
954 #endif
955 }
f8513317 956
3d0dca16 957 if (g_strPort.IsEmpty())
abbca718 958 {
8354f747 959 if (!g_bSingleCommand)
5ad0ea13 960 cout << "no serial port given. trying autodetect: ";
25701fa6
LOK
961 cec_adapter devices[10];
962 uint8_t iDevicesFound = parser->FindAdapters(devices, 10, NULL);
abbca718
LOK
963 if (iDevicesFound <= 0)
964 {
8354f747 965 if (g_bSingleCommand)
5ad0ea13 966 cout << "autodetect ";
abbca718
LOK
967 cout << "FAILED" << endl;
968 UnloadLibCec(parser);
969 return 1;
970 }
971 else
972 {
8354f747 973 if (!g_bSingleCommand)
5ad0ea13
LOK
974 {
975 cout << endl << " path: " << devices[0].path << endl <<
976 " com port: " << devices[0].comm << endl << endl;
977 }
3d0dca16 978 g_strPort = devices[0].comm;
abbca718
LOK
979 }
980 }
e6d2161b 981
8354f747 982 if (g_iHDMIPort > 0)
16b1e052 983 {
8354f747 984 parser->SetHDMIPort((cec_logical_address)CEC_DEFAULT_BASE_DEVICE, (uint8_t)g_iHDMIPort);
16b1e052
LOK
985 FlushLog(parser);
986 }
987
988 cout << "scanning the CEC bus..." << endl;
989
3d0dca16 990 if (!parser->Open(g_strPort.c_str()))
abbca718 991 {
3d0dca16 992 cout << "unable to open the device on port " << g_strPort << endl;
d5f66c28 993 FlushLog(parser);
abbca718
LOK
994 UnloadLibCec(parser);
995 return 1;
996 }
997
8354f747 998 if (!g_bSingleCommand)
5ad0ea13 999 {
45b97de7 1000 FlushLog(parser);
5ad0ea13 1001 cout << "cec device opened" << endl;
abbca718 1002
5ad0ea13
LOK
1003 parser->PowerOnDevices(CECDEVICE_TV);
1004 FlushLog(parser);
abbca718 1005
5ad0ea13
LOK
1006 parser->SetActiveSource();
1007 FlushLog(parser);
1008
1009 cout << "waiting for input" << endl;
1010 }
abbca718
LOK
1011
1012 bool bContinue(true);
abbca718
LOK
1013 while (bContinue)
1014 {
d5f66c28 1015 FlushLog(parser);
abbca718 1016
b5b53c7d
LOK
1017 /* just ignore the command buffer and clear it */
1018 cec_command dummy;
1019 while (parser && parser->GetNextCommand(&dummy)) {}
1020
abbca718
LOK
1021 string input;
1022 getline(cin, input);
1023 cin.clear();
1024
8354f747 1025 if (ProcessConsoleCommand(parser, input) && !g_bSingleCommand)
abbca718 1026 {
8354f747 1027 if (!input.empty())
abbca718
LOK
1028 cout << "waiting for input" << endl;
1029 }
8354f747
LOK
1030 else
1031 bContinue = false;
5ad0ea13
LOK
1032
1033 if (bContinue)
1034 CCondition::Sleep(50);
abbca718
LOK
1035 }
1036
8354f747 1037 if (!g_bSingleCommand)
5ad0ea13
LOK
1038 parser->StandbyDevices(CECDEVICE_BROADCAST);
1039
5f39c4d8 1040 parser->Close();
d5f66c28 1041 FlushLog(parser);
abbca718 1042 UnloadLibCec(parser);
e6d2161b
LOK
1043
1044 if (g_logOutput.is_open())
1045 g_logOutput.close();
1046
abbca718
LOK
1047 return 0;
1048}