* @return True when initialised, false otherwise.
*/
#ifdef __cplusplus
-extern DECLSPEC bool cec_init(const char *strDeviceName, CEC::cec_logical_address iLogicalAddress = CEC::CECDEVICE_PLAYBACKDEVICE1, int iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS);
+extern DECLSPEC bool cec_init(const char *strDeviceName, CEC::cec_logical_address iLogicalAddress = CEC::CECDEVICE_PLAYBACKDEVICE1, uint8_t iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS);
#else
-extern DECLSPEC bool cec_init(const char *strDeviceName, cec_logical_address iLogicalAddress = CECDEVICE_PLAYBACKDEVICE1, int iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS);
+extern DECLSPEC bool cec_init(const char *strDeviceName, cec_logical_address iLogicalAddress = CECDEVICE_PLAYBACKDEVICE1, uint8_t iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS);
#endif
/*!
* @param iTimeout Connection timeout in ms.
* @return True when connected, false otherwise.
*/
-extern DECLSPEC bool cec_open(const char *strPort, int iTimeout);
+extern DECLSPEC bool cec_open(const char *strPort, uint64_t iTimeout);
/*!
* @brief Close the connection to the CEC adapter.
/*!
* @see cec_open
*/
- virtual bool Open(const char *strPort, int iTimeoutMs = 10000) = 0;
+ virtual bool Open(const char *strPort, uint64_t iTimeoutMs = 10000) = 0;
/*!
* @see cec_close
};
};
-extern DECLSPEC void * CECCreate(const char *strDeviceName, CEC::cec_logical_address iLogicalAddress = CEC::CECDEVICE_PLAYBACKDEVICE1, int iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS);
+extern DECLSPEC void * CECCreate(const char *strDeviceName, CEC::cec_logical_address iLogicalAddress = CEC::CECDEVICE_PLAYBACKDEVICE1, uint8_t iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS);
#if !defined(DLL_EXPORT)
#if defined(_WIN32) || defined(_WIN64)
/*!
* @see cec_init
*/
-inline CEC::ICECAdapter *LoadLibCec(const char *strName, CEC::cec_logical_address iLogicalAddress = CEC::CECDEVICE_PLAYBACKDEVICE1, int iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS)
+inline CEC::ICECAdapter *LoadLibCec(const char *strName, CEC::cec_logical_address iLogicalAddress = CEC::CECDEVICE_PLAYBACKDEVICE1, uint8_t iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS)
{
typedef void* (__cdecl*_CreateLibCec)(const char *, uint8_t, uint8_t);
_CreateLibCec CreateLibCec;
CreateLibCec = (_CreateLibCec) (GetProcAddress(g_libCEC, "CECCreate"));
if (!CreateLibCec)
return NULL;
- return static_cast< CEC::ICECAdapter* > (CreateLibCec(strName, iLogicalAddress, iPhysicalAddress));
+ return static_cast< CEC::ICECAdapter* > (CreateLibCec(strName, (uint8_t) iLogicalAddress, iPhysicalAddress));
}
/*!
m_port = NULL;
}
-bool CAdapterCommunication::Open(const char *strPort, int iBaudRate /* = 38400 */, int iTimeoutMs /* = 10000 */)
+bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint64_t iTimeoutMs /* = 10000 */)
{
CLockObject lock(&m_commMutex);
if (m_bStarted)
return NULL;
}
-bool CAdapterCommunication::ReadFromDevice(int iTimeout)
+bool CAdapterCommunication::ReadFromDevice(uint64_t iTimeout)
{
uint8_t buff[1024];
CLockObject lock(&m_commMutex);
if (!m_port)
return false;
- int iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout);
+ int32_t iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout);
lock.Leave();
- if (iBytesRead < 0)
+ if (iBytesRead < 0 || iBytesRead > 256)
{
CStdString strError;
strError.Format("error reading from serial port: %s", m_port->GetError().c_str());
return false;
}
else if (iBytesRead > 0)
- AddData(buff, iBytesRead);
+ AddData(buff, (uint8_t) iBytesRead);
return true;
}
-void CAdapterCommunication::AddData(uint8_t *data, int iLen)
+void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen)
{
CLockObject lock(&m_bufferMutex);
if (iLen + m_iInbufUsed > m_iInbufSize)
{
CLockObject lock(&m_commMutex);
- if (m_port->Write(data) != data.size())
+ if (m_port->Write(data) != (int) data.size())
{
CStdString strError;
strError.Format("error writing to serial port: %s", m_port->GetError().c_str());
return true;
}
-bool CAdapterCommunication::Read(cec_frame &msg, int iTimeout)
+bool CAdapterCommunication::Read(cec_frame &msg, uint64_t iTimeout)
{
CLockObject lock(&m_bufferMutex);
CAdapterCommunication(CLibCEC *controller);
virtual ~CAdapterCommunication();
- bool Open(const char *strPort, int iBaudRate = 38400, int iTimeoutMs = 10000);
- bool Read(cec_frame &msg, int iTimeout = 1000);
+ bool Open(const char *strPort, uint16_t iBaudRate = 38400, uint64_t iTimeoutMs = 10000);
+ bool Read(cec_frame &msg, uint64_t iTimeout = 1000);
bool Write(const cec_frame &frame);
bool PingAdapter(void);
void Close(void);
bool SetAckMask(uint16_t iMask);
static void PushEscaped(cec_frame &vec, uint8_t byte);
private:
- void AddData(uint8_t *data, int iLen);
- bool ReadFromDevice(int iTimeout);
+ void AddData(uint8_t *data, uint8_t iLen);
+ bool ReadFromDevice(uint64_t iTimeout);
CSerialPort * m_port;
CLibCEC * m_controller;
m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
cec_frame frame;
frame.push_back(GetSourceDestination(address));
- frame.push_back(CEC_OPCODE_TEXT_VIEW_ON);
+ frame.push_back((uint8_t) CEC_OPCODE_TEXT_VIEW_ON);
return Transmit(frame);
}
m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
cec_frame frame;
frame.push_back(GetSourceDestination(address));
- frame.push_back(CEC_OPCODE_STANDBY);
+ frame.push_back((uint8_t) CEC_OPCODE_STANDBY);
return Transmit(frame);
}
m_controller->AddLog(CEC_LOG_DEBUG, "setting active view");
cec_frame frame;
frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST));
- frame.push_back(CEC_OPCODE_ACTIVE_SOURCE);
+ frame.push_back((uint8_t) CEC_OPCODE_ACTIVE_SOURCE);
frame.push_back((m_physicaladdress >> 8) & 0xFF);
frame.push_back(m_physicaladdress & 0xFF);
return Transmit(frame);
m_controller->AddLog(CEC_LOG_DEBUG, "setting inactive view");
cec_frame frame;
frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST));
- frame.push_back(CEC_OPCODE_INACTIVE_SOURCE);
+ frame.push_back((uint8_t) CEC_OPCODE_INACTIVE_SOURCE);
frame.push_back((m_physicaladdress >> 8) & 0xFF);
frame.push_back(m_physicaladdress & 0xFF);
return Transmit(frame);
m_controller->AddLog(CEC_LOG_DEBUG, "transmitting abort message");
cec_frame frame;
frame.push_back(GetSourceDestination(address));
- frame.push_back(CEC_OPCODE_FEATURE_ABORT);
- frame.push_back(opcode);
- frame.push_back(reason);
+ frame.push_back((uint8_t) CEC_OPCODE_FEATURE_ABORT);
+ frame.push_back((uint8_t) opcode);
+ frame.push_back((uint8_t) reason);
Transmit(frame);
}
cec_frame frame;
m_controller->AddLog(CEC_LOG_NOTICE, "reporting CEC version as 1.3a");
frame.push_back(GetSourceDestination(address));
- frame.push_back(CEC_OPCODE_CEC_VERSION);
+ frame.push_back((uint8_t) CEC_OPCODE_CEC_VERSION);
frame.push_back(CEC_VERSION_1_3A);
Transmit(frame);
}
m_controller->AddLog(CEC_LOG_NOTICE, "reporting \"Off\" power status");
frame.push_back(GetSourceDestination(address));
- frame.push_back(CEC_OPCODE_REPORT_POWER_STATUS);
- frame.push_back(bOn ? CEC_POWER_STATUS_ON : CEC_POWER_STATUS_STANDBY);
+ frame.push_back((uint8_t) CEC_OPCODE_REPORT_POWER_STATUS);
+ frame.push_back(bOn ? (uint8_t) CEC_POWER_STATUS_ON : (uint8_t) CEC_POWER_STATUS_STANDBY);
Transmit(frame);
}
m_controller->AddLog(CEC_LOG_NOTICE, "reporting menu state as inactive");
frame.push_back(GetSourceDestination(address));
- frame.push_back(CEC_OPCODE_MENU_STATUS);
- frame.push_back(bActive ? CEC_MENU_STATE_ACTIVATED : CEC_MENU_STATE_DEACTIVATED);
+ frame.push_back((uint8_t) CEC_OPCODE_MENU_STATUS);
+ frame.push_back(bActive ? (uint8_t) CEC_MENU_STATE_ACTIVATED : (uint8_t) CEC_MENU_STATE_DEACTIVATED);
Transmit(frame);
}
strLog.Format("reporting OSD name as %s", osdname);
m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
frame.push_back(GetSourceDestination(address));
- frame.push_back(CEC_OPCODE_SET_OSD_NAME);
+ frame.push_back((uint8_t) CEC_OPCODE_SET_OSD_NAME);
for (unsigned int i = 0; i < strlen(osdname); i++)
frame.push_back(osdname[i]);
strLog.Format("reporting physical address as %04x", m_physicaladdress);
m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST));
- frame.push_back(CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
+ frame.push_back((uint8_t) CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
frame.push_back((m_physicaladdress >> 8) & 0xFF);
frame.push_back(m_physicaladdress & 0xFF);
frame.push_back(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
cec_frame frame;
m_controller->AddLog(CEC_LOG_NOTICE, "broadcasting active source");
frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST));
- frame.push_back(CEC_OPCODE_ACTIVE_SOURCE);
+ frame.push_back((uint8_t) CEC_OPCODE_ACTIVE_SOURCE);
frame.push_back((m_physicaladdress >> 8) & 0xFF);
frame.push_back(m_physicaladdress & 0xFF);
Transmit(frame);
using namespace std;
using namespace CEC;
-CLibCEC::CLibCEC(const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, int iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */) :
+CLibCEC::CLibCEC(const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint8_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */) :
m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN),
m_buttontime(0)
{
m_comm = NULL;
}
-bool CLibCEC::Open(const char *strPort, int iTimeoutMs /* = 10000 */)
+bool CLibCEC::Open(const char *strPort, uint64_t iTimeoutMs /* = 10000 */)
{
if (!m_comm)
return false;
m_buttontime = GetTimeMs();
}
-DECLSPEC void * CECCreate(const char *strDeviceName, CEC::cec_logical_address iLogicalAddress /*= CEC::CECDEVICE_PLAYBACKDEVICE1 */, int iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
+DECLSPEC void * CECCreate(const char *strDeviceName, CEC::cec_logical_address iLogicalAddress /*= CEC::CECDEVICE_PLAYBACKDEVICE1 */, uint8_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
{
return static_cast< void* > (new CLibCEC(strDeviceName, iLogicalAddress, iPhysicalAddress));
}
* ICECAdapter implementation
*/
//@{
- CLibCEC(const char *strDeviceName, cec_logical_address iLogicalAddress = CECDEVICE_PLAYBACKDEVICE1, int iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS);
+ CLibCEC(const char *strDeviceName, cec_logical_address iLogicalAddress = CECDEVICE_PLAYBACKDEVICE1, uint8_t iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS);
virtual ~CLibCEC(void);
- virtual bool Open(const char *strPort, int iTimeout = 10000);
+ virtual bool Open(const char *strPort, uint64_t iTimeout = 10000);
virtual void Close(void);
virtual int FindAdapters(std::vector<cec_adapter> &deviceList, const char *strDevicePath = NULL);
virtual bool PingAdapter(void);
//@{
ICECAdapter *cec_parser;
-bool cec_init(const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, int iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
+bool cec_init(const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint8_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
{
cec_parser = (ICECAdapter *) CECCreate(strDeviceName, iLogicalAddress, iPhysicalAddress);
return (cec_parser != NULL);
cec_parser = NULL;
}
-bool cec_open(const char *strPort, int iTimeout)
+bool cec_open(const char *strPort, uint64_t iTimeout)
{
if (cec_parser)
return cec_parser->Open(strPort, iTimeout);
//every baudrate I could find is in here in an #ifdef block
//so it should compile on everything
+#include "os-dependent.h"
+
+#ifndef __WINDOWS__
#include <termios.h>
+#endif
namespace CEC
{
static struct sbaudrate
{
- int rate;
- int symbol;
+ int32_t rate;
+ int32_t symbol;
}
baudrates[] =
{
{ -1, -1}
};
- static int IntToBaudrate(int baudrate)
+ static int32_t IntToBaudrate(uint32_t baudrate)
{
for (unsigned int i = 0; i < sizeof(baudrates) / sizeof(CEC::sbaudrate) - 1; i++)
{
- if (baudrates[i].rate == baudrate)
+ if (baudrates[i].rate == (int32_t) baudrate)
return baudrates[i].symbol;
}
*/
#include "os-dependent.h"
-#include "baudrate.h"
#include <string>
#include <vector>
#include <stdint.h>
CSerialPort();
virtual ~CSerialPort();
- bool Open(std::string name, int baudrate, int databits = 8, int stopbits = 1, int parity = PAR_NONE);
+ bool Open(std::string name, uint32_t baudrate, uint8_t databits = 8, uint8_t stopbits = 1, uint8_t parity = PAR_NONE);
bool IsOpen();
void Close();
- int Write(std::vector<uint8_t> data)
+ int32_t Write(std::vector<uint8_t> data)
{
- return Write(&data[0], data.size());
+ return Write(&data[0], (uint32_t) data.size());
}
- int Write(uint8_t* data, int len);
- int Read(uint8_t* data, int len, int iTimeoutMs = -1);
+ int32_t Write(uint8_t* data, uint32_t len);
+ int32_t Read(uint8_t* data, uint32_t len, uint64_t iTimeoutMs = 0);
std::string GetError() { return m_error; }
std::string GetName() { return m_name; }
private:
- bool SetBaudRate(int baudrate);
+ bool SetBaudRate(uint32_t baudrate);
std::string m_error;
std::string m_name;
HANDLE m_handle;
bool m_bIsOpen;
- int m_iBaudrate;
- int m_iDatabits;
- int m_iStopbits;
- int m_iParity;
+ uint32_t m_iBaudrate;
+ uint8_t m_iDatabits;
+ uint8_t m_iStopbits;
+ uint8_t m_iParity;
int64_t m_iTimeout;
CecBuffer<uint8_t> m_buffer;
HANDLE m_ovHandle;
Close();
}
-bool CSerialPort::Open(string name, int baudrate, int databits, int stopbits, int parity)
+bool CSerialPort::Open(string name, uint32_t baudrate, uint8_t databits, uint8_t stopbits, uint8_t parity)
{
+ CLockObject lock(&m_mutex);
m_handle = CreateFile(name.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (m_handle == INVALID_HANDLE_VALUE)
{
void CSerialPort::Close(void)
{
+ CLockObject lock(&m_mutex);
if (m_bIsOpen)
{
CloseHandle(m_handle);
}
}
-int CSerialPort::Write(uint8_t* data, int len)
+int32_t CSerialPort::Write(uint8_t* data, uint32_t len)
{
+ CLockObject lock(&m_mutex);
DWORD iBytesWritten = 0;
if (!m_bIsOpen)
return -1;
return -1;
}
- return (int) iBytesWritten;
+ return iBytesWritten;
}
-int CSerialPort::Read(uint8_t* data, int len, int iTimeoutMs /* = -1 */)
+int32_t CSerialPort::Read(uint8_t* data, uint32_t len, uint64_t iTimeoutMs /* = 0 */)
{
+ CLockObject lock(&m_mutex);
+ int32_t iReturn(-1);
DWORD iBytesRead = 0;
if (m_handle == 0)
{
m_error = "Error while reading from COM port: invalid handle";
- return -1;
+ return iReturn;
}
if(!ReadFile(m_handle, data, len, &iBytesRead, NULL) != 0)
{
m_error = "unable to read from device";
FormatWindowsError(GetLastError(), m_error);
- iBytesRead = -1;
+ iReturn = -1;
+ }
+ else
+ {
+ iReturn = (int32_t) iBytesRead;
}
- return (int) iBytesRead;
+ return iReturn;
}
-bool CSerialPort::SetBaudRate(int baudrate)
+bool CSerialPort::SetBaudRate(uint32_t baudrate)
{
- m_iBaudrate = baudrate;
+ int32_t rate = IntToBaudrate(baudrate);
+ if (rate < 0)
+ m_iBaudrate = baudrate > 0 ? baudrate : 0;
+ else
+ m_iBaudrate = rate;
DCB dcb;
memset(&dcb,0,sizeof(dcb));
return true;
}
-bool CSerialPort::IsOpen() const
+bool CSerialPort::IsOpen()
{
+ CLockObject lock(&m_mutex);
return m_bIsOpen;
}
struct CecBuffer
{
public:
- CecBuffer(int iMaxSize = 100)
+ CecBuffer(unsigned int iMaxSize = 100)
{
m_maxSize = iMaxSize;
}
}
private:
- int m_maxSize;
+ unsigned int m_maxSize;
std::queue<_BType> m_buffer;
CMutex m_mutex;
};
#define CEC_TEST_CLIENT_VERSION 3
-inline bool HexStrToInt(const std::string& data, int& value)
+inline bool HexStrToInt(const std::string& data, uint8_t& value)
{
- return sscanf(data.c_str(), "%x", &value) == 1;
-}
+ int iTmp(0);
+ if (sscanf(data.c_str(), "%x", &iTmp) == 1)
+ {
+ if (iTmp > 256)
+ value = 255;
+ else if (iTmp < 0)
+ value = 0;
+ else
+ value = (uint8_t) iTmp;
+ return true;
+ }
+
+ return false;
+}
//get the first word (separated by whitespace) from string data and place that in word
//then remove that word from string data
if (command == "tx")
{
string strvalue;
- int ivalue;
+ uint8_t ivalue;
vector<uint8_t> bytes;
while (GetWord(input, strvalue) && HexStrToInt(strvalue, ivalue))
bytes.push_back(ivalue);