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