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