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