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