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