cec: fixed - don't keep the mutex locked in CCECProcessor::Initialise()
[deb_libcec.git] / src / lib / AdapterCommunication.h
CommitLineData
a8f0bd18
LOK
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
761dcc45 34#include <cectypes.h>
f00ff009
LOK
35#include "platform/os.h"
36
37namespace PLATFORM
38{
39 class CSerialPort;
40}
a8f0bd18
LOK
41
42namespace CEC
43{
0e31a62c
LOK
44 typedef enum cec_adapter_message_state
45 {
46 ADAPTER_MESSAGE_STATE_UNKNOWN = 0,
47 ADAPTER_MESSAGE_STATE_WAITING,
48 ADAPTER_MESSAGE_STATE_SENT,
49 ADAPTER_MESSAGE_STATE_RECEIVED,
50 ADAPTER_MESSAGE_STATE_ERROR
51 } cec_adapter_message_state;
52
53
28352a04 54 class CCECAdapterMessage
220537f2
LOK
55 {
56 public:
0e31a62c 57 CCECAdapterMessage(void) { clear(); }
ad24cbaf 58 CCECAdapterMessage(const cec_command &command);
88c43521 59 CCECAdapterMessage &operator =(const CCECAdapterMessage &msg);
e41b06f9 60 CStdString ToString(void) const;
b8c91906 61 CStdString MessageCodeAsString(void) const;
220537f2 62
ab1469a0 63 bool empty(void) const { return packet.IsEmpty(); }
220537f2
LOK
64 uint8_t operator[](uint8_t pos) const { return packet[pos]; }
65 uint8_t at(uint8_t pos) const { return packet[pos]; }
66 uint8_t size(void) const { return packet.size; }
ae693aaa 67 void clear(void) { state = ADAPTER_MESSAGE_STATE_UNKNOWN; transmit_timeout = 0; packet.Clear(); maxTries = CEC_DEFAULT_TRANSMIT_RETRIES + 1; tries = 0; reply = MSGCODE_NOTHING; }
ab1469a0
LOK
68 void shift(uint8_t iShiftBy) { packet.Shift(iShiftBy); }
69 void push_back(uint8_t add) { packet.PushBack(add); }
70 cec_adapter_messagecode message(void) const { return packet.size >= 1 ? (cec_adapter_messagecode) (packet.At(0) & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK)) : MSGCODE_NOTHING; }
71 bool eom(void) const { return packet.size >= 1 ? (packet.At(0) & MSGCODE_FRAME_EOM) != 0 : false; }
72 bool ack(void) const { return packet.size >= 1 ? (packet.At(0) & MSGCODE_FRAME_ACK) != 0 : false; }
73 cec_logical_address initiator(void) const { return packet.size >= 2 ? (cec_logical_address) (packet.At(1) >> 4) : CECDEVICE_UNKNOWN; };
74 cec_logical_address destination(void) const { return packet.size >= 2 ? (cec_logical_address) (packet.At(1) & 0xF) : CECDEVICE_UNKNOWN; };
e41b06f9 75 bool is_error(void) const;
c7903c02 76 void push_escaped(uint8_t byte);
82d1a9a1
LOK
77 bool needs_retry(void) const { return reply == MSGCODE_NOTHING ||
78 reply == MSGCODE_RECEIVE_FAILED ||
79 reply == MSGCODE_TIMEOUT_ERROR ||
82d1a9a1
LOK
80 reply == MSGCODE_TRANSMIT_FAILED_LINE ||
81 reply == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA ||
82 reply == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE ||
83 reply == MSGCODE_TRANSMIT_LINE_TIMEOUT; }
220537f2 84
82d1a9a1
LOK
85 uint8_t maxTries;
86 uint8_t tries;
87 cec_adapter_messagecode reply;
0e31a62c
LOK
88 cec_datapacket packet;
89 cec_adapter_message_state state;
06bfd4d7 90 int32_t transmit_timeout;
f00ff009
LOK
91 PLATFORM::CMutex mutex;
92 PLATFORM::CCondition condition;
220537f2
LOK
93 };
94
1113cb7d 95 class CCECProcessor;
a8f0bd18 96
f00ff009 97 class CAdapterCommunication : private PLATFORM::CThread
a8f0bd18
LOK
98 {
99 public:
1113cb7d 100 CAdapterCommunication(CCECProcessor *processor);
828682d3 101 virtual ~CAdapterCommunication();
a8f0bd18 102
25701fa6 103 bool Open(const char *strPort, uint16_t iBaudRate = 38400, uint32_t iTimeoutMs = 10000);
220537f2 104 bool Read(CCECAdapterMessage &msg, uint32_t iTimeout = 1000);
28352a04 105 bool Write(CCECAdapterMessage *data);
2abe74eb 106 bool PingAdapter(void);
a8f0bd18 107 void Close(void);
f00ff009 108 bool IsOpen(void);
a8f0bd18
LOK
109 std::string GetError(void) const;
110
828682d3 111 void *Process(void);
2abe74eb 112
a171d2fd 113 bool SetLineTimeout(uint8_t iTimeout);
2abe74eb 114 bool StartBootloader(void);
9dee1670 115
a8f0bd18 116 private:
a96eb42e 117 void SendMessageToAdapter(CCECAdapterMessage *msg);
3c53ac93 118 void WriteNextCommand(void);
8bca69de 119 void AddData(uint8_t *data, uint8_t iLen);
25701fa6 120 bool ReadFromDevice(uint32_t iTimeout);
a8f0bd18 121
f00ff009
LOK
122 PLATFORM::CSerialPort * m_port;
123 CCECProcessor * m_processor;
124 PLATFORM::SyncedBuffer<uint8_t> m_inBuffer;
125 PLATFORM::SyncedBuffer<CCECAdapterMessage *> m_outBuffer;
126 PLATFORM::CMutex m_mutex;
127 PLATFORM::CCondition m_rcvCondition;
128 PLATFORM::CCondition m_startCondition;
129 uint8_t m_iLineTimeout;
a8f0bd18
LOK
130 };
131};