2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
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.
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.
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.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
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/
33 #include "CECCommandHandler.h"
34 #include "../devices/CECBusDevice.h"
35 #include "../devices/CECAudioSystem.h"
36 #include "../CECProcessor.h"
40 CCECCommandHandler::CCECCommandHandler(CCECBusDevice
*busDevice
)
42 m_busDevice
= busDevice
;
45 bool CCECCommandHandler::HandleCommand(const cec_command
&command
)
49 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
51 switch(command
.opcode
)
53 case CEC_OPCODE_REPORT_POWER_STATUS
:
54 HandleReportPowerStatus(command
);
56 case CEC_OPCODE_CEC_VERSION
:
57 HandleDeviceCecVersion(command
);
59 case CEC_OPCODE_SET_MENU_LANGUAGE
:
60 HandleSetMenuLanguage(command
);
61 m_busDevice
->GetProcessor()->AddCommand(command
);
63 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
64 HandleGivePhysicalAddress(command
);
66 case CEC_OPCODE_GIVE_OSD_NAME
:
67 HandleGiveOSDName(command
);
69 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
70 HandleGiveDeviceVendorId(command
);
72 case CEC_OPCODE_DEVICE_VENDOR_ID
:
73 HandleDeviceVendorId(command
);
75 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
76 HandleDeviceVendorCommandWithId(command
);
78 case CEC_OPCODE_GIVE_DECK_STATUS
:
79 HandleGiveDeckStatus(command
);
81 case CEC_OPCODE_MENU_REQUEST
:
82 HandleMenuRequest(command
);
84 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
85 HandleGiveDevicePowerStatus(command
);
87 case CEC_OPCODE_GET_CEC_VERSION
:
88 HandleGetCecVersion(command
);
90 case CEC_OPCODE_USER_CONTROL_PRESSED
:
91 HandleUserControlPressed(command
);
93 case CEC_OPCODE_USER_CONTROL_RELEASE
:
94 HandleUserControlRelease(command
);
96 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
97 HandleGiveAudioStatus(command
);
99 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
100 HandleGiveSystemAudioModeStatus(command
);
102 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
103 HandleSetSystemAudioModeRequest(command
);
106 UnhandledCommand(command
);
107 m_busDevice
->GetProcessor()->AddCommand(command
);
112 else if (command
.destination
== CECDEVICE_BROADCAST
)
115 switch (command
.opcode
)
117 case CEC_OPCODE_SET_MENU_LANGUAGE
:
118 HandleSetMenuLanguage(command
);
119 m_busDevice
->GetProcessor()->AddCommand(command
);
121 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
122 HandleRequestActiveSource(command
);
124 case CEC_OPCODE_SET_STREAM_PATH
:
125 HandleSetStreamPath(command
);
126 m_busDevice
->GetProcessor()->AddCommand(command
);
128 case CEC_OPCODE_ROUTING_CHANGE
:
129 HandleRoutingChange(command
);
130 m_busDevice
->GetProcessor()->AddCommand(command
);
132 case CEC_OPCODE_DEVICE_VENDOR_ID
:
133 HandleDeviceVendorId(command
);
135 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
136 HandleDeviceVendorCommandWithId(command
);
139 UnhandledCommand(command
);
140 m_busDevice
->GetProcessor()->AddCommand(command
);
148 strLog
.Format("ignoring frame: we're not at destination %x", command
.destination
);
149 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
156 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
158 if (command
.parameters
.size
== 1)
160 CCECBusDevice
*device
= GetDevice(command
.initiator
);
162 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
168 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
170 SetVendorId(command
);
174 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
176 SetVendorId(command
);
180 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
182 CCECBusDevice
*device
= GetDevice(command
.destination
);
184 return device
->TransmitCECVersion(command
.initiator
);
189 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command
&command
)
191 CCECBusDevice
*device
= GetDevice(command
.destination
);
192 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
193 return ((CCECAudioSystem
*) device
)->TransmitAudioStatus(command
.initiator
);
198 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
200 CCECBusDevice
*device
= GetDevice(command
.destination
);
202 return device
->TransmitDeckStatus(command
.initiator
);
207 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
209 CCECBusDevice
*device
= GetDevice(command
.destination
);
211 return device
->TransmitPowerState(command
.initiator
);
216 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
218 CCECBusDevice
*device
= GetDevice(command
.destination
);
220 return device
->TransmitVendorID(command
.initiator
);
225 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
227 CCECBusDevice
*device
= GetDevice(command
.destination
);
229 return device
->TransmitOSDName(command
.initiator
);
234 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
236 CCECBusDevice
*device
= GetDevice(command
.destination
);
238 return device
->TransmitPhysicalAddress();
243 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
245 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_QUERY
)
247 CCECBusDevice
*device
= GetDevice(command
.destination
);
249 return device
->TransmitMenuState(command
.initiator
);
254 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
256 if (command
.parameters
.size
== 1)
258 CCECBusDevice
*device
= GetDevice(command
.initiator
);
260 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
265 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
268 strLog
.Format(">> %i requests active source", (uint8_t) command
.initiator
);
269 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
270 CCECBusDevice
*device
= m_busDevice
->GetProcessor()->m_busDevices
[m_busDevice
->GetMyLogicalAddress()];
272 return device
->TransmitActiveSource();
276 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
278 if (command
.parameters
.size
== 4)
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]);
283 CCECBusDevice
*device
= GetDevice(command
.initiator
);
285 device
->SetStreamPath(iNewAddress
, iOldAddress
);
290 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
292 if (command
.parameters
.size
== 3)
294 CCECBusDevice
*device
= GetDevice(command
.initiator
);
297 cec_menu_language language
;
298 language
.device
= command
.initiator
;
299 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
300 language
.language
[iPtr
] = command
.parameters
[iPtr
];
301 language
.language
[3] = 0;
302 device
->SetMenuLanguage(language
);
308 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
310 if (command
.parameters
.size
>= 2)
312 int streamaddr
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
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 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(streamaddr
);
318 return device
->TransmitActiveSource();
323 bool CCECCommandHandler::HandleSetSystemAudioModeRequest(const cec_command
&command
)
325 if (command
.parameters
.size
>= 1)
327 CCECBusDevice
*device
= GetDevice(command
.destination
);
328 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
329 return ((CCECAudioSystem
*) device
)->SetSystemAudioMode(command
);
334 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command
&command
)
336 CCECBusDevice
*device
= GetDevice(command
.destination
);
337 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
338 return ((CCECAudioSystem
*) device
)->TransmitSystemAudioModeStatus(command
.initiator
);
343 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
345 if (command
.parameters
.size
> 0)
347 m_busDevice
->GetProcessor()->AddKey();
349 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
352 strLog
.Format("key pressed: %1x", command
.parameters
[0]);
353 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
355 m_busDevice
->GetProcessor()->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
361 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
363 m_busDevice
->GetProcessor()->AddKey();
367 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
370 strLog
.Format("unhandled command with opcode %02x from address %d", command
.opcode
, command
.initiator
);
371 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
);
374 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
376 CCECBusDevice
*device
= NULL
;
378 if (iLogicalAddress
>= CECDEVICE_TV
&& iLogicalAddress
<= CECDEVICE_BROADCAST
)
379 device
= m_busDevice
->GetProcessor()->m_busDevices
[iLogicalAddress
];
384 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
386 CCECBusDevice
*device
= NULL
;
388 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
390 if (m_busDevice
->GetProcessor()->m_busDevices
[iPtr
]->GetPhysicalAddress() == iPhysicalAddress
)
392 device
= m_busDevice
->GetProcessor()->m_busDevices
[iPtr
];
401 void CCECCommandHandler::SetVendorId(const cec_command
&command
)
403 if (command
.parameters
.size
< 3)
405 m_busDevice
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
409 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 3) +
410 ((uint64_t)command
.parameters
[1] << 2) +
411 (uint64_t)command
.parameters
[2];
413 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
415 device
->SetVendorId(iVendorId
, iVendorId
);