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