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