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