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