| 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_CONNECT_ACCEPT 0x05 |
| 49 | |
| 50 | CSLCommandHandler::CSLCommandHandler(CCECBusDevice *busDevice) : |
| 51 | CCECCommandHandler(busDevice), |
| 52 | m_bSLEnabled(false), |
| 53 | m_bPowerStateReset(false), |
| 54 | m_bActiveSourceSent(false) |
| 55 | { |
| 56 | m_vendorId = CEC_VENDOR_LG; |
| 57 | CCECBusDevice *primary = m_processor->GetPrimaryDevice(); |
| 58 | |
| 59 | /* imitate LG devices */ |
| 60 | if (primary && m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress()) |
| 61 | primary->SetVendorId(CEC_VENDOR_LG); |
| 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 | bool CSLCommandHandler::InitHandler(void) |
| 75 | { |
| 76 | if (m_bHandlerInited) |
| 77 | return true; |
| 78 | m_bHandlerInited = true; |
| 79 | |
| 80 | /* reply with LGs vendor id */ |
| 81 | CCECBusDevice *primary = m_processor->GetPrimaryDevice(); |
| 82 | if (m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress()) |
| 83 | primary->TransmitVendorID(CECDEVICE_TV, false); |
| 84 | |
| 85 | primary->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | bool CSLCommandHandler::ActivateSource(void) |
| 90 | { |
| 91 | if (m_bActiveSourceSent) |
| 92 | return false; |
| 93 | m_bActiveSourceSent = true; |
| 94 | |
| 95 | CCECBusDevice *primary = m_processor->GetPrimaryDevice(); |
| 96 | primary->SetActiveSource(); |
| 97 | primary->TransmitActiveSource(); |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | bool CSLCommandHandler::HandleActiveSource(const cec_command &command) |
| 102 | { |
| 103 | if (command.parameters.size == 2) |
| 104 | { |
| 105 | uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]); |
| 106 | if (iAddress != m_busDevice->GetPhysicalAddress(false)) |
| 107 | m_bSLEnabled = false; |
| 108 | return m_processor->SetActiveSource(iAddress); |
| 109 | } |
| 110 | |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | bool CSLCommandHandler::HandleDeviceVendorId(const cec_command &command) |
| 115 | { |
| 116 | SetVendorId(command); |
| 117 | |
| 118 | if (!m_bSLEnabled) |
| 119 | { |
| 120 | cec_command response; |
| 121 | cec_command::Format(response, m_processor->GetLogicalAddress(), command.initiator, CEC_OPCODE_FEATURE_ABORT); |
| 122 | return Transmit(response); |
| 123 | } |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | bool CSLCommandHandler::HandleGivePhysicalAddress(const cec_command &command) |
| 128 | { |
| 129 | if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination)) |
| 130 | { |
| 131 | CCECBusDevice *device = GetDevice(command.destination); |
| 132 | if (device) |
| 133 | return device->TransmitPhysicalAddress(); // only the physical address, don't send image view on |
| 134 | } |
| 135 | |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | bool CSLCommandHandler::HandleVendorCommand(const cec_command &command) |
| 140 | { |
| 141 | if (command.parameters.size == 1 && |
| 142 | command.parameters[0] == SL_COMMAND_UNKNOWN_01) |
| 143 | { |
| 144 | HandleVendorCommand01(command); |
| 145 | return true; |
| 146 | } |
| 147 | else if (command.parameters.size == 2 && |
| 148 | command.parameters[0] == SL_COMMAND_POWER_ON) |
| 149 | { |
| 150 | HandleVendorCommandPowerOn(command); |
| 151 | return true; |
| 152 | } |
| 153 | else if (command.parameters.size == 2 && |
| 154 | command.parameters[0] == SL_COMMAND_CONNECT_REQUEST) |
| 155 | { |
| 156 | HandleVendorCommandSLConnect(command); |
| 157 | return true; |
| 158 | } |
| 159 | else if (command.parameters.size == 1 && |
| 160 | command.parameters[0] == SL_COMMAND_REQUEST_POWER_STATUS) |
| 161 | { |
| 162 | HandleVendorCommandPowerOnStatus(command); |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | void CSLCommandHandler::HandleVendorCommand01(const cec_command &command) |
| 170 | { |
| 171 | m_processor->GetPrimaryDevice()->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); |
| 172 | TransmitVendorCommand0205(command.destination, command.initiator); |
| 173 | } |
| 174 | |
| 175 | void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource, const cec_logical_address iDestination) |
| 176 | { |
| 177 | cec_command response; |
| 178 | cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND); |
| 179 | response.PushBack(SL_COMMAND_UNKNOWN_02); |
| 180 | response.PushBack(SL_COMMAND_UNKNOWN_03); |
| 181 | |
| 182 | Transmit(response, false); |
| 183 | } |
| 184 | |
| 185 | void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command &command) |
| 186 | { |
| 187 | CCECBusDevice *device = m_processor->GetPrimaryDevice(); |
| 188 | if (device) |
| 189 | { |
| 190 | m_bSLEnabled = true; |
| 191 | |
| 192 | device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); //XXX |
| 193 | device->TransmitPowerState(command.initiator); |
| 194 | device->SetPowerStatus(CEC_POWER_STATUS_ON); |
| 195 | |
| 196 | device->SetActiveSource(); |
| 197 | TransmitImageViewOn(device->GetLogicalAddress(), command.initiator); |
| 198 | } |
| 199 | } |
| 200 | void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command &command) |
| 201 | { |
| 202 | if (command.destination != CECDEVICE_BROADCAST) |
| 203 | { |
| 204 | CCECBusDevice *device = m_processor->m_busDevices[m_processor->GetLogicalAddresses().primary]; |
| 205 | device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); |
| 206 | device->TransmitPowerState(command.initiator); |
| 207 | device->SetPowerStatus(CEC_POWER_STATUS_ON); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command &command) |
| 212 | { |
| 213 | m_bSLEnabled = true; |
| 214 | TransmitVendorCommand05(m_processor->GetLogicalAddress(), command.initiator); |
| 215 | |
| 216 | ActivateSource(); |
| 217 | } |
| 218 | |
| 219 | void CSLCommandHandler::TransmitVendorCommand05(const cec_logical_address iSource, const cec_logical_address iDestination) |
| 220 | { |
| 221 | cec_command response; |
| 222 | cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND); |
| 223 | response.PushBack(SL_COMMAND_CONNECT_ACCEPT); |
| 224 | response.PushBack((uint8_t)m_processor->m_busDevices[iSource]->GetType()); |
| 225 | Transmit(response, false); |
| 226 | } |
| 227 | |
| 228 | bool CSLCommandHandler::HandleGiveDeckStatus(const cec_command &command) |
| 229 | { |
| 230 | if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination)) |
| 231 | { |
| 232 | CCECBusDevice *device = GetDevice(command.destination); |
| 233 | if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE)) |
| 234 | { |
| 235 | if (command.parameters.size > 0) |
| 236 | { |
| 237 | if (command.parameters[0] == CEC_STATUS_REQUEST_ON) |
| 238 | { |
| 239 | ((CCECPlaybackDevice *) device)->SetDeckStatus(CEC_DECK_INFO_STOP); |
| 240 | return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator) && |
| 241 | device->TransmitImageViewOn() && |
| 242 | device->TransmitPhysicalAddress(); |
| 243 | } |
| 244 | else if (command.parameters[0] == CEC_STATUS_REQUEST_ONCE) |
| 245 | { |
| 246 | ((CCECPlaybackDevice *) device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG); |
| 247 | return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator); |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | return CCECCommandHandler::HandleGiveDeckStatus(command); |
| 252 | } |
| 253 | |
| 254 | return false; |
| 255 | } |
| 256 | |
| 257 | bool CSLCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command) |
| 258 | { |
| 259 | bool bReturn(false); |
| 260 | if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination)) |
| 261 | { |
| 262 | CCECBusDevice *device = GetDevice(command.destination); |
| 263 | if (device && device->GetPowerStatus(false) != CEC_POWER_STATUS_ON) |
| 264 | { |
| 265 | bReturn = device->TransmitPowerState(command.initiator); |
| 266 | device->SetPowerStatus(CEC_POWER_STATUS_ON); |
| 267 | } |
| 268 | else if (!ActivateSource()) |
| 269 | { |
| 270 | /* assume that we've bugged out */ |
| 271 | CLibCEC::AddLog(CEC_LOG_NOTICE, "LG seems to have bugged out. resetting to 'in transition standby to on'"); |
| 272 | m_bActiveSourceSent = false; |
| 273 | device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); |
| 274 | bReturn = device->TransmitPowerState(command.initiator); |
| 275 | device->SetPowerStatus(CEC_POWER_STATUS_ON); |
| 276 | } |
| 277 | else |
| 278 | { |
| 279 | bReturn = true; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | return bReturn; |
| 284 | } |
| 285 | |
| 286 | bool CSLCommandHandler::HandleRequestActiveSource(const cec_command &command) |
| 287 | { |
| 288 | if (m_processor->IsRunning()) |
| 289 | { |
| 290 | CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %i requests active source, ignored", (uint8_t) command.initiator); |
| 291 | return true; |
| 292 | } |
| 293 | return false; |
| 294 | } |