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 (command
.destination
== m_busDevice
->GetMyLogicalAddress())
50 switch(command
.opcode
)
52 case CEC_OPCODE_CEC_VERSION
:
53 HandleDeviceCecVersion(command
);
55 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
56 HandleGivePhysicalAddress(command
);
58 case CEC_OPCODE_GIVE_OSD_NAME
:
59 HandleGiveOSDName(command
);
61 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
62 HandleGiveDeviceVendorId(command
);
64 case CEC_OPCODE_DEVICE_VENDOR_ID
:
65 HandleDeviceVendorId(command
);
67 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
68 HandleDeviceVendorCommandWithId(command
);
70 case CEC_OPCODE_GIVE_DECK_STATUS
:
71 HandleGiveDeckStatus(command
);
73 case CEC_OPCODE_MENU_REQUEST
:
74 HandleMenuRequest(command
);
76 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
77 HandleGiveDevicePowerStatus(command
);
79 case CEC_OPCODE_GET_CEC_VERSION
:
80 HandleGetCecVersion(command
);
82 case CEC_OPCODE_USER_CONTROL_PRESSED
:
83 HandleUserControlPressed(command
);
85 case CEC_OPCODE_USER_CONTROL_RELEASE
:
86 HandleUserControlRelease(command
);
89 UnhandledCommand(command
);
94 else if (command
.destination
== CECDEVICE_BROADCAST
)
97 switch (command
.opcode
)
99 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
100 HandleRequestActiveSource(command
);
102 case CEC_OPCODE_SET_STREAM_PATH
:
103 HandleSetStreamPath(command
);
105 case CEC_OPCODE_ROUTING_CHANGE
:
106 HandleRoutingChange(command
);
108 case CEC_OPCODE_DEVICE_VENDOR_ID
:
109 HandleDeviceVendorId(command
);
111 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
112 HandleDeviceVendorCommandWithId(command
);
115 UnhandledCommand(command
);
123 strLog
.Format("ignoring frame: destination: %u != %u", command
.destination
, (uint8_t)m_busDevice
->GetMyLogicalAddress());
124 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
131 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
133 if (command
.parameters
.size
== 1)
135 CCECBusDevice
*device
= GetDevice(command
.initiator
);
137 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
143 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
145 CCECBusDevice
*device
= GetDevice(command
.initiator
);
147 device
->SetVendorId(command
.parameters
);
152 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
154 CCECBusDevice
*device
= GetDevice(command
.initiator
);
156 device
->SetVendorId(command
.parameters
);
161 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
163 CCECBusDevice
*device
= GetDevice(command
.initiator
);
165 return device
->ReportCECVersion();
170 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
172 CCECBusDevice
*device
= GetDevice(command
.initiator
);
174 return device
->ReportDeckStatus();
179 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
181 CCECBusDevice
*device
= GetDevice(command
.initiator
);
183 return device
->ReportPowerState();
188 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
190 CCECBusDevice
*device
= GetDevice(command
.initiator
);
192 return device
->ReportVendorID();
197 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
199 CCECBusDevice
*device
= GetDevice(command
.initiator
);
201 return device
->ReportOSDName();
206 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
208 CCECBusDevice
*device
= GetThisDevice();
210 return device
->BroadcastPhysicalAddress();
215 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
217 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_QUERY
)
219 CCECBusDevice
*device
= GetDevice(command
.initiator
);
221 return device
->ReportMenuState();
226 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
229 strLog
.Format(">> %i requests active source", (uint8_t) command
.initiator
);
230 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
231 CCECBusDevice
*device
= GetThisDevice();
233 return device
->BroadcastActiveSource();
237 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
239 if (command
.parameters
.size
== 4)
241 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
242 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
244 CCECBusDevice
*device
= GetDevice(command
.initiator
);
246 device
->SetPhysicalAddress(iNewAddress
, iOldAddress
);
251 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
253 if (command
.parameters
.size
>= 2)
255 int streamaddr
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
257 strLog
.Format(">> %i requests stream path from physical address %04x", command
.initiator
, streamaddr
);
258 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
259 if (streamaddr
== m_busDevice
->GetMyPhysicalAddress())
261 CCECBusDevice
*device
= GetThisDevice();
263 return device
->BroadcastActiveSource();
270 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
272 if (command
.parameters
.size
> 0)
274 m_busDevice
->GetProcessor()->AddKey();
276 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
279 strLog
.Format("key pressed: %1x", command
.parameters
[0]);
280 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
282 m_busDevice
->GetProcessor()->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
288 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
290 m_busDevice
->GetProcessor()->AddKey();
294 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
296 m_busDevice
->GetProcessor()->AddCommand(command
);
299 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
301 CCECBusDevice
*device
= NULL
;
303 if (iLogicalAddress
>= CECDEVICE_TV
&& iLogicalAddress
<= CECDEVICE_BROADCAST
)
304 device
= m_busDevice
->GetProcessor()->m_busDevices
[iLogicalAddress
];
309 CCECBusDevice
*CCECCommandHandler::GetThisDevice(void) const
311 return m_busDevice
->GetProcessor()->m_busDevices
[m_busDevice
->GetMyLogicalAddress()];