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