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 | ||
2b44051c | 33 | #include "env.h" |
a75e3a5a | 34 | #include "USBCECAdapterMessageQueue.h" |
2b44051c | 35 | |
a75e3a5a | 36 | #include "USBCECAdapterCommunication.h" |
2b44051c LOK |
37 | #include "USBCECAdapterMessage.h" |
38 | #include "lib/platform/sockets/socket.h" | |
39 | #include "lib/LibCEC.h" | |
40 | #include "lib/platform/util/StdString.h" | |
a75e3a5a LOK |
41 | |
42 | using namespace CEC; | |
43 | using namespace PLATFORM; | |
8cdaa059 | 44 | using namespace std; |
a75e3a5a | 45 | |
b32ffd87 LOK |
46 | #define MESSAGE_QUEUE_SIGNAL_WAIT_TIME 1000 |
47 | ||
004b8382 LOK |
48 | CCECAdapterMessageQueueEntry::CCECAdapterMessageQueueEntry(CCECAdapterMessageQueue *queue, CCECAdapterMessage *message) : |
49 | m_queue(queue), | |
a75e3a5a LOK |
50 | m_message(message), |
51 | m_iPacketsLeft(message->IsTranmission() ? message->Size() / 4 : 1), | |
52 | m_bSucceeded(false), | |
daec0320 LOK |
53 | m_bWaiting(true), |
54 | m_queueTimeout(message->transmit_timeout) {} | |
a75e3a5a LOK |
55 | |
56 | CCECAdapterMessageQueueEntry::~CCECAdapterMessageQueueEntry(void) { } | |
57 | ||
58 | void CCECAdapterMessageQueueEntry::Broadcast(void) | |
59 | { | |
60 | CLockObject lock(m_mutex); | |
61 | m_condition.Broadcast(); | |
62 | } | |
63 | ||
64 | bool CCECAdapterMessageQueueEntry::MessageReceived(const CCECAdapterMessage &message) | |
65 | { | |
a75e3a5a LOK |
66 | bool bHandled(false); |
67 | ||
8cdaa059 | 68 | if (IsResponse(message)) |
a75e3a5a | 69 | { |
a75e3a5a LOK |
70 | switch (message.Message()) |
71 | { | |
72 | case MSGCODE_COMMAND_ACCEPTED: | |
8cdaa059 | 73 | bHandled = MessageReceivedCommandAccepted(message); |
a75e3a5a LOK |
74 | break; |
75 | case MSGCODE_TRANSMIT_SUCCEEDED: | |
8cdaa059 | 76 | bHandled = MessageReceivedTransmitSucceeded(message); |
a75e3a5a LOK |
77 | break; |
78 | default: | |
8cdaa059 | 79 | bHandled = MessageReceivedResponse(message); |
a75e3a5a LOK |
80 | break; |
81 | } | |
82 | } | |
83 | ||
a75e3a5a LOK |
84 | return bHandled; |
85 | } | |
86 | ||
8cdaa059 LOK |
87 | void CCECAdapterMessageQueueEntry::Signal(void) |
88 | { | |
89 | CLockObject lock(m_mutex); | |
90 | m_bSucceeded = true; | |
91 | m_condition.Signal(); | |
92 | } | |
93 | ||
a75e3a5a LOK |
94 | bool CCECAdapterMessageQueueEntry::Wait(uint32_t iTimeout) |
95 | { | |
96 | bool bReturn(false); | |
97 | /* wait until we receive a signal when the tranmission succeeded */ | |
98 | { | |
99 | CLockObject lock(m_mutex); | |
100 | bReturn = m_bSucceeded ? true : m_condition.Wait(m_mutex, m_bSucceeded, iTimeout); | |
101 | m_bWaiting = false; | |
102 | } | |
103 | return bReturn; | |
104 | } | |
105 | ||
106 | bool CCECAdapterMessageQueueEntry::IsWaiting(void) | |
107 | { | |
108 | CLockObject lock(m_mutex); | |
109 | return m_bWaiting; | |
110 | } | |
111 | ||
112 | cec_adapter_messagecode CCECAdapterMessageQueueEntry::MessageCode(void) | |
113 | { | |
114 | return m_message->Message(); | |
115 | } | |
116 | ||
3ead056c | 117 | bool CCECAdapterMessageQueueEntry::IsResponseOld(const CCECAdapterMessage &msg) |
a75e3a5a LOK |
118 | { |
119 | cec_adapter_messagecode msgCode = msg.Message(); | |
3ead056c | 120 | |
a75e3a5a | 121 | return msgCode == MessageCode() || |
a75e3a5a LOK |
122 | msgCode == MSGCODE_COMMAND_ACCEPTED || |
123 | msgCode == MSGCODE_COMMAND_REJECTED || | |
3ead056c LOK |
124 | (m_message->IsTranmission() && (msgCode == MSGCODE_TIMEOUT_ERROR || |
125 | msgCode == MSGCODE_HIGH_ERROR || | |
126 | msgCode == MSGCODE_LOW_ERROR || | |
127 | msgCode == MSGCODE_RECEIVE_FAILED || | |
128 | msgCode == MSGCODE_TRANSMIT_FAILED_LINE || | |
129 | msgCode == MSGCODE_TRANSMIT_FAILED_ACK || | |
130 | msgCode == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA || | |
131 | msgCode == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE || | |
132 | msgCode == MSGCODE_TRANSMIT_SUCCEEDED)); | |
133 | } | |
134 | ||
135 | bool CCECAdapterMessageQueueEntry::IsResponse(const CCECAdapterMessage &msg) | |
136 | { | |
137 | cec_adapter_messagecode thisMsgCode = m_message->Message(); | |
138 | cec_adapter_messagecode msgCode = msg.Message(); | |
139 | cec_adapter_messagecode msgResponse = msg.ResponseTo(); | |
140 | ||
141 | // msgcode matches, always a response | |
142 | if (msgCode == MessageCode()) | |
143 | return true; | |
144 | ||
145 | if (!ProvidesExtendedResponse()) | |
146 | return IsResponseOld(msg); | |
147 | ||
148 | // response without a msgcode | |
149 | if (msgResponse == MSGCODE_NOTHING) | |
9f236526 | 150 | return false; |
3ead056c LOK |
151 | |
152 | // commands that only repond with accepted/rejected | |
153 | if (thisMsgCode == MSGCODE_PING || | |
154 | thisMsgCode == MSGCODE_SET_ACK_MASK || | |
155 | thisMsgCode == MSGCODE_SET_CONTROLLED || | |
156 | thisMsgCode == MSGCODE_SET_AUTO_ENABLED || | |
157 | thisMsgCode == MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS || | |
158 | thisMsgCode == MSGCODE_SET_LOGICAL_ADDRESS_MASK || | |
159 | thisMsgCode == MSGCODE_SET_PHYSICAL_ADDRESS || | |
160 | thisMsgCode == MSGCODE_SET_DEVICE_TYPE || | |
161 | thisMsgCode == MSGCODE_SET_HDMI_VERSION || | |
162 | thisMsgCode == MSGCODE_SET_OSD_NAME || | |
163 | thisMsgCode == MSGCODE_WRITE_EEPROM || | |
164 | thisMsgCode == MSGCODE_TRANSMIT_IDLETIME) | |
165 | return thisMsgCode == msgResponse; | |
166 | ||
167 | if (!m_message->IsTranmission()) | |
168 | { | |
169 | m_queue->m_com->m_callback->GetLib()->AddLog(CEC_LOG_WARNING, "FIXME! not a transmission"); | |
170 | return false; | |
171 | } | |
172 | ||
173 | return ((msgCode == MSGCODE_COMMAND_ACCEPTED || msgCode == MSGCODE_COMMAND_REJECTED) && | |
174 | (msgResponse == MSGCODE_TRANSMIT_ACK_POLARITY || msgResponse == MSGCODE_TRANSMIT || msgResponse == MSGCODE_TRANSMIT_EOM)) || | |
175 | msgCode == MSGCODE_TIMEOUT_ERROR || | |
3ead056c | 176 | msgCode == MSGCODE_RECEIVE_FAILED || |
3ead056c LOK |
177 | msgCode == MSGCODE_TRANSMIT_FAILED_ACK || |
178 | msgCode == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA || | |
179 | msgCode == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE || | |
180 | msgCode == MSGCODE_TRANSMIT_SUCCEEDED; | |
a75e3a5a LOK |
181 | } |
182 | ||
183 | const char *CCECAdapterMessageQueueEntry::ToString(void) const | |
184 | { | |
185 | /* CEC transmissions got the 'set ack polarity' msgcode, which doesn't look nice */ | |
186 | if (m_message->IsTranmission()) | |
187 | return "CEC transmission"; | |
188 | else | |
189 | return CCECAdapterMessage::ToString(m_message->Message()); | |
190 | } | |
191 | ||
192 | bool CCECAdapterMessageQueueEntry::MessageReceivedCommandAccepted(const CCECAdapterMessage &message) | |
193 | { | |
194 | bool bSendSignal(false); | |
8cdaa059 | 195 | bool bHandled(false); |
a75e3a5a | 196 | { |
8cdaa059 | 197 | CLockObject lock(m_mutex); |
a75e3a5a | 198 | if (m_iPacketsLeft > 0) |
8cdaa059 LOK |
199 | { |
200 | /* decrease by 1 */ | |
a75e3a5a LOK |
201 | m_iPacketsLeft--; |
202 | ||
8cdaa059 LOK |
203 | /* log this message */ |
204 | CStdString strLog; | |
205 | strLog.Format("%s - command accepted", ToString()); | |
206 | if (m_iPacketsLeft > 0) | |
207 | strLog.AppendFormat(" - waiting for %d more", m_iPacketsLeft); | |
004b8382 | 208 | m_queue->m_com->m_callback->GetLib()->AddLog(CEC_LOG_DEBUG, strLog); |
8cdaa059 LOK |
209 | |
210 | /* no more packets left and not a transmission, so we're done */ | |
211 | if (!m_message->IsTranmission() && m_iPacketsLeft == 0) | |
212 | { | |
213 | m_message->state = ADAPTER_MESSAGE_STATE_SENT_ACKED; | |
214 | m_message->response = message.packet; | |
215 | bSendSignal = true; | |
216 | } | |
217 | bHandled = true; | |
a75e3a5a LOK |
218 | } |
219 | } | |
8cdaa059 LOK |
220 | |
221 | if (bSendSignal) | |
222 | Signal(); | |
223 | ||
224 | return bHandled; | |
a75e3a5a LOK |
225 | } |
226 | ||
227 | bool CCECAdapterMessageQueueEntry::MessageReceivedTransmitSucceeded(const CCECAdapterMessage &message) | |
228 | { | |
a75e3a5a | 229 | { |
8cdaa059 LOK |
230 | CLockObject lock(m_mutex); |
231 | if (m_iPacketsLeft == 0) | |
232 | { | |
233 | /* transmission succeeded, so we're done */ | |
004b8382 | 234 | m_queue->m_com->m_callback->GetLib()->AddLog(CEC_LOG_DEBUG, "%s - transmit succeeded", ToString()); |
8cdaa059 LOK |
235 | m_message->state = ADAPTER_MESSAGE_STATE_SENT_ACKED; |
236 | m_message->response = message.packet; | |
237 | } | |
238 | else | |
239 | { | |
240 | /* error, we expected more acks | |
241 | since the messages are processed in order, this should not happen, so this is an error situation */ | |
004b8382 | 242 | m_queue->m_com->m_callback->GetLib()->AddLog(CEC_LOG_WARNING, "%s - received 'transmit succeeded' but not enough 'command accepted' messages (%d left)", ToString(), m_iPacketsLeft); |
8cdaa059 LOK |
243 | m_message->state = ADAPTER_MESSAGE_STATE_ERROR; |
244 | } | |
a75e3a5a | 245 | } |
8cdaa059 LOK |
246 | |
247 | Signal(); | |
248 | ||
a75e3a5a LOK |
249 | return true; |
250 | } | |
251 | ||
252 | bool CCECAdapterMessageQueueEntry::MessageReceivedResponse(const CCECAdapterMessage &message) | |
253 | { | |
8cdaa059 LOK |
254 | { |
255 | CLockObject lock(m_mutex); | |
004b8382 | 256 | m_queue->m_com->m_callback->GetLib()->AddLog(CEC_LOG_DEBUG, "%s - received response - %s", ToString(), message.ToString().c_str()); |
8cdaa059 LOK |
257 | m_message->response = message.packet; |
258 | if (m_message->IsTranmission()) | |
259 | m_message->state = message.Message() == MSGCODE_TRANSMIT_SUCCEEDED ? ADAPTER_MESSAGE_STATE_SENT_ACKED : ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED; | |
260 | else | |
261 | m_message->state = ADAPTER_MESSAGE_STATE_SENT_ACKED; | |
262 | } | |
263 | ||
264 | Signal(); | |
265 | ||
a75e3a5a LOK |
266 | return true; |
267 | } | |
268 | ||
3ead056c LOK |
269 | bool CCECAdapterMessageQueueEntry::ProvidesExtendedResponse(void) |
270 | { | |
271 | return m_queue && m_queue->ProvidesExtendedResponse(); | |
272 | } | |
273 | ||
daec0320 LOK |
274 | bool CCECAdapterMessageQueueEntry::TimedOutOrSucceeded(void) const |
275 | { | |
276 | return m_message->bFireAndForget && (m_bSucceeded || m_queueTimeout.TimeLeft() == 0); | |
277 | } | |
278 | ||
2b44051c LOK |
279 | CCECAdapterMessageQueue::CCECAdapterMessageQueue(CUSBCECAdapterCommunication *com) : |
280 | PLATFORM::CThread(), | |
281 | m_com(com), | |
282 | m_iNextMessage(0) | |
283 | { | |
284 | m_incomingAdapterMessage = new CCECAdapterMessage; | |
285 | m_currentCECFrame.Clear(); | |
286 | } | |
a75e3a5a LOK |
287 | |
288 | CCECAdapterMessageQueue::~CCECAdapterMessageQueue(void) | |
289 | { | |
290 | Clear(); | |
a8559e01 | 291 | StopThread(0); |
2b44051c | 292 | delete m_incomingAdapterMessage; |
a75e3a5a LOK |
293 | } |
294 | ||
295 | void CCECAdapterMessageQueue::Clear(void) | |
296 | { | |
a8559e01 | 297 | StopThread(5); |
a75e3a5a | 298 | CLockObject lock(m_mutex); |
a8559e01 | 299 | m_writeQueue.Clear(); |
8cdaa059 | 300 | m_messages.clear(); |
a75e3a5a LOK |
301 | } |
302 | ||
a8559e01 LOK |
303 | void *CCECAdapterMessageQueue::Process(void) |
304 | { | |
305 | CCECAdapterMessageQueueEntry *message(NULL); | |
306 | while (!IsStopped()) | |
307 | { | |
308 | /* wait for a new message */ | |
55c75e6e | 309 | if (m_writeQueue.Pop(message, MESSAGE_QUEUE_SIGNAL_WAIT_TIME) && message) |
a8559e01 LOK |
310 | { |
311 | /* write this message */ | |
55c75e6e LOK |
312 | { |
313 | CLockObject lock(m_mutex); | |
314 | m_com->WriteToDevice(message->m_message); | |
315 | } | |
316 | if (message->m_message->state == ADAPTER_MESSAGE_STATE_ERROR || | |
317 | message->m_message->Message() == MSGCODE_START_BOOTLOADER) | |
a8559e01 LOK |
318 | { |
319 | message->Signal(); | |
320 | Clear(); | |
321 | break; | |
322 | } | |
323 | } | |
daec0320 LOK |
324 | |
325 | CheckTimedOutMessages(); | |
a8559e01 LOK |
326 | } |
327 | return NULL; | |
328 | } | |
329 | ||
daec0320 LOK |
330 | void CCECAdapterMessageQueue::CheckTimedOutMessages(void) |
331 | { | |
332 | CLockObject lock(m_mutex); | |
333 | vector<uint64_t> timedOut; | |
334 | for (map<uint64_t, CCECAdapterMessageQueueEntry *>::iterator it = m_messages.begin(); it != m_messages.end(); it++) | |
335 | { | |
336 | if (it->second->TimedOutOrSucceeded()) | |
337 | { | |
338 | timedOut.push_back(it->first); | |
339 | if (!it->second->m_bSucceeded) | |
340 | m_com->m_callback->GetLib()->AddLog(CEC_LOG_DEBUG, "command '%s' was not acked by the controller", CCECAdapterMessage::ToString(it->second->m_message->Message())); | |
341 | delete it->second->m_message; | |
342 | delete it->second; | |
343 | } | |
344 | } | |
345 | ||
346 | for (vector<uint64_t>::iterator it = timedOut.begin(); it != timedOut.end(); it++) | |
347 | { | |
348 | uint64_t iEntryId = *it; | |
349 | m_messages.erase(iEntryId); | |
350 | } | |
351 | } | |
352 | ||
a75e3a5a LOK |
353 | void CCECAdapterMessageQueue::MessageReceived(const CCECAdapterMessage &msg) |
354 | { | |
8cdaa059 | 355 | bool bHandled(false); |
a75e3a5a | 356 | CLockObject lock(m_mutex); |
8cdaa059 LOK |
357 | /* send the received message to each entry in the queue until it is handled */ |
358 | for (map<uint64_t, CCECAdapterMessageQueueEntry *>::iterator it = m_messages.begin(); !bHandled && it != m_messages.end(); it++) | |
359 | bHandled = it->second->MessageReceived(msg); | |
a75e3a5a | 360 | |
8cdaa059 | 361 | if (!bHandled) |
a75e3a5a LOK |
362 | { |
363 | /* the message wasn't handled */ | |
364 | bool bIsError(m_com->HandlePoll(msg)); | |
2b44051c | 365 | m_com->m_callback->GetLib()->AddLog(bIsError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString().c_str()); |
a75e3a5a LOK |
366 | |
367 | /* push this message to the current frame */ | |
368 | if (!bIsError && msg.PushToCecCommand(m_currentCECFrame)) | |
369 | { | |
370 | /* and push the current frame back over the callback method when a full command was received */ | |
371 | if (m_com->IsInitialised()) | |
372 | m_com->m_callback->OnCommandReceived(m_currentCECFrame); | |
373 | ||
374 | /* clear the current frame */ | |
375 | m_currentCECFrame.Clear(); | |
376 | } | |
377 | } | |
378 | } | |
379 | ||
380 | void CCECAdapterMessageQueue::AddData(uint8_t *data, size_t iLen) | |
381 | { | |
382 | for (size_t iPtr = 0; iPtr < iLen; iPtr++) | |
383 | { | |
384 | bool bFullMessage(false); | |
385 | { | |
386 | CLockObject lock(m_mutex); | |
2b44051c | 387 | bFullMessage = m_incomingAdapterMessage->PushReceivedByte(data[iPtr]); |
a75e3a5a LOK |
388 | } |
389 | ||
390 | if (bFullMessage) | |
391 | { | |
392 | /* a full message was received */ | |
393 | CCECAdapterMessage newMessage; | |
2b44051c | 394 | newMessage.packet = m_incomingAdapterMessage->packet; |
a75e3a5a LOK |
395 | MessageReceived(newMessage); |
396 | ||
397 | /* clear the current message */ | |
398 | CLockObject lock(m_mutex); | |
2b44051c | 399 | m_incomingAdapterMessage->Clear(); |
a75e3a5a LOK |
400 | } |
401 | } | |
402 | } | |
403 | ||
404 | bool CCECAdapterMessageQueue::Write(CCECAdapterMessage *msg) | |
405 | { | |
406 | msg->state = ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT; | |
407 | ||
408 | /* set the correct line timeout */ | |
409 | if (msg->IsTranmission()) | |
410 | { | |
33dd87a9 | 411 | m_com->SetLineTimeout(msg->lineTimeout); |
a75e3a5a LOK |
412 | } |
413 | ||
004b8382 | 414 | CCECAdapterMessageQueueEntry *entry = new CCECAdapterMessageQueueEntry(this, msg); |
daec0320 LOK |
415 | if (!entry) |
416 | { | |
417 | m_com->m_callback->GetLib()->AddLog(CEC_LOG_ERROR, "couldn't create queue entry for '%s'", CCECAdapterMessage::ToString(msg->Message())); | |
418 | msg->state = ADAPTER_MESSAGE_STATE_ERROR; | |
419 | return false; | |
420 | } | |
421 | ||
8cdaa059 | 422 | uint64_t iEntryId(0); |
a75e3a5a LOK |
423 | /* add to the wait for ack queue */ |
424 | if (msg->Message() != MSGCODE_START_BOOTLOADER) | |
425 | { | |
8cdaa059 | 426 | CLockObject lock(m_mutex); |
8cdaa059 LOK |
427 | iEntryId = m_iNextMessage++; |
428 | m_messages.insert(make_pair(iEntryId, entry)); | |
a75e3a5a LOK |
429 | } |
430 | ||
a8559e01 LOK |
431 | /* add the message to the write queue */ |
432 | m_writeQueue.Push(entry); | |
a75e3a5a | 433 | |
8cdaa059 | 434 | bool bReturn(true); |
daec0320 | 435 | if (!msg->bFireAndForget) |
a75e3a5a | 436 | { |
8cdaa059 | 437 | if (!entry->Wait(msg->transmit_timeout <= 5 ? CEC_DEFAULT_TRANSMIT_WAIT : msg->transmit_timeout)) |
a75e3a5a | 438 | { |
004b8382 | 439 | m_com->m_callback->GetLib()->AddLog(CEC_LOG_DEBUG, "command '%s' was not acked by the controller", CCECAdapterMessage::ToString(msg->Message())); |
8cdaa059 LOK |
440 | msg->state = ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED; |
441 | bReturn = false; | |
a75e3a5a | 442 | } |
8cdaa059 | 443 | |
55c75e6e LOK |
444 | if (msg->Message() != MSGCODE_START_BOOTLOADER) |
445 | { | |
446 | CLockObject lock(m_mutex); | |
447 | m_messages.erase(iEntryId); | |
448 | } | |
815dbda2 LOK |
449 | |
450 | if (msg->ReplyIsError()) | |
451 | msg->state = ADAPTER_MESSAGE_STATE_ERROR; | |
452 | ||
4c2e665c | 453 | delete entry; |
a75e3a5a | 454 | } |
8cdaa059 LOK |
455 | |
456 | return bReturn; | |
a75e3a5a | 457 | } |
3ead056c LOK |
458 | |
459 | bool CCECAdapterMessageQueue::ProvidesExtendedResponse(void) | |
460 | { | |
461 | return m_com && m_com->ProvidesExtendedResponse(); | |
462 | } |