2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2012 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 "USBCECAdapterCommands.h"
34 #include "../LibCEC.h"
35 #include "../CECProcessor.h"
38 using namespace PLATFORM
;
40 cec_datapacket
CUSBCECAdapterCommands::RequestSetting(cec_adapter_messagecode msgCode
)
42 cec_datapacket retVal
;
45 CCECAdapterMessage params
;
46 CCECAdapterMessage
*message
= m_comm
->SendCommand(msgCode
, params
);
47 if (message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
)
49 retVal
= message
->response
;
50 retVal
.Shift(2); // shift out start and msgcode
51 retVal
.size
-= 1; // remove end
57 uint16_t CUSBCECAdapterCommands::RequestFirmwareVersion(void)
59 m_iFirmwareVersion
= CEC_FW_VERSION_UNKNOWN
;
60 unsigned int iFwVersionTry(0);
62 while (m_iFirmwareVersion
== CEC_FW_VERSION_UNKNOWN
&& iFwVersionTry
++ < 3)
64 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting the firmware version");
65 cec_datapacket response
= RequestSetting(MSGCODE_FIRMWARE_VERSION
);
66 if (response
.size
== 2)
67 m_iFirmwareVersion
= (response
[0] << 8 | response
[1]);
70 CLibCEC::AddLog(CEC_LOG_WARNING
, "the adapter did not respond with a correct firmware version (try %d)", iFwVersionTry
);
75 if (m_iFirmwareVersion
== CEC_FW_VERSION_UNKNOWN
)
77 CLibCEC::AddLog(CEC_LOG_DEBUG
, "defaulting to firmware version 1");
78 m_iFirmwareVersion
= 1;
81 return m_iFirmwareVersion
;
84 bool CUSBCECAdapterCommands::RequestSettingAutoEnabled(bool &enabled
)
86 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting autonomous mode setting");
88 cec_datapacket response
= RequestSetting(MSGCODE_GET_AUTO_ENABLED
);
89 if (response
.size
== 1)
91 enabled
= response
[0] == 1;
97 bool CUSBCECAdapterCommands::RequestSettingCECVersion(cec_version
&version
)
99 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting CEC version setting");
101 cec_datapacket response
= RequestSetting(MSGCODE_GET_HDMI_VERSION
);
102 if (response
.size
== 1)
104 version
= (cec_version
)response
[0];
110 bool CUSBCECAdapterCommands::RequestSettingDefaultLogicalAddress(cec_logical_address
&address
)
112 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting default logical address setting");
114 cec_datapacket response
= RequestSetting(MSGCODE_GET_DEFAULT_LOGICAL_ADDRESS
);
115 if (response
.size
== 1)
117 address
= (cec_logical_address
)response
[0];
123 bool CUSBCECAdapterCommands::RequestSettingDeviceType(cec_device_type
&value
)
125 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting device type setting");
127 cec_datapacket response
= RequestSetting(MSGCODE_GET_DEVICE_TYPE
);
128 if (response
.size
== 1)
130 value
= (cec_device_type
)response
[0];
136 bool CUSBCECAdapterCommands::RequestSettingLogicalAddressMask(uint16_t &iMask
)
138 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting logical address mask setting");
140 cec_datapacket response
= RequestSetting(MSGCODE_GET_LOGICAL_ADDRESS_MASK
);
141 if (response
.size
== 2)
143 iMask
= ((uint16_t)response
[0] << 8) | ((uint16_t)response
[1]);
149 bool CUSBCECAdapterCommands::RequestSettingOSDName(CStdString
&strOSDName
)
151 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting OSD name setting");
153 cec_datapacket response
= RequestSetting(MSGCODE_GET_OSD_NAME
);
154 if (response
.size
== 0)
158 for (uint8_t iPtr
= 0; iPtr
< response
.size
&& iPtr
< 13; iPtr
++)
159 buf
[iPtr
] = (char)response
[iPtr
];
160 buf
[response
.size
] = 0;
162 strOSDName
.Format("%s", buf
);
166 bool CUSBCECAdapterCommands::RequestSettingPhysicalAddress(uint16_t &iPhysicalAddress
)
168 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting physical address setting");
170 cec_datapacket response
= RequestSetting(MSGCODE_GET_PHYSICAL_ADDRESS
);
171 if (response
.size
== 2)
173 iPhysicalAddress
= ((uint16_t)response
[0] << 8) | ((uint16_t)response
[1]);
179 bool CUSBCECAdapterCommands::SetSettingAutoEnabled(bool enabled
)
181 CLibCEC::AddLog(CEC_LOG_DEBUG
, "turning autonomous mode %s", enabled
? "on" : "off");
183 CCECAdapterMessage params
;
184 params
.PushEscaped(enabled
? 1 : 0);
185 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_AUTO_ENABLED
, params
);
186 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
191 bool CUSBCECAdapterCommands::SetSettingDeviceType(cec_device_type type
)
193 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the device type to %1X", (uint8_t)type
);
195 CCECAdapterMessage params
;
196 params
.PushEscaped((uint8_t)type
);
197 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_DEVICE_TYPE
, params
);
198 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
203 bool CUSBCECAdapterCommands::SetSettingDefaultLogicalAddress(cec_logical_address address
)
205 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the default logical address to %1X", address
);
207 CCECAdapterMessage params
;
208 params
.PushEscaped((uint8_t)address
);
209 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS
, params
);
210 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
215 bool CUSBCECAdapterCommands::SetSettingLogicalAddressMask(uint16_t iMask
)
217 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the logical address mask to %2X", iMask
);
219 CCECAdapterMessage params
;
220 params
.PushEscaped(iMask
>> 8);
221 params
.PushEscaped((uint8_t)iMask
);
222 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_LOGICAL_ADDRESS_MASK
, params
);
223 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
228 bool CUSBCECAdapterCommands::SetSettingPhysicalAddress(uint16_t iPhysicalAddress
)
230 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the physical address to %04X", iPhysicalAddress
);
232 CCECAdapterMessage params
;
233 params
.PushEscaped(iPhysicalAddress
>> 8);
234 params
.PushEscaped((uint8_t)iPhysicalAddress
);
235 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_PHYSICAL_ADDRESS
, params
);
236 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
241 bool CUSBCECAdapterCommands::SetSettingCECVersion(cec_version version
)
243 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the CEC version to %s", CLibCEC::GetInstance()->ToString(version
));
245 CCECAdapterMessage params
;
246 params
.PushEscaped((uint8_t)version
);
247 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_HDMI_VERSION
, params
);
248 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
253 bool CUSBCECAdapterCommands::SetSettingOSDName(const char *strOSDName
)
255 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the OSD name to %s", strOSDName
);
257 CCECAdapterMessage params
;
258 for (size_t iPtr
= 0; iPtr
< strlen(strOSDName
); iPtr
++)
259 params
.PushEscaped(strOSDName
[iPtr
]);
260 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_OSD_NAME
, params
);
261 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
266 bool CUSBCECAdapterCommands::WriteEEPROM(void)
268 CLibCEC::AddLog(CEC_LOG_DEBUG
, "writing settings in the EEPROM");
270 CCECAdapterMessage params
;
271 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_WRITE_EEPROM
, params
);
272 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
277 bool CUSBCECAdapterCommands::PersistConfiguration(libcec_configuration
*configuration
)
279 if (m_iFirmwareVersion
< 2)
283 bReturn
&= SetSettingAutoEnabled(true);
284 bReturn
&= SetSettingDeviceType(CLibCEC::GetType(configuration
->logicalAddresses
.primary
));
285 bReturn
&= SetSettingDefaultLogicalAddress(configuration
->logicalAddresses
.primary
);
286 bReturn
&= SetSettingLogicalAddressMask(CLibCEC::GetMaskForType(configuration
->logicalAddresses
.primary
));
287 bReturn
&= SetSettingPhysicalAddress(configuration
->iPhysicalAddress
);
288 bReturn
&= SetSettingCECVersion(CEC_VERSION_1_3A
);
289 bReturn
&= SetSettingOSDName(configuration
->strDeviceName
);
291 bReturn
= WriteEEPROM();
295 bool CUSBCECAdapterCommands::GetConfiguration(libcec_configuration
*configuration
)
297 configuration
->iFirmwareVersion
= m_iFirmwareVersion
;
298 if (m_iFirmwareVersion
< 2)
302 cec_device_type type
;
303 if (RequestSettingDeviceType(type
))
305 CLibCEC::AddLog(CEC_LOG_DEBUG
, "using persisted device type setting %s", CLibCEC::GetInstance()->ToString(type
));
306 configuration
->deviceTypes
.Clear();
307 configuration
->deviceTypes
.Add(type
);
311 CLibCEC::AddLog(CEC_LOG_DEBUG
, "no persisted device type setting");
315 if (RequestSettingPhysicalAddress(configuration
->iPhysicalAddress
))
317 CLibCEC::AddLog(CEC_LOG_DEBUG
, "using persisted physical address setting %4x", configuration
->iPhysicalAddress
);
321 CLibCEC::AddLog(CEC_LOG_DEBUG
, "no persisted physical address setting");
325 CStdString strDeviceName
;
326 if (RequestSettingOSDName(strDeviceName
))
328 snprintf(configuration
->strDeviceName
, 13, "%s", strDeviceName
.c_str());
329 CLibCEC::AddLog(CEC_LOG_DEBUG
, "using persisted device name setting %s", configuration
->strDeviceName
);
333 CLibCEC::AddLog(CEC_LOG_DEBUG
, "no persisted device name setting");
337 // don't read the following settings:
338 // - auto enabled (always enabled)
339 // - default logical address (autodetected)
340 // - logical address mask (autodetected)
341 // - CEC version (1.3a)
343 // TODO to be added to the firmware:
344 // - base device (4 bits)
345 // - HDMI port number (4 bits)
346 // - TV vendor id (12 bits)
347 // - wake devices (8 bits)
348 // - standby devices (8 bits)
349 // - use TV menu language (1 bit)
350 // - activate source (1 bit)
351 // - power off screensaver (1 bit)
352 // - power off on standby (1 bit)
353 // - send inactive source (1 bit)
357 bool CUSBCECAdapterCommands::PingAdapter(void)
359 CLibCEC::AddLog(CEC_LOG_DEBUG
, "sending ping");
361 CCECAdapterMessage params
;
362 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_PING
, params
);
363 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
368 bool CUSBCECAdapterCommands::SetAckMask(uint16_t iMask
)
370 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting ackmask to %2x", iMask
);
372 CCECAdapterMessage params
;
373 params
.PushEscaped(iMask
>> 8);
374 params
.PushEscaped((uint8_t)iMask
);
375 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_ACK_MASK
, params
);
376 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
381 bool CUSBCECAdapterCommands::StartBootloader(void)
383 CLibCEC::AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
385 CCECAdapterMessage params
;
386 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_START_BOOTLOADER
, params
);
387 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
392 bool CUSBCECAdapterCommands::SetLineTimeout(uint8_t iTimeout
)
394 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the line timeout to %d", iTimeout
);
395 CCECAdapterMessage params
;
396 params
.PushEscaped(iTimeout
);
397 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_TRANSMIT_IDLETIME
, params
);
398 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
403 bool CUSBCECAdapterCommands::SetControlledMode(bool controlled
)
405 CLibCEC::AddLog(CEC_LOG_DEBUG
, "turning controlled mode %s", controlled
? "on" : "off");
407 CCECAdapterMessage params
;
408 params
.PushEscaped(controlled
? 1 : 0);
409 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_CONTROLLED
, params
);
410 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;