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 CUSBCECAdapterCommands::CUSBCECAdapterCommands(CUSBCECAdapterCommunication
*comm
) :
42 m_bSettingsRetrieved(false),
43 m_bSettingAutoEnabled(false),
44 m_settingCecVersion(CEC_VERSION_UNKNOWN
),
47 m_iBuildDate(CEC_FW_BUILD_UNKNOWN
)
49 m_persistedConfiguration
.Clear();
52 cec_datapacket
CUSBCECAdapterCommands::RequestSetting(cec_adapter_messagecode msgCode
)
54 cec_datapacket retVal
;
57 CCECAdapterMessage params
;
58 CCECAdapterMessage
*message
= m_comm
->SendCommand(msgCode
, params
);
59 if (message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
)
61 retVal
= message
->response
;
62 retVal
.Shift(2); // shift out start and msgcode
63 retVal
.size
-= 1; // remove end
69 uint16_t CUSBCECAdapterCommands::RequestFirmwareVersion(void)
71 m_persistedConfiguration
.iFirmwareVersion
= CEC_FW_VERSION_UNKNOWN
;
72 unsigned int iFwVersionTry(0);
74 while (m_persistedConfiguration
.iFirmwareVersion
== CEC_FW_VERSION_UNKNOWN
&& iFwVersionTry
++ < 3)
76 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting the firmware version");
77 cec_datapacket response
= RequestSetting(MSGCODE_FIRMWARE_VERSION
);
78 if (response
.size
== 2)
79 m_persistedConfiguration
.iFirmwareVersion
= (response
[0] << 8 | response
[1]);
82 CLibCEC::AddLog(CEC_LOG_WARNING
, "the adapter did not respond with a correct firmware version (try %d)", iFwVersionTry
);
87 if (m_persistedConfiguration
.iFirmwareVersion
== CEC_FW_VERSION_UNKNOWN
)
89 CLibCEC::AddLog(CEC_LOG_DEBUG
, "defaulting to firmware version 1");
90 m_persistedConfiguration
.iFirmwareVersion
= 1;
93 return m_persistedConfiguration
.iFirmwareVersion
;
96 bool CUSBCECAdapterCommands::RequestSettingAutoEnabled(void)
98 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting autonomous mode setting");
100 cec_datapacket response
= RequestSetting(MSGCODE_GET_AUTO_ENABLED
);
101 if (response
.size
== 1)
103 m_bSettingAutoEnabled
= response
[0] == 1;
104 CLibCEC::AddLog(CEC_LOG_DEBUG
, "using persisted autonomous mode setting: '%s'", m_bSettingAutoEnabled
? "enabled" : "disabled");
110 bool CUSBCECAdapterCommands::RequestSettingCECVersion(void)
112 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting CEC version setting");
114 cec_datapacket response
= RequestSetting(MSGCODE_GET_HDMI_VERSION
);
115 if (response
.size
== 1)
117 m_settingCecVersion
= (cec_version
)response
[0];
118 CLibCEC::AddLog(CEC_LOG_DEBUG
, "using persisted CEC version setting: '%s'", CLibCEC::GetInstance()->ToString(m_settingCecVersion
));
124 uint32_t CUSBCECAdapterCommands::RequestBuildDate(void)
126 if (m_iBuildDate
== CEC_FW_BUILD_UNKNOWN
)
128 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting firmware build date");
130 cec_datapacket response
= RequestSetting(MSGCODE_GET_BUILDDATE
);
131 if (response
.size
== 4)
132 m_iBuildDate
= (uint32_t)response
[0] << 24 | (uint32_t)response
[1] << 16 | (uint32_t)response
[2] << 8 | (uint32_t)response
[3];
137 bool CUSBCECAdapterCommands::RequestSettingDefaultLogicalAddress(void)
139 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting default logical address setting");
141 cec_datapacket response
= RequestSetting(MSGCODE_GET_DEFAULT_LOGICAL_ADDRESS
);
142 if (response
.size
== 1)
144 m_persistedConfiguration
.logicalAddresses
.primary
= (cec_logical_address
)response
[0];
145 CLibCEC::AddLog(CEC_LOG_DEBUG
, "using persisted logical address setting: '%s'", CLibCEC::GetInstance()->ToString(m_persistedConfiguration
.logicalAddresses
.primary
));
151 bool CUSBCECAdapterCommands::RequestSettingDeviceType(void)
153 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting device type setting");
154 m_persistedConfiguration
.deviceTypes
.Clear();
156 cec_datapacket response
= RequestSetting(MSGCODE_GET_DEVICE_TYPE
);
157 if (response
.size
== 1)
159 m_persistedConfiguration
.deviceTypes
.Add((cec_device_type
)response
[0]);
160 CLibCEC::AddLog(CEC_LOG_DEBUG
, "using persisted device type setting: '%s'", CLibCEC::GetInstance()->ToString((cec_device_type
)response
[0]));
163 CLibCEC::AddLog(CEC_LOG_DEBUG
, "no persisted device type setting");
167 bool CUSBCECAdapterCommands::RequestSettingLogicalAddressMask(void)
169 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting logical address mask setting");
171 cec_datapacket response
= RequestSetting(MSGCODE_GET_LOGICAL_ADDRESS_MASK
);
172 if (response
.size
== 2)
174 m_iSettingLAMask
= ((uint16_t)response
[0] << 8) | ((uint16_t)response
[1]);
175 CLibCEC::AddLog(CEC_LOG_DEBUG
, "using persisted logical address mask setting: '%x'", m_iSettingLAMask
);
181 bool CUSBCECAdapterCommands::RequestSettingOSDName(void)
183 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting OSD name setting");
185 memset(m_persistedConfiguration
.strDeviceName
, 0, 13);
186 cec_datapacket response
= RequestSetting(MSGCODE_GET_OSD_NAME
);
187 if (response
.size
== 0)
189 CLibCEC::AddLog(CEC_LOG_DEBUG
, "no persisted device name setting");
194 for (uint8_t iPtr
= 0; iPtr
< response
.size
&& iPtr
< 13; iPtr
++)
195 buf
[iPtr
] = (char)response
[iPtr
];
196 buf
[response
.size
] = 0;
198 snprintf(m_persistedConfiguration
.strDeviceName
, 13, "%s", buf
);
199 CLibCEC::AddLog(CEC_LOG_DEBUG
, "using persisted device name setting: '%s'", buf
);
203 bool CUSBCECAdapterCommands::RequestSettingPhysicalAddress(void)
205 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting physical address setting");
207 cec_datapacket response
= RequestSetting(MSGCODE_GET_PHYSICAL_ADDRESS
);
208 if (response
.size
== 2)
210 m_persistedConfiguration
.iPhysicalAddress
= ((uint16_t)response
[0] << 8) | ((uint16_t)response
[1]);
211 CLibCEC::AddLog(CEC_LOG_DEBUG
, "using persisted physical address setting: '%4x'", m_persistedConfiguration
.iPhysicalAddress
);
214 CLibCEC::AddLog(CEC_LOG_DEBUG
, "no persisted physical address setting");
218 bool CUSBCECAdapterCommands::SetSettingAutoEnabled(bool enabled
)
222 /* check whether this value was changed */
223 if (m_bSettingAutoEnabled
== enabled
)
225 CLibCEC::AddLog(CEC_LOG_DEBUG
, "autonomous mode setting unchanged (%s)", enabled
? "on" : "off");
229 m_bNeedsWrite
= true;
230 CLibCEC::AddLog(CEC_LOG_DEBUG
, "turning autonomous mode %s", enabled
? "on" : "off");
232 CCECAdapterMessage params
;
233 params
.PushEscaped(enabled
? 1 : 0);
234 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_AUTO_ENABLED
, params
);
235 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
239 m_bSettingAutoEnabled
= enabled
;
244 bool CUSBCECAdapterCommands::SetSettingDeviceType(cec_device_type type
)
248 /* check whether this value was changed */
249 if (m_persistedConfiguration
.deviceTypes
.types
[0] == type
)
251 CLibCEC::AddLog(CEC_LOG_DEBUG
, "device type setting unchanged (%X)", (uint8_t)type
);
255 m_bNeedsWrite
= true;
256 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the device type to %X (previous: %X)", (uint8_t)type
, (uint8_t)m_persistedConfiguration
.deviceTypes
.types
[0]);
258 CCECAdapterMessage params
;
259 params
.PushEscaped((uint8_t)type
);
260 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_DEVICE_TYPE
, params
);
261 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
267 bool CUSBCECAdapterCommands::SetSettingDefaultLogicalAddress(cec_logical_address address
)
271 /* check whether this value was changed */
272 if (m_persistedConfiguration
.logicalAddresses
.primary
== address
)
274 CLibCEC::AddLog(CEC_LOG_DEBUG
, "logical address setting unchanged (%X)", (uint8_t)address
);
278 m_bNeedsWrite
= true;
279 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the default logical address to %X (previous: %X)", (uint8_t)address
, (uint8_t)m_persistedConfiguration
.logicalAddresses
.primary
);
281 CCECAdapterMessage params
;
282 params
.PushEscaped((uint8_t)address
);
283 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS
, params
);
284 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
288 m_persistedConfiguration
.logicalAddresses
.primary
= address
;
293 bool CUSBCECAdapterCommands::SetSettingLogicalAddressMask(uint16_t iMask
)
297 /* check whether this value was changed */
298 if (m_iSettingLAMask
== iMask
)
300 CLibCEC::AddLog(CEC_LOG_DEBUG
, "logical address mask setting unchanged (%2X)", iMask
);
304 m_bNeedsWrite
= true;
305 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the logical address mask to %2X (previous: %2X)", iMask
, m_iSettingLAMask
);
307 CCECAdapterMessage params
;
308 params
.PushEscaped(iMask
>> 8);
309 params
.PushEscaped((uint8_t)iMask
);
310 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_LOGICAL_ADDRESS_MASK
, params
);
311 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
315 m_iSettingLAMask
= iMask
;
320 bool CUSBCECAdapterCommands::SetSettingPhysicalAddress(uint16_t iPhysicalAddress
)
324 /* check whether this value was changed */
325 if (m_persistedConfiguration
.iPhysicalAddress
== iPhysicalAddress
)
327 CLibCEC::AddLog(CEC_LOG_DEBUG
, "physical address setting unchanged (%04X)", iPhysicalAddress
);
331 m_bNeedsWrite
= true;
332 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the physical address to %04X (previous: %04X)", iPhysicalAddress
, m_persistedConfiguration
.iPhysicalAddress
);
334 CCECAdapterMessage params
;
335 params
.PushEscaped(iPhysicalAddress
>> 8);
336 params
.PushEscaped((uint8_t)iPhysicalAddress
);
337 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_PHYSICAL_ADDRESS
, params
);
338 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
342 m_persistedConfiguration
.iPhysicalAddress
= iPhysicalAddress
;
347 bool CUSBCECAdapterCommands::SetSettingCECVersion(cec_version version
)
351 /* check whether this value was changed */
352 if (m_settingCecVersion
== version
)
354 CLibCEC::AddLog(CEC_LOG_DEBUG
, "CEC version setting unchanged (%s)", CLibCEC::GetInstance()->ToString(version
));
358 m_bNeedsWrite
= true;
359 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the CEC version to %s (previous: %s)", CLibCEC::GetInstance()->ToString(version
), CLibCEC::GetInstance()->ToString(m_settingCecVersion
));
361 CCECAdapterMessage params
;
362 params
.PushEscaped((uint8_t)version
);
363 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_HDMI_VERSION
, params
);
364 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
368 m_settingCecVersion
= version
;
373 bool CUSBCECAdapterCommands::SetSettingOSDName(const char *strOSDName
)
377 /* check whether this value was changed */
378 if (!strcmp(m_persistedConfiguration
.strDeviceName
, strOSDName
))
380 CLibCEC::AddLog(CEC_LOG_DEBUG
, "OSD name setting unchanged (%s)", strOSDName
);
384 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the OSD name to %s (previous: %s)", strOSDName
, m_persistedConfiguration
.strDeviceName
);
386 CCECAdapterMessage params
;
387 for (size_t iPtr
= 0; iPtr
< strlen(strOSDName
); iPtr
++)
388 params
.PushEscaped(strOSDName
[iPtr
]);
389 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_OSD_NAME
, params
);
390 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
394 snprintf(m_persistedConfiguration
.strDeviceName
, 13, "%s", strOSDName
);
399 bool CUSBCECAdapterCommands::WriteEEPROM(void)
404 CLibCEC::AddLog(CEC_LOG_DEBUG
, "writing settings in the EEPROM");
406 CCECAdapterMessage params
;
407 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_WRITE_EEPROM
, params
);
408 m_bNeedsWrite
= !(message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
);
410 return m_bNeedsWrite
;
413 bool CUSBCECAdapterCommands::PersistConfiguration(libcec_configuration
*configuration
)
415 if (m_persistedConfiguration
.iFirmwareVersion
< 2)
418 if (!RequestSettings())
422 bReturn
&= SetSettingAutoEnabled(true);
423 bReturn
&= SetSettingDeviceType(CLibCEC::GetType(configuration
->logicalAddresses
.primary
));
424 bReturn
&= SetSettingDefaultLogicalAddress(configuration
->logicalAddresses
.primary
);
425 bReturn
&= SetSettingLogicalAddressMask(CLibCEC::GetMaskForType(configuration
->logicalAddresses
.primary
));
426 bReturn
&= SetSettingPhysicalAddress(configuration
->iPhysicalAddress
);
427 bReturn
&= SetSettingCECVersion(CEC_VERSION_1_3A
);
428 bReturn
&= SetSettingOSDName(configuration
->strDeviceName
);
429 bReturn
&= WriteEEPROM();
433 bool CUSBCECAdapterCommands::RequestSettings(void)
435 if (m_persistedConfiguration
.iFirmwareVersion
< 2)
437 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - firmware version %d does not have any eeprom settings", __FUNCTION__
, m_persistedConfiguration
.iFirmwareVersion
);
438 // settings can only be persisted with firmware v2+
442 if (m_bSettingsRetrieved
)
446 bReturn
&= RequestSettingAutoEnabled();
447 bReturn
&= RequestSettingCECVersion();
448 bReturn
&= RequestSettingDefaultLogicalAddress();
449 bReturn
&= RequestSettingDeviceType();
450 bReturn
&= RequestSettingLogicalAddressMask();
451 bReturn
&= RequestSettingOSDName();
452 bReturn
&= RequestSettingPhysicalAddress();
454 // don't read the following settings:
455 // - auto enabled (always enabled)
456 // - default logical address (autodetected)
457 // - logical address mask (autodetected)
458 // - CEC version (1.3a)
460 // TODO to be added to the firmware:
461 // - base device (4 bits)
462 // - HDMI port number (4 bits)
463 // - TV vendor id (12 bits)
464 // - wake devices (8 bits)
465 // - standby devices (8 bits)
466 // - use TV menu language (1 bit)
467 // - activate source (1 bit)
468 // - power off screensaver (1 bit)
469 // - power off on standby (1 bit)
470 // - send inactive source (1 bit)
472 m_bSettingsRetrieved
= true;
477 bool CUSBCECAdapterCommands::GetConfiguration(libcec_configuration
*configuration
)
479 // get the settings from the eeprom if needed
480 if (!RequestSettings())
484 configuration
->iFirmwareVersion
= m_persistedConfiguration
.iFirmwareVersion
;
485 configuration
->deviceTypes
= m_persistedConfiguration
.deviceTypes
;
486 configuration
->iPhysicalAddress
= m_persistedConfiguration
.iPhysicalAddress
;
487 snprintf(configuration
->strDeviceName
, 13, "%s", m_persistedConfiguration
.strDeviceName
);
492 bool CUSBCECAdapterCommands::PingAdapter(void)
494 CLibCEC::AddLog(CEC_LOG_DEBUG
, "sending ping");
496 CCECAdapterMessage params
;
497 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_PING
, params
);
498 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
503 bool CUSBCECAdapterCommands::SetAckMask(uint16_t iMask
)
505 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting ackmask to %2x", iMask
);
507 CCECAdapterMessage params
;
508 params
.PushEscaped(iMask
>> 8);
509 params
.PushEscaped((uint8_t)iMask
);
510 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_ACK_MASK
, params
);
511 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
516 bool CUSBCECAdapterCommands::StartBootloader(void)
518 CLibCEC::AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
520 CCECAdapterMessage params
;
521 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_START_BOOTLOADER
, params
);
522 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
527 bool CUSBCECAdapterCommands::SetLineTimeout(uint8_t iTimeout
)
529 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the line timeout to %d", iTimeout
);
530 CCECAdapterMessage params
;
531 params
.PushEscaped(iTimeout
);
532 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_TRANSMIT_IDLETIME
, params
);
533 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
538 bool CUSBCECAdapterCommands::SetControlledMode(bool controlled
)
540 CLibCEC::AddLog(CEC_LOG_DEBUG
, "turning controlled mode %s", controlled
? "on" : "off");
542 CCECAdapterMessage params
;
543 params
.PushEscaped(controlled
? 1 : 0);
544 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_CONTROLLED
, params
);
545 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;