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