-
Notifications
You must be signed in to change notification settings - Fork 295
/
Copy pathcec-client.cpp
1419 lines (1276 loc) · 41.7 KB
/
cec-client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of the libCEC(R) library.
*
* libCEC(R) is Copyright (C) 2011-2015 Pulse-Eight Limited. All rights reserved.
* libCEC(R) is an original work, containing original code.
*
* libCEC(R) is a trademark of Pulse-Eight Limited.
*
* This program is dual-licensed; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*
* Alternatively, you can license this library under a commercial license,
* please contact Pulse-Eight Licensing for more information.
*
* For more information contact:
* Pulse-Eight Licensing <[email protected]>
* http://www.pulse-eight.com/
* http://www.pulse-eight.net/
*/
#include "env.h"
#include "cec.h"
#include <cstdio>
#include <fcntl.h>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <signal.h>
#include <stdlib.h>
#include "p8-platform/os.h"
#include "p8-platform/util/StringUtils.h"
#include "p8-platform/threads/threads.h"
#if defined(HAVE_CURSES_API)
#include "curses/CursesControl.h"
#endif
using namespace CEC;
using namespace P8PLATFORM;
#include "cecloader.h"
static void PrintToStdOut(const char *strFormat, ...);
ICECCallbacks g_callbacks;
libcec_configuration g_config;
int g_cecLogLevel(-1);
int g_cecDefaultLogLevel(CEC_LOG_ALL);
std::ofstream g_logOutput;
bool g_bShortLog(false);
std::string g_strPort;
bool g_bSingleCommand(false);
volatile sig_atomic_t g_bExit(0);
bool g_bHardExit(false);
CMutex g_outputMutex;
ICECAdapter* g_parser;
#if defined(HAVE_CURSES_API)
bool g_cursesEnable(false);
CCursesControl g_cursesControl("1", "0");
#endif
class CReconnect : public P8PLATFORM::CThread
{
public:
static CReconnect& Get(void)
{
static CReconnect _instance;
return _instance;
}
virtual ~CReconnect(void) {}
void* Process(void)
{
if (g_parser)
{
g_parser->Close();
if (!g_parser->Open(g_strPort.c_str()))
{
PrintToStdOut("Failed to reconnect\n");
g_bExit = 1;
}
}
return NULL;
}
private:
CReconnect(void) {}
};
static void PrintToStdOut(const char *strFormat, ...)
{
std::string strLog;
va_list argList;
va_start(argList, strFormat);
strLog = StringUtils::FormatV(strFormat, argList);
va_end(argList);
CLockObject lock(g_outputMutex);
std::cout << strLog << std::endl;
}
inline bool HexStrToInt(const std::string& data, uint8_t& value)
{
int iTmp(0);
if (sscanf(data.c_str(), "%x", &iTmp) == 1)
{
if (iTmp > 256)
value = 255;
else if (iTmp < 0)
value = 0;
else
value = (uint8_t) iTmp;
return true;
}
return false;
}
//get the first word (separated by whitespace) from string data and place that in word
//then remove that word from string data
static bool GetWord(std::string& data, std::string& word)
{
std::stringstream datastream(data);
std::string end;
datastream >> word;
if (datastream.fail())
{
data.clear();
return false;
}
size_t pos = data.find(word) + word.length();
if (pos >= data.length())
{
data.clear();
return true;
}
data = data.substr(pos);
datastream.clear();
datastream.str(data);
datastream >> end;
if (datastream.fail())
data.clear();
return true;
}
static cec_logical_address GetAddressFromInput(std::string& arguments)
{
std::string strDev;
if (GetWord(arguments, strDev))
{
unsigned long iDev = strtoul(strDev.c_str(), NULL, 16);
if ((iDev >= CECDEVICE_TV) && (iDev <= CECDEVICE_BROADCAST))
return (cec_logical_address)iDev;
}
return CECDEVICE_UNKNOWN;
}
void CecLogMessage(void *UNUSED(cbParam), const cec_log_message* message)
{
if ((message->level & g_cecLogLevel) == message->level)
{
std::string strLevel;
switch (message->level)
{
case CEC_LOG_ERROR:
strLevel = "ERROR: ";
break;
case CEC_LOG_WARNING:
strLevel = "WARNING: ";
break;
case CEC_LOG_NOTICE:
strLevel = "NOTICE: ";
break;
case CEC_LOG_TRAFFIC:
strLevel = "TRAFFIC: ";
break;
case CEC_LOG_DEBUG:
strLevel = "DEBUG: ";
break;
default:
break;
}
std::string strFullLog;
strFullLog = StringUtils::Format("%s[%16lld]\t%s", strLevel.c_str(), message->time, message->message);
PrintToStdOut(strFullLog.c_str());
if (g_logOutput.is_open())
{
if (g_bShortLog)
g_logOutput << message->message << std::endl;
else
g_logOutput << strFullLog.c_str() << std::endl;
}
}
}
void CecKeyPress(void *UNUSED(cbParam), const cec_keypress* UNUSED(key))
{
}
void CecCommand(void *UNUSED(cbParam), const cec_command* UNUSED(command))
{
}
int CecCommandHandler(void *UNUSED(cbParam), const cec_command* UNUSED(command))
{
return 0;
}
void CecAlert(void *UNUSED(cbParam), const libcec_alert type, const libcec_parameter UNUSED(param))
{
switch (type)
{
case CEC_ALERT_CONNECTION_LOST:
if (!CReconnect::Get().IsRunning())
{
PrintToStdOut("Connection lost - trying to reconnect\n");
CReconnect::Get().CreateThread(false);
}
break;
default:
break;
}
}
void ListDevices(ICECAdapter *parser)
{
cec_adapter_descriptor devices[10];
std::string strMessage = StringUtils::Format("libCEC version: %s, %s",
parser->VersionToString(g_config.serverVersion).c_str(),
parser->GetLibInfo());
PrintToStdOut(strMessage.c_str());
int8_t iDevicesFound = parser->DetectAdapters(devices, 10, NULL);
if (iDevicesFound <= 0)
{
PrintToStdOut("Found devices: NONE");
}
else
{
PrintToStdOut("Found devices: %d\n", iDevicesFound);
for (int8_t iDevicePtr = 0; iDevicePtr < iDevicesFound; iDevicePtr++)
{
PrintToStdOut("device: %d", iDevicePtr + 1);
PrintToStdOut("com port: %s", devices[iDevicePtr].strComName);
PrintToStdOut("vendor id: %04x", devices[iDevicePtr].iVendorId);
PrintToStdOut("product id: %04x", devices[iDevicePtr].iProductId);
PrintToStdOut("firmware version: %d", devices[iDevicePtr].iFirmwareVersion);
if (devices[iDevicePtr].iFirmwareBuildDate != CEC_FW_BUILD_UNKNOWN)
{
time_t buildTime = (time_t)devices[iDevicePtr].iFirmwareBuildDate;
std::string strDeviceInfo;
strDeviceInfo = StringUtils::Format("firmware build date: %s", asctime(gmtime(&buildTime)));
strDeviceInfo = StringUtils::Left(strDeviceInfo, strDeviceInfo.length() > 1 ? (unsigned)(strDeviceInfo.length() - 1) : 0); // strip \n added by asctime
strDeviceInfo.append(" +0000");
PrintToStdOut(strDeviceInfo.c_str());
}
if (devices[iDevicePtr].adapterType != ADAPTERTYPE_UNKNOWN)
{
PrintToStdOut("type: %s", parser->ToString(devices[iDevicePtr].adapterType));
}
PrintToStdOut("");
}
}
}
void ShowHelpCommandLine(const char* strExec)
{
CLockObject lock(g_outputMutex);
std::cout << std::endl <<
strExec << " {-h|--help|-l|--list-devices|[COM PORT]}" << std::endl <<
std::endl <<
"parameters:" << std::endl <<
" -h --help Shows this help text" << std::endl <<
" -l --list-devices List all devices on this system" << std::endl <<
" -t --type {p|r|t|a} The device type to use. More than one is possible." << std::endl <<
" -p --port {int} The HDMI port to use as active source." << std::endl <<
" -b --base {int} The logical address of the device to which this " << std::endl <<
" adapter is connected." << std::endl <<
" -f --log-file {file} Writes all libCEC log message to a file" << std::endl <<
" -r --rom Read saved settings from the EEPROM" << std::endl <<
" -sf --short-log-file {file} Writes all libCEC log message without timestamps" << std::endl <<
" and log levels to a file." << std::endl <<
" -d --log-level {level} Sets the log level. See cectypes.h for values." << std::endl <<
" -s --single-command Execute a single command and exit. Does not power" << std::endl <<
" on devices on startup and power them off on exit." << std::endl <<
" -o --osd-name {osd name} Use a custom osd name." << std::endl <<
" -m --monitor Start a monitor-only client." << std::endl <<
" -i --info Shows information about how libCEC was compiled." << std::endl <<
" [COM PORT] The com port to connect to. If no COM" << std::endl <<
" port is given, the client tries to connect to the" << std::endl <<
" first device that is detected." << std::endl <<
std::endl <<
"Type 'h' or 'help' and press enter after starting the client to display all " << std::endl <<
"available commands" << std::endl;
}
void ShowHelpConsole(void)
{
CLockObject lock(g_outputMutex);
std::cout << std::endl <<
"================================================================================" << std::endl <<
"Available commands:" << std::endl <<
std::endl <<
"[tx] {bytes} transfer bytes over the CEC line." << std::endl <<
"[txn] {bytes} transfer bytes but don't wait for transmission ACK." << std::endl <<
"[on] {address} power on the device with the given logical address." << std::endl <<
"[standby] {address} put the device with the given address in standby mode." << std::endl <<
"[la] {logical address} change the logical address of the CEC adapter." << std::endl <<
"[p] {device} {port} change the HDMI port number of the CEC adapter." << std::endl <<
"[pa] {physical address} change the physical address of the CEC adapter." << std::endl <<
"[as] make the CEC adapter the active source." << std::endl <<
"[is] mark the CEC adapter as inactive source." << std::endl <<
"[osd] {addr} {string} set OSD message on the specified device." << std::endl <<
"[ver] {addr} get the CEC version of the specified device." << std::endl <<
"[ven] {addr} get the vendor ID of the specified device." << std::endl <<
"[lang] {addr} get the menu language of the specified device." << std::endl <<
"[pow] {addr} get the power status of the specified device." << std::endl <<
"[name] {addr} get the OSD name of the specified device." << std::endl <<
"[poll] {addr} poll the specified device." << std::endl <<
"[lad] lists active devices on the bus" << std::endl <<
"[ad] {addr} checks whether the specified device is active." << std::endl <<
"[at] {type} checks whether the specified device type is active." << std::endl <<
"[sp] {addr} makes the specified physical address active." << std::endl <<
"[spl] {addr} makes the specified logical address active." << std::endl <<
"[volup] send a volume up command to the amp if present" << std::endl <<
"[voldown] send a volume down command to the amp if present" << std::endl <<
"[mute] send a mute/unmute command to the amp if present" << std::endl <<
"[self] show the list of addresses controlled by libCEC" << std::endl <<
"[scan] scan the CEC bus and display device info" << std::endl <<
"[mon] {1|0} enable or disable CEC bus monitoring." << std::endl <<
"[log] {1 - 31} change the log level. see cectypes.h for values." << std::endl <<
"[ping] send a ping command to the CEC adapter." << std::endl <<
"[bl] to let the adapter enter the bootloader, to upgrade" << std::endl <<
" the flash rom." << std::endl <<
"[r] reconnect to the CEC adapter." << std::endl <<
"[h] or [help] show this help." << std::endl <<
"[q] or [quit] to quit the CEC test client and switch off all" << std::endl <<
" connected CEC devices." << std::endl <<
"================================================================================" << std::endl;
}
bool ProcessCommandSELF(ICECAdapter *parser, const std::string &command, std::string & UNUSED(arguments))
{
if (command == "self")
{
cec_logical_addresses addr = parser->GetLogicalAddresses();
std::string strOut = "Addresses controlled by libCEC: ";
bool bFirst(true);
for (uint8_t iPtr = 0; iPtr <= 15; iPtr++)
{
if (addr[iPtr])
{
strOut += StringUtils::Format((bFirst ? "%d%s" : ", %d%s"), iPtr, parser->IsActiveSource((cec_logical_address)iPtr) ? "*" : "");
bFirst = false;
}
}
PrintToStdOut(strOut.c_str());
return true;
}
return false;
}
bool ProcessCommandSP(ICECAdapter *parser, const std::string &command, std::string &arguments)
{
if (command == "sp")
{
std::string strAddress;
int iAddress;
if (GetWord(arguments, strAddress))
{
sscanf(strAddress.c_str(), "%x", &iAddress);
if (iAddress >= 0 && iAddress <= CEC_INVALID_PHYSICAL_ADDRESS)
parser->SetStreamPath((uint16_t)iAddress);
return true;
}
}
return false;
}
bool ProcessCommandSPL(ICECAdapter *parser, const std::string &command, std::string &arguments)
{
if (command == "spl")
{
cec_logical_address addr = GetAddressFromInput(arguments);
if ((addr != CECDEVICE_UNKNOWN) && (addr != CECDEVICE_BROADCAST))
{
parser->SetStreamPath(addr);
return true;
}
}
return false;
}
bool ProcessCommandTX(ICECAdapter *parser, const std::string &command, std::string &arguments)
{
if (command == "tx" || command == "txn")
{
std::string strvalue;
cec_command bytes = parser->CommandFromString(arguments.c_str());
if (command == "txn")
bytes.transmit_timeout = 0;
parser->Transmit(bytes);
return true;
}
return false;
}
bool ProcessCommandON(ICECAdapter *parser, const std::string &command, std::string &arguments)
{
if (command == "on")
{
std::string strValue;
uint8_t iValue = 0;
if (GetWord(arguments, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF)
{
parser->PowerOnDevices((cec_logical_address) iValue);
return true;
}
else
{
PrintToStdOut("invalid destination");
}
}
return false;
}
bool ProcessCommandSTANDBY(ICECAdapter *parser, const std::string &command, std::string &arguments)
{
if (command == "standby")
{
std::string strValue;
uint8_t iValue = 0;
if (GetWord(arguments, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF)
{
parser->StandbyDevices((cec_logical_address) iValue);
return true;
}
else
{
PrintToStdOut("invalid destination");
}
}
return false;
}
bool ProcessCommandPOLL(ICECAdapter *parser, const std::string &command, std::string &arguments)
{
if (command == "poll")
{
std::string strValue;
uint8_t iValue = 0;
if (GetWord(arguments, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF)
{
if (parser->PollDevice((cec_logical_address) iValue))
PrintToStdOut("POLL message sent");
else
PrintToStdOut("POLL message not sent");
return true;
}
else
{
PrintToStdOut("invalid destination");
}
}
return false;
}
bool ProcessCommandLA(ICECAdapter *parser, const std::string &command, std::string &arguments)
{
if (command == "la")
{
cec_logical_address addr = GetAddressFromInput(arguments);
if ((addr != CECDEVICE_UNKNOWN) && (addr != CECDEVICE_BROADCAST))
{
parser->SetLogicalAddress(addr);
return true;
}
}
return false;
}
bool ProcessCommandP(ICECAdapter *parser, const std::string &command, std::string &arguments)
{
if (command == "p")
{
std::string strPort;
cec_logical_address addr = GetAddressFromInput(arguments);
if ((addr != CECDEVICE_UNKNOWN) && (addr != CECDEVICE_BROADCAST) &&
GetWord(arguments, strPort))
{
parser->SetHDMIPort(addr, (uint8_t)atoi(strPort.c_str()));
return true;
}
}
return false;
}
bool ProcessCommandPA(ICECAdapter *parser, const std::string &command, std::string &arguments)
{
if (command == "pa")
{
std::string strB1, strB2;
uint8_t iB1, iB2;
if (GetWord(arguments, strB1) && HexStrToInt(strB1, iB1) &&
GetWord(arguments, strB2) && HexStrToInt(strB2, iB2))
{
uint16_t iPhysicalAddress = ((uint16_t)iB1 << 8) + iB2;
parser->SetPhysicalAddress(iPhysicalAddress);
return true;
}
}
return false;
}
bool ProcessCommandOSD(ICECAdapter *parser, const std::string &command, std::string &arguments)
{
if (command == "osd")
{
bool bFirstWord(false);
std::string strAddr, strMessage, strWord;
uint8_t iAddr;
if (GetWord(arguments, strAddr) && HexStrToInt(strAddr, iAddr) && iAddr < 0xF)
{
while (GetWord(arguments, strWord))
{
if (bFirstWord)
{
bFirstWord = false;
strMessage.append(" ");
}
strMessage.append(strWord);
}
parser->SetOSDString((cec_logical_address) iAddr, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME, strMessage.c_str());
return true;
}
}
return false;
}
bool ProcessCommandAS(ICECAdapter *parser, const std::string &command, std::string & UNUSED(arguments))
{
if (command == "as")
{
parser->SetActiveSource();
// wait for the source switch to finish for 15 seconds tops
if (g_bSingleCommand)
{
CTimeout timeout(15000);
bool bActiveSource(false);
while (timeout.TimeLeft() > 0 && !bActiveSource)
{
bActiveSource = parser->IsLibCECActiveSource();
if (!bActiveSource)
CEvent::Sleep(100);
}
}
return true;
}
return false;
}
bool ProcessCommandIS(ICECAdapter *parser, const std::string &command, std::string & UNUSED(arguments))
{
if (command == "is")
return parser->SetInactiveView();
return false;
}
bool ProcessCommandPING(ICECAdapter *parser, const std::string &command, std::string & UNUSED(arguments))
{
if (command == "ping")
{
parser->PingAdapter();
return true;
}
return false;
}
bool ProcessCommandVOLUP(ICECAdapter *parser, const std::string &command, std::string & UNUSED(arguments))
{
if (command == "volup")
{
PrintToStdOut("volume up: %2X", parser->VolumeUp());
return true;
}
return false;
}
bool ProcessCommandVOLDOWN(ICECAdapter *parser, const std::string &command, std::string & UNUSED(arguments))
{
if (command == "voldown")
{
PrintToStdOut("volume down: %2X", parser->VolumeDown());
return true;
}
return false;
}
bool ProcessCommandMUTE(ICECAdapter *parser, const std::string &command, std::string & UNUSED(arguments))
{
if (command == "mute")
{
PrintToStdOut("mute: %2X", parser->AudioToggleMute());
return true;
}
return false;
}
bool ProcessCommandMON(ICECAdapter *parser, const std::string &command, std::string &arguments)
{
if (command == "mon")
{
std::string strEnable;
if (GetWord(arguments, strEnable) && (strEnable == "0" || strEnable == "1"))
{
parser->SwitchMonitoring(strEnable == "1");
return true;
}
}
return false;
}
bool ProcessCommandBL(ICECAdapter *parser, const std::string &command, std::string & UNUSED(arguments))
{
if (command == "bl")
{
if (parser->StartBootloader())
{
PrintToStdOut("entered bootloader mode. exiting cec-client");
g_bExit = 1;
g_bHardExit = true;
}
return true;
}
return false;
}
bool ProcessCommandLANG(ICECAdapter *parser, const std::string &command, std::string &arguments)
{
if (command == "lang")
{
cec_logical_address addr = GetAddressFromInput(arguments);
if ((addr != CECDEVICE_UNKNOWN) && (addr != CECDEVICE_BROADCAST))
{
std::string strLog;
strLog = StringUtils::Format("menu language '%s'", parser->GetDeviceMenuLanguage(addr).c_str());
PrintToStdOut(strLog.c_str());
return true;
}
}
return false;
}
bool ProcessCommandVEN(ICECAdapter *parser, const std::string &command, std::string &arguments)
{
if (command == "ven")
{
cec_logical_address addr = GetAddressFromInput(arguments);
if ((addr != CECDEVICE_UNKNOWN) && (addr != CECDEVICE_BROADCAST))
{
uint64_t iVendor = parser->GetDeviceVendorId(addr);
PrintToStdOut("vendor id: %06llx", iVendor);
return true;
}
}
return false;
}
bool ProcessCommandVER(ICECAdapter *parser, const std::string &command, std::string &arguments)
{
if (command == "ver")
{
cec_logical_address addr = GetAddressFromInput(arguments);
if ((addr != CECDEVICE_UNKNOWN) && (addr != CECDEVICE_BROADCAST))
{
cec_version iVersion = parser->GetDeviceCecVersion(addr);
PrintToStdOut("CEC version %s", parser->ToString(iVersion));
return true;
}
}
return false;
}
bool ProcessCommandPOW(ICECAdapter *parser, const std::string &command, std::string &arguments)
{
if (command == "pow")
{
cec_logical_address addr = GetAddressFromInput(arguments);
if ((addr != CECDEVICE_UNKNOWN) && (addr != CECDEVICE_BROADCAST))
{
cec_power_status iPower = parser->GetDevicePowerStatus(addr);
PrintToStdOut("power status: %s", parser->ToString(iPower));
return true;
}
}
return false;
}
bool ProcessCommandNAME(ICECAdapter *parser, const std::string &command, std::string &arguments)
{
if (command == "name")
{
cec_logical_address addr = GetAddressFromInput(arguments);
if ((addr != CECDEVICE_UNKNOWN) && (addr != CECDEVICE_BROADCAST))
{
std::string name = parser->GetDeviceOSDName(addr);
PrintToStdOut("OSD name of device %d is '%s'", addr, name.c_str());
return true;
}
}
return false;
}
bool ProcessCommandLAD(ICECAdapter *parser, const std::string &command, std::string & UNUSED(arguments))
{
if (command == "lad")
{
PrintToStdOut("listing active devices:");
cec_logical_addresses addresses = parser->GetActiveDevices();
for (uint8_t iPtr = 0; iPtr <= 11; iPtr++)
if (addresses[iPtr])
{
PrintToStdOut("logical address %X", (int)iPtr);
}
return true;
}
return false;
}
bool ProcessCommandAD(ICECAdapter *parser, const std::string &command, std::string &arguments)
{
if (command == "ad")
{
cec_logical_address addr = GetAddressFromInput(arguments);
if ((addr != CECDEVICE_UNKNOWN) && (addr != CECDEVICE_BROADCAST))
{
PrintToStdOut("logical address %X is %s", addr,
(parser->IsActiveDevice(addr) ? "active" : "not active"));
}
}
return false;
}
bool ProcessCommandAT(ICECAdapter *parser, const std::string &command, std::string &arguments)
{
if (command == "at")
{
std::string strType;
if (GetWord(arguments, strType))
{
cec_device_type type = CEC_DEVICE_TYPE_TV;
if (strType == "a")
type = CEC_DEVICE_TYPE_AUDIO_SYSTEM;
else if (strType == "p")
type = CEC_DEVICE_TYPE_PLAYBACK_DEVICE;
else if (strType == "r")
type = CEC_DEVICE_TYPE_RECORDING_DEVICE;
else if (strType == "t")
type = CEC_DEVICE_TYPE_TUNER;
PrintToStdOut("device %d is %s", type, (parser->IsActiveDeviceType(type) ? "active" : "not active"));
return true;
}
}
return false;
}
bool ProcessCommandR(ICECAdapter *parser, const std::string &command, std::string & UNUSED(arguments))
{
if (command == "r")
{
bool bReactivate = parser->IsLibCECActiveSource();
PrintToStdOut("closing the connection");
parser->Close();
PrintToStdOut("opening a new connection");
parser->Open(g_strPort.c_str());
if (bReactivate)
{
PrintToStdOut("setting active source");
parser->SetActiveSource();
}
return true;
}
return false;
}
bool ProcessCommandH(ICECAdapter * UNUSED(parser), const std::string &command, std::string & UNUSED(arguments))
{
if (command == "h" || command == "help")
{
ShowHelpConsole();
return true;
}
return false;
}
bool ProcessCommandLOG(ICECAdapter * UNUSED(parser), const std::string &command, std::string &arguments)
{
if (command == "log")
{
std::string strLevel;
if (GetWord(arguments, strLevel))
{
int iNewLevel = atoi(strLevel.c_str());
if (iNewLevel >= CEC_LOG_ERROR && iNewLevel <= CEC_LOG_ALL)
{
g_cecLogLevel = iNewLevel;
PrintToStdOut("log level changed to %s", strLevel.c_str());
return true;
}
}
}
return false;
}
bool ProcessCommandSCAN(ICECAdapter *parser, const std::string &command, std::string & UNUSED(arguments))
{
if (command == "scan")
{
std::string strLog;
PrintToStdOut("requesting CEC bus information ...");
strLog.append("CEC bus information\n===================\n");
cec_logical_addresses addresses = parser->GetActiveDevices();
cec_logical_address activeSource = parser->GetActiveSource();
for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
{
if (addresses[iPtr])
{
uint64_t iVendorId = parser->GetDeviceVendorId((cec_logical_address)iPtr);
uint16_t iPhysicalAddress = parser->GetDevicePhysicalAddress((cec_logical_address)iPtr);
bool bActive = parser->IsActiveSource((cec_logical_address)iPtr);
cec_version iCecVersion = parser->GetDeviceCecVersion((cec_logical_address)iPtr);
cec_power_status power = parser->GetDevicePowerStatus((cec_logical_address)iPtr);
std::string osdName = parser->GetDeviceOSDName((cec_logical_address)iPtr);
std::string strAddr;
strAddr = StringUtils::Format("%x.%x.%x.%x", (iPhysicalAddress >> 12) & 0xF, (iPhysicalAddress >> 8) & 0xF, (iPhysicalAddress >> 4) & 0xF, iPhysicalAddress & 0xF);
std::string lang = parser->GetDeviceMenuLanguage((cec_logical_address)iPtr);
strLog += StringUtils::Format("device #%X: %s\n", (int)iPtr, parser->ToString((cec_logical_address)iPtr));
strLog += StringUtils::Format("address: %s\n", strAddr.c_str());
strLog += StringUtils::Format("active source: %s\n", (bActive ? "yes" : "no"));
strLog += StringUtils::Format("vendor: %s\n", parser->ToString((cec_vendor_id)iVendorId));
strLog += StringUtils::Format("osd string: %s\n", osdName.c_str());
strLog += StringUtils::Format("CEC version: %s\n", parser->ToString(iCecVersion));
strLog += StringUtils::Format("power status: %s\n", parser->ToString(power));
strLog += StringUtils::Format("language: %s\n", lang.c_str());
strLog.append("\n\n");
}
}
activeSource = parser->GetActiveSource();
strLog += StringUtils::Format("currently active source: %s (%d)", parser->ToString(activeSource), (int)activeSource);
PrintToStdOut(strLog.c_str());
return true;
}
return false;
}
#if CEC_LIB_VERSION_MAJOR >= 5
bool ProcessCommandSTATS(ICECAdapter *parser, const std::string &command, std::string & UNUSED(arguments))
{
if (command == "stats")
{
cec_adapter_stats stats;
if (parser->GetStats(&stats))
{
std::string strLog;
strLog += StringUtils::Format("tx acked: %u\n", stats.tx_ack);
strLog += StringUtils::Format("tx nacked: %u\n", stats.tx_nack);
strLog += StringUtils::Format("tx error: %u\n", stats.tx_error);
strLog += StringUtils::Format("rx total: %u\n", stats.rx_total);
strLog += StringUtils::Format("rx error: %u\n", stats.rx_error);
PrintToStdOut(strLog.c_str());
}
else
{
PrintToStdOut("not supported\n");
}
return true;
}
return false;
}
#endif
bool ProcessConsoleCommand(ICECAdapter *parser, std::string &input)
{
if (!input.empty())
{
std::string command;
if (GetWord(input, command))
{
if (command == "q" || command == "quit")
return false;
ProcessCommandTX(parser, command, input) ||
ProcessCommandON(parser, command, input) ||
ProcessCommandSTANDBY(parser, command, input) ||
ProcessCommandPOLL(parser, command, input) ||
ProcessCommandLA(parser, command, input) ||
ProcessCommandP(parser, command, input) ||
ProcessCommandPA(parser, command, input) ||
ProcessCommandAS(parser, command, input) ||
ProcessCommandIS(parser, command, input) ||
ProcessCommandOSD(parser, command, input) ||
ProcessCommandPING(parser, command, input) ||
ProcessCommandVOLUP(parser, command, input) ||
ProcessCommandVOLDOWN(parser, command, input) ||
ProcessCommandMUTE(parser, command, input) ||
ProcessCommandMON(parser, command, input) ||
ProcessCommandBL(parser, command, input) ||
ProcessCommandLANG(parser, command, input) ||
ProcessCommandVEN(parser, command, input) ||
ProcessCommandVER(parser, command, input) ||
ProcessCommandPOW(parser, command, input) ||
ProcessCommandNAME(parser, command, input) ||
ProcessCommandLAD(parser, command, input) ||
ProcessCommandAD(parser, command, input) ||
ProcessCommandAT(parser, command, input) ||
ProcessCommandR(parser, command, input) ||
ProcessCommandH(parser, command, input) ||
ProcessCommandLOG(parser, command, input) ||
ProcessCommandSCAN(parser, command, input) ||
ProcessCommandSP(parser, command, input) ||
ProcessCommandSPL(parser, command, input) ||
ProcessCommandSELF(parser, command, input)
#if CEC_LIB_VERSION_MAJOR >= 5
|| ProcessCommandSTATS(parser, command, input)
#endif
;
}
}