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