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