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