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