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