cec: fix handler init
[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
0d4c3a7b
LOK
41#define SL_COMMAND_UNKNOWN_01 0x01
42#define SL_COMMAND_UNKNOWN_02 0x02
43#define SL_COMMAND_UNKNOWN_03 0x05
11d13a02 44
0d4c3a7b 45#define SL_COMMAND_REQUEST_POWER_STATUS 0xa0
9a0d7b9f 46#define SL_COMMAND_POWER_ON 0x03
0d4c3a7b
LOK
47#define SL_COMMAND_CONNECT_REQUEST 0x04
48#define SL_COMMAND_CONNECT_ACCEPT 0x05
11d13a02 49
e9de9629 50CSLCommandHandler::CSLCommandHandler(CCECBusDevice *busDevice) :
855a3a98 51 CCECCommandHandler(busDevice),
b4c4ef7d
LOK
52 m_bAwaitingReceiveFailed(false),
53 m_bSLEnabled(false),
5f316715 54 m_bVendorIdSent(false)
e9de9629 55{
0ecbcd4d 56 /* TODO set to powered off until we fixed the connect on start loop issue */
842262d8 57 m_processor->GetPrimaryDevice()->m_powerStatus = CEC_POWER_STATUS_STANDBY;
e9de9629 58}
e54fd7d2
LOK
59
60bool CSLCommandHandler::HandleVendorCommand(const cec_command &command)
61{
62 if (command.parameters.size == 1 &&
11d13a02 63 command.parameters[0] == SL_COMMAND_UNKNOWN_01)
e54fd7d2 64 {
5f316715 65 HandleVendorCommand01(command);
8d915412 66 return true;
e54fd7d2 67 }
9a0d7b9f
LOK
68 else if (command.parameters.size == 2 &&
69 command.parameters[0] == SL_COMMAND_POWER_ON)
70 {
0ecbcd4d 71 HandleVendorCommandPowerOn(command);
9a0d7b9f
LOK
72 return true;
73 }
797dd7c4 74 else if (command.parameters.size == 2 &&
11d13a02 75 command.parameters[0] == SL_COMMAND_CONNECT_REQUEST)
9902f4e8 76 {
0ecbcd4d 77 HandleVendorCommandSLConnect(command);
8d915412 78 return true;
9902f4e8
LOK
79 }
80 else if (command.parameters.size == 1 &&
0d4c3a7b 81 command.parameters[0] == SL_COMMAND_REQUEST_POWER_STATUS)
9902f4e8 82 {
0ecbcd4d 83 HandleVendorCommandPowerOnStatus(command);
8d915412 84 return true;
9902f4e8 85 }
e54fd7d2
LOK
86
87 return false;
88}
89
5f316715
LOK
90bool CSLCommandHandler::HandleGiveDeckStatus(const cec_command &command)
91{
92 if (command.parameters.size == 1)
93 {
94 if (command.parameters[0] == CEC_STATUS_REQUEST_ONCE ||
95 command.parameters[0] == CEC_STATUS_REQUEST_ON)
96 {
97 TransmitDeckStatus(command.initiator);
98 }
99 else
100 {
101 CCECCommandHandler::HandleGiveDeckStatus(command);
102 }
103 }
104 return true;
105}
106
107void CSLCommandHandler::HandleVendorCommand01(const cec_command &command)
fe6f8e37
LOK
108{
109 TransmitVendorCommand0205(command.destination, command.initiator);
110}
111
112void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource, const cec_logical_address iDestination)
5f316715
LOK
113{
114 cec_command response;
fe6f8e37 115 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
5f316715
LOK
116 response.PushBack(SL_COMMAND_UNKNOWN_02);
117 response.PushBack(SL_COMMAND_UNKNOWN_03);
118
119 Transmit(response);
fe6f8e37 120}
5f316715 121
0e54ee2e 122void CSLCommandHandler::TransmitVendorCommand05(const cec_logical_address iSource, const cec_logical_address iDestination)
fe6f8e37
LOK
123{
124 m_bSLEnabled = true;
125 cec_command response;
126 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
127 response.PushBack(SL_COMMAND_CONNECT_ACCEPT);
128 response.PushBack((uint8_t)iSource);
129 Transmit(response);
5f316715
LOK
130}
131
0ecbcd4d 132void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command &command)
9a0d7b9f 133{
842262d8 134 CCECBusDevice *device = m_processor->GetPrimaryDevice();
9a0d7b9f
LOK
135 if (device)
136 {
fe6f8e37
LOK
137 m_bSLEnabled = true;
138 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
9a0d7b9f 139 device->TransmitPowerState(command.initiator);
fe6f8e37 140 device->TransmitVendorID(command.initiator);
9a0d7b9f 141 TransmitPowerOn(device->GetLogicalAddress(), command.initiator);
9a0d7b9f
LOK
142 }
143}
144
0ecbcd4d 145void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command &command)
5f316715 146{
fe6f8e37 147 m_bSLEnabled = true;
37b0c572 148 m_processor->m_busDevices[command.initiator]->SetActiveSource();
0ecbcd4d 149 m_processor->m_busDevices[command.destination]->TransmitActiveSource();
0e54ee2e 150 TransmitVendorCommand05(command.destination, command.initiator);
5f316715
LOK
151 TransmitDeckStatus(command.initiator);
152}
153
0ecbcd4d 154void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command &command)
5f316715
LOK
155{
156 if (command.destination != CECDEVICE_BROADCAST)
157 {
158 CCECBusDevice *device = m_processor->m_busDevices[m_processor->GetLogicalAddresses().primary];
159 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
160 device->TransmitPowerState(command.initiator);
161 device->SetPowerStatus(CEC_POWER_STATUS_ON);
162 }
163}
164
b4c4ef7d
LOK
165void CSLCommandHandler::TransmitDeckStatus(const cec_logical_address iDestination)
166{
167 /* set deck status for the playback device */
842262d8 168 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
b4c4ef7d
LOK
169 if (primary->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || primary->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE)
170 {
171 ((CCECPlaybackDevice *)primary)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
172 ((CCECPlaybackDevice *)primary)->TransmitDeckStatus(iDestination);
173 }
174}
175
8d915412
LOK
176bool CSLCommandHandler::TransmitLGVendorId(const cec_logical_address iInitiator, const cec_logical_address iDestination)
177{
178 cec_command response;
0d4c3a7b 179 cec_command::Format(response, iInitiator, iDestination, CEC_OPCODE_DEVICE_VENDOR_ID);
8d915412
LOK
180 response.parameters.PushBack((uint8_t) (((uint64_t)CEC_VENDOR_LG >> 16) & 0xFF));
181 response.parameters.PushBack((uint8_t) (((uint64_t)CEC_VENDOR_LG >> 8) & 0xFF));
182 response.parameters.PushBack((uint8_t) ((uint64_t)CEC_VENDOR_LG & 0xFF));
183
8fa35473 184 Transmit(response);
b4c4ef7d 185 return true;
8d915412
LOK
186}
187
e54fd7d2
LOK
188bool CSLCommandHandler::HandleGiveDeviceVendorId(const cec_command &command)
189{
190 /* imitate LG devices */
191 CCECBusDevice *device = GetDevice(command.destination);
192 if (device)
193 device->SetVendorId(CEC_VENDOR_LG);
194
195 return CCECCommandHandler::HandleGiveDeviceVendorId(command);
196}
197
198bool CSLCommandHandler::HandleCommand(const cec_command &command)
199{
200 bool bHandled(false);
5f316715 201
fe6f8e37
LOK
202 if (m_processor->IsStarted() && (m_busDevice->MyLogicalAddressContains(command.destination) ||
203 command.destination == CECDEVICE_BROADCAST))
e54fd7d2
LOK
204 {
205 switch(command.opcode)
206 {
207 case CEC_OPCODE_VENDOR_COMMAND:
208 bHandled = HandleVendorCommand(command);
209 break;
b4c4ef7d
LOK
210 case CEC_OPCODE_FEATURE_ABORT:
211 {
5f316715 212 if (!m_bVendorIdSent)
b4c4ef7d 213 {
5f316715
LOK
214 m_bVendorIdSent = true;
215 TransmitLGVendorId(m_processor->GetLogicalAddresses().primary, CECDEVICE_BROADCAST);
45b97de7 216 }
b4c4ef7d
LOK
217 }
218 bHandled = true;
e54fd7d2
LOK
219 default:
220 break;
221 }
222 }
223
224 if (!bHandled)
225 bHandled = CCECCommandHandler::HandleCommand(command);
226
227 return bHandled;
228}
855a3a98 229
855a3a98
LOK
230void CSLCommandHandler::HandlePoll(const cec_logical_address iInitiator, const cec_logical_address iDestination)
231{
232 CCECCommandHandler::HandlePoll(iInitiator, iDestination);
233 m_bAwaitingReceiveFailed = true;
234}
235
236bool CSLCommandHandler::HandleReceiveFailed(void)
237{
238 if (m_bAwaitingReceiveFailed)
239 {
240 m_bAwaitingReceiveFailed = false;
241 return false;
242 }
243
244 return true;
245}
8d915412
LOK
246
247bool CSLCommandHandler::InitHandler(void)
248{
89a726fa
LOK
249 if (m_bHandlerInited)
250 return true;
251 m_bHandlerInited = true;
252
5f316715
LOK
253 m_processor->SetStandardLineTimeout(3);
254 m_processor->SetRetryLineTimeout(3);
dcd240b2 255
ae693aaa
LOK
256 /* increase the number of retries because the tv is keeping the bus busy at times */
257 m_iTransmitWait = 2000;
258 m_iTransmitRetries = 4;
259 m_iTransmitTimeout = 500;
260
842262d8 261 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
d211708b 262 if (m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
56035c86 263 {
d211708b 264 primary->SetVendorId(CEC_VENDOR_LG, false);
56035c86
LOK
265 primary->TransmitVendorID(CECDEVICE_TV, false);
266 }
b4c4ef7d 267
37acf382
LOK
268 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
269 {
37acf382
LOK
270 /* LG TVs don't always reply to CEC version requests, so just set it to 1.3a */
271 m_busDevice->SetCecVersion(CEC_VERSION_1_3A);
272 }
caaf64d7 273
37acf382
LOK
274 /* LG devices always return "korean" as language */
275 cec_menu_language lang;
276 lang.device = m_busDevice->GetLogicalAddress();
277 snprintf(lang.language, 4, "eng");
278 m_busDevice->SetMenuLanguage(lang);
caaf64d7 279
37acf382
LOK
280 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
281 {
fe6f8e37
LOK
282 m_processor->SetActiveSource();
283
37acf382 284 /* LG TVs only route keypresses when the deck status is set to 0x20 */
5f316715 285 cec_logical_addresses addr = m_processor->GetLogicalAddresses();
37acf382 286 for (uint8_t iPtr = 0; iPtr < 15; iPtr++)
8d915412 287 {
5f316715 288 CCECBusDevice *device = m_processor->m_busDevices[iPtr];
37acf382 289
37acf382 290 if (addr[iPtr])
8d915412 291 {
37acf382
LOK
292 if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE ||
293 device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
294 {
295 ((CCECPlaybackDevice *)device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
a45656ee 296 ((CCECPlaybackDevice *)device)->TransmitDeckStatus(CECDEVICE_TV);
37acf382 297 }
8d915412
LOK
298 }
299 }
300 }
37acf382 301
8d915412
LOK
302 return true;
303}
0d4c3a7b 304
0d4c3a7b
LOK
305bool CSLCommandHandler::TransmitPowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
306{
5f316715 307 if (iDestination != CECDEVICE_BROADCAST &&
9a0d7b9f
LOK
308 iDestination != CECDEVICE_TV &&
309 m_processor->m_busDevices[iDestination]->GetVendorId(false) == CEC_VENDOR_LG)
5f316715
LOK
310 {
311 cec_command command;
312 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_VENDOR_COMMAND);
9a0d7b9f
LOK
313 command.parameters.PushBack((uint8_t)SL_COMMAND_POWER_ON);
314 command.parameters.PushBack(0x00);
5f316715
LOK
315 return Transmit(command);
316 }
0d4c3a7b 317
5f316715 318 return CCECCommandHandler::TransmitPowerOn(iInitiator, iDestination);
0d4c3a7b 319}
fe6f8e37 320