Commit | Line | Data |
---|---|---|
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> |
acec5f48 LOK |
41 | #include "../lib/platform/threads.h" |
42 | #include "../lib/util/StdString.h" | |
abbca718 LOK |
43 | |
44 | using namespace CEC; | |
45 | using namespace std; | |
46 | ||
d75be19b | 47 | #define CEC_TEST_CLIENT_VERSION 1 |
abbca718 | 48 | |
761dcc45 | 49 | #include <cecloader.h> |
8bc45476 | 50 | |
3d0dca16 LOK |
51 | int g_cecLogLevel = CEC_LOG_ALL; |
52 | ofstream g_logOutput; | |
53 | bool g_bShortLog = false; | |
54 | CStdString g_strPort; | |
b9187cc6 | 55 | |
8bca69de | 56 | inline bool HexStrToInt(const std::string& data, uint8_t& value) |
b9187cc6 | 57 | { |
8bca69de LOK |
58 | int iTmp(0); |
59 | if (sscanf(data.c_str(), "%x", &iTmp) == 1) | |
60 | { | |
61 | if (iTmp > 256) | |
62 | value = 255; | |
63 | else if (iTmp < 0) | |
64 | value = 0; | |
65 | else | |
66 | value = (uint8_t) iTmp; | |
b9187cc6 | 67 | |
8bca69de LOK |
68 | return true; |
69 | } | |
70 | ||
71 | return false; | |
72 | } | |
b9187cc6 LOK |
73 | |
74 | //get the first word (separated by whitespace) from string data and place that in word | |
75 | //then remove that word from string data | |
76 | bool GetWord(string& data, string& word) | |
77 | { | |
78 | stringstream datastream(data); | |
79 | string end; | |
80 | ||
81 | datastream >> word; | |
82 | if (datastream.fail()) | |
83 | { | |
84 | data.clear(); | |
85 | return false; | |
86 | } | |
87 | ||
88 | size_t pos = data.find(word) + word.length(); | |
89 | ||
90 | if (pos >= data.length()) | |
91 | { | |
92 | data.clear(); | |
93 | return true; | |
94 | } | |
95 | ||
96 | data = data.substr(pos); | |
97 | ||
98 | datastream.clear(); | |
99 | datastream.str(data); | |
100 | ||
101 | datastream >> end; | |
102 | if (datastream.fail()) | |
103 | data.clear(); | |
104 | ||
105 | return true; | |
106 | } | |
107 | ||
d5f66c28 | 108 | void FlushLog(ICECAdapter *cecParser) |
abbca718 LOK |
109 | { |
110 | cec_log_message message; | |
111 | while (cecParser && cecParser->GetNextLogMessage(&message)) | |
112 | { | |
bdd433cb | 113 | if ((message.level & g_cecLogLevel) == message.level) |
abbca718 | 114 | { |
e6d2161b | 115 | CStdString strLevel; |
bdd433cb LOK |
116 | switch (message.level) |
117 | { | |
118 | case CEC_LOG_ERROR: | |
e6d2161b | 119 | strLevel = "ERROR: "; |
bdd433cb LOK |
120 | break; |
121 | case CEC_LOG_WARNING: | |
e6d2161b | 122 | strLevel = "WARNING: "; |
bdd433cb LOK |
123 | break; |
124 | case CEC_LOG_NOTICE: | |
e6d2161b | 125 | strLevel = "NOTICE: "; |
bdd433cb LOK |
126 | break; |
127 | case CEC_LOG_TRAFFIC: | |
e6d2161b | 128 | strLevel = "TRAFFIC: "; |
bdd433cb LOK |
129 | break; |
130 | case CEC_LOG_DEBUG: | |
e6d2161b | 131 | strLevel = "DEBUG: "; |
bdd433cb LOK |
132 | break; |
133 | default: | |
134 | break; | |
135 | } | |
40339852 | 136 | |
8bc45476 LOK |
137 | CStdString strFullLog; |
138 | strFullLog.Format("%s[%16lld]\t%s", strLevel.c_str(), message.time, message.message); | |
139 | cout << strFullLog.c_str() << endl; | |
e6d2161b LOK |
140 | |
141 | if (g_logOutput.is_open()) | |
8bc45476 LOK |
142 | { |
143 | if (g_bShortLog) | |
144 | g_logOutput << message.message << endl; | |
145 | else | |
146 | g_logOutput << strFullLog.c_str() << endl; | |
147 | } | |
bdd433cb | 148 | } |
abbca718 LOK |
149 | } |
150 | } | |
151 | ||
d5f66c28 | 152 | void ListDevices(ICECAdapter *parser) |
abbca718 | 153 | { |
25701fa6 LOK |
154 | cec_adapter *devices = new cec_adapter[10]; |
155 | uint8_t iDevicesFound = parser->FindAdapters(devices, 10, NULL); | |
abbca718 LOK |
156 | if (iDevicesFound <= 0) |
157 | { | |
25701fa6 | 158 | cout << "Found devices: NONE" << endl; |
abbca718 LOK |
159 | } |
160 | else | |
161 | { | |
25701fa6 LOK |
162 | CStdString strLog; |
163 | strLog.Format("Found devices: %d", iDevicesFound); | |
164 | cout << strLog.c_str() << endl; | |
165 | for (unsigned int iDevicePtr = 0; iDevicePtr < iDevicesFound; iDevicePtr++) | |
abbca718 LOK |
166 | { |
167 | CStdString strDevice; | |
25701fa6 | 168 | strDevice.Format("device: %d\npath: %s\ncom port: %s", iDevicePtr + 1, devices[iDevicePtr].path, devices[iDevicePtr].comm); |
abbca718 LOK |
169 | cout << endl << strDevice.c_str() << endl; |
170 | } | |
171 | } | |
172 | } | |
173 | ||
d5f66c28 | 174 | void ShowHelpCommandLine(const char* strExec) |
abbca718 LOK |
175 | { |
176 | cout << endl << | |
177 | strExec << " {-h|--help|-l|--list-devices|[COM PORT]}" << endl << | |
178 | endl << | |
179 | "parameters:" << endl << | |
8bc45476 LOK |
180 | " -h --help Shows this help text" << endl << |
181 | " -l --list-devices List all devices on this system" << endl << | |
f8513317 | 182 | " -t --type {p|r|t|a} The device type to use. More than one is possible." << endl << |
16b1e052 | 183 | " -p --port {int} The HDMI port to use as active source." << endl << |
8bc45476 LOK |
184 | " -f --log-file {file} Writes all libCEC log message to a file" << endl << |
185 | " -sf --short-log-file {file} Writes all libCEC log message without timestamps" << endl << | |
186 | " and log levels to a file." << endl << | |
606034b6 | 187 | " -d --log-level {level} Sets the log level. See cectypes.h for values." << endl << |
5ad0ea13 LOK |
188 | " -s --single-command Execute a single command and exit. Does not power" << endl << |
189 | " on devices on startup and power them off on exit." << endl << | |
8bc45476 LOK |
190 | " [COM PORT] The com port to connect to. If no COM" << endl << |
191 | " port is given, the client tries to connect to the" << endl << | |
192 | " first device that is detected." << endl << | |
825ddb96 | 193 | endl << |
8bc45476 LOK |
194 | "Type 'h' or 'help' and press enter after starting the client to display all " << endl << |
195 | "available commands" << endl; | |
825ddb96 LOK |
196 | } |
197 | ||
d5f66c28 | 198 | ICECAdapter *CreateParser(cec_device_type_list typeList) |
f8513317 LOK |
199 | { |
200 | ICECAdapter *parser = LibCecInit("CECTester", typeList); | |
201 | if (!parser || parser->GetMinLibVersion() > CEC_TEST_CLIENT_VERSION) | |
202 | { | |
203 | #ifdef __WINDOWS__ | |
204 | cout << "Cannot load libcec.dll" << endl; | |
205 | #else | |
206 | cout << "Cannot load libcec.so" << endl; | |
207 | #endif | |
208 | return NULL; | |
209 | } | |
210 | ||
211 | CStdString strLog; | |
212 | strLog.Format("CEC Parser created - libcec version %d.%d", parser->GetLibVersionMajor(), parser->GetLibVersionMinor()); | |
213 | cout << strLog.c_str() << endl; | |
214 | ||
215 | return parser; | |
216 | } | |
217 | ||
d5f66c28 | 218 | void ShowHelpConsole(void) |
825ddb96 LOK |
219 | { |
220 | cout << endl << | |
221 | "================================================================================" << endl << | |
222 | "Available commands:" << endl << | |
223 | endl << | |
7e793bdd LOK |
224 | "[tx] {bytes} transfer bytes over the CEC line." << endl << |
225 | "[txn] {bytes} transfer bytes but don't wait for transmission ACK." << endl << | |
226 | "[on] {address} power on the device with the given logical address." << endl << | |
227 | "[standby] {address} put the device with the given address in standby mode." << endl << | |
228 | "[la] {logical address} change the logical address of the CEC adapter." << endl << | |
229 | "[p] {port number} change the HDMI port number of the CEC adapter." << endl << | |
230 | "[pa] {physical address} change the physical address of the CEC adapter." << endl << | |
231 | "[osd] {addr} {string} set OSD message on the specified device." << endl << | |
232 | "[ver] {addr} get the CEC version of the specified device." << endl << | |
233 | "[ven] {addr} get the vendor ID of the specified device." << endl << | |
234 | "[lang] {addr} get the menu language of the specified device." << endl << | |
235 | "[pow] {addr} get the power status of the specified device." << endl << | |
ed21be2a | 236 | "[name] {addr} get the OSD name of the specified device." << endl << |
7e793bdd LOK |
237 | "[poll] {addr} poll the specified device." << endl << |
238 | "[lad] lists active devices on the bus" << endl << | |
239 | "[ad] {addr} checks whether the specified device is active." << endl << | |
240 | "[at] {type} checks whether the specified device type is active." << endl << | |
241 | "[volup] send a volume up command to the amp if present" << endl << | |
242 | "[voldown] send a volume down command to the amp if present" << endl << | |
243 | "[mute] send a mute/unmute command to the amp if present" << endl << | |
8b7e5ff6 | 244 | "[mon] {1|0} enable or disable CEC bus monitoring." << endl << |
bdd433cb | 245 | "[log] {1 - 31} change the log level. see cectypes.h for values." << endl << |
825ddb96 | 246 | "[ping] send a ping command to the CEC adapter." << endl << |
88f45c9b LOK |
247 | "[bl] to let the adapter enter the bootloader, to upgrade" << endl << |
248 | " the flash rom." << endl << | |
249 | "[r] reconnect to the CEC adapter." << endl << | |
825ddb96 | 250 | "[h] or [help] show this help." << endl << |
88f45c9b LOK |
251 | "[q] or [quit] to quit the CEC test client and switch off all" << endl << |
252 | " connected CEC devices." << endl << | |
825ddb96 | 253 | "================================================================================" << endl; |
abbca718 LOK |
254 | } |
255 | ||
256 | int main (int argc, char *argv[]) | |
257 | { | |
16b1e052 | 258 | int8_t iHDMIPort(-1); |
f8513317 | 259 | cec_device_type_list typeList; |
20b8870a | 260 | typeList.clear(); |
abbca718 | 261 | |
3d0dca16 | 262 | int iArgPtr = 1; |
5ad0ea13 | 263 | bool bSingleCommand(false); |
3d0dca16 | 264 | while (iArgPtr < argc) |
e6d2161b | 265 | { |
3d0dca16 | 266 | if (argc >= iArgPtr + 1) |
e6d2161b | 267 | { |
3d0dca16 LOK |
268 | if (!strcmp(argv[iArgPtr], "-f") || |
269 | !strcmp(argv[iArgPtr], "--log-file") || | |
270 | !strcmp(argv[iArgPtr], "-sf") || | |
271 | !strcmp(argv[iArgPtr], "--short-log-file")) | |
272 | { | |
273 | if (argc >= iArgPtr + 2) | |
274 | { | |
275 | g_logOutput.open(argv[iArgPtr + 1]); | |
276 | g_bShortLog = (!strcmp(argv[iArgPtr], "-sf") || !strcmp(argv[iArgPtr], "--short-log-file")); | |
277 | iArgPtr += 2; | |
278 | } | |
279 | else | |
280 | { | |
606034b6 LOK |
281 | cout << "== skipped log-file parameter: no file given ==" << endl; |
282 | ++iArgPtr; | |
283 | } | |
284 | } | |
285 | else if (!strcmp(argv[iArgPtr], "-d") || | |
286 | !strcmp(argv[iArgPtr], "--log-level")) | |
287 | { | |
288 | if (argc >= iArgPtr + 2) | |
289 | { | |
290 | int iNewLevel = atoi(argv[iArgPtr + 1]); | |
291 | if (iNewLevel >= CEC_LOG_ERROR && iNewLevel <= CEC_LOG_ALL) | |
292 | { | |
293 | g_cecLogLevel = iNewLevel; | |
5ad0ea13 LOK |
294 | if (!bSingleCommand) |
295 | cout << "log level set to " << argv[iArgPtr + 1] << endl; | |
606034b6 LOK |
296 | } |
297 | else | |
298 | { | |
299 | cout << "== skipped log-level parameter: invalid level '" << argv[iArgPtr + 1] << "' ==" << endl; | |
300 | } | |
301 | iArgPtr += 2; | |
302 | } | |
303 | else | |
304 | { | |
305 | cout << "== skipped log-level parameter: no level given ==" << endl; | |
3d0dca16 LOK |
306 | ++iArgPtr; |
307 | } | |
308 | } | |
f8513317 LOK |
309 | else if (!strcmp(argv[iArgPtr], "-t") || |
310 | !strcmp(argv[iArgPtr], "--type")) | |
d6cc5f60 LOK |
311 | { |
312 | if (argc >= iArgPtr + 2) | |
313 | { | |
f8513317 LOK |
314 | if (!strcmp(argv[iArgPtr + 1], "p")) |
315 | { | |
5ad0ea13 LOK |
316 | if (!bSingleCommand) |
317 | cout << "== using device type 'playback device'" << endl; | |
20b8870a | 318 | typeList.add(CEC_DEVICE_TYPE_PLAYBACK_DEVICE); |
f8513317 LOK |
319 | } |
320 | else if (!strcmp(argv[iArgPtr + 1], "r")) | |
d6cc5f60 | 321 | { |
5ad0ea13 LOK |
322 | if (!bSingleCommand) |
323 | cout << "== using device type 'recording device'" << endl; | |
20b8870a | 324 | typeList.add(CEC_DEVICE_TYPE_RECORDING_DEVICE); |
f8513317 LOK |
325 | } |
326 | else if (!strcmp(argv[iArgPtr + 1], "t")) | |
327 | { | |
5ad0ea13 LOK |
328 | if (!bSingleCommand) |
329 | cout << "== using device type 'tuner'" << endl; | |
20b8870a | 330 | typeList.add(CEC_DEVICE_TYPE_TUNER); |
f8513317 LOK |
331 | } |
332 | else if (!strcmp(argv[iArgPtr + 1], "a")) | |
333 | { | |
5ad0ea13 LOK |
334 | if (!bSingleCommand) |
335 | cout << "== using device type 'audio system'" << endl; | |
20b8870a | 336 | typeList.add(CEC_DEVICE_TYPE_AUDIO_SYSTEM); |
d6cc5f60 LOK |
337 | } |
338 | else | |
339 | { | |
f8513317 | 340 | cout << "== skipped invalid device type '" << argv[iArgPtr + 1] << "'" << endl; |
d6cc5f60 | 341 | } |
d6cc5f60 LOK |
342 | ++iArgPtr; |
343 | } | |
f8513317 | 344 | ++iArgPtr; |
d6cc5f60 | 345 | } |
3d0dca16 LOK |
346 | else if (!strcmp(argv[iArgPtr], "--list-devices") || |
347 | !strcmp(argv[iArgPtr], "-l")) | |
348 | { | |
d5f66c28 | 349 | ICECAdapter *parser = CreateParser(typeList); |
f8513317 LOK |
350 | if (parser) |
351 | { | |
d5f66c28 | 352 | ListDevices(parser); |
f8513317 LOK |
353 | UnloadLibCec(parser); |
354 | } | |
3d0dca16 LOK |
355 | return 0; |
356 | } | |
5ad0ea13 LOK |
357 | else if (!strcmp(argv[iArgPtr], "--single-command") || |
358 | !strcmp(argv[iArgPtr], "-s")) | |
359 | { | |
360 | bSingleCommand = true; | |
361 | ++iArgPtr; | |
362 | } | |
3d0dca16 LOK |
363 | else if (!strcmp(argv[iArgPtr], "--help") || |
364 | !strcmp(argv[iArgPtr], "-h")) | |
365 | { | |
d5f66c28 | 366 | ShowHelpCommandLine(argv[0]); |
3d0dca16 LOK |
367 | return 0; |
368 | } | |
45de9d9f | 369 | else if (!strcmp(argv[iArgPtr], "-p") || |
16b1e052 | 370 | !strcmp(argv[iArgPtr], "--port")) |
45de9d9f LOK |
371 | { |
372 | if (argc >= iArgPtr + 2) | |
373 | { | |
988de7b9 | 374 | iHDMIPort = (int8_t)atoi(argv[iArgPtr + 1]); |
16b1e052 | 375 | cout << "using HDMI port '" << iHDMIPort << "'" << endl; |
45de9d9f LOK |
376 | ++iArgPtr; |
377 | } | |
378 | ++iArgPtr; | |
379 | } | |
3d0dca16 LOK |
380 | else |
381 | { | |
382 | g_strPort = argv[iArgPtr++]; | |
383 | } | |
e6d2161b LOK |
384 | } |
385 | } | |
386 | ||
ab1469a0 | 387 | if (typeList.IsEmpty()) |
f8513317 | 388 | { |
5ad0ea13 | 389 | if (!bSingleCommand) |
88e5de6f LOK |
390 | cout << "No device type given. Using 'recording device'" << endl; |
391 | typeList.add(CEC_DEVICE_TYPE_RECORDING_DEVICE); | |
f8513317 LOK |
392 | } |
393 | ||
394 | ICECAdapter *parser = LibCecInit("CECTester", typeList); | |
395 | if (!parser || parser->GetMinLibVersion() > CEC_TEST_CLIENT_VERSION) | |
396 | { | |
397 | #ifdef __WINDOWS__ | |
398 | cout << "Cannot load libcec.dll" << endl; | |
399 | #else | |
400 | cout << "Cannot load libcec.so" << endl; | |
401 | #endif | |
402 | return 1; | |
403 | } | |
f8513317 | 404 | |
5ad0ea13 LOK |
405 | if (!bSingleCommand) |
406 | { | |
407 | CStdString strLog; | |
408 | strLog.Format("CEC Parser created - libcec version %d.%d", parser->GetLibVersionMajor(), parser->GetLibVersionMinor()); | |
409 | cout << strLog.c_str() << endl; | |
410 | ||
411 | //make stdin non-blocking | |
412 | #ifndef __WINDOWS__ | |
413 | int flags = fcntl(0, F_GETFL, 0); | |
414 | flags |= O_NONBLOCK; | |
415 | fcntl(0, F_SETFL, flags); | |
416 | #endif | |
417 | } | |
f8513317 | 418 | |
3d0dca16 | 419 | if (g_strPort.IsEmpty()) |
abbca718 | 420 | { |
5ad0ea13 LOK |
421 | if (!bSingleCommand) |
422 | cout << "no serial port given. trying autodetect: "; | |
25701fa6 LOK |
423 | cec_adapter devices[10]; |
424 | uint8_t iDevicesFound = parser->FindAdapters(devices, 10, NULL); | |
abbca718 LOK |
425 | if (iDevicesFound <= 0) |
426 | { | |
5ad0ea13 LOK |
427 | if (bSingleCommand) |
428 | cout << "autodetect "; | |
abbca718 LOK |
429 | cout << "FAILED" << endl; |
430 | UnloadLibCec(parser); | |
431 | return 1; | |
432 | } | |
433 | else | |
434 | { | |
5ad0ea13 LOK |
435 | if (!bSingleCommand) |
436 | { | |
437 | cout << endl << " path: " << devices[0].path << endl << | |
438 | " com port: " << devices[0].comm << endl << endl; | |
439 | } | |
3d0dca16 | 440 | g_strPort = devices[0].comm; |
abbca718 LOK |
441 | } |
442 | } | |
e6d2161b | 443 | |
16b1e052 LOK |
444 | if (iHDMIPort > 0) |
445 | { | |
446 | parser->SetHDMIPort((uint8_t)iHDMIPort); | |
447 | FlushLog(parser); | |
448 | } | |
449 | ||
450 | cout << "scanning the CEC bus..." << endl; | |
451 | ||
3d0dca16 | 452 | if (!parser->Open(g_strPort.c_str())) |
abbca718 | 453 | { |
3d0dca16 | 454 | cout << "unable to open the device on port " << g_strPort << endl; |
d5f66c28 | 455 | FlushLog(parser); |
abbca718 LOK |
456 | UnloadLibCec(parser); |
457 | return 1; | |
458 | } | |
459 | ||
5ad0ea13 LOK |
460 | if (!bSingleCommand) |
461 | { | |
462 | cout << "cec device opened" << endl; | |
abbca718 | 463 | |
5ad0ea13 LOK |
464 | parser->PowerOnDevices(CECDEVICE_TV); |
465 | FlushLog(parser); | |
abbca718 | 466 | |
5ad0ea13 LOK |
467 | parser->SetActiveSource(); |
468 | FlushLog(parser); | |
469 | ||
470 | cout << "waiting for input" << endl; | |
471 | } | |
abbca718 LOK |
472 | |
473 | bool bContinue(true); | |
abbca718 LOK |
474 | while (bContinue) |
475 | { | |
5ad0ea13 LOK |
476 | if (bSingleCommand) |
477 | bContinue = false; | |
478 | ||
d5f66c28 | 479 | FlushLog(parser); |
abbca718 | 480 | |
b5b53c7d LOK |
481 | /* just ignore the command buffer and clear it */ |
482 | cec_command dummy; | |
483 | while (parser && parser->GetNextCommand(&dummy)) {} | |
484 | ||
abbca718 LOK |
485 | string input; |
486 | getline(cin, input); | |
487 | cin.clear(); | |
488 | ||
489 | if (!input.empty()) | |
490 | { | |
491 | string command; | |
492 | if (GetWord(input, command)) | |
493 | { | |
faa6a23b | 494 | if (command == "tx" || command == "txn") |
abbca718 LOK |
495 | { |
496 | string strvalue; | |
8bca69de | 497 | uint8_t ivalue; |
9dee1670 | 498 | cec_command bytes; |
ab1469a0 | 499 | bytes.Clear(); |
25701fa6 | 500 | |
abbca718 | 501 | while (GetWord(input, strvalue) && HexStrToInt(strvalue, ivalue)) |
ab1469a0 | 502 | bytes.PushBack(ivalue); |
abbca718 | 503 | |
8d84e2c0 | 504 | if (command == "txn") |
1f0e9e0f | 505 | bytes.transmit_timeout = 0; |
8d84e2c0 LOK |
506 | |
507 | parser->Transmit(bytes); | |
abbca718 | 508 | } |
88f45c9b LOK |
509 | else if (command == "on") |
510 | { | |
511 | string strValue; | |
512 | uint8_t iValue = 0; | |
513 | if (GetWord(input, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF) | |
514 | { | |
515 | parser->PowerOnDevices((cec_logical_address) iValue); | |
516 | } | |
517 | else | |
518 | { | |
519 | cout << "invalid destination" << endl; | |
520 | } | |
521 | } | |
522 | else if (command == "standby") | |
523 | { | |
524 | string strValue; | |
525 | uint8_t iValue = 0; | |
526 | if (GetWord(input, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF) | |
527 | { | |
528 | parser->StandbyDevices((cec_logical_address) iValue); | |
529 | } | |
530 | else | |
531 | { | |
532 | cout << "invalid destination" << endl; | |
533 | } | |
534 | } | |
57f45e6c LOK |
535 | else if (command == "poll") |
536 | { | |
537 | string strValue; | |
538 | uint8_t iValue = 0; | |
539 | if (GetWord(input, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF) | |
540 | { | |
541 | if (parser->PollDevice((cec_logical_address) iValue)) | |
542 | cout << "POLL message sent" << endl; | |
543 | else | |
544 | cout << "POLL message not sent" << endl; | |
545 | } | |
546 | else | |
547 | { | |
548 | cout << "invalid destination" << endl; | |
549 | } | |
550 | } | |
6dfe9213 LOK |
551 | else if (command == "la") |
552 | { | |
553 | string strvalue; | |
6dfe9213 LOK |
554 | if (GetWord(input, strvalue)) |
555 | { | |
556 | parser->SetLogicalAddress((cec_logical_address) atoi(strvalue.c_str())); | |
abbca718 LOK |
557 | } |
558 | } | |
16b1e052 LOK |
559 | else if (command == "p") |
560 | { | |
561 | string strvalue; | |
562 | if (GetWord(input, strvalue)) | |
563 | { | |
988de7b9 | 564 | parser->SetHDMIPort((uint8_t)atoi(strvalue.c_str())); |
16b1e052 LOK |
565 | } |
566 | } | |
a424ebac LOK |
567 | else if (command == "pa") |
568 | { | |
569 | string strB1, strB2; | |
570 | uint8_t iB1, iB2; | |
571 | if (GetWord(input, strB1) && HexStrToInt(strB1, iB1) && | |
572 | GetWord(input, strB2) && HexStrToInt(strB2, iB2)) | |
573 | { | |
574 | uint16_t iPhysicalAddress = ((uint16_t)iB1 << 8) + iB2; | |
575 | parser->SetPhysicalAddress(iPhysicalAddress); | |
576 | } | |
577 | } | |
1969b140 LOK |
578 | else if (command == "osd") |
579 | { | |
580 | bool bFirstWord(false); | |
581 | string strAddr, strMessage, strWord; | |
582 | uint8_t iAddr; | |
583 | if (GetWord(input, strAddr) && HexStrToInt(strAddr, iAddr) && iAddr < 0xF) | |
584 | { | |
585 | while (GetWord(input, strWord)) | |
586 | { | |
587 | if (bFirstWord) | |
588 | { | |
589 | bFirstWord = false; | |
590 | strMessage.append(" "); | |
591 | } | |
592 | strMessage.append(strWord); | |
593 | } | |
594 | parser->SetOSDString((cec_logical_address) iAddr, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME, strMessage.c_str()); | |
595 | } | |
596 | } | |
abbca718 LOK |
597 | else if (command == "ping") |
598 | { | |
2abe74eb | 599 | parser->PingAdapter(); |
abbca718 | 600 | } |
04e637f9 LOK |
601 | else if (command == "volup") |
602 | { | |
9f332fe2 LOK |
603 | CStdString strLog; |
604 | strLog.Format("volume up: %2X", parser->VolumeUp()); | |
605 | cout << strLog.c_str() << endl; | |
04e637f9 LOK |
606 | } |
607 | else if (command == "voldown") | |
608 | { | |
9f332fe2 LOK |
609 | CStdString strLog; |
610 | strLog.Format("volume up: %2X", parser->VolumeDown()); | |
611 | cout << strLog.c_str() << endl; | |
04e637f9 LOK |
612 | } |
613 | else if (command == "mute") | |
614 | { | |
9f332fe2 LOK |
615 | CStdString strLog; |
616 | strLog.Format("mute: %2X", parser->MuteAudio()); | |
617 | cout << strLog.c_str() << endl; | |
04e637f9 | 618 | } |
8b7e5ff6 LOK |
619 | else if (command == "mon") |
620 | { | |
621 | CStdString strEnable; | |
622 | if (GetWord(input, strEnable) && (strEnable.Equals("0") || strEnable.Equals("1"))) | |
623 | { | |
624 | parser->SwitchMonitoring(strEnable.Equals("1")); | |
625 | } | |
626 | } | |
abbca718 LOK |
627 | else if (command == "bl") |
628 | { | |
629 | parser->StartBootloader(); | |
630 | } | |
a3269a0a LOK |
631 | else if (command == "lang") |
632 | { | |
633 | CStdString strDev; | |
634 | if (GetWord(input, strDev)) | |
635 | { | |
636 | int iDev = atoi(strDev); | |
637 | if (iDev >= 0 && iDev < 15) | |
638 | { | |
639 | CStdString strLog; | |
640 | cec_menu_language language; | |
641 | if (parser->GetDeviceMenuLanguage((cec_logical_address) iDev, &language)) | |
642 | strLog.Format("menu language '%s'", language.language); | |
643 | else | |
644 | strLog = "failed!"; | |
645 | cout << strLog.c_str() << endl; | |
646 | } | |
647 | } | |
648 | } | |
44c74256 LOK |
649 | else if (command == "ven") |
650 | { | |
651 | CStdString strDev; | |
652 | if (GetWord(input, strDev)) | |
653 | { | |
654 | int iDev = atoi(strDev); | |
655 | if (iDev >= 0 && iDev < 15) | |
656 | { | |
657 | uint64_t iVendor = parser->GetDeviceVendorId((cec_logical_address) iDev); | |
658 | CStdString strLog; | |
659 | strLog.Format("vendor id: %06x", iVendor); | |
660 | cout << strLog.c_str() << endl; | |
661 | } | |
662 | } | |
663 | } | |
6a1c0009 LOK |
664 | else if (command == "ver") |
665 | { | |
666 | CStdString strDev; | |
667 | if (GetWord(input, strDev)) | |
668 | { | |
669 | int iDev = atoi(strDev); | |
670 | if (iDev >= 0 && iDev < 15) | |
671 | { | |
672 | cec_version iVersion = parser->GetDeviceCecVersion((cec_logical_address) iDev); | |
673 | switch (iVersion) | |
674 | { | |
675 | case CEC_VERSION_1_2: | |
676 | cout << "CEC version 1.2" << endl; | |
677 | break; | |
678 | case CEC_VERSION_1_2A: | |
679 | cout << "CEC version 1.2a" << endl; | |
680 | break; | |
681 | case CEC_VERSION_1_3: | |
682 | cout << "CEC version 1.3" << endl; | |
683 | break; | |
684 | case CEC_VERSION_1_3A: | |
685 | cout << "CEC version 1.3a" << endl; | |
686 | break; | |
687 | default: | |
688 | cout << "unknown CEC version" << endl; | |
689 | break; | |
690 | } | |
691 | } | |
692 | } | |
693 | } | |
e55f3f70 LOK |
694 | else if (command == "pow") |
695 | { | |
696 | CStdString strDev; | |
697 | if (GetWord(input, strDev)) | |
698 | { | |
699 | int iDev = atoi(strDev); | |
700 | if (iDev >= 0 && iDev < 15) | |
701 | { | |
702 | cec_power_status iPower = parser->GetDevicePowerStatus((cec_logical_address) iDev); | |
703 | switch (iPower) | |
704 | { | |
705 | case CEC_POWER_STATUS_ON: | |
706 | cout << "powered on" << endl; | |
707 | break; | |
708 | case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY: | |
709 | cout << "on -> standby" << endl; | |
710 | break; | |
711 | case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON: | |
712 | cout << "standby -> on" << endl; | |
713 | break; | |
714 | case CEC_POWER_STATUS_STANDBY: | |
715 | cout << "standby" << endl; | |
716 | break; | |
717 | default: | |
718 | cout << "unknown power status" << endl; | |
719 | break; | |
720 | } | |
721 | } | |
722 | } | |
723 | } | |
ed21be2a LOK |
724 | else if (command == "name") |
725 | { | |
726 | CStdString strDev; | |
727 | if (GetWord(input, strDev)) | |
728 | { | |
729 | int iDev = atoi(strDev); | |
730 | if (iDev >= 0 && iDev < 15) | |
731 | { | |
732 | cec_osd_name name = parser->GetOSDName((cec_logical_address)iDev); | |
733 | cout << "OSD name of device " << iDev << " is '" << name.name << "'" << endl; | |
734 | } | |
735 | } | |
736 | } | |
6d858ba4 LOK |
737 | else if (command == "lad") |
738 | { | |
739 | cout << "listing active devices:" << endl; | |
740 | cec_logical_addresses addresses = parser->GetActiveDevices(); | |
988de7b9 | 741 | for (uint8_t iPtr = 0; iPtr < 16; iPtr++) |
6d858ba4 LOK |
742 | if (addresses[iPtr]) |
743 | cout << "logical address " << iPtr << endl; | |
744 | } | |
745 | else if (command == "ad") | |
746 | { | |
747 | CStdString strDev; | |
748 | if (GetWord(input, strDev)) | |
749 | { | |
750 | int iDev = atoi(strDev); | |
751 | if (iDev >= 0 && iDev < 15) | |
752 | cout << "logical address " << iDev << " is " << (parser->IsActiveDevice((cec_logical_address)iDev) ? "active" : "not active") << endl; | |
753 | } | |
754 | } | |
755 | else if (command == "at") | |
756 | { | |
757 | CStdString strType; | |
758 | if (GetWord(input, strType)) | |
759 | { | |
760 | cec_device_type type = CEC_DEVICE_TYPE_TV; | |
761 | if (strType.Equals("a")) | |
762 | type = CEC_DEVICE_TYPE_AUDIO_SYSTEM; | |
763 | else if (strType.Equals("p")) | |
764 | type = CEC_DEVICE_TYPE_PLAYBACK_DEVICE; | |
765 | else if (strType.Equals("r")) | |
766 | type = CEC_DEVICE_TYPE_RECORDING_DEVICE; | |
767 | else if (strType.Equals("t")) | |
768 | type = CEC_DEVICE_TYPE_TUNER; | |
769 | cout << "device " << type << " is " << (parser->IsActiveDeviceType(type) ? "active" : "not active") << endl; | |
770 | } | |
771 | } | |
12027dbe LOK |
772 | else if (command == "r") |
773 | { | |
25701fa6 | 774 | cout << "closing the connection" << endl; |
12027dbe | 775 | parser->Close(); |
d5f66c28 | 776 | FlushLog(parser); |
25701fa6 LOK |
777 | |
778 | cout << "opening a new connection" << endl; | |
3d0dca16 | 779 | parser->Open(g_strPort.c_str()); |
d5f66c28 | 780 | FlushLog(parser); |
25701fa6 | 781 | |
18203d17 LOK |
782 | cout << "setting active source" << endl; |
783 | parser->SetActiveSource(); | |
12027dbe | 784 | } |
825ddb96 LOK |
785 | else if (command == "h" || command == "help") |
786 | { | |
d5f66c28 | 787 | ShowHelpConsole(); |
825ddb96 | 788 | } |
abbca718 LOK |
789 | else if (command == "q" || command == "quit") |
790 | { | |
791 | bContinue = false; | |
792 | } | |
bdd433cb LOK |
793 | else if (command == "log") |
794 | { | |
795 | CStdString strLevel; | |
796 | if (GetWord(input, strLevel)) | |
797 | { | |
798 | int iNewLevel = atoi(strLevel); | |
799 | if (iNewLevel >= CEC_LOG_ERROR && iNewLevel <= CEC_LOG_ALL) | |
800 | { | |
801 | g_cecLogLevel = iNewLevel; | |
802 | cout << "log level changed to " << strLevel.c_str() << endl; | |
803 | } | |
804 | } | |
805 | } | |
abbca718 LOK |
806 | } |
807 | if (bContinue) | |
808 | cout << "waiting for input" << endl; | |
809 | } | |
5ad0ea13 LOK |
810 | |
811 | if (bContinue) | |
812 | CCondition::Sleep(50); | |
abbca718 LOK |
813 | } |
814 | ||
5ad0ea13 LOK |
815 | if (!bSingleCommand) |
816 | parser->StandbyDevices(CECDEVICE_BROADCAST); | |
817 | ||
5f39c4d8 | 818 | parser->Close(); |
d5f66c28 | 819 | FlushLog(parser); |
abbca718 | 820 | UnloadLibCec(parser); |
e6d2161b LOK |
821 | |
822 | if (g_logOutput.is_open()) | |
823 | g_logOutput.close(); | |
824 | ||
abbca718 LOK |
825 | return 0; |
826 | } |