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