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