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