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 */
234 CLockObject
lock(m_mutex
);
235 if (m_bSettingAutoEnabled
== enabled
)
237 m_bNeedsWrite
= true;
240 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "turning autonomous mode %s", enabled
? "on" : "off");
242 CCECAdapterMessage params
;
243 params
.PushEscaped(enabled
? 1 : 0);
244 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_AUTO_ENABLED
, params
);
245 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
250 CLockObject
lock(m_mutex
);
251 m_bSettingAutoEnabled
= enabled
;
257 bool CUSBCECAdapterCommands::SetSettingDeviceType(cec_device_type type
)
261 /* check whether this value was changed */
263 CLockObject
lock(m_mutex
);
264 if (m_persistedConfiguration
.deviceTypes
.types
[0] == type
)
266 m_bNeedsWrite
= true;
269 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting the device type to %X (previous: %X)", (uint8_t)type
, (uint8_t)m_persistedConfiguration
.deviceTypes
.types
[0]);
271 CCECAdapterMessage params
;
272 params
.PushEscaped((uint8_t)type
);
273 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_DEVICE_TYPE
, params
);
274 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
279 CLockObject
lock(m_mutex
);
280 m_persistedConfiguration
.deviceTypes
.types
[0] = type
;
286 bool CUSBCECAdapterCommands::SetSettingDefaultLogicalAddress(cec_logical_address address
)
290 /* check whether this value was changed */
292 CLockObject
lock(m_mutex
);
293 if (m_persistedConfiguration
.logicalAddresses
.primary
== address
)
295 m_bNeedsWrite
= true;
298 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting the default logical address to %X (previous: %X)", (uint8_t)address
, (uint8_t)m_persistedConfiguration
.logicalAddresses
.primary
);
300 CCECAdapterMessage params
;
301 params
.PushEscaped((uint8_t)address
);
302 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS
, params
);
303 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
308 CLockObject
lock(m_mutex
);
309 m_persistedConfiguration
.logicalAddresses
.primary
= address
;
315 bool CUSBCECAdapterCommands::SetSettingLogicalAddressMask(uint16_t iMask
)
319 /* check whether this value was changed */
321 CLockObject
lock(m_mutex
);
322 if (m_iSettingLAMask
== iMask
)
324 m_bNeedsWrite
= true;
327 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting the logical address mask to %2X (previous: %2X)", iMask
, m_iSettingLAMask
);
329 CCECAdapterMessage params
;
330 params
.PushEscaped(iMask
>> 8);
331 params
.PushEscaped((uint8_t)iMask
);
332 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_LOGICAL_ADDRESS_MASK
, params
);
333 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
338 CLockObject
lock(m_mutex
);
339 m_iSettingLAMask
= iMask
;
345 bool CUSBCECAdapterCommands::SetSettingPhysicalAddress(uint16_t iPhysicalAddress
)
349 /* check whether this value was changed */
351 CLockObject
lock(m_mutex
);
352 if (m_persistedConfiguration
.iPhysicalAddress
== iPhysicalAddress
)
354 m_bNeedsWrite
= true;
357 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting the physical address to %04X (previous: %04X)", iPhysicalAddress
, m_persistedConfiguration
.iPhysicalAddress
);
359 CCECAdapterMessage params
;
360 params
.PushEscaped(iPhysicalAddress
>> 8);
361 params
.PushEscaped((uint8_t)iPhysicalAddress
);
362 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_PHYSICAL_ADDRESS
, params
);
363 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
368 CLockObject
lock(m_mutex
);
369 m_persistedConfiguration
.iPhysicalAddress
= iPhysicalAddress
;
375 bool CUSBCECAdapterCommands::SetSettingCECVersion(cec_version version
)
379 /* check whether this value was changed */
381 CLockObject
lock(m_mutex
);
382 if (m_settingCecVersion
== version
)
384 m_bNeedsWrite
= true;
387 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting the CEC version to %s (previous: %s)", ToString(version
), ToString(m_settingCecVersion
));
389 CCECAdapterMessage params
;
390 params
.PushEscaped((uint8_t)version
);
391 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_HDMI_VERSION
, params
);
392 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
397 CLockObject
lock(m_mutex
);
398 m_settingCecVersion
= version
;
404 bool CUSBCECAdapterCommands::SetSettingOSDName(const char *strOSDName
)
408 /* check whether this value was changed */
409 if (!strcmp(m_persistedConfiguration
.strDeviceName
, strOSDName
))
412 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting the OSD name to %s (previous: %s)", strOSDName
, m_persistedConfiguration
.strDeviceName
);
414 CCECAdapterMessage params
;
415 for (size_t iPtr
= 0; iPtr
< strlen(strOSDName
); iPtr
++)
416 params
.PushEscaped(strOSDName
[iPtr
]);
417 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_OSD_NAME
, params
);
418 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
422 snprintf(m_persistedConfiguration
.strDeviceName
, 13, "%s", strOSDName
);
427 bool CUSBCECAdapterCommands::WriteEEPROM(void)
430 CLockObject
lock(m_mutex
);
435 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "writing settings in the EEPROM");
437 CCECAdapterMessage params
;
438 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_WRITE_EEPROM
, params
);
439 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
444 CLockObject
lock(m_mutex
);
445 m_bNeedsWrite
= false;
451 bool CUSBCECAdapterCommands::PersistConfiguration(const libcec_configuration
&configuration
)
454 if (m_persistedConfiguration
.iFirmwareVersion
< 2)
457 if (!RequestSettings())
460 bReturn
|= SetSettingAutoEnabled(true);
461 bReturn
|= SetSettingDeviceType(CLibCEC::GetType(configuration
.logicalAddresses
.primary
));
462 bReturn
|= SetSettingDefaultLogicalAddress(configuration
.logicalAddresses
.primary
);
463 bReturn
|= SetSettingLogicalAddressMask(CLibCEC::GetMaskForType(configuration
.logicalAddresses
.primary
));
464 bReturn
|= SetSettingPhysicalAddress(configuration
.iPhysicalAddress
);
465 bReturn
|= SetSettingCECVersion(configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_8_0
? configuration
.cecVersion
: CEC_VERSION_1_4
);
466 bReturn
|= SetSettingOSDName(configuration
.strDeviceName
);
471 bool CUSBCECAdapterCommands::RequestSettings(void)
473 if (m_persistedConfiguration
.iFirmwareVersion
< 2)
475 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - firmware version %d does not have any eeprom settings", __FUNCTION__
, m_persistedConfiguration
.iFirmwareVersion
);
476 // settings can only be persisted with firmware v2+
480 if (m_bSettingsRetrieved
)
484 bReturn
&= RequestSettingAutoEnabled();
485 bReturn
&= RequestSettingCECVersion();
486 bReturn
&= RequestSettingDefaultLogicalAddress();
487 bReturn
&= RequestSettingDeviceType();
488 bReturn
&= RequestSettingLogicalAddressMask();
489 bReturn
&= RequestSettingOSDName();
490 bReturn
&= RequestSettingPhysicalAddress();
492 // don't read the following settings:
493 // - auto enabled (always enabled)
494 // - default logical address (autodetected)
495 // - logical address mask (autodetected)
496 // - CEC version (1.3a)
498 // TODO to be added to the firmware:
499 // - base device (4 bits)
500 // - HDMI port number (4 bits)
501 // - TV vendor id (12 bits)
502 // - wake devices (8 bits)
503 // - standby devices (8 bits)
504 // - use TV menu language (1 bit)
505 // - activate source (1 bit)
506 // - power off screensaver (1 bit)
507 // - power off on standby (1 bit)
508 // - send inactive source (1 bit)
510 m_bSettingsRetrieved
= true;
515 bool CUSBCECAdapterCommands::GetConfiguration(libcec_configuration
&configuration
)
517 // get the settings from the eeprom if needed
518 if (!RequestSettings())
522 configuration
.iFirmwareVersion
= m_persistedConfiguration
.iFirmwareVersion
;
523 configuration
.deviceTypes
= m_persistedConfiguration
.deviceTypes
;
524 configuration
.iPhysicalAddress
= m_persistedConfiguration
.iPhysicalAddress
;
525 snprintf(configuration
.strDeviceName
, 13, "%s", m_persistedConfiguration
.strDeviceName
);
530 bool CUSBCECAdapterCommands::PingAdapter(void)
532 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "sending ping");
534 CCECAdapterMessage params
;
535 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_PING
, params
);
536 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
541 bool CUSBCECAdapterCommands::SetAckMask(uint16_t iMask
)
543 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting ackmask to %2x", iMask
);
545 CCECAdapterMessage params
;
546 params
.PushEscaped(iMask
>> 8);
547 params
.PushEscaped((uint8_t)iMask
);
548 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_ACK_MASK
, params
);
549 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
554 bool CUSBCECAdapterCommands::StartBootloader(void)
556 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
558 CCECAdapterMessage params
;
559 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_START_BOOTLOADER
, params
);
560 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
565 bool CUSBCECAdapterCommands::SetLineTimeout(uint8_t iTimeout
)
567 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting the line timeout to %d", iTimeout
);
568 CCECAdapterMessage params
;
569 params
.PushEscaped(iTimeout
);
570 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_TRANSMIT_IDLETIME
, params
);
571 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
576 bool CUSBCECAdapterCommands::SetControlledMode(bool controlled
)
578 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "turning controlled mode %s", controlled
? "on" : "off");
580 CCECAdapterMessage params
;
581 params
.PushEscaped(controlled
? 1 : 0);
582 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_CONTROLLED
, params
);
583 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;