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