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