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