cec: drop shared_ptr and use a normal pointer. removed boost dependency
[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>
b9187cc6 35#include "platform/threads.h"
50aa01e6 36#include "util/buffer.h"
e41b06f9 37#include "util/StdString.h"
25701fa6 38#include <string>
a8f0bd18
LOK
39
40namespace CEC
41{
0e31a62c
LOK
42 typedef enum cec_adapter_message_state
43 {
44 ADAPTER_MESSAGE_STATE_UNKNOWN = 0,
45 ADAPTER_MESSAGE_STATE_WAITING,
46 ADAPTER_MESSAGE_STATE_SENT,
47 ADAPTER_MESSAGE_STATE_RECEIVED,
48 ADAPTER_MESSAGE_STATE_ERROR
49 } cec_adapter_message_state;
50
51
28352a04 52 class CCECAdapterMessage
220537f2
LOK
53 {
54 public:
0e31a62c 55 CCECAdapterMessage(void) { clear(); }
ad24cbaf 56 CCECAdapterMessage(const cec_command &command);
88c43521 57 CCECAdapterMessage &operator =(const CCECAdapterMessage &msg);
e41b06f9 58 CStdString ToString(void) const;
b8c91906 59 CStdString MessageCodeAsString(void) const;
220537f2
LOK
60
61 bool empty(void) const { return packet.empty(); }
62 uint8_t operator[](uint8_t pos) const { return packet[pos]; }
63 uint8_t at(uint8_t pos) const { return packet[pos]; }
64 uint8_t size(void) const { return packet.size; }
06bfd4d7 65 void clear(void) { state = ADAPTER_MESSAGE_STATE_UNKNOWN; transmit_timeout = 0; packet.clear(); }
220537f2
LOK
66 void shift(uint8_t iShiftBy) { packet.shift(iShiftBy); }
67 void push_back(uint8_t add) { packet.push_back(add); }
68 cec_adapter_messagecode message(void) const { return packet.size >= 1 ? (cec_adapter_messagecode) (packet.at(0) & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK)) : MSGCODE_NOTHING; }
69 bool eom(void) const { return packet.size >= 1 ? (packet.at(0) & MSGCODE_FRAME_EOM) != 0 : false; }
70 bool ack(void) const { return packet.size >= 1 ? (packet.at(0) & MSGCODE_FRAME_ACK) != 0 : false; }
71 cec_logical_address initiator(void) const { return packet.size >= 2 ? (cec_logical_address) (packet.at(1) >> 4) : CECDEVICE_UNKNOWN; };
72 cec_logical_address destination(void) const { return packet.size >= 2 ? (cec_logical_address) (packet.at(1) & 0xF) : CECDEVICE_UNKNOWN; };
e41b06f9 73 bool is_error(void) const;
ad24cbaf 74 void push_escaped(int16_t byte);
220537f2 75
0e31a62c
LOK
76 cec_datapacket packet;
77 cec_adapter_message_state state;
06bfd4d7 78 int32_t transmit_timeout;
0e31a62c
LOK
79 CMutex mutex;
80 CCondition condition;
220537f2
LOK
81 };
82
b9187cc6 83 class CSerialPort;
2abe74eb 84 class CLibCEC;
a8f0bd18 85
13fd6a66 86 class CAdapterCommunication : private CThread
a8f0bd18
LOK
87 {
88 public:
2abe74eb 89 CAdapterCommunication(CLibCEC *controller);
828682d3 90 virtual ~CAdapterCommunication();
a8f0bd18 91
25701fa6 92 bool Open(const char *strPort, uint16_t iBaudRate = 38400, uint32_t iTimeoutMs = 10000);
220537f2 93 bool Read(CCECAdapterMessage &msg, uint32_t iTimeout = 1000);
28352a04 94 bool Write(CCECAdapterMessage *data);
2abe74eb 95 bool PingAdapter(void);
a8f0bd18 96 void Close(void);
13fd6a66 97 bool IsOpen(void) const;
a8f0bd18
LOK
98 std::string GetError(void) const;
99
828682d3 100 void *Process(void);
2abe74eb
LOK
101
102 bool StartBootloader(void);
9dee1670 103
a8f0bd18 104 private:
3c53ac93 105 void WriteNextCommand(void);
8bca69de 106 void AddData(uint8_t *data, uint8_t iLen);
25701fa6 107 bool ReadFromDevice(uint32_t iTimeout);
a8f0bd18 108
5a1e87c8
LOK
109 CSerialPort * m_port;
110 CLibCEC * m_controller;
111 CecBuffer<uint8_t> m_inBuffer;
28352a04 112 CecBuffer<CCECAdapterMessage *> m_outBuffer;
f077e8ed 113 CMutex m_mutex;
5a1e87c8 114 CCondition m_rcvCondition;
b9eea66d 115 CCondition m_startCondition;
a8f0bd18
LOK
116 };
117};