cec: show the command as string instead of int when an unexpected command was received.
[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 "platform/threads.h"
36 #include "util/buffer.h"
37 #include "util/StdString.h"
38 #include <string>
39 #include <boost/enable_shared_from_this.hpp>
40 #include <boost/shared_ptr.hpp>
41
42 namespace CEC
43 {
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
54 class CCECAdapterMessage : public boost::enable_shared_from_this<CCECAdapterMessage>
55 {
56 public:
57 CCECAdapterMessage(void) { clear(); }
58 CCECAdapterMessage(const cec_command &command);
59 CCECAdapterMessage &operator =(const CCECAdapterMessage &msg);
60 CStdString ToString(void) const;
61 CStdString MessageCodeAsString(void) const;
62
63 bool empty(void) const { return packet.empty(); }
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; }
67 void clear(void) { state = ADAPTER_MESSAGE_STATE_UNKNOWN; packet.clear(); }
68 void shift(uint8_t iShiftBy) { packet.shift(iShiftBy); }
69 void push_back(uint8_t add) { packet.push_back(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; };
75 bool is_error(void) const;
76 void push_escaped(int16_t byte);
77
78 cec_datapacket packet;
79 cec_adapter_message_state state;
80 CMutex mutex;
81 CCondition condition;
82 };
83 typedef boost::shared_ptr<CCECAdapterMessage> CCECAdapterMessagePtr;
84
85 class CSerialPort;
86 class CLibCEC;
87
88 class CAdapterCommunication : private CThread
89 {
90 public:
91 CAdapterCommunication(CLibCEC *controller);
92 virtual ~CAdapterCommunication();
93
94 bool Open(const char *strPort, uint16_t iBaudRate = 38400, uint32_t iTimeoutMs = 10000);
95 bool Read(CCECAdapterMessage &msg, uint32_t iTimeout = 1000);
96 bool Write(CCECAdapterMessagePtr data);
97 bool PingAdapter(void);
98 void Close(void);
99 bool IsOpen(void) const;
100 std::string GetError(void) const;
101
102 void *Process(void);
103
104 bool StartBootloader(void);
105 bool SetAckMask(uint16_t iMask);
106
107 private:
108 void WriteNextCommand(void);
109 void AddData(uint8_t *data, uint8_t iLen);
110 bool ReadFromDevice(uint32_t iTimeout);
111
112 CSerialPort * m_port;
113 CLibCEC * m_controller;
114 CecBuffer<uint8_t> m_inBuffer;
115 CecBuffer<CCECAdapterMessagePtr> m_outBuffer;
116 CMutex m_mutex;
117 CCondition m_rcvCondition;
118 CCondition m_startCondition;
119 };
120 };