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