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_GIVE_PHYSICAL_ADDRESS
:
53 HandleGivePhysicalAddress(command
);
55 case CEC_OPCODE_GIVE_OSD_NAME
:
56 HandleGiveOSDName(command
);
58 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
59 HandleGiveDeviceVendorId(command
);
61 case CEC_OPCODE_DEVICE_VENDOR_ID
:
62 HandleDeviceVendorId(command
);
64 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
65 HandleDeviceVendorCommandWithId(command
);
67 case CEC_OPCODE_GIVE_DECK_STATUS
:
68 HandleGiveDeckStatus(command
);
70 case CEC_OPCODE_MENU_REQUEST
:
71 HandleMenuRequest(command
);
73 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
74 HandleGiveDevicePowerStatus(command
);
76 case CEC_OPCODE_GET_CEC_VERSION
:
77 HandleGetCecVersion(command
);
79 case CEC_OPCODE_USER_CONTROL_PRESSED
:
80 HandleUserControlPressed(command
);
82 case CEC_OPCODE_USER_CONTROL_RELEASE
:
83 HandleUserControlRelease(command
);
86 UnhandledCommand(command
);
91 else if (command
.destination
== CECDEVICE_BROADCAST
)
94 switch (command
.opcode
)
96 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
97 HandleRequestActiveSource(command
);
99 case CEC_OPCODE_SET_STREAM_PATH
:
100 HandleSetStreamPath(command
);
102 case CEC_OPCODE_ROUTING_CHANGE
:
103 HandleRoutingChange(command
);
105 case CEC_OPCODE_DEVICE_VENDOR_ID
:
106 HandleDeviceVendorId(command
);
108 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
109 HandleDeviceVendorCommandWithId(command
);
112 UnhandledCommand(command
);
120 strLog
.Format("ignoring frame: destination: %u != %u", command
.destination
, (uint8_t)m_busDevice
->GetMyLogicalAddress());
121 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
128 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
130 CCECBusDevice
*device
= GetDevice(command
.initiator
);
132 device
->SetVendorId(command
.parameters
);
137 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
139 CCECBusDevice
*device
= GetDevice(command
.initiator
);
141 device
->SetVendorId(command
.parameters
);
146 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
148 CCECBusDevice
*device
= GetDevice(command
.initiator
);
150 return device
->ReportCECVersion();
155 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
157 CCECBusDevice
*device
= GetDevice(command
.initiator
);
159 return device
->ReportDeckStatus();
164 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
166 CCECBusDevice
*device
= GetDevice(command
.initiator
);
168 return device
->ReportPowerState();
173 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
175 CCECBusDevice
*device
= GetDevice(command
.initiator
);
177 return device
->ReportVendorID();
182 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
184 CCECBusDevice
*device
= GetDevice(command
.initiator
);
186 return device
->ReportOSDName();
191 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
193 CCECBusDevice
*device
= GetThisDevice();
195 return device
->BroadcastPhysicalAddress();
200 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
202 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_QUERY
)
204 CCECBusDevice
*device
= GetDevice(command
.initiator
);
206 return device
->ReportMenuState();
211 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
214 strLog
.Format(">> %i requests active source", (uint8_t) command
.initiator
);
215 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
216 CCECBusDevice
*device
= GetThisDevice();
218 return device
->BroadcastActiveSource();
222 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
224 if (command
.parameters
.size
== 4)
226 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
227 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
229 CCECBusDevice
*device
= GetDevice(command
.initiator
);
231 device
->SetPhysicalAddress(iNewAddress
, iOldAddress
);
236 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
238 if (command
.parameters
.size
>= 2)
240 int streamaddr
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
242 strLog
.Format(">> %i requests stream path from physical address %04x", command
.initiator
, streamaddr
);
243 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
244 if (streamaddr
== m_busDevice
->GetMyPhysicalAddress())
246 CCECBusDevice
*device
= GetThisDevice();
248 return device
->BroadcastActiveSource();
255 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
257 if (command
.parameters
.size
> 0)
259 m_busDevice
->GetProcessor()->AddKey();
261 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
264 strLog
.Format("key pressed: %1x", command
.parameters
[0]);
265 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
267 m_busDevice
->GetProcessor()->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
273 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
275 m_busDevice
->GetProcessor()->AddKey();
279 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
281 m_busDevice
->GetProcessor()->AddCommand(command
);
284 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
286 CCECBusDevice
*device
= NULL
;
288 if (iLogicalAddress
>= CECDEVICE_TV
&& iLogicalAddress
<= CECDEVICE_BROADCAST
)
289 device
= m_busDevice
->GetProcessor()->m_busDevices
[iLogicalAddress
];
294 CCECBusDevice
*CCECCommandHandler::GetThisDevice(void) const
296 return m_busDevice
->GetProcessor()->m_busDevices
[m_busDevice
->GetMyLogicalAddress()];