Commit | Line | Data |
---|---|---|
e9de9629 LOK |
1 | /* |
2 | * This file is part of the libCEC(R) library. | |
3 | * | |
b492c10e | 4 | * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved. |
e9de9629 LOK |
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" | |
e54fd7d2 | 34 | #include "../devices/CECBusDevice.h" |
8d915412 | 35 | #include "../devices/CECPlaybackDevice.h" |
e54fd7d2 | 36 | #include "../CECProcessor.h" |
2abe628c | 37 | #include "../LibCEC.h" |
e9de9629 LOK |
38 | |
39 | using namespace CEC; | |
cc1b9bc4 | 40 | using namespace PLATFORM; |
e9de9629 | 41 | |
0d4c3a7b LOK |
42 | #define SL_COMMAND_UNKNOWN_01 0x01 |
43 | #define SL_COMMAND_UNKNOWN_02 0x02 | |
415e9bad LOK |
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 | |
11d13a02 | 50 | |
0d4c3a7b | 51 | #define SL_COMMAND_REQUEST_POWER_STATUS 0xa0 |
9a0d7b9f | 52 | #define SL_COMMAND_POWER_ON 0x03 |
0d4c3a7b | 53 | #define SL_COMMAND_CONNECT_REQUEST 0x04 |
5abb18f3 | 54 | #define SL_COMMAND_SET_DEVICE_MODE 0x05 |
11d13a02 | 55 | |
e9de9629 | 56 | CSLCommandHandler::CSLCommandHandler(CCECBusDevice *busDevice) : |
855a3a98 | 57 | CCECCommandHandler(busDevice), |
b4c4ef7d | 58 | m_bSLEnabled(false), |
b8952342 | 59 | m_bActiveSourceSent(false) |
e9de9629 | 60 | { |
cf4931be | 61 | m_vendorId = CEC_VENDOR_LG; |
79f01d26 LOK |
62 | CCECBusDevice *primary = m_processor->GetPrimaryDevice(); |
63 | ||
64 | /* imitate LG devices */ | |
ef583662 | 65 | if (primary && m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress()) |
686740b9 | 66 | { |
3e61b350 | 67 | primary->SetVendorId(CEC_VENDOR_LG); |
686740b9 LOK |
68 | primary->ReplaceHandler(false); |
69 | } | |
79f01d26 LOK |
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); | |
9f65e017 LOK |
80 | } |
81 | ||
468a1414 LOK |
82 | bool CSLCommandHandler::InitHandler(void) |
83 | { | |
84 | if (m_bHandlerInited) | |
85 | return true; | |
86 | m_bHandlerInited = true; | |
87 | ||
632f5810 | 88 | if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV) |
871934d3 | 89 | { |
632f5810 LOK |
90 | CCECBusDevice *primary = m_processor->GetPrimaryDevice(); |
91 | ||
871934d3 LOK |
92 | /* start as 'in transition standby->on' */ |
93 | primary->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); | |
94 | primary->TransmitPowerState(CECDEVICE_TV); | |
b8952342 LOK |
95 | |
96 | /* send the vendor id */ | |
97 | primary->TransmitVendorID(CECDEVICE_BROADCAST); | |
871934d3 | 98 | } |
468a1414 | 99 | |
468a1414 LOK |
100 | return true; |
101 | } | |
102 | ||
3e61b350 LOK |
103 | bool CSLCommandHandler::ActivateSource(void) |
104 | { | |
cc1b9bc4 | 105 | if (!SLInitialised()) |
682b21de LOK |
106 | { |
107 | CLibCEC::AddLog(CEC_LOG_NOTICE, "not activating the source until SL has been initialised"); | |
3faa971c | 108 | return true; |
682b21de | 109 | } |
3faa971c | 110 | |
cc1b9bc4 LOK |
111 | { |
112 | CLockObject lock(m_SLMutex); | |
113 | if (m_bActiveSourceSent) | |
114 | return true; | |
115 | m_bActiveSourceSent = true; | |
116 | } | |
b818fb5a | 117 | |
3e61b350 LOK |
118 | CCECBusDevice *primary = m_processor->GetPrimaryDevice(); |
119 | primary->SetActiveSource(); | |
120 | primary->TransmitActiveSource(); | |
121 | return true; | |
122 | } | |
123 | ||
468a1414 LOK |
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 | if (iAddress != m_busDevice->GetPhysicalAddress(false)) | |
682b21de | 130 | { |
cc1b9bc4 | 131 | ResetSLState(); |
682b21de | 132 | } |
468a1414 LOK |
133 | return m_processor->SetActiveSource(iAddress); |
134 | } | |
135 | ||
136 | return true; | |
137 | } | |
138 | ||
a463f954 LOK |
139 | bool CSLCommandHandler::HandleDeviceVendorId(const cec_command &command) |
140 | { | |
141 | SetVendorId(command); | |
142 | ||
632f5810 | 143 | if (!SLInitialised() && command.initiator == CECDEVICE_TV) |
468a1414 | 144 | { |
2abe628c LOK |
145 | cec_command response; |
146 | cec_command::Format(response, m_processor->GetLogicalAddress(), command.initiator, CEC_OPCODE_FEATURE_ABORT); | |
147 | return Transmit(response); | |
468a1414 | 148 | } |
2abe628c | 149 | return true; |
468a1414 LOK |
150 | } |
151 | ||
152 | bool CSLCommandHandler::HandleGivePhysicalAddress(const cec_command &command) | |
153 | { | |
8e57f83c | 154 | if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination)) |
468a1414 LOK |
155 | { |
156 | CCECBusDevice *device = GetDevice(command.destination); | |
157 | if (device) | |
b818fb5a | 158 | return device->TransmitPhysicalAddress(); // only the physical address, don't send image view on |
468a1414 LOK |
159 | } |
160 | ||
161 | return false; | |
e9de9629 | 162 | } |
e54fd7d2 LOK |
163 | |
164 | bool CSLCommandHandler::HandleVendorCommand(const cec_command &command) | |
165 | { | |
415e9bad LOK |
166 | if (!m_busDevice->MyLogicalAddressContains(command.destination)) |
167 | return true; | |
168 | ||
e54fd7d2 | 169 | if (command.parameters.size == 1 && |
11d13a02 | 170 | command.parameters[0] == SL_COMMAND_UNKNOWN_01) |
e54fd7d2 | 171 | { |
5f316715 | 172 | HandleVendorCommand01(command); |
8d915412 | 173 | return true; |
e54fd7d2 | 174 | } |
9a0d7b9f LOK |
175 | else if (command.parameters.size == 2 && |
176 | command.parameters[0] == SL_COMMAND_POWER_ON) | |
177 | { | |
0ecbcd4d | 178 | HandleVendorCommandPowerOn(command); |
9a0d7b9f LOK |
179 | return true; |
180 | } | |
797dd7c4 | 181 | else if (command.parameters.size == 2 && |
11d13a02 | 182 | command.parameters[0] == SL_COMMAND_CONNECT_REQUEST) |
9902f4e8 | 183 | { |
0ecbcd4d | 184 | HandleVendorCommandSLConnect(command); |
8d915412 | 185 | return true; |
9902f4e8 LOK |
186 | } |
187 | else if (command.parameters.size == 1 && | |
0d4c3a7b | 188 | command.parameters[0] == SL_COMMAND_REQUEST_POWER_STATUS) |
9902f4e8 | 189 | { |
0ecbcd4d | 190 | HandleVendorCommandPowerOnStatus(command); |
8d915412 | 191 | return true; |
9902f4e8 | 192 | } |
e54fd7d2 LOK |
193 | |
194 | return false; | |
195 | } | |
196 | ||
5f316715 | 197 | void CSLCommandHandler::HandleVendorCommand01(const cec_command &command) |
fe6f8e37 | 198 | { |
b818fb5a | 199 | m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); |
fe6f8e37 LOK |
200 | TransmitVendorCommand0205(command.destination, command.initiator); |
201 | } | |
202 | ||
203 | void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource, const cec_logical_address iDestination) | |
5f316715 LOK |
204 | { |
205 | cec_command response; | |
fe6f8e37 | 206 | cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND); |
5f316715 | 207 | response.PushBack(SL_COMMAND_UNKNOWN_02); |
415e9bad | 208 | response.PushBack(SL_COMMAND_TYPE_HDDRECORDER); |
5f316715 | 209 | |
19cbfa8f | 210 | Transmit(response, false); |
fe6f8e37 | 211 | } |
5f316715 | 212 | |
0ecbcd4d | 213 | void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command &command) |
9a0d7b9f | 214 | { |
632f5810 LOK |
215 | if (command.initiator != CECDEVICE_TV) |
216 | return; | |
217 | ||
842262d8 | 218 | CCECBusDevice *device = m_processor->GetPrimaryDevice(); |
9a0d7b9f LOK |
219 | if (device) |
220 | { | |
cc1b9bc4 | 221 | SetSLInitialised(); |
468a1414 LOK |
222 | |
223 | device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); //XXX | |
9a0d7b9f | 224 | device->TransmitPowerState(command.initiator); |
468a1414 | 225 | device->SetPowerStatus(CEC_POWER_STATUS_ON); |
9a0d7b9f | 226 | |
468a1414 LOK |
227 | device->SetActiveSource(); |
228 | TransmitImageViewOn(device->GetLogicalAddress(), command.initiator); | |
229 | } | |
5f316715 | 230 | } |
0ecbcd4d | 231 | void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command &command) |
5f316715 LOK |
232 | { |
233 | if (command.destination != CECDEVICE_BROADCAST) | |
234 | { | |
235 | CCECBusDevice *device = m_processor->m_busDevices[m_processor->GetLogicalAddresses().primary]; | |
236 | device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); | |
237 | device->TransmitPowerState(command.initiator); | |
238 | device->SetPowerStatus(CEC_POWER_STATUS_ON); | |
239 | } | |
240 | } | |
241 | ||
468a1414 | 242 | void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command &command) |
8d915412 | 243 | { |
cc1b9bc4 | 244 | SetSLInitialised(); |
5abb18f3 | 245 | TransmitVendorCommandSetDeviceMode(m_processor->GetLogicalAddress(), command.initiator, CEC_DEVICE_TYPE_RECORDING_DEVICE); |
37acf382 | 246 | |
2abe628c | 247 | ActivateSource(); |
8d915412 | 248 | } |
0d4c3a7b | 249 | |
5abb18f3 | 250 | void CSLCommandHandler::TransmitVendorCommandSetDeviceMode(const cec_logical_address iSource, const cec_logical_address iDestination, const cec_device_type type) |
0d4c3a7b | 251 | { |
468a1414 LOK |
252 | cec_command response; |
253 | cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND); | |
5abb18f3 LOK |
254 | response.PushBack(SL_COMMAND_SET_DEVICE_MODE); |
255 | response.PushBack((uint8_t)type); | |
468a1414 | 256 | Transmit(response, false); |
0d4c3a7b | 257 | } |
fe6f8e37 | 258 | |
b818fb5a | 259 | bool CSLCommandHandler::HandleGiveDeckStatus(const cec_command &command) |
9f65e017 | 260 | { |
b818fb5a LOK |
261 | if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination)) |
262 | { | |
263 | CCECBusDevice *device = GetDevice(command.destination); | |
264 | if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE)) | |
265 | { | |
266 | if (command.parameters.size > 0) | |
267 | { | |
b818fb5a LOK |
268 | if (command.parameters[0] == CEC_STATUS_REQUEST_ON) |
269 | { | |
2abe628c | 270 | ((CCECPlaybackDevice *) device)->SetDeckStatus(CEC_DECK_INFO_STOP); |
b818fb5a LOK |
271 | return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator) && |
272 | device->TransmitImageViewOn() && | |
273 | device->TransmitPhysicalAddress(); | |
274 | } | |
275 | else if (command.parameters[0] == CEC_STATUS_REQUEST_ONCE) | |
276 | { | |
2abe628c | 277 | ((CCECPlaybackDevice *) device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG); |
b818fb5a LOK |
278 | return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator); |
279 | } | |
280 | } | |
281 | } | |
282 | return CCECCommandHandler::HandleGiveDeckStatus(command); | |
283 | } | |
9f65e017 | 284 | |
b818fb5a | 285 | return false; |
9f65e017 | 286 | } |
718b3632 | 287 | |
718b3632 LOK |
288 | bool CSLCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command) |
289 | { | |
2abe628c | 290 | bool bReturn(false); |
632f5810 | 291 | if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination) && command.initiator == CECDEVICE_TV) |
718b3632 LOK |
292 | { |
293 | CCECBusDevice *device = GetDevice(command.destination); | |
294 | if (device && device->GetPowerStatus(false) != CEC_POWER_STATUS_ON) | |
2abe628c LOK |
295 | { |
296 | bReturn = device->TransmitPowerState(command.initiator); | |
297 | device->SetPowerStatus(CEC_POWER_STATUS_ON); | |
298 | } | |
2abe628c LOK |
299 | else |
300 | { | |
cc1b9bc4 | 301 | if (!ActiveSourceSent()) |
3faa971c LOK |
302 | { |
303 | device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); | |
304 | bReturn = device->TransmitPowerState(command.initiator); | |
305 | ActivateSource(); | |
306 | } | |
307 | else if (m_resetPowerState.IsSet() && m_resetPowerState.TimeLeft() > 0) | |
308 | { | |
415e9bad | 309 | /* TODO assume that we've bugged out. the return button no longer works after this */ |
3faa971c | 310 | CLibCEC::AddLog(CEC_LOG_NOTICE, "LG seems to have bugged out. resetting to 'in transition standby to on'"); |
cc1b9bc4 LOK |
311 | { |
312 | CLockObject lock(m_SLMutex); | |
313 | m_bActiveSourceSent = false; | |
314 | } | |
3faa971c LOK |
315 | device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); |
316 | bReturn = device->TransmitPowerState(command.initiator); | |
317 | device->SetPowerStatus(CEC_POWER_STATUS_ON); | |
318 | m_resetPowerState.Init(5000); | |
319 | } | |
320 | else | |
321 | { | |
322 | bReturn = device->TransmitPowerState(command.initiator); | |
323 | m_resetPowerState.Init(5000); | |
324 | } | |
b818fb5a | 325 | } |
718b3632 LOK |
326 | } |
327 | ||
2abe628c LOK |
328 | return bReturn; |
329 | } | |
330 | ||
331 | bool CSLCommandHandler::HandleRequestActiveSource(const cec_command &command) | |
332 | { | |
333 | if (m_processor->IsRunning()) | |
334 | { | |
335 | CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %i requests active source, ignored", (uint8_t) command.initiator); | |
336 | return true; | |
337 | } | |
718b3632 LOK |
338 | return false; |
339 | } | |
5c3b1f92 LOK |
340 | |
341 | bool CSLCommandHandler::HandleFeatureAbort(const cec_command &command) | |
342 | { | |
632f5810 LOK |
343 | if (command.parameters.size == 0 && m_processor->GetPrimaryDevice()->GetPowerStatus() == CEC_POWER_STATUS_ON && !SLInitialised() && |
344 | command.initiator == CECDEVICE_TV) | |
5c3b1f92 LOK |
345 | { |
346 | m_processor->GetPrimaryDevice()->TransmitPowerState(command.initiator); | |
347 | m_processor->GetPrimaryDevice()->TransmitVendorID(CECDEVICE_BROADCAST, false); | |
348 | } | |
349 | ||
350 | return CCECCommandHandler::HandleFeatureAbort(command); | |
351 | } | |
352 | ||
353 | bool CSLCommandHandler::HandleStandby(const cec_command &command) | |
354 | { | |
355 | if (command.initiator == CECDEVICE_TV) | |
cc1b9bc4 | 356 | ResetSLState(); |
5c3b1f92 | 357 | |
cc1b9bc4 LOK |
358 | return CCECCommandHandler::HandleStandby(command); |
359 | } | |
5c3b1f92 | 360 | |
cc1b9bc4 LOK |
361 | void CSLCommandHandler::ResetSLState(void) |
362 | { | |
363 | CLibCEC::AddLog(CEC_LOG_NOTICE, "resetting SL initialised state"); | |
364 | CLockObject lock(m_SLMutex); | |
365 | m_bSLEnabled = false; | |
366 | m_bActiveSourceSent = false; | |
367 | m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); | |
368 | } | |
369 | ||
370 | void CSLCommandHandler::SetSLInitialised(void) | |
371 | { | |
372 | CLibCEC::AddLog(CEC_LOG_NOTICE, "SL initialised"); | |
373 | CLockObject lock(m_SLMutex); | |
374 | m_bSLEnabled = true; | |
375 | } | |
376 | ||
377 | bool CSLCommandHandler::SLInitialised(void) | |
378 | { | |
379 | CLockObject lock(m_SLMutex); | |
380 | return m_bSLEnabled; | |
381 | } | |
382 | ||
383 | bool CSLCommandHandler::ActiveSourceSent(void) | |
384 | { | |
385 | CLockObject lock(m_SLMutex); | |
386 | return m_bActiveSourceSent; | |
5c3b1f92 | 387 | } |
632f5810 LOK |
388 | |
389 | bool CSLCommandHandler::PowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination) | |
390 | { | |
391 | if (iDestination != CECDEVICE_TV) | |
392 | { | |
393 | cec_command command; | |
394 | cec_command::Format(command, CECDEVICE_TV, iDestination, CEC_OPCODE_VENDOR_COMMAND); | |
395 | command.PushBack(SL_COMMAND_POWER_ON); | |
396 | command.PushBack(0); | |
397 | return Transmit(command); | |
398 | } | |
399 | ||
400 | return CCECCommandHandler::PowerOn(iInitiator, iDestination); | |
401 | } |