Merge branch 'master' into release
[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 devices don't always reply to CEC version requests, so just set it to 1.3a */
72 m_busDevice->SetCecVersion(CEC_VERSION_1_3A);
73
74 /* LG devices always return "korean" as language */
75 cec_menu_language lang;
76 lang.device = m_busDevice->GetLogicalAddress();
77 snprintf(lang.language, 4, "eng");
78 m_busDevice->SetMenuLanguage(lang);
79 }
80
81 bool CSLCommandHandler::InitHandler(void)
82 {
83 if (m_bHandlerInited)
84 return true;
85 m_bHandlerInited = true;
86
87 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
88 {
89 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
90
91 /* start as 'in transition standby->on' */
92 primary->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
93 primary->TransmitPowerState(CECDEVICE_TV);
94
95 /* send the vendor id */
96 primary->TransmitVendorID(CECDEVICE_BROADCAST);
97 }
98
99 return true;
100 }
101
102 bool CSLCommandHandler::ActivateSource(void)
103 {
104 if (!m_processor->GetPrimaryDevice()->IsActiveSource())
105 {
106 CLibCEC::AddLog(CEC_LOG_NOTICE, "not activating the source because we're not marked as active");
107 return true;
108 }
109
110 {
111 CLockObject lock(m_SLMutex);
112 m_bActiveSourceSent = true;
113 }
114
115 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
116 primary->SetActiveSource();
117 primary->SetPowerStatus(CEC_POWER_STATUS_ON);
118 primary->TransmitPowerState(CECDEVICE_TV);
119 primary->TransmitImageViewOn();
120 primary->TransmitActiveSource();
121 return true;
122 }
123
124 bool CSLCommandHandler::HandleActiveSource(const cec_command &command)
125 {
126 if (command.parameters.size == 2)
127 {
128 uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
129 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
130 bool bSendPowerOffState(iAddress != primary->GetPhysicalAddress() && primary->IsActiveSource());
131
132 m_processor->SetActiveSource(iAddress);
133 if (bSendPowerOffState)
134 {
135 {
136 CLockObject lock(m_SLMutex);
137 m_bActiveSourceSent = false;
138 }
139 primary->TransmitPowerState(CECDEVICE_TV);
140 }
141 }
142
143 return true;
144
145 }
146
147 bool CSLCommandHandler::HandleDeviceVendorId(const cec_command &command)
148 {
149 SetVendorId(command);
150
151 if (!SLInitialised() && command.initiator == CECDEVICE_TV)
152 {
153 cec_command response;
154 cec_command::Format(response, m_processor->GetLogicalAddress(), command.initiator, CEC_OPCODE_FEATURE_ABORT);
155 return Transmit(response);
156 }
157 return true;
158 }
159
160 bool CSLCommandHandler::HandleVendorCommand(const cec_command &command)
161 {
162 if (!m_busDevice->MyLogicalAddressContains(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 true;
170 }
171 else if (command.parameters.size == 2 &&
172 command.parameters[0] == SL_COMMAND_POWER_ON)
173 {
174 HandleVendorCommandPowerOn(command);
175 return true;
176 }
177 else if (command.parameters.size == 2 &&
178 command.parameters[0] == SL_COMMAND_CONNECT_REQUEST)
179 {
180 HandleVendorCommandSLConnect(command);
181 return true;
182 }
183 else if (command.parameters.size == 1 &&
184 command.parameters[0] == SL_COMMAND_REQUEST_POWER_STATUS)
185 {
186 HandleVendorCommandPowerOnStatus(command);
187 return true;
188 }
189
190 return false;
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);
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->SetActiveSource();
219 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
220 device->TransmitPowerState(command.initiator);
221
222 CEvent::Sleep(2000);
223 device->SetPowerStatus(CEC_POWER_STATUS_ON);
224 device->TransmitPowerState(command.initiator);
225 device->TransmitPhysicalAddress();
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->m_busDevices[m_processor->GetLogicalAddresses().primary];
237 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
238 device->TransmitPowerState(command.initiator);
239 device->SetPowerStatus(CEC_POWER_STATUS_ON);
240 }
241 }
242
243 void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command &command)
244 {
245 SetSLInitialised();
246 TransmitVendorCommandSetDeviceMode(m_processor->GetLogicalAddress(), command.initiator, CEC_DEVICE_TYPE_RECORDING_DEVICE);
247
248 ActivateSource();
249 }
250
251 void CSLCommandHandler::TransmitVendorCommandSetDeviceMode(const cec_logical_address iSource, const cec_logical_address iDestination, const cec_device_type type)
252 {
253 cec_command response;
254 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
255 response.PushBack(SL_COMMAND_SET_DEVICE_MODE);
256 response.PushBack((uint8_t)type);
257 Transmit(response);
258 }
259
260 bool CSLCommandHandler::HandleGiveDeckStatus(const cec_command &command)
261 {
262 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
263 {
264 CCECBusDevice *device = GetDevice(command.destination);
265 if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
266 {
267 if (command.parameters.size > 0)
268 {
269 ((CCECPlaybackDevice *) 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 bool bReturn = ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator);
273 if (!ActiveSourceSent())
274 ActivateSource();
275 return bReturn;
276 }
277 else if (command.parameters[0] == CEC_STATUS_REQUEST_ONCE)
278 {
279 return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator);
280 }
281 }
282 }
283 return CCECCommandHandler::HandleGiveDeckStatus(command);
284 }
285
286 return false;
287 }
288
289 bool CSLCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
290 {
291 bool bReturn(false);
292 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination) && command.initiator == CECDEVICE_TV)
293 {
294 CCECBusDevice *device = GetDevice(command.destination);
295 if (device && device->GetPowerStatus(false) != CEC_POWER_STATUS_ON)
296 {
297 bReturn = device->TransmitPowerState(command.initiator);
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 bReturn = device->TransmitPowerState(command.initiator);
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 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");
312 {
313 CLockObject lock(m_SLMutex);
314 m_bActiveSourceSent = false;
315 }
316 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
317 bReturn = device->TransmitPowerState(command.initiator);
318 device->SetPowerStatus(CEC_POWER_STATUS_ON);
319 m_resetPowerState.Init(5000);
320 }
321 else
322 {
323 bReturn = device->TransmitPowerState(command.initiator);
324 m_resetPowerState.Init(5000);
325 }
326 }
327 }
328
329 return bReturn;
330 }
331
332 bool CSLCommandHandler::HandleRequestActiveSource(const cec_command &command)
333 {
334 if (m_processor->IsRunning())
335 {
336 if (ActiveSourceSent())
337 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %i requests active source, ignored", (uint8_t) command.initiator);
338 else
339 ActivateSource();
340 return true;
341 }
342 return false;
343 }
344
345 bool CSLCommandHandler::HandleFeatureAbort(const cec_command &command)
346 {
347 if (command.parameters.size == 0 && m_processor->GetPrimaryDevice()->GetPowerStatus() == CEC_POWER_STATUS_ON && !SLInitialised() &&
348 command.initiator == CECDEVICE_TV)
349 {
350 m_processor->GetPrimaryDevice()->TransmitPowerState(command.initiator);
351 m_processor->GetPrimaryDevice()->TransmitVendorID(CECDEVICE_BROADCAST, false);
352 }
353
354 return CCECCommandHandler::HandleFeatureAbort(command);
355 }
356
357 bool CSLCommandHandler::HandleStandby(const cec_command &command)
358 {
359 if (command.initiator == CECDEVICE_TV)
360 {
361 CLockObject lock(m_SLMutex);
362 m_bActiveSourceSent = false;
363 }
364
365 return CCECCommandHandler::HandleStandby(command);
366 }
367
368 void CSLCommandHandler::ResetSLState(void)
369 {
370 CLibCEC::AddLog(CEC_LOG_NOTICE, "resetting SL initialised state");
371 CLockObject lock(m_SLMutex);
372 m_bSLEnabled = false;
373 m_bActiveSourceSent = false;
374 m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
375 }
376
377 void CSLCommandHandler::SetSLInitialised(void)
378 {
379 CLibCEC::AddLog(CEC_LOG_NOTICE, "SL initialised");
380 CLockObject lock(m_SLMutex);
381 m_bSLEnabled = true;
382 }
383
384 bool CSLCommandHandler::SLInitialised(void)
385 {
386 CLockObject lock(m_SLMutex);
387 return m_bSLEnabled;
388 }
389
390 bool CSLCommandHandler::ActiveSourceSent(void)
391 {
392 CLockObject lock(m_SLMutex);
393 return m_bActiveSourceSent;
394 }
395
396 bool CSLCommandHandler::PowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
397 {
398 if (iDestination != CECDEVICE_TV)
399 {
400 /* LG devices only allow themselves to be woken up by the TV with a vendor command */
401 cec_command command;
402
403 if (!m_bSLEnabled)
404 TransmitVendorID(CECDEVICE_TV, CEC_VENDOR_LG);
405
406 cec_command::Format(command, CECDEVICE_TV, iDestination, CEC_OPCODE_VENDOR_COMMAND);
407 command.PushBack(SL_COMMAND_POWER_ON);
408 command.PushBack(0);
409 return Transmit(command);
410 }
411
412 return CCECCommandHandler::PowerOn(iInitiator, iDestination);
413 }