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