| 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 | |
| 41 | #define SL_COMMAND_UNKNOWN_01 0x01 |
| 42 | #define SL_COMMAND_UNKNOWN_02 0x02 |
| 43 | #define SL_COMMAND_UNKNOWN_03 0x05 |
| 44 | |
| 45 | #define SL_COMMAND_REQUEST_POWER_STATUS 0xa0 |
| 46 | #define SL_COMMAND_POWER_ON 0x03 |
| 47 | #define SL_COMMAND_CONNECT_REQUEST 0x04 |
| 48 | #define SL_COMMAND_SET_DEVICE_MODE 0x05 |
| 49 | |
| 50 | CSLCommandHandler::CSLCommandHandler(CCECBusDevice *busDevice) : |
| 51 | CCECCommandHandler(busDevice), |
| 52 | m_bSLEnabled(false), |
| 53 | m_bPowerStateReset(false), |
| 54 | m_bActiveSourceSent(false), |
| 55 | m_bVendorIdSent(false) |
| 56 | { |
| 57 | m_vendorId = CEC_VENDOR_LG; |
| 58 | CCECBusDevice *primary = m_processor->GetPrimaryDevice(); |
| 59 | |
| 60 | /* imitate LG devices */ |
| 61 | if (primary && m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress()) |
| 62 | primary->SetVendorId(CEC_VENDOR_LG); |
| 63 | |
| 64 | /* LG TVs don't always reply to CEC version requests, so just set it to 1.3a */ |
| 65 | if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV) |
| 66 | m_busDevice->SetCecVersion(CEC_VERSION_1_3A); |
| 67 | |
| 68 | /* LG devices always return "korean" as language */ |
| 69 | cec_menu_language lang; |
| 70 | lang.device = m_busDevice->GetLogicalAddress(); |
| 71 | snprintf(lang.language, 4, "eng"); |
| 72 | m_busDevice->SetMenuLanguage(lang); |
| 73 | } |
| 74 | |
| 75 | bool CSLCommandHandler::InitHandler(void) |
| 76 | { |
| 77 | if (m_bHandlerInited) |
| 78 | return true; |
| 79 | m_bHandlerInited = true; |
| 80 | |
| 81 | CCECBusDevice *primary = m_processor->GetPrimaryDevice(); |
| 82 | if (m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress()) |
| 83 | { |
| 84 | /* start as 'in transition standby->on' */ |
| 85 | primary->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); |
| 86 | primary->TransmitPowerState(CECDEVICE_TV); |
| 87 | } |
| 88 | |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | bool CSLCommandHandler::ActivateSource(void) |
| 93 | { |
| 94 | /* reply with LGs vendor id */ |
| 95 | if (!m_bVendorIdSent) |
| 96 | { |
| 97 | m_bVendorIdSent = true; |
| 98 | m_processor->GetPrimaryDevice()->TransmitVendorID(CECDEVICE_BROADCAST, false); |
| 99 | } |
| 100 | |
| 101 | if (!m_bSLEnabled) |
| 102 | return true; |
| 103 | |
| 104 | if (m_bActiveSourceSent) |
| 105 | return true; |
| 106 | m_bActiveSourceSent = true; |
| 107 | |
| 108 | CCECBusDevice *primary = m_processor->GetPrimaryDevice(); |
| 109 | primary->SetActiveSource(); |
| 110 | primary->TransmitActiveSource(); |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | bool CSLCommandHandler::HandleActiveSource(const cec_command &command) |
| 115 | { |
| 116 | if (command.parameters.size == 2) |
| 117 | { |
| 118 | uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]); |
| 119 | if (iAddress != m_busDevice->GetPhysicalAddress(false)) |
| 120 | m_bSLEnabled = false; |
| 121 | return m_processor->SetActiveSource(iAddress); |
| 122 | } |
| 123 | |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | bool CSLCommandHandler::HandleDeviceVendorId(const cec_command &command) |
| 128 | { |
| 129 | SetVendorId(command); |
| 130 | |
| 131 | if (!m_bSLEnabled) |
| 132 | { |
| 133 | cec_command response; |
| 134 | cec_command::Format(response, m_processor->GetLogicalAddress(), command.initiator, CEC_OPCODE_FEATURE_ABORT); |
| 135 | return Transmit(response); |
| 136 | } |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | bool CSLCommandHandler::HandleGivePhysicalAddress(const cec_command &command) |
| 141 | { |
| 142 | if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination)) |
| 143 | { |
| 144 | CCECBusDevice *device = GetDevice(command.destination); |
| 145 | if (device) |
| 146 | return device->TransmitPhysicalAddress(); // only the physical address, don't send image view on |
| 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 | m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); |
| 185 | TransmitVendorCommand0205(command.destination, command.initiator); |
| 186 | } |
| 187 | |
| 188 | void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource, const cec_logical_address iDestination) |
| 189 | { |
| 190 | cec_command response; |
| 191 | cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND); |
| 192 | response.PushBack(SL_COMMAND_UNKNOWN_02); |
| 193 | response.PushBack(SL_COMMAND_UNKNOWN_03); |
| 194 | |
| 195 | Transmit(response, false); |
| 196 | } |
| 197 | |
| 198 | void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command &command) |
| 199 | { |
| 200 | CCECBusDevice *device = m_processor->GetPrimaryDevice(); |
| 201 | if (device) |
| 202 | { |
| 203 | m_bSLEnabled = true; |
| 204 | |
| 205 | device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); //XXX |
| 206 | device->TransmitPowerState(command.initiator); |
| 207 | device->SetPowerStatus(CEC_POWER_STATUS_ON); |
| 208 | |
| 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 | TransmitVendorCommandSetDeviceMode(m_processor->GetLogicalAddress(), command.initiator, CEC_DEVICE_TYPE_RECORDING_DEVICE); |
| 228 | |
| 229 | ActivateSource(); |
| 230 | } |
| 231 | |
| 232 | void CSLCommandHandler::TransmitVendorCommandSetDeviceMode(const cec_logical_address iSource, const cec_logical_address iDestination, const cec_device_type type) |
| 233 | { |
| 234 | cec_command response; |
| 235 | cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND); |
| 236 | response.PushBack(SL_COMMAND_SET_DEVICE_MODE); |
| 237 | response.PushBack((uint8_t)type); |
| 238 | Transmit(response, false); |
| 239 | } |
| 240 | |
| 241 | bool CSLCommandHandler::HandleGiveDeckStatus(const cec_command &command) |
| 242 | { |
| 243 | if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination)) |
| 244 | { |
| 245 | CCECBusDevice *device = GetDevice(command.destination); |
| 246 | if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE)) |
| 247 | { |
| 248 | if (command.parameters.size > 0) |
| 249 | { |
| 250 | if (command.parameters[0] == CEC_STATUS_REQUEST_ON) |
| 251 | { |
| 252 | ((CCECPlaybackDevice *) device)->SetDeckStatus(CEC_DECK_INFO_STOP); |
| 253 | return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator) && |
| 254 | device->TransmitImageViewOn() && |
| 255 | device->TransmitPhysicalAddress(); |
| 256 | } |
| 257 | else if (command.parameters[0] == CEC_STATUS_REQUEST_ONCE) |
| 258 | { |
| 259 | ((CCECPlaybackDevice *) device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG); |
| 260 | return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | return CCECCommandHandler::HandleGiveDeckStatus(command); |
| 265 | } |
| 266 | |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | bool CSLCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command) |
| 271 | { |
| 272 | bool bReturn(false); |
| 273 | if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination)) |
| 274 | { |
| 275 | CCECBusDevice *device = GetDevice(command.destination); |
| 276 | if (device && device->GetPowerStatus(false) != CEC_POWER_STATUS_ON) |
| 277 | { |
| 278 | bReturn = device->TransmitPowerState(command.initiator); |
| 279 | device->SetPowerStatus(CEC_POWER_STATUS_ON); |
| 280 | } |
| 281 | else |
| 282 | { |
| 283 | if (!m_bActiveSourceSent) |
| 284 | { |
| 285 | device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); |
| 286 | bReturn = device->TransmitPowerState(command.initiator); |
| 287 | ActivateSource(); |
| 288 | } |
| 289 | else if (m_resetPowerState.IsSet() && m_resetPowerState.TimeLeft() > 0) |
| 290 | { |
| 291 | /* assume that we've bugged out */ |
| 292 | CLibCEC::AddLog(CEC_LOG_NOTICE, "LG seems to have bugged out. resetting to 'in transition standby to on'"); |
| 293 | m_bActiveSourceSent = false; |
| 294 | device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); |
| 295 | bReturn = device->TransmitPowerState(command.initiator); |
| 296 | device->SetPowerStatus(CEC_POWER_STATUS_ON); |
| 297 | m_resetPowerState.Init(5000); |
| 298 | } |
| 299 | else |
| 300 | { |
| 301 | bReturn = device->TransmitPowerState(command.initiator); |
| 302 | m_resetPowerState.Init(5000); |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | return bReturn; |
| 308 | } |
| 309 | |
| 310 | bool CSLCommandHandler::HandleRequestActiveSource(const cec_command &command) |
| 311 | { |
| 312 | if (m_processor->IsRunning()) |
| 313 | { |
| 314 | CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %i requests active source, ignored", (uint8_t) command.initiator); |
| 315 | return true; |
| 316 | } |
| 317 | return false; |
| 318 | } |
| 319 | |
| 320 | bool CSLCommandHandler::HandleFeatureAbort(const cec_command &command) |
| 321 | { |
| 322 | if (command.parameters.size == 0 && m_processor->GetPrimaryDevice()->GetPowerStatus() == CEC_POWER_STATUS_ON && !m_bSLEnabled) |
| 323 | { |
| 324 | m_processor->GetPrimaryDevice()->TransmitPowerState(command.initiator); |
| 325 | m_processor->GetPrimaryDevice()->TransmitVendorID(CECDEVICE_BROADCAST, false); |
| 326 | } |
| 327 | |
| 328 | return CCECCommandHandler::HandleFeatureAbort(command); |
| 329 | } |
| 330 | |
| 331 | bool CSLCommandHandler::HandleStandby(const cec_command &command) |
| 332 | { |
| 333 | if (command.initiator == CECDEVICE_TV) |
| 334 | { |
| 335 | m_bSLEnabled = false; |
| 336 | m_bPowerStateReset = false; |
| 337 | m_bActiveSourceSent = false; |
| 338 | } |
| 339 | |
| 340 | CCECBusDevice *device = GetDevice(command.initiator); |
| 341 | if (device) |
| 342 | device->SetPowerStatus(CEC_POWER_STATUS_STANDBY); |
| 343 | |
| 344 | return true; |
| 345 | } |