cec: stick some interfaces before the pthread stuff, so we can add other implementati...
[deb_libcec.git] / src / lib / AdapterCommunication.h
1 #pragma once
2 /*
3 * This file is part of the libCEC(R) library.
4 *
5 * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
6 * libCEC(R) is an original work, containing original code.
7 *
8 * libCEC(R) is a trademark of Pulse-Eight Limited.
9 *
10 * This program is dual-licensed; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 *
24 *
25 * Alternatively, you can license this library under a commercial license,
26 * please contact Pulse-Eight Licensing for more information.
27 *
28 * For more information contact:
29 * Pulse-Eight Licensing <license@pulse-eight.com>
30 * http://www.pulse-eight.com/
31 * http://www.pulse-eight.net/
32 */
33
34 #include <cectypes.h>
35 #include "util/buffer.h"
36 #include <string>
37 #include "util/StdString.h"
38
39 namespace CEC
40 {
41 typedef enum cec_adapter_message_state
42 {
43 ADAPTER_MESSAGE_STATE_UNKNOWN = 0,
44 ADAPTER_MESSAGE_STATE_WAITING,
45 ADAPTER_MESSAGE_STATE_SENT,
46 ADAPTER_MESSAGE_STATE_RECEIVED,
47 ADAPTER_MESSAGE_STATE_ERROR
48 } cec_adapter_message_state;
49
50
51 class CCECAdapterMessage
52 {
53 public:
54 CCECAdapterMessage(void) { clear(); }
55 CCECAdapterMessage(const cec_command &command);
56 CCECAdapterMessage &operator =(const CCECAdapterMessage &msg);
57 CStdString ToString(void) const;
58 CStdString MessageCodeAsString(void) const;
59
60 bool empty(void) const { return packet.IsEmpty(); }
61 uint8_t operator[](uint8_t pos) const { return packet[pos]; }
62 uint8_t at(uint8_t pos) const { return packet[pos]; }
63 uint8_t size(void) const { return packet.size; }
64 void clear(void) { state = ADAPTER_MESSAGE_STATE_UNKNOWN; transmit_timeout = 0; packet.Clear(); maxTries = CEC_DEFAULT_TRANSMIT_RETRIES + 1; tries = 0; reply = MSGCODE_NOTHING; }
65 void shift(uint8_t iShiftBy) { packet.Shift(iShiftBy); }
66 void push_back(uint8_t add) { packet.PushBack(add); }
67 cec_adapter_messagecode message(void) const { return packet.size >= 1 ? (cec_adapter_messagecode) (packet.At(0) & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK)) : MSGCODE_NOTHING; }
68 bool eom(void) const { return packet.size >= 1 ? (packet.At(0) & MSGCODE_FRAME_EOM) != 0 : false; }
69 bool ack(void) const { return packet.size >= 1 ? (packet.At(0) & MSGCODE_FRAME_ACK) != 0 : false; }
70 cec_logical_address initiator(void) const { return packet.size >= 2 ? (cec_logical_address) (packet.At(1) >> 4) : CECDEVICE_UNKNOWN; };
71 cec_logical_address destination(void) const { return packet.size >= 2 ? (cec_logical_address) (packet.At(1) & 0xF) : CECDEVICE_UNKNOWN; };
72 bool is_error(void) const;
73 void push_escaped(uint8_t byte);
74 bool needs_retry(void) const { return reply == MSGCODE_NOTHING ||
75 reply == MSGCODE_RECEIVE_FAILED ||
76 reply == MSGCODE_TIMEOUT_ERROR ||
77 reply == MSGCODE_TRANSMIT_FAILED_LINE ||
78 reply == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA ||
79 reply == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE ||
80 reply == MSGCODE_TRANSMIT_LINE_TIMEOUT; }
81
82 uint8_t maxTries;
83 uint8_t tries;
84 cec_adapter_messagecode reply;
85 cec_datapacket packet;
86 cec_adapter_message_state state;
87 int32_t transmit_timeout;
88 CMutex mutex;
89 CCondition condition;
90 };
91
92 class CSerialPort;
93 class CCECProcessor;
94
95 class CAdapterCommunication : private CThread
96 {
97 public:
98 CAdapterCommunication(CCECProcessor *processor);
99 virtual ~CAdapterCommunication();
100
101 bool Open(const char *strPort, uint16_t iBaudRate = 38400, uint32_t iTimeoutMs = 10000);
102 bool Read(CCECAdapterMessage &msg, uint32_t iTimeout = 1000);
103 bool Write(CCECAdapterMessage *data);
104 bool PingAdapter(void);
105 void Close(void);
106 bool IsOpen(void) const;
107 std::string GetError(void) const;
108
109 void *Process(void);
110
111 bool SetLineTimeout(uint8_t iTimeout);
112 bool StartBootloader(void);
113
114 private:
115 void SendMessageToAdapter(CCECAdapterMessage *msg);
116 void WriteNextCommand(void);
117 void AddData(uint8_t *data, uint8_t iLen);
118 bool ReadFromDevice(uint32_t iTimeout);
119
120 CSerialPort * m_port;
121 CCECProcessor * m_processor;
122 CecBuffer<uint8_t> m_inBuffer;
123 CecBuffer<CCECAdapterMessage *> m_outBuffer;
124 CMutex m_mutex;
125 CCondition m_rcvCondition;
126 CCondition m_startCondition;
127 uint8_t m_iLineTimeout;
128 };
129 };