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