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