3 * This file is part of the libCEC(R) library.
5 * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
6 * libCEC(R) is an original work, containing original code.
8 * libCEC(R) is a trademark of Pulse-Eight Limited.
10 * This program is dual-licensed; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * Alternatively, you can license this library under a commercial license,
26 * please contact Pulse-Eight Licensing for more information.
28 * For more information contact:
29 * Pulse-Eight Licensing <license@pulse-eight.com>
30 * http://www.pulse-eight.com/
31 * http://www.pulse-eight.net/
34 #include "CECCommandHandler.h"
35 #include "../CECBusDevice.h"
36 #include "../CECProcessor.h"
40 CCECCommandHandler::CCECCommandHandler(CCECBusDevice
*busDevice
)
42 m_busDevice
= busDevice
;
45 bool CCECCommandHandler::HandleCommand(const cec_command
&command
)
49 if (command
.destination
== m_busDevice
->GetMyLogicalAddress())
51 switch(command
.opcode
)
53 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
54 HandleGivePhysicalAddress(command
);
56 case CEC_OPCODE_GIVE_OSD_NAME
:
57 HandleGiveOSDName(command
);
59 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
60 HandleGiveDeviceVendorId(command
);
62 case CEC_OPCODE_DEVICE_VENDOR_ID
:
63 HandleDeviceVendorId(command
);
65 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
66 HandleDeviceVendorCommandWithId(command
);
68 case CEC_OPCODE_GIVE_DECK_STATUS
:
69 HandleGiveDeckStatus(command
);
71 case CEC_OPCODE_MENU_REQUEST
:
72 HandleMenuRequest(command
);
74 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
75 HandleGiveDevicePowerStatus(command
);
77 case CEC_OPCODE_GET_CEC_VERSION
:
78 HandleGetCecVersion(command
);
80 case CEC_OPCODE_USER_CONTROL_PRESSED
:
81 HandleUserControlPressed(command
);
83 case CEC_OPCODE_USER_CONTROL_RELEASE
:
84 HandleUserControlRelease(command
);
87 UnhandledCommand(command
);
92 else if (command
.destination
== CECDEVICE_BROADCAST
)
95 switch (command
.opcode
)
97 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
98 HandleRequestActiveSource(command
);
100 case CEC_OPCODE_SET_STREAM_PATH
:
101 HandleSetStreamPath(command
);
103 case CEC_OPCODE_ROUTING_CHANGE
:
104 HandleRoutingChange(command
);
106 case CEC_OPCODE_DEVICE_VENDOR_ID
:
107 HandleDeviceVendorId(command
);
109 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
110 HandleDeviceVendorCommandWithId(command
);
113 UnhandledCommand(command
);
121 strLog
.Format("ignoring frame: destination: %u != %u", command
.destination
, (uint8_t)m_busDevice
->GetMyLogicalAddress());
122 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
129 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
131 m_busDevice
->GetProcessor()->ParseVendorId(command
.initiator
, command
.parameters
);
135 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
137 m_busDevice
->GetProcessor()->ParseVendorId(command
.initiator
, command
.parameters
);
141 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
143 m_busDevice
->GetProcessor()->ReportCECVersion(command
.initiator
);
147 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
149 // need to support opcodes play and deck control before doing anything with this
150 m_busDevice
->GetProcessor()->TransmitAbort(command
.initiator
, CEC_OPCODE_GIVE_DECK_STATUS
);
154 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
156 m_busDevice
->GetProcessor()->ReportPowerState(command
.initiator
);
160 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
162 m_busDevice
->GetProcessor()->ReportVendorID(command
.initiator
);
166 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
168 m_busDevice
->GetProcessor()->ReportOSDName(command
.initiator
);
172 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
174 m_busDevice
->GetProcessor()->ReportPhysicalAddress();
178 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
180 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_QUERY
)
181 m_busDevice
->GetProcessor()->ReportMenuState(command
.initiator
);
185 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
188 strLog
.Format(">> %i requests active source", (uint8_t) command
.initiator
);
189 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
190 m_busDevice
->GetProcessor()->BroadcastActiveSource();
194 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
196 if (command
.parameters
.size
== 4)
198 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
199 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
201 strLog
.Format(">> %i changed physical address from %04x to %04x", command
.initiator
, iOldAddress
, iNewAddress
);
202 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
204 m_busDevice
->GetProcessor()->AddCommand(command
);
209 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
211 if (command
.parameters
.size
>= 2)
213 int streamaddr
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
215 strLog
.Format(">> %i requests stream path from physical address %04x", command
.initiator
, streamaddr
);
216 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
217 if (streamaddr
== m_busDevice
->GetMyPhysicalAddress())
218 m_busDevice
->GetProcessor()->BroadcastActiveSource();
223 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
225 if (command
.parameters
.size
> 0)
227 m_busDevice
->GetProcessor()->AddKey();
229 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
232 strLog
.Format("key pressed: %1x", command
.parameters
[0]);
233 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
235 m_busDevice
->GetProcessor()->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
241 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
243 m_busDevice
->GetProcessor()->AddKey();
247 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
249 m_busDevice
->GetProcessor()->AddCommand(command
);;