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