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 return m_busDevice
->BroadcastPhysicalAddress();
196 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
198 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_QUERY
)
200 CCECBusDevice
*device
= GetDevice(command
.initiator
);
202 return device
->ReportMenuState();
207 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
210 strLog
.Format(">> %i requests active source", (uint8_t) command
.initiator
);
211 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
212 m_busDevice
->BroadcastActiveSource();
216 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
218 if (command
.parameters
.size
== 4)
220 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
221 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
223 CCECBusDevice
*device
= GetDevice(command
.initiator
);
225 device
->SetPhysicalAddress(iNewAddress
, iOldAddress
);
230 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
232 if (command
.parameters
.size
>= 2)
234 int streamaddr
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
236 strLog
.Format(">> %i requests stream path from physical address %04x", command
.initiator
, streamaddr
);
237 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
238 if (streamaddr
== m_busDevice
->GetMyPhysicalAddress())
239 m_busDevice
->BroadcastActiveSource();
244 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
246 if (command
.parameters
.size
> 0)
248 m_busDevice
->GetProcessor()->AddKey();
250 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
253 strLog
.Format("key pressed: %1x", command
.parameters
[0]);
254 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
256 m_busDevice
->GetProcessor()->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
262 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
264 m_busDevice
->GetProcessor()->AddKey();
268 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
270 m_busDevice
->GetProcessor()->AddCommand(command
);
273 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
275 CCECBusDevice
*device
= NULL
;
277 if (iLogicalAddress
>= CECDEVICE_TV
&& iLogicalAddress
<= CECDEVICE_BROADCAST
)
278 device
= m_busDevice
->GetProcessor()->m_busDevices
[iLogicalAddress
];