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
),
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 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting firmware build date");
128 cec_datapacket response
= RequestSetting(MSGCODE_GET_BUILDDATE
);
129 if (response
.size
== 4)
130 return (uint32_t)response
[0] << 24 | (uint32_t)response
[1] << 16 | (uint32_t)response
[2] << 8 | (uint32_t)response
[3];
134 bool CUSBCECAdapterCommands::RequestSettingDefaultLogicalAddress(void)
136 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting default logical address setting");
138 cec_datapacket response
= RequestSetting(MSGCODE_GET_DEFAULT_LOGICAL_ADDRESS
);
139 if (response
.size
== 1)
141 m_persistedConfiguration
.logicalAddresses
.primary
= (cec_logical_address
)response
[0];
142 CLibCEC::AddLog(CEC_LOG_DEBUG
, "using persisted logical address setting: '%s'", CLibCEC::GetInstance()->ToString(m_persistedConfiguration
.logicalAddresses
.primary
));
148 bool CUSBCECAdapterCommands::RequestSettingDeviceType(void)
150 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting device type setting");
151 m_persistedConfiguration
.deviceTypes
.Clear();
153 cec_datapacket response
= RequestSetting(MSGCODE_GET_DEVICE_TYPE
);
154 if (response
.size
== 1)
156 m_persistedConfiguration
.deviceTypes
.Add((cec_device_type
)response
[0]);
157 CLibCEC::AddLog(CEC_LOG_DEBUG
, "using persisted device type setting: '%s'", CLibCEC::GetInstance()->ToString((cec_device_type
)response
[0]));
160 CLibCEC::AddLog(CEC_LOG_DEBUG
, "no persisted device type setting");
164 bool CUSBCECAdapterCommands::RequestSettingLogicalAddressMask(void)
166 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting logical address mask setting");
168 cec_datapacket response
= RequestSetting(MSGCODE_GET_LOGICAL_ADDRESS_MASK
);
169 if (response
.size
== 2)
171 m_iSettingLAMask
= ((uint16_t)response
[0] << 8) | ((uint16_t)response
[1]);
172 CLibCEC::AddLog(CEC_LOG_DEBUG
, "using persisted logical address mask setting: '%x'", m_iSettingLAMask
);
178 bool CUSBCECAdapterCommands::RequestSettingOSDName(void)
180 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting OSD name setting");
182 memset(m_persistedConfiguration
.strDeviceName
, 0, 13);
183 cec_datapacket response
= RequestSetting(MSGCODE_GET_OSD_NAME
);
184 if (response
.size
== 0)
186 CLibCEC::AddLog(CEC_LOG_DEBUG
, "no persisted device name setting");
191 for (uint8_t iPtr
= 0; iPtr
< response
.size
&& iPtr
< 13; iPtr
++)
192 buf
[iPtr
] = (char)response
[iPtr
];
193 buf
[response
.size
] = 0;
195 snprintf(m_persistedConfiguration
.strDeviceName
, 13, "%s", buf
);
196 CLibCEC::AddLog(CEC_LOG_DEBUG
, "using persisted device name setting: '%s'", buf
);
200 bool CUSBCECAdapterCommands::RequestSettingPhysicalAddress(void)
202 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting physical address setting");
204 cec_datapacket response
= RequestSetting(MSGCODE_GET_PHYSICAL_ADDRESS
);
205 if (response
.size
== 2)
207 m_persistedConfiguration
.iPhysicalAddress
= ((uint16_t)response
[0] << 8) | ((uint16_t)response
[1]);
208 CLibCEC::AddLog(CEC_LOG_DEBUG
, "using persisted physical address setting: '%4x'", m_persistedConfiguration
.iPhysicalAddress
);
211 CLibCEC::AddLog(CEC_LOG_DEBUG
, "no persisted physical address setting");
215 bool CUSBCECAdapterCommands::SetSettingAutoEnabled(bool enabled
)
219 /* check whether this value was changed */
220 if (m_bSettingAutoEnabled
== enabled
)
222 CLibCEC::AddLog(CEC_LOG_DEBUG
, "autonomous mode setting unchanged (%s)", enabled
? "on" : "off");
226 m_bNeedsWrite
= true;
227 CLibCEC::AddLog(CEC_LOG_DEBUG
, "turning autonomous mode %s", enabled
? "on" : "off");
229 CCECAdapterMessage params
;
230 params
.PushEscaped(enabled
? 1 : 0);
231 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_AUTO_ENABLED
, params
);
232 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
236 m_bSettingAutoEnabled
= enabled
;
241 bool CUSBCECAdapterCommands::SetSettingDeviceType(cec_device_type type
)
245 /* check whether this value was changed */
246 if (m_persistedConfiguration
.deviceTypes
.types
[0] == type
)
248 CLibCEC::AddLog(CEC_LOG_DEBUG
, "device type setting unchanged (%X)", (uint8_t)type
);
252 m_bNeedsWrite
= true;
253 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the device type to %X (previous: %X)", (uint8_t)type
, (uint8_t)m_persistedConfiguration
.deviceTypes
.types
[0]);
255 CCECAdapterMessage params
;
256 params
.PushEscaped((uint8_t)type
);
257 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_DEVICE_TYPE
, params
);
258 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
264 bool CUSBCECAdapterCommands::SetSettingDefaultLogicalAddress(cec_logical_address address
)
268 /* check whether this value was changed */
269 if (m_persistedConfiguration
.logicalAddresses
.primary
== address
)
271 CLibCEC::AddLog(CEC_LOG_DEBUG
, "logical address setting unchanged (%X)", (uint8_t)address
);
275 m_bNeedsWrite
= true;
276 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the default logical address to %X (previous: %X)", (uint8_t)address
, (uint8_t)m_persistedConfiguration
.logicalAddresses
.primary
);
278 CCECAdapterMessage params
;
279 params
.PushEscaped((uint8_t)address
);
280 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS
, params
);
281 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
285 m_persistedConfiguration
.logicalAddresses
.primary
= address
;
290 bool CUSBCECAdapterCommands::SetSettingLogicalAddressMask(uint16_t iMask
)
294 /* check whether this value was changed */
295 if (m_iSettingLAMask
== iMask
)
297 CLibCEC::AddLog(CEC_LOG_DEBUG
, "logical address mask setting unchanged (%2X)", iMask
);
301 m_bNeedsWrite
= true;
302 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the logical address mask to %2X (previous: %2X)", iMask
, m_iSettingLAMask
);
304 CCECAdapterMessage params
;
305 params
.PushEscaped(iMask
>> 8);
306 params
.PushEscaped((uint8_t)iMask
);
307 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_LOGICAL_ADDRESS_MASK
, params
);
308 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
312 m_iSettingLAMask
= iMask
;
317 bool CUSBCECAdapterCommands::SetSettingPhysicalAddress(uint16_t iPhysicalAddress
)
321 /* check whether this value was changed */
322 if (m_persistedConfiguration
.iPhysicalAddress
== iPhysicalAddress
)
324 CLibCEC::AddLog(CEC_LOG_DEBUG
, "physical address setting unchanged (%04X)", iPhysicalAddress
);
328 m_bNeedsWrite
= true;
329 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the physical address to %04X (previous: %04X)", iPhysicalAddress
, m_persistedConfiguration
.iPhysicalAddress
);
331 CCECAdapterMessage params
;
332 params
.PushEscaped(iPhysicalAddress
>> 8);
333 params
.PushEscaped((uint8_t)iPhysicalAddress
);
334 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_PHYSICAL_ADDRESS
, params
);
335 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
339 m_persistedConfiguration
.iPhysicalAddress
= iPhysicalAddress
;
344 bool CUSBCECAdapterCommands::SetSettingCECVersion(cec_version version
)
348 /* check whether this value was changed */
349 if (m_settingCecVersion
== version
)
351 CLibCEC::AddLog(CEC_LOG_DEBUG
, "CEC version setting unchanged (%s)", CLibCEC::GetInstance()->ToString(version
));
355 m_bNeedsWrite
= true;
356 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the CEC version to %s (previous: %s)", CLibCEC::GetInstance()->ToString(version
), CLibCEC::GetInstance()->ToString(m_settingCecVersion
));
358 CCECAdapterMessage params
;
359 params
.PushEscaped((uint8_t)version
);
360 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_HDMI_VERSION
, params
);
361 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
365 m_settingCecVersion
= version
;
370 bool CUSBCECAdapterCommands::SetSettingOSDName(const char *strOSDName
)
374 /* check whether this value was changed */
375 if (!strcmp(m_persistedConfiguration
.strDeviceName
, strOSDName
))
377 CLibCEC::AddLog(CEC_LOG_DEBUG
, "OSD name setting unchanged (%s)", strOSDName
);
381 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the OSD name to %s (previous: %s)", strOSDName
, m_persistedConfiguration
.strDeviceName
);
383 CCECAdapterMessage params
;
384 for (size_t iPtr
= 0; iPtr
< strlen(strOSDName
); iPtr
++)
385 params
.PushEscaped(strOSDName
[iPtr
]);
386 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_OSD_NAME
, params
);
387 bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
391 snprintf(m_persistedConfiguration
.strDeviceName
, 13, "%s", strOSDName
);
396 bool CUSBCECAdapterCommands::WriteEEPROM(void)
401 CLibCEC::AddLog(CEC_LOG_DEBUG
, "writing settings in the EEPROM");
403 CCECAdapterMessage params
;
404 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_WRITE_EEPROM
, params
);
405 m_bNeedsWrite
= !(message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
);
407 return m_bNeedsWrite
;
410 bool CUSBCECAdapterCommands::PersistConfiguration(libcec_configuration
*configuration
)
412 if (m_persistedConfiguration
.iFirmwareVersion
< 2)
415 if (!RequestSettings())
419 bReturn
&= SetSettingAutoEnabled(true);
420 bReturn
&= SetSettingDeviceType(CLibCEC::GetType(configuration
->logicalAddresses
.primary
));
421 bReturn
&= SetSettingDefaultLogicalAddress(configuration
->logicalAddresses
.primary
);
422 bReturn
&= SetSettingLogicalAddressMask(CLibCEC::GetMaskForType(configuration
->logicalAddresses
.primary
));
423 bReturn
&= SetSettingPhysicalAddress(configuration
->iPhysicalAddress
);
424 bReturn
&= SetSettingCECVersion(CEC_VERSION_1_3A
);
425 bReturn
&= SetSettingOSDName(configuration
->strDeviceName
);
426 bReturn
&= WriteEEPROM();
430 bool CUSBCECAdapterCommands::RequestSettings(void)
432 if (m_persistedConfiguration
.iFirmwareVersion
< 2)
434 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - firmware version %d does not have any eeprom settings", __FUNCTION__
, m_persistedConfiguration
.iFirmwareVersion
);
435 // settings can only be persisted with firmware v2+
439 if (m_bSettingsRetrieved
)
443 bReturn
&= RequestSettingAutoEnabled();
444 bReturn
&= RequestSettingCECVersion();
445 bReturn
&= RequestSettingDefaultLogicalAddress();
446 bReturn
&= RequestSettingDeviceType();
447 bReturn
&= RequestSettingLogicalAddressMask();
448 bReturn
&= RequestSettingOSDName();
449 bReturn
&= RequestSettingPhysicalAddress();
451 // don't read the following settings:
452 // - auto enabled (always enabled)
453 // - default logical address (autodetected)
454 // - logical address mask (autodetected)
455 // - CEC version (1.3a)
457 // TODO to be added to the firmware:
458 // - base device (4 bits)
459 // - HDMI port number (4 bits)
460 // - TV vendor id (12 bits)
461 // - wake devices (8 bits)
462 // - standby devices (8 bits)
463 // - use TV menu language (1 bit)
464 // - activate source (1 bit)
465 // - power off screensaver (1 bit)
466 // - power off on standby (1 bit)
467 // - send inactive source (1 bit)
469 m_bSettingsRetrieved
= true;
474 bool CUSBCECAdapterCommands::GetConfiguration(libcec_configuration
*configuration
)
476 // get the settings from the eeprom if needed
477 if (!RequestSettings())
481 configuration
->iFirmwareVersion
= m_persistedConfiguration
.iFirmwareVersion
;
482 configuration
->deviceTypes
= m_persistedConfiguration
.deviceTypes
;
483 configuration
->iPhysicalAddress
= m_persistedConfiguration
.iPhysicalAddress
;
484 snprintf(configuration
->strDeviceName
, 13, "%s", m_persistedConfiguration
.strDeviceName
);
489 bool CUSBCECAdapterCommands::PingAdapter(void)
491 CLibCEC::AddLog(CEC_LOG_DEBUG
, "sending ping");
493 CCECAdapterMessage params
;
494 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_PING
, params
);
495 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
500 bool CUSBCECAdapterCommands::SetAckMask(uint16_t iMask
)
502 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting ackmask to %2x", iMask
);
504 CCECAdapterMessage params
;
505 params
.PushEscaped(iMask
>> 8);
506 params
.PushEscaped((uint8_t)iMask
);
507 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_ACK_MASK
, params
);
508 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
513 bool CUSBCECAdapterCommands::StartBootloader(void)
515 CLibCEC::AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
517 CCECAdapterMessage params
;
518 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_START_BOOTLOADER
, params
);
519 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
524 bool CUSBCECAdapterCommands::SetLineTimeout(uint8_t iTimeout
)
526 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the line timeout to %d", iTimeout
);
527 CCECAdapterMessage params
;
528 params
.PushEscaped(iTimeout
);
529 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_TRANSMIT_IDLETIME
, params
);
530 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
535 bool CUSBCECAdapterCommands::SetControlledMode(bool controlled
)
537 CLibCEC::AddLog(CEC_LOG_DEBUG
, "turning controlled mode %s", controlled
? "on" : "off");
539 CCECAdapterMessage params
;
540 params
.PushEscaped(controlled
? 1 : 0);
541 CCECAdapterMessage
*message
= m_comm
->SendCommand(MSGCODE_SET_CONTROLLED
, params
);
542 bool bReturn
= message
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;