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/
34 #include "USBCECAdapterCommands.h"
36 #include "USBCECAdapterMessage.h"
37 #include "USBCECAdapterCommunication.h"
38 #include "lib/LibCEC.h"
39 #include "lib/CECProcessor.h"
40 #include "lib/CECTypeUtils.h"
44 using namespace PLATFORM
;
46 #define LIB_CEC m_comm->m_callback->GetLib()
47 #define ToString(p) CCECTypeUtils::ToString(p)
49 CUSBCECAdapterCommands::CUSBCECAdapterCommands(CUSBCECAdapterCommunication
*comm
) :
51 m_bSettingsRetrieved(false),
52 m_bSettingAutoEnabled(false),
53 m_settingCecVersion(CEC_VERSION_UNKNOWN
),
56 m_iBuildDate(CEC_FW_BUILD_UNKNOWN
)
58 m_persistedConfiguration
.Clear();
61 cec_datapacket
CUSBCECAdapterCommands::RequestSetting(cec_adapter_messagecode msgCode
)
63 cec_datapacket retVal
;
66 CCECAdapterMessage params
;
67 CCECAdapterMessage
*message
= m_comm
->SendCommand(msgCode
, params
);
68 if (message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
)
70 retVal
= message
->response
;
71 retVal
.Shift(2); // shift out start and msgcode
72 retVal
.size
-= 1; // remove end
79 uint16_t CUSBCECAdapterCommands::RequestFirmwareVersion(void)
81 m_persistedConfiguration
.iFirmwareVersion
= CEC_FW_VERSION_UNKNOWN
;
82 unsigned int iFwVersionTry(0);
84 while (m_persistedConfiguration
.iFirmwareVersion
== CEC_FW_VERSION_UNKNOWN
&& iFwVersionTry
++ < 3)
86 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "requesting the firmware version");
87 cec_datapacket response
= RequestSetting(MSGCODE_FIRMWARE_VERSION
);
88 if (response
.size
== 2)
89 m_persistedConfiguration
.iFirmwareVersion
= (response
[0] << 8 | response
[1]);
92 LIB_CEC
->AddLog(CEC_LOG_WARNING
, "the adapter did not respond with a correct firmware version (try %d)", iFwVersionTry
);
97 if (m_persistedConfiguration
.iFirmwareVersion
== CEC_FW_VERSION_UNKNOWN
)
99 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "defaulting to firmware version 1");
100 m_persistedConfiguration
.iFirmwareVersion
= 1;
103 return m_persistedConfiguration
.iFirmwareVersion
;
106 bool CUSBCECAdapterCommands::RequestSettingAutoEnabled(void)
108 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "requesting autonomous mode setting");
110 cec_datapacket response
= RequestSetting(MSGCODE_GET_AUTO_ENABLED
);
111 if (response
.size
== 1)
113 m_bSettingAutoEnabled
= response
[0] == 1;
114 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "using persisted autonomous mode setting: '%s'", m_bSettingAutoEnabled
? "enabled" : "disabled");
120 bool CUSBCECAdapterCommands::RequestSettingCECVersion(void)
122 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "requesting CEC version setting");
124 cec_datapacket response
= RequestSetting(MSGCODE_GET_HDMI_VERSION
);
125 if (response
.size
== 1)
127 m_settingCecVersion
= (cec_version
)response
[0];
128 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "using persisted CEC version setting: '%s'", ToString(m_settingCecVersion
));
134 uint32_t CUSBCECAdapterCommands::RequestBuildDate(void)
136 if (m_iBuildDate
== CEC_FW_BUILD_UNKNOWN
)
138 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "requesting firmware build date");
140 cec_datapacket response
= RequestSetting(MSGCODE_GET_BUILDDATE
);
141 if (response
.size
== 4)
142 m_iBuildDate
= (uint32_t)response
[0] << 24 | (uint32_t)response
[1] << 16 | (uint32_t)response
[2] << 8 | (uint32_t)response
[3];
147 bool CUSBCECAdapterCommands::RequestSettingDefaultLogicalAddress(void)
149 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "requesting default logical address setting");
151 cec_datapacket response
= RequestSetting(MSGCODE_GET_DEFAULT_LOGICAL_ADDRESS
);
152 if (response
.size
== 1)
154 m_persistedConfiguration
.logicalAddresses
.primary
= (cec_logical_address
)response
[0];
155 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "using persisted logical address setting: '%s'", ToString(m_persistedConfiguration
.logicalAddresses
.primary
));
161 bool CUSBCECAdapterCommands::RequestSettingDeviceType(void)
163 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "requesting device type setting");
164 m_persistedConfiguration
.deviceTypes
.Clear();
166 cec_datapacket response
= RequestSetting(MSGCODE_GET_DEVICE_TYPE
);
167 if (response
.size
== 1)
169 m_persistedConfiguration
.deviceTypes
.Add((cec_device_type
)response
[0]);
170 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "using persisted device type setting: '%s'", ToString((cec_device_type
)response
[0]));
173 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "no persisted device type setting");
177 bool CUSBCECAdapterCommands::RequestSettingLogicalAddressMask(void)
179 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "requesting logical address mask setting");
181 cec_datapacket response
= RequestSetting(MSGCODE_GET_LOGICAL_ADDRESS_MASK
);
182 if (response
.size
== 2)
184 m_iSettingLAMask
= ((uint16_t)response
[0] << 8) | ((uint16_t)response
[1]);
185 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "using persisted logical address mask setting: '%x'", m_iSettingLAMask
);
191 bool CUSBCECAdapterCommands::RequestSettingOSDName(void)
193 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "requesting OSD name setting");
195 memset(m_persistedConfiguration
.strDeviceName
, 0, 13);
196 cec_datapacket response
= RequestSetting(MSGCODE_GET_OSD_NAME
);
197 if (response
.size
== 0)
199 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "no persisted device name setting");
204 for (uint8_t iPtr
= 0; iPtr
< response
.size
&& iPtr
< 13; iPtr
++)
205 buf
[iPtr
] = (char)response
[iPtr
];
206 buf
[response
.size
] = 0;
208 snprintf(m_persistedConfiguration
.strDeviceName
, 13, "%s", buf
);
209 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "using persisted device name setting: '%s'", buf
);
213 bool CUSBCECAdapterCommands::RequestSettingPhysicalAddress(void)
215 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "requesting physical address setting");
217 cec_datapacket response
= RequestSetting(MSGCODE_GET_PHYSICAL_ADDRESS
);
218 if (response
.size
== 2)
220 m_persistedConfiguration
.iPhysicalAddress
= ((uint16_t)response
[0] << 8) | ((uint16_t)response
[1]);
221 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "using persisted physical address setting: '%4x'", m_persistedConfiguration
.iPhysicalAddress
);
224 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "no persisted physical address setting");
228 bool CUSBCECAdapterCommands::SetSettingAutoEnabled(bool enabled
)
232 /* check whether this value was changed */
233 if (m_bSettingAutoEnabled
== enabled
)
236 m_bNeedsWrite
= true;
237 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "turning autonomous mode %s", enabled
? "on" : "off");
239 CCECAdapterMessage params
;
240 params
.PushEscaped(enabled
? 1 : 0);
241 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_AUTO_ENABLED
, params
);
242 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
246 m_bSettingAutoEnabled
= enabled
;
251 bool CUSBCECAdapterCommands::SetSettingDeviceType(cec_device_type type
)
255 /* check whether this value was changed */
256 if (m_persistedConfiguration
.deviceTypes
.types
[0] == type
)
259 m_bNeedsWrite
= true;
260 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting the device type to %X (previous: %X)", (uint8_t)type
, (uint8_t)m_persistedConfiguration
.deviceTypes
.types
[0]);
262 CCECAdapterMessage params
;
263 params
.PushEscaped((uint8_t)type
);
264 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_DEVICE_TYPE
, params
);
265 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
271 bool CUSBCECAdapterCommands::SetSettingDefaultLogicalAddress(cec_logical_address address
)
275 /* check whether this value was changed */
276 if (m_persistedConfiguration
.logicalAddresses
.primary
== address
)
279 m_bNeedsWrite
= true;
280 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting the default logical address to %X (previous: %X)", (uint8_t)address
, (uint8_t)m_persistedConfiguration
.logicalAddresses
.primary
);
282 CCECAdapterMessage params
;
283 params
.PushEscaped((uint8_t)address
);
284 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS
, params
);
285 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
289 m_persistedConfiguration
.logicalAddresses
.primary
= address
;
294 bool CUSBCECAdapterCommands::SetSettingLogicalAddressMask(uint16_t iMask
)
298 /* check whether this value was changed */
299 if (m_iSettingLAMask
== iMask
)
302 m_bNeedsWrite
= true;
303 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting the logical address mask to %2X (previous: %2X)", iMask
, m_iSettingLAMask
);
305 CCECAdapterMessage params
;
306 params
.PushEscaped(iMask
>> 8);
307 params
.PushEscaped((uint8_t)iMask
);
308 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_LOGICAL_ADDRESS_MASK
, params
);
309 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
313 m_iSettingLAMask
= iMask
;
318 bool CUSBCECAdapterCommands::SetSettingPhysicalAddress(uint16_t iPhysicalAddress
)
322 /* check whether this value was changed */
323 if (m_persistedConfiguration
.iPhysicalAddress
== iPhysicalAddress
)
326 m_bNeedsWrite
= true;
327 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting the physical address to %04X (previous: %04X)", iPhysicalAddress
, m_persistedConfiguration
.iPhysicalAddress
);
329 CCECAdapterMessage params
;
330 params
.PushEscaped(iPhysicalAddress
>> 8);
331 params
.PushEscaped((uint8_t)iPhysicalAddress
);
332 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_PHYSICAL_ADDRESS
, params
);
333 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
337 m_persistedConfiguration
.iPhysicalAddress
= iPhysicalAddress
;
342 bool CUSBCECAdapterCommands::SetSettingCECVersion(cec_version version
)
346 /* check whether this value was changed */
347 if (m_settingCecVersion
== version
)
350 m_bNeedsWrite
= true;
351 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting the CEC version to %s (previous: %s)", ToString(version
), ToString(m_settingCecVersion
));
353 CCECAdapterMessage params
;
354 params
.PushEscaped((uint8_t)version
);
355 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_HDMI_VERSION
, params
);
356 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
360 m_settingCecVersion
= version
;
365 bool CUSBCECAdapterCommands::SetSettingOSDName(const char *strOSDName
)
369 /* check whether this value was changed */
370 if (!strcmp(m_persistedConfiguration
.strDeviceName
, strOSDName
))
373 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting the OSD name to %s (previous: %s)", strOSDName
, m_persistedConfiguration
.strDeviceName
);
375 CCECAdapterMessage params
;
376 for (size_t iPtr
= 0; iPtr
< strlen(strOSDName
); iPtr
++)
377 params
.PushEscaped(strOSDName
[iPtr
]);
378 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_OSD_NAME
, params
);
379 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
383 snprintf(m_persistedConfiguration
.strDeviceName
, 13, "%s", strOSDName
);
388 bool CUSBCECAdapterCommands::WriteEEPROM(void)
393 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "writing settings in the EEPROM");
395 CCECAdapterMessage params
;
396 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_WRITE_EEPROM
, params
);
397 m_bNeedsWrite
= !(message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
);
399 return m_bNeedsWrite
;
402 bool CUSBCECAdapterCommands::PersistConfiguration(const libcec_configuration
&configuration
)
405 if (m_persistedConfiguration
.iFirmwareVersion
< 2)
408 if (!RequestSettings())
411 bReturn
|= SetSettingAutoEnabled(true);
412 bReturn
|= SetSettingDeviceType(CLibCEC::GetType(configuration
.logicalAddresses
.primary
));
413 bReturn
|= SetSettingDefaultLogicalAddress(configuration
.logicalAddresses
.primary
);
414 bReturn
|= SetSettingLogicalAddressMask(CLibCEC::GetMaskForType(configuration
.logicalAddresses
.primary
));
415 bReturn
|= SetSettingPhysicalAddress(configuration
.iPhysicalAddress
);
416 bReturn
|= SetSettingCECVersion(configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_8_0
? configuration
.cecVersion
: CEC_VERSION_1_4
);
417 bReturn
|= SetSettingOSDName(configuration
.strDeviceName
);
422 bool CUSBCECAdapterCommands::RequestSettings(void)
424 if (m_persistedConfiguration
.iFirmwareVersion
< 2)
426 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - firmware version %d does not have any eeprom settings", __FUNCTION__
, m_persistedConfiguration
.iFirmwareVersion
);
427 // settings can only be persisted with firmware v2+
431 if (m_bSettingsRetrieved
)
435 bReturn
&= RequestSettingAutoEnabled();
436 bReturn
&= RequestSettingCECVersion();
437 bReturn
&= RequestSettingDefaultLogicalAddress();
438 bReturn
&= RequestSettingDeviceType();
439 bReturn
&= RequestSettingLogicalAddressMask();
440 bReturn
&= RequestSettingOSDName();
441 bReturn
&= RequestSettingPhysicalAddress();
443 // don't read the following settings:
444 // - auto enabled (always enabled)
445 // - default logical address (autodetected)
446 // - logical address mask (autodetected)
447 // - CEC version (1.3a)
449 // TODO to be added to the firmware:
450 // - base device (4 bits)
451 // - HDMI port number (4 bits)
452 // - TV vendor id (12 bits)
453 // - wake devices (8 bits)
454 // - standby devices (8 bits)
455 // - use TV menu language (1 bit)
456 // - activate source (1 bit)
457 // - power off screensaver (1 bit)
458 // - power off on standby (1 bit)
459 // - send inactive source (1 bit)
461 m_bSettingsRetrieved
= true;
466 bool CUSBCECAdapterCommands::GetConfiguration(libcec_configuration
&configuration
)
468 // get the settings from the eeprom if needed
469 if (!RequestSettings())
473 configuration
.iFirmwareVersion
= m_persistedConfiguration
.iFirmwareVersion
;
474 configuration
.deviceTypes
= m_persistedConfiguration
.deviceTypes
;
475 configuration
.iPhysicalAddress
= m_persistedConfiguration
.iPhysicalAddress
;
476 snprintf(configuration
.strDeviceName
, 13, "%s", m_persistedConfiguration
.strDeviceName
);
481 bool CUSBCECAdapterCommands::PingAdapter(void)
483 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "sending ping");
485 CCECAdapterMessage params
;
486 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_PING
, params
);
487 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
492 bool CUSBCECAdapterCommands::SetAckMask(uint16_t iMask
)
494 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting ackmask to %2x", iMask
);
496 CCECAdapterMessage params
;
497 params
.PushEscaped(iMask
>> 8);
498 params
.PushEscaped((uint8_t)iMask
);
499 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_ACK_MASK
, params
);
500 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
505 bool CUSBCECAdapterCommands::StartBootloader(void)
507 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
509 CCECAdapterMessage params
;
510 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_START_BOOTLOADER
, params
);
511 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
516 bool CUSBCECAdapterCommands::SetLineTimeout(uint8_t iTimeout
)
518 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting the line timeout to %d", iTimeout
);
519 CCECAdapterMessage params
;
520 params
.PushEscaped(iTimeout
);
521 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_TRANSMIT_IDLETIME
, params
);
522 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
527 bool CUSBCECAdapterCommands::SetControlledMode(bool controlled
)
529 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "turning controlled mode %s", controlled
? "on" : "off");
531 CCECAdapterMessage params
;
532 params
.PushEscaped(controlled
? 1 : 0);
533 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_CONTROLLED
, params
);
534 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;