X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Flib%2FAdapterCommunication.h;h=c9a8e8c1155de1132d85c324c702182b5123e538;hb=13929cffe20a14024ab52a2f93b4edd414e129d1;hp=c95f729998d75d5505323125011397229599422b;hpb=8bca69de10349c3cfb7ea3ba4ab5f7ebbb34a740;p=deb_libcec.git diff --git a/src/lib/AdapterCommunication.h b/src/lib/AdapterCommunication.h index c95f729..c9a8e8c 100644 --- a/src/lib/AdapterCommunication.h +++ b/src/lib/AdapterCommunication.h @@ -31,46 +31,124 @@ * http://www.pulse-eight.net/ */ -#include "../../include/CECExports.h" +#include #include "platform/threads.h" +#include "util/buffer.h" +#include 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; - class CAdapterCommunication : CThread + class CAdapterCommunication : private CThread { public: CAdapterCommunication(CLibCEC *controller); virtual ~CAdapterCommunication(); - 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 Open(const char *strPort, uint16_t iBaudRate = 38400, uint32_t iTimeoutMs = 10000); + bool Read(CCECAdapterMessage &msg, uint32_t iTimeout = 1000); + bool Write(const CCECAdapterMessage &frame); bool PingAdapter(void); void Close(void); - bool IsOpen(void) const { return !m_bStop && m_bStarted; } + bool IsOpen(void) const; std::string GetError(void) const; void *Process(void); bool StartBootloader(void); bool SetAckMask(uint16_t iMask); - static void PushEscaped(cec_frame &vec, uint8_t byte); + private: + void WriteNextCommand(void); void AddData(uint8_t *data, uint8_t iLen); - bool ReadFromDevice(uint64_t iTimeout); - - CSerialPort * m_port; - CLibCEC * m_controller; - uint8_t* m_inbuf; - int m_iInbufSize; - int m_iInbufUsed; - bool m_bStarted; - bool m_bStop; - CMutex m_commMutex; - CMutex m_bufferMutex; - CCondition m_condition; + 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; }; };