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