| 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 | #define LIB_CEC m_busDevice->GetProcessor()->GetLib() |
| 57 | #define ToString(p) LIB_CEC->ToString(p) |
| 58 | |
| 59 | CSLCommandHandler::CSLCommandHandler(CCECBusDevice *busDevice) : |
| 60 | CCECCommandHandler(busDevice), |
| 61 | m_bSLEnabled(false), |
| 62 | m_bActiveSourceSent(false) |
| 63 | { |
| 64 | m_vendorId = CEC_VENDOR_LG; |
| 65 | CCECBusDevice *primary = m_processor->GetPrimaryDevice(); |
| 66 | |
| 67 | /* imitate LG devices */ |
| 68 | if (primary && m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress()) |
| 69 | { |
| 70 | primary->SetVendorId(CEC_VENDOR_LG); |
| 71 | primary->ReplaceHandler(false); |
| 72 | } |
| 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 | lang.device = m_busDevice->GetLogicalAddress(); |
| 80 | snprintf(lang.language, 4, "eng"); |
| 81 | m_busDevice->SetMenuLanguage(lang); |
| 82 | } |
| 83 | |
| 84 | bool CSLCommandHandler::InitHandler(void) |
| 85 | { |
| 86 | if (m_bHandlerInited) |
| 87 | return true; |
| 88 | m_bHandlerInited = true; |
| 89 | |
| 90 | if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV) |
| 91 | { |
| 92 | CCECBusDevice *primary = m_processor->GetPrimaryDevice(); |
| 93 | |
| 94 | /* start as 'in transition standby->on' */ |
| 95 | primary->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); |
| 96 | primary->TransmitPowerState(CECDEVICE_TV); |
| 97 | |
| 98 | /* send the vendor id */ |
| 99 | primary->TransmitVendorID(CECDEVICE_BROADCAST); |
| 100 | } |
| 101 | |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | bool CSLCommandHandler::HandleActiveSource(const cec_command &command) |
| 106 | { |
| 107 | if (command.parameters.size == 2) |
| 108 | { |
| 109 | uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]); |
| 110 | CCECBusDevice *primary = m_processor->GetPrimaryDevice(); |
| 111 | bool bSendPowerOffState(iAddress != primary->GetCurrentPhysicalAddress() && primary->IsActiveSource()); |
| 112 | |
| 113 | CCECBusDevice *device = m_processor->GetDeviceByPhysicalAddress(iAddress); |
| 114 | if (device) |
| 115 | device->MarkAsActiveSource(); |
| 116 | if (bSendPowerOffState) |
| 117 | { |
| 118 | { |
| 119 | CLockObject lock(m_SLMutex); |
| 120 | m_bActiveSourceSent = false; |
| 121 | } |
| 122 | primary->TransmitPowerState(CECDEVICE_TV); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return true; |
| 127 | |
| 128 | } |
| 129 | |
| 130 | bool CSLCommandHandler::HandleDeviceVendorId(const cec_command &command) |
| 131 | { |
| 132 | SetVendorId(command); |
| 133 | |
| 134 | if (!SLInitialised() && command.initiator == CECDEVICE_TV) |
| 135 | { |
| 136 | CCECBusDevice *destination = m_processor->GetDevice(command.destination); |
| 137 | if (destination && (destination->GetLogicalAddress() == CECDEVICE_BROADCAST || destination->IsHandledByLibCEC())) |
| 138 | { |
| 139 | cec_logical_address initiator = destination->GetLogicalAddress(); |
| 140 | if (initiator == CECDEVICE_BROADCAST) |
| 141 | initiator = m_processor->GetPrimaryDevice()->GetLogicalAddress(); |
| 142 | |
| 143 | cec_command response; |
| 144 | cec_command::Format(response, initiator, command.initiator, CEC_OPCODE_FEATURE_ABORT); |
| 145 | return Transmit(response); |
| 146 | } |
| 147 | } |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | bool CSLCommandHandler::HandleVendorCommand(const cec_command &command) |
| 152 | { |
| 153 | if (!m_processor->IsHandledByLibCEC(command.destination)) |
| 154 | return true; |
| 155 | |
| 156 | if (command.parameters.size == 1 && |
| 157 | command.parameters[0] == SL_COMMAND_UNKNOWN_01) |
| 158 | { |
| 159 | HandleVendorCommand01(command); |
| 160 | return true; |
| 161 | } |
| 162 | else if (command.parameters.size == 2 && |
| 163 | command.parameters[0] == SL_COMMAND_POWER_ON) |
| 164 | { |
| 165 | HandleVendorCommandPowerOn(command); |
| 166 | return true; |
| 167 | } |
| 168 | else if (command.parameters.size == 2 && |
| 169 | command.parameters[0] == SL_COMMAND_CONNECT_REQUEST) |
| 170 | { |
| 171 | HandleVendorCommandSLConnect(command); |
| 172 | return true; |
| 173 | } |
| 174 | else if (command.parameters.size == 1 && |
| 175 | command.parameters[0] == SL_COMMAND_REQUEST_POWER_STATUS) |
| 176 | { |
| 177 | HandleVendorCommandPowerOnStatus(command); |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | void CSLCommandHandler::HandleVendorCommand01(const cec_command &command) |
| 185 | { |
| 186 | m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); |
| 187 | TransmitVendorCommand0205(command.destination, command.initiator); |
| 188 | } |
| 189 | |
| 190 | void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource, const cec_logical_address iDestination) |
| 191 | { |
| 192 | cec_command response; |
| 193 | cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND); |
| 194 | response.PushBack(SL_COMMAND_UNKNOWN_02); |
| 195 | response.PushBack(SL_COMMAND_TYPE_HDDRECORDER); |
| 196 | |
| 197 | Transmit(response); |
| 198 | } |
| 199 | |
| 200 | void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command &command) |
| 201 | { |
| 202 | if (command.initiator != CECDEVICE_TV) |
| 203 | return; |
| 204 | |
| 205 | CCECBusDevice *device = m_processor->GetPrimaryDevice(); |
| 206 | if (device) |
| 207 | { |
| 208 | SetSLInitialised(); |
| 209 | device->MarkAsActiveSource(); |
| 210 | device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); |
| 211 | device->TransmitPowerState(command.initiator); |
| 212 | |
| 213 | CEvent::Sleep(2000); |
| 214 | device->SetPowerStatus(CEC_POWER_STATUS_ON); |
| 215 | device->TransmitPowerState(command.initiator); |
| 216 | device->TransmitPhysicalAddress(); |
| 217 | { |
| 218 | CLockObject lock(m_SLMutex); |
| 219 | m_bActiveSourceSent = false; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command &command) |
| 224 | { |
| 225 | if (command.destination != CECDEVICE_BROADCAST) |
| 226 | { |
| 227 | CCECBusDevice *device = m_processor->GetPrimaryDevice(); |
| 228 | if (device) |
| 229 | { |
| 230 | device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); |
| 231 | device->TransmitPowerState(command.initiator); |
| 232 | device->SetPowerStatus(CEC_POWER_STATUS_ON); |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command &command) |
| 238 | { |
| 239 | SetSLInitialised(); |
| 240 | TransmitVendorCommandSetDeviceMode(command.destination, command.initiator, CEC_DEVICE_TYPE_RECORDING_DEVICE); |
| 241 | |
| 242 | ActivateSource(); |
| 243 | } |
| 244 | |
| 245 | void CSLCommandHandler::TransmitVendorCommandSetDeviceMode(const cec_logical_address iSource, const cec_logical_address iDestination, const cec_device_type type) |
| 246 | { |
| 247 | cec_command response; |
| 248 | cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND); |
| 249 | response.PushBack(SL_COMMAND_SET_DEVICE_MODE); |
| 250 | response.PushBack((uint8_t)type); |
| 251 | Transmit(response); |
| 252 | } |
| 253 | |
| 254 | bool CSLCommandHandler::HandleGiveDeckStatus(const cec_command &command) |
| 255 | { |
| 256 | if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination)) |
| 257 | { |
| 258 | CCECBusDevice *device = GetDevice(command.destination); |
| 259 | if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE)) |
| 260 | { |
| 261 | if (command.parameters.size > 0) |
| 262 | { |
| 263 | ((CCECPlaybackDevice *) device)->SetDeckStatus(!device->IsActiveSource() ? CEC_DECK_INFO_OTHER_STATUS : CEC_DECK_INFO_OTHER_STATUS_LG); |
| 264 | if (command.parameters[0] == CEC_STATUS_REQUEST_ON) |
| 265 | { |
| 266 | bool bReturn = ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator); |
| 267 | if (!ActiveSourceSent()) |
| 268 | ActivateSource(); |
| 269 | return bReturn; |
| 270 | } |
| 271 | else if (command.parameters[0] == CEC_STATUS_REQUEST_ONCE) |
| 272 | { |
| 273 | return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator); |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | return CCECCommandHandler::HandleGiveDeckStatus(command); |
| 278 | } |
| 279 | |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | bool CSLCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command) |
| 284 | { |
| 285 | bool bReturn(false); |
| 286 | if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination) && command.initiator == CECDEVICE_TV) |
| 287 | { |
| 288 | CCECBusDevice *device = GetDevice(command.destination); |
| 289 | if (device && device->GetCurrentPowerStatus() != CEC_POWER_STATUS_ON) |
| 290 | { |
| 291 | bReturn = device->TransmitPowerState(command.initiator); |
| 292 | device->SetPowerStatus(CEC_POWER_STATUS_ON); |
| 293 | } |
| 294 | else |
| 295 | { |
| 296 | if (!ActiveSourceSent()) |
| 297 | { |
| 298 | device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); |
| 299 | bReturn = device->TransmitPowerState(command.initiator); |
| 300 | ActivateSource(); |
| 301 | } |
| 302 | else if (m_resetPowerState.IsSet() && m_resetPowerState.TimeLeft() > 0) |
| 303 | { |
| 304 | /* TODO assume that we've bugged out. the return button no longer works after this */ |
| 305 | 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"); |
| 306 | { |
| 307 | CLockObject lock(m_SLMutex); |
| 308 | m_bActiveSourceSent = false; |
| 309 | } |
| 310 | device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); |
| 311 | bReturn = device->TransmitPowerState(command.initiator); |
| 312 | device->SetPowerStatus(CEC_POWER_STATUS_ON); |
| 313 | m_resetPowerState.Init(5000); |
| 314 | } |
| 315 | else |
| 316 | { |
| 317 | bReturn = device->TransmitPowerState(command.initiator); |
| 318 | m_resetPowerState.Init(5000); |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | return bReturn; |
| 324 | } |
| 325 | |
| 326 | bool CSLCommandHandler::HandleRequestActiveSource(const cec_command &command) |
| 327 | { |
| 328 | if (m_processor->CECInitialised()) |
| 329 | { |
| 330 | if (ActiveSourceSent()) |
| 331 | LIB_CEC->AddLog(CEC_LOG_DEBUG, ">> %i requests active source, ignored", (uint8_t) command.initiator); |
| 332 | else |
| 333 | ActivateSource(); |
| 334 | return true; |
| 335 | } |
| 336 | return false; |
| 337 | } |
| 338 | |
| 339 | bool CSLCommandHandler::HandleFeatureAbort(const cec_command &command) |
| 340 | { |
| 341 | if (command.parameters.size == 0 && m_processor->GetPrimaryDevice()->GetCurrentPowerStatus() == CEC_POWER_STATUS_ON && !SLInitialised() && |
| 342 | command.initiator == CECDEVICE_TV) |
| 343 | { |
| 344 | m_processor->GetPrimaryDevice()->TransmitPowerState(command.initiator); |
| 345 | m_processor->GetPrimaryDevice()->TransmitVendorID(CECDEVICE_BROADCAST, false); |
| 346 | } |
| 347 | |
| 348 | return CCECCommandHandler::HandleFeatureAbort(command); |
| 349 | } |
| 350 | |
| 351 | bool CSLCommandHandler::HandleStandby(const cec_command &command) |
| 352 | { |
| 353 | if (command.initiator == CECDEVICE_TV) |
| 354 | { |
| 355 | CLockObject lock(m_SLMutex); |
| 356 | m_bActiveSourceSent = false; |
| 357 | } |
| 358 | |
| 359 | return CCECCommandHandler::HandleStandby(command); |
| 360 | } |
| 361 | |
| 362 | void CSLCommandHandler::ResetSLState(void) |
| 363 | { |
| 364 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "resetting SL initialised state"); |
| 365 | CLockObject lock(m_SLMutex); |
| 366 | m_bSLEnabled = false; |
| 367 | m_bActiveSourceSent = false; |
| 368 | m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); |
| 369 | } |
| 370 | |
| 371 | void CSLCommandHandler::SetSLInitialised(void) |
| 372 | { |
| 373 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "SL initialised"); |
| 374 | CLockObject lock(m_SLMutex); |
| 375 | m_bSLEnabled = true; |
| 376 | } |
| 377 | |
| 378 | bool CSLCommandHandler::SLInitialised(void) |
| 379 | { |
| 380 | CLockObject lock(m_SLMutex); |
| 381 | return m_bSLEnabled; |
| 382 | } |
| 383 | |
| 384 | bool CSLCommandHandler::ActiveSourceSent(void) |
| 385 | { |
| 386 | CLockObject lock(m_SLMutex); |
| 387 | return m_bActiveSourceSent; |
| 388 | } |
| 389 | |
| 390 | bool CSLCommandHandler::PowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination) |
| 391 | { |
| 392 | if (iDestination != CECDEVICE_TV) |
| 393 | { |
| 394 | /* LG devices only allow themselves to be woken up by the TV with a vendor command */ |
| 395 | cec_command command; |
| 396 | |
| 397 | if (!m_bSLEnabled) |
| 398 | TransmitVendorID(CECDEVICE_TV, CEC_VENDOR_LG); |
| 399 | |
| 400 | cec_command::Format(command, CECDEVICE_TV, iDestination, CEC_OPCODE_VENDOR_COMMAND); |
| 401 | command.PushBack(SL_COMMAND_POWER_ON); |
| 402 | command.PushBack(0); |
| 403 | return Transmit(command); |
| 404 | } |
| 405 | |
| 406 | return CCECCommandHandler::PowerOn(iInitiator, iDestination); |
| 407 | } |