2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
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.
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.
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.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
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/
34 #include "USBCECAdapterMessageQueue.h"
36 #include "USBCECAdapterCommunication.h"
37 #include "USBCECAdapterMessage.h"
38 #include "lib/platform/sockets/socket.h"
39 #include "lib/LibCEC.h"
40 #include "lib/platform/util/StdString.h"
43 using namespace PLATFORM
;
46 #define MESSAGE_QUEUE_SIGNAL_WAIT_TIME 1000
48 CCECAdapterMessageQueueEntry::CCECAdapterMessageQueueEntry(CCECAdapterMessageQueue
*queue
, CCECAdapterMessage
*message
) :
51 m_iPacketsLeft(message
->IsTranmission() ? message
->Size() / 4 : 1),
55 CCECAdapterMessageQueueEntry::~CCECAdapterMessageQueueEntry(void) { }
57 void CCECAdapterMessageQueueEntry::Broadcast(void)
59 CLockObject
lock(m_mutex
);
60 m_condition
.Broadcast();
63 bool CCECAdapterMessageQueueEntry::MessageReceived(const CCECAdapterMessage
&message
)
67 if (IsResponse(message
))
69 switch (message
.Message())
71 case MSGCODE_COMMAND_ACCEPTED
:
72 bHandled
= MessageReceivedCommandAccepted(message
);
74 case MSGCODE_TRANSMIT_SUCCEEDED
:
75 bHandled
= MessageReceivedTransmitSucceeded(message
);
78 bHandled
= MessageReceivedResponse(message
);
86 void CCECAdapterMessageQueueEntry::Signal(void)
88 CLockObject
lock(m_mutex
);
93 bool CCECAdapterMessageQueueEntry::Wait(uint32_t iTimeout
)
96 /* wait until we receive a signal when the tranmission succeeded */
98 CLockObject
lock(m_mutex
);
99 bReturn
= m_bSucceeded
? true : m_condition
.Wait(m_mutex
, m_bSucceeded
, iTimeout
);
105 bool CCECAdapterMessageQueueEntry::IsWaiting(void)
107 CLockObject
lock(m_mutex
);
111 cec_adapter_messagecode
CCECAdapterMessageQueueEntry::MessageCode(void)
113 return m_message
->Message();
116 bool CCECAdapterMessageQueueEntry::IsResponse(const CCECAdapterMessage
&msg
)
118 cec_adapter_messagecode msgCode
= msg
.Message();
119 return msgCode
== MessageCode() ||
120 (m_message
->IsTranmission() && msgCode
== MSGCODE_TIMEOUT_ERROR
) ||
121 msgCode
== MSGCODE_COMMAND_ACCEPTED
||
122 msgCode
== MSGCODE_COMMAND_REJECTED
||
123 (m_message
->IsTranmission() && msgCode
== MSGCODE_HIGH_ERROR
) ||
124 (m_message
->IsTranmission() && msgCode
== MSGCODE_LOW_ERROR
) ||
125 (m_message
->IsTranmission() && msgCode
== MSGCODE_RECEIVE_FAILED
) ||
126 (m_message
->IsTranmission() && msgCode
== MSGCODE_TRANSMIT_FAILED_LINE
) ||
127 (m_message
->IsTranmission() && msgCode
== MSGCODE_TRANSMIT_FAILED_ACK
) ||
128 (m_message
->IsTranmission() && msgCode
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
) ||
129 (m_message
->IsTranmission() && msgCode
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
) ||
130 (m_message
->IsTranmission() && msgCode
== MSGCODE_TRANSMIT_SUCCEEDED
);
133 const char *CCECAdapterMessageQueueEntry::ToString(void) const
135 /* CEC transmissions got the 'set ack polarity' msgcode, which doesn't look nice */
136 if (m_message
->IsTranmission())
137 return "CEC transmission";
139 return CCECAdapterMessage::ToString(m_message
->Message());
142 bool CCECAdapterMessageQueueEntry::MessageReceivedCommandAccepted(const CCECAdapterMessage
&message
)
144 bool bSendSignal(false);
145 bool bHandled(false);
147 CLockObject
lock(m_mutex
);
148 if (m_iPacketsLeft
> 0)
153 /* log this message */
155 strLog
.Format("%s - command accepted", ToString());
156 if (m_iPacketsLeft
> 0)
157 strLog
.AppendFormat(" - waiting for %d more", m_iPacketsLeft
);
158 m_queue
->m_com
->m_callback
->GetLib()->AddLog(CEC_LOG_DEBUG
, strLog
);
160 /* no more packets left and not a transmission, so we're done */
161 if (!m_message
->IsTranmission() && m_iPacketsLeft
== 0)
163 m_message
->state
= ADAPTER_MESSAGE_STATE_SENT_ACKED
;
164 m_message
->response
= message
.packet
;
177 bool CCECAdapterMessageQueueEntry::MessageReceivedTransmitSucceeded(const CCECAdapterMessage
&message
)
180 CLockObject
lock(m_mutex
);
181 if (m_iPacketsLeft
== 0)
183 /* transmission succeeded, so we're done */
184 m_queue
->m_com
->m_callback
->GetLib()->AddLog(CEC_LOG_DEBUG
, "%s - transmit succeeded", ToString());
185 m_message
->state
= ADAPTER_MESSAGE_STATE_SENT_ACKED
;
186 m_message
->response
= message
.packet
;
190 /* error, we expected more acks
191 since the messages are processed in order, this should not happen, so this is an error situation */
192 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
);
193 m_message
->state
= ADAPTER_MESSAGE_STATE_ERROR
;
202 bool CCECAdapterMessageQueueEntry::MessageReceivedResponse(const CCECAdapterMessage
&message
)
205 CLockObject
lock(m_mutex
);
206 m_queue
->m_com
->m_callback
->GetLib()->AddLog(CEC_LOG_DEBUG
, "%s - received response - %s", ToString(), message
.ToString().c_str());
207 m_message
->response
= message
.packet
;
208 if (m_message
->IsTranmission())
209 m_message
->state
= message
.Message() == MSGCODE_TRANSMIT_SUCCEEDED
? ADAPTER_MESSAGE_STATE_SENT_ACKED
: ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED
;
211 m_message
->state
= ADAPTER_MESSAGE_STATE_SENT_ACKED
;
219 CCECAdapterMessageQueue::CCECAdapterMessageQueue(CUSBCECAdapterCommunication
*com
) :
224 m_incomingAdapterMessage
= new CCECAdapterMessage
;
225 m_currentCECFrame
.Clear();
228 CCECAdapterMessageQueue::~CCECAdapterMessageQueue(void)
232 delete m_incomingAdapterMessage
;
235 void CCECAdapterMessageQueue::Clear(void)
238 CLockObject
lock(m_mutex
);
239 m_writeQueue
.Clear();
243 void *CCECAdapterMessageQueue::Process(void)
245 CCECAdapterMessageQueueEntry
*message(NULL
);
248 /* wait for a new message */
249 if (m_writeQueue
.Pop(message
, MESSAGE_QUEUE_SIGNAL_WAIT_TIME
) && message
)
251 /* write this message */
253 CLockObject
lock(m_mutex
);
254 m_com
->WriteToDevice(message
->m_message
);
256 if (message
->m_message
->state
== ADAPTER_MESSAGE_STATE_ERROR
||
257 message
->m_message
->Message() == MSGCODE_START_BOOTLOADER
)
268 void CCECAdapterMessageQueue::MessageReceived(const CCECAdapterMessage
&msg
)
270 bool bHandled(false);
271 CLockObject
lock(m_mutex
);
272 /* send the received message to each entry in the queue until it is handled */
273 for (map
<uint64_t, CCECAdapterMessageQueueEntry
*>::iterator it
= m_messages
.begin(); !bHandled
&& it
!= m_messages
.end(); it
++)
274 bHandled
= it
->second
->MessageReceived(msg
);
278 /* the message wasn't handled */
279 bool bIsError(m_com
->HandlePoll(msg
));
280 m_com
->m_callback
->GetLib()->AddLog(bIsError
? CEC_LOG_WARNING
: CEC_LOG_DEBUG
, msg
.ToString().c_str());
282 /* push this message to the current frame */
283 if (!bIsError
&& msg
.PushToCecCommand(m_currentCECFrame
))
285 /* and push the current frame back over the callback method when a full command was received */
286 if (m_com
->IsInitialised())
287 m_com
->m_callback
->OnCommandReceived(m_currentCECFrame
);
289 /* clear the current frame */
290 m_currentCECFrame
.Clear();
295 void CCECAdapterMessageQueue::AddData(uint8_t *data
, size_t iLen
)
297 for (size_t iPtr
= 0; iPtr
< iLen
; iPtr
++)
299 bool bFullMessage(false);
301 CLockObject
lock(m_mutex
);
302 bFullMessage
= m_incomingAdapterMessage
->PushReceivedByte(data
[iPtr
]);
307 /* a full message was received */
308 CCECAdapterMessage newMessage
;
309 newMessage
.packet
= m_incomingAdapterMessage
->packet
;
310 MessageReceived(newMessage
);
312 /* clear the current message */
313 CLockObject
lock(m_mutex
);
314 m_incomingAdapterMessage
->Clear();
319 bool CCECAdapterMessageQueue::Write(CCECAdapterMessage
*msg
)
321 msg
->state
= ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT
;
323 /* set the correct line timeout */
324 if (msg
->IsTranmission())
326 m_com
->SetLineTimeout(msg
->lineTimeout
);
329 CCECAdapterMessageQueueEntry
*entry
= new CCECAdapterMessageQueueEntry(this, msg
);
330 uint64_t iEntryId(0);
331 /* add to the wait for ack queue */
332 if (msg
->Message() != MSGCODE_START_BOOTLOADER
)
334 CLockObject
lock(m_mutex
);
335 iEntryId
= m_iNextMessage
++;
336 m_messages
.insert(make_pair(iEntryId
, entry
));
339 /* add the message to the write queue */
340 m_writeQueue
.Push(entry
);
345 if (!entry
->Wait(msg
->transmit_timeout
<= 5 ? CEC_DEFAULT_TRANSMIT_WAIT
: msg
->transmit_timeout
))
347 m_com
->m_callback
->GetLib()->AddLog(CEC_LOG_DEBUG
, "command '%s' was not acked by the controller", CCECAdapterMessage::ToString(msg
->Message()));
348 msg
->state
= ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED
;
352 if (msg
->Message() != MSGCODE_START_BOOTLOADER
)
354 CLockObject
lock(m_mutex
);
355 m_messages
.erase(iEntryId
);