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/
33 #include "USBCECAdapterMessageQueue.h"
34 #include "USBCECAdapterCommunication.h"
35 #include "../platform/sockets/socket.h"
36 #include "../LibCEC.h"
39 using namespace PLATFORM
;
41 CCECAdapterMessageQueueEntry::CCECAdapterMessageQueueEntry(CCECAdapterMessage
*message
) :
43 m_iPacketsLeft(message
->IsTranmission() ? message
->Size() / 4 : 1),
47 CCECAdapterMessageQueueEntry::~CCECAdapterMessageQueueEntry(void) { }
49 void CCECAdapterMessageQueueEntry::Broadcast(void)
51 CLockObject
lock(m_mutex
);
52 m_condition
.Broadcast();
55 bool CCECAdapterMessageQueueEntry::MessageReceived(const CCECAdapterMessage
&message
)
57 bool bSendSignal(false);
60 PLATFORM::CLockObject
lock(m_mutex
);
61 if (!IsResponse(message
))
63 /* we received a message from the adapter that's not a response to this command */
64 if (!message
.IsTranmission())
66 /* we received something that's not a transmission while waiting for an ack to this command, so this command failed */
68 //TODO verify whether we're not failing too soon
69 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - %s - not a response %s - failed", __FUNCTION__
, ToString(), CCECAdapterMessage::ToString(message
.Message()));
70 m_message
->state
= ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED
;
76 /* we received a response, so this message is handled */
78 switch (message
.Message())
80 case MSGCODE_COMMAND_ACCEPTED
:
81 bSendSignal
= MessageReceivedCommandAccepted(message
);
83 case MSGCODE_TRANSMIT_SUCCEEDED
:
84 bSendSignal
= MessageReceivedTransmitSucceeded(message
);
87 bSendSignal
= MessageReceivedResponse(message
);
92 /* signal the waiting thread when we're done */
102 bool CCECAdapterMessageQueueEntry::Wait(uint32_t iTimeout
)
105 /* wait until we receive a signal when the tranmission succeeded */
107 CLockObject
lock(m_mutex
);
108 bReturn
= m_bSucceeded
? true : m_condition
.Wait(m_mutex
, m_bSucceeded
, iTimeout
);
114 bool CCECAdapterMessageQueueEntry::IsWaiting(void)
116 CLockObject
lock(m_mutex
);
120 cec_adapter_messagecode
CCECAdapterMessageQueueEntry::MessageCode(void)
122 return m_message
->Message();
125 bool CCECAdapterMessageQueueEntry::IsResponse(const CCECAdapterMessage
&msg
)
127 cec_adapter_messagecode msgCode
= msg
.Message();
128 return msgCode
== MessageCode() ||
129 msgCode
== MSGCODE_TIMEOUT_ERROR
||
130 msgCode
== MSGCODE_COMMAND_ACCEPTED
||
131 msgCode
== MSGCODE_COMMAND_REJECTED
||
132 (m_message
->IsTranmission() && msgCode
== MSGCODE_HIGH_ERROR
) ||
133 (m_message
->IsTranmission() && msgCode
== MSGCODE_LOW_ERROR
) ||
134 (m_message
->IsTranmission() && msgCode
== MSGCODE_RECEIVE_FAILED
) ||
135 (m_message
->IsTranmission() && msgCode
== MSGCODE_TRANSMIT_FAILED_LINE
) ||
136 (m_message
->IsTranmission() && msgCode
== MSGCODE_TRANSMIT_FAILED_ACK
) ||
137 (m_message
->IsTranmission() && msgCode
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
) ||
138 (m_message
->IsTranmission() && msgCode
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
) ||
139 (m_message
->IsTranmission() && msgCode
== MSGCODE_TRANSMIT_SUCCEEDED
);
142 const char *CCECAdapterMessageQueueEntry::ToString(void) const
144 /* CEC transmissions got the 'set ack polarity' msgcode, which doesn't look nice */
145 if (m_message
->IsTranmission())
146 return "CEC transmission";
148 return CCECAdapterMessage::ToString(m_message
->Message());
151 bool CCECAdapterMessageQueueEntry::MessageReceivedCommandAccepted(const CCECAdapterMessage
&message
)
153 bool bSendSignal(false);
154 if (m_iPacketsLeft
== 0)
156 /* we received a "command accepted", but we're not waiting for one anymore */
157 CLibCEC::AddLog(CEC_LOG_ERROR
, "%s - received unexpected 'command accepted' message", ToString());
158 m_message
->state
= ADAPTER_MESSAGE_STATE_ERROR
;
163 /* decrease number of acks we're waiting on by 1 */
164 if (m_iPacketsLeft
> 0)
167 /* log this message */
169 strLog
.Format("%s - command accepted", ToString());
170 if (m_iPacketsLeft
> 0)
171 strLog
.AppendFormat(" - waiting for %d more", m_iPacketsLeft
);
172 CLibCEC::AddLog(CEC_LOG_DEBUG
, strLog
);
174 /* no more packets left and not a transmission, so we're done */
175 if (!m_message
->IsTranmission() && m_iPacketsLeft
== 0)
177 m_message
->state
= ADAPTER_MESSAGE_STATE_SENT_ACKED
;
178 m_message
->response
= message
.packet
;
185 bool CCECAdapterMessageQueueEntry::MessageReceivedTransmitSucceeded(const CCECAdapterMessage
&message
)
187 if (m_iPacketsLeft
== 0)
189 /* transmission succeeded, so we're done */
190 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - transmit succeeded", ToString());
191 m_message
->state
= ADAPTER_MESSAGE_STATE_SENT_ACKED
;
192 m_message
->response
= message
.packet
;
196 /* error, we expected more acks */
197 CLibCEC::AddLog(CEC_LOG_WARNING
, "%s - received 'transmit succeeded' but not enough 'command accepted' messages (%d left)", ToString(), m_iPacketsLeft
);
198 m_message
->state
= ADAPTER_MESSAGE_STATE_ERROR
;
203 bool CCECAdapterMessageQueueEntry::MessageReceivedResponse(const CCECAdapterMessage
&message
)
205 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - received response", ToString());
206 m_message
->response
= message
.packet
;
207 if (m_message
->IsTranmission())
208 m_message
->state
= message
.Message() == MSGCODE_TRANSMIT_SUCCEEDED
? ADAPTER_MESSAGE_STATE_SENT_ACKED
: ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED
;
210 m_message
->state
= ADAPTER_MESSAGE_STATE_SENT_ACKED
;
215 CCECAdapterMessageQueue::~CCECAdapterMessageQueue(void)
220 void CCECAdapterMessageQueue::Clear(void)
222 CLockObject
lock(m_mutex
);
223 CCECAdapterMessageQueueEntry
*message(NULL
);
224 while (m_messages
.Pop(message
))
225 message
->Broadcast();
228 void CCECAdapterMessageQueue::MessageReceived(const CCECAdapterMessage
&msg
)
230 CLockObject
lock(m_mutex
);
231 CCECAdapterMessageQueueEntry
*message
= GetNextQueuedEntry();
233 /* send the received message to the first entry in the queue */
234 bool bHandled
= message
? message
->MessageReceived(msg
) : false;
236 if (!message
|| !bHandled
)
238 /* the message wasn't handled */
239 bool bIsError(m_com
->HandlePoll(msg
));
240 CLibCEC::AddLog(bIsError
? CEC_LOG_WARNING
: CEC_LOG_DEBUG
, msg
.ToString());
242 /* push this message to the current frame */
243 if (!bIsError
&& msg
.PushToCecCommand(m_currentCECFrame
))
245 /* and push the current frame back over the callback method when a full command was received */
246 if (m_com
->IsInitialised())
247 m_com
->m_callback
->OnCommandReceived(m_currentCECFrame
);
249 /* clear the current frame */
250 m_currentCECFrame
.Clear();
255 void CCECAdapterMessageQueue::AddData(uint8_t *data
, size_t iLen
)
257 for (size_t iPtr
= 0; iPtr
< iLen
; iPtr
++)
259 bool bFullMessage(false);
261 CLockObject
lock(m_mutex
);
262 bFullMessage
= m_incomingAdapterMessage
.PushReceivedByte(data
[iPtr
]);
267 /* a full message was received */
268 CCECAdapterMessage newMessage
;
269 newMessage
.packet
= m_incomingAdapterMessage
.packet
;
270 MessageReceived(newMessage
);
272 /* clear the current message */
273 CLockObject
lock(m_mutex
);
274 m_incomingAdapterMessage
.Clear();
279 bool CCECAdapterMessageQueue::Write(CCECAdapterMessage
*msg
)
281 msg
->state
= ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT
;
283 /* set the correct line timeout */
284 if (msg
->IsTranmission())
287 m_com
->SetLineTimeout(msg
->lineTimeout
);
289 m_com
->SetLineTimeout(msg
->retryTimeout
);
292 CCECAdapterMessageQueueEntry
*entry(NULL
);
293 /* add to the wait for ack queue */
294 if (msg
->Message() != MSGCODE_START_BOOTLOADER
)
296 entry
= new CCECAdapterMessageQueueEntry(msg
);
297 PLATFORM::CLockObject
lock(m_mutex
);
298 m_messages
.Push(entry
);
301 /* TODO write the message async */
302 if (!m_com
->WriteToDevice(msg
))
309 if (entry
&& !entry
->Wait(msg
->transmit_timeout
<= 5 ? CEC_DEFAULT_TRANSMIT_WAIT
: msg
->transmit_timeout
))
311 CLibCEC::AddLog(CEC_LOG_DEBUG
, "command '%s' was not acked by the controller", CCECAdapterMessage::ToString(msg
->Message()));
312 msg
->state
= ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED
;
319 CCECAdapterMessageQueueEntry
*CCECAdapterMessageQueue::GetNextQueuedEntry(void)
321 CCECAdapterMessageQueueEntry
*message(NULL
);
322 while (message
== NULL
&& m_messages
.Peek(message
))
324 if (!message
->IsWaiting())
326 /* delete old messages */
327 m_messages
.Pop(message
);