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 #define LIB_CEC m_comm->m_callback->GetLib()
41 #define ToString(p) LIB_CEC->ToString(p)
43 CUSBCECAdapterCommands::CUSBCECAdapterCommands(CUSBCECAdapterCommunication
*comm
) :
45 m_bSettingsRetrieved(false),
46 m_bSettingAutoEnabled(false),
47 m_settingCecVersion(CEC_VERSION_UNKNOWN
),
50 m_iBuildDate(CEC_FW_BUILD_UNKNOWN
)
52 m_persistedConfiguration
.Clear();
55 cec_datapacket
CUSBCECAdapterCommands::RequestSetting(cec_adapter_messagecode msgCode
)
57 cec_datapacket retVal
;
60 CCECAdapterMessage params
;
61 CCECAdapterMessage
*message
= m_comm
->SendCommand(msgCode
, params
);
62 if (message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
)
64 retVal
= message
->response
;
65 retVal
.Shift(2); // shift out start and msgcode
66 retVal
.size
-= 1; // remove end
73 uint16_t CUSBCECAdapterCommands::RequestFirmwareVersion(void)
75 m_persistedConfiguration
.iFirmwareVersion
= CEC_FW_VERSION_UNKNOWN
;
76 unsigned int iFwVersionTry(0);
78 while (m_persistedConfiguration
.iFirmwareVersion
== CEC_FW_VERSION_UNKNOWN
&& iFwVersionTry
++ < 3)
80 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "requesting the firmware version");
81 cec_datapacket response
= RequestSetting(MSGCODE_FIRMWARE_VERSION
);
82 if (response
.size
== 2)
83 m_persistedConfiguration
.iFirmwareVersion
= (response
[0] << 8 | response
[1]);
86 LIB_CEC
->AddLog(CEC_LOG_WARNING
, "the adapter did not respond with a correct firmware version (try %d)", iFwVersionTry
);
91 if (m_persistedConfiguration
.iFirmwareVersion
== CEC_FW_VERSION_UNKNOWN
)
93 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "defaulting to firmware version 1");
94 m_persistedConfiguration
.iFirmwareVersion
= 1;
97 return m_persistedConfiguration
.iFirmwareVersion
;
100 bool CUSBCECAdapterCommands::RequestSettingAutoEnabled(void)
102 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "requesting autonomous mode setting");
104 cec_datapacket response
= RequestSetting(MSGCODE_GET_AUTO_ENABLED
);
105 if (response
.size
== 1)
107 m_bSettingAutoEnabled
= response
[0] == 1;
108 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "using persisted autonomous mode setting: '%s'", m_bSettingAutoEnabled
? "enabled" : "disabled");
114 bool CUSBCECAdapterCommands::RequestSettingCECVersion(void)
116 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "requesting CEC version setting");
118 cec_datapacket response
= RequestSetting(MSGCODE_GET_HDMI_VERSION
);
119 if (response
.size
== 1)
121 m_settingCecVersion
= (cec_version
)response
[0];
122 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "using persisted CEC version setting: '%s'", ToString(m_settingCecVersion
));
128 uint32_t CUSBCECAdapterCommands::RequestBuildDate(void)
130 if (m_iBuildDate
== CEC_FW_BUILD_UNKNOWN
)
132 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "requesting firmware build date");
134 cec_datapacket response
= RequestSetting(MSGCODE_GET_BUILDDATE
);
135 if (response
.size
== 4)
136 m_iBuildDate
= (uint32_t)response
[0] << 24 | (uint32_t)response
[1] << 16 | (uint32_t)response
[2] << 8 | (uint32_t)response
[3];
141 bool CUSBCECAdapterCommands::RequestSettingDefaultLogicalAddress(void)
143 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "requesting default logical address setting");
145 cec_datapacket response
= RequestSetting(MSGCODE_GET_DEFAULT_LOGICAL_ADDRESS
);
146 if (response
.size
== 1)
148 m_persistedConfiguration
.logicalAddresses
.primary
= (cec_logical_address
)response
[0];
149 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "using persisted logical address setting: '%s'", ToString(m_persistedConfiguration
.logicalAddresses
.primary
));
155 bool CUSBCECAdapterCommands::RequestSettingDeviceType(void)
157 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "requesting device type setting");
158 m_persistedConfiguration
.deviceTypes
.Clear();
160 cec_datapacket response
= RequestSetting(MSGCODE_GET_DEVICE_TYPE
);
161 if (response
.size
== 1)
163 m_persistedConfiguration
.deviceTypes
.Add((cec_device_type
)response
[0]);
164 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "using persisted device type setting: '%s'", ToString((cec_device_type
)response
[0]));
167 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "no persisted device type setting");
171 bool CUSBCECAdapterCommands::RequestSettingLogicalAddressMask(void)
173 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "requesting logical address mask setting");
175 cec_datapacket response
= RequestSetting(MSGCODE_GET_LOGICAL_ADDRESS_MASK
);
176 if (response
.size
== 2)
178 m_iSettingLAMask
= ((uint16_t)response
[0] << 8) | ((uint16_t)response
[1]);
179 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "using persisted logical address mask setting: '%x'", m_iSettingLAMask
);
185 bool CUSBCECAdapterCommands::RequestSettingOSDName(void)
187 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "requesting OSD name setting");
189 memset(m_persistedConfiguration
.strDeviceName
, 0, 13);
190 cec_datapacket response
= RequestSetting(MSGCODE_GET_OSD_NAME
);
191 if (response
.size
== 0)
193 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "no persisted device name setting");
198 for (uint8_t iPtr
= 0; iPtr
< response
.size
&& iPtr
< 13; iPtr
++)
199 buf
[iPtr
] = (char)response
[iPtr
];
200 buf
[response
.size
] = 0;
202 snprintf(m_persistedConfiguration
.strDeviceName
, 13, "%s", buf
);
203 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "using persisted device name setting: '%s'", buf
);
207 bool CUSBCECAdapterCommands::RequestSettingPhysicalAddress(void)
209 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "requesting physical address setting");
211 cec_datapacket response
= RequestSetting(MSGCODE_GET_PHYSICAL_ADDRESS
);
212 if (response
.size
== 2)
214 m_persistedConfiguration
.iPhysicalAddress
= ((uint16_t)response
[0] << 8) | ((uint16_t)response
[1]);
215 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "using persisted physical address setting: '%4x'", m_persistedConfiguration
.iPhysicalAddress
);
218 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "no persisted physical address setting");
222 bool CUSBCECAdapterCommands::SetSettingAutoEnabled(bool enabled
)
226 /* check whether this value was changed */
227 if (m_bSettingAutoEnabled
== enabled
)
229 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "autonomous mode setting unchanged (%s)", enabled
? "on" : "off");
233 m_bNeedsWrite
= true;
234 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "turning autonomous mode %s", enabled
? "on" : "off");
236 CCECAdapterMessage params
;
237 params
.PushEscaped(enabled
? 1 : 0);
238 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_AUTO_ENABLED
, params
);
239 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
243 m_bSettingAutoEnabled
= enabled
;
248 bool CUSBCECAdapterCommands::SetSettingDeviceType(cec_device_type type
)
252 /* check whether this value was changed */
253 if (m_persistedConfiguration
.deviceTypes
.types
[0] == type
)
255 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "device type setting unchanged (%X)", (uint8_t)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
)
278 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "logical address setting unchanged (%X)", (uint8_t)address
);
282 m_bNeedsWrite
= true;
283 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting the default logical address to %X (previous: %X)", (uint8_t)address
, (uint8_t)m_persistedConfiguration
.logicalAddresses
.primary
);
285 CCECAdapterMessage params
;
286 params
.PushEscaped((uint8_t)address
);
287 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS
, params
);
288 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
292 m_persistedConfiguration
.logicalAddresses
.primary
= address
;
297 bool CUSBCECAdapterCommands::SetSettingLogicalAddressMask(uint16_t iMask
)
301 /* check whether this value was changed */
302 if (m_iSettingLAMask
== iMask
)
304 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "logical address mask setting unchanged (%2X)", iMask
);
308 m_bNeedsWrite
= true;
309 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting the logical address mask to %2X (previous: %2X)", iMask
, m_iSettingLAMask
);
311 CCECAdapterMessage params
;
312 params
.PushEscaped(iMask
>> 8);
313 params
.PushEscaped((uint8_t)iMask
);
314 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_LOGICAL_ADDRESS_MASK
, params
);
315 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
319 m_iSettingLAMask
= iMask
;
324 bool CUSBCECAdapterCommands::SetSettingPhysicalAddress(uint16_t iPhysicalAddress
)
328 /* check whether this value was changed */
329 if (m_persistedConfiguration
.iPhysicalAddress
== iPhysicalAddress
)
331 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "physical address setting unchanged (%04X)", iPhysicalAddress
);
335 m_bNeedsWrite
= true;
336 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting the physical address to %04X (previous: %04X)", iPhysicalAddress
, m_persistedConfiguration
.iPhysicalAddress
);
338 CCECAdapterMessage params
;
339 params
.PushEscaped(iPhysicalAddress
>> 8);
340 params
.PushEscaped((uint8_t)iPhysicalAddress
);
341 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_PHYSICAL_ADDRESS
, params
);
342 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
346 m_persistedConfiguration
.iPhysicalAddress
= iPhysicalAddress
;
351 bool CUSBCECAdapterCommands::SetSettingCECVersion(cec_version version
)
355 /* check whether this value was changed */
356 if (m_settingCecVersion
== version
)
358 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "CEC version setting unchanged (%s)", ToString(version
));
362 m_bNeedsWrite
= true;
363 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting the CEC version to %s (previous: %s)", ToString(version
), ToString(m_settingCecVersion
));
365 CCECAdapterMessage params
;
366 params
.PushEscaped((uint8_t)version
);
367 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_HDMI_VERSION
, params
);
368 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
372 m_settingCecVersion
= version
;
377 bool CUSBCECAdapterCommands::SetSettingOSDName(const char *strOSDName
)
381 /* check whether this value was changed */
382 if (!strcmp(m_persistedConfiguration
.strDeviceName
, strOSDName
))
384 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "OSD name setting unchanged (%s)", strOSDName
);
388 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting the OSD name to %s (previous: %s)", strOSDName
, m_persistedConfiguration
.strDeviceName
);
390 CCECAdapterMessage params
;
391 for (size_t iPtr
= 0; iPtr
< strlen(strOSDName
); iPtr
++)
392 params
.PushEscaped(strOSDName
[iPtr
]);
393 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_OSD_NAME
, params
);
394 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
398 snprintf(m_persistedConfiguration
.strDeviceName
, 13, "%s", strOSDName
);
403 bool CUSBCECAdapterCommands::WriteEEPROM(void)
408 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "writing settings in the EEPROM");
410 CCECAdapterMessage params
;
411 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_WRITE_EEPROM
, params
);
412 m_bNeedsWrite
= !(message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
);
414 return m_bNeedsWrite
;
417 bool CUSBCECAdapterCommands::PersistConfiguration(const libcec_configuration
&configuration
)
419 if (m_persistedConfiguration
.iFirmwareVersion
< 2)
422 if (!RequestSettings())
426 bReturn
&= SetSettingAutoEnabled(true);
427 bReturn
&= SetSettingDeviceType(CLibCEC::GetType(configuration
.logicalAddresses
.primary
));
428 bReturn
&= SetSettingDefaultLogicalAddress(configuration
.logicalAddresses
.primary
);
429 bReturn
&= SetSettingLogicalAddressMask(CLibCEC::GetMaskForType(configuration
.logicalAddresses
.primary
));
430 bReturn
&= SetSettingPhysicalAddress(configuration
.iPhysicalAddress
);
431 bReturn
&= SetSettingCECVersion(CEC_VERSION_1_3A
);
432 bReturn
&= SetSettingOSDName(configuration
.strDeviceName
);
433 bReturn
&= WriteEEPROM();
437 bool CUSBCECAdapterCommands::RequestSettings(void)
439 if (m_persistedConfiguration
.iFirmwareVersion
< 2)
441 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - firmware version %d does not have any eeprom settings", __FUNCTION__
, m_persistedConfiguration
.iFirmwareVersion
);
442 // settings can only be persisted with firmware v2+
446 if (m_bSettingsRetrieved
)
450 bReturn
&= RequestSettingAutoEnabled();
451 bReturn
&= RequestSettingCECVersion();
452 bReturn
&= RequestSettingDefaultLogicalAddress();
453 bReturn
&= RequestSettingDeviceType();
454 bReturn
&= RequestSettingLogicalAddressMask();
455 bReturn
&= RequestSettingOSDName();
456 bReturn
&= RequestSettingPhysicalAddress();
458 // don't read the following settings:
459 // - auto enabled (always enabled)
460 // - default logical address (autodetected)
461 // - logical address mask (autodetected)
462 // - CEC version (1.3a)
464 // TODO to be added to the firmware:
465 // - base device (4 bits)
466 // - HDMI port number (4 bits)
467 // - TV vendor id (12 bits)
468 // - wake devices (8 bits)
469 // - standby devices (8 bits)
470 // - use TV menu language (1 bit)
471 // - activate source (1 bit)
472 // - power off screensaver (1 bit)
473 // - power off on standby (1 bit)
474 // - send inactive source (1 bit)
476 m_bSettingsRetrieved
= true;
481 bool CUSBCECAdapterCommands::GetConfiguration(libcec_configuration
&configuration
)
483 // get the settings from the eeprom if needed
484 if (!RequestSettings())
488 configuration
.iFirmwareVersion
= m_persistedConfiguration
.iFirmwareVersion
;
489 configuration
.deviceTypes
= m_persistedConfiguration
.deviceTypes
;
490 configuration
.iPhysicalAddress
= m_persistedConfiguration
.iPhysicalAddress
;
491 snprintf(configuration
.strDeviceName
, 13, "%s", m_persistedConfiguration
.strDeviceName
);
496 bool CUSBCECAdapterCommands::PingAdapter(void)
498 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "sending ping");
500 CCECAdapterMessage params
;
501 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_PING
, params
);
502 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
507 bool CUSBCECAdapterCommands::SetAckMask(uint16_t iMask
)
509 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting ackmask to %2x", iMask
);
511 CCECAdapterMessage params
;
512 params
.PushEscaped(iMask
>> 8);
513 params
.PushEscaped((uint8_t)iMask
);
514 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_ACK_MASK
, params
);
515 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
520 bool CUSBCECAdapterCommands::StartBootloader(void)
522 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
524 CCECAdapterMessage params
;
525 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_START_BOOTLOADER
, params
);
526 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
531 bool CUSBCECAdapterCommands::SetLineTimeout(uint8_t iTimeout
)
533 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting the line timeout to %d", iTimeout
);
534 CCECAdapterMessage params
;
535 params
.PushEscaped(iTimeout
);
536 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_TRANSMIT_IDLETIME
, params
);
537 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
542 bool CUSBCECAdapterCommands::SetControlledMode(bool controlled
)
544 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "turning controlled mode %s", controlled
? "on" : "off");
546 CCECAdapterMessage params
;
547 params
.PushEscaped(controlled
? 1 : 0);
548 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_CONTROLLED
, params
);
549 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;