cec: refactored threading/locking - added windows native instead of pthread-win32...
[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 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
38 using namespace CEC;
39
40 #define SL_COMMAND_UNKNOWN_01 0x01
41 #define SL_COMMAND_UNKNOWN_02 0x02
42 #define SL_COMMAND_UNKNOWN_03 0x05
43
44 #define SL_COMMAND_REQUEST_POWER_STATUS 0xa0
45 #define SL_COMMAND_POWER_ON 0x03
46 #define SL_COMMAND_CONNECT_REQUEST 0x04
47 #define SL_COMMAND_CONNECT_ACCEPT 0x05
48
49 CSLCommandHandler::CSLCommandHandler(CCECBusDevice *busDevice) :
50 CCECCommandHandler(busDevice),
51 m_bAwaitingReceiveFailed(false),
52 m_bSLEnabled(false),
53 m_bPowerStateReset(false)
54 {
55 m_vendorId = CEC_VENDOR_LG;
56 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
57
58 /* imitate LG devices */
59 if (m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
60 primary->SetVendorId(CEC_VENDOR_LG);
61 SetLGDeckStatus();
62
63 /* LG TVs don't always reply to CEC version requests, so just set it to 1.3a */
64 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
65 m_busDevice->SetCecVersion(CEC_VERSION_1_3A);
66
67 /* LG devices always return "korean" as language */
68 cec_menu_language lang;
69 lang.device = m_busDevice->GetLogicalAddress();
70 snprintf(lang.language, 4, "eng");
71 m_busDevice->SetMenuLanguage(lang);
72 }
73
74
75 void CSLCommandHandler::HandlePoll(const cec_logical_address iInitiator, const cec_logical_address iDestination)
76 {
77 CCECCommandHandler::HandlePoll(iInitiator, iDestination);
78 m_bAwaitingReceiveFailed = true;
79 }
80
81 bool CSLCommandHandler::HandleReceiveFailed(void)
82 {
83 if (m_bAwaitingReceiveFailed)
84 {
85 m_bAwaitingReceiveFailed = false;
86 return false;
87 }
88
89 return true;
90 }
91
92 bool CSLCommandHandler::InitHandler(void)
93 {
94 if (m_bHandlerInited)
95 return true;
96 m_bHandlerInited = true;
97
98 /* reply with LGs vendor id */
99 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
100 if (m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
101 primary->TransmitVendorID(CECDEVICE_TV, false);
102
103 primary->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
104 return true;
105 }
106
107 bool CSLCommandHandler::ActivateSource(void)
108 {
109 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
110 primary->SetActiveSource();
111 primary->TransmitActiveSource();
112 return true;
113 }
114
115 bool CSLCommandHandler::HandleActiveSource(const cec_command &command)
116 {
117 if (command.parameters.size == 2)
118 {
119 uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
120 if (iAddress != m_busDevice->GetPhysicalAddress(false))
121 m_bSLEnabled = false;
122 return m_processor->SetActiveSource(iAddress);
123 }
124
125 return true;
126 }
127
128 bool CSLCommandHandler::HandleFeatureAbort(const cec_command &command)
129 {
130 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
131 if (primary->GetPowerStatus(false) == CEC_POWER_STATUS_ON && !m_bPowerStateReset && !m_bSLEnabled)
132 {
133 m_bPowerStateReset = true;
134 primary->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
135 }
136
137 return CCECCommandHandler::HandleFeatureAbort(command);
138 }
139
140 bool CSLCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
141 {
142 if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
143 {
144 CCECBusDevice *device = GetDevice(command.destination);
145 if (device)
146 return device->TransmitPhysicalAddress();
147 }
148
149 return false;
150 }
151
152 bool CSLCommandHandler::HandleVendorCommand(const cec_command &command)
153 {
154 if (command.parameters.size == 1 &&
155 command.parameters[0] == SL_COMMAND_UNKNOWN_01)
156 {
157 HandleVendorCommand01(command);
158 return true;
159 }
160 else if (command.parameters.size == 2 &&
161 command.parameters[0] == SL_COMMAND_POWER_ON)
162 {
163 HandleVendorCommandPowerOn(command);
164 return true;
165 }
166 else if (command.parameters.size == 2 &&
167 command.parameters[0] == SL_COMMAND_CONNECT_REQUEST)
168 {
169 HandleVendorCommandSLConnect(command);
170 return true;
171 }
172 else if (command.parameters.size == 1 &&
173 command.parameters[0] == SL_COMMAND_REQUEST_POWER_STATUS)
174 {
175 HandleVendorCommandPowerOnStatus(command);
176 return true;
177 }
178
179 return false;
180 }
181
182 void CSLCommandHandler::HandleVendorCommand01(const cec_command &command)
183 {
184 TransmitVendorCommand0205(command.destination, command.initiator);
185 }
186
187 void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource, const cec_logical_address iDestination)
188 {
189 cec_command response;
190 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
191 response.PushBack(SL_COMMAND_UNKNOWN_02);
192 response.PushBack(SL_COMMAND_UNKNOWN_03);
193
194 Transmit(response, false);
195 }
196
197 void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command &command)
198 {
199 CCECBusDevice *device = m_processor->GetPrimaryDevice();
200 if (device)
201 {
202 m_bSLEnabled = true;
203
204 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); //XXX
205 device->TransmitPowerState(command.initiator);
206 device->SetPowerStatus(CEC_POWER_STATUS_ON);
207
208 SetLGDeckStatus();
209 device->SetActiveSource();
210 TransmitImageViewOn(device->GetLogicalAddress(), command.initiator);
211 }
212 }
213 void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command &command)
214 {
215 if (command.destination != CECDEVICE_BROADCAST)
216 {
217 CCECBusDevice *device = m_processor->m_busDevices[m_processor->GetLogicalAddresses().primary];
218 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
219 device->TransmitPowerState(command.initiator);
220 device->SetPowerStatus(CEC_POWER_STATUS_ON);
221 }
222 }
223
224 void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command &command)
225 {
226 m_bSLEnabled = true;
227 SetLGDeckStatus();
228
229 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
230
231 primary->SetActiveSource();
232 TransmitImageViewOn(primary->GetLogicalAddress(), command.initiator);
233 TransmitVendorCommand05(primary->GetLogicalAddress(), command.initiator);
234 }
235
236 void CSLCommandHandler::TransmitVendorCommand05(const cec_logical_address iSource, const cec_logical_address iDestination)
237 {
238 cec_command response;
239 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
240 response.PushBack(SL_COMMAND_CONNECT_ACCEPT);
241 response.PushBack((uint8_t)iSource);
242 Transmit(response, false);
243 }
244
245 void CSLCommandHandler::SetLGDeckStatus(void)
246 {
247 /* LG TVs only route keypresses when the deck status is set to 0x20 */
248 CCECBusDevice *device = m_processor->GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
249 if (device)
250 ((CCECPlaybackDevice *)device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
251
252 device = m_processor->GetDeviceByType(CEC_DEVICE_TYPE_RECORDING_DEVICE);
253 if (device)
254 ((CCECPlaybackDevice *)device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
255 }