cec: added GetDeviceMenuLanguage()/cec_get_device_menu_language()
[deb_libcec.git] / src / lib / implementations / CECCommandHandler.cpp
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"
34 #include "../devices/CECBusDevice.h"
35 #include "../CECProcessor.h"
36
37 using namespace CEC;
38
39 CCECCommandHandler::CCECCommandHandler(CCECBusDevice *busDevice)
40 {
41 m_busDevice = busDevice;
42 }
43
44 bool 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_CEC_VERSION:
53 HandleDeviceCecVersion(command);
54 break;
55 case CEC_OPCODE_SET_MENU_LANGUAGE:
56 HandleSetMenuLanguage(command);
57 break;
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 }
96 }
97 else if (command.destination == CECDEVICE_BROADCAST)
98 {
99 CStdString strLog;
100 switch (command.opcode)
101 {
102 case CEC_OPCODE_SET_MENU_LANGUAGE:
103 HandleSetMenuLanguage(command);
104 break;
105 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
106 HandleRequestActiveSource(command);
107 break;
108 case CEC_OPCODE_SET_STREAM_PATH:
109 HandleSetStreamPath(command);
110 break;
111 case CEC_OPCODE_ROUTING_CHANGE:
112 HandleRoutingChange(command);
113 break;
114 case CEC_OPCODE_DEVICE_VENDOR_ID:
115 HandleDeviceVendorId(command);
116 break;
117 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
118 HandleDeviceVendorCommandWithId(command);
119 break;
120 default:
121 UnhandledCommand(command);
122 bHandled = false;
123 break;
124 }
125 }
126 else
127 {
128 CStdString strLog;
129 strLog.Format("ignoring frame: destination: %u != %u", command.destination, (uint8_t)m_busDevice->GetMyLogicalAddress());
130 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
131 bHandled = false;
132 }
133
134 return bHandled;
135 }
136
137 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command &command)
138 {
139 if (command.parameters.size == 1)
140 {
141 CCECBusDevice *device = GetDevice(command.initiator);
142 if (device)
143 device->SetCecVersion((cec_version) command.parameters[0]);
144 }
145
146 return true;
147 }
148
149 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command)
150 {
151 CCECBusDevice *device = GetDevice(command.initiator);
152 if (device)
153 device->SetVendorId(command.parameters);
154
155 return true;
156 }
157
158 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command &command)
159 {
160 CCECBusDevice *device = GetDevice(command.initiator);
161 if (device)
162 device->SetVendorId(command.parameters);
163
164 return true;
165 }
166
167 bool CCECCommandHandler::HandleGetCecVersion(const cec_command &command)
168 {
169 CCECBusDevice *device = GetDevice(command.initiator);
170 if (device)
171 return device->ReportCECVersion();
172
173 return false;
174 }
175
176 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command &command)
177 {
178 CCECBusDevice *device = GetDevice(command.initiator);
179 if (device)
180 return device->ReportDeckStatus();
181
182 return false;
183 }
184
185 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
186 {
187 CCECBusDevice *device = GetDevice(command.initiator);
188 if (device)
189 return device->ReportPowerState();
190
191 return false;
192 }
193
194 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command &command)
195 {
196 CCECBusDevice *device = GetDevice(command.initiator);
197 if (device)
198 return device->ReportVendorID();
199
200 return false;
201 }
202
203 bool CCECCommandHandler::HandleGiveOSDName(const cec_command &command)
204 {
205 CCECBusDevice *device = GetDevice(command.initiator);
206 if (device)
207 return device->ReportOSDName();
208
209 return false;
210 }
211
212 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
213 {
214 CCECBusDevice *device = GetThisDevice();
215 if (device)
216 return device->BroadcastPhysicalAddress();
217
218 return false;
219 }
220
221 bool CCECCommandHandler::HandleMenuRequest(const cec_command &command)
222 {
223 if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_QUERY)
224 {
225 CCECBusDevice *device = GetDevice(command.initiator);
226 if (device)
227 return device->ReportMenuState();
228 }
229 return false;
230 }
231
232 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command &command)
233 {
234 CStdString strLog;
235 strLog.Format(">> %i requests active source", (uint8_t) command.initiator);
236 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
237 CCECBusDevice *device = GetThisDevice();
238 if (device)
239 return device->BroadcastActiveSource();
240 return false;
241 }
242
243 bool CCECCommandHandler::HandleRoutingChange(const cec_command &command)
244 {
245 if (command.parameters.size == 4)
246 {
247 uint16_t iOldAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
248 uint16_t iNewAddress = ((uint16_t)command.parameters[2] << 8) | ((uint16_t)command.parameters[3]);
249
250 CCECBusDevice *device = GetDevice(command.initiator);
251 if (device)
252 device->SetPhysicalAddress(iNewAddress, iOldAddress);
253 }
254 return true;
255 }
256
257 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command &command)
258 {
259 if (command.parameters.size == 3)
260 {
261 CCECBusDevice *device = GetDevice(command.initiator);
262 if (device)
263 {
264 cec_menu_language language;
265 language.device = command.initiator;
266 for (unsigned int iPtr = 0; iPtr < 4; iPtr++)
267 language.language[iPtr] = command.parameters[iPtr];
268 language.language[3] = 0;
269 device->SetMenuLanguage(language);
270 }
271 }
272 return true;
273 }
274
275
276 bool CCECCommandHandler::HandleSetStreamPath(const cec_command &command)
277 {
278 if (command.parameters.size >= 2)
279 {
280 int streamaddr = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
281 CStdString strLog;
282 strLog.Format(">> %i requests stream path from physical address %04x", command.initiator, streamaddr);
283 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
284 if (streamaddr == m_busDevice->GetMyPhysicalAddress())
285 {
286 CCECBusDevice *device = GetThisDevice();
287 if (device)
288 return device->BroadcastActiveSource();
289 return false;
290 }
291 }
292 return true;
293 }
294
295 bool CCECCommandHandler::HandleUserControlPressed(const cec_command &command)
296 {
297 if (command.parameters.size > 0)
298 {
299 m_busDevice->GetProcessor()->AddKey();
300
301 if (command.parameters[0] <= CEC_USER_CONTROL_CODE_MAX)
302 {
303 CStdString strLog;
304 strLog.Format("key pressed: %1x", command.parameters[0]);
305 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
306
307 m_busDevice->GetProcessor()->SetCurrentButton((cec_user_control_code) command.parameters[0]);
308 }
309 }
310 return true;
311 }
312
313 bool CCECCommandHandler::HandleUserControlRelease(const cec_command &command)
314 {
315 m_busDevice->GetProcessor()->AddKey();
316 return true;
317 }
318
319 void CCECCommandHandler::UnhandledCommand(const cec_command &command)
320 {
321 m_busDevice->GetProcessor()->AddCommand(command);
322 }
323
324 CCECBusDevice *CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress) const
325 {
326 CCECBusDevice *device = NULL;
327
328 if (iLogicalAddress >= CECDEVICE_TV && iLogicalAddress <= CECDEVICE_BROADCAST)
329 device = m_busDevice->GetProcessor()->m_busDevices[iLogicalAddress];
330
331 return device;
332 }
333
334 CCECBusDevice *CCECCommandHandler::GetThisDevice(void) const
335 {
336 return m_busDevice->GetProcessor()->m_busDevices[m_busDevice->GetMyLogicalAddress()];
337 }