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