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