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