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