1 /*
2  * This file is part of the libCEC(R) library.
3  *
4  * libCEC(R) is Copyright (C) 2011-2015 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., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301  USA
23  *
24  *
25  * Alternatively, you can license this library under a commercial license,
26  * please contact Pulse-Eight Licensing for more information.
27  *
28  * For more information contact:
29  * Pulse-Eight Licensing       <license@pulse-eight.com>
30  *     http://www.pulse-eight.com/
31  *     http://www.pulse-eight.net/
32  */
33 
34 #include "env.h"
35 #include "SLCommandHandler.h"
36 
37 #include "p8-platform/util/timeutils.h"
38 #include "devices/CECBusDevice.h"
39 #include "devices/CECPlaybackDevice.h"
40 #include "CECProcessor.h"
41 #include "LibCEC.h"
42 #include <stdio.h>
43 
44 using namespace CEC;
45 using namespace P8PLATFORM;
46 
47 #define SL_COMMAND_TYPE_HDDRECORDER_DISC  0x01
48 #define SL_COMMAND_TYPE_VCR               0x02
49 #define SL_COMMAND_TYPE_DVDPLAYER         0x03
50 #define SL_COMMAND_TYPE_HDDRECORDER_DISC2 0x04
51 #define SL_COMMAND_TYPE_HDDRECORDER       0x05
52 
53 #define SL_COMMAND_INIT                 0x01
54 #define SL_COMMAND_ACK_INIT             0x02
55 #define SL_COMMAND_POWER_ON             0x03
56 #define SL_COMMAND_CONNECT_REQUEST      0x04
57 #define SL_COMMAND_SET_DEVICE_MODE      0x05
58 #define SL_COMMAND_REQUEST_RECONNECT    0x0b
59 #define SL_COMMAND_REQUEST_POWER_STATUS 0xa0
60 
61 #define LIB_CEC     m_busDevice->GetProcessor()->GetLib()
62 #define ToString(p) LIB_CEC->ToString(p)
63 
CSLCommandHandler(CCECBusDevice * busDevice,int32_t iTransmitTimeout,int32_t iTransmitWait,int8_t iTransmitRetries,int64_t iActiveSourcePending)64 CSLCommandHandler::CSLCommandHandler(CCECBusDevice *busDevice,
65                                      int32_t iTransmitTimeout /* = CEC_DEFAULT_TRANSMIT_TIMEOUT */,
66                                      int32_t iTransmitWait /* = CEC_DEFAULT_TRANSMIT_WAIT */,
67                                      int8_t iTransmitRetries /* = CEC_DEFAULT_TRANSMIT_RETRIES */,
68                                      int64_t iActiveSourcePending /* = 0 */) :
69     CCECCommandHandler(busDevice, iTransmitTimeout, iTransmitWait, iTransmitRetries, iActiveSourcePending),
70     m_bSLEnabled(false)
71 {
72   m_vendorId = CEC_VENDOR_LG;
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   snprintf(lang, 4, "eng");
80   m_busDevice->SetMenuLanguage(lang);
81 }
82 
InitHandler(void)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 
103   return true;
104 }
105 
HandleVendorCommand(const cec_command & command)106 int CSLCommandHandler::HandleVendorCommand(const cec_command &command)
107 {
108   if (!m_processor->IsHandledByLibCEC(command.destination) && command.destination != CECDEVICE_BROADCAST)
109     return COMMAND_HANDLED;
110 
111   if (command.parameters.size == 1 &&
112       command.parameters[0] == SL_COMMAND_INIT)
113   {
114     HandleVendorCommandSLInit(command);
115     return COMMAND_HANDLED;
116   }
117   else if (command.parameters.size == 2 &&
118       command.parameters[0] == SL_COMMAND_POWER_ON)
119   {
120     HandleVendorCommandPowerOn(command, true);
121     return COMMAND_HANDLED;
122   }
123   else if (command.parameters.size == 2 &&
124       command.parameters[0] == SL_COMMAND_CONNECT_REQUEST)
125   {
126     HandleVendorCommandSLConnect(command);
127     return COMMAND_HANDLED;
128   }
129   else if (command.parameters.size == 1 &&
130       command.parameters[0] == SL_COMMAND_REQUEST_RECONNECT)
131   {
132     HandleVendorCommandPowerOnStatus(command);
133     return COMMAND_HANDLED;
134   }
135   else if (command.parameters.size == 1 &&
136       command.parameters[0] == SL_COMMAND_REQUEST_POWER_STATUS)
137   {
138     HandleVendorCommandPowerOnStatus(command);
139     return COMMAND_HANDLED;
140   }
141 
142   return CCECCommandHandler::HandleVendorCommand(command);
143 }
144 
HandleVendorCommandSLInit(const cec_command & command)145 void CSLCommandHandler::HandleVendorCommandSLInit(const cec_command &command)
146 {
147   CCECBusDevice* dev = m_processor->GetDevice(command.destination);
148   if (dev && dev->IsHandledByLibCEC())
149   {
150     if (!dev->IsActiveSource())
151     {
152       dev->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
153       dev->TransmitPowerState(command.initiator, true);
154     }
155 
156     TransmitVendorCommandSLAckInit(command.destination, command.initiator);
157   }
158 }
159 
TransmitVendorCommandSLAckInit(const cec_logical_address iSource,const cec_logical_address iDestination)160 void CSLCommandHandler::TransmitVendorCommandSLAckInit(const cec_logical_address iSource, const cec_logical_address iDestination)
161 {
162   cec_command response;
163   cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
164   response.PushBack(SL_COMMAND_ACK_INIT);
165   response.PushBack(SL_COMMAND_TYPE_HDDRECORDER);
166 
167   Transmit(response, false, true);
168   SetSLInitialised();
169 }
170 
HandleVendorCommandPowerOn(const cec_command & command,bool activateSource)171 void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command &command, bool activateSource)
172 {
173   if (command.initiator != CECDEVICE_TV)
174     return;
175 
176   CCECBusDevice *device = m_processor->GetPrimaryDevice();
177   if (device)
178   {
179     bool wasActive = device->IsActiveSource();
180     SetSLInitialised();
181     device->MarkAsActiveSource();
182     device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
183     device->TransmitPowerState(command.initiator, true);
184 
185     CEvent::Sleep(2000);
186     device->SetPowerStatus(CEC_POWER_STATUS_ON);
187     device->TransmitPowerState(command.initiator, false);
188     device->TransmitPhysicalAddress(false);
189 
190     if (!wasActive || activateSource)
191       ActivateSource();
192   }
193 }
HandleVendorCommandPowerOnStatus(const cec_command & command)194 void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command &command)
195 {
196   if (command.destination != CECDEVICE_BROADCAST)
197   {
198     CCECBusDevice *device = m_processor->GetPrimaryDevice();
199     if (device)
200     {
201       device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
202       device->TransmitPowerState(command.initiator, true);
203       device->SetPowerStatus(CEC_POWER_STATUS_ON);
204     }
205   }
206 }
207 
HandleVendorCommandSLConnect(const cec_command & command)208 void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command &command)
209 {
210   SetSLInitialised();
211   TransmitVendorCommandSetDeviceMode(command.destination, command.initiator, CEC_DEVICE_TYPE_RECORDING_DEVICE);
212 
213 
214   if (m_processor->IsActiveSource(command.destination) && m_processor->IsHandledByLibCEC(command.destination))
215   {
216     CCECBusDevice* dev = m_processor->GetDevice(command.destination);
217     CCECPlaybackDevice* pb = dev->AsPlaybackDevice();
218     if (pb)
219       pb->TransmitDeckStatus(command.initiator, true);
220     dev->TransmitPowerState(command.initiator, true);
221   }
222 }
223 
TransmitVendorCommandSetDeviceMode(const cec_logical_address iSource,const cec_logical_address iDestination,const cec_device_type type)224 void CSLCommandHandler::TransmitVendorCommandSetDeviceMode(const cec_logical_address iSource, const cec_logical_address iDestination, const cec_device_type type)
225 {
226   cec_command response;
227   cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
228   response.PushBack(SL_COMMAND_SET_DEVICE_MODE);
229   response.PushBack((uint8_t)type);
230   Transmit(response, false, true);
231 }
232 
HandleGiveDeckStatus(const cec_command & command)233 int CSLCommandHandler::HandleGiveDeckStatus(const cec_command &command)
234 {
235   if (!m_processor->CECInitialised() ||
236       !m_processor->IsHandledByLibCEC(command.destination))
237     return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
238 
239   CCECPlaybackDevice *device = CCECBusDevice::AsPlaybackDevice(GetDevice(command.destination));
240   if (!device || command.parameters.size == 0)
241     return CEC_ABORT_REASON_INVALID_OPERAND;
242 
243   device->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
244   if (command.parameters[0] == CEC_STATUS_REQUEST_ON)
245   {
246     device->TransmitDeckStatus(command.initiator, true);
247     ActivateSource();
248     return COMMAND_HANDLED;
249   }
250   else if (command.parameters[0] == CEC_STATUS_REQUEST_ONCE)
251   {
252     device->TransmitDeckStatus(command.initiator, true);
253     return COMMAND_HANDLED;
254   }
255 
256   return CCECCommandHandler::HandleGiveDeckStatus(command);
257 }
258 
HandleGiveDevicePowerStatus(const cec_command & command)259 int CSLCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
260 {
261   if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination) && command.initiator == CECDEVICE_TV)
262   {
263     CCECBusDevice *device = GetDevice(command.destination);
264     if (device && device->GetCurrentPowerStatus() != CEC_POWER_STATUS_ON)
265     {
266       device->TransmitPowerState(command.initiator, true);
267       device->SetPowerStatus(CEC_POWER_STATUS_ON);
268     }
269     else
270     {
271       if (m_resetPowerState.IsSet() && m_resetPowerState.TimeLeft() > 0)
272       {
273         /* TODO assume that we've bugged out. the return button no longer works after this */
274         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");
275         device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
276         device->TransmitPowerState(command.initiator, true);
277         device->SetPowerStatus(CEC_POWER_STATUS_ON);
278         m_resetPowerState.Init(5000);
279       }
280       else
281       {
282         device->TransmitPowerState(command.initiator, true);
283         m_resetPowerState.Init(5000);
284       }
285     }
286 
287     return COMMAND_HANDLED;
288   }
289 
290   return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
291 }
292 
HandleRequestActiveSource(const cec_command & command)293 int CSLCommandHandler::HandleRequestActiveSource(const cec_command &command)
294 {
295   if (m_processor->CECInitialised())
296   {
297     if (!SLInitialised())
298       TransmitVendorCommandSLAckInit(m_processor->GetPrimaryDevice()->GetLogicalAddress(), command.initiator);
299     CCECCommandHandler::HandleRequestActiveSource(command);
300   }
301   return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
302 }
303 
HandleFeatureAbort(const cec_command & command)304 int CSLCommandHandler::HandleFeatureAbort(const cec_command &command)
305 {
306   CCECBusDevice* primary = m_processor->GetPrimaryDevice();
307   if (command.parameters.size == 0 && primary->GetLogicalAddress() != CECDEVICE_UNKNOWN &&
308       primary->GetCurrentPowerStatus() == CEC_POWER_STATUS_ON && !SLInitialised() &&
309       command.initiator == CECDEVICE_TV)
310   {
311     if (!SLInitialised() && m_processor->IsActiveSource(command.destination))
312     {
313       TransmitVendorCommandSLAckInit(command.destination, command.initiator);
314       return COMMAND_HANDLED;
315     }
316   }
317 
318   return CCECCommandHandler::HandleFeatureAbort(command);
319 }
320 
HandleStandby(const cec_command & command)321 int CSLCommandHandler::HandleStandby(const cec_command &command)
322 {
323   ResetSLState();
324 
325   return CCECCommandHandler::HandleStandby(command);
326 }
327 
ResetSLState(void)328 void CSLCommandHandler::ResetSLState(void)
329 {
330   LIB_CEC->AddLog(CEC_LOG_NOTICE, "resetting SL initialised state");
331   CLockObject lock(m_SLMutex);
332   m_bSLEnabled = false;
333   m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
334 }
335 
SetSLInitialised(void)336 void CSLCommandHandler::SetSLInitialised(void)
337 {
338   LIB_CEC->AddLog(CEC_LOG_NOTICE, "SL initialised");
339   CLockObject lock(m_SLMutex);
340   m_bSLEnabled = true;
341 }
342 
SLInitialised(void)343 bool CSLCommandHandler::SLInitialised(void)
344 {
345   CLockObject lock(m_SLMutex);
346   return m_bSLEnabled;
347 }
348 
PowerOn(const cec_logical_address iInitiator,const cec_logical_address iDestination)349 bool CSLCommandHandler::PowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
350 {
351   if (iDestination != CECDEVICE_TV)
352   {
353     /* LG devices only allow themselves to be woken up by the TV with a vendor command */
354     cec_command command;
355 
356     if (!m_bSLEnabled)
357       TransmitVendorID(CECDEVICE_TV, iDestination, CEC_VENDOR_LG, false);
358 
359     cec_command::Format(command, CECDEVICE_TV, iDestination, CEC_OPCODE_VENDOR_COMMAND);
360     command.PushBack(SL_COMMAND_POWER_ON);
361     command.PushBack(0);
362     return Transmit(command, false, false);
363   }
364 
365   return CCECCommandHandler::PowerOn(iInitiator, iDestination);
366 }
367 
ActivateSource(bool bTransmitDelayedCommandsOnly)368 bool CSLCommandHandler::ActivateSource(bool bTransmitDelayedCommandsOnly /* = false */)
369 {
370   if (m_busDevice->IsActiveSource() &&
371       m_busDevice->IsHandledByLibCEC())
372   {
373     {
374       CLockObject lock(m_mutex);
375       // check if we need to send a delayed source switch
376       if (bTransmitDelayedCommandsOnly)
377       {
378         if (m_iActiveSourcePending == 0 || GetTimeMs() < m_iActiveSourcePending)
379           return false;
380 
381 #ifdef CEC_DEBUGGING
382         LIB_CEC->AddLog(CEC_LOG_DEBUG, "transmitting delayed activate source command");
383 #endif
384       }
385     }
386 
387     CCECPlaybackDevice *device = m_busDevice->AsPlaybackDevice();
388     if (device)
389       device->SetDeckStatus(!device->IsActiveSource() ? CEC_DECK_INFO_OTHER_STATUS : CEC_DECK_INFO_OTHER_STATUS_LG);
390 
391     // power on the TV
392     CCECBusDevice* tv = m_processor->GetDevice(CECDEVICE_TV);
393     bool bTvPresent = (tv && tv->GetStatus() == CEC_DEVICE_STATUS_PRESENT);
394     bool bActiveSourceFailed(false);
395     if (bTvPresent)
396     {
397       bActiveSourceFailed = !device->TransmitImageViewOn();
398     }
399     else
400     {
401       LIB_CEC->AddLog(CEC_LOG_DEBUG, "TV not present, not sending 'image view on'");
402     }
403 
404     // check if we're allowed to switch sources
405     bool bSourceSwitchAllowed = SourceSwitchAllowed();
406     if (!bSourceSwitchAllowed)
407       LIB_CEC->AddLog(CEC_LOG_DEBUG, "source switch is currently not allowed by command handler");
408 
409     // switch sources (if allowed)
410     if (!bActiveSourceFailed && bSourceSwitchAllowed)
411     {
412       bActiveSourceFailed = !m_busDevice->TransmitActiveSource(false);
413     }
414 
415     // retry later
416     if (bActiveSourceFailed || !bSourceSwitchAllowed)
417     {
418       LIB_CEC->AddLog(CEC_LOG_DEBUG, "failed to make '%s' the active source. will retry later", m_busDevice->GetLogicalAddressName());
419       int64_t now(GetTimeMs());
420       CLockObject lock(m_mutex);
421       if (m_iActiveSourcePending == 0 || m_iActiveSourcePending < now)
422         m_iActiveSourcePending = now + (int64_t)CEC_ACTIVE_SOURCE_SWITCH_RETRY_TIME_MS;
423       return false;
424     }
425     else
426     {
427       CLockObject lock(m_mutex);
428       // clear previous pending active source command
429       m_iActiveSourcePending = 0;
430     }
431 
432     // mark the handler as initialised
433     CLockObject lock(m_mutex);
434     m_bHandlerInited = true;
435   }
436   return true;
437 }
438