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