tabs -> spaces
[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 (!m_processor->GetPrimaryDevice()->IsActiveSource())
106 {
107 CLibCEC::AddLog(CEC_LOG_NOTICE, "not activating the source because we're not marked as active");
108 return true;
109 }
110
111 {
112 CLockObject lock(m_SLMutex);
113 m_bActiveSourceSent = true;
114 }
115
116 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
117 primary->SetActiveSource();
118 primary->SetPowerStatus(CEC_POWER_STATUS_ON);
119 primary->TransmitPowerState(CECDEVICE_TV);
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 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
131 bool bSendPowerOffState(iAddress != primary->GetPhysicalAddress(false) && primary->IsActiveSource());
132
133 m_processor->SetActiveSource(iAddress);
134 if (bSendPowerOffState)
135 {
136 {
137 CLockObject lock(m_SLMutex);
138 m_bActiveSourceSent = false;
139 }
140 primary->TransmitPowerState(CECDEVICE_TV);
141 }
142 }
143
144 return true;
145
146 }
147
148 bool CSLCommandHandler::HandleDeviceVendorId(const cec_command &command)
149 {
150 SetVendorId(command);
151
152 if (!SLInitialised() && command.initiator == CECDEVICE_TV)
153 {
154 cec_command response;
155 cec_command::Format(response, m_processor->GetLogicalAddress(), command.initiator, CEC_OPCODE_FEATURE_ABORT);
156 return Transmit(response);
157 }
158 return true;
159 }
160
161 bool CSLCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
162 {
163 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
164 {
165 CCECBusDevice *device = GetDevice(command.destination);
166 if (device)
167 return device->TransmitPhysicalAddress(); // only the physical address, don't send image view on
168 }
169
170 return false;
171 }
172
173 bool CSLCommandHandler::HandleVendorCommand(const cec_command &command)
174 {
175 if (!m_busDevice->MyLogicalAddressContains(command.destination))
176 return true;
177
178 if (command.parameters.size == 1 &&
179 command.parameters[0] == SL_COMMAND_UNKNOWN_01)
180 {
181 HandleVendorCommand01(command);
182 return true;
183 }
184 else if (command.parameters.size == 2 &&
185 command.parameters[0] == SL_COMMAND_POWER_ON)
186 {
187 HandleVendorCommandPowerOn(command);
188 return true;
189 }
190 else if (command.parameters.size == 2 &&
191 command.parameters[0] == SL_COMMAND_CONNECT_REQUEST)
192 {
193 HandleVendorCommandSLConnect(command);
194 return true;
195 }
196 else if (command.parameters.size == 1 &&
197 command.parameters[0] == SL_COMMAND_REQUEST_POWER_STATUS)
198 {
199 HandleVendorCommandPowerOnStatus(command);
200 return true;
201 }
202
203 return false;
204 }
205
206 void CSLCommandHandler::HandleVendorCommand01(const cec_command &command)
207 {
208 m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
209 TransmitVendorCommand0205(command.destination, command.initiator);
210 }
211
212 void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource, const cec_logical_address iDestination)
213 {
214 cec_command response;
215 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
216 response.PushBack(SL_COMMAND_UNKNOWN_02);
217 response.PushBack(SL_COMMAND_TYPE_HDDRECORDER);
218
219 Transmit(response, false);
220 }
221
222 void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command &command)
223 {
224 if (command.initiator != CECDEVICE_TV)
225 return;
226
227 CCECBusDevice *device = m_processor->GetPrimaryDevice();
228 if (device)
229 {
230 SetSLInitialised();
231 device->SetActiveSource();
232 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
233 device->TransmitPowerState(command.initiator);
234
235 CEvent::Sleep(2000);
236 device->SetPowerStatus(CEC_POWER_STATUS_ON);
237 device->TransmitPowerState(command.initiator);
238 device->TransmitPhysicalAddress();
239 {
240 CLockObject lock(m_SLMutex);
241 m_bActiveSourceSent = false;
242 }
243 }
244 }
245 void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command &command)
246 {
247 if (command.destination != CECDEVICE_BROADCAST)
248 {
249 CCECBusDevice *device = m_processor->m_busDevices[m_processor->GetLogicalAddresses().primary];
250 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
251 device->TransmitPowerState(command.initiator);
252 device->SetPowerStatus(CEC_POWER_STATUS_ON);
253 }
254 }
255
256 void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command &command)
257 {
258 SetSLInitialised();
259 TransmitVendorCommandSetDeviceMode(m_processor->GetLogicalAddress(), command.initiator, CEC_DEVICE_TYPE_RECORDING_DEVICE);
260
261 ActivateSource();
262 }
263
264 void CSLCommandHandler::TransmitVendorCommandSetDeviceMode(const cec_logical_address iSource, const cec_logical_address iDestination, const cec_device_type type)
265 {
266 cec_command response;
267 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
268 response.PushBack(SL_COMMAND_SET_DEVICE_MODE);
269 response.PushBack((uint8_t)type);
270 Transmit(response, false);
271 }
272
273 bool CSLCommandHandler::HandleGiveDeckStatus(const cec_command &command)
274 {
275 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
276 {
277 CCECBusDevice *device = GetDevice(command.destination);
278 if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
279 {
280 if (command.parameters.size > 0)
281 {
282 ((CCECPlaybackDevice *) device)->SetDeckStatus(!device->IsActiveSource() || !ActiveSourceSent() ? CEC_DECK_INFO_OTHER_STATUS : CEC_DECK_INFO_OTHER_STATUS_LG);
283 if (command.parameters[0] == CEC_STATUS_REQUEST_ON)
284 {
285 bool bReturn = ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator);
286 if (!ActiveSourceSent())
287 ActivateSource();
288 return bReturn;
289 }
290 else if (command.parameters[0] == CEC_STATUS_REQUEST_ONCE)
291 {
292 return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator);
293 }
294 }
295 }
296 return CCECCommandHandler::HandleGiveDeckStatus(command);
297 }
298
299 return false;
300 }
301
302 bool CSLCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
303 {
304 bool bReturn(false);
305 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination) && command.initiator == CECDEVICE_TV)
306 {
307 CCECBusDevice *device = GetDevice(command.destination);
308 if (device && device->GetPowerStatus(false) != CEC_POWER_STATUS_ON)
309 {
310 bReturn = device->TransmitPowerState(command.initiator);
311 device->SetPowerStatus(CEC_POWER_STATUS_ON);
312 }
313 else
314 {
315 if (!ActiveSourceSent())
316 {
317 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
318 bReturn = device->TransmitPowerState(command.initiator);
319 ActivateSource();
320 }
321 else if (m_resetPowerState.IsSet() && m_resetPowerState.TimeLeft() > 0)
322 {
323 /* TODO assume that we've bugged out. the return button no longer works after this */
324 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");
325 {
326 CLockObject lock(m_SLMutex);
327 m_bActiveSourceSent = false;
328 }
329 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
330 bReturn = device->TransmitPowerState(command.initiator);
331 device->SetPowerStatus(CEC_POWER_STATUS_ON);
332 m_resetPowerState.Init(5000);
333 }
334 else
335 {
336 bReturn = device->TransmitPowerState(command.initiator);
337 m_resetPowerState.Init(5000);
338 }
339 }
340 }
341
342 return bReturn;
343 }
344
345 bool CSLCommandHandler::HandleRequestActiveSource(const cec_command &command)
346 {
347 if (m_processor->IsRunning())
348 {
349 if (ActiveSourceSent())
350 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %i requests active source, ignored", (uint8_t) command.initiator);
351 else
352 ActivateSource();
353 return true;
354 }
355 return false;
356 }
357
358 bool CSLCommandHandler::HandleFeatureAbort(const cec_command &command)
359 {
360 if (command.parameters.size == 0 && m_processor->GetPrimaryDevice()->GetPowerStatus() == CEC_POWER_STATUS_ON && !SLInitialised() &&
361 command.initiator == CECDEVICE_TV)
362 {
363 m_processor->GetPrimaryDevice()->TransmitPowerState(command.initiator);
364 m_processor->GetPrimaryDevice()->TransmitVendorID(CECDEVICE_BROADCAST, false);
365 }
366
367 return CCECCommandHandler::HandleFeatureAbort(command);
368 }
369
370 bool CSLCommandHandler::HandleStandby(const cec_command &command)
371 {
372 if (command.initiator == CECDEVICE_TV)
373 {
374 CLockObject lock(m_SLMutex);
375 m_bActiveSourceSent = false;
376 }
377
378 return CCECCommandHandler::HandleStandby(command);
379 }
380
381 void CSLCommandHandler::ResetSLState(void)
382 {
383 CLibCEC::AddLog(CEC_LOG_NOTICE, "resetting SL initialised state");
384 CLockObject lock(m_SLMutex);
385 m_bSLEnabled = false;
386 m_bActiveSourceSent = false;
387 m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
388 }
389
390 void CSLCommandHandler::SetSLInitialised(void)
391 {
392 CLibCEC::AddLog(CEC_LOG_NOTICE, "SL initialised");
393 CLockObject lock(m_SLMutex);
394 m_bSLEnabled = true;
395 }
396
397 bool CSLCommandHandler::SLInitialised(void)
398 {
399 CLockObject lock(m_SLMutex);
400 return m_bSLEnabled;
401 }
402
403 bool CSLCommandHandler::ActiveSourceSent(void)
404 {
405 CLockObject lock(m_SLMutex);
406 return m_bActiveSourceSent;
407 }
408
409 bool CSLCommandHandler::PowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
410 {
411 if (iDestination != CECDEVICE_TV)
412 {
413 /* LG devices only allow themselves to be woken up by the TV with a vendor command */
414 cec_command command;
415 cec_command::Format(command, CECDEVICE_TV, iDestination, CEC_OPCODE_VENDOR_COMMAND);
416 command.PushBack(SL_COMMAND_POWER_ON);
417 command.PushBack(0);
418 return Transmit(command);
419 }
420
421 return CCECCommandHandler::PowerOn(iInitiator, iDestination);
422 }