cec: fix some threading related bugs, like trying to join with a detached thread...
[deb_libcec.git] / src / lib / AdapterCommunication.cpp
CommitLineData
a8f0bd18
LOK
1/*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011 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
828682d3 33#include "AdapterCommunication.h"
2abe74eb
LOK
34
35#include "LibCEC.h"
b9187cc6 36#include "platform/serialport.h"
a8f0bd18
LOK
37#include "util/StdString.h"
38
39using namespace std;
40using namespace CEC;
41
2abe74eb
LOK
42CAdapterCommunication::CAdapterCommunication(CLibCEC *controller) :
43 m_controller(controller),
a8f0bd18
LOK
44 m_inbuf(NULL),
45 m_iInbufSize(0),
46 m_iInbufUsed(0),
47 m_bStarted(false),
48 m_bStop(false)
49{
50 m_port = new CSerialPort;
51}
52
828682d3 53CAdapterCommunication::~CAdapterCommunication(void)
a8f0bd18 54{
c7e95492 55 StopThread();
a8f0bd18 56 m_port->Close();
c7e95492 57 delete m_port;
a8f0bd18
LOK
58 m_port = NULL;
59}
60
8bca69de 61bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint64_t iTimeoutMs /* = 10000 */)
a8f0bd18
LOK
62{
63 CLockObject lock(&m_commMutex);
64 if (m_bStarted)
65 return false;
66
67 if (!m_port->Open(strPort, iBaudRate))
68 {
69 CStdString strError;
70 strError.Format("error opening serial port '%s': %s", strPort, m_port->GetError().c_str());
2abe74eb 71 m_controller->AddLog(CEC_LOG_ERROR, strError);
a8f0bd18
LOK
72 return false;
73 }
74
2abe74eb 75 m_controller->AddLog(CEC_LOG_DEBUG, "connection opened");
a8f0bd18
LOK
76
77 //clear any input bytes
78 uint8_t buff[1024];
79 m_port->Read(buff, sizeof(buff), 50);
80
81 CCondition::Sleep(CEC_SETTLE_DOWN_TIME);
82
83 m_bStop = false;
84 m_bStarted = true;
828682d3
LOK
85
86 if (CreateThread())
a8f0bd18 87 {
2abe74eb 88 m_controller->AddLog(CEC_LOG_DEBUG, "reader thread created");
a8f0bd18
LOK
89 return true;
90 }
91 else
92 {
2abe74eb 93 m_controller->AddLog(CEC_LOG_DEBUG, "could not create a reader thread");
a8f0bd18
LOK
94 }
95
96 return false;
97}
98
828682d3 99void CAdapterCommunication::Close(void)
a8f0bd18 100{
828682d3 101 StopThread();
a8f0bd18
LOK
102 m_port->Close();
103}
104
828682d3 105void *CAdapterCommunication::Process(void)
a8f0bd18
LOK
106{
107 while (!m_bStop)
108 {
109 if (!ReadFromDevice(250))
110 {
111 m_bStarted = false;
112 break;
113 }
114
f7e6ba70
LOK
115 if (!m_bStop)
116 CCondition::Sleep(50);
a8f0bd18
LOK
117 }
118
a8f0bd18
LOK
119 CLockObject lock(&m_commMutex);
120 m_bStarted = false;
121 return NULL;
122}
123
8bca69de 124bool CAdapterCommunication::ReadFromDevice(uint64_t iTimeout)
a8f0bd18
LOK
125{
126 uint8_t buff[1024];
127 CLockObject lock(&m_commMutex);
b6c82769
LOK
128 if (!m_port)
129 return false;
130
8bca69de 131 int32_t iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout);
a8f0bd18 132 lock.Leave();
8bca69de 133 if (iBytesRead < 0 || iBytesRead > 256)
a8f0bd18
LOK
134 {
135 CStdString strError;
136 strError.Format("error reading from serial port: %s", m_port->GetError().c_str());
2abe74eb 137 m_controller->AddLog(CEC_LOG_ERROR, strError);
a8f0bd18
LOK
138 return false;
139 }
140 else if (iBytesRead > 0)
8bca69de 141 AddData(buff, (uint8_t) iBytesRead);
a8f0bd18
LOK
142
143 return true;
144}
145
8bca69de 146void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen)
a8f0bd18
LOK
147{
148 CLockObject lock(&m_bufferMutex);
149 if (iLen + m_iInbufUsed > m_iInbufSize)
150 {
151 m_iInbufSize = iLen + m_iInbufUsed;
152 m_inbuf = (uint8_t*)realloc(m_inbuf, m_iInbufSize);
153 }
154
155 memcpy(m_inbuf + m_iInbufUsed, data, iLen);
156 m_iInbufUsed += iLen;
157 lock.Leave();
158 m_condition.Signal();
159}
160
828682d3 161bool CAdapterCommunication::Write(const cec_frame &data)
a8f0bd18
LOK
162{
163 CLockObject lock(&m_commMutex);
164
8bca69de 165 if (m_port->Write(data) != (int) data.size())
a8f0bd18
LOK
166 {
167 CStdString strError;
168 strError.Format("error writing to serial port: %s", m_port->GetError().c_str());
2abe74eb 169 m_controller->AddLog(CEC_LOG_ERROR, strError);
a8f0bd18
LOK
170 return false;
171 }
172
2abe74eb
LOK
173 m_controller->AddLog(CEC_LOG_DEBUG, "command sent");
174
175 CCondition::Sleep((int) data.size() * 24 /*data*/ + 5 /*start bit (4.5 ms)*/ + 50 /* to be on the safe side */);
176
a8f0bd18
LOK
177 return true;
178}
179
8bca69de 180bool CAdapterCommunication::Read(cec_frame &msg, uint64_t iTimeout)
a8f0bd18
LOK
181{
182 CLockObject lock(&m_bufferMutex);
183
60fa4578 184 if (m_iInbufUsed < 1)
a8f0bd18
LOK
185 m_condition.Wait(&m_bufferMutex, iTimeout);
186
187 if (m_iInbufUsed < 1)
188 return false;
189
190 //search for first start of message
191 int startpos = -1;
192 for (int i = 0; i < m_iInbufUsed; i++)
193 {
194 if (m_inbuf[i] == MSGSTART)
195 {
196 startpos = i;
197 break;
198 }
199 }
200
201 if (startpos == -1)
202 return false;
203
204 //move anything from the first start of message to the beginning of the buffer
205 if (startpos > 0)
206 {
207 memmove(m_inbuf, m_inbuf + startpos, m_iInbufUsed - startpos);
208 m_iInbufUsed -= startpos;
209 }
210
211 if (m_iInbufUsed < 2)
212 return false;
213
214 //look for end of message
215 startpos = -1;
216 int endpos = -1;
217 for (int i = 1; i < m_iInbufUsed; i++)
218 {
219 if (m_inbuf[i] == MSGEND)
220 {
221 endpos = i;
222 break;
223 }
224 else if (m_inbuf[i] == MSGSTART)
225 {
226 startpos = i;
227 break;
228 }
229 }
230
231 if (startpos > 0) //we found a msgstart before msgend, this is not right, remove
232 {
2abe74eb 233 m_controller->AddLog(CEC_LOG_ERROR, "received MSGSTART before MSGEND");
a8f0bd18
LOK
234 memmove(m_inbuf, m_inbuf + startpos, m_iInbufUsed - startpos);
235 m_iInbufUsed -= startpos;
236 return false;
237 }
238
239 if (endpos > 0) //found a MSGEND
240 {
241 msg.clear();
242 bool isesc = false;
243 for (int i = 1; i < endpos; i++)
244 {
245 if (isesc)
246 {
247 msg.push_back(m_inbuf[i] + (uint8_t)ESCOFFSET);
248 isesc = false;
249 }
250 else if (m_inbuf[i] == MSGESC)
251 {
252 isesc = true;
253 }
254 else
255 {
256 msg.push_back(m_inbuf[i]);
257 }
258 }
259
260 if (endpos + 1 < m_iInbufUsed)
261 memmove(m_inbuf, m_inbuf + endpos + 1, m_iInbufUsed - endpos - 1);
262
263 m_iInbufUsed -= endpos + 1;
264
265 return true;
266 }
267
268 return false;
269}
270
828682d3 271std::string CAdapterCommunication::GetError(void) const
a8f0bd18
LOK
272{
273 return m_port->GetError();
274}
2abe74eb
LOK
275
276bool CAdapterCommunication::StartBootloader(void)
277{
278 if (!IsRunning())
279 return false;
280
281 m_controller->AddLog(CEC_LOG_DEBUG, "starting the bootloader");
282 cec_frame output;
283 output.push_back(MSGSTART);
284 PushEscaped(output, MSGCODE_START_BOOTLOADER);
285 output.push_back(MSGEND);
286
287 if (!Write(output))
288 {
289 m_controller->AddLog(CEC_LOG_ERROR, "could not start the bootloader");
290 return false;
291 }
292 m_controller->AddLog(CEC_LOG_DEBUG, "bootloader start command transmitted");
293 return true;
294}
295
296void CAdapterCommunication::PushEscaped(cec_frame &vec, uint8_t byte)
297{
298 if (byte >= MSGESC && byte != MSGSTART)
299 {
300 vec.push_back(MSGESC);
301 vec.push_back(byte - ESCOFFSET);
302 }
303 else
304 {
305 vec.push_back(byte);
306 }
307}
308
309bool CAdapterCommunication::SetAckMask(uint16_t iMask)
310{
311 if (!IsRunning())
312 return false;
313
314 CStdString strLog;
315 strLog.Format("setting ackmask to %2x", iMask);
316 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
317
318 cec_frame output;
319
320 output.push_back(MSGSTART);
321 PushEscaped(output, MSGCODE_SET_ACK_MASK);
322 PushEscaped(output, iMask >> 8);
323 PushEscaped(output, (uint8_t)iMask);
324 output.push_back(MSGEND);
325
326 if (!Write(output))
327 {
328 m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask");
329 return false;
330 }
331
332 return true;
333}
334
335bool CAdapterCommunication::PingAdapter(void)
336{
337 if (!IsRunning())
338 return false;
339
340 m_controller->AddLog(CEC_LOG_DEBUG, "sending ping");
341 cec_frame output;
342 output.push_back(MSGSTART);
343 PushEscaped(output, MSGCODE_PING);
344 output.push_back(MSGEND);
345
346 if (!Write(output))
347 {
348 m_controller->AddLog(CEC_LOG_ERROR, "could not send ping command");
349 return false;
350 }
351
352 m_controller->AddLog(CEC_LOG_DEBUG, "ping tranmitted");
353
354 // TODO check for pong
355 return true;
356}