cec: fixed - correct source for broadcast messages
[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{
d5bffd3c 68 CLockObject lock(&m_commMutex);
13fd6a66
LOK
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
828682d3 94 if (CreateThread())
a8f0bd18 95 {
13fd6a66 96 m_controller->AddLog(CEC_LOG_DEBUG, "communication thread created");
a8f0bd18
LOK
97 return true;
98 }
99 else
100 {
13fd6a66 101 m_controller->AddLog(CEC_LOG_DEBUG, "could not create a communication thread");
a8f0bd18
LOK
102 }
103
104 return false;
105}
106
828682d3 107void CAdapterCommunication::Close(void)
a8f0bd18 108{
d5bffd3c 109 CLockObject lock(&m_commMutex);
828682d3 110 StopThread();
25701fa6 111
13fd6a66
LOK
112 if (m_inbuf)
113 {
114 free(m_inbuf);
115 m_inbuf = NULL;
116 m_iInbufSize = 0;
117 m_iInbufUsed = 0;
118 }
d5bffd3c
LOK
119
120 m_rcvCondition.Broadcast();
a8f0bd18
LOK
121}
122
828682d3 123void *CAdapterCommunication::Process(void)
a8f0bd18 124{
12027dbe
LOK
125 m_controller->AddLog(CEC_LOG_DEBUG, "communication thread started");
126
13fd6a66 127 while (!IsStopped())
a8f0bd18 128 {
a8f0bd18 129 {
d5bffd3c
LOK
130 CLockObject lock(&m_commMutex);
131 ReadFromDevice(100);
a8f0bd18
LOK
132 }
133
13fd6a66 134 if (!IsStopped())
d5bffd3c 135 Sleep(5);
a8f0bd18
LOK
136 }
137
a8f0bd18
LOK
138 return NULL;
139}
140
25701fa6 141bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout)
a8f0bd18 142{
25701fa6 143 int32_t iBytesRead;
13fd6a66
LOK
144 uint8_t buff[1024];
145 if (!m_port)
146 return false;
b6c82769 147
13fd6a66
LOK
148 iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout);
149 if (iBytesRead < 0 || iBytesRead > 256)
a8f0bd18 150 {
13fd6a66
LOK
151 CStdString strError;
152 strError.Format("error reading from serial port: %s", m_port->GetError().c_str());
153 m_controller->AddLog(CEC_LOG_ERROR, strError);
13fd6a66 154 return false;
a8f0bd18 155 }
13fd6a66
LOK
156 else if (iBytesRead > 0)
157 AddData(buff, (uint8_t) iBytesRead);
25701fa6 158
13fd6a66 159 return iBytesRead > 0;
a8f0bd18
LOK
160}
161
8bca69de 162void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen)
a8f0bd18 163{
d5bffd3c 164 CLockObject lock(&m_bufferMutex);
25701fa6 165 if (m_iInbufUsed + iLen > m_iInbufSize)
a8f0bd18 166 {
25701fa6 167 m_iInbufSize = m_iInbufUsed + iLen;
a8f0bd18
LOK
168 m_inbuf = (uint8_t*)realloc(m_inbuf, m_iInbufSize);
169 }
170
171 memcpy(m_inbuf + m_iInbufUsed, data, iLen);
172 m_iInbufUsed += iLen;
d5bffd3c
LOK
173
174 m_rcvCondition.Signal();
a8f0bd18
LOK
175}
176
9dee1670 177bool CAdapterCommunication::Write(const cec_adapter_message &data)
a8f0bd18 178{
d5bffd3c 179 CLockObject lock(&m_commMutex);
a6b6469c 180 if (m_port->Write(data) != (int32_t) data.size())
a8f0bd18 181 {
d5bffd3c
LOK
182 CStdString strError;
183 strError.Format("error writing to serial port: %s", m_port->GetError().c_str());
184 m_controller->AddLog(CEC_LOG_ERROR, strError);
185 return false;
25701fa6 186 }
2abe74eb 187
d5bffd3c 188 m_controller->AddLog(CEC_LOG_DEBUG, "command sent");
a6b6469c 189 CCondition::Sleep((uint32_t) data.size() * (uint32_t)24 /*data*/ + (uint32_t)5 /*start bit (4.5 ms)*/);
d5bffd3c 190
a8f0bd18
LOK
191 return true;
192}
193
9dee1670 194bool CAdapterCommunication::Read(cec_adapter_message &msg, uint32_t iTimeout)
a8f0bd18 195{
d5bffd3c 196 CLockObject lock(&m_bufferMutex);
a8f0bd18 197
60fa4578 198 if (m_iInbufUsed < 1)
13fd6a66 199 {
d5bffd3c 200 if (!m_rcvCondition.Wait(&m_bufferMutex, iTimeout))
13fd6a66
LOK
201 return false;
202 }
a8f0bd18 203
13fd6a66 204 if (m_iInbufUsed < 1 || IsStopped())
a8f0bd18
LOK
205 return false;
206
207 //search for first start of message
25701fa6
LOK
208 int16_t startpos = -1;
209 for (int16_t iPtr = 0; iPtr < m_iInbufUsed; iPtr++)
a8f0bd18 210 {
25701fa6 211 if (m_inbuf[iPtr] == MSGSTART)
a8f0bd18 212 {
25701fa6 213 startpos = iPtr;
a8f0bd18
LOK
214 break;
215 }
216 }
217
218 if (startpos == -1)
219 return false;
220
221 //move anything from the first start of message to the beginning of the buffer
222 if (startpos > 0)
223 {
224 memmove(m_inbuf, m_inbuf + startpos, m_iInbufUsed - startpos);
225 m_iInbufUsed -= startpos;
226 }
227
228 if (m_iInbufUsed < 2)
229 return false;
230
231 //look for end of message
232 startpos = -1;
25701fa6
LOK
233 int16_t endpos = -1;
234 for (int16_t iPtr = 1; iPtr < m_iInbufUsed; iPtr++)
a8f0bd18 235 {
25701fa6 236 if (m_inbuf[iPtr] == MSGEND)
a8f0bd18 237 {
25701fa6 238 endpos = iPtr;
a8f0bd18
LOK
239 break;
240 }
25701fa6 241 else if (m_inbuf[iPtr] == MSGSTART)
a8f0bd18 242 {
25701fa6 243 startpos = iPtr;
a8f0bd18
LOK
244 break;
245 }
246 }
247
248 if (startpos > 0) //we found a msgstart before msgend, this is not right, remove
249 {
2abe74eb 250 m_controller->AddLog(CEC_LOG_ERROR, "received MSGSTART before MSGEND");
a8f0bd18
LOK
251 memmove(m_inbuf, m_inbuf + startpos, m_iInbufUsed - startpos);
252 m_iInbufUsed -= startpos;
253 return false;
254 }
255
256 if (endpos > 0) //found a MSGEND
257 {
258 msg.clear();
259 bool isesc = false;
25701fa6 260 for (int16_t iPtr = 1; iPtr < endpos; iPtr++)
a8f0bd18
LOK
261 {
262 if (isesc)
263 {
25701fa6 264 msg.push_back(m_inbuf[iPtr] + (uint8_t)ESCOFFSET);
a8f0bd18
LOK
265 isesc = false;
266 }
25701fa6 267 else if (m_inbuf[iPtr] == MSGESC)
a8f0bd18
LOK
268 {
269 isesc = true;
270 }
271 else
272 {
25701fa6 273 msg.push_back(m_inbuf[iPtr]);
a8f0bd18
LOK
274 }
275 }
276
277 if (endpos + 1 < m_iInbufUsed)
278 memmove(m_inbuf, m_inbuf + endpos + 1, m_iInbufUsed - endpos - 1);
279
280 m_iInbufUsed -= endpos + 1;
281
282 return true;
283 }
284
285 return false;
286}
287
828682d3 288std::string CAdapterCommunication::GetError(void) const
a8f0bd18
LOK
289{
290 return m_port->GetError();
291}
2abe74eb
LOK
292
293bool CAdapterCommunication::StartBootloader(void)
294{
295 if (!IsRunning())
296 return false;
297
298 m_controller->AddLog(CEC_LOG_DEBUG, "starting the bootloader");
9dee1670 299 cec_adapter_message output;
25701fa6
LOK
300 output.clear();
301
2abe74eb
LOK
302 output.push_back(MSGSTART);
303 PushEscaped(output, MSGCODE_START_BOOTLOADER);
304 output.push_back(MSGEND);
305
306 if (!Write(output))
307 {
308 m_controller->AddLog(CEC_LOG_ERROR, "could not start the bootloader");
309 return false;
310 }
311 m_controller->AddLog(CEC_LOG_DEBUG, "bootloader start command transmitted");
312 return true;
313}
314
9dee1670 315void CAdapterCommunication::PushEscaped(cec_adapter_message &vec, uint8_t byte)
2abe74eb
LOK
316{
317 if (byte >= MSGESC && byte != MSGSTART)
318 {
319 vec.push_back(MSGESC);
320 vec.push_back(byte - ESCOFFSET);
321 }
322 else
323 {
324 vec.push_back(byte);
325 }
326}
327
328bool CAdapterCommunication::SetAckMask(uint16_t iMask)
329{
330 if (!IsRunning())
331 return false;
332
333 CStdString strLog;
334 strLog.Format("setting ackmask to %2x", iMask);
335 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
336
9dee1670 337 cec_adapter_message output;
25701fa6 338 output.clear();
2abe74eb
LOK
339
340 output.push_back(MSGSTART);
341 PushEscaped(output, MSGCODE_SET_ACK_MASK);
342 PushEscaped(output, iMask >> 8);
343 PushEscaped(output, (uint8_t)iMask);
344 output.push_back(MSGEND);
345
346 if (!Write(output))
347 {
348 m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask");
349 return false;
350 }
351
352 return true;
353}
354
355bool CAdapterCommunication::PingAdapter(void)
356{
357 if (!IsRunning())
358 return false;
359
360 m_controller->AddLog(CEC_LOG_DEBUG, "sending ping");
9dee1670 361 cec_adapter_message output;
25701fa6
LOK
362 output.clear();
363
2abe74eb
LOK
364 output.push_back(MSGSTART);
365 PushEscaped(output, MSGCODE_PING);
366 output.push_back(MSGEND);
367
368 if (!Write(output))
369 {
370 m_controller->AddLog(CEC_LOG_ERROR, "could not send ping command");
371 return false;
372 }
373
374 m_controller->AddLog(CEC_LOG_DEBUG, "ping tranmitted");
375
376 // TODO check for pong
377 return true;
378}
13fd6a66
LOK
379
380bool CAdapterCommunication::IsOpen(void) const
381{
382 return !IsStopped() && m_port->IsOpen();
383}
9dee1670
LOK
384
385void CAdapterCommunication::FormatAdapterMessage(const cec_command &command, cec_adapter_message &packet)
386{
387 packet.clear();
388
389 //set ack polarity to high when transmitting to the broadcast address
390 //set ack polarity low when transmitting to any other address
391 packet.push_back(MSGSTART);
392 PushEscaped(packet, MSGCODE_TRANSMIT_ACK_POLARITY);
393 if (command.destination == CECDEVICE_BROADCAST)
394 PushEscaped(packet, CEC_TRUE);
395 else
396 PushEscaped(packet, CEC_FALSE);
397 packet.push_back(MSGEND);
398
399 // add source and destination
400 packet.push_back(MSGSTART);
401 PushEscaped(packet, MSGCODE_TRANSMIT);
402 packet.push_back(((uint8_t)command.initiator << 4) + (uint8_t)command.destination);
403 packet.push_back(MSGEND);
404
405 // add opcode
406 packet.push_back(MSGSTART);
06a1f7ce 407 PushEscaped(packet, command.parameters.empty() ? (uint8_t)MSGCODE_TRANSMIT_EOM : (uint8_t)MSGCODE_TRANSMIT);
9dee1670
LOK
408 packet.push_back((uint8_t) command.opcode);
409 packet.push_back(MSGEND);
410
411 // add parameters
412 for (int8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
413 {
414 packet.push_back(MSGSTART);
415
416 if (iPtr == command.parameters.size - 1)
417 PushEscaped(packet, MSGCODE_TRANSMIT_EOM);
418 else
419 PushEscaped(packet, MSGCODE_TRANSMIT);
420
421 PushEscaped(packet, command.parameters[iPtr]);
422
423 packet.push_back(MSGEND);
424 }
425}
426