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