Commit | Line | Data |
---|---|---|
a75e3a5a 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/platform/util/buffer.h" | |
daec0320 | 36 | #include "lib/platform/util/timeutils.h" |
8cdaa059 | 37 | #include <map> |
cb8feb41 | 38 | #include "USBCECAdapterMessage.h" |
a75e3a5a LOK |
39 | |
40 | namespace CEC | |
41 | { | |
42 | class CUSBCECAdapterCommunication; | |
004b8382 | 43 | class CCECAdapterMessageQueue; |
2b44051c | 44 | class CCECAdapterMessage; |
a75e3a5a LOK |
45 | |
46 | class CCECAdapterMessageQueueEntry | |
47 | { | |
48 | public: | |
004b8382 | 49 | CCECAdapterMessageQueueEntry(CCECAdapterMessageQueue *queue, CCECAdapterMessage *message); |
a75e3a5a LOK |
50 | virtual ~CCECAdapterMessageQueueEntry(void); |
51 | ||
52 | /*! | |
53 | * @brief Signal waiting threads | |
54 | */ | |
55 | void Broadcast(void); | |
56 | ||
57 | /*! | |
58 | * @brief Called when a message was received. | |
59 | * @param message The message that was received. | |
60 | * @return True when this message was handled by this entry, false otherwise. | |
61 | */ | |
62 | bool MessageReceived(const CCECAdapterMessage &message); | |
63 | ||
64 | /*! | |
65 | * @brief Wait for a response to this command. | |
66 | * @param iTimeout The timeout to use while waiting. | |
67 | * @return True when a response was received before the timeout passed, false otherwise. | |
68 | */ | |
69 | bool Wait(uint32_t iTimeout); | |
70 | ||
71 | /*! | |
72 | * @return True while a thread is waiting for a signal or isn't waiting yet, false otherwise. | |
73 | */ | |
74 | bool IsWaiting(void); | |
75 | ||
76 | /*! | |
77 | * @return The msgcode of the command that was sent. | |
78 | */ | |
79 | cec_adapter_messagecode MessageCode(void); | |
80 | ||
81 | /*! | |
82 | * @brief Check whether a message is a response to this command. | |
83 | * @param msg The message to check. | |
84 | * @return True when it's a response, false otherwise. | |
85 | */ | |
86 | bool IsResponse(const CCECAdapterMessage &msg); | |
3ead056c | 87 | bool IsResponseOld(const CCECAdapterMessage &msg); |
a75e3a5a LOK |
88 | |
89 | /*! | |
90 | * @return The command that was sent in human readable form. | |
91 | */ | |
92 | const char *ToString(void) const; | |
93 | ||
a75e3a5a LOK |
94 | /*! |
95 | * @brief Called when a 'command accepted' message was received. | |
96 | * @param message The message that was received. | |
8cdaa059 | 97 | * @return True when the message was handled, false otherwise. |
a75e3a5a LOK |
98 | */ |
99 | bool MessageReceivedCommandAccepted(const CCECAdapterMessage &message); | |
100 | ||
101 | /*! | |
102 | * @brief Called when a 'transmit succeeded' message was received. | |
103 | * @param message The message that was received. | |
8cdaa059 | 104 | * @return True when the message was handled, false otherwise. |
a75e3a5a LOK |
105 | */ |
106 | bool MessageReceivedTransmitSucceeded(const CCECAdapterMessage &message); | |
107 | ||
108 | /*! | |
109 | * @brief Called when a message that's not a 'command accepted' or 'transmit succeeded' message was received. | |
110 | * @param message The message that was received. | |
8cdaa059 | 111 | * @return True when the message was handled, false otherwise. |
a75e3a5a LOK |
112 | */ |
113 | bool MessageReceivedResponse(const CCECAdapterMessage &message); | |
114 | ||
8cdaa059 LOK |
115 | /*! |
116 | * @brief Signals the waiting thread. | |
117 | */ | |
118 | void Signal(void); | |
119 | ||
3ead056c LOK |
120 | bool ProvidesExtendedResponse(void); |
121 | ||
daec0320 LOK |
122 | /*! |
123 | * @return True when a fire and forget packet timed out or succeeded, false otherwise | |
124 | */ | |
125 | bool TimedOutOrSucceeded(void) const; | |
126 | ||
004b8382 | 127 | CCECAdapterMessageQueue * m_queue; |
a75e3a5a LOK |
128 | CCECAdapterMessage * m_message; /**< the message that was sent */ |
129 | uint8_t m_iPacketsLeft; /**< the amount of acks that we're waiting on */ | |
130 | bool m_bSucceeded; /**< true when the command received a response, false otherwise */ | |
131 | bool m_bWaiting; /**< true while a thread is waiting or when it hasn't started waiting yet */ | |
132 | PLATFORM::CCondition<bool> m_condition; /**< the condition to wait on */ | |
133 | PLATFORM::CMutex m_mutex; /**< mutex for changes to this class */ | |
daec0320 | 134 | PLATFORM::CTimeout m_queueTimeout; /**< ack timeout for fire and forget commands */ |
a75e3a5a LOK |
135 | }; |
136 | ||
a8559e01 | 137 | class CCECAdapterMessageQueue : public PLATFORM::CThread |
a75e3a5a LOK |
138 | { |
139 | friend class CUSBCECAdapterCommunication; | |
004b8382 | 140 | friend class CCECAdapterMessageQueueEntry; |
a75e3a5a LOK |
141 | |
142 | public: | |
143 | /*! | |
144 | * @brief Create a new message queue. | |
145 | * @param com The communication handler callback to use. | |
146 | * @param iQueueSize The outgoing message queue size. | |
147 | */ | |
2b44051c | 148 | CCECAdapterMessageQueue(CUSBCECAdapterCommunication *com); |
a75e3a5a LOK |
149 | virtual ~CCECAdapterMessageQueue(void); |
150 | ||
151 | /*! | |
152 | * @brief Signal and delete everything in the queue | |
153 | */ | |
154 | void Clear(void); | |
155 | ||
156 | /*! | |
157 | * @brief Called when a message was received from the adapter. | |
158 | * @param msg The message that was received. | |
159 | */ | |
160 | void MessageReceived(const CCECAdapterMessage &msg); | |
161 | ||
162 | /*! | |
163 | * @brief Adds received data to the current frame. | |
164 | * @param data The data to add. | |
165 | * @param iLen The length of the data to add. | |
166 | */ | |
167 | void AddData(uint8_t *data, size_t iLen); | |
168 | ||
169 | /*! | |
170 | * @brief Transmit a command to the adapter and wait for a response. | |
171 | * @param msg The command to send. | |
172 | * @return True when written, false otherwise. | |
173 | */ | |
174 | bool Write(CCECAdapterMessage *msg); | |
175 | ||
3ead056c LOK |
176 | bool ProvidesExtendedResponse(void); |
177 | ||
a8559e01 LOK |
178 | virtual void *Process(void); |
179 | ||
daec0320 LOK |
180 | void CheckTimedOutMessages(void); |
181 | ||
a75e3a5a | 182 | private: |
a75e3a5a LOK |
183 | CUSBCECAdapterCommunication * m_com; /**< the communication handler */ |
184 | PLATFORM::CMutex m_mutex; /**< mutex for changes to this class */ | |
8cdaa059 | 185 | std::map<uint64_t, CCECAdapterMessageQueueEntry *> m_messages; /**< the outgoing message queue */ |
a8559e01 | 186 | PLATFORM::SyncedBuffer<CCECAdapterMessageQueueEntry *> m_writeQueue; /**< the queue for messages that are to be written */ |
8cdaa059 | 187 | uint64_t m_iNextMessage; /**< the index of the next message */ |
2b44051c | 188 | CCECAdapterMessage *m_incomingAdapterMessage; /**< the current incoming message that's being assembled */ |
a75e3a5a LOK |
189 | cec_command m_currentCECFrame; /**< the current incoming CEC command that's being assembled */ |
190 | }; | |
191 | } |