cec: fixed - wait for ack while keeping a lock
[deb_libcec.git] / src / lib / implementations / CECCommandHandler.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 "CECCommandHandler.h"
eafa9d46 34#include "../devices/CECBusDevice.h"
387b6f6f 35#include "../CECProcessor.h"
e9de9629
LOK
36
37using namespace CEC;
38
39CCECCommandHandler::CCECCommandHandler(CCECBusDevice *busDevice)
40{
41 m_busDevice = busDevice;
42}
43
44bool CCECCommandHandler::HandleCommand(const cec_command &command)
45{
46 bool bHandled(true);
47
48 if (command.destination == m_busDevice->GetMyLogicalAddress())
49 {
50 switch(command.opcode)
51 {
52 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
53 HandleGivePhysicalAddress(command);
54 break;
55 case CEC_OPCODE_GIVE_OSD_NAME:
56 HandleGiveOSDName(command);
57 break;
58 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
59 HandleGiveDeviceVendorId(command);
60 break;
61 case CEC_OPCODE_DEVICE_VENDOR_ID:
62 HandleDeviceVendorId(command);
63 break;
64 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
65 HandleDeviceVendorCommandWithId(command);
66 break;
67 case CEC_OPCODE_GIVE_DECK_STATUS:
68 HandleGiveDeckStatus(command);
69 break;
70 case CEC_OPCODE_MENU_REQUEST:
71 HandleMenuRequest(command);
72 break;
73 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
74 HandleGiveDevicePowerStatus(command);
75 break;
76 case CEC_OPCODE_GET_CEC_VERSION:
77 HandleGetCecVersion(command);
78 break;
79 case CEC_OPCODE_USER_CONTROL_PRESSED:
80 HandleUserControlPressed(command);
81 break;
82 case CEC_OPCODE_USER_CONTROL_RELEASE:
83 HandleUserControlRelease(command);
84 break;
85 default:
86 UnhandledCommand(command);
87 bHandled = false;
88 break;
89 }
90 }
91 else if (command.destination == CECDEVICE_BROADCAST)
92 {
93 CStdString strLog;
94 switch (command.opcode)
95 {
96 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
97 HandleRequestActiveSource(command);
98 break;
99 case CEC_OPCODE_SET_STREAM_PATH:
100 HandleSetStreamPath(command);
101 break;
102 case CEC_OPCODE_ROUTING_CHANGE:
103 HandleRoutingChange(command);
104 break;
105 case CEC_OPCODE_DEVICE_VENDOR_ID:
106 HandleDeviceVendorId(command);
107 break;
108 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
109 HandleDeviceVendorCommandWithId(command);
110 break;
111 default:
112 UnhandledCommand(command);
113 bHandled = false;
114 break;
115 }
116 }
117 else
118 {
119 CStdString strLog;
120 strLog.Format("ignoring frame: destination: %u != %u", command.destination, (uint8_t)m_busDevice->GetMyLogicalAddress());
121 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
122 bHandled = false;
123 }
124
125 return bHandled;
126}
127
128bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command)
129{
0f23c85c
LOK
130 CCECBusDevice *device = GetDevice(command.initiator);
131 if (device)
132 device->SetVendorId(command.parameters);
133
134 return false;
e9de9629
LOK
135}
136
137bool CCECCommandHandler::HandleDeviceVendorId(const cec_command &command)
138{
0f23c85c
LOK
139 CCECBusDevice *device = GetDevice(command.initiator);
140 if (device)
141 device->SetVendorId(command.parameters);
142
143 return false;
e9de9629
LOK
144}
145
146bool CCECCommandHandler::HandleGetCecVersion(const cec_command &command)
147{
0f23c85c
LOK
148 CCECBusDevice *device = GetDevice(command.initiator);
149 if (device)
150 return device->ReportCECVersion();
151
152 return false;
e9de9629
LOK
153}
154
155bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command &command)
156{
0f23c85c
LOK
157 CCECBusDevice *device = GetDevice(command.initiator);
158 if (device)
159 return device->ReportDeckStatus();
160
161 return false;
e9de9629
LOK
162}
163
164bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
165{
0f23c85c
LOK
166 CCECBusDevice *device = GetDevice(command.initiator);
167 if (device)
168 return device->ReportPowerState();
169
170 return false;
e9de9629
LOK
171}
172
173bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command &command)
174{
0f23c85c
LOK
175 CCECBusDevice *device = GetDevice(command.initiator);
176 if (device)
177 return device->ReportVendorID();
178
179 return false;
e9de9629
LOK
180}
181
182bool CCECCommandHandler::HandleGiveOSDName(const cec_command &command)
183{
0f23c85c
LOK
184 CCECBusDevice *device = GetDevice(command.initiator);
185 if (device)
186 return device->ReportOSDName();
187
188 return false;
e9de9629
LOK
189}
190
191bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
192{
09c10b66
LOK
193 CCECBusDevice *device = GetThisDevice();
194 if (device)
195 return device->BroadcastPhysicalAddress();
196
197 return false;
e9de9629
LOK
198}
199
200bool CCECCommandHandler::HandleMenuRequest(const cec_command &command)
201{
202 if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_QUERY)
0f23c85c
LOK
203 {
204 CCECBusDevice *device = GetDevice(command.initiator);
205 if (device)
206 return device->ReportMenuState();
207 }
208 return false;
e9de9629
LOK
209}
210
211bool CCECCommandHandler::HandleRequestActiveSource(const cec_command &command)
212{
213 CStdString strLog;
214 strLog.Format(">> %i requests active source", (uint8_t) command.initiator);
215 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
09c10b66
LOK
216 CCECBusDevice *device = GetThisDevice();
217 if (device)
218 return device->BroadcastActiveSource();
219 return false;
e9de9629
LOK
220}
221
222bool CCECCommandHandler::HandleRoutingChange(const cec_command &command)
223{
224 if (command.parameters.size == 4)
225 {
226 uint16_t iOldAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
227 uint16_t iNewAddress = ((uint16_t)command.parameters[2] << 8) | ((uint16_t)command.parameters[3]);
e9de9629 228
0f23c85c
LOK
229 CCECBusDevice *device = GetDevice(command.initiator);
230 if (device)
231 device->SetPhysicalAddress(iNewAddress, iOldAddress);
e9de9629
LOK
232 }
233 return true;
234}
235
236bool CCECCommandHandler::HandleSetStreamPath(const cec_command &command)
237{
238 if (command.parameters.size >= 2)
239 {
240 int streamaddr = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
241 CStdString strLog;
242 strLog.Format(">> %i requests stream path from physical address %04x", command.initiator, streamaddr);
243 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
244 if (streamaddr == m_busDevice->GetMyPhysicalAddress())
09c10b66
LOK
245 {
246 CCECBusDevice *device = GetThisDevice();
247 if (device)
248 return device->BroadcastActiveSource();
249 return false;
250 }
e9de9629
LOK
251 }
252 return true;
253}
254
255bool CCECCommandHandler::HandleUserControlPressed(const cec_command &command)
256{
257 if (command.parameters.size > 0)
258 {
259 m_busDevice->GetProcessor()->AddKey();
260
261 if (command.parameters[0] <= CEC_USER_CONTROL_CODE_MAX)
262 {
263 CStdString strLog;
264 strLog.Format("key pressed: %1x", command.parameters[0]);
265 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
266
267 m_busDevice->GetProcessor()->SetCurrentButton((cec_user_control_code) command.parameters[0]);
268 }
269 }
270 return true;
271}
272
273bool CCECCommandHandler::HandleUserControlRelease(const cec_command &command)
274{
275 m_busDevice->GetProcessor()->AddKey();
276 return true;
277}
278
279void CCECCommandHandler::UnhandledCommand(const cec_command &command)
280{
0f23c85c
LOK
281 m_busDevice->GetProcessor()->AddCommand(command);
282}
283
284CCECBusDevice *CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress) const
285{
286 CCECBusDevice *device = NULL;
287
288 if (iLogicalAddress >= CECDEVICE_TV && iLogicalAddress <= CECDEVICE_BROADCAST)
289 device = m_busDevice->GetProcessor()->m_busDevices[iLogicalAddress];
290
291 return device;
e9de9629 292}
09c10b66
LOK
293
294CCECBusDevice *CCECCommandHandler::GetThisDevice(void) const
295{
296 return m_busDevice->GetProcessor()->m_busDevices[m_busDevice->GetMyLogicalAddress()];
297}