cec: wait for messages to be transmitted before continueing in CCECProcessor::Transmit()
[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"
25701fa6 37#include <string>
5a1e87c8
LOK
38#include <boost/enable_shared_from_this.hpp>
39#include <boost/shared_ptr.hpp>
a8f0bd18
LOK
40
41namespace CEC
42{
5a1e87c8 43 class CCECAdapterMessage : public boost::enable_shared_from_this<CCECAdapterMessage>
220537f2
LOK
44 {
45 public:
46 CCECAdapterMessage(void) {}
ad24cbaf 47 CCECAdapterMessage(const cec_command &command);
220537f2
LOK
48
49 bool empty(void) const { return packet.empty(); }
50 uint8_t operator[](uint8_t pos) const { return packet[pos]; }
51 uint8_t at(uint8_t pos) const { return packet[pos]; }
52 uint8_t size(void) const { return packet.size; }
53 void clear(void) { packet.clear(); }
54 void shift(uint8_t iShiftBy) { packet.shift(iShiftBy); }
55 void push_back(uint8_t add) { packet.push_back(add); }
56 cec_adapter_messagecode message(void) const { return packet.size >= 1 ? (cec_adapter_messagecode) (packet.at(0) & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK)) : MSGCODE_NOTHING; }
57 bool eom(void) const { return packet.size >= 1 ? (packet.at(0) & MSGCODE_FRAME_EOM) != 0 : false; }
58 bool ack(void) const { return packet.size >= 1 ? (packet.at(0) & MSGCODE_FRAME_ACK) != 0 : false; }
59 cec_logical_address initiator(void) const { return packet.size >= 2 ? (cec_logical_address) (packet.at(1) >> 4) : CECDEVICE_UNKNOWN; };
60 cec_logical_address destination(void) const { return packet.size >= 2 ? (cec_logical_address) (packet.at(1) & 0xF) : CECDEVICE_UNKNOWN; };
ad24cbaf 61 void push_escaped(int16_t byte);
220537f2
LOK
62
63 cec_datapacket packet;
eb35e4ca
LOK
64 CMutex mutex;
65 CCondition condition;
c6fc9e4b
LOK
66
67 private:
68 CCECAdapterMessage &operator =(const CCECAdapterMessage &msg);
220537f2 69 };
5a1e87c8 70 typedef boost::shared_ptr<CCECAdapterMessage> CCECAdapterMessagePtr;
220537f2 71
b9187cc6 72 class CSerialPort;
2abe74eb 73 class CLibCEC;
a8f0bd18 74
13fd6a66 75 class CAdapterCommunication : private CThread
a8f0bd18
LOK
76 {
77 public:
2abe74eb 78 CAdapterCommunication(CLibCEC *controller);
828682d3 79 virtual ~CAdapterCommunication();
a8f0bd18 80
25701fa6 81 bool Open(const char *strPort, uint16_t iBaudRate = 38400, uint32_t iTimeoutMs = 10000);
220537f2 82 bool Read(CCECAdapterMessage &msg, uint32_t iTimeout = 1000);
5a1e87c8 83 bool Write(CCECAdapterMessagePtr data);
2abe74eb 84 bool PingAdapter(void);
a8f0bd18 85 void Close(void);
13fd6a66 86 bool IsOpen(void) const;
a8f0bd18
LOK
87 std::string GetError(void) const;
88
828682d3 89 void *Process(void);
2abe74eb
LOK
90
91 bool StartBootloader(void);
92 bool SetAckMask(uint16_t iMask);
9dee1670 93
a8f0bd18 94 private:
3c53ac93 95 void WriteNextCommand(void);
8bca69de 96 void AddData(uint8_t *data, uint8_t iLen);
25701fa6 97 bool ReadFromDevice(uint32_t iTimeout);
a8f0bd18 98
5a1e87c8
LOK
99 CSerialPort * m_port;
100 CLibCEC * m_controller;
101 CecBuffer<uint8_t> m_inBuffer;
102 CecBuffer<CCECAdapterMessagePtr> m_outBuffer;
f077e8ed 103 CMutex m_mutex;
5a1e87c8 104 CCondition m_rcvCondition;
a8f0bd18
LOK
105 };
106};