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