Commit | Line | Data |
---|---|---|
a75e3a5a LOK |
1 | /* |
2 | * This file is part of the libCEC(R) library. | |
3 | * | |
4 | * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved. | |
5 | * libCEC(R) is an original work, containing original code. | |
6 | * | |
7 | * libCEC(R) is a trademark of Pulse-Eight Limited. | |
8 | * | |
9 | * This program is dual-licensed; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
22 | * | |
23 | * | |
24 | * Alternatively, you can license this library under a commercial license, | |
25 | * please contact Pulse-Eight Licensing for more information. | |
26 | * | |
27 | * For more information contact: | |
28 | * Pulse-Eight Licensing <license@pulse-eight.com> | |
29 | * http://www.pulse-eight.com/ | |
30 | * http://www.pulse-eight.net/ | |
31 | */ | |
32 | ||
33 | #include "USBCECAdapterMessageQueue.h" | |
34 | #include "USBCECAdapterCommunication.h" | |
35 | #include "../platform/sockets/socket.h" | |
36 | #include "../LibCEC.h" | |
37 | ||
38 | using namespace CEC; | |
39 | using namespace PLATFORM; | |
8cdaa059 | 40 | using namespace std; |
a75e3a5a LOK |
41 | |
42 | CCECAdapterMessageQueueEntry::CCECAdapterMessageQueueEntry(CCECAdapterMessage *message) : | |
43 | m_message(message), | |
44 | m_iPacketsLeft(message->IsTranmission() ? message->Size() / 4 : 1), | |
45 | m_bSucceeded(false), | |
46 | m_bWaiting(true) {} | |
47 | ||
48 | CCECAdapterMessageQueueEntry::~CCECAdapterMessageQueueEntry(void) { } | |
49 | ||
50 | void CCECAdapterMessageQueueEntry::Broadcast(void) | |
51 | { | |
52 | CLockObject lock(m_mutex); | |
53 | m_condition.Broadcast(); | |
54 | } | |
55 | ||
56 | bool CCECAdapterMessageQueueEntry::MessageReceived(const CCECAdapterMessage &message) | |
57 | { | |
a75e3a5a LOK |
58 | bool bHandled(false); |
59 | ||
8cdaa059 | 60 | if (IsResponse(message)) |
a75e3a5a | 61 | { |
a75e3a5a LOK |
62 | switch (message.Message()) |
63 | { | |
64 | case MSGCODE_COMMAND_ACCEPTED: | |
8cdaa059 | 65 | bHandled = MessageReceivedCommandAccepted(message); |
a75e3a5a LOK |
66 | break; |
67 | case MSGCODE_TRANSMIT_SUCCEEDED: | |
8cdaa059 | 68 | bHandled = MessageReceivedTransmitSucceeded(message); |
a75e3a5a LOK |
69 | break; |
70 | default: | |
8cdaa059 | 71 | bHandled = MessageReceivedResponse(message); |
a75e3a5a LOK |
72 | break; |
73 | } | |
74 | } | |
75 | ||
a75e3a5a LOK |
76 | return bHandled; |
77 | } | |
78 | ||
8cdaa059 LOK |
79 | void CCECAdapterMessageQueueEntry::Signal(void) |
80 | { | |
81 | CLockObject lock(m_mutex); | |
82 | m_bSucceeded = true; | |
83 | m_condition.Signal(); | |
84 | } | |
85 | ||
a75e3a5a LOK |
86 | bool CCECAdapterMessageQueueEntry::Wait(uint32_t iTimeout) |
87 | { | |
88 | bool bReturn(false); | |
89 | /* wait until we receive a signal when the tranmission succeeded */ | |
90 | { | |
91 | CLockObject lock(m_mutex); | |
92 | bReturn = m_bSucceeded ? true : m_condition.Wait(m_mutex, m_bSucceeded, iTimeout); | |
93 | m_bWaiting = false; | |
94 | } | |
95 | return bReturn; | |
96 | } | |
97 | ||
98 | bool CCECAdapterMessageQueueEntry::IsWaiting(void) | |
99 | { | |
100 | CLockObject lock(m_mutex); | |
101 | return m_bWaiting; | |
102 | } | |
103 | ||
104 | cec_adapter_messagecode CCECAdapterMessageQueueEntry::MessageCode(void) | |
105 | { | |
106 | return m_message->Message(); | |
107 | } | |
108 | ||
109 | bool CCECAdapterMessageQueueEntry::IsResponse(const CCECAdapterMessage &msg) | |
110 | { | |
111 | cec_adapter_messagecode msgCode = msg.Message(); | |
112 | return msgCode == MessageCode() || | |
113 | msgCode == MSGCODE_TIMEOUT_ERROR || | |
114 | msgCode == MSGCODE_COMMAND_ACCEPTED || | |
115 | msgCode == MSGCODE_COMMAND_REJECTED || | |
116 | (m_message->IsTranmission() && msgCode == MSGCODE_HIGH_ERROR) || | |
117 | (m_message->IsTranmission() && msgCode == MSGCODE_LOW_ERROR) || | |
118 | (m_message->IsTranmission() && msgCode == MSGCODE_RECEIVE_FAILED) || | |
119 | (m_message->IsTranmission() && msgCode == MSGCODE_TRANSMIT_FAILED_LINE) || | |
120 | (m_message->IsTranmission() && msgCode == MSGCODE_TRANSMIT_FAILED_ACK) || | |
121 | (m_message->IsTranmission() && msgCode == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA) || | |
122 | (m_message->IsTranmission() && msgCode == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE) || | |
123 | (m_message->IsTranmission() && msgCode == MSGCODE_TRANSMIT_SUCCEEDED); | |
124 | } | |
125 | ||
126 | const char *CCECAdapterMessageQueueEntry::ToString(void) const | |
127 | { | |
128 | /* CEC transmissions got the 'set ack polarity' msgcode, which doesn't look nice */ | |
129 | if (m_message->IsTranmission()) | |
130 | return "CEC transmission"; | |
131 | else | |
132 | return CCECAdapterMessage::ToString(m_message->Message()); | |
133 | } | |
134 | ||
135 | bool CCECAdapterMessageQueueEntry::MessageReceivedCommandAccepted(const CCECAdapterMessage &message) | |
136 | { | |
137 | bool bSendSignal(false); | |
8cdaa059 | 138 | bool bHandled(false); |
a75e3a5a | 139 | { |
8cdaa059 | 140 | CLockObject lock(m_mutex); |
a75e3a5a | 141 | if (m_iPacketsLeft > 0) |
8cdaa059 LOK |
142 | { |
143 | /* decrease by 1 */ | |
a75e3a5a LOK |
144 | m_iPacketsLeft--; |
145 | ||
8cdaa059 LOK |
146 | /* log this message */ |
147 | CStdString strLog; | |
148 | strLog.Format("%s - command accepted", ToString()); | |
149 | if (m_iPacketsLeft > 0) | |
150 | strLog.AppendFormat(" - waiting for %d more", m_iPacketsLeft); | |
151 | CLibCEC::AddLog(CEC_LOG_DEBUG, strLog); | |
152 | ||
153 | /* no more packets left and not a transmission, so we're done */ | |
154 | if (!m_message->IsTranmission() && m_iPacketsLeft == 0) | |
155 | { | |
156 | m_message->state = ADAPTER_MESSAGE_STATE_SENT_ACKED; | |
157 | m_message->response = message.packet; | |
158 | bSendSignal = true; | |
159 | } | |
160 | bHandled = true; | |
a75e3a5a LOK |
161 | } |
162 | } | |
8cdaa059 LOK |
163 | |
164 | if (bSendSignal) | |
165 | Signal(); | |
166 | ||
167 | return bHandled; | |
a75e3a5a LOK |
168 | } |
169 | ||
170 | bool CCECAdapterMessageQueueEntry::MessageReceivedTransmitSucceeded(const CCECAdapterMessage &message) | |
171 | { | |
a75e3a5a | 172 | { |
8cdaa059 LOK |
173 | CLockObject lock(m_mutex); |
174 | if (m_iPacketsLeft == 0) | |
175 | { | |
176 | /* transmission succeeded, so we're done */ | |
177 | CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - transmit succeeded", ToString()); | |
178 | m_message->state = ADAPTER_MESSAGE_STATE_SENT_ACKED; | |
179 | m_message->response = message.packet; | |
180 | } | |
181 | else | |
182 | { | |
183 | /* error, we expected more acks | |
184 | since the messages are processed in order, this should not happen, so this is an error situation */ | |
185 | CLibCEC::AddLog(CEC_LOG_WARNING, "%s - received 'transmit succeeded' but not enough 'command accepted' messages (%d left)", ToString(), m_iPacketsLeft); | |
186 | m_message->state = ADAPTER_MESSAGE_STATE_ERROR; | |
187 | } | |
a75e3a5a | 188 | } |
8cdaa059 LOK |
189 | |
190 | Signal(); | |
191 | ||
a75e3a5a LOK |
192 | return true; |
193 | } | |
194 | ||
195 | bool CCECAdapterMessageQueueEntry::MessageReceivedResponse(const CCECAdapterMessage &message) | |
196 | { | |
8cdaa059 LOK |
197 | { |
198 | CLockObject lock(m_mutex); | |
95acc41b | 199 | CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - received response - %s", ToString(), message.ToString().c_str()); |
8cdaa059 LOK |
200 | m_message->response = message.packet; |
201 | if (m_message->IsTranmission()) | |
202 | m_message->state = message.Message() == MSGCODE_TRANSMIT_SUCCEEDED ? ADAPTER_MESSAGE_STATE_SENT_ACKED : ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED; | |
203 | else | |
204 | m_message->state = ADAPTER_MESSAGE_STATE_SENT_ACKED; | |
205 | } | |
206 | ||
207 | Signal(); | |
208 | ||
a75e3a5a LOK |
209 | return true; |
210 | } | |
211 | ||
212 | ||
213 | CCECAdapterMessageQueue::~CCECAdapterMessageQueue(void) | |
214 | { | |
215 | Clear(); | |
a8559e01 | 216 | StopThread(0); |
a75e3a5a LOK |
217 | } |
218 | ||
219 | void CCECAdapterMessageQueue::Clear(void) | |
220 | { | |
a8559e01 | 221 | StopThread(5); |
a75e3a5a | 222 | CLockObject lock(m_mutex); |
a8559e01 | 223 | m_writeQueue.Clear(); |
8cdaa059 | 224 | m_messages.clear(); |
a75e3a5a LOK |
225 | } |
226 | ||
a8559e01 LOK |
227 | void *CCECAdapterMessageQueue::Process(void) |
228 | { | |
229 | CCECAdapterMessageQueueEntry *message(NULL); | |
230 | while (!IsStopped()) | |
231 | { | |
232 | /* wait for a new message */ | |
233 | if (m_writeQueue.Pop(message, 1000)) | |
234 | { | |
235 | /* write this message */ | |
236 | m_com->WriteToDevice(message->m_message); | |
237 | if (message->m_message->state == ADAPTER_MESSAGE_STATE_ERROR) | |
238 | { | |
239 | message->Signal(); | |
240 | Clear(); | |
241 | break; | |
242 | } | |
243 | } | |
244 | } | |
245 | return NULL; | |
246 | } | |
247 | ||
a75e3a5a LOK |
248 | void CCECAdapterMessageQueue::MessageReceived(const CCECAdapterMessage &msg) |
249 | { | |
8cdaa059 | 250 | bool bHandled(false); |
a75e3a5a | 251 | CLockObject lock(m_mutex); |
8cdaa059 LOK |
252 | /* send the received message to each entry in the queue until it is handled */ |
253 | for (map<uint64_t, CCECAdapterMessageQueueEntry *>::iterator it = m_messages.begin(); !bHandled && it != m_messages.end(); it++) | |
254 | bHandled = it->second->MessageReceived(msg); | |
a75e3a5a | 255 | |
8cdaa059 | 256 | if (!bHandled) |
a75e3a5a LOK |
257 | { |
258 | /* the message wasn't handled */ | |
259 | bool bIsError(m_com->HandlePoll(msg)); | |
260 | CLibCEC::AddLog(bIsError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString()); | |
261 | ||
262 | /* push this message to the current frame */ | |
263 | if (!bIsError && msg.PushToCecCommand(m_currentCECFrame)) | |
264 | { | |
265 | /* and push the current frame back over the callback method when a full command was received */ | |
266 | if (m_com->IsInitialised()) | |
267 | m_com->m_callback->OnCommandReceived(m_currentCECFrame); | |
268 | ||
269 | /* clear the current frame */ | |
270 | m_currentCECFrame.Clear(); | |
271 | } | |
272 | } | |
273 | } | |
274 | ||
275 | void CCECAdapterMessageQueue::AddData(uint8_t *data, size_t iLen) | |
276 | { | |
277 | for (size_t iPtr = 0; iPtr < iLen; iPtr++) | |
278 | { | |
279 | bool bFullMessage(false); | |
280 | { | |
281 | CLockObject lock(m_mutex); | |
282 | bFullMessage = m_incomingAdapterMessage.PushReceivedByte(data[iPtr]); | |
283 | } | |
284 | ||
285 | if (bFullMessage) | |
286 | { | |
287 | /* a full message was received */ | |
288 | CCECAdapterMessage newMessage; | |
289 | newMessage.packet = m_incomingAdapterMessage.packet; | |
290 | MessageReceived(newMessage); | |
291 | ||
292 | /* clear the current message */ | |
293 | CLockObject lock(m_mutex); | |
294 | m_incomingAdapterMessage.Clear(); | |
295 | } | |
296 | } | |
297 | } | |
298 | ||
299 | bool CCECAdapterMessageQueue::Write(CCECAdapterMessage *msg) | |
300 | { | |
301 | msg->state = ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT; | |
302 | ||
303 | /* set the correct line timeout */ | |
304 | if (msg->IsTranmission()) | |
305 | { | |
33dd87a9 | 306 | m_com->SetLineTimeout(msg->lineTimeout); |
a75e3a5a LOK |
307 | } |
308 | ||
309 | CCECAdapterMessageQueueEntry *entry(NULL); | |
8cdaa059 | 310 | uint64_t iEntryId(0); |
a75e3a5a LOK |
311 | /* add to the wait for ack queue */ |
312 | if (msg->Message() != MSGCODE_START_BOOTLOADER) | |
313 | { | |
8cdaa059 | 314 | CLockObject lock(m_mutex); |
a75e3a5a | 315 | entry = new CCECAdapterMessageQueueEntry(msg); |
8cdaa059 LOK |
316 | iEntryId = m_iNextMessage++; |
317 | m_messages.insert(make_pair(iEntryId, entry)); | |
a75e3a5a LOK |
318 | } |
319 | ||
a8559e01 LOK |
320 | /* add the message to the write queue */ |
321 | m_writeQueue.Push(entry); | |
a75e3a5a | 322 | |
8cdaa059 LOK |
323 | bool bReturn(true); |
324 | if (entry) | |
a75e3a5a | 325 | { |
8cdaa059 | 326 | if (!entry->Wait(msg->transmit_timeout <= 5 ? CEC_DEFAULT_TRANSMIT_WAIT : msg->transmit_timeout)) |
a75e3a5a | 327 | { |
8cdaa059 LOK |
328 | CLibCEC::AddLog(CEC_LOG_DEBUG, "command '%s' was not acked by the controller", CCECAdapterMessage::ToString(msg->Message())); |
329 | msg->state = ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED; | |
330 | bReturn = false; | |
a75e3a5a | 331 | } |
8cdaa059 LOK |
332 | |
333 | CLockObject lock(m_mutex); | |
334 | m_messages.erase(iEntryId); | |
4c2e665c | 335 | delete entry; |
a75e3a5a | 336 | } |
8cdaa059 LOK |
337 | |
338 | return bReturn; | |
a75e3a5a | 339 | } |