-
- for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
- {
- delete m_busDevices[iPtr];
- m_busDevices[iPtr] = NULL;
- }
-}
-
-void CCECProcessor::Close(void)
-{
- StopThread(false);
- SetInitialised(false);
- StopThread();
-
- bool bClose(false);
- {
- CLockObject lock(m_mutex);
- bClose = m_bConnectionOpened;
- m_bConnectionOpened = false;
- }
-
- if (bClose && m_communication)
- {
- delete m_communication;
- m_communication = NULL;
- }
-}
-
-bool CCECProcessor::OpenConnection(const char *strPort, uint16_t iBaudRate, uint32_t iTimeoutMs, bool bStartListening /* = true */)
-{
- bool bReturn(false);
- Close();
-
- {
- CLockObject lock(m_mutex);
- if (m_bConnectionOpened)
- {
- CLibCEC::AddLog(CEC_LOG_ERROR, "connection already opened");
- return false;
- }
- m_communication = new CUSBCECAdapterCommunication(this, strPort, iBaudRate);
- m_bConnectionOpened = (m_communication != NULL);
- }
-
- CTimeout timeout(iTimeoutMs > 0 ? iTimeoutMs : CEC_DEFAULT_TRANSMIT_WAIT);
-
- /* open a new connection */
- unsigned iConnectTry(0);
- while (timeout.TimeLeft() > 0 && (bReturn = m_communication->Open((timeout.TimeLeft() / CEC_CONNECT_TRIES), false, bStartListening)) == false)
- {
- CLibCEC::AddLog(CEC_LOG_ERROR, "could not open a connection (try %d)", ++iConnectTry);
- m_communication->Close();
- CEvent::Sleep(1000);
- }
-
- if (bReturn)
- {
- m_configuration.iFirmwareVersion = m_communication->GetFirmwareVersion();
- uint32_t iBuildDate = m_communication->GetFirmwareBuildDate();
- CStdString strLog;
- strLog.Format("connected to the CEC adapter. libCEC version = %s, client version = %s, firmware version = %d", ToString((cec_server_version)m_configuration.serverVersion), ToString((cec_client_version)m_configuration.clientVersion), m_configuration.iFirmwareVersion);
- if (iBuildDate > 0)
- {
- time_t buildTime = (time_t)iBuildDate;
- strLog.AppendFormat(", firmware build date: %s +0000", asctime(gmtime(&buildTime)));
- strLog = strLog.Left(strLog.length() - 1); // strip \n added by asctime
- }
- CLibCEC::AddLog(CEC_LOG_NOTICE, strLog);
- }
-
- if (m_configuration.bGetSettingsFromROM == 1)
- {
- libcec_configuration config;
- config.Clear();
- m_communication->GetConfiguration(&config);
-
- CLockObject lock(m_mutex);
- if (!config.deviceTypes.IsEmpty())
- m_configuration.deviceTypes = config.deviceTypes;
- if (config.iPhysicalAddress > 0)
- m_configuration.iPhysicalAddress = config.iPhysicalAddress;
- snprintf(m_configuration.strDeviceName, 13, "%s", config.strDeviceName);
- }
-
- return bReturn;
-}
-
-bool CCECProcessor::IsInitialised(void)
-{
- CLockObject lock(m_threadMutex);
- return m_bInitialised;
-}
-
-void CCECProcessor::SetInitialised(bool bSetTo /* = true */)
-{
- CLockObject lock(m_mutex);
- m_bInitialised = bSetTo;
-}
-
-bool CCECProcessor::Initialise(void)
-{
- bool bReturn(false);
- {
- CLockObject lock(m_mutex);
- if (!m_configuration.logicalAddresses.IsEmpty())
- m_configuration.logicalAddresses.Clear();
-
- if (!FindLogicalAddresses())
- {
- CLibCEC::AddLog(CEC_LOG_ERROR, "could not detect our logical addresses");
- return bReturn;
- }
-
- /* only set our OSD name for the primary device */
- m_busDevices[m_configuration.logicalAddresses.primary]->m_strDeviceName = m_configuration.strDeviceName;
-
- /* make the primary device the active source if the option is set */
- if (m_configuration.bActivateSource == 1)
- m_busDevices[m_configuration.logicalAddresses.primary]->m_bActiveSource = true;
-
- /* set the default menu language for devices we control */
- cec_menu_language language;
- language.device = m_configuration.logicalAddresses.primary;
- memcpy(language.language, m_configuration.strDeviceLanguage, 3);
- language.language[3] = 0;
-
- for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
- {
- if (m_configuration.logicalAddresses[iPtr])
- {
- language.device = (cec_logical_address) iPtr;
- m_busDevices[iPtr]->SetMenuLanguage(language);
- }
- }
- }
-
- /* get the vendor id from the TV, so we are using the correct handler */
- m_busDevices[CECDEVICE_TV]->GetVendorId();
-
- if (m_configuration.iPhysicalAddress != 0)
- {
- CLibCEC::AddLog(CEC_LOG_NOTICE, "setting the physical address to %4x", m_configuration.iPhysicalAddress);
- m_busDevices[m_configuration.logicalAddresses.primary]->m_iPhysicalAddress = m_configuration.iPhysicalAddress;
- if ((bReturn = m_busDevices[m_configuration.logicalAddresses.primary]->TransmitPhysicalAddress()) == false)
- CLibCEC::AddLog(CEC_LOG_ERROR, "unable to set the physical address to %4x", m_configuration.iPhysicalAddress);
- }
- else if (m_configuration.iPhysicalAddress == 0 && (bReturn = SetHDMIPort(m_configuration.baseDevice, m_configuration.iHDMIPort, true)) == false)
- CLibCEC::AddLog(CEC_LOG_ERROR, "unable to set HDMI port %d on %s (%x)", m_configuration.iHDMIPort, ToString(m_configuration.baseDevice), (uint8_t)m_configuration.baseDevice);
-
- if (bReturn && m_configuration.bActivateSource == 1)
- m_busDevices[m_configuration.logicalAddresses.primary]->ActivateSource();
-
- SetInitialised(bReturn);
- if (bReturn)
- CLibCEC::ConfigurationChanged(m_configuration);
-
- return bReturn;
-}
-
-bool CCECProcessor::Start(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */)
-{
- bool bReturn(false);
-
- {
- CLockObject lock(m_mutex);
- if (!OpenConnection(strPort, iBaudRate, iTimeoutMs))
- return bReturn;
-
- /* create the processor thread */
- if (!CreateThread())
- {
- CLibCEC::AddLog(CEC_LOG_ERROR, "could not create a processor thread");
- return bReturn;
- }
- }
-
- if ((bReturn = Initialise()) == false)
- {
- CLibCEC::AddLog(CEC_LOG_ERROR, "could not create a processor thread");
- StopThread(true);
- }
- else
- {
- CLibCEC::AddLog(CEC_LOG_DEBUG, "processor thread started");
- }
-
- return bReturn;
-}
-
-bool CCECProcessor::TryLogicalAddress(cec_logical_address address)
-{
- if (m_busDevices[address]->TryLogicalAddress())
- {
- m_configuration.logicalAddresses.Set(address);
- return true;
- }
-
- return false;
-}
-
-bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
-{
- CLibCEC::AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'recording device'");
- return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1) ||
- TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2) ||
- TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3);
-}
-
-bool CCECProcessor::FindLogicalAddressTuner(void)
-{
- CLibCEC::AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'tuner'");
- return TryLogicalAddress(CECDEVICE_TUNER1) ||
- TryLogicalAddress(CECDEVICE_TUNER2) ||
- TryLogicalAddress(CECDEVICE_TUNER3) ||
- TryLogicalAddress(CECDEVICE_TUNER4);
-}
-
-bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
-{
- CLibCEC::AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'playback device'");
- return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1) ||
- TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2) ||
- TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3);