cec: fix SL reconnect when the source has been switched before standby
[deb_libcec.git] / src / lib / implementations / SLCommandHandler.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 "SLCommandHandler.h"
34 #include "../devices/CECBusDevice.h"
35 #include "../devices/CECPlaybackDevice.h"
36 #include "../CECProcessor.h"
37 #include "../LibCEC.h"
38
39 using namespace CEC;
40 using namespace PLATFORM;
41
42 #define SL_COMMAND_UNKNOWN_01 0x01
43 #define SL_COMMAND_UNKNOWN_02 0x02
44
45 #define SL_COMMAND_TYPE_HDDRECORDER_DISC 0x01
46 #define SL_COMMAND_TYPE_VCR 0x02
47 #define SL_COMMAND_TYPE_DVDPLAYER 0x03
48 #define SL_COMMAND_TYPE_HDDRECORDER_DISC2 0x04
49 #define SL_COMMAND_TYPE_HDDRECORDER 0x05
50
51 #define SL_COMMAND_REQUEST_POWER_STATUS 0xa0
52 #define SL_COMMAND_POWER_ON 0x03
53 #define SL_COMMAND_CONNECT_REQUEST 0x04
54 #define SL_COMMAND_SET_DEVICE_MODE 0x05
55
56 CSLCommandHandler::CSLCommandHandler(CCECBusDevice *busDevice) :
57 CCECCommandHandler(busDevice),
58 m_bSLEnabled(false),
59 m_bActiveSourceSent(false)
60 {
61 m_vendorId = CEC_VENDOR_LG;
62 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
63
64 /* imitate LG devices */
65 if (primary && m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
66 {
67 primary->SetVendorId(CEC_VENDOR_LG);
68 primary->ReplaceHandler(false);
69 }
70
71 /* LG TVs don't always reply to CEC version requests, so just set it to 1.3a */
72 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
73 m_busDevice->SetCecVersion(CEC_VERSION_1_3A);
74
75 /* LG devices always return "korean" as language */
76 cec_menu_language lang;
77 lang.device = m_busDevice->GetLogicalAddress();
78 snprintf(lang.language, 4, "eng");
79 m_busDevice->SetMenuLanguage(lang);
80 }
81
82 bool CSLCommandHandler::InitHandler(void)
83 {
84 if (m_bHandlerInited)
85 return true;
86 m_bHandlerInited = true;
87
88 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
89 {
90 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
91
92 /* start as 'in transition standby->on' */
93 primary->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
94 primary->TransmitPowerState(CECDEVICE_TV);
95
96 /* send the vendor id */
97 primary->TransmitVendorID(CECDEVICE_BROADCAST);
98 }
99
100 return true;
101 }
102
103 bool CSLCommandHandler::ActivateSource(void)
104 {
105 if (!SLInitialised())
106 {
107 CLibCEC::AddLog(CEC_LOG_NOTICE, "not activating the source until SL has been initialised");
108 return true;
109 }
110
111 {
112 CLockObject lock(m_SLMutex);
113 if (m_bActiveSourceSent)
114 return true;
115 m_bActiveSourceSent = true;
116 }
117
118 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
119 primary->SetActiveSource();
120 primary->TransmitImageViewOn();
121 primary->TransmitActiveSource();
122 return true;
123 }
124
125 bool CSLCommandHandler::HandleActiveSource(const cec_command &command)
126 {
127 if (command.parameters.size == 2)
128 {
129 uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
130 if (iAddress != m_processor->GetPrimaryDevice()->GetPhysicalAddress(false))
131 {
132 CLockObject lock(m_SLMutex);
133 m_bActiveSourceSent = false;
134 }
135 return m_processor->SetActiveSource(iAddress);
136 }
137
138 return true;
139 }
140
141 bool CSLCommandHandler::HandleDeviceVendorId(const cec_command &command)
142 {
143 SetVendorId(command);
144
145 if (!SLInitialised() && command.initiator == CECDEVICE_TV)
146 {
147 cec_command response;
148 cec_command::Format(response, m_processor->GetLogicalAddress(), command.initiator, CEC_OPCODE_FEATURE_ABORT);
149 return Transmit(response);
150 }
151 return true;
152 }
153
154 bool CSLCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
155 {
156 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
157 {
158 CCECBusDevice *device = GetDevice(command.destination);
159 if (device)
160 return device->TransmitPhysicalAddress(); // only the physical address, don't send image view on
161 }
162
163 return false;
164 }
165
166 bool CSLCommandHandler::HandleVendorCommand(const cec_command &command)
167 {
168 if (!m_busDevice->MyLogicalAddressContains(command.destination))
169 return true;
170
171 if (command.parameters.size == 1 &&
172 command.parameters[0] == SL_COMMAND_UNKNOWN_01)
173 {
174 HandleVendorCommand01(command);
175 return true;
176 }
177 else if (command.parameters.size == 2 &&
178 command.parameters[0] == SL_COMMAND_POWER_ON)
179 {
180 HandleVendorCommandPowerOn(command);
181 return true;
182 }
183 else if (command.parameters.size == 2 &&
184 command.parameters[0] == SL_COMMAND_CONNECT_REQUEST)
185 {
186 HandleVendorCommandSLConnect(command);
187 return true;
188 }
189 else if (command.parameters.size == 1 &&
190 command.parameters[0] == SL_COMMAND_REQUEST_POWER_STATUS)
191 {
192 HandleVendorCommandPowerOnStatus(command);
193 return true;
194 }
195
196 return false;
197 }
198
199 void CSLCommandHandler::HandleVendorCommand01(const cec_command &command)
200 {
201 m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
202 TransmitVendorCommand0205(command.destination, command.initiator);
203 }
204
205 void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource, const cec_logical_address iDestination)
206 {
207 cec_command response;
208 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
209 response.PushBack(SL_COMMAND_UNKNOWN_02);
210 response.PushBack(SL_COMMAND_TYPE_HDDRECORDER);
211
212 Transmit(response, false);
213 }
214
215 void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command &command)
216 {
217 if (command.initiator != CECDEVICE_TV)
218 return;
219
220 CCECBusDevice *device = m_processor->GetPrimaryDevice();
221 if (device)
222 {
223 SetSLInitialised();
224
225 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); //XXX
226 device->TransmitPowerState(command.initiator);
227 device->SetPowerStatus(CEC_POWER_STATUS_ON);
228
229 device->SetActiveSource();
230 TransmitImageViewOn(device->GetLogicalAddress(), command.initiator);
231 }
232 }
233 void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command &command)
234 {
235 if (command.destination != CECDEVICE_BROADCAST)
236 {
237 CCECBusDevice *device = m_processor->m_busDevices[m_processor->GetLogicalAddresses().primary];
238 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
239 device->TransmitPowerState(command.initiator);
240 device->SetPowerStatus(CEC_POWER_STATUS_ON);
241 }
242 }
243
244 void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command &command)
245 {
246 SetSLInitialised();
247 TransmitVendorCommandSetDeviceMode(m_processor->GetLogicalAddress(), command.initiator, CEC_DEVICE_TYPE_RECORDING_DEVICE);
248
249 ActivateSource();
250 }
251
252 void CSLCommandHandler::TransmitVendorCommandSetDeviceMode(const cec_logical_address iSource, const cec_logical_address iDestination, const cec_device_type type)
253 {
254 cec_command response;
255 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
256 response.PushBack(SL_COMMAND_SET_DEVICE_MODE);
257 response.PushBack((uint8_t)type);
258 Transmit(response, false);
259 }
260
261 bool CSLCommandHandler::HandleGiveDeckStatus(const cec_command &command)
262 {
263 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
264 {
265 CCECBusDevice *device = GetDevice(command.destination);
266 if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
267 {
268 if (command.parameters.size > 0)
269 {
270 if (!SLInitialised())
271 {
272 cec_command response;
273 cec_command::Format(response, command.destination, command.initiator, CEC_OPCODE_FEATURE_ABORT);
274 response.PushBack(CEC_OPCODE_GIVE_DECK_STATUS);
275 response.PushBack(CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND);
276 return Transmit(response);
277 }
278 ((CCECPlaybackDevice *) device)->SetDeckStatus(!device->IsActiveSource() ? CEC_DECK_INFO_OTHER_STATUS : CEC_DECK_INFO_OTHER_STATUS_LG);
279 if (command.parameters[0] == CEC_STATUS_REQUEST_ON)
280 {
281 bool bReturn = ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator);
282 if (device->IsActiveSource())
283 {
284 bReturn &= device->TransmitImageViewOn() &&
285 device->TransmitPhysicalAddress();
286 }
287 return bReturn;
288 }
289 else if (command.parameters[0] == CEC_STATUS_REQUEST_ONCE)
290 {
291 return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator);
292 }
293 }
294 }
295 return CCECCommandHandler::HandleGiveDeckStatus(command);
296 }
297
298 return false;
299 }
300
301 bool CSLCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
302 {
303 bool bReturn(false);
304 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination) && command.initiator == CECDEVICE_TV)
305 {
306 CCECBusDevice *device = GetDevice(command.destination);
307 if (device && device->GetPowerStatus(false) != CEC_POWER_STATUS_ON)
308 {
309 bReturn = device->TransmitPowerState(command.initiator);
310 device->SetPowerStatus(CEC_POWER_STATUS_ON);
311 }
312 else
313 {
314 if (!ActiveSourceSent())
315 {
316 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
317 bReturn = device->TransmitPowerState(command.initiator);
318 ActivateSource();
319 }
320 else if (m_resetPowerState.IsSet() && m_resetPowerState.TimeLeft() > 0)
321 {
322 /* TODO assume that we've bugged out. the return button no longer works after this */
323 CLibCEC::AddLog(CEC_LOG_WARNING, "FIXME: LG seems to have bugged out. resetting to 'in transition standby to on'. the return button will not work");
324 {
325 CLockObject lock(m_SLMutex);
326 m_bActiveSourceSent = false;
327 }
328 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
329 bReturn = device->TransmitPowerState(command.initiator);
330 device->SetPowerStatus(CEC_POWER_STATUS_ON);
331 m_resetPowerState.Init(5000);
332 }
333 else
334 {
335 bReturn = device->TransmitPowerState(command.initiator);
336 m_resetPowerState.Init(5000);
337 }
338 }
339 }
340
341 return bReturn;
342 }
343
344 bool CSLCommandHandler::HandleRequestActiveSource(const cec_command &command)
345 {
346 if (m_processor->IsRunning())
347 {
348 if (ActiveSourceSent())
349 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %i requests active source, ignored", (uint8_t) command.initiator);
350 else
351 ActivateSource();
352 return true;
353 }
354 return false;
355 }
356
357 bool CSLCommandHandler::HandleFeatureAbort(const cec_command &command)
358 {
359 if (command.parameters.size == 0 && m_processor->GetPrimaryDevice()->GetPowerStatus() == CEC_POWER_STATUS_ON && !SLInitialised() &&
360 command.initiator == CECDEVICE_TV)
361 {
362 m_processor->GetPrimaryDevice()->TransmitPowerState(command.initiator);
363 m_processor->GetPrimaryDevice()->TransmitVendorID(CECDEVICE_BROADCAST, false);
364 }
365
366 return CCECCommandHandler::HandleFeatureAbort(command);
367 }
368
369 bool CSLCommandHandler::HandleStandby(const cec_command &command)
370 {
371 if (command.initiator == CECDEVICE_TV)
372 {
373 CLockObject lock(m_SLMutex);
374 m_bActiveSourceSent = false;
375 }
376
377 return CCECCommandHandler::HandleStandby(command);
378 }
379
380 void CSLCommandHandler::ResetSLState(void)
381 {
382 CLibCEC::AddLog(CEC_LOG_NOTICE, "resetting SL initialised state");
383 CLockObject lock(m_SLMutex);
384 m_bSLEnabled = false;
385 m_bActiveSourceSent = false;
386 m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
387 }
388
389 void CSLCommandHandler::SetSLInitialised(void)
390 {
391 CLibCEC::AddLog(CEC_LOG_NOTICE, "SL initialised");
392 CLockObject lock(m_SLMutex);
393 m_bSLEnabled = true;
394 }
395
396 bool CSLCommandHandler::SLInitialised(void)
397 {
398 CLockObject lock(m_SLMutex);
399 return m_bSLEnabled;
400 }
401
402 bool CSLCommandHandler::ActiveSourceSent(void)
403 {
404 CLockObject lock(m_SLMutex);
405 return m_bActiveSourceSent;
406 }
407
408 bool CSLCommandHandler::PowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
409 {
410 if (iDestination != CECDEVICE_TV)
411 {
412 cec_command command;
413 cec_command::Format(command, CECDEVICE_TV, iDestination, CEC_OPCODE_VENDOR_COMMAND);
414 command.PushBack(SL_COMMAND_POWER_ON);
415 command.PushBack(0);
416 return Transmit(command);
417 }
418
419 return CCECCommandHandler::PowerOn(iInitiator, iDestination);
420 }