CRPiCECAdapterCommunication::~CRPiCECAdapterCommunication(void)
{
delete(m_queue);
+ UnregisterLogicalAddress();
Close();
+ vc_cec_set_passive(false);
}
const char *ToString(const VC_CEC_ERROR_T error)
{
m_bLogicalAddressChanged = true;
m_logicalAddress = (cec_logical_address)(p0 & 0xF);
+ m_bLogicalAddressRegistered = true;
LIB_CEC->AddLog(CEC_LOG_DEBUG, "logical address changed to %s (%x)", LIB_CEC->ToString(m_logicalAddress), m_logicalAddress);
}
else
break;
case VC_CEC_LOGICAL_ADDR_LOST:
{
+ LIB_CEC->AddLog(CEC_LOG_DEBUG, "logical %s (%x) address lost", LIB_CEC->ToString(m_logicalAddress), m_logicalAddress);
// the logical address was taken by another device
- cec_logical_address previousAddress = m_logicalAddress == CECDEVICE_BROADCAST ? m_previousLogicalAddress : m_logicalAddress;
+ cec_logical_address previousAddress = m_logicalAddress == CECDEVICE_FREEUSE ? m_previousLogicalAddress : m_logicalAddress;
m_logicalAddress = CECDEVICE_UNKNOWN;
// notify libCEC that we lost our LA when the connection was initialised
vc_cec_register_callback(rpi_cec_callback, (void*)this);
vc_tv_register_callback(rpi_tv_callback, (void*)this);
- // release previous LA
- vc_cec_release_logical_address();
- if (!m_logicalAddressCondition.Wait(m_mutex, m_bLogicalAddressChanged, iTimeoutMs))
- {
- LIB_CEC->AddLog(CEC_LOG_ERROR, "failed to release the previous LA");
- return false;
- }
-
// register LA "freeuse"
- if (RegisterLogicalAddress(CECDEVICE_FREEUSE))
+ if (RegisterLogicalAddress(CECDEVICE_FREEUSE, iTimeoutMs))
{
LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - vc_cec initialised", __FUNCTION__);
CLockObject lock(m_mutex);
m_bInitialised = true;
}
else
+ {
LIB_CEC->AddLog(CEC_LOG_ERROR, "%s - vc_cec could not be initialised", __FUNCTION__);
+ return false;
+ }
}
return true;
void CRPiCECAdapterCommunication::Close(void)
{
- {
- CLockObject lock(m_mutex);
- if (m_bInitialised)
- m_bInitialised = false;
- else
- return;
- }
- if (m_bInitialised)
+ if (m_bInitialised) {
vc_tv_unregister_callback(rpi_tv_callback);
-
- UnregisterLogicalAddress();
-
- // disable passive mode
- vc_cec_set_passive(false);
+ m_bInitialised = false;
+ }
if (!g_bHostInited)
{
return strError;
}
-cec_adapter_message_state CRPiCECAdapterCommunication::Write(const cec_command &data, bool &UNUSED(bRetry), uint8_t UNUSED(iLineTimeout), bool bIsReply)
+cec_adapter_message_state CRPiCECAdapterCommunication::Write(const cec_command &data, bool &bRetry, uint8_t iLineTimeout, bool bIsReply)
{
- // ensure that the source LA is registered
- if (!RegisterLogicalAddress(data.initiator))
- {
- LIB_CEC->AddLog(CEC_LOG_DEBUG, "failed to register logical address %s (%X)", CCECTypeUtils::ToString(data.initiator), data.initiator);
- return (data.initiator == data.destination) ? ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED : ADAPTER_MESSAGE_STATE_ERROR;
- }
-
- if (!m_queue->Write(data, bIsReply))
- {
- if (!data.opcode_set)
- {
- return ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED;
- }
-
- return ADAPTER_MESSAGE_STATE_SENT;
- }
-
- return ADAPTER_MESSAGE_STATE_SENT_ACKED;
+ VC_CEC_ERROR_T vcAnswer;
+ uint32_t iTimeout = (data.transmit_timeout ? data.transmit_timeout : iLineTimeout*1000);
+ cec_adapter_message_state rc = m_queue->Write(data, bRetry, iTimeout, bIsReply, vcAnswer);
+#ifdef CEC_DEBUGGING
+ LIB_CEC->AddLog(CEC_LOG_DEBUG, "sending data: result %s", ToString(vcAnswer));
+#endif
+ return rc;
}
uint16_t CRPiCECAdapterCommunication::GetFirmwareVersion(void)
cec_logical_address CRPiCECAdapterCommunication::GetLogicalAddress(void)
{
- {
- CLockObject lock(m_mutex);
- if (m_logicalAddress != CECDEVICE_UNKNOWN)
- return m_logicalAddress;
- }
+ CLockObject lock(m_mutex);
- CEC_AllDevices_T address;
- return (vc_cec_get_logical_address(&address) == VCHIQ_SUCCESS) ?
- (cec_logical_address)address : CECDEVICE_UNKNOWN;
+ return m_logicalAddress;
}
bool CRPiCECAdapterCommunication::UnregisterLogicalAddress(void)
{
CLockObject lock(m_mutex);
- if (m_logicalAddress == CECDEVICE_UNKNOWN ||
- m_logicalAddress == CECDEVICE_BROADCAST)
+ if (!m_bInitialised)
return true;
LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - releasing previous logical address", __FUNCTION__);
return m_logicalAddressCondition.Wait(m_mutex, m_bLogicalAddressChanged);
}
-bool CRPiCECAdapterCommunication::RegisterLogicalAddress(const cec_logical_address address)
+bool CRPiCECAdapterCommunication::RegisterLogicalAddress(const cec_logical_address address, uint32_t iTimeoutMs)
{
{
CLockObject lock(m_mutex);
- if (m_logicalAddress == address)
+ if ((m_logicalAddress == address) && m_bLogicalAddressRegistered)
return true;
}
- if (!UnregisterLogicalAddress())
- return false;
-
- LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - registering address %x", __FUNCTION__, address);
-
- CLockObject lock(m_mutex);
m_bLogicalAddressChanged = false;
- vc_cec_poll_address((CEC_AllDevices_T)address);
// register the new LA
int iRetval = vc_cec_set_logical_address((CEC_AllDevices_T)address, (CEC_DEVICE_TYPE_T)CCECTypeUtils::GetType(address), CEC_VENDOR_ID_BROADCOM);
if (iRetval != VCHIQ_SUCCESS)
{
LIB_CEC->AddLog(CEC_LOG_ERROR, "%s - vc_cec_set_logical_address(%X) returned %s (%d)", __FUNCTION__, address, ToString((VC_CEC_ERROR_T)iRetval), iRetval);
- return false;
+ UnregisterLogicalAddress();
}
-
- if (m_logicalAddressCondition.Wait(m_mutex, m_bLogicalAddressChanged))
+ else if (m_logicalAddressCondition.Wait(m_mutex, m_bLogicalAddressChanged, iTimeoutMs))
{
- m_bLogicalAddressRegistered = true;
return true;
}
return false;
cec_logical_addresses CRPiCECAdapterCommunication::GetLogicalAddresses(void)
{
cec_logical_addresses addresses; addresses.Clear();
- cec_logical_address current = GetLogicalAddress();
- if (current != CECDEVICE_UNKNOWN)
- addresses.Set(current);
+ if (m_bLogicalAddressRegistered)
+ addresses.primary = GetLogicalAddress();
return addresses;
}
bool CRPiCECAdapterMessageQueueEntry::MessageReceived(cec_opcode opcode, cec_logical_address initiator, cec_logical_address destination, uint32_t response)
{
- if ((!m_command.opcode_set || m_command.opcode == opcode) &&
- m_command.initiator == initiator &&
- m_command.destination == destination)
+ if ((m_command.opcode_set && m_command.opcode == opcode &&
+ m_command.initiator == initiator &&
+ m_command.destination == destination)
+ ||
+ (!m_command.opcode_set &&
+ m_command.destination == destination))
{
CLockObject lock(m_mutex);
m_retval = response;
CLockObject lock(m_mutex);
bReturn = m_bSucceeded ? true : m_condition.Wait(m_mutex, m_bSucceeded, iTimeout);
m_bWaiting = false;
-
- if (bReturn)
- bReturn = m_retval == VCHIQ_SUCCESS;
}
return bReturn;
}
LIB_CEC->AddLog(CEC_LOG_WARNING, "unhandled response received: opcode=%x initiator=%x destination=%x response=%x", (int)opcode, (int)initiator, (int)destination, response);
}
-bool CRPiCECAdapterMessageQueue::Write(const cec_command &command, bool bIsReply)
+uint32_t CRPiCECAdapterMessageQueueEntry::Result() const
+{
+ return m_retval;
+}
+
+cec_adapter_message_state CRPiCECAdapterMessageQueue::Write(const cec_command &command, bool &bRetry, uint32_t iLineTimeout, bool bIsReply, VC_CEC_ERROR_T &vcReply)
{
CRPiCECAdapterMessageQueueEntry *entry = new CRPiCECAdapterMessageQueueEntry(this, command);
uint64_t iEntryId(0);
LIB_CEC->AddLog(CEC_LOG_DEBUG, "sending data: %s", strDump.c_str());
#endif
- int iReturn = vc_cec_send_message((uint32_t)command.destination, (uint8_t*)&payload, iLength, bIsReply);
+ int iReturn = vc_cec_send_message((uint32_t)command.destination, command.opcode_set ? (uint8_t*)&payload : NULL, iLength, bIsReply);
#endif
+ bRetry = false;
if (iReturn != VCHIQ_SUCCESS)
{
LIB_CEC->AddLog(CEC_LOG_DEBUG, "sending command '%s' failed (%d)", command.opcode_set ? CCECTypeUtils::ToString(command.opcode) : "POLL", iReturn);
delete (entry);
- return false;
+ return ADAPTER_MESSAGE_STATE_ERROR;
}
- bool bReturn(true);
+ cec_adapter_message_state bReturn(ADAPTER_MESSAGE_STATE_ERROR);
if (entry)
{
- if (!entry->Wait(CEC_DEFAULT_TRANSMIT_WAIT))
+ if (entry->Wait(iLineTimeout))
{
- LIB_CEC->AddLog(CEC_LOG_DEBUG, "command '%s' was not acked by the controller", command.opcode_set ? CCECTypeUtils::ToString(command.opcode) : "POLL");
- bReturn = false;
+ int status = entry->Result();
+
+ if (status == VC_CEC_ERROR_NO_ACK)
+ bReturn = ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED;
+ else if (status == VC_CEC_SUCCESS)
+ bReturn = ADAPTER_MESSAGE_STATE_SENT_ACKED;
+ else
+ bReturn = ADAPTER_MESSAGE_STATE_SENT;
}
+ else
+ {
+ if (command.opcode_set)
+ {
+ bRetry = true;
+ LIB_CEC->AddLog(CEC_LOG_DEBUG, "command '%s' timeout", command.opcode_set ? CCECTypeUtils::ToString(command.opcode) : "POLL");
+ sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT);
+ }
+ bReturn = ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT;
+ }
+
+ vcReply = (VC_CEC_ERROR_T)entry->Result();
CLockObject lock(m_mutex);
m_messages.erase(iEntryId);