cec: added a new initialiser method: CecInitialise()/cec_initialise(), that accepts...
[deb_libcec.git] / src / cec-config / cec-config.cpp
CommitLineData
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
45using namespace CEC;
46using namespace std;
47using namespace PLATFORM;
48
49#include <cecloader.h>
50
51CMutex g_outputMutex;
52
53CMutex g_responseMutex;
54CCondition g_responseCondtion;
55cec_opcode g_lastCommand = CEC_OPCODE_NONE;
56
57CMutex g_keyMutex;
58CCondition g_keyCondtion;
59cec_user_control_code g_lastKey = CEC_USER_CONTROL_CODE_UNKNOWN;
60
61ICECCallbacks g_callbacks;
62ICECAdapter * g_parser;
63cec_logical_address g_primaryAddress;
64
65inline void PrintToStdOut(const char *strFormat, ...)
66{
67 CStdString strLog;
68
69 va_list argList;
70 va_start(argList, strFormat);
71 strLog.FormatV(strFormat, argList);
72 va_end(argList);
73
74 CLockObject lock(g_outputMutex);
75 cout << strLog << endl;
76}
77
78//get the first word (separated by whitespace) from string data and place that in word
79//then remove that word from string data
80bool GetWord(string& data, string& word)
81{
82 stringstream datastream(data);
83 string end;
84
85 datastream >> word;
86 if (datastream.fail())
87 {
88 data.clear();
89 return false;
90 }
91
92 size_t pos = data.find(word) + word.length();
93
94 if (pos >= data.length())
95 {
96 data.clear();
97 return true;
98 }
99
100 data = data.substr(pos);
101
102 datastream.clear();
103 datastream.str(data);
104
105 datastream >> end;
106 if (datastream.fail())
107 data.clear();
108
109 return true;
110}
111
112int CecLogMessage(void *UNUSED(cbParam), const cec_log_message &message)
113{
114 switch (message.level)
115 {
116 case CEC_LOG_ERROR:
117 PrintToStdOut("ERROR:\t%s", message.message);
118 break;
119 case CEC_LOG_WARNING:
120 PrintToStdOut("ERROR:\t%s", message.message);
121 break;
122 default:
123 break;
124 }
125
126 return 0;
127}
128
129int CecKeyPress(void *UNUSED(cbParam), const cec_keypress &key)
130{
131 CLockObject lock(g_keyMutex);
132 g_lastKey = key.keycode;
133 g_keyCondtion.Signal();
134 return 0;
135}
136
137int CecCommand(void *UNUSED(cbParam), const cec_command &command)
138{
139 CLockObject lock(g_responseMutex);
140 g_lastCommand = command.opcode;
141 g_responseCondtion.Signal();
142 return 0;
143}
144
145void EnableCallbacks(ICECAdapter *adapter)
146{
147 g_callbacks.CBCecLogMessage = &CecLogMessage;
148 g_callbacks.CBCecKeyPress = &CecKeyPress;
149 g_callbacks.CBCecCommand = &CecCommand;
150 adapter->EnableCallbacks(NULL, &g_callbacks);
151}
152
153ICECAdapter *CreateParser(cec_device_type_list typeList)
154{
155 ICECAdapter *parser = LibCecInit("ButtonConfig", typeList);
156 if (!parser)
157 {
158 #ifdef __WINDOWS__
159 PrintToStdOut("Cannot load libcec.dll");
160 #else
161 PrintToStdOut("Cannot load libcec.so");
162 #endif
163 return NULL;
164 }
165
166 PrintToStdOut("CEC Parser created - libcec version %d.%d", parser->GetLibVersionMajor(), parser->GetLibVersionMinor());
167
168 return parser;
169}
170
171bool ProcessConsoleCommand(string &input)
172{
173 if (!input.empty())
174 {
175 string command;
176 if (GetWord(input, command))
177 {
178 if (command == "q" || command == "quit")
179 return false;
180 }
181 }
182 return true;
183}
184
185bool OpenConnection(cec_device_type type = CEC_DEVICE_TYPE_RECORDING_DEVICE)
186{
187 cec_device_type_list types;
188 types.clear();
189 types.add(type);
190
191 g_parser = CreateParser(types);
192 if (!g_parser)
193 return false;
194
195 CStdString strPort;
196 cec_adapter devices[10];
197 uint8_t iDevicesFound = g_parser->FindAdapters(devices, 10, NULL);
198 if (iDevicesFound <= 0)
199 {
200 PrintToStdOut("autodetect FAILED");
201 UnloadLibCec(g_parser);
202 return false;
203 }
204 else
205 {
206 strPort = devices[0].comm;
207 }
208
209 EnableCallbacks(g_parser);
210
211 // start with HDMI1 on the TV
212 g_parser->SetHDMIPort(CECDEVICE_TV, 1);
213 PrintToStdOut("opening a connection to the CEC adapter...");
214
215 if (!g_parser->Open(strPort.c_str()))
216 {
217 PrintToStdOut("unable to open the device on port %s", strPort.c_str());
218 UnloadLibCec(g_parser);
219 return false;
220 }
221
222 cec_logical_addresses addr = g_parser->GetLogicalAddresses();
223 g_primaryAddress = addr.primary;
224
225 PrintToStdOut("cec device opened. using logical address %X", g_primaryAddress);
226 return true;
227}
228
229int8_t FindPhysicalAddressPortNumber(void)
230{
231 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.");
232 string input;
233 getline(cin, input);
234 cin.clear();
235 if (input.empty() || (input != "1" && input != "2" && input != "3" && input != "4"))
236 return -1;
237 return (int8_t)atoi(input.c_str());
238}
239
240cec_logical_address FindPhysicalAddressBaseDevice(void)
241{
242 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.");
243
244 string input;
245 getline(cin, input);
246 cin.clear();
247 if (input.empty() || (input != "1" && input != "2"))
248 {
249 PrintToStdOut("Exiting...");
250 return CECDEVICE_UNKNOWN;
251 }
252 return (input == "2") ?
253 CECDEVICE_AUDIOSYSTEM :
254 CECDEVICE_TV;
255}
256
257uint16_t FindPhysicalAddress(void)
258{
259 PrintToStdOut("=== Physical Address Configuration ===\n");
260 uint16_t iAddress(0xFFFF);
261
262 PrintToStdOut("Do you want to let libCEC try to autodetect the address (y/n)?");
263 string input;
264 getline(cin, input);
265 cin.clear();
266 if (input == "y" || input == "Y")
267 {
268 cec_logical_address baseDevice = FindPhysicalAddressBaseDevice();
269 if (baseDevice == CECDEVICE_UNKNOWN)
270 return iAddress;
271
272 int8_t iPortNumber = FindPhysicalAddressPortNumber();
273 if (iPortNumber == -1)
274 return iAddress;
275
276 PrintToStdOut("Trying to detect the physical address...");
277 if (!g_parser->SetHDMIPort(baseDevice, iPortNumber))
278 PrintToStdOut("Failed. Please enter the address manually, or restart this wizard and use different settings.");
279 else
280 {
281 iAddress = g_parser->GetDevicePhysicalAddress(g_primaryAddress);
282 if (iAddress == 0 || iAddress == 0xFFFF)
283 PrintToStdOut("Failed. Please enter the address manually, or restart this wizard and use different settings.");
284 }
285 }
286
287 if (iAddress == 0 || iAddress == 0xFFFF)
288 {
289 PrintToStdOut("Please enter the physical address (0000 - FFFF), followed by <enter>.");
290 getline(cin, input);
291 cin.clear();
292
293 int iAddressTmp;
294 if (sscanf(input.c_str(), "%x", &iAddressTmp) == 1)
295 {
296 if (iAddressTmp < 0 || iAddressTmp > 0xFFFF)
297 iAddressTmp = 0xFFFF;
298 iAddress = (uint16_t)iAddressTmp;
299 }
300 }
301
302 return iAddress;
303}
304
305bool PowerOnTV(uint64_t iTimeout = 60000, unsigned iTries = 2)
306{
307 cec_power_status currentTvPower(CEC_POWER_STATUS_UNKNOWN);
308 uint64_t iNow = GetTimeMs();
309 uint64_t iTarget = iNow + iTimeout;
310 unsigned iTry(0);
311
312 while (currentTvPower != CEC_POWER_STATUS_ON && iTarget > iNow && iTry < iTries)
313 {
314 currentTvPower = g_parser->GetDevicePowerStatus(CECDEVICE_TV);
315 if (currentTvPower != CEC_POWER_STATUS_ON)
316 {
317 PrintToStdOut("Sending 'power on' command to the TV");
318 g_parser->PowerOnDevices(CECDEVICE_TV);
319 while (iTarget > iNow)
320 {
321 CLockObject lock(g_responseMutex);
322 g_responseCondtion.Wait(g_responseMutex, (uint32_t)((iTarget - iNow)/iTries));
323 if (g_lastCommand == CEC_OPCODE_REQUEST_ACTIVE_SOURCE)
324 break;
325 iNow = GetTimeMs();
326 }
327 }
328 }
329
330 currentTvPower = g_parser->GetDevicePowerStatus(CECDEVICE_TV);
331
332 if (currentTvPower != CEC_POWER_STATUS_ON)
333 PrintToStdOut("Failed to power on the TV, or the TV does not respond properly");
334
335 return currentTvPower == CEC_POWER_STATUS_ON;
336}
337
338int main (int argc, char *argv[])
339{
340 PrintToStdOut("=== USB-CEC Adapter Configuration ===\n");
341 if (!OpenConnection())
342 return 1;
343
344 if (!PowerOnTV())
345 return 1;
346
347 bool bAddressOk(false);
61064737 348 uint16_t iAddress(0xFFFF);
f57cba64
LOK
349 while (!bAddressOk)
350 {
351 iAddress = FindPhysicalAddress();
352
353 PrintToStdOut("Physical address: %4X", iAddress);
354 PrintToStdOut("Is this correct (y/n)?");
355 string input;
356 getline(cin, input);
357 cin.clear();
358 bAddressOk = (input == "y" || input == "Y");
359 }
360
361 PrintToStdOut("=== USB-CEC Adapter Configuration Summary ===\n");
caca2d81
LOK
362 bool bHasAudiosystem = g_parser->IsActiveDevice(CECDEVICE_AUDIOSYSTEM);
363
f57cba64
LOK
364 PrintToStdOut("Physical address: %4X", iAddress);
365
366 g_parser->StandbyDevices();
367 g_parser->Close();
368 UnloadLibCec(g_parser);
369
370 PrintToStdOut("Press enter to close this wizard.");
371 string input;
372 getline(cin, input);
373 return 0;
374}