Commit | Line | Data |
---|---|---|
f57cba64 LOK |
1 | /* |
2 | * This file is part of the libCEC(R) library. | |
3 | * | |
4 | * libCEC(R) is Copyright (C) 2011-2012 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 | ||
33 | #include "../../include/cec.h" | |
34 | ||
35 | #include <cstdio> | |
36 | #include <fcntl.h> | |
37 | #include <iostream> | |
38 | #include <fstream> | |
39 | #include <string> | |
40 | #include <sstream> | |
41 | #include "../lib/platform/threads/mutex.h" | |
42 | #include "../lib/platform/util/timeutils.h" | |
43 | #include "../lib/implementations/CECCommandHandler.h" | |
44 | ||
45 | using namespace CEC; | |
46 | using namespace std; | |
47 | using namespace PLATFORM; | |
48 | ||
49 | #include <cecloader.h> | |
50 | ||
51 | CMutex g_outputMutex; | |
52 | ||
960f33c6 | 53 | CEvent g_responseEvent; |
f57cba64 LOK |
54 | cec_opcode g_lastCommand = CEC_OPCODE_NONE; |
55 | ||
960f33c6 | 56 | CEvent g_keyEvent; |
f57cba64 LOK |
57 | cec_user_control_code g_lastKey = CEC_USER_CONTROL_CODE_UNKNOWN; |
58 | ||
59 | ICECCallbacks g_callbacks; | |
c7dc6e28 | 60 | libcec_configuration g_config; |
f57cba64 | 61 | ICECAdapter * g_parser; |
f57cba64 LOK |
62 | |
63 | inline void PrintToStdOut(const char *strFormat, ...) | |
64 | { | |
65 | CStdString strLog; | |
66 | ||
67 | va_list argList; | |
68 | va_start(argList, strFormat); | |
69 | strLog.FormatV(strFormat, argList); | |
70 | va_end(argList); | |
71 | ||
72 | CLockObject lock(g_outputMutex); | |
73 | cout << strLog << endl; | |
74 | } | |
75 | ||
76 | //get the first word (separated by whitespace) from string data and place that in word | |
77 | //then remove that word from string data | |
78 | bool GetWord(string& data, string& word) | |
79 | { | |
80 | stringstream datastream(data); | |
81 | string end; | |
82 | ||
83 | datastream >> word; | |
84 | if (datastream.fail()) | |
85 | { | |
86 | data.clear(); | |
87 | return false; | |
88 | } | |
89 | ||
90 | size_t pos = data.find(word) + word.length(); | |
91 | ||
92 | if (pos >= data.length()) | |
93 | { | |
94 | data.clear(); | |
95 | return true; | |
96 | } | |
97 | ||
98 | data = data.substr(pos); | |
99 | ||
100 | datastream.clear(); | |
101 | datastream.str(data); | |
102 | ||
103 | datastream >> end; | |
104 | if (datastream.fail()) | |
105 | data.clear(); | |
106 | ||
107 | return true; | |
108 | } | |
109 | ||
110 | int CecLogMessage(void *UNUSED(cbParam), const cec_log_message &message) | |
111 | { | |
112 | switch (message.level) | |
113 | { | |
114 | case CEC_LOG_ERROR: | |
115 | PrintToStdOut("ERROR:\t%s", message.message); | |
116 | break; | |
117 | case CEC_LOG_WARNING: | |
118 | PrintToStdOut("ERROR:\t%s", message.message); | |
119 | break; | |
120 | default: | |
121 | break; | |
122 | } | |
123 | ||
124 | return 0; | |
125 | } | |
126 | ||
127 | int CecKeyPress(void *UNUSED(cbParam), const cec_keypress &key) | |
128 | { | |
f57cba64 | 129 | g_lastKey = key.keycode; |
960f33c6 | 130 | g_keyEvent.Signal(); |
f57cba64 LOK |
131 | return 0; |
132 | } | |
133 | ||
134 | int CecCommand(void *UNUSED(cbParam), const cec_command &command) | |
135 | { | |
f57cba64 | 136 | g_lastCommand = command.opcode; |
960f33c6 | 137 | g_responseEvent.Signal(); |
f57cba64 LOK |
138 | return 0; |
139 | } | |
140 | ||
f57cba64 LOK |
141 | bool ProcessConsoleCommand(string &input) |
142 | { | |
143 | if (!input.empty()) | |
144 | { | |
145 | string command; | |
146 | if (GetWord(input, command)) | |
147 | { | |
148 | if (command == "q" || command == "quit") | |
149 | return false; | |
150 | } | |
151 | } | |
152 | return true; | |
153 | } | |
154 | ||
155 | bool OpenConnection(cec_device_type type = CEC_DEVICE_TYPE_RECORDING_DEVICE) | |
156 | { | |
c7dc6e28 LOK |
157 | g_config.Clear(); |
158 | snprintf(g_config.strDeviceName, 13, "CEC-config"); | |
159 | g_config.callbackParam = NULL; | |
eb617f35 | 160 | g_config.clientVersion = (uint32_t)CEC_CLIENT_VERSION_1_6_2; |
c7dc6e28 LOK |
161 | g_callbacks.CBCecLogMessage = &CecLogMessage; |
162 | g_callbacks.CBCecKeyPress = &CecKeyPress; | |
163 | g_callbacks.CBCecCommand = &CecCommand; | |
164 | g_config.callbacks = &g_callbacks; | |
f57cba64 | 165 | |
c7dc6e28 LOK |
166 | g_config.deviceTypes.add(type); |
167 | ||
168 | g_parser = LibCecInitialise(&g_config); | |
f57cba64 LOK |
169 | if (!g_parser) |
170 | return false; | |
171 | ||
172 | CStdString strPort; | |
173 | cec_adapter devices[10]; | |
174 | uint8_t iDevicesFound = g_parser->FindAdapters(devices, 10, NULL); | |
175 | if (iDevicesFound <= 0) | |
176 | { | |
177 | PrintToStdOut("autodetect FAILED"); | |
178 | UnloadLibCec(g_parser); | |
179 | return false; | |
180 | } | |
181 | else | |
182 | { | |
183 | strPort = devices[0].comm; | |
184 | } | |
185 | ||
f57cba64 | 186 | PrintToStdOut("opening a connection to the CEC adapter..."); |
f57cba64 LOK |
187 | if (!g_parser->Open(strPort.c_str())) |
188 | { | |
189 | PrintToStdOut("unable to open the device on port %s", strPort.c_str()); | |
190 | UnloadLibCec(g_parser); | |
191 | return false; | |
192 | } | |
193 | ||
eb617f35 LOK |
194 | g_parser->GetCurrentConfiguration(&g_config); |
195 | PrintToStdOut("CEC Parser created - libCEC version %s", g_parser->ToString((cec_server_version)g_config.serverVersion)); | |
f57cba64 | 196 | |
f57cba64 LOK |
197 | return true; |
198 | } | |
199 | ||
200 | int8_t FindPhysicalAddressPortNumber(void) | |
201 | { | |
86393878 | 202 | PrintToStdOut("Enter the HDMI port number to which you connected your CEC adapter, followed by <enter>. Valid ports are in the range 1-15. Anything else will cancel this wizard."); |
f57cba64 LOK |
203 | string input; |
204 | getline(cin, input); | |
205 | cin.clear(); | |
86393878 | 206 | if (input.empty()) |
f57cba64 | 207 | return -1; |
86393878 | 208 | |
21d83978 LOK |
209 | int hdmiport = atoi(input.c_str()); |
210 | return (hdmiport < 1 || hdmiport > 15) ? -1 : (int8_t)hdmiport; | |
f57cba64 LOK |
211 | } |
212 | ||
213 | cec_logical_address FindPhysicalAddressBaseDevice(void) | |
214 | { | |
eb617f35 | 215 | PrintToStdOut("Press 1 if your CEC adapter is connected to your TV or\nPress 2 if it's connected to an AVR, followed by <enter>. Anything else will cancel this wizard."); |
f57cba64 LOK |
216 | |
217 | string input; | |
218 | getline(cin, input); | |
219 | cin.clear(); | |
220 | if (input.empty() || (input != "1" && input != "2")) | |
221 | { | |
222 | PrintToStdOut("Exiting..."); | |
223 | return CECDEVICE_UNKNOWN; | |
224 | } | |
225 | return (input == "2") ? | |
226 | CECDEVICE_AUDIOSYSTEM : | |
227 | CECDEVICE_TV; | |
228 | } | |
229 | ||
230 | uint16_t FindPhysicalAddress(void) | |
231 | { | |
232 | PrintToStdOut("=== Physical Address Configuration ===\n"); | |
b32ffd87 | 233 | uint16_t iAddress(CEC_INVALID_PHYSICAL_ADDRESS); |
f57cba64 LOK |
234 | |
235 | PrintToStdOut("Do you want to let libCEC try to autodetect the address (y/n)?"); | |
236 | string input; | |
237 | getline(cin, input); | |
238 | cin.clear(); | |
239 | if (input == "y" || input == "Y") | |
240 | { | |
241 | cec_logical_address baseDevice = FindPhysicalAddressBaseDevice(); | |
242 | if (baseDevice == CECDEVICE_UNKNOWN) | |
243 | return iAddress; | |
244 | ||
245 | int8_t iPortNumber = FindPhysicalAddressPortNumber(); | |
246 | if (iPortNumber == -1) | |
247 | return iAddress; | |
248 | ||
249 | PrintToStdOut("Trying to detect the physical address..."); | |
250 | if (!g_parser->SetHDMIPort(baseDevice, iPortNumber)) | |
251 | PrintToStdOut("Failed. Please enter the address manually, or restart this wizard and use different settings."); | |
252 | else | |
253 | { | |
eb617f35 LOK |
254 | g_parser->GetCurrentConfiguration(&g_config); |
255 | iAddress = g_parser->GetDevicePhysicalAddress(g_config.logicalAddresses.primary); | |
b32ffd87 | 256 | if (iAddress == 0 || iAddress == CEC_INVALID_PHYSICAL_ADDRESS) |
f57cba64 LOK |
257 | PrintToStdOut("Failed. Please enter the address manually, or restart this wizard and use different settings."); |
258 | } | |
259 | } | |
260 | ||
b32ffd87 | 261 | if (iAddress == 0 || iAddress == CEC_INVALID_PHYSICAL_ADDRESS) |
f57cba64 | 262 | { |
b32ffd87 | 263 | PrintToStdOut("Please enter the physical address (0001 - FFFE), followed by <enter>."); |
f57cba64 LOK |
264 | getline(cin, input); |
265 | cin.clear(); | |
266 | ||
267 | int iAddressTmp; | |
268 | if (sscanf(input.c_str(), "%x", &iAddressTmp) == 1) | |
269 | { | |
b32ffd87 LOK |
270 | if (iAddressTmp <= CEC_PHYSICAL_ADDRESS_TV || iAddressTmp > CEC_MAX_PHYSICAL_ADDRESS) |
271 | iAddressTmp = CEC_INVALID_PHYSICAL_ADDRESS; | |
f57cba64 LOK |
272 | iAddress = (uint16_t)iAddressTmp; |
273 | } | |
274 | } | |
275 | ||
9a5fc0e8 LOK |
276 | if (iAddress != 0) |
277 | { | |
278 | g_parser->SetPhysicalAddress(iAddress); | |
279 | g_parser->SetActiveSource(g_config.deviceTypes[0]); | |
280 | } | |
281 | ||
f57cba64 LOK |
282 | return iAddress; |
283 | } | |
284 | ||
9a5fc0e8 | 285 | bool PowerOnTV(uint64_t iTimeout = 60000) |
f57cba64 LOK |
286 | { |
287 | cec_power_status currentTvPower(CEC_POWER_STATUS_UNKNOWN); | |
288 | uint64_t iNow = GetTimeMs(); | |
289 | uint64_t iTarget = iNow + iTimeout; | |
f57cba64 | 290 | |
9a5fc0e8 | 291 | if (currentTvPower != CEC_POWER_STATUS_ON) |
f57cba64 LOK |
292 | { |
293 | currentTvPower = g_parser->GetDevicePowerStatus(CECDEVICE_TV); | |
294 | if (currentTvPower != CEC_POWER_STATUS_ON) | |
295 | { | |
9a5fc0e8 | 296 | PrintToStdOut("Sending 'power on' command to the TV\n=== Please wait ==="); |
f57cba64 LOK |
297 | g_parser->PowerOnDevices(CECDEVICE_TV); |
298 | while (iTarget > iNow) | |
299 | { | |
960f33c6 | 300 | g_responseEvent.Wait((uint32_t)(iTarget - iNow)); |
f57cba64 LOK |
301 | if (g_lastCommand == CEC_OPCODE_REQUEST_ACTIVE_SOURCE) |
302 | break; | |
303 | iNow = GetTimeMs(); | |
304 | } | |
305 | } | |
306 | } | |
307 | ||
308 | currentTvPower = g_parser->GetDevicePowerStatus(CECDEVICE_TV); | |
309 | ||
310 | if (currentTvPower != CEC_POWER_STATUS_ON) | |
311 | PrintToStdOut("Failed to power on the TV, or the TV does not respond properly"); | |
312 | ||
313 | return currentTvPower == CEC_POWER_STATUS_ON; | |
314 | } | |
315 | ||
39b1216c | 316 | int main (int UNUSED(argc), char *UNUSED(argv[])) |
f57cba64 LOK |
317 | { |
318 | PrintToStdOut("=== USB-CEC Adapter Configuration ===\n"); | |
319 | if (!OpenConnection()) | |
320 | return 1; | |
321 | ||
322 | if (!PowerOnTV()) | |
323 | return 1; | |
324 | ||
325 | bool bAddressOk(false); | |
f57cba64 LOK |
326 | while (!bAddressOk) |
327 | { | |
d40928b5 | 328 | uint16_t iAddress = FindPhysicalAddress(); |
f57cba64 LOK |
329 | |
330 | PrintToStdOut("Physical address: %4X", iAddress); | |
331 | PrintToStdOut("Is this correct (y/n)?"); | |
332 | string input; | |
333 | getline(cin, input); | |
334 | cin.clear(); | |
335 | bAddressOk = (input == "y" || input == "Y"); | |
336 | } | |
337 | ||
b0a7410c | 338 | g_parser->GetCurrentConfiguration(&g_config); |
caca2d81 | 339 | |
b0a7410c LOK |
340 | { |
341 | cec_menu_language lang; | |
342 | if (g_parser->GetDeviceMenuLanguage(CECDEVICE_TV, &lang)) | |
343 | { | |
344 | PrintToStdOut("TV menu language: %s", lang.language); | |
345 | PrintToStdOut("Do you want the application to use the menu language of the TV (y/n)?"); | |
346 | string input; | |
347 | getline(cin, input); | |
348 | cin.clear(); | |
9a5fc0e8 | 349 | g_config.bUseTVMenuLanguage = (input == "y" || input == "Y") ? 1 : 0; |
b0a7410c LOK |
350 | } |
351 | else | |
352 | { | |
353 | PrintToStdOut("The TV did not respond properly to the menu language request."); | |
354 | } | |
355 | } | |
356 | ||
b0a7410c | 357 | { |
ca27e6cf | 358 | PrintToStdOut("Do you want to make the CEC adapter the active source when starting the application (y/n)?"); |
b0a7410c LOK |
359 | string input; |
360 | getline(cin, input); | |
361 | cin.clear(); | |
ca27e6cf | 362 | g_config.bActivateSource = (input == "y" || input == "Y") ? 1 : 0; |
b0a7410c LOK |
363 | } |
364 | ||
eb617f35 LOK |
365 | { |
366 | PrintToStdOut("Do you want to power on the TV when starting the application (y/n)?"); | |
367 | string input; | |
368 | getline(cin, input); | |
369 | cin.clear(); | |
370 | if (input == "y" || input == "Y") | |
371 | g_config.wakeDevices.Set(CECDEVICE_TV); | |
372 | } | |
373 | ||
b0a7410c LOK |
374 | { |
375 | PrintToStdOut("Do you want to power off CEC devices when closing the application (y/n)?"); | |
376 | string input; | |
377 | getline(cin, input); | |
378 | cin.clear(); | |
ca27e6cf | 379 | if (input == "y" || input == "Y") |
eb617f35 | 380 | g_config.powerOffDevices.Set(CECDEVICE_TV); |
b0a7410c LOK |
381 | } |
382 | ||
b0a7410c LOK |
383 | { |
384 | PrintToStdOut("Do you want to power off CEC devices when the screensaver is activated (y/n)?"); | |
385 | string input; | |
386 | getline(cin, input); | |
387 | cin.clear(); | |
9a5fc0e8 | 388 | g_config.bPowerOffScreensaver = (input == "y" || input == "Y") ? 1 : 0; |
b0a7410c LOK |
389 | } |
390 | ||
b0a7410c LOK |
391 | { |
392 | PrintToStdOut("Do you want to put the PC in standby when the TV is put in standby mode (y/n)?"); | |
393 | string input; | |
394 | getline(cin, input); | |
395 | cin.clear(); | |
41e3372a LOK |
396 | g_config.bPowerOffOnStandby = (input == "y" || input == "Y") ? 1 : 0; |
397 | } | |
398 | ||
399 | { | |
400 | PrintToStdOut("Do you want to send an inactive source message when stopping the application (y/n)?"); | |
401 | string input; | |
402 | getline(cin, input); | |
403 | cin.clear(); | |
404 | g_config.bSendInactiveSource = (input == "y" || input == "Y") ? 1 : 0; | |
b0a7410c LOK |
405 | } |
406 | ||
407 | PrintToStdOut("\n\n=== USB-CEC Adapter Configuration Summary ==="); | |
408 | PrintToStdOut("HDMI port number: %d", g_config.iHDMIPort); | |
409 | PrintToStdOut("Connected to HDMI device: %X", (uint8_t)g_config.baseDevice); | |
410 | PrintToStdOut("Physical address: %4X", g_config.iPhysicalAddress); | |
9a5fc0e8 | 411 | PrintToStdOut("Use the TV's language setting: %s", g_config.bUseTVMenuLanguage ? "yes" : "no"); |
ca27e6cf | 412 | PrintToStdOut("Make the adapter the active source when starting XBMC: %s", g_config.bActivateSource ? "yes" : "no"); |
eb617f35 | 413 | PrintToStdOut("Power on the TV when starting XBMC: %s", g_config.wakeDevices.IsSet(CECDEVICE_BROADCAST) ? "yes" : "no"); |
ca27e6cf | 414 | PrintToStdOut("Power off devices when stopping XBMC: %s", g_config.powerOffDevices.IsSet(CECDEVICE_BROADCAST) ? "yes" : "no"); |
9a5fc0e8 | 415 | PrintToStdOut("Put devices in standby mode when activating screensaver: %s", g_config.bPowerOffScreensaver ? "yes" : "no"); |
41e3372a | 416 | PrintToStdOut("Put this PC in standby mode when the TV is switched off: %s", g_config.bPowerOffOnStandby ? "yes" : "no"); |
eb617f35 | 417 | PrintToStdOut("Send an inactive source message when stopping XBMC: %s\n\n", g_config.bSendInactiveSource ? "yes" : "no"); |
9a5fc0e8 | 418 | |
eb617f35 LOK |
419 | PrintToStdOut("Storing settings ..."); |
420 | if (g_parser->PersistConfiguration(&g_config)) | |
421 | PrintToStdOut("Settings stored."); | |
9a5fc0e8 | 422 | else |
eb617f35 | 423 | PrintToStdOut("The settings could not be stored"); |
1361efd2 | 424 | |
eb617f35 LOK |
425 | ofstream configOutput; |
426 | configOutput.open("usb_2548_1001.xml"); | |
427 | if (configOutput.is_open()) | |
428 | { | |
429 | CStdString strWakeDevices; | |
430 | for (uint8_t iPtr = 0; iPtr < 16; iPtr++) | |
431 | if (g_config.wakeDevices[iPtr]) | |
432 | strWakeDevices.AppendFormat(" %d" + iPtr); | |
433 | CStdString strStandbyDevices; | |
434 | for (uint8_t iPtr = 0; iPtr < 16; iPtr++) | |
435 | if (g_config.powerOffDevices[iPtr]) | |
436 | strStandbyDevices.AppendFormat(" %d" + iPtr); | |
437 | ||
438 | configOutput << | |
1361efd2 | 439 | "<settings>\n" << |
41e3372a LOK |
440 | "\t<setting id=\"enabled\" value=\"1\" />\n" << |
441 | "\t<setting id=\"activate_source\" value=\"" << (int)g_config.bActivateSource << "\" />\n" << | |
be54289b LOK |
442 | "\t<setting id=\"wake_devices\" value=\"" << strWakeDevices.c_str() << "\" />\n" << |
443 | "\t<setting id=\"standby_devices\" value=\"" << strStandbyDevices.c_str() << "\" />\n" << | |
1361efd2 LOK |
444 | "\t<setting id=\"cec_standby_screensaver\" value=\"" << (int)g_config.bPowerOffScreensaver << "\" />\n" << |
445 | "\t<setting id=\"standby_pc_on_tv_standby\" value=\"" << (int)g_config.bPowerOffOnStandby << "\" />\n" << | |
41e3372a LOK |
446 | "\t<setting id=\"use_tv_menu_language\" value=\"" << (int)g_config.bUseTVMenuLanguage << "\" />\n" << |
447 | "\t<setting id=\"physical_address\" value=\"" << hex << g_config.iPhysicalAddress << "\" />\n" << | |
448 | "\t<setting id=\"cec_hdmi_port\" value=\"" << g_config.iHDMIPort << "\" />\n" << | |
449 | "\t<setting id=\"connected_device\" value=\"" << (int)g_config.baseDevice << "\" />\n" << | |
1361efd2 | 450 | "\t<setting id=\"port\" value=\"\" />\n" << |
41e3372a | 451 | "\t<setting id=\"send_inactive_source\" value=\"" << (int)g_config.bSendInactiveSource << "\" />\n" << |
1361efd2 | 452 | "</settings>"; |
eb617f35 | 453 | configOutput.close(); |
1361efd2 | 454 | |
eb617f35 | 455 | PrintToStdOut("The configuration has been stored in 'usb_2548_1001.xml'. Copy this file to ~/.userdata/peripheral_data to use it in XBMC"); |
9a5fc0e8 | 456 | } |
f57cba64 LOK |
457 | |
458 | g_parser->StandbyDevices(); | |
459 | g_parser->Close(); | |
460 | UnloadLibCec(g_parser); | |
461 | ||
462 | PrintToStdOut("Press enter to close this wizard."); | |
463 | string input; | |
464 | getline(cin, input); | |
465 | return 0; | |
466 | } |