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 "../CECProcessor.h"
39 CCECCommandHandler::CCECCommandHandler(CCECBusDevice
*busDevice
)
41 m_busDevice
= busDevice
;
44 bool CCECCommandHandler::HandleCommand(const cec_command
&command
)
48 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
50 switch(command
.opcode
)
52 case CEC_OPCODE_REPORT_POWER_STATUS
:
53 HandleReportPowerStatus(command
);
55 case CEC_OPCODE_CEC_VERSION
:
56 HandleDeviceCecVersion(command
);
58 case CEC_OPCODE_SET_MENU_LANGUAGE
:
59 HandleSetMenuLanguage(command
);
60 m_busDevice
->GetProcessor()->AddCommand(command
);
62 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
63 HandleGivePhysicalAddress(command
);
65 case CEC_OPCODE_GIVE_OSD_NAME
:
66 HandleGiveOSDName(command
);
68 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
69 HandleGiveDeviceVendorId(command
);
71 case CEC_OPCODE_DEVICE_VENDOR_ID
:
72 HandleDeviceVendorId(command
);
74 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
75 HandleDeviceVendorCommandWithId(command
);
77 case CEC_OPCODE_GIVE_DECK_STATUS
:
78 HandleGiveDeckStatus(command
);
80 case CEC_OPCODE_MENU_REQUEST
:
81 HandleMenuRequest(command
);
83 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
84 HandleGiveDevicePowerStatus(command
);
86 case CEC_OPCODE_GET_CEC_VERSION
:
87 HandleGetCecVersion(command
);
89 case CEC_OPCODE_USER_CONTROL_PRESSED
:
90 HandleUserControlPressed(command
);
92 case CEC_OPCODE_USER_CONTROL_RELEASE
:
93 HandleUserControlRelease(command
);
96 UnhandledCommand(command
);
97 m_busDevice
->GetProcessor()->AddCommand(command
);
102 else if (command
.destination
== CECDEVICE_BROADCAST
)
105 switch (command
.opcode
)
107 case CEC_OPCODE_SET_MENU_LANGUAGE
:
108 HandleSetMenuLanguage(command
);
109 m_busDevice
->GetProcessor()->AddCommand(command
);
111 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
112 HandleRequestActiveSource(command
);
114 case CEC_OPCODE_SET_STREAM_PATH
:
115 HandleSetStreamPath(command
);
116 m_busDevice
->GetProcessor()->AddCommand(command
);
118 case CEC_OPCODE_ROUTING_CHANGE
:
119 HandleRoutingChange(command
);
120 m_busDevice
->GetProcessor()->AddCommand(command
);
122 case CEC_OPCODE_DEVICE_VENDOR_ID
:
123 HandleDeviceVendorId(command
);
125 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
126 HandleDeviceVendorCommandWithId(command
);
129 UnhandledCommand(command
);
130 m_busDevice
->GetProcessor()->AddCommand(command
);
138 strLog
.Format("ignoring frame: we're not at destination %x", command
.destination
);
139 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
146 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
148 if (command
.parameters
.size
== 1)
150 CCECBusDevice
*device
= GetDevice(command
.initiator
);
152 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
158 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
160 CCECBusDevice
*device
= GetDevice(command
.initiator
);
162 device
->SetVendorId(command
.parameters
);
167 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
169 CCECBusDevice
*device
= GetDevice(command
.initiator
);
171 device
->SetVendorId(command
.parameters
);
176 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
178 CCECBusDevice
*device
= GetDevice(command
.destination
);
180 return device
->TransmitCECVersion(command
.initiator
);
185 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
187 CCECBusDevice
*device
= GetDevice(command
.destination
);
189 return device
->TransmitDeckStatus(command
.initiator
);
194 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
196 CCECBusDevice
*device
= GetDevice(command
.destination
);
198 return device
->TransmitPowerState(command
.initiator
);
203 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
205 CCECBusDevice
*device
= GetDevice(command
.destination
);
207 return device
->TransmitVendorID(command
.initiator
);
212 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
214 CCECBusDevice
*device
= GetDevice(command
.destination
);
216 return device
->TransmitOSDName(command
.initiator
);
221 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
223 CCECBusDevice
*device
= GetDevice(command
.destination
);
225 return device
->TransmitPhysicalAddress();
230 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
232 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_QUERY
)
234 CCECBusDevice
*device
= GetDevice(command
.destination
);
236 return device
->TransmitMenuState(command
.initiator
);
241 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
243 if (command
.parameters
.size
== 1)
245 CCECBusDevice
*device
= GetDevice(command
.initiator
);
247 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
252 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
255 strLog
.Format(">> %i requests active source", (uint8_t) command
.initiator
);
256 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
257 CCECBusDevice
*device
= m_busDevice
->GetProcessor()->m_busDevices
[m_busDevice
->GetMyLogicalAddress()];
259 return device
->TransmitActiveSource();
263 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
265 if (command
.parameters
.size
== 4)
267 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
268 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
270 CCECBusDevice
*device
= GetDevice(command
.initiator
);
272 device
->SetPhysicalAddress(iNewAddress
, iOldAddress
);
277 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
279 if (command
.parameters
.size
== 3)
281 CCECBusDevice
*device
= GetDevice(command
.initiator
);
284 cec_menu_language language
;
285 language
.device
= command
.initiator
;
286 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
287 language
.language
[iPtr
] = command
.parameters
[iPtr
];
288 language
.language
[3] = 0;
289 device
->SetMenuLanguage(language
);
295 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
297 if (command
.parameters
.size
>= 2)
299 int streamaddr
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
301 strLog
.Format(">> %i requests stream path from physical address %04x", command
.initiator
, streamaddr
);
302 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
303 if (streamaddr
== m_busDevice
->GetMyPhysicalAddress())
305 CCECBusDevice
*device
= GetDevice(command
.destination
);
307 return device
->TransmitActiveSource();
314 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
316 if (command
.parameters
.size
> 0)
318 m_busDevice
->GetProcessor()->AddKey();
320 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
323 strLog
.Format("key pressed: %1x", command
.parameters
[0]);
324 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
326 m_busDevice
->GetProcessor()->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
332 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
334 m_busDevice
->GetProcessor()->AddKey();
338 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
341 strLog
.Format("unhandled command with opcode %02x from address %d", command
.opcode
, command
.initiator
);
342 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
);
345 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
347 CCECBusDevice
*device
= NULL
;
349 if (iLogicalAddress
>= CECDEVICE_TV
&& iLogicalAddress
<= CECDEVICE_BROADCAST
)
350 device
= m_busDevice
->GetProcessor()->m_busDevices
[iLogicalAddress
];