cec: make all reads and write in CAdapterCommunication go through buffers, so we...
[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 37#include "util/StdString.h"
50aa01e6 38#include "platform/timeutils.h"
a8f0bd18
LOK
39
40using namespace std;
41using namespace CEC;
42
2abe74eb 43CAdapterCommunication::CAdapterCommunication(CLibCEC *controller) :
12027dbe 44 m_port(NULL),
50aa01e6 45 m_controller(controller)
a8f0bd18
LOK
46{
47 m_port = new CSerialPort;
48}
49
828682d3 50CAdapterCommunication::~CAdapterCommunication(void)
a8f0bd18 51{
12027dbe
LOK
52 Close();
53
54 if (m_port)
55 {
56 delete m_port;
57 m_port = NULL;
58 }
a8f0bd18
LOK
59}
60
25701fa6 61bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */)
a8f0bd18 62{
d5bffd3c 63 CLockObject lock(&m_commMutex);
13fd6a66
LOK
64 if (!m_port)
65 {
66 m_controller->AddLog(CEC_LOG_ERROR, "port is NULL");
a8f0bd18 67 return false;
13fd6a66
LOK
68 }
69
70 if (IsOpen())
71 {
72 m_controller->AddLog(CEC_LOG_ERROR, "port is already open");
73 }
a8f0bd18
LOK
74
75 if (!m_port->Open(strPort, iBaudRate))
76 {
77 CStdString strError;
78 strError.Format("error opening serial port '%s': %s", strPort, m_port->GetError().c_str());
2abe74eb 79 m_controller->AddLog(CEC_LOG_ERROR, strError);
a8f0bd18
LOK
80 return false;
81 }
82
2abe74eb 83 m_controller->AddLog(CEC_LOG_DEBUG, "connection opened");
a8f0bd18
LOK
84
85 //clear any input bytes
86 uint8_t buff[1024];
59c9774c 87 m_port->Read(buff, sizeof(buff), 500);
a8f0bd18 88
828682d3 89 if (CreateThread())
a8f0bd18 90 {
13fd6a66 91 m_controller->AddLog(CEC_LOG_DEBUG, "communication thread created");
a8f0bd18
LOK
92 return true;
93 }
94 else
95 {
13fd6a66 96 m_controller->AddLog(CEC_LOG_DEBUG, "could not create a communication thread");
a8f0bd18
LOK
97 }
98
99 return false;
100}
101
828682d3 102void CAdapterCommunication::Close(void)
a8f0bd18 103{
d5bffd3c 104 CLockObject lock(&m_commMutex);
828682d3 105 StopThread();
25701fa6 106
d5bffd3c 107 m_rcvCondition.Broadcast();
a8f0bd18
LOK
108}
109
828682d3 110void *CAdapterCommunication::Process(void)
a8f0bd18 111{
12027dbe
LOK
112 m_controller->AddLog(CEC_LOG_DEBUG, "communication thread started");
113
13fd6a66 114 while (!IsStopped())
a8f0bd18 115 {
3c53ac93
LOK
116 ReadFromDevice(100);
117 WriteNextCommand();
118 Sleep(5);
a8f0bd18
LOK
119 }
120
a8f0bd18
LOK
121 return NULL;
122}
123
25701fa6 124bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout)
a8f0bd18 125{
3c53ac93 126 CLockObject lock(&m_commMutex);
25701fa6 127 int32_t iBytesRead;
13fd6a66
LOK
128 uint8_t buff[1024];
129 if (!m_port)
130 return false;
b6c82769 131
13fd6a66
LOK
132 iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout);
133 if (iBytesRead < 0 || iBytesRead > 256)
a8f0bd18 134 {
13fd6a66
LOK
135 CStdString strError;
136 strError.Format("error reading from serial port: %s", m_port->GetError().c_str());
137 m_controller->AddLog(CEC_LOG_ERROR, strError);
13fd6a66 138 return false;
a8f0bd18 139 }
13fd6a66
LOK
140 else if (iBytesRead > 0)
141 AddData(buff, (uint8_t) iBytesRead);
25701fa6 142
13fd6a66 143 return iBytesRead > 0;
a8f0bd18
LOK
144}
145
8bca69de 146void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen)
a8f0bd18 147{
d5bffd3c 148 CLockObject lock(&m_bufferMutex);
50aa01e6
LOK
149 for (unsigned int iPtr = 0; iPtr < iLen; iPtr++)
150 m_inBuffer.Push(data[iPtr]);
d5bffd3c
LOK
151
152 m_rcvCondition.Signal();
a8f0bd18
LOK
153}
154
3c53ac93 155void CAdapterCommunication::WriteNextCommand(void)
a8f0bd18 156{
d5bffd3c 157 CLockObject lock(&m_commMutex);
3c53ac93
LOK
158 cec_adapter_message msg;
159 if (m_outBuffer.Pop(msg))
a8f0bd18 160 {
3c53ac93
LOK
161 if (m_port->Write(msg) != (int32_t) msg.size())
162 {
163 CStdString strError;
164 strError.Format("error writing to serial port: %s", m_port->GetError().c_str());
165 m_controller->AddLog(CEC_LOG_ERROR, strError);
166 }
167 else
168 {
169 m_controller->AddLog(CEC_LOG_DEBUG, "command sent");
170 CCondition::Sleep((uint32_t) msg.size() * (uint32_t)24 /*data*/ + (uint32_t)5 /*start bit (4.5 ms)*/);
171 }
25701fa6 172 }
3c53ac93 173}
2abe74eb 174
3c53ac93
LOK
175bool CAdapterCommunication::Write(const cec_adapter_message &data)
176{
177 m_outBuffer.Push(data);
a8f0bd18
LOK
178 return true;
179}
180
9dee1670 181bool CAdapterCommunication::Read(cec_adapter_message &msg, uint32_t iTimeout)
a8f0bd18 182{
d5bffd3c 183 CLockObject lock(&m_bufferMutex);
a8f0bd18 184
50aa01e6
LOK
185 msg.clear();
186 uint64_t iNow = GetTimeMs();
187 uint64_t iTarget = iNow + iTimeout;
188 bool bGotFullMessage(false);
189 bool bNextIsEscaped(false);
190 bool bGotStart(false);
a8f0bd18 191
50aa01e6 192 while(!bGotFullMessage && iNow < iTarget)
a8f0bd18 193 {
50aa01e6
LOK
194 uint8_t buf = 0;
195 if (!m_inBuffer.Pop(buf))
a8f0bd18 196 {
50aa01e6
LOK
197 if (!m_rcvCondition.Wait(&m_bufferMutex, iTarget - iNow))
198 return false;
a8f0bd18 199 }
a8f0bd18 200
50aa01e6 201 if (!bGotStart)
a8f0bd18 202 {
50aa01e6
LOK
203 if (buf == MSGSTART)
204 bGotStart = true;
205 continue;
a8f0bd18 206 }
50aa01e6 207 else if (buf == MSGSTART) //we found a msgstart before msgend, this is not right, remove
a8f0bd18 208 {
50aa01e6
LOK
209 m_controller->AddLog(CEC_LOG_ERROR, "received MSGSTART before MSGEND");
210 msg.clear();
211 bGotStart = true;
a8f0bd18 212 }
a8f0bd18 213
50aa01e6 214 if (buf == MSGEND)
a8f0bd18 215 {
50aa01e6 216 bGotFullMessage = true;
a8f0bd18 217 }
50aa01e6
LOK
218 else if (bNextIsEscaped)
219 {
220 msg.push_back(buf + (uint8_t)ESCOFFSET);
221 bNextIsEscaped = false;
222 }
223 else if (buf == MSGESC)
224 bNextIsEscaped = true;
225 else
226 msg.push_back(buf);
a8f0bd18
LOK
227 }
228
50aa01e6 229 return bGotFullMessage;
a8f0bd18
LOK
230}
231
828682d3 232std::string CAdapterCommunication::GetError(void) const
a8f0bd18
LOK
233{
234 return m_port->GetError();
235}
2abe74eb
LOK
236
237bool CAdapterCommunication::StartBootloader(void)
238{
239 if (!IsRunning())
240 return false;
241
242 m_controller->AddLog(CEC_LOG_DEBUG, "starting the bootloader");
9dee1670 243 cec_adapter_message output;
25701fa6
LOK
244 output.clear();
245
2abe74eb
LOK
246 output.push_back(MSGSTART);
247 PushEscaped(output, MSGCODE_START_BOOTLOADER);
248 output.push_back(MSGEND);
249
250 if (!Write(output))
251 {
252 m_controller->AddLog(CEC_LOG_ERROR, "could not start the bootloader");
253 return false;
254 }
255 m_controller->AddLog(CEC_LOG_DEBUG, "bootloader start command transmitted");
256 return true;
257}
258
9dee1670 259void CAdapterCommunication::PushEscaped(cec_adapter_message &vec, uint8_t byte)
2abe74eb
LOK
260{
261 if (byte >= MSGESC && byte != MSGSTART)
262 {
263 vec.push_back(MSGESC);
264 vec.push_back(byte - ESCOFFSET);
265 }
266 else
267 {
268 vec.push_back(byte);
269 }
270}
271
272bool CAdapterCommunication::SetAckMask(uint16_t iMask)
273{
274 if (!IsRunning())
275 return false;
276
277 CStdString strLog;
278 strLog.Format("setting ackmask to %2x", iMask);
279 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
280
9dee1670 281 cec_adapter_message output;
25701fa6 282 output.clear();
2abe74eb
LOK
283
284 output.push_back(MSGSTART);
285 PushEscaped(output, MSGCODE_SET_ACK_MASK);
286 PushEscaped(output, iMask >> 8);
287 PushEscaped(output, (uint8_t)iMask);
288 output.push_back(MSGEND);
289
290 if (!Write(output))
291 {
292 m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask");
293 return false;
294 }
295
296 return true;
297}
298
299bool CAdapterCommunication::PingAdapter(void)
300{
301 if (!IsRunning())
302 return false;
303
304 m_controller->AddLog(CEC_LOG_DEBUG, "sending ping");
9dee1670 305 cec_adapter_message output;
25701fa6
LOK
306 output.clear();
307
2abe74eb
LOK
308 output.push_back(MSGSTART);
309 PushEscaped(output, MSGCODE_PING);
310 output.push_back(MSGEND);
311
312 if (!Write(output))
313 {
314 m_controller->AddLog(CEC_LOG_ERROR, "could not send ping command");
315 return false;
316 }
317
318 m_controller->AddLog(CEC_LOG_DEBUG, "ping tranmitted");
319
320 // TODO check for pong
321 return true;
322}
13fd6a66
LOK
323
324bool CAdapterCommunication::IsOpen(void) const
325{
326 return !IsStopped() && m_port->IsOpen();
327}
9dee1670
LOK
328
329void CAdapterCommunication::FormatAdapterMessage(const cec_command &command, cec_adapter_message &packet)
330{
331 packet.clear();
332
333 //set ack polarity to high when transmitting to the broadcast address
334 //set ack polarity low when transmitting to any other address
335 packet.push_back(MSGSTART);
336 PushEscaped(packet, MSGCODE_TRANSMIT_ACK_POLARITY);
337 if (command.destination == CECDEVICE_BROADCAST)
338 PushEscaped(packet, CEC_TRUE);
339 else
340 PushEscaped(packet, CEC_FALSE);
341 packet.push_back(MSGEND);
342
343 // add source and destination
344 packet.push_back(MSGSTART);
345 PushEscaped(packet, MSGCODE_TRANSMIT);
346 packet.push_back(((uint8_t)command.initiator << 4) + (uint8_t)command.destination);
347 packet.push_back(MSGEND);
348
349 // add opcode
350 packet.push_back(MSGSTART);
06a1f7ce 351 PushEscaped(packet, command.parameters.empty() ? (uint8_t)MSGCODE_TRANSMIT_EOM : (uint8_t)MSGCODE_TRANSMIT);
9dee1670
LOK
352 packet.push_back((uint8_t) command.opcode);
353 packet.push_back(MSGEND);
354
355 // add parameters
356 for (int8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
357 {
358 packet.push_back(MSGSTART);
359
360 if (iPtr == command.parameters.size - 1)
361 PushEscaped(packet, MSGCODE_TRANSMIT_EOM);
362 else
363 PushEscaped(packet, MSGCODE_TRANSMIT);
364
365 PushEscaped(packet, command.parameters[iPtr]);
366
367 packet.push_back(MSGEND);
368 }
369}
370