cfe294480495033b71e96ddb4e580e552d57c226
[deb_libcec.git] / src / lib / adapter / USBCECAdapterCommunication.h
1 #pragma once
2 /*
3 * This file is part of the libCEC(R) library.
4 *
5 * libCEC(R) is Copyright (C) 2011-2012 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 "../../../include/cectypes.h"
35 #include "../platform/threads/threads.h"
36 #include "../platform/util/buffer.h"
37 #include "AdapterCommunication.h"
38 #include "USBCECAdapterMessage.h"
39
40 namespace PLATFORM
41 {
42 class ISocket;
43 }
44
45 namespace CEC
46 {
47 class CCECProcessor;
48 class CAdapterPingThread;
49 class CUSBCECAdapterCommands;
50 class CCECAdapterMessageQueue;
51
52 class CUSBCECAdapterCommunication : public IAdapterCommunication, public PLATFORM::CThread
53 {
54 friend class CUSBCECAdapterCommands;
55 friend class CCECAdapterMessageQueue;
56
57 public:
58 /*!
59 * @brief Create a new USB-CEC communication handler.
60 * @param callback The callback to use for incoming CEC commands.
61 * @param strPort The name of the com port to use.
62 * @param iBaudRate The baudrate to use on the com port connection.
63 */
64 CUSBCECAdapterCommunication(IAdapterCommunicationCallback *callback, const char *strPort, uint16_t iBaudRate = CEC_SERIAL_DEFAULT_BAUDRATE);
65 virtual ~CUSBCECAdapterCommunication(void);
66
67 /** @name IAdapterCommunication implementation */
68 ///{
69 bool Open(uint32_t iTimeoutMs = CEC_DEFAULT_CONNECT_TIMEOUT, bool bSkipChecks = false, bool bStartListening = true);
70 void Close(void);
71 bool IsOpen(void);
72 CStdString GetError(void) const;
73 cec_adapter_message_state Write(const cec_command &data, bool &bRetry, uint8_t iLineTimeout = 3);
74
75 bool StartBootloader(void);
76 bool SetAckMask(uint16_t iMask);
77 bool PingAdapter(void);
78 uint16_t GetFirmwareVersion(void);
79 uint32_t GetFirmwareBuildDate(void);
80 bool IsRunningLatestFirmware(void);
81 bool PersistConfiguration(libcec_configuration *configuration);
82 bool GetConfiguration(libcec_configuration *configuration);
83 CStdString GetPortName(void);
84 uint16_t GetPhysicalAddress(void) { return 0; }
85 bool SetControlledMode(bool controlled);
86 ///}
87
88 void *Process(void);
89
90 private:
91 /*!
92 * @brief Clear all input bytes.
93 * @param iTimeout Timeout when anything was received.
94 */
95 void ClearInputBytes(uint32_t iTimeout = CEC_CLEAR_INPUT_DEFAULT_WAIT);
96
97 /*!
98 * @brief Change the current CEC line timeout.
99 * @param iTimeout The new timeout.
100 * @return True when acked by the controller, false otherwise.
101 */
102 bool SetLineTimeout(uint8_t iTimeout);
103
104 /*!
105 * @brief Send a command to the controller and wait for an ack.
106 * @param msgCode The command to send.
107 * @param params The parameters to the command.
108 * @param bIsRetry True when this command is being retried, false otherwise.
109 * @return The message. Delete when done with it.
110 */
111 CCECAdapterMessage *SendCommand(cec_adapter_messagecode msgCode, CCECAdapterMessage &params, bool bIsRetry = false);
112
113 /*!
114 * @brief Change the "initialised" status.
115 * @param bSetTo The new value.
116 */
117 void SetInitialised(bool bSetTo = true);
118
119 /*!
120 * @return True when initialised, false otherwise.
121 */
122 bool IsInitialised(void);
123
124 /*!
125 * @brief Pings the adapter, checks the firmware version and sets controlled mode.
126 * @param iTimeoutMs The timeout after which this fails if no proper data was received.
127 * @return True when the checks passed, false otherwise.
128 */
129 bool CheckAdapter(uint32_t iTimeoutMs = CEC_DEFAULT_CONNECT_TIMEOUT);
130
131 /*!
132 * @brief Handle a poll message inside the adapter message (checks if one is present).
133 * @param msg The adapter message to parse.
134 * @return True when the message resulted in a CEC error, false otherwise.
135 */
136 bool HandlePoll(const CCECAdapterMessage &msg);
137
138 /*!
139 * @brief Read data from the device.
140 * @param iTimeout The read timeout to use.
141 * @param iSize The maximum read size.
142 * @return True when something was read, false otherwise.
143 */
144 bool ReadFromDevice(uint32_t iTimeout, size_t iSize = 256);
145
146 /*!
147 * @brief Writes a message to the serial port.
148 * @param message The message to write.
149 * @return True when written, false otherwise.
150 */
151 bool WriteToDevice(CCECAdapterMessage *message);
152
153 /*!
154 * @brief Called before sending a CEC command over the line, so we know we're expecting an ack.
155 * @param dest The destination of the CEC command.
156 */
157 void MarkAsWaiting(const cec_logical_address dest);
158
159 PLATFORM::ISocket * m_port; /**< the com port connection */
160 PLATFORM::CMutex m_mutex; /**< mutex for changes in this class */
161 uint8_t m_iLineTimeout; /**< the current line timeout on the CEC line */
162 cec_logical_address m_lastPollDestination; /**< the destination of the last poll message that was received */
163 bool m_bInitialised; /**< true when the connection is initialised, false otherwise */
164 bool m_bWaitingForAck[15]; /**< array in which we store from which devices we're expecting acks */
165 CAdapterPingThread * m_pingThread; /**< ping thread, that pings the adapter every 15 seconds */
166 CUSBCECAdapterCommands * m_commands; /**< commands that can be sent to the adapter */
167 CCECAdapterMessageQueue * m_adapterMessageQueue; /**< the incoming and outgoing message queue */
168 };
169
170 class CAdapterPingThread : public PLATFORM::CThread
171 {
172 public:
173 CAdapterPingThread(CUSBCECAdapterCommunication *com, uint32_t iTimeout) :
174 m_com(com),
175 m_timeout(iTimeout){}
176 virtual ~CAdapterPingThread(void) {}
177
178 virtual void* Process(void);
179 private:
180 CUSBCECAdapterCommunication *m_com;
181 PLATFORM::CTimeout m_timeout;
182 };
183 };