| 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 | CSLCommandHandler::CSLCommandHandler(CCECBusDevice *busDevice) : |
| 41 | CCECCommandHandler(busDevice), |
| 42 | m_bAwaitingReceiveFailed(false) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | bool CSLCommandHandler::HandleVendorCommand(const cec_command &command) |
| 47 | { |
| 48 | if (command.parameters.size == 1 && |
| 49 | command.parameters[0] == 0x01) |
| 50 | { |
| 51 | /* enable SL */ |
| 52 | cec_command response; |
| 53 | cec_command::Format(response, command.destination, command.initiator, CEC_OPCODE_VENDOR_COMMAND, m_busDevice->GetTransmitTimeout()); |
| 54 | response.PushBack(0x02); |
| 55 | response.PushBack(0x05); |
| 56 | |
| 57 | m_busDevice->GetProcessor()->Transmit(response); |
| 58 | TransmitLGVendorId(command.destination, command.initiator); |
| 59 | return true; |
| 60 | } |
| 61 | else if (command.parameters.size >= 1 && |
| 62 | command.parameters[0] == 0x04) |
| 63 | { |
| 64 | CCECBusDevice *primary = m_busDevice->GetProcessor()->m_busDevices[m_busDevice->GetProcessor()->GetLogicalAddresses().primary]; |
| 65 | if (primary->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || primary->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE) |
| 66 | { |
| 67 | ((CCECPlaybackDevice *)primary)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG); |
| 68 | ((CCECPlaybackDevice *)primary)->TransmitDeckStatus(command.initiator); |
| 69 | } |
| 70 | return true; |
| 71 | } |
| 72 | else if (command.parameters.size == 1 && |
| 73 | command.parameters[0] == 0xa0) |
| 74 | { |
| 75 | TransmitLGVendorId(command.destination, command.initiator); |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | bool CSLCommandHandler::TransmitLGVendorId(const cec_logical_address iInitiator, const cec_logical_address iDestination) |
| 83 | { |
| 84 | cec_command response; |
| 85 | cec_command::Format(response, iInitiator, iDestination, CEC_OPCODE_VENDOR_COMMAND, m_busDevice->GetTransmitTimeout()); |
| 86 | response.parameters.PushBack((uint8_t) (((uint64_t)CEC_VENDOR_LG >> 16) & 0xFF)); |
| 87 | response.parameters.PushBack((uint8_t) (((uint64_t)CEC_VENDOR_LG >> 8) & 0xFF)); |
| 88 | response.parameters.PushBack((uint8_t) ((uint64_t)CEC_VENDOR_LG & 0xFF)); |
| 89 | |
| 90 | return m_busDevice->GetProcessor()->Transmit(response); |
| 91 | } |
| 92 | |
| 93 | bool CSLCommandHandler::HandleGiveDeviceVendorId(const cec_command &command) |
| 94 | { |
| 95 | /* imitate LG devices */ |
| 96 | CCECBusDevice *device = GetDevice(command.destination); |
| 97 | if (device) |
| 98 | device->SetVendorId(CEC_VENDOR_LG); |
| 99 | |
| 100 | return CCECCommandHandler::HandleGiveDeviceVendorId(command); |
| 101 | } |
| 102 | |
| 103 | bool CSLCommandHandler::HandleCommand(const cec_command &command) |
| 104 | { |
| 105 | bool bHandled(false); |
| 106 | if (m_busDevice->MyLogicalAddressContains(command.destination)) |
| 107 | { |
| 108 | switch(command.opcode) |
| 109 | { |
| 110 | case CEC_OPCODE_VENDOR_COMMAND: |
| 111 | bHandled = HandleVendorCommand(command); |
| 112 | break; |
| 113 | case CEC_OPCODE_DEVICE_VENDOR_ID: |
| 114 | { |
| 115 | if (command.initiator == m_busDevice->GetLogicalAddress()) |
| 116 | { |
| 117 | TransmitLGVendorId(m_busDevice->GetProcessor()->GetLogicalAddresses().primary, command.initiator); |
| 118 | bHandled = true; |
| 119 | } |
| 120 | } |
| 121 | break; |
| 122 | default: |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if (!bHandled) |
| 128 | bHandled = CCECCommandHandler::HandleCommand(command); |
| 129 | |
| 130 | return bHandled; |
| 131 | } |
| 132 | |
| 133 | void CSLCommandHandler::HandlePoll(const cec_logical_address iInitiator, const cec_logical_address iDestination) |
| 134 | { |
| 135 | CCECCommandHandler::HandlePoll(iInitiator, iDestination); |
| 136 | m_bAwaitingReceiveFailed = true; |
| 137 | } |
| 138 | |
| 139 | bool CSLCommandHandler::HandleReceiveFailed(void) |
| 140 | { |
| 141 | if (m_bAwaitingReceiveFailed) |
| 142 | { |
| 143 | m_bAwaitingReceiveFailed = false; |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | bool CSLCommandHandler::InitHandler(void) |
| 151 | { |
| 152 | if (m_busDevice->GetLogicalAddress() != CECDEVICE_TV) |
| 153 | return true; |
| 154 | |
| 155 | /* LG TVs don't always reply to CEC version requests, so just set it to 1.3a */ |
| 156 | m_busDevice->GetProcessor()->m_busDevices[CECDEVICE_TV]->SetCecVersion(CEC_VERSION_1_3A); |
| 157 | |
| 158 | /* LG TVs only route keypresses when the deck status is set to 0x20 */ |
| 159 | cec_logical_addresses addr = m_busDevice->GetProcessor()->GetLogicalAddresses(); |
| 160 | for (uint8_t iPtr = 0; iPtr < 15; iPtr++) |
| 161 | { |
| 162 | CCECBusDevice *device = m_busDevice->GetProcessor()->m_busDevices[iPtr]; |
| 163 | |
| 164 | /* increase the transmit timeout because the tv is keeping the bus busy at times */ |
| 165 | device->SetTransmitTimeout(3000); |
| 166 | if (addr[iPtr]) |
| 167 | { |
| 168 | if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || |
| 169 | device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE)) |
| 170 | { |
| 171 | ((CCECPlaybackDevice *)device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG); |
| 172 | ((CCECPlaybackDevice *)device)->TransmitDeckStatus(CECDEVICE_TV); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | return true; |
| 177 | } |