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