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