#define CEC_POWER_STATE_REFRESH_TIME 30000
#define CEC_FW_VERSION_UNKNOWN 0xFFFF
#define CEC_CONNECT_TRIES 3
+#define CEC_PHYSICAL_ADDRESS_TV 0
+#define CEC_MAX_PHYSICAL_ADDRESS 0xFFFE
+#define CEC_INVALID_PHYSICAL_ADDRESS 0xFFFF
#define CEC_DEFAULT_SETTING_USE_TV_MENU_LANGUAGE 1
#define CEC_DEFAULT_SETTING_ACTIVATE_SOURCE 1
#define CEC_DEFAULT_TRANSMIT_WAIT 2000
#define CEC_DEFAULT_TRANSMIT_RETRIES 1
+#define CEC_DEFAULT_CONNECT_TIMEOUT 10000
+#define CEC_DEFAULT_CONNECT_RETRY_WAIT 1000
+#define CEC_SERIAL_DEFAULT_BAUDRATE 38400
+#define CEC_CLEAR_INPUT_DEFAULT_WAIT 1000
+
#define CEC_MIN_LIB_VERSION 1
#define CEC_LIB_VERSION_MAJOR 1
#define CEC_LIB_VERSION_MINOR 6
uint16_t FindPhysicalAddress(void)
{
PrintToStdOut("=== Physical Address Configuration ===\n");
- uint16_t iAddress(0xFFFF);
+ uint16_t iAddress(CEC_INVALID_PHYSICAL_ADDRESS);
PrintToStdOut("Do you want to let libCEC try to autodetect the address (y/n)?");
string input;
else
{
iAddress = g_parser->GetDevicePhysicalAddress(g_primaryAddress);
- if (iAddress == 0 || iAddress == 0xFFFF)
+ if (iAddress == 0 || iAddress == CEC_INVALID_PHYSICAL_ADDRESS)
PrintToStdOut("Failed. Please enter the address manually, or restart this wizard and use different settings.");
}
}
- if (iAddress == 0 || iAddress == 0xFFFF)
+ if (iAddress == 0 || iAddress == CEC_INVALID_PHYSICAL_ADDRESS)
{
- PrintToStdOut("Please enter the physical address (0000 - FFFF), followed by <enter>.");
+ PrintToStdOut("Please enter the physical address (0001 - FFFE), followed by <enter>.");
getline(cin, input);
cin.clear();
int iAddressTmp;
if (sscanf(input.c_str(), "%x", &iAddressTmp) == 1)
{
- if (iAddressTmp < 0 || iAddressTmp > 0xFFFF)
- iAddressTmp = 0xFFFF;
+ if (iAddressTmp <= CEC_PHYSICAL_ADDRESS_TV || iAddressTmp > CEC_MAX_PHYSICAL_ADDRESS)
+ iAddressTmp = CEC_INVALID_PHYSICAL_ADDRESS;
iAddress = (uint16_t)iAddressTmp;
}
}
using namespace std;
using namespace PLATFORM;
+#define CEC_PROCESSOR_SIGNAL_WAIT_TIME 1000
+
CCECProcessor::CCECProcessor(CLibCEC *controller, libcec_configuration *configuration) :
m_bConnectionOpened(false),
m_bInitialised(false),
switch(iPtr)
{
case CECDEVICE_AUDIOSYSTEM:
- m_busDevices[iPtr] = new CCECAudioSystem(this, (cec_logical_address) iPtr, 0xFFFF);
+ m_busDevices[iPtr] = new CCECAudioSystem(this, (cec_logical_address) iPtr, CEC_INVALID_PHYSICAL_ADDRESS);
break;
case CECDEVICE_PLAYBACKDEVICE1:
case CECDEVICE_PLAYBACKDEVICE2:
case CECDEVICE_PLAYBACKDEVICE3:
- m_busDevices[iPtr] = new CCECPlaybackDevice(this, (cec_logical_address) iPtr, 0xFFFF);
+ m_busDevices[iPtr] = new CCECPlaybackDevice(this, (cec_logical_address) iPtr, CEC_INVALID_PHYSICAL_ADDRESS);
break;
case CECDEVICE_RECORDINGDEVICE1:
case CECDEVICE_RECORDINGDEVICE2:
case CECDEVICE_RECORDINGDEVICE3:
- m_busDevices[iPtr] = new CCECRecordingDevice(this, (cec_logical_address) iPtr, 0xFFFF);
+ m_busDevices[iPtr] = new CCECRecordingDevice(this, (cec_logical_address) iPtr, CEC_INVALID_PHYSICAL_ADDRESS);
break;
case CECDEVICE_TUNER1:
case CECDEVICE_TUNER2:
case CECDEVICE_TUNER3:
case CECDEVICE_TUNER4:
- m_busDevices[iPtr] = new CCECTuner(this, (cec_logical_address) iPtr, 0xFFFF);
+ m_busDevices[iPtr] = new CCECTuner(this, (cec_logical_address) iPtr, CEC_INVALID_PHYSICAL_ADDRESS);
break;
case CECDEVICE_TV:
- m_busDevices[iPtr] = new CCECTV(this, (cec_logical_address) iPtr, 0);
+ m_busDevices[iPtr] = new CCECTV(this, (cec_logical_address) iPtr, CEC_PHYSICAL_ADDRESS_TV);
break;
default:
- m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, 0xFFFF);
+ m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, CEC_INVALID_PHYSICAL_ADDRESS);
break;
}
}
{
CLibCEC::AddLog(CEC_LOG_ERROR, "could not open a connection (try %d)", ++iConnectTry);
m_communication->Close();
- CEvent::Sleep(1000);
+ CEvent::Sleep(CEC_DEFAULT_CONNECT_RETRY_WAIT);
}
if (bReturn)
return bReturn;
}
-bool CCECProcessor::Start(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */)
+bool CCECProcessor::Start(const char *strPort, uint16_t iBaudRate /* = CEC_SERIAL_DEFAULT_BAUDRATE */, uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */)
{
bool bReturn(false);
previousDevice->SetOSDName(ToString(previousDevice->GetLogicalAddress()));
newDevice->SetPhysicalAddress(previousDevice->GetPhysicalAddress());
- previousDevice->SetPhysicalAddress(0xFFFF);
+ previousDevice->SetPhysicalAddress(CEC_INVALID_PHYSICAL_ADDRESS);
newDevice->SetPowerStatus(previousDevice->GetPowerStatus(false));
previousDevice->SetPowerStatus(CEC_POWER_STATUS_UNKNOWN);
while (!IsStopped() && m_communication->IsOpen())
{
- if (m_inBuffer.Pop(command, 500))
+ if (m_inBuffer.Pop(command, CEC_PROCESSOR_SIGNAL_WAIT_TIME))
ParseCommand(command);
if (IsInitialised())
}
m_busDevices[addr]->SetActiveSource();
- if (m_busDevices[addr]->GetPhysicalAddress() != 0xFFFF)
+ if (m_busDevices[addr]->GetPhysicalAddress() != CEC_INVALID_PHYSICAL_ADDRESS)
bReturn = m_busDevices[addr]->ActivateSource();
return bReturn;
iPhysicalAddress = m_busDevices[iBaseDevice]->GetPhysicalAddress();
}
- if (iPhysicalAddress < 0xffff)
+ if (iPhysicalAddress <= CEC_MAX_PHYSICAL_ADDRESS)
{
if (iPhysicalAddress == 0)
iPhysicalAddress += 0x1000 * iPort;
CCECBusDevice *device(NULL);
// invalid PA
- if (iPhysicalAddress == 0xFFFF)
+ if (iPhysicalAddress == CEC_INVALID_PHYSICAL_ADDRESS)
return device;
// check each device until we found a match
{
bool bReturn(false);
IAdapterCommunication *comm = new CUSBCECAdapterCommunication(this, strPort);
- CTimeout timeout(10000);
+ CTimeout timeout(CEC_DEFAULT_CONNECT_TIMEOUT);
int iConnectTry(0);
while (timeout.TimeLeft() > 0 && (bReturn = comm->Open(timeout.TimeLeft() / CEC_CONNECT_TRIES, true)) == false)
{
CLibCEC::AddLog(CEC_LOG_ERROR, "could not open a connection (try %d)", ++iConnectTry);
comm->Close();
- Sleep(500);
+ Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT);
}
if (comm->IsOpen())
{
m_busDevices[iPtr]->GetStatus(true);
}
-bool CCECProcessor::GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs /* = 10000 */)
+bool CCECProcessor::GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */)
{
- if (!OpenConnection(strPort, 38400, iTimeoutMs, false))
+ if (!OpenConnection(strPort, CEC_SERIAL_DEFAULT_BAUDRATE, iTimeoutMs, false))
return false;
config->iFirmwareVersion = m_communication->GetFirmwareVersion();
return bReturn;
}
- bool Pop(cec_command &command, uint16_t iTimeout = 10000)
+ bool Pop(cec_command &command, uint16_t iTimeout)
{
bool bReturn(false);
PLATFORM::CLockObject lock(m_mutex);
CCECProcessor(CLibCEC *controller, libcec_configuration *configuration);
virtual ~CCECProcessor(void);
- bool Start(const char *strPort, uint16_t iBaudRate = 38400, uint32_t iTimeoutMs = 10000);
+ bool Start(const char *strPort, uint16_t iBaudRate = CEC_SERIAL_DEFAULT_BAUDRATE, uint32_t iTimeoutMs = CEC_DEFAULT_CONNECT_TIMEOUT);
void *Process(void);
void Close(void);
void HandlePoll(cec_logical_address initiator, cec_logical_address destination);
bool HandleReceiveFailed(cec_logical_address initiator);
- bool GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs = 10000);
+ bool GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs = CEC_DEFAULT_CONNECT_TIMEOUT);
bool TransmitPendingActiveSourceCommands(void);
delete m_cec;
}
-bool CLibCEC::Open(const char *strPort, uint32_t iTimeoutMs /* = 10000 */)
+bool CLibCEC::Open(const char *strPort, uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */)
{
if (m_cec->IsRunning())
{
return false;
}
- if (!m_cec->Start(strPort, 38400, iTimeoutMs))
+ if (!m_cec->Start(strPort, CEC_SERIAL_DEFAULT_BAUDRATE, iTimeoutMs))
{
AddLog(CEC_LOG_ERROR, "could not start CEC communications");
return false;
bool CLibCEC::SetStreamPath(cec_logical_address iAddress)
{
uint16_t iPhysicalAddress = GetDevicePhysicalAddress(iAddress);
- if (iPhysicalAddress != 0xFFFF)
+ if (iPhysicalAddress != CEC_INVALID_PHYSICAL_ADDRESS)
return SetStreamPath(iPhysicalAddress);
return false;
}
if (CUSBCECAdapterDetection::FindAdapters(deviceList, 1) > 0)
{
CUSBCECAdapterCommunication comm(NULL, deviceList[0].comm);
- CTimeout timeout(10000);
+ CTimeout timeout(CEC_DEFAULT_CONNECT_TIMEOUT);
while (timeout.TimeLeft() > 0 && (bReturn = comm.Open(timeout.TimeLeft() / CEC_CONNECT_TRIES, true)) == false)
{
comm.Close();
}
}
-bool CLibCEC::GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs /* = 10000 */)
+bool CLibCEC::GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */)
{
if (m_cec->IsRunning())
return false;
CLibCEC(libcec_configuration *configuration);
virtual ~CLibCEC(void);
- bool Open(const char *strPort, uint32_t iTimeout = 10000);
+ bool Open(const char *strPort, uint32_t iTimeout = CEC_DEFAULT_CONNECT_TIMEOUT);
void Close(void);
bool EnableCallbacks(void *cbParam, ICECCallbacks *callbacks);
int8_t FindAdapters(cec_adapter *deviceList, uint8_t iBufSize, const char *strDevicePath = NULL);
static uint16_t GetMaskForType(cec_logical_address address);
static uint16_t GetMaskForType(cec_device_type type);
- bool GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs = 10000);
+ bool GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs = CEC_DEFAULT_CONNECT_TIMEOUT);
//@}
static void AddLog(const cec_log_level level, const char *strFormat, ...);
* @param bStartListening Start a listener thread when true. False to just open a connection, read the device info, and close the connection.
* @return True when connected, false otherwise
*/
- virtual bool Open(uint32_t iTimeoutMs = 10000, bool bSkipChecks = false, bool bStartListening = true) = 0;
+ virtual bool Open(uint32_t iTimeoutMs = CEC_DEFAULT_CONNECT_TIMEOUT, bool bSkipChecks = false, bool bStartListening = true) = 0;
/*!
* @brief Close an open connection
#define CEC_ADAPTER_PING_TIMEOUT 15000
-CUSBCECAdapterCommunication::CUSBCECAdapterCommunication(IAdapterCommunicationCallback *callback, const char *strPort, uint16_t iBaudRate /* = 38400 */) :
+CUSBCECAdapterCommunication::CUSBCECAdapterCommunication(IAdapterCommunicationCallback *callback, const char *strPort, uint16_t iBaudRate /* = CEC_SERIAL_DEFAULT_BAUDRATE */) :
IAdapterCommunication(callback),
m_port(NULL),
m_iLineTimeout(0),
delete m_port;
}
-bool CUSBCECAdapterCommunication::Open(uint32_t iTimeoutMs /* = 10000 */, bool bSkipChecks /* = false */, bool bStartListening /* = true */)
+bool CUSBCECAdapterCommunication::Open(uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */, bool bSkipChecks /* = false */, bool bStartListening /* = true */)
{
bool bConnectionOpened(false);
{
}
}
-void CUSBCECAdapterCommunication::ClearInputBytes(uint32_t iTimeout /* = 1000 */)
+void CUSBCECAdapterCommunication::ClearInputBytes(uint32_t iTimeout /* = CEC_CLEAR_INPUT_DEFAULT_WAIT */)
{
CTimeout timeout(iTimeout);
uint8_t buff[1024];
return output;
}
-bool CUSBCECAdapterCommunication::CheckAdapter(uint32_t iTimeoutMs /* = 10000 */)
+bool CUSBCECAdapterCommunication::CheckAdapter(uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */)
{
bool bReturn(false);
CTimeout timeout(iTimeoutMs > 0 ? iTimeoutMs : CEC_DEFAULT_TRANSMIT_WAIT);
{
if (!m_com->PingAdapter())
{
- /* sleep 1 second and retry */
- Sleep(1000);
+ /* sleep and retry */
+ Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT);
++iFailedCounter;
}
else
* @param strPort The name of the com port to use.
* @param iBaudRate The baudrate to use on the com port connection.
*/
- CUSBCECAdapterCommunication(IAdapterCommunicationCallback *callback, const char *strPort, uint16_t iBaudRate = 38400);
+ CUSBCECAdapterCommunication(IAdapterCommunicationCallback *callback, const char *strPort, uint16_t iBaudRate = CEC_SERIAL_DEFAULT_BAUDRATE);
virtual ~CUSBCECAdapterCommunication(void);
/** @name IAdapterCommunication implementation */
///{
- bool Open(uint32_t iTimeoutMs = 10000, bool bSkipChecks = false, bool bStartListening = true);
+ bool Open(uint32_t iTimeoutMs = CEC_DEFAULT_CONNECT_TIMEOUT, bool bSkipChecks = false, bool bStartListening = true);
void Close(void);
bool IsOpen(void);
CStdString GetError(void) const;
* @brief Clear all input bytes.
* @param iTimeout Timeout when anything was received.
*/
- void ClearInputBytes(uint32_t iTimeout = 1000);
+ void ClearInputBytes(uint32_t iTimeout = CEC_CLEAR_INPUT_DEFAULT_WAIT);
/*!
* @brief Change the current CEC line timeout.
* @param iTimeoutMs The timeout after which this fails if no proper data was received.
* @return True when the checks passed, false otherwise.
*/
- bool CheckAdapter(uint32_t iTimeoutMs = 10000);
+ bool CheckAdapter(uint32_t iTimeoutMs = CEC_DEFAULT_CONNECT_TIMEOUT);
/*!
* @brief Handle a poll message inside the adapter message (checks if one is present).
using namespace PLATFORM;
using namespace std;
+#define MESSAGE_QUEUE_SIGNAL_WAIT_TIME 1000
+
CCECAdapterMessageQueueEntry::CCECAdapterMessageQueueEntry(CCECAdapterMessage *message) :
m_message(message),
m_iPacketsLeft(message->IsTranmission() ? message->Size() / 4 : 1),
while (!IsStopped())
{
/* wait for a new message */
- if (m_writeQueue.Pop(message, 1000))
+ if (m_writeQueue.Pop(message, MESSAGE_QUEUE_SIGNAL_WAIT_TIME))
{
/* write this message */
m_com->WriteToDevice(message->m_message);
CCECBusDevice::CCECBusDevice(CCECProcessor *processor, cec_logical_address iLogicalAddress, uint16_t iPhysicalAddress) :
m_type (CEC_DEVICE_TYPE_RESERVED),
m_iPhysicalAddress (iPhysicalAddress),
- m_iStreamPath (0xFFFF),
+ m_iStreamPath (CEC_INVALID_PHYSICAL_ADDRESS),
m_iLogicalAddress (iLogicalAddress),
m_powerStatus (CEC_POWER_STATUS_UNKNOWN),
m_processor (processor),
bool bRequestUpdate(false);
{
CLockObject lock(m_mutex);
- bRequestUpdate = bIsPresent && m_iPhysicalAddress == 0xFFFF;
+ bRequestUpdate = bIsPresent && m_iPhysicalAddress == CEC_INVALID_PHYSICAL_ADDRESS;
}
if (bRequestUpdate)
SetVendorId (CEC_VENDOR_UNKNOWN);
SetMenuState (CEC_MENU_STATE_ACTIVATED);
SetCecVersion (CEC_VERSION_UNKNOWN);
- SetStreamPath (0xFFFF);
+ SetStreamPath (CEC_INVALID_PHYSICAL_ADDRESS);
SetOSDName (ToString(m_iLogicalAddress));
SetInactiveSource();
m_iLastActive = 0;
SetVendorId (CEC_VENDOR_UNKNOWN);
SetMenuState (CEC_MENU_STATE_ACTIVATED);
SetCecVersion (CEC_VERSION_1_3A);
- SetStreamPath (0xFFFF);
+ SetStreamPath (CEC_INVALID_PHYSICAL_ADDRESS);
SetInactiveSource();
m_iLastActive = 0;
m_deviceStatus = newStatus;
}
}
-void CCECBusDevice::SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress /* = 0xFFFF */)
+void CCECBusDevice::SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress /* = CEC_INVALID_PHYSICAL_ADDRESS */)
{
CLockObject lock(m_mutex);
if (iNewAddress != m_iStreamPath)
cec_device_type type;
{
CLockObject lock(m_mutex);
- if (m_iPhysicalAddress == 0xffff)
+ if (m_iPhysicalAddress == CEC_INVALID_PHYSICAL_ADDRESS)
return false;
CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress);
virtual void SetDeviceStatus(const cec_bus_device_status newStatus);
virtual void SetPhysicalAddress(uint16_t iNewAddress);
- virtual void SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress = 0xFFFF);
+ virtual void SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress = CEC_INVALID_PHYSICAL_ADDRESS);
virtual void SetCecVersion(const cec_version newVersion);
virtual void SetMenuLanguage(const cec_menu_language &menuLanguage);
virtual void SetOSDName(CStdString strName);
if (GetWord(arguments, strAddress))
{
sscanf(strAddress.c_str(), "%x", &iAddress);
- if (iAddress >= 0 && iAddress <= 0xFFFF)
+ if (iAddress >= 0 && iAddress <= CEC_INVALID_PHYSICAL_ADDRESS)
parser->SetStreamPath((uint16_t)iAddress);
return true;
}