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
;
42 CCECAdapterMessageQueueEntry::CCECAdapterMessageQueueEntry(CCECAdapterMessage
*message
) :
44 m_iPacketsLeft(message
->IsTranmission() ? message
->Size() / 4 : 1),
48 CCECAdapterMessageQueueEntry::~CCECAdapterMessageQueueEntry(void) { }
50 void CCECAdapterMessageQueueEntry::Broadcast(void)
52 CLockObject
lock(m_mutex
);
53 m_condition
.Broadcast();
56 bool CCECAdapterMessageQueueEntry::MessageReceived(const CCECAdapterMessage
&message
)
60 if (IsResponse(message
))
62 switch (message
.Message())
64 case MSGCODE_COMMAND_ACCEPTED
:
65 bHandled
= MessageReceivedCommandAccepted(message
);
67 case MSGCODE_TRANSMIT_SUCCEEDED
:
68 bHandled
= MessageReceivedTransmitSucceeded(message
);
71 bHandled
= MessageReceivedResponse(message
);
79 void CCECAdapterMessageQueueEntry::Signal(void)
81 CLockObject
lock(m_mutex
);
86 bool CCECAdapterMessageQueueEntry::Wait(uint32_t iTimeout
)
89 /* wait until we receive a signal when the tranmission succeeded */
91 CLockObject
lock(m_mutex
);
92 bReturn
= m_bSucceeded
? true : m_condition
.Wait(m_mutex
, m_bSucceeded
, iTimeout
);
98 bool CCECAdapterMessageQueueEntry::IsWaiting(void)
100 CLockObject
lock(m_mutex
);
104 cec_adapter_messagecode
CCECAdapterMessageQueueEntry::MessageCode(void)
106 return m_message
->Message();
109 bool CCECAdapterMessageQueueEntry::IsResponse(const CCECAdapterMessage
&msg
)
111 cec_adapter_messagecode msgCode
= msg
.Message();
112 return msgCode
== MessageCode() ||
113 msgCode
== MSGCODE_TIMEOUT_ERROR
||
114 msgCode
== MSGCODE_COMMAND_ACCEPTED
||
115 msgCode
== MSGCODE_COMMAND_REJECTED
||
116 (m_message
->IsTranmission() && msgCode
== MSGCODE_HIGH_ERROR
) ||
117 (m_message
->IsTranmission() && msgCode
== MSGCODE_LOW_ERROR
) ||
118 (m_message
->IsTranmission() && msgCode
== MSGCODE_RECEIVE_FAILED
) ||
119 (m_message
->IsTranmission() && msgCode
== MSGCODE_TRANSMIT_FAILED_LINE
) ||
120 (m_message
->IsTranmission() && msgCode
== MSGCODE_TRANSMIT_FAILED_ACK
) ||
121 (m_message
->IsTranmission() && msgCode
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
) ||
122 (m_message
->IsTranmission() && msgCode
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
) ||
123 (m_message
->IsTranmission() && msgCode
== MSGCODE_TRANSMIT_SUCCEEDED
);
126 const char *CCECAdapterMessageQueueEntry::ToString(void) const
128 /* CEC transmissions got the 'set ack polarity' msgcode, which doesn't look nice */
129 if (m_message
->IsTranmission())
130 return "CEC transmission";
132 return CCECAdapterMessage::ToString(m_message
->Message());
135 bool CCECAdapterMessageQueueEntry::MessageReceivedCommandAccepted(const CCECAdapterMessage
&message
)
137 bool bSendSignal(false);
138 bool bHandled(false);
140 CLockObject
lock(m_mutex
);
141 if (m_iPacketsLeft
> 0)
146 /* log this message */
148 strLog
.Format("%s - command accepted", ToString());
149 if (m_iPacketsLeft
> 0)
150 strLog
.AppendFormat(" - waiting for %d more", m_iPacketsLeft
);
151 CLibCEC::AddLog(CEC_LOG_DEBUG
, strLog
);
153 /* no more packets left and not a transmission, so we're done */
154 if (!m_message
->IsTranmission() && m_iPacketsLeft
== 0)
156 m_message
->state
= ADAPTER_MESSAGE_STATE_SENT_ACKED
;
157 m_message
->response
= message
.packet
;
170 bool CCECAdapterMessageQueueEntry::MessageReceivedTransmitSucceeded(const CCECAdapterMessage
&message
)
173 CLockObject
lock(m_mutex
);
174 if (m_iPacketsLeft
== 0)
176 /* transmission succeeded, so we're done */
177 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - transmit succeeded", ToString());
178 m_message
->state
= ADAPTER_MESSAGE_STATE_SENT_ACKED
;
179 m_message
->response
= message
.packet
;
183 /* error, we expected more acks
184 since the messages are processed in order, this should not happen, so this is an error situation */
185 CLibCEC::AddLog(CEC_LOG_WARNING
, "%s - received 'transmit succeeded' but not enough 'command accepted' messages (%d left)", ToString(), m_iPacketsLeft
);
186 m_message
->state
= ADAPTER_MESSAGE_STATE_ERROR
;
195 bool CCECAdapterMessageQueueEntry::MessageReceivedResponse(const CCECAdapterMessage
&message
)
198 CLockObject
lock(m_mutex
);
199 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - received response", ToString());
200 m_message
->response
= message
.packet
;
201 if (m_message
->IsTranmission())
202 m_message
->state
= message
.Message() == MSGCODE_TRANSMIT_SUCCEEDED
? ADAPTER_MESSAGE_STATE_SENT_ACKED
: ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED
;
204 m_message
->state
= ADAPTER_MESSAGE_STATE_SENT_ACKED
;
213 CCECAdapterMessageQueue::~CCECAdapterMessageQueue(void)
218 void CCECAdapterMessageQueue::Clear(void)
220 CLockObject
lock(m_mutex
);
224 void CCECAdapterMessageQueue::MessageReceived(const CCECAdapterMessage
&msg
)
226 bool bHandled(false);
227 CLockObject
lock(m_mutex
);
228 /* send the received message to each entry in the queue until it is handled */
229 for (map
<uint64_t, CCECAdapterMessageQueueEntry
*>::iterator it
= m_messages
.begin(); !bHandled
&& it
!= m_messages
.end(); it
++)
230 bHandled
= it
->second
->MessageReceived(msg
);
234 /* the message wasn't handled */
235 bool bIsError(m_com
->HandlePoll(msg
));
236 CLibCEC::AddLog(bIsError
? CEC_LOG_WARNING
: CEC_LOG_DEBUG
, msg
.ToString());
238 /* push this message to the current frame */
239 if (!bIsError
&& msg
.PushToCecCommand(m_currentCECFrame
))
241 /* and push the current frame back over the callback method when a full command was received */
242 if (m_com
->IsInitialised())
243 m_com
->m_callback
->OnCommandReceived(m_currentCECFrame
);
245 /* clear the current frame */
246 m_currentCECFrame
.Clear();
251 void CCECAdapterMessageQueue::AddData(uint8_t *data
, size_t iLen
)
253 for (size_t iPtr
= 0; iPtr
< iLen
; iPtr
++)
255 bool bFullMessage(false);
257 CLockObject
lock(m_mutex
);
258 bFullMessage
= m_incomingAdapterMessage
.PushReceivedByte(data
[iPtr
]);
263 /* a full message was received */
264 CCECAdapterMessage newMessage
;
265 newMessage
.packet
= m_incomingAdapterMessage
.packet
;
266 MessageReceived(newMessage
);
268 /* clear the current message */
269 CLockObject
lock(m_mutex
);
270 m_incomingAdapterMessage
.Clear();
275 bool CCECAdapterMessageQueue::Write(CCECAdapterMessage
*msg
)
277 msg
->state
= ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT
;
279 /* set the correct line timeout */
280 if (msg
->IsTranmission())
283 m_com
->SetLineTimeout(msg
->lineTimeout
);
285 m_com
->SetLineTimeout(msg
->retryTimeout
);
288 CCECAdapterMessageQueueEntry
*entry(NULL
);
289 uint64_t iEntryId(0);
290 /* add to the wait for ack queue */
291 if (msg
->Message() != MSGCODE_START_BOOTLOADER
)
293 CLockObject
lock(m_mutex
);
294 entry
= new CCECAdapterMessageQueueEntry(msg
);
295 iEntryId
= m_iNextMessage
++;
296 m_messages
.insert(make_pair(iEntryId
, entry
));
299 /* TODO write the message async */
300 if (!m_com
->WriteToDevice(msg
))
310 if (!entry
->Wait(msg
->transmit_timeout
<= 5 ? CEC_DEFAULT_TRANSMIT_WAIT
: msg
->transmit_timeout
))
312 CLibCEC::AddLog(CEC_LOG_DEBUG
, "command '%s' was not acked by the controller", CCECAdapterMessage::ToString(msg
->Message()));
313 msg
->state
= ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED
;
317 CLockObject
lock(m_mutex
);
318 m_messages
.erase(iEntryId
);