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_SET_MENU_LANGUAGE
:
56 HandleSetMenuLanguage(command
);
58 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
59 HandleGivePhysicalAddress(command
);
61 case CEC_OPCODE_GIVE_OSD_NAME
:
62 HandleGiveOSDName(command
);
64 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
65 HandleGiveDeviceVendorId(command
);
67 case CEC_OPCODE_DEVICE_VENDOR_ID
:
68 HandleDeviceVendorId(command
);
70 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
71 HandleDeviceVendorCommandWithId(command
);
73 case CEC_OPCODE_GIVE_DECK_STATUS
:
74 HandleGiveDeckStatus(command
);
76 case CEC_OPCODE_MENU_REQUEST
:
77 HandleMenuRequest(command
);
79 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
80 HandleGiveDevicePowerStatus(command
);
82 case CEC_OPCODE_GET_CEC_VERSION
:
83 HandleGetCecVersion(command
);
85 case CEC_OPCODE_USER_CONTROL_PRESSED
:
86 HandleUserControlPressed(command
);
88 case CEC_OPCODE_USER_CONTROL_RELEASE
:
89 HandleUserControlRelease(command
);
92 UnhandledCommand(command
);
97 else if (command
.destination
== CECDEVICE_BROADCAST
)
100 switch (command
.opcode
)
102 case CEC_OPCODE_SET_MENU_LANGUAGE
:
103 HandleSetMenuLanguage(command
);
105 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
106 HandleRequestActiveSource(command
);
108 case CEC_OPCODE_SET_STREAM_PATH
:
109 HandleSetStreamPath(command
);
111 case CEC_OPCODE_ROUTING_CHANGE
:
112 HandleRoutingChange(command
);
114 case CEC_OPCODE_DEVICE_VENDOR_ID
:
115 HandleDeviceVendorId(command
);
117 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
118 HandleDeviceVendorCommandWithId(command
);
121 UnhandledCommand(command
);
129 strLog
.Format("ignoring frame: destination: %u != %u", command
.destination
, (uint8_t)m_busDevice
->GetMyLogicalAddress());
130 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
137 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
139 if (command
.parameters
.size
== 1)
141 CCECBusDevice
*device
= GetDevice(command
.initiator
);
143 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
149 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
151 CCECBusDevice
*device
= GetDevice(command
.initiator
);
153 device
->SetVendorId(command
.parameters
);
158 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
160 CCECBusDevice
*device
= GetDevice(command
.initiator
);
162 device
->SetVendorId(command
.parameters
);
167 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
169 CCECBusDevice
*device
= GetDevice(command
.initiator
);
171 return device
->ReportCECVersion();
176 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
178 CCECBusDevice
*device
= GetDevice(command
.initiator
);
180 return device
->ReportDeckStatus();
185 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
187 CCECBusDevice
*device
= GetDevice(command
.initiator
);
189 return device
->ReportPowerState();
194 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
196 CCECBusDevice
*device
= GetDevice(command
.initiator
);
198 return device
->ReportVendorID();
203 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
205 CCECBusDevice
*device
= GetDevice(command
.initiator
);
207 return device
->ReportOSDName();
212 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
214 CCECBusDevice
*device
= GetThisDevice();
216 return device
->BroadcastPhysicalAddress();
221 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
223 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_QUERY
)
225 CCECBusDevice
*device
= GetDevice(command
.initiator
);
227 return device
->ReportMenuState();
232 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
235 strLog
.Format(">> %i requests active source", (uint8_t) command
.initiator
);
236 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
237 CCECBusDevice
*device
= GetThisDevice();
239 return device
->BroadcastActiveSource();
243 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
245 if (command
.parameters
.size
== 4)
247 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
248 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
250 CCECBusDevice
*device
= GetDevice(command
.initiator
);
252 device
->SetPhysicalAddress(iNewAddress
, iOldAddress
);
257 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
259 if (command
.parameters
.size
== 3)
261 CCECBusDevice
*device
= GetDevice(command
.initiator
);
264 cec_menu_language language
;
265 language
.device
= command
.initiator
;
266 for (unsigned int iPtr
= 0; iPtr
< 4; iPtr
++)
267 language
.language
[iPtr
] = command
.parameters
[iPtr
];
268 language
.language
[3] = 0;
269 device
->SetMenuLanguage(language
);
276 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
278 if (command
.parameters
.size
>= 2)
280 int streamaddr
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
282 strLog
.Format(">> %i requests stream path from physical address %04x", command
.initiator
, streamaddr
);
283 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
284 if (streamaddr
== m_busDevice
->GetMyPhysicalAddress())
286 CCECBusDevice
*device
= GetThisDevice();
288 return device
->BroadcastActiveSource();
295 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
297 if (command
.parameters
.size
> 0)
299 m_busDevice
->GetProcessor()->AddKey();
301 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
304 strLog
.Format("key pressed: %1x", command
.parameters
[0]);
305 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
307 m_busDevice
->GetProcessor()->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
313 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
315 m_busDevice
->GetProcessor()->AddKey();
319 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
321 m_busDevice
->GetProcessor()->AddCommand(command
);
324 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
326 CCECBusDevice
*device
= NULL
;
328 if (iLogicalAddress
>= CECDEVICE_TV
&& iLogicalAddress
<= CECDEVICE_BROADCAST
)
329 device
= m_busDevice
->GetProcessor()->m_busDevices
[iLogicalAddress
];
334 CCECBusDevice
*CCECCommandHandler::GetThisDevice(void) const
336 return m_busDevice
->GetProcessor()->m_busDevices
[m_busDevice
->GetMyLogicalAddress()];