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