From: Lars Op den Kamp Date: Sat, 29 Oct 2011 15:17:35 +0000 (+0200) Subject: cec: moved cec_adapter_message to CCECAdapterMessage X-Git-Tag: upstream/2.2.0~1^2~189 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=220537f268251d7f12ccb0b0058be1eccf1bc214;p=deb_libcec.git cec: moved cec_adapter_message to CCECAdapterMessage --- diff --git a/include/cectypes.h b/include/cectypes.h index 4396755..c08c64d 100644 --- a/include/cectypes.h +++ b/include/cectypes.h @@ -625,26 +625,6 @@ typedef struct cec_datapacket } 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; diff --git a/src/lib/AdapterCommunication.cpp b/src/lib/AdapterCommunication.cpp index 2e330d3..0439100 100644 --- a/src/lib/AdapterCommunication.cpp +++ b/src/lib/AdapterCommunication.cpp @@ -155,7 +155,7 @@ void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen) 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()) @@ -172,13 +172,13 @@ void CAdapterCommunication::WriteNextCommand(void) } } -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); @@ -240,11 +240,10 @@ bool CAdapterCommunication::StartBootloader(void) 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)) @@ -256,19 +255,6 @@ bool CAdapterCommunication::StartBootloader(void) 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()) @@ -278,13 +264,12 @@ bool CAdapterCommunication::SetAckMask(uint16_t iMask) 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)) @@ -302,11 +287,10 @@ bool CAdapterCommunication::PingAdapter(void) 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)) @@ -325,46 +309,3 @@ bool CAdapterCommunication::IsOpen(void) const { 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); - } -} - diff --git a/src/lib/AdapterCommunication.h b/src/lib/AdapterCommunication.h index 32d4a15..c9a8e8c 100644 --- a/src/lib/AdapterCommunication.h +++ b/src/lib/AdapterCommunication.h @@ -38,6 +38,84 @@ 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; @@ -48,8 +126,8 @@ namespace CEC 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; @@ -59,20 +137,18 @@ namespace CEC 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 m_inBuffer; - CecBuffer m_outBuffer; - CMutex m_bufferMutex; - CMutex m_commMutex; - CCondition m_rcvCondition; + CSerialPort * m_port; + CLibCEC * m_controller; + CecBuffer m_inBuffer; + CecBuffer m_outBuffer; + CMutex m_bufferMutex; + CMutex m_commMutex; + CCondition m_rcvCondition; }; }; diff --git a/src/lib/CECProcessor.cpp b/src/lib/CECProcessor.cpp index 08f69cd..e15b234 100644 --- a/src/lib/CECProcessor.cpp +++ b/src/lib/CECProcessor.cpp @@ -88,7 +88,7 @@ void *CCECProcessor::Process(void) m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started"); cec_command command; - cec_adapter_message msg; + CCECAdapterMessage msg; while (!IsStopped()) { @@ -151,10 +151,7 @@ bool CCECProcessor::Transmit(const cec_command &data, bool bWaitForAck /* = true { LogOutput(data); - cec_adapter_message output; - output.clear(); - CAdapterCommunication::FormatAdapterMessage(data, output); - + CCECAdapterMessage output(data); return TransmitFormatted(output, bWaitForAck); } @@ -187,7 +184,7 @@ bool CCECProcessor::SwitchMonitoring(bool bEnable) 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); @@ -240,8 +237,7 @@ bool CCECProcessor::WaitForAck(bool *bError, uint8_t iLength, uint32_t iTimeout 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)) { @@ -315,7 +311,7 @@ bool CCECProcessor::WaitForAck(bool *bError, uint8_t iLength, uint32_t iTimeout return bTransmitSucceeded && !*bError; } -bool CCECProcessor::ParseMessage(cec_adapter_message &msg) +bool CCECProcessor::ParseMessage(CCECAdapterMessage &msg) { bool bEom = false; diff --git a/src/lib/CECProcessor.h b/src/lib/CECProcessor.h index fc37c02..f635cd7 100644 --- a/src/lib/CECProcessor.h +++ b/src/lib/CECProcessor.h @@ -33,6 +33,7 @@ #include #include +#include "AdapterCommunication.h" #include "platform/threads.h" #include "util/buffer.h" #include "util/StdString.h" @@ -70,7 +71,7 @@ namespace CEC 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]; @@ -78,16 +79,16 @@ namespace CEC 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 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 m_frameBuffer; + std::string m_strDeviceName; + CMutex m_mutex; + CAdapterCommunication *m_communication; + CLibCEC *m_controller; + bool m_bMonitor; }; }; diff --git a/src/lib/platform/linux/serialport.cpp b/src/lib/platform/linux/serialport.cpp index d323708..32f7675 100644 --- a/src/lib/platform/linux/serialport.cpp +++ b/src/lib/platform/linux/serialport.cpp @@ -46,7 +46,7 @@ CSerialPort::~CSerialPort() Close(); } -int8_t CSerialPort::Write(const cec_adapter_message &data) +int8_t CSerialPort::Write(const CCECAdapterMessage &data) { fd_set port; diff --git a/src/lib/platform/serialport.h b/src/lib/platform/serialport.h index 4d1be82..ae46543 100644 --- a/src/lib/platform/serialport.h +++ b/src/lib/platform/serialport.h @@ -22,6 +22,7 @@ #include #include #include +#include "../AdapterCommunication.h" #include "../platform/threads.h" #ifndef __WINDOWS__ @@ -46,7 +47,7 @@ namespace CEC 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; } diff --git a/src/lib/platform/windows/serialport.cpp b/src/lib/platform/windows/serialport.cpp index de2a9a3..d8f0abe 100644 --- a/src/lib/platform/windows/serialport.cpp +++ b/src/lib/platform/windows/serialport.cpp @@ -166,7 +166,7 @@ void CSerialPort::Close(void) } } -int8_t CSerialPort::Write(const cec_adapter_message &data) +int8_t CSerialPort::Write(const CCECAdapterMessage &data) { CLockObject lock(&m_mutex); DWORD iBytesWritten = 0;