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