Imported Upstream version 2.2.0
[deb_libcec.git] / src / lib / adapter / Pulse-Eight / USBCECAdapterCommunication.h
CommitLineData
cbbe90dd
JB
1#pragma once
2/*
3 * This file is part of the libCEC(R) library.
4 *
5 * libCEC(R) is Copyright (C) 2011-2013 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 "lib/platform/threads/threads.h"
35#include "lib/adapter/AdapterCommunication.h"
36#include "USBCECAdapterMessage.h"
37
38namespace PLATFORM
39{
40 class ISocket;
41}
42
43namespace CEC
44{
45 class CCECProcessor;
46 class CAdapterPingThread;
47 class CAdapterEepromWriteThread;
48 class CUSBCECAdapterCommands;
49 class CCECAdapterMessageQueue;
50 class CCECAdapterMessage;
51
52 class CUSBCECAdapterCommunication : public IAdapterCommunication, public PLATFORM::CThread
53 {
54 friend class CUSBCECAdapterCommands;
55 friend class CCECAdapterMessageQueue;
56 friend class CAdapterEepromWriteThread;
57
58 public:
59 /*!
60 * @brief Create a new USB-CEC communication handler.
61 * @param callback The callback to use for incoming CEC commands.
62 * @param strPort The name of the com port to use.
63 * @param iBaudRate The baudrate to use on the com port connection.
64 */
65 CUSBCECAdapterCommunication(IAdapterCommunicationCallback *callback, const char *strPort, uint16_t iBaudRate = CEC_SERIAL_DEFAULT_BAUDRATE);
66 virtual ~CUSBCECAdapterCommunication(void);
67
68 /** @name IAdapterCommunication implementation */
69 ///{
70 bool Open(uint32_t iTimeoutMs = CEC_DEFAULT_CONNECT_TIMEOUT, bool bSkipChecks = false, bool bStartListening = true);
71 void Close(void);
72 bool IsOpen(void);
73 std::string GetError(void) const;
74 cec_adapter_message_state Write(const cec_command &data, bool &bRetry, uint8_t iLineTimeout, bool bIsReply);
75
76 bool StartBootloader(void);
77 bool SetLogicalAddresses(const cec_logical_addresses &addresses);
78 cec_logical_addresses GetLogicalAddresses(void);
79 bool PingAdapter(void);
80 uint16_t GetFirmwareVersion(void);
81 uint32_t GetFirmwareBuildDate(void);
82 bool IsRunningLatestFirmware(void);
83 bool PersistConfiguration(const libcec_configuration &configuration);
84 bool GetConfiguration(libcec_configuration &configuration);
85 std::string GetPortName(void);
86 uint16_t GetPhysicalAddress(void);
87 bool SetControlledMode(bool controlled);
88 cec_vendor_id GetVendorId(void) { return CEC_VENDOR_UNKNOWN; }
89 bool SupportsSourceLogicalAddress(const cec_logical_address UNUSED(address)) { return true; }
90 cec_adapter_type GetAdapterType(void);
91 uint16_t GetAdapterVendorId(void) const;
92 uint16_t GetAdapterProductId(void) const;
93 void SetActiveSource(bool bSetTo, bool bClientUnregistered);
94 ///}
95
96 bool ProvidesExtendedResponse(void);
97
98 void *Process(void);
99
100 private:
101 /*!
102 * @brief Clear all input bytes.
103 * @param iTimeout Timeout when anything was received.
104 */
105 void ClearInputBytes(uint32_t iTimeout = CEC_CLEAR_INPUT_DEFAULT_WAIT);
106
107 /*!
108 * @brief Change the current CEC line timeout.
109 * @param iTimeout The new timeout.
110 * @return True when acked by the controller, false otherwise.
111 */
112 bool SetLineTimeout(uint8_t iTimeout);
113
114 /*!
115 * @brief Send a command to the controller and wait for an ack.
116 * @param msgCode The command to send.
117 * @param params The parameters to the command.
118 * @param bIsRetry True when this command is being retried, false otherwise.
119 * @return The message. Delete when done with it.
120 */
121 CCECAdapterMessage *SendCommand(cec_adapter_messagecode msgCode, CCECAdapterMessage &params, bool bIsRetry = false);
122
123 /*!
124 * @brief Change the "initialised" status.
125 * @param bSetTo The new value.
126 */
127 void SetInitialised(bool bSetTo = true);
128
129 /*!
130 * @return True when initialised, false otherwise.
131 */
132 bool IsInitialised(void);
133
134 /*!
135 * @brief Pings the adapter, checks the firmware version and sets controlled mode.
136 * @param iTimeoutMs The timeout after which this fails if no proper data was received.
137 * @return True when the checks passed, false otherwise.
138 */
139 bool CheckAdapter(uint32_t iTimeoutMs = CEC_DEFAULT_CONNECT_TIMEOUT);
140
141 /*!
142 * @brief Handle a poll message inside the adapter message (checks if one is present).
143 * @param msg The adapter message to parse.
144 * @return True when the message resulted in a CEC error, false otherwise.
145 */
146 bool HandlePoll(const CCECAdapterMessage &msg);
147
148 /*!
149 * @brief Read data from the device.
150 * @param iTimeout The read timeout to use.
151 * @param iSize The maximum read size.
152 * @return True when something was read, false otherwise.
153 */
154 bool ReadFromDevice(uint32_t iTimeout, size_t iSize = 256);
155
156 /*!
157 * @brief Writes a message to the serial port.
158 * @param message The message to write.
159 * @return True when written, false otherwise.
160 */
161 bool WriteToDevice(CCECAdapterMessage *message);
162
163 /*!
164 * @brief Called before sending a CEC command over the line, so we know we're expecting an ack.
165 * @param dest The destination of the CEC command.
166 */
167 void MarkAsWaiting(const cec_logical_address dest);
168
169 /*!
170 * @brief Clear and reset the message queue.
171 */
172 void ResetMessageQueue(void);
173
174 PLATFORM::ISocket * m_port; /**< the com port connection */
175 PLATFORM::CMutex m_mutex; /**< mutex for changes in this class */
176 uint8_t m_iLineTimeout; /**< the current line timeout on the CEC line */
177 cec_logical_address m_lastPollDestination; /**< the destination of the last poll message that was received */
178 bool m_bInitialised; /**< true when the connection is initialised, false otherwise */
179 bool m_bWaitingForAck[15]; /**< array in which we store from which devices we're expecting acks */
180 CAdapterPingThread * m_pingThread; /**< ping thread, that pings the adapter every 15 seconds */
181 CAdapterEepromWriteThread * m_eepromWriteThread; /**< eeprom writes are done async */
182 CUSBCECAdapterCommands * m_commands; /**< commands that can be sent to the adapter */
183 CCECAdapterMessageQueue * m_adapterMessageQueue; /**< the incoming and outgoing message queue */
184 cec_logical_addresses m_logicalAddresses; /**< the logical address list that this instance is using */
185 PLATFORM::CMutex m_waitingMutex;
186 };
187
188 class CAdapterEepromWriteThread : public PLATFORM::CThread
189 {
190 public:
191 CAdapterEepromWriteThread(CUSBCECAdapterCommunication *com) :
192 m_com(com),
193 m_bWrite(false),
194 m_iLastEepromWrite(0),
195 m_iScheduleEepromWrite(0) {}
196 virtual ~CAdapterEepromWriteThread(void) {}
197
198 bool Write(void);
199 void* Process(void);
200 void Stop(void);
201 private:
202 CUSBCECAdapterCommunication *m_com;
203 bool m_bWrite;
204 PLATFORM::CCondition<bool> m_condition;
205 PLATFORM::CMutex m_mutex;
206 int64_t m_iLastEepromWrite; /**< last time that this instance did an eeprom write */
207 int64_t m_iScheduleEepromWrite; /**< in case there were more than 2 changes within 30 seconds, do another write at this time */
208 };
209
210 class CAdapterPingThread : public PLATFORM::CThread
211 {
212 public:
213 CAdapterPingThread(CUSBCECAdapterCommunication *com, uint32_t iTimeout) :
214 m_com(com),
215 m_timeout(iTimeout){}
216 virtual ~CAdapterPingThread(void) {}
217
218 void* Process(void);
219 private:
220 CUSBCECAdapterCommunication *m_com;
221 PLATFORM::CTimeout m_timeout;
222 };
223};