cec: don't send a 'disconnected' alert when the close method is called
[deb_libcec.git] / src / lib / adapter / 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
33#include "USBCECAdapterMessageQueue.h"
34#include "USBCECAdapterCommunication.h"
35#include "../platform/sockets/socket.h"
36#include "../LibCEC.h"
37
38using namespace CEC;
39using namespace PLATFORM;
8cdaa059 40using namespace std;
a75e3a5a 41
b32ffd87
LOK
42#define MESSAGE_QUEUE_SIGNAL_WAIT_TIME 1000
43
004b8382
LOK
44CCECAdapterMessageQueueEntry::CCECAdapterMessageQueueEntry(CCECAdapterMessageQueue *queue, CCECAdapterMessage *message) :
45 m_queue(queue),
a75e3a5a
LOK
46 m_message(message),
47 m_iPacketsLeft(message->IsTranmission() ? message->Size() / 4 : 1),
48 m_bSucceeded(false),
49 m_bWaiting(true) {}
50
51CCECAdapterMessageQueueEntry::~CCECAdapterMessageQueueEntry(void) { }
52
53void CCECAdapterMessageQueueEntry::Broadcast(void)
54{
55 CLockObject lock(m_mutex);
56 m_condition.Broadcast();
57}
58
59bool CCECAdapterMessageQueueEntry::MessageReceived(const CCECAdapterMessage &message)
60{
a75e3a5a
LOK
61 bool bHandled(false);
62
8cdaa059 63 if (IsResponse(message))
a75e3a5a 64 {
a75e3a5a
LOK
65 switch (message.Message())
66 {
67 case MSGCODE_COMMAND_ACCEPTED:
8cdaa059 68 bHandled = MessageReceivedCommandAccepted(message);
a75e3a5a
LOK
69 break;
70 case MSGCODE_TRANSMIT_SUCCEEDED:
8cdaa059 71 bHandled = MessageReceivedTransmitSucceeded(message);
a75e3a5a
LOK
72 break;
73 default:
8cdaa059 74 bHandled = MessageReceivedResponse(message);
a75e3a5a
LOK
75 break;
76 }
77 }
78
a75e3a5a
LOK
79 return bHandled;
80}
81
8cdaa059
LOK
82void CCECAdapterMessageQueueEntry::Signal(void)
83{
84 CLockObject lock(m_mutex);
85 m_bSucceeded = true;
86 m_condition.Signal();
87}
88
a75e3a5a
LOK
89bool CCECAdapterMessageQueueEntry::Wait(uint32_t iTimeout)
90{
91 bool bReturn(false);
92 /* wait until we receive a signal when the tranmission succeeded */
93 {
94 CLockObject lock(m_mutex);
95 bReturn = m_bSucceeded ? true : m_condition.Wait(m_mutex, m_bSucceeded, iTimeout);
96 m_bWaiting = false;
97 }
98 return bReturn;
99}
100
101bool CCECAdapterMessageQueueEntry::IsWaiting(void)
102{
103 CLockObject lock(m_mutex);
104 return m_bWaiting;
105}
106
107cec_adapter_messagecode CCECAdapterMessageQueueEntry::MessageCode(void)
108{
109 return m_message->Message();
110}
111
112bool CCECAdapterMessageQueueEntry::IsResponse(const CCECAdapterMessage &msg)
113{
114 cec_adapter_messagecode msgCode = msg.Message();
115 return msgCode == MessageCode() ||
116 msgCode == MSGCODE_TIMEOUT_ERROR ||
117 msgCode == MSGCODE_COMMAND_ACCEPTED ||
118 msgCode == MSGCODE_COMMAND_REJECTED ||
119 (m_message->IsTranmission() && msgCode == MSGCODE_HIGH_ERROR) ||
120 (m_message->IsTranmission() && msgCode == MSGCODE_LOW_ERROR) ||
121 (m_message->IsTranmission() && msgCode == MSGCODE_RECEIVE_FAILED) ||
122 (m_message->IsTranmission() && msgCode == MSGCODE_TRANSMIT_FAILED_LINE) ||
123 (m_message->IsTranmission() && msgCode == MSGCODE_TRANSMIT_FAILED_ACK) ||
124 (m_message->IsTranmission() && msgCode == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA) ||
125 (m_message->IsTranmission() && msgCode == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE) ||
126 (m_message->IsTranmission() && msgCode == MSGCODE_TRANSMIT_SUCCEEDED);
127}
128
129const char *CCECAdapterMessageQueueEntry::ToString(void) const
130{
131 /* CEC transmissions got the 'set ack polarity' msgcode, which doesn't look nice */
132 if (m_message->IsTranmission())
133 return "CEC transmission";
134 else
135 return CCECAdapterMessage::ToString(m_message->Message());
136}
137
138bool CCECAdapterMessageQueueEntry::MessageReceivedCommandAccepted(const CCECAdapterMessage &message)
139{
140 bool bSendSignal(false);
8cdaa059 141 bool bHandled(false);
a75e3a5a 142 {
8cdaa059 143 CLockObject lock(m_mutex);
a75e3a5a 144 if (m_iPacketsLeft > 0)
8cdaa059
LOK
145 {
146 /* decrease by 1 */
a75e3a5a
LOK
147 m_iPacketsLeft--;
148
8cdaa059
LOK
149 /* log this message */
150 CStdString strLog;
151 strLog.Format("%s - command accepted", ToString());
152 if (m_iPacketsLeft > 0)
153 strLog.AppendFormat(" - waiting for %d more", m_iPacketsLeft);
004b8382 154 m_queue->m_com->m_callback->GetLib()->AddLog(CEC_LOG_DEBUG, strLog);
8cdaa059
LOK
155
156 /* no more packets left and not a transmission, so we're done */
157 if (!m_message->IsTranmission() && m_iPacketsLeft == 0)
158 {
159 m_message->state = ADAPTER_MESSAGE_STATE_SENT_ACKED;
160 m_message->response = message.packet;
161 bSendSignal = true;
162 }
163 bHandled = true;
a75e3a5a
LOK
164 }
165 }
8cdaa059
LOK
166
167 if (bSendSignal)
168 Signal();
169
170 return bHandled;
a75e3a5a
LOK
171}
172
173bool CCECAdapterMessageQueueEntry::MessageReceivedTransmitSucceeded(const CCECAdapterMessage &message)
174{
a75e3a5a 175 {
8cdaa059
LOK
176 CLockObject lock(m_mutex);
177 if (m_iPacketsLeft == 0)
178 {
179 /* transmission succeeded, so we're done */
004b8382 180 m_queue->m_com->m_callback->GetLib()->AddLog(CEC_LOG_DEBUG, "%s - transmit succeeded", ToString());
8cdaa059
LOK
181 m_message->state = ADAPTER_MESSAGE_STATE_SENT_ACKED;
182 m_message->response = message.packet;
183 }
184 else
185 {
186 /* error, we expected more acks
187 since the messages are processed in order, this should not happen, so this is an error situation */
004b8382 188 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
189 m_message->state = ADAPTER_MESSAGE_STATE_ERROR;
190 }
a75e3a5a 191 }
8cdaa059
LOK
192
193 Signal();
194
a75e3a5a
LOK
195 return true;
196}
197
198bool CCECAdapterMessageQueueEntry::MessageReceivedResponse(const CCECAdapterMessage &message)
199{
8cdaa059
LOK
200 {
201 CLockObject lock(m_mutex);
004b8382 202 m_queue->m_com->m_callback->GetLib()->AddLog(CEC_LOG_DEBUG, "%s - received response - %s", ToString(), message.ToString().c_str());
8cdaa059
LOK
203 m_message->response = message.packet;
204 if (m_message->IsTranmission())
205 m_message->state = message.Message() == MSGCODE_TRANSMIT_SUCCEEDED ? ADAPTER_MESSAGE_STATE_SENT_ACKED : ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED;
206 else
207 m_message->state = ADAPTER_MESSAGE_STATE_SENT_ACKED;
208 }
209
210 Signal();
211
a75e3a5a
LOK
212 return true;
213}
214
215
216CCECAdapterMessageQueue::~CCECAdapterMessageQueue(void)
217{
218 Clear();
a8559e01 219 StopThread(0);
a75e3a5a
LOK
220}
221
222void CCECAdapterMessageQueue::Clear(void)
223{
a8559e01 224 StopThread(5);
a75e3a5a 225 CLockObject lock(m_mutex);
a8559e01 226 m_writeQueue.Clear();
8cdaa059 227 m_messages.clear();
a75e3a5a
LOK
228}
229
a8559e01
LOK
230void *CCECAdapterMessageQueue::Process(void)
231{
232 CCECAdapterMessageQueueEntry *message(NULL);
233 while (!IsStopped())
234 {
235 /* wait for a new message */
55c75e6e 236 if (m_writeQueue.Pop(message, MESSAGE_QUEUE_SIGNAL_WAIT_TIME) && message)
a8559e01
LOK
237 {
238 /* write this message */
55c75e6e
LOK
239 {
240 CLockObject lock(m_mutex);
241 m_com->WriteToDevice(message->m_message);
242 }
243 if (message->m_message->state == ADAPTER_MESSAGE_STATE_ERROR ||
244 message->m_message->Message() == MSGCODE_START_BOOTLOADER)
a8559e01
LOK
245 {
246 message->Signal();
247 Clear();
248 break;
249 }
250 }
251 }
252 return NULL;
253}
254
a75e3a5a
LOK
255void CCECAdapterMessageQueue::MessageReceived(const CCECAdapterMessage &msg)
256{
8cdaa059 257 bool bHandled(false);
a75e3a5a 258 CLockObject lock(m_mutex);
8cdaa059
LOK
259 /* send the received message to each entry in the queue until it is handled */
260 for (map<uint64_t, CCECAdapterMessageQueueEntry *>::iterator it = m_messages.begin(); !bHandled && it != m_messages.end(); it++)
261 bHandled = it->second->MessageReceived(msg);
a75e3a5a 262
8cdaa059 263 if (!bHandled)
a75e3a5a
LOK
264 {
265 /* the message wasn't handled */
266 bool bIsError(m_com->HandlePoll(msg));
004b8382 267 m_com->m_callback->GetLib()->AddLog(bIsError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
a75e3a5a
LOK
268
269 /* push this message to the current frame */
270 if (!bIsError && msg.PushToCecCommand(m_currentCECFrame))
271 {
272 /* and push the current frame back over the callback method when a full command was received */
273 if (m_com->IsInitialised())
274 m_com->m_callback->OnCommandReceived(m_currentCECFrame);
275
276 /* clear the current frame */
277 m_currentCECFrame.Clear();
278 }
279 }
280}
281
282void CCECAdapterMessageQueue::AddData(uint8_t *data, size_t iLen)
283{
284 for (size_t iPtr = 0; iPtr < iLen; iPtr++)
285 {
286 bool bFullMessage(false);
287 {
288 CLockObject lock(m_mutex);
289 bFullMessage = m_incomingAdapterMessage.PushReceivedByte(data[iPtr]);
290 }
291
292 if (bFullMessage)
293 {
294 /* a full message was received */
295 CCECAdapterMessage newMessage;
296 newMessage.packet = m_incomingAdapterMessage.packet;
297 MessageReceived(newMessage);
298
299 /* clear the current message */
300 CLockObject lock(m_mutex);
301 m_incomingAdapterMessage.Clear();
302 }
303 }
304}
305
306bool CCECAdapterMessageQueue::Write(CCECAdapterMessage *msg)
307{
308 msg->state = ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT;
309
310 /* set the correct line timeout */
311 if (msg->IsTranmission())
312 {
33dd87a9 313 m_com->SetLineTimeout(msg->lineTimeout);
a75e3a5a
LOK
314 }
315
004b8382 316 CCECAdapterMessageQueueEntry *entry = new CCECAdapterMessageQueueEntry(this, msg);
8cdaa059 317 uint64_t iEntryId(0);
a75e3a5a
LOK
318 /* add to the wait for ack queue */
319 if (msg->Message() != MSGCODE_START_BOOTLOADER)
320 {
8cdaa059 321 CLockObject lock(m_mutex);
8cdaa059
LOK
322 iEntryId = m_iNextMessage++;
323 m_messages.insert(make_pair(iEntryId, entry));
a75e3a5a
LOK
324 }
325
a8559e01
LOK
326 /* add the message to the write queue */
327 m_writeQueue.Push(entry);
a75e3a5a 328
8cdaa059
LOK
329 bool bReturn(true);
330 if (entry)
a75e3a5a 331 {
8cdaa059 332 if (!entry->Wait(msg->transmit_timeout <= 5 ? CEC_DEFAULT_TRANSMIT_WAIT : msg->transmit_timeout))
a75e3a5a 333 {
004b8382 334 m_com->m_callback->GetLib()->AddLog(CEC_LOG_DEBUG, "command '%s' was not acked by the controller", CCECAdapterMessage::ToString(msg->Message()));
8cdaa059
LOK
335 msg->state = ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED;
336 bReturn = false;
a75e3a5a 337 }
8cdaa059 338
55c75e6e
LOK
339 if (msg->Message() != MSGCODE_START_BOOTLOADER)
340 {
341 CLockObject lock(m_mutex);
342 m_messages.erase(iEntryId);
343 }
4c2e665c 344 delete entry;
a75e3a5a 345 }
8cdaa059
LOK
346
347 return bReturn;
a75e3a5a 348}