} cec_datapacket;
-typedef struct cec_adapter_message
-{
- cec_datapacket packet;
-
-#ifdef __cplusplus
- bool empty(void) const { return packet.empty(); }
- uint8_t operator[](uint8_t pos) const { return packet[pos]; }
- uint8_t at(uint8_t pos) const { return packet[pos]; }
- uint8_t size(void) const { return packet.size; }
- void clear(void) { packet.clear(); }
- void shift(uint8_t iShiftBy) { packet.shift(iShiftBy); }
- void push_back(uint8_t add) { packet.push_back(add); }
- cec_adapter_messagecode message(void) const { return packet.size >= 1 ? (cec_adapter_messagecode) (packet.at(0) & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK)) : MSGCODE_NOTHING; }
- bool eom(void) const { return packet.size >= 1 ? (packet.at(0) & MSGCODE_FRAME_EOM) != 0 : false; }
- bool ack(void) const { return packet.size >= 1 ? (packet.at(0) & MSGCODE_FRAME_ACK) != 0 : false; }
- cec_logical_address initiator(void) const { return packet.size >= 2 ? (cec_logical_address) (packet.at(1) >> 4) : CECDEVICE_UNKNOWN; };
- cec_logical_address destination(void) const { return packet.size >= 2 ? (cec_logical_address) (packet.at(1) & 0xF) : CECDEVICE_UNKNOWN; };
-#endif
-} cec_adapter_message;
-
typedef struct cec_command
{
cec_logical_address initiator;
void CAdapterCommunication::WriteNextCommand(void)
{
CLockObject lock(&m_commMutex);
- cec_adapter_message msg;
+ CCECAdapterMessage msg;
if (m_outBuffer.Pop(msg))
{
if (m_port->Write(msg) != (int32_t) msg.size())
}
}
-bool CAdapterCommunication::Write(const cec_adapter_message &data)
+bool CAdapterCommunication::Write(const CCECAdapterMessage &data)
{
m_outBuffer.Push(data);
return true;
}
-bool CAdapterCommunication::Read(cec_adapter_message &msg, uint32_t iTimeout)
+bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout)
{
CLockObject lock(&m_bufferMutex);
return false;
m_controller->AddLog(CEC_LOG_DEBUG, "starting the bootloader");
- cec_adapter_message output;
- output.clear();
+ CCECAdapterMessage output;
output.push_back(MSGSTART);
- PushEscaped(output, MSGCODE_START_BOOTLOADER);
+ output.push_escaped(MSGCODE_START_BOOTLOADER);
output.push_back(MSGEND);
if (!Write(output))
return true;
}
-void CAdapterCommunication::PushEscaped(cec_adapter_message &vec, uint8_t byte)
-{
- if (byte >= MSGESC && byte != MSGSTART)
- {
- vec.push_back(MSGESC);
- vec.push_back(byte - ESCOFFSET);
- }
- else
- {
- vec.push_back(byte);
- }
-}
-
bool CAdapterCommunication::SetAckMask(uint16_t iMask)
{
if (!IsRunning())
strLog.Format("setting ackmask to %2x", iMask);
m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
- cec_adapter_message output;
- output.clear();
+ CCECAdapterMessage output;
output.push_back(MSGSTART);
- PushEscaped(output, MSGCODE_SET_ACK_MASK);
- PushEscaped(output, iMask >> 8);
- PushEscaped(output, (uint8_t)iMask);
+ output.push_escaped(MSGCODE_SET_ACK_MASK);
+ output.push_escaped(iMask >> 8);
+ output.push_escaped((uint8_t)iMask);
output.push_back(MSGEND);
if (!Write(output))
return false;
m_controller->AddLog(CEC_LOG_DEBUG, "sending ping");
- cec_adapter_message output;
- output.clear();
+ CCECAdapterMessage output;
output.push_back(MSGSTART);
- PushEscaped(output, MSGCODE_PING);
+ output.push_escaped(MSGCODE_PING);
output.push_back(MSGEND);
if (!Write(output))
{
return !IsStopped() && m_port->IsOpen();
}
-
-void CAdapterCommunication::FormatAdapterMessage(const cec_command &command, cec_adapter_message &packet)
-{
- packet.clear();
-
- //set ack polarity to high when transmitting to the broadcast address
- //set ack polarity low when transmitting to any other address
- packet.push_back(MSGSTART);
- PushEscaped(packet, MSGCODE_TRANSMIT_ACK_POLARITY);
- if (command.destination == CECDEVICE_BROADCAST)
- PushEscaped(packet, CEC_TRUE);
- else
- PushEscaped(packet, CEC_FALSE);
- packet.push_back(MSGEND);
-
- // add source and destination
- packet.push_back(MSGSTART);
- PushEscaped(packet, MSGCODE_TRANSMIT);
- packet.push_back(((uint8_t)command.initiator << 4) + (uint8_t)command.destination);
- packet.push_back(MSGEND);
-
- // add opcode
- packet.push_back(MSGSTART);
- PushEscaped(packet, command.parameters.empty() ? (uint8_t)MSGCODE_TRANSMIT_EOM : (uint8_t)MSGCODE_TRANSMIT);
- packet.push_back((uint8_t) command.opcode);
- packet.push_back(MSGEND);
-
- // add parameters
- for (int8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
- {
- packet.push_back(MSGSTART);
-
- if (iPtr == command.parameters.size - 1)
- PushEscaped(packet, MSGCODE_TRANSMIT_EOM);
- else
- PushEscaped(packet, MSGCODE_TRANSMIT);
-
- PushEscaped(packet, command.parameters[iPtr]);
-
- packet.push_back(MSGEND);
- }
-}
-
namespace CEC
{
+ class CCECAdapterMessage
+ {
+ public:
+ CCECAdapterMessage(void) {}
+ CCECAdapterMessage(const cec_command &command)
+ {
+ clear();
+
+ //set ack polarity to high when transmitting to the broadcast address
+ //set ack polarity low when transmitting to any other address
+ push_back(MSGSTART);
+ push_escaped(MSGCODE_TRANSMIT_ACK_POLARITY);
+ if (command.destination == CECDEVICE_BROADCAST)
+ push_escaped(CEC_TRUE);
+ else
+ push_escaped(CEC_FALSE);
+ push_back(MSGEND);
+
+ // add source and destination
+ push_back(MSGSTART);
+ push_escaped(MSGCODE_TRANSMIT);
+ push_back(((uint8_t)command.initiator << 4) + (uint8_t)command.destination);
+ push_back(MSGEND);
+
+ // add opcode
+ push_back(MSGSTART);
+ push_escaped(command.parameters.empty() ? (uint8_t)MSGCODE_TRANSMIT_EOM : (uint8_t)MSGCODE_TRANSMIT);
+ push_back((uint8_t) command.opcode);
+ push_back(MSGEND);
+
+ // add parameters
+ for (int8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
+ {
+ push_back(MSGSTART);
+
+ if (iPtr == command.parameters.size - 1)
+ push_escaped( MSGCODE_TRANSMIT_EOM);
+ else
+ push_escaped(MSGCODE_TRANSMIT);
+
+ push_escaped(command.parameters[iPtr]);
+
+ push_back(MSGEND);
+ }
+ }
+
+ CCECAdapterMessage &operator =(const CCECAdapterMessage &msg)
+ {
+ packet = msg.packet;
+ return *this;
+ }
+
+ bool empty(void) const { return packet.empty(); }
+ uint8_t operator[](uint8_t pos) const { return packet[pos]; }
+ uint8_t at(uint8_t pos) const { return packet[pos]; }
+ uint8_t size(void) const { return packet.size; }
+ void clear(void) { packet.clear(); }
+ void shift(uint8_t iShiftBy) { packet.shift(iShiftBy); }
+ void push_back(uint8_t add) { packet.push_back(add); }
+ cec_adapter_messagecode message(void) const { return packet.size >= 1 ? (cec_adapter_messagecode) (packet.at(0) & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK)) : MSGCODE_NOTHING; }
+ bool eom(void) const { return packet.size >= 1 ? (packet.at(0) & MSGCODE_FRAME_EOM) != 0 : false; }
+ bool ack(void) const { return packet.size >= 1 ? (packet.at(0) & MSGCODE_FRAME_ACK) != 0 : false; }
+ cec_logical_address initiator(void) const { return packet.size >= 2 ? (cec_logical_address) (packet.at(1) >> 4) : CECDEVICE_UNKNOWN; };
+ cec_logical_address destination(void) const { return packet.size >= 2 ? (cec_logical_address) (packet.at(1) & 0xF) : CECDEVICE_UNKNOWN; };
+ void push_escaped(int16_t byte)
+ {
+ if (byte >= MSGESC && byte != MSGSTART)
+ {
+ push_back(MSGESC);
+ push_back(byte - ESCOFFSET);
+ }
+ else
+ push_back(byte);
+ }
+
+ cec_datapacket packet;
+ };
+
class CSerialPort;
class CLibCEC;
virtual ~CAdapterCommunication();
bool Open(const char *strPort, uint16_t iBaudRate = 38400, uint32_t iTimeoutMs = 10000);
- bool Read(cec_adapter_message &msg, uint32_t iTimeout = 1000);
- bool Write(const cec_adapter_message &frame);
+ bool Read(CCECAdapterMessage &msg, uint32_t iTimeout = 1000);
+ bool Write(const CCECAdapterMessage &frame);
bool PingAdapter(void);
void Close(void);
bool IsOpen(void) const;
bool StartBootloader(void);
bool SetAckMask(uint16_t iMask);
- static void PushEscaped(cec_adapter_message &vec, uint8_t byte);
- static void FormatAdapterMessage(const cec_command &command, cec_adapter_message &packet);
private:
void WriteNextCommand(void);
void AddData(uint8_t *data, uint8_t iLen);
bool ReadFromDevice(uint32_t iTimeout);
- CSerialPort * m_port;
- CLibCEC * m_controller;
- CecBuffer<uint8_t> m_inBuffer;
- CecBuffer<cec_adapter_message> m_outBuffer;
- CMutex m_bufferMutex;
- CMutex m_commMutex;
- CCondition m_rcvCondition;
+ CSerialPort * m_port;
+ CLibCEC * m_controller;
+ CecBuffer<uint8_t> m_inBuffer;
+ CecBuffer<CCECAdapterMessage> m_outBuffer;
+ CMutex m_bufferMutex;
+ CMutex m_commMutex;
+ CCondition m_rcvCondition;
};
};
m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
cec_command command;
- cec_adapter_message msg;
+ CCECAdapterMessage msg;
while (!IsStopped())
{
{
LogOutput(data);
- cec_adapter_message output;
- output.clear();
- CAdapterCommunication::FormatAdapterMessage(data, output);
-
+ CCECAdapterMessage output(data);
return TransmitFormatted(output, bWaitForAck);
}
return m_communication && m_communication->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
}
-bool CCECProcessor::TransmitFormatted(const cec_adapter_message &data, bool bWaitForAck /* = true */)
+bool CCECProcessor::TransmitFormatted(const CCECAdapterMessage &data, bool bWaitForAck /* = true */)
{
bool bReturn(false);
CLockObject lock(&m_mutex);
while (!bTransmitSucceeded && !*bError && (iTimeout == 0 || iNow < iTargetTime))
{
- cec_adapter_message msg;
- msg.clear();
+ CCECAdapterMessage msg;
if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000))
{
return bTransmitSucceeded && !*bError;
}
-bool CCECProcessor::ParseMessage(cec_adapter_message &msg)
+bool CCECProcessor::ParseMessage(CCECAdapterMessage &msg)
{
bool bEom = false;
#include <string>
#include <cectypes.h>
+#include "AdapterCommunication.h"
#include "platform/threads.h"
#include "util/buffer.h"
#include "util/StdString.h"
virtual void AddKey(void);
virtual void AddLog(cec_log_level level, const CStdString &strMessage);
- virtual bool TransmitFormatted(const cec_adapter_message &data, bool bWaitForAck = true);
+ virtual bool TransmitFormatted(const CCECAdapterMessage &data, bool bWaitForAck = true);
virtual void TransmitAbort(cec_logical_address address, cec_opcode opcode, ECecAbortReason reason = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE);
CCECBusDevice *m_busDevices[16];
private:
void LogOutput(const cec_command &data);
bool WaitForAck(bool *bError, uint8_t iLength, uint32_t iTimeout = 1000);
- bool ParseMessage(cec_adapter_message &msg);
+ bool ParseMessage(CCECAdapterMessage &msg);
void ParseCommand(cec_command &command);
- cec_command m_currentframe;
- cec_logical_address m_iLogicalAddress;
- CecBuffer<cec_adapter_message> m_frameBuffer;
- std::string m_strDeviceName;
- CMutex m_mutex;
- CAdapterCommunication *m_communication;
- CLibCEC *m_controller;
- bool m_bMonitor;
+ cec_command m_currentframe;
+ cec_logical_address m_iLogicalAddress;
+ CecBuffer<CCECAdapterMessage> m_frameBuffer;
+ std::string m_strDeviceName;
+ CMutex m_mutex;
+ CAdapterCommunication *m_communication;
+ CLibCEC *m_controller;
+ bool m_bMonitor;
};
};
Close();
}
-int8_t CSerialPort::Write(const cec_adapter_message &data)
+int8_t CSerialPort::Write(const CCECAdapterMessage &data)
{
fd_set port;
#include <cectypes.h>
#include <string>
#include <stdint.h>
+#include "../AdapterCommunication.h"
#include "../platform/threads.h"
#ifndef __WINDOWS__
bool IsOpen();
void Close();
- int8_t Write(const cec_adapter_message &data);
+ int8_t Write(const CCECAdapterMessage &data);
int32_t Read(uint8_t* data, uint32_t len, uint64_t iTimeoutMs = 0);
std::string GetError() { return m_error; }
}
}
-int8_t CSerialPort::Write(const cec_adapter_message &data)
+int8_t CSerialPort::Write(const CCECAdapterMessage &data)
{
CLockObject lock(&m_mutex);
DWORD iBytesWritten = 0;