cec: refactored threading/locking - added windows native instead of pthread-win32...
[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
0d4c3a7b
LOK
40#define SL_COMMAND_UNKNOWN_01 0x01
41#define SL_COMMAND_UNKNOWN_02 0x02
42#define SL_COMMAND_UNKNOWN_03 0x05
11d13a02 43
0d4c3a7b 44#define SL_COMMAND_REQUEST_POWER_STATUS 0xa0
9a0d7b9f 45#define SL_COMMAND_POWER_ON 0x03
0d4c3a7b
LOK
46#define SL_COMMAND_CONNECT_REQUEST 0x04
47#define SL_COMMAND_CONNECT_ACCEPT 0x05
11d13a02 48
e9de9629 49CSLCommandHandler::CSLCommandHandler(CCECBusDevice *busDevice) :
855a3a98 50 CCECCommandHandler(busDevice),
b4c4ef7d
LOK
51 m_bAwaitingReceiveFailed(false),
52 m_bSLEnabled(false),
468a1414 53 m_bPowerStateReset(false)
e9de9629 54{
cf4931be 55 m_vendorId = CEC_VENDOR_LG;
79f01d26
LOK
56 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
57
58 /* imitate LG devices */
59 if (m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
3e61b350 60 primary->SetVendorId(CEC_VENDOR_LG);
468a1414 61 SetLGDeckStatus();
79f01d26
LOK
62
63 /* LG TVs don't always reply to CEC version requests, so just set it to 1.3a */
64 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
65 m_busDevice->SetCecVersion(CEC_VERSION_1_3A);
66
67 /* LG devices always return "korean" as language */
68 cec_menu_language lang;
69 lang.device = m_busDevice->GetLogicalAddress();
70 snprintf(lang.language, 4, "eng");
71 m_busDevice->SetMenuLanguage(lang);
9f65e017
LOK
72}
73
468a1414
LOK
74
75void CSLCommandHandler::HandlePoll(const cec_logical_address iInitiator, const cec_logical_address iDestination)
9f65e017 76{
468a1414
LOK
77 CCECCommandHandler::HandlePoll(iInitiator, iDestination);
78 m_bAwaitingReceiveFailed = true;
79}
9f65e017 80
468a1414
LOK
81bool CSLCommandHandler::HandleReceiveFailed(void)
82{
83 if (m_bAwaitingReceiveFailed)
84 {
85 m_bAwaitingReceiveFailed = false;
86 return false;
87 }
88
89 return true;
90}
91
92bool CSLCommandHandler::InitHandler(void)
93{
94 if (m_bHandlerInited)
95 return true;
96 m_bHandlerInited = true;
97
98 /* reply with LGs vendor id */
99 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
100 if (m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
101 primary->TransmitVendorID(CECDEVICE_TV, false);
102
103 primary->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
104 return true;
105}
106
3e61b350
LOK
107bool CSLCommandHandler::ActivateSource(void)
108{
109 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
110 primary->SetActiveSource();
111 primary->TransmitActiveSource();
112 return true;
113}
114
468a1414
LOK
115bool CSLCommandHandler::HandleActiveSource(const cec_command &command)
116{
117 if (command.parameters.size == 2)
118 {
119 uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
120 if (iAddress != m_busDevice->GetPhysicalAddress(false))
121 m_bSLEnabled = false;
122 return m_processor->SetActiveSource(iAddress);
123 }
124
125 return true;
126}
127
128bool CSLCommandHandler::HandleFeatureAbort(const cec_command &command)
129{
130 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
131 if (primary->GetPowerStatus(false) == CEC_POWER_STATUS_ON && !m_bPowerStateReset && !m_bSLEnabled)
132 {
133 m_bPowerStateReset = true;
134 primary->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
135 }
136
137 return CCECCommandHandler::HandleFeatureAbort(command);
138}
139
140bool CSLCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
141{
142 if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
143 {
144 CCECBusDevice *device = GetDevice(command.destination);
145 if (device)
146 return device->TransmitPhysicalAddress();
147 }
148
149 return false;
e9de9629 150}
e54fd7d2
LOK
151
152bool CSLCommandHandler::HandleVendorCommand(const cec_command &command)
153{
154 if (command.parameters.size == 1 &&
11d13a02 155 command.parameters[0] == SL_COMMAND_UNKNOWN_01)
e54fd7d2 156 {
5f316715 157 HandleVendorCommand01(command);
8d915412 158 return true;
e54fd7d2 159 }
9a0d7b9f
LOK
160 else if (command.parameters.size == 2 &&
161 command.parameters[0] == SL_COMMAND_POWER_ON)
162 {
0ecbcd4d 163 HandleVendorCommandPowerOn(command);
9a0d7b9f
LOK
164 return true;
165 }
797dd7c4 166 else if (command.parameters.size == 2 &&
11d13a02 167 command.parameters[0] == SL_COMMAND_CONNECT_REQUEST)
9902f4e8 168 {
0ecbcd4d 169 HandleVendorCommandSLConnect(command);
8d915412 170 return true;
9902f4e8
LOK
171 }
172 else if (command.parameters.size == 1 &&
0d4c3a7b 173 command.parameters[0] == SL_COMMAND_REQUEST_POWER_STATUS)
9902f4e8 174 {
0ecbcd4d 175 HandleVendorCommandPowerOnStatus(command);
8d915412 176 return true;
9902f4e8 177 }
e54fd7d2
LOK
178
179 return false;
180}
181
5f316715 182void CSLCommandHandler::HandleVendorCommand01(const cec_command &command)
fe6f8e37
LOK
183{
184 TransmitVendorCommand0205(command.destination, command.initiator);
185}
186
187void CSLCommandHandler::TransmitVendorCommand0205(const cec_logical_address iSource, const cec_logical_address iDestination)
5f316715
LOK
188{
189 cec_command response;
fe6f8e37 190 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
5f316715
LOK
191 response.PushBack(SL_COMMAND_UNKNOWN_02);
192 response.PushBack(SL_COMMAND_UNKNOWN_03);
193
19cbfa8f 194 Transmit(response, false);
fe6f8e37 195}
5f316715 196
0ecbcd4d 197void CSLCommandHandler::HandleVendorCommandPowerOn(const cec_command &command)
9a0d7b9f 198{
842262d8 199 CCECBusDevice *device = m_processor->GetPrimaryDevice();
9a0d7b9f
LOK
200 if (device)
201 {
fe6f8e37 202 m_bSLEnabled = true;
468a1414
LOK
203
204 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); //XXX
9a0d7b9f 205 device->TransmitPowerState(command.initiator);
468a1414 206 device->SetPowerStatus(CEC_POWER_STATUS_ON);
9a0d7b9f 207
468a1414
LOK
208 SetLGDeckStatus();
209 device->SetActiveSource();
210 TransmitImageViewOn(device->GetLogicalAddress(), command.initiator);
211 }
5f316715 212}
0ecbcd4d 213void CSLCommandHandler::HandleVendorCommandPowerOnStatus(const cec_command &command)
5f316715
LOK
214{
215 if (command.destination != CECDEVICE_BROADCAST)
216 {
217 CCECBusDevice *device = m_processor->m_busDevices[m_processor->GetLogicalAddresses().primary];
218 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
219 device->TransmitPowerState(command.initiator);
220 device->SetPowerStatus(CEC_POWER_STATUS_ON);
221 }
222}
223
468a1414 224void CSLCommandHandler::HandleVendorCommandSLConnect(const cec_command &command)
8d915412 225{
468a1414
LOK
226 m_bSLEnabled = true;
227 SetLGDeckStatus();
89a726fa 228
79f01d26 229 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
37acf382 230
468a1414
LOK
231 primary->SetActiveSource();
232 TransmitImageViewOn(primary->GetLogicalAddress(), command.initiator);
233 TransmitVendorCommand05(primary->GetLogicalAddress(), command.initiator);
8d915412 234}
0d4c3a7b 235
468a1414 236void CSLCommandHandler::TransmitVendorCommand05(const cec_logical_address iSource, const cec_logical_address iDestination)
0d4c3a7b 237{
468a1414
LOK
238 cec_command response;
239 cec_command::Format(response, iSource, iDestination, CEC_OPCODE_VENDOR_COMMAND);
240 response.PushBack(SL_COMMAND_CONNECT_ACCEPT);
241 response.PushBack((uint8_t)iSource);
242 Transmit(response, false);
0d4c3a7b 243}
fe6f8e37 244
468a1414 245void CSLCommandHandler::SetLGDeckStatus(void)
9f65e017 246{
468a1414
LOK
247 /* LG TVs only route keypresses when the deck status is set to 0x20 */
248 CCECBusDevice *device = m_processor->GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
249 if (device)
250 ((CCECPlaybackDevice *)device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
9f65e017 251
468a1414
LOK
252 device = m_processor->GetDeviceByType(CEC_DEVICE_TYPE_RECORDING_DEVICE);
253 if (device)
254 ((CCECPlaybackDevice *)device)->SetDeckStatus(CEC_DECK_INFO_OTHER_STATUS_LG);
9f65e017 255}