cec: refactor CEC read/write. keep a single lock for all writes, not one per device
[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"
b4c4ef7d 37#include "../platform/timeutils.h"
e9de9629
LOK
38
39using namespace CEC;
40
11d13a02
LOK
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_VENDOR_ID 0xa0
46#define SL_COMMAND_CONNECT_REQUEST 0x04
47#define SL_COMMAND_CONNECT_ACCEPT 0x05
48
e9de9629 49CSLCommandHandler::CSLCommandHandler(CCECBusDevice *busDevice) :
855a3a98 50 CCECCommandHandler(busDevice),
b4c4ef7d
LOK
51 m_bAwaitingReceiveFailed(false),
52 m_bSLEnabled(false),
53 m_bSkipNextVendorId(false)
e9de9629
LOK
54{
55}
e54fd7d2
LOK
56
57bool CSLCommandHandler::HandleVendorCommand(const cec_command &command)
58{
59 if (command.parameters.size == 1 &&
11d13a02 60 command.parameters[0] == SL_COMMAND_UNKNOWN_01)
e54fd7d2
LOK
61 {
62 /* enable SL */
63 cec_command response;
8fa35473 64 cec_command::Format(response, command.destination, command.initiator, CEC_OPCODE_VENDOR_COMMAND, m_iTransmitTimeout);
11d13a02
LOK
65 response.PushBack(SL_COMMAND_UNKNOWN_02);
66 response.PushBack(SL_COMMAND_UNKNOWN_03);
e54fd7d2 67
8fa35473 68 Transmit(response);
8d915412 69 return true;
e54fd7d2 70 }
797dd7c4 71 else if (command.parameters.size == 2 &&
11d13a02 72 command.parameters[0] == SL_COMMAND_CONNECT_REQUEST)
9902f4e8 73 {
797dd7c4
LOK
74 /* enable SL */
75 cec_command response;
8fa35473 76 cec_command::Format(response, command.destination, command.initiator, CEC_OPCODE_VENDOR_COMMAND, m_iTransmitTimeout);
11d13a02 77 response.PushBack(SL_COMMAND_CONNECT_ACCEPT);
b4c4ef7d 78 response.PushBack(m_busDevice->GetProcessor()->GetLogicalAddresses().primary);
8fa35473 79 Transmit(response);
797dd7c4 80
11d13a02 81 /* set deck status for the playback device */
b4c4ef7d 82 TransmitDeckStatus(command.initiator);
8d915412 83 return true;
9902f4e8
LOK
84 }
85 else if (command.parameters.size == 1 &&
11d13a02 86 command.parameters[0] == SL_COMMAND_REQUEST_VENDOR_ID)
9902f4e8 87 {
45b97de7
LOK
88// if (command.destination != CECDEVICE_BROADCAST)
89// m_busDevice->GetProcessor()->m_busDevices[m_busDevice->GetProcessor()->GetLogicalAddresses().primary]->TransmitPowerState(command.initiator);
8d915412 90 return true;
9902f4e8 91 }
e54fd7d2
LOK
92
93 return false;
94}
95
b4c4ef7d
LOK
96void CSLCommandHandler::TransmitDeckStatus(const cec_logical_address iDestination)
97{
98 /* set deck status for the playback device */
99 CCECBusDevice *primary = m_busDevice->GetProcessor()->m_busDevices[m_busDevice->GetProcessor()->GetLogicalAddresses().primary];
100 if (primary->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || primary->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE)
101 {
102 ((CCECPlaybackDevice *)primary)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
103 ((CCECPlaybackDevice *)primary)->TransmitDeckStatus(iDestination);
104 }
105}
106
8d915412
LOK
107bool CSLCommandHandler::TransmitLGVendorId(const cec_logical_address iInitiator, const cec_logical_address iDestination)
108{
109 cec_command response;
8fa35473 110 cec_command::Format(response, iInitiator, iDestination, CEC_OPCODE_DEVICE_VENDOR_ID, m_iTransmitTimeout);
8d915412
LOK
111 response.parameters.PushBack((uint8_t) (((uint64_t)CEC_VENDOR_LG >> 16) & 0xFF));
112 response.parameters.PushBack((uint8_t) (((uint64_t)CEC_VENDOR_LG >> 8) & 0xFF));
113 response.parameters.PushBack((uint8_t) ((uint64_t)CEC_VENDOR_LG & 0xFF));
114
8fa35473 115 Transmit(response);
b4c4ef7d 116 return true;
8d915412
LOK
117}
118
e54fd7d2
LOK
119bool CSLCommandHandler::HandleGiveDeviceVendorId(const cec_command &command)
120{
121 /* imitate LG devices */
122 CCECBusDevice *device = GetDevice(command.destination);
123 if (device)
124 device->SetVendorId(CEC_VENDOR_LG);
125
126 return CCECCommandHandler::HandleGiveDeviceVendorId(command);
127}
128
129bool CSLCommandHandler::HandleCommand(const cec_command &command)
130{
131 bool bHandled(false);
b4c4ef7d
LOK
132 if (m_busDevice->MyLogicalAddressContains(command.destination) ||
133 command.destination == CECDEVICE_BROADCAST)
e54fd7d2
LOK
134 {
135 switch(command.opcode)
136 {
137 case CEC_OPCODE_VENDOR_COMMAND:
138 bHandled = HandleVendorCommand(command);
139 break;
b4c4ef7d 140 case CEC_OPCODE_GIVE_DECK_STATUS:
8d915412 141 {
b4c4ef7d 142 if (command.parameters.size == 1)
8d915412 143 {
b4c4ef7d
LOK
144 if (command.parameters[0] == CEC_STATUS_REQUEST_ONCE ||
145 command.parameters[0] == CEC_STATUS_REQUEST_ON)
146 TransmitDeckStatus(command.initiator);
147 else
148 CCECCommandHandler::HandleCommand(command);
8d915412 149 }
b4c4ef7d 150 bHandled = true;
8d915412
LOK
151 }
152 break;
b4c4ef7d
LOK
153 case CEC_OPCODE_FEATURE_ABORT:
154 {
155 if (!m_bSkipNextVendorId)
156 {
157 m_bSkipNextVendorId = true;
158 TransmitLGVendorId(m_busDevice->GetProcessor()->GetLogicalAddresses().primary, CECDEVICE_BROADCAST);
159 }
45b97de7
LOK
160 else
161 {
162 m_bSkipNextVendorId = false;
163 }
b4c4ef7d
LOK
164 m_bSLEnabled = false;
165 }
166 bHandled = true;
e54fd7d2
LOK
167 default:
168 break;
169 }
170 }
171
172 if (!bHandled)
173 bHandled = CCECCommandHandler::HandleCommand(command);
174
175 return bHandled;
176}
855a3a98 177
855a3a98
LOK
178void CSLCommandHandler::HandlePoll(const cec_logical_address iInitiator, const cec_logical_address iDestination)
179{
180 CCECCommandHandler::HandlePoll(iInitiator, iDestination);
181 m_bAwaitingReceiveFailed = true;
182}
183
184bool CSLCommandHandler::HandleReceiveFailed(void)
185{
186 if (m_bAwaitingReceiveFailed)
187 {
188 m_bAwaitingReceiveFailed = false;
189 return false;
190 }
191
192 return true;
193}
8d915412
LOK
194
195bool CSLCommandHandler::InitHandler(void)
196{
b4c4ef7d
LOK
197 if (m_bSLEnabled)
198 return true;
199 m_bSLEnabled = true;
200
dcd240b2
LOK
201 m_busDevice->GetProcessor()->SetStandardLineTimeout(3);
202 m_busDevice->GetProcessor()->SetRetryLineTimeout(3);
203
b4c4ef7d 204 CCECBusDevice *primary = m_busDevice->GetProcessor()->m_busDevices[m_busDevice->GetProcessor()->GetLogicalAddresses().primary];
d211708b
LOK
205 if (m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
206 primary->SetVendorId(CEC_VENDOR_LG, false);
b4c4ef7d 207
37acf382
LOK
208 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
209 {
210 primary->TransmitVendorID(CECDEVICE_TV);
211 primary->TransmitPhysicalAddress();
212
213 cec_command command;
8fa35473
LOK
214 cec_command::Format(command, m_busDevice->GetProcessor()->GetLogicalAddresses().primary, CECDEVICE_TV, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID, m_iTransmitTimeout);
215 Transmit(command);
b4c4ef7d 216
37acf382
LOK
217 /* LG TVs don't always reply to CEC version requests, so just set it to 1.3a */
218 m_busDevice->SetCecVersion(CEC_VERSION_1_3A);
219 }
caaf64d7 220
37acf382
LOK
221 /* LG devices always return "korean" as language */
222 cec_menu_language lang;
223 lang.device = m_busDevice->GetLogicalAddress();
224 snprintf(lang.language, 4, "eng");
225 m_busDevice->SetMenuLanguage(lang);
caaf64d7 226
8fa35473
LOK
227 /* increase the transmit timeout because the tv is keeping the bus busy at times */
228 m_iTransmitTimeout = 5000;
229
37acf382
LOK
230 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
231 {
232 /* LG TVs only route keypresses when the deck status is set to 0x20 */
233 cec_logical_addresses addr = m_busDevice->GetProcessor()->GetLogicalAddresses();
234 for (uint8_t iPtr = 0; iPtr < 15; iPtr++)
8d915412 235 {
37acf382
LOK
236 CCECBusDevice *device = m_busDevice->GetProcessor()->m_busDevices[iPtr];
237
37acf382 238 if (addr[iPtr])
8d915412 239 {
37acf382
LOK
240 if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE ||
241 device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
242 {
243 ((CCECPlaybackDevice *)device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
244 TransmitDeckStatus(CECDEVICE_TV);
245 }
8d915412
LOK
246 }
247 }
248 }
37acf382 249
8d915412
LOK
250 return true;
251}