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