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_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
);
61 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
62 HandleGivePhysicalAddress(command
);
64 case CEC_OPCODE_GIVE_OSD_NAME
:
65 HandleGiveOSDName(command
);
67 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
68 HandleGiveDeviceVendorId(command
);
70 case CEC_OPCODE_DEVICE_VENDOR_ID
:
71 HandleDeviceVendorId(command
);
73 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
74 HandleDeviceVendorCommandWithId(command
);
76 case CEC_OPCODE_GIVE_DECK_STATUS
:
77 HandleGiveDeckStatus(command
);
79 case CEC_OPCODE_MENU_REQUEST
:
80 HandleMenuRequest(command
);
82 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
83 HandleGiveDevicePowerStatus(command
);
85 case CEC_OPCODE_GET_CEC_VERSION
:
86 HandleGetCecVersion(command
);
88 case CEC_OPCODE_USER_CONTROL_PRESSED
:
89 HandleUserControlPressed(command
);
91 case CEC_OPCODE_USER_CONTROL_RELEASE
:
92 HandleUserControlRelease(command
);
95 UnhandledCommand(command
);
100 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
);
110 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
111 HandleRequestActiveSource(command
);
113 case CEC_OPCODE_SET_STREAM_PATH
:
114 HandleSetStreamPath(command
);
116 case CEC_OPCODE_ROUTING_CHANGE
:
117 HandleRoutingChange(command
);
119 case CEC_OPCODE_DEVICE_VENDOR_ID
:
120 HandleDeviceVendorId(command
);
122 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
123 HandleDeviceVendorCommandWithId(command
);
126 UnhandledCommand(command
);
131 m_busDevice
->GetProcessor()->AddCommand(command
);
136 strLog
.Format("ignoring frame: destination: %u != %u", command
.destination
, (uint8_t)m_busDevice
->GetMyLogicalAddress());
137 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
144 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
146 if (command
.parameters
.size
== 1)
148 CCECBusDevice
*device
= GetDevice(command
.initiator
);
150 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
156 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
158 CCECBusDevice
*device
= GetDevice(command
.initiator
);
160 device
->SetVendorId(command
.parameters
);
165 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
167 CCECBusDevice
*device
= GetDevice(command
.initiator
);
169 device
->SetVendorId(command
.parameters
);
174 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
176 CCECBusDevice
*device
= GetDevice(command
.initiator
);
178 return device
->ReportCECVersion();
183 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
185 CCECBusDevice
*device
= GetDevice(command
.initiator
);
187 return device
->ReportDeckStatus();
192 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
194 CCECBusDevice
*device
= GetDevice(command
.initiator
);
196 return device
->ReportPowerState();
201 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
203 CCECBusDevice
*device
= GetDevice(command
.initiator
);
205 return device
->ReportVendorID();
210 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
212 CCECBusDevice
*device
= GetDevice(command
.initiator
);
214 return device
->ReportOSDName();
219 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
221 CCECBusDevice
*device
= GetThisDevice();
223 return device
->BroadcastPhysicalAddress();
228 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
230 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_QUERY
)
232 CCECBusDevice
*device
= GetDevice(command
.initiator
);
234 return device
->ReportMenuState();
239 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
241 if (command
.parameters
.size
== 1)
243 CCECBusDevice
*device
= GetDevice(command
.initiator
);
245 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
250 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
253 strLog
.Format(">> %i requests active source", (uint8_t) command
.initiator
);
254 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
255 CCECBusDevice
*device
= GetThisDevice();
257 return device
->BroadcastActiveSource();
261 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
263 if (command
.parameters
.size
== 4)
265 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
266 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
268 CCECBusDevice
*device
= GetDevice(command
.initiator
);
270 device
->SetPhysicalAddress(iNewAddress
, iOldAddress
);
275 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
277 if (command
.parameters
.size
== 3)
279 CCECBusDevice
*device
= GetDevice(command
.initiator
);
282 cec_menu_language language
;
283 language
.device
= command
.initiator
;
284 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
285 language
.language
[iPtr
] = command
.parameters
[iPtr
];
286 language
.language
[3] = 0;
287 device
->SetMenuLanguage(language
);
293 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
295 if (command
.parameters
.size
>= 2)
297 int streamaddr
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
299 strLog
.Format(">> %i requests stream path from physical address %04x", command
.initiator
, streamaddr
);
300 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
301 if (streamaddr
== m_busDevice
->GetMyPhysicalAddress())
303 CCECBusDevice
*device
= GetThisDevice();
305 return device
->BroadcastActiveSource();
312 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
314 if (command
.parameters
.size
> 0)
316 m_busDevice
->GetProcessor()->AddKey();
318 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
321 strLog
.Format("key pressed: %1x", command
.parameters
[0]);
322 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
324 m_busDevice
->GetProcessor()->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
330 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
332 m_busDevice
->GetProcessor()->AddKey();
336 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
339 strLog
.Format("unhandled command with opcode %02x from address %d", command
.opcode
, command
.initiator
);
340 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
);
343 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
345 CCECBusDevice
*device
= NULL
;
347 if (iLogicalAddress
>= CECDEVICE_TV
&& iLogicalAddress
<= CECDEVICE_BROADCAST
)
348 device
= m_busDevice
->GetProcessor()->m_busDevices
[iLogicalAddress
];
353 CCECBusDevice
*CCECCommandHandler::GetThisDevice(void) const
355 return m_busDevice
->GetProcessor()->m_busDevices
[m_busDevice
->GetMyLogicalAddress()];