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