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