cec : stop the processor thread and delete the CSerialPort instance on exit. zzzz
[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
828682d3 61bool CAdapterCommunication::Open(const char *strPort, int iBaudRate /* = 38400 */, int 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
2abe74eb 119 m_controller->AddLog(CEC_LOG_DEBUG, "reader thread terminated");
a8f0bd18
LOK
120
121 CLockObject lock(&m_commMutex);
122 m_bStarted = false;
123 return NULL;
124}
125
828682d3 126bool CAdapterCommunication::ReadFromDevice(int iTimeout)
a8f0bd18
LOK
127{
128 uint8_t buff[1024];
129 CLockObject lock(&m_commMutex);
130 int iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout);
131 lock.Leave();
132 if (iBytesRead < 0)
133 {
134 CStdString strError;
135 strError.Format("error reading from serial port: %s", m_port->GetError().c_str());
2abe74eb 136 m_controller->AddLog(CEC_LOG_ERROR, strError);
a8f0bd18
LOK
137 return false;
138 }
139 else if (iBytesRead > 0)
140 AddData(buff, iBytesRead);
141
142 return true;
143}
144
828682d3 145void CAdapterCommunication::AddData(uint8_t *data, int iLen)
a8f0bd18
LOK
146{
147 CLockObject lock(&m_bufferMutex);
148 if (iLen + m_iInbufUsed > m_iInbufSize)
149 {
150 m_iInbufSize = iLen + m_iInbufUsed;
151 m_inbuf = (uint8_t*)realloc(m_inbuf, m_iInbufSize);
152 }
153
154 memcpy(m_inbuf + m_iInbufUsed, data, iLen);
155 m_iInbufUsed += iLen;
156 lock.Leave();
157 m_condition.Signal();
158}
159
828682d3 160bool CAdapterCommunication::Write(const cec_frame &data)
a8f0bd18
LOK
161{
162 CLockObject lock(&m_commMutex);
163
164 if (m_port->Write(data) != data.size())
165 {
166 CStdString strError;
167 strError.Format("error writing to serial port: %s", m_port->GetError().c_str());
2abe74eb 168 m_controller->AddLog(CEC_LOG_ERROR, strError);
a8f0bd18
LOK
169 return false;
170 }
171
2abe74eb
LOK
172 m_controller->AddLog(CEC_LOG_DEBUG, "command sent");
173
174 CCondition::Sleep((int) data.size() * 24 /*data*/ + 5 /*start bit (4.5 ms)*/ + 50 /* to be on the safe side */);
175
a8f0bd18
LOK
176 return true;
177}
178
828682d3 179bool CAdapterCommunication::Read(cec_frame &msg, int iTimeout)
a8f0bd18
LOK
180{
181 CLockObject lock(&m_bufferMutex);
182
60fa4578 183 if (m_iInbufUsed < 1)
a8f0bd18
LOK
184 m_condition.Wait(&m_bufferMutex, iTimeout);
185
186 if (m_iInbufUsed < 1)
187 return false;
188
189 //search for first start of message
190 int startpos = -1;
191 for (int i = 0; i < m_iInbufUsed; i++)
192 {
193 if (m_inbuf[i] == MSGSTART)
194 {
195 startpos = i;
196 break;
197 }
198 }
199
200 if (startpos == -1)
201 return false;
202
203 //move anything from the first start of message to the beginning of the buffer
204 if (startpos > 0)
205 {
206 memmove(m_inbuf, m_inbuf + startpos, m_iInbufUsed - startpos);
207 m_iInbufUsed -= startpos;
208 }
209
210 if (m_iInbufUsed < 2)
211 return false;
212
213 //look for end of message
214 startpos = -1;
215 int endpos = -1;
216 for (int i = 1; i < m_iInbufUsed; i++)
217 {
218 if (m_inbuf[i] == MSGEND)
219 {
220 endpos = i;
221 break;
222 }
223 else if (m_inbuf[i] == MSGSTART)
224 {
225 startpos = i;
226 break;
227 }
228 }
229
230 if (startpos > 0) //we found a msgstart before msgend, this is not right, remove
231 {
2abe74eb 232 m_controller->AddLog(CEC_LOG_ERROR, "received MSGSTART before MSGEND");
a8f0bd18
LOK
233 memmove(m_inbuf, m_inbuf + startpos, m_iInbufUsed - startpos);
234 m_iInbufUsed -= startpos;
235 return false;
236 }
237
238 if (endpos > 0) //found a MSGEND
239 {
240 msg.clear();
241 bool isesc = false;
242 for (int i = 1; i < endpos; i++)
243 {
244 if (isesc)
245 {
246 msg.push_back(m_inbuf[i] + (uint8_t)ESCOFFSET);
247 isesc = false;
248 }
249 else if (m_inbuf[i] == MSGESC)
250 {
251 isesc = true;
252 }
253 else
254 {
255 msg.push_back(m_inbuf[i]);
256 }
257 }
258
259 if (endpos + 1 < m_iInbufUsed)
260 memmove(m_inbuf, m_inbuf + endpos + 1, m_iInbufUsed - endpos - 1);
261
262 m_iInbufUsed -= endpos + 1;
263
264 return true;
265 }
266
267 return false;
268}
269
828682d3 270std::string CAdapterCommunication::GetError(void) const
a8f0bd18
LOK
271{
272 return m_port->GetError();
273}
2abe74eb
LOK
274
275bool CAdapterCommunication::StartBootloader(void)
276{
277 if (!IsRunning())
278 return false;
279
280 m_controller->AddLog(CEC_LOG_DEBUG, "starting the bootloader");
281 cec_frame output;
282 output.push_back(MSGSTART);
283 PushEscaped(output, MSGCODE_START_BOOTLOADER);
284 output.push_back(MSGEND);
285
286 if (!Write(output))
287 {
288 m_controller->AddLog(CEC_LOG_ERROR, "could not start the bootloader");
289 return false;
290 }
291 m_controller->AddLog(CEC_LOG_DEBUG, "bootloader start command transmitted");
292 return true;
293}
294
295void CAdapterCommunication::PushEscaped(cec_frame &vec, uint8_t byte)
296{
297 if (byte >= MSGESC && byte != MSGSTART)
298 {
299 vec.push_back(MSGESC);
300 vec.push_back(byte - ESCOFFSET);
301 }
302 else
303 {
304 vec.push_back(byte);
305 }
306}
307
308bool CAdapterCommunication::SetAckMask(uint16_t iMask)
309{
310 if (!IsRunning())
311 return false;
312
313 CStdString strLog;
314 strLog.Format("setting ackmask to %2x", iMask);
315 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
316
317 cec_frame output;
318
319 output.push_back(MSGSTART);
320 PushEscaped(output, MSGCODE_SET_ACK_MASK);
321 PushEscaped(output, iMask >> 8);
322 PushEscaped(output, (uint8_t)iMask);
323 output.push_back(MSGEND);
324
325 if (!Write(output))
326 {
327 m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask");
328 return false;
329 }
330
331 return true;
332}
333
334bool CAdapterCommunication::PingAdapter(void)
335{
336 if (!IsRunning())
337 return false;
338
339 m_controller->AddLog(CEC_LOG_DEBUG, "sending ping");
340 cec_frame output;
341 output.push_back(MSGSTART);
342 PushEscaped(output, MSGCODE_PING);
343 output.push_back(MSGEND);
344
345 if (!Write(output))
346 {
347 m_controller->AddLog(CEC_LOG_ERROR, "could not send ping command");
348 return false;
349 }
350
351 m_controller->AddLog(CEC_LOG_DEBUG, "ping tranmitted");
352
353 // TODO check for pong
354 return true;
355}