cec: wait a bit longer to clear previous input
[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{
0f23c85c 193 return m_busDevice->BroadcastPhysicalAddress();
e9de9629
LOK
194}
195
196bool CCECCommandHandler::HandleMenuRequest(const cec_command &command)
197{
198 if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_QUERY)
0f23c85c
LOK
199 {
200 CCECBusDevice *device = GetDevice(command.initiator);
201 if (device)
202 return device->ReportMenuState();
203 }
204 return false;
e9de9629
LOK
205}
206
207bool CCECCommandHandler::HandleRequestActiveSource(const cec_command &command)
208{
209 CStdString strLog;
210 strLog.Format(">> %i requests active source", (uint8_t) command.initiator);
211 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
0f23c85c 212 m_busDevice->BroadcastActiveSource();
e9de9629
LOK
213 return true;
214}
215
216bool CCECCommandHandler::HandleRoutingChange(const cec_command &command)
217{
218 if (command.parameters.size == 4)
219 {
220 uint16_t iOldAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
221 uint16_t iNewAddress = ((uint16_t)command.parameters[2] << 8) | ((uint16_t)command.parameters[3]);
e9de9629 222
0f23c85c
LOK
223 CCECBusDevice *device = GetDevice(command.initiator);
224 if (device)
225 device->SetPhysicalAddress(iNewAddress, iOldAddress);
e9de9629
LOK
226 }
227 return true;
228}
229
230bool CCECCommandHandler::HandleSetStreamPath(const cec_command &command)
231{
232 if (command.parameters.size >= 2)
233 {
234 int streamaddr = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
235 CStdString strLog;
236 strLog.Format(">> %i requests stream path from physical address %04x", command.initiator, streamaddr);
237 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
238 if (streamaddr == m_busDevice->GetMyPhysicalAddress())
0f23c85c 239 m_busDevice->BroadcastActiveSource();
e9de9629
LOK
240 }
241 return true;
242}
243
244bool CCECCommandHandler::HandleUserControlPressed(const cec_command &command)
245{
246 if (command.parameters.size > 0)
247 {
248 m_busDevice->GetProcessor()->AddKey();
249
250 if (command.parameters[0] <= CEC_USER_CONTROL_CODE_MAX)
251 {
252 CStdString strLog;
253 strLog.Format("key pressed: %1x", command.parameters[0]);
254 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
255
256 m_busDevice->GetProcessor()->SetCurrentButton((cec_user_control_code) command.parameters[0]);
257 }
258 }
259 return true;
260}
261
262bool CCECCommandHandler::HandleUserControlRelease(const cec_command &command)
263{
264 m_busDevice->GetProcessor()->AddKey();
265 return true;
266}
267
268void CCECCommandHandler::UnhandledCommand(const cec_command &command)
269{
0f23c85c
LOK
270 m_busDevice->GetProcessor()->AddCommand(command);
271}
272
273CCECBusDevice *CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress) const
274{
275 CCECBusDevice *device = NULL;
276
277 if (iLogicalAddress >= CECDEVICE_TV && iLogicalAddress <= CECDEVICE_BROADCAST)
278 device = m_busDevice->GetProcessor()->m_busDevices[iLogicalAddress];
279
280 return device;
e9de9629 281}