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