d3eecf45ea7f1045ef466ba38e1291f71f0186ca
[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_startCondition.Wait(&m_mutex);
151 m_controller->AddLog(CEC_LOG_DEBUG, "communication thread started");
152 return true;
153 }
154 else
155 {
156 m_controller->AddLog(CEC_LOG_DEBUG, "could not create a communication thread");
157 }
158
159 return false;
160 }
161
162 void CAdapterCommunication::Close(void)
163 {
164 CLockObject lock(&m_mutex);
165 m_startCondition.Broadcast();
166 m_rcvCondition.Broadcast();
167 StopThread();
168 }
169
170 void *CAdapterCommunication::Process(void)
171 {
172 {
173 CLockObject lock(&m_mutex);
174 m_startCondition.Signal();
175 }
176
177 while (!IsStopped())
178 {
179 ReadFromDevice(500);
180 WriteNextCommand();
181 Sleep(5);
182 }
183
184 return NULL;
185 }
186
187 bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout)
188 {
189 int32_t iBytesRead;
190 uint8_t buff[1024];
191 if (!m_port)
192 return false;
193
194 iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout);
195 if (iBytesRead < 0 || iBytesRead > 256)
196 {
197 CStdString strError;
198 strError.Format("error reading from serial port: %s", m_port->GetError().c_str());
199 m_controller->AddLog(CEC_LOG_ERROR, strError);
200 return false;
201 }
202 else if (iBytesRead > 0)
203 AddData(buff, (uint8_t) iBytesRead);
204
205 return iBytesRead > 0;
206 }
207
208 void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen)
209 {
210 CLockObject lock(&m_mutex);
211 for (unsigned int iPtr = 0; iPtr < iLen; iPtr++)
212 m_inBuffer.Push(data[iPtr]);
213
214 m_rcvCondition.Signal();
215 }
216
217 void CAdapterCommunication::WriteNextCommand(void)
218 {
219 CCECAdapterMessagePtr msg;
220 if (m_outBuffer.Pop(msg))
221 {
222 CLockObject lock(&msg->mutex);
223 if (m_port->Write(msg) != (int32_t) msg->size())
224 {
225 CStdString strError;
226 strError.Format("error writing to serial port: %s", m_port->GetError().c_str());
227 m_controller->AddLog(CEC_LOG_ERROR, strError);
228 msg->state = ADAPTER_MESSAGE_STATE_ERROR;
229 }
230 else
231 {
232 m_controller->AddLog(CEC_LOG_DEBUG, "command sent");
233 CCondition::Sleep((uint32_t) msg->size() * (uint32_t)24 /*data*/ + (uint32_t)5 /*start bit (4.5 ms)*/);
234 msg->state = ADAPTER_MESSAGE_STATE_SENT;
235 }
236 msg->condition.Signal();
237 }
238 }
239
240 bool CAdapterCommunication::Write(CCECAdapterMessagePtr data)
241 {
242 data->state = ADAPTER_MESSAGE_STATE_WAITING;
243 m_outBuffer.Push(data);
244 return true;
245 }
246
247 bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout)
248 {
249 CLockObject lock(&m_mutex);
250
251 msg.clear();
252 uint64_t iNow = GetTimeMs();
253 uint64_t iTarget = iNow + iTimeout;
254 bool bGotFullMessage(false);
255 bool bNextIsEscaped(false);
256 bool bGotStart(false);
257
258 while(!bGotFullMessage && iNow < iTarget)
259 {
260 uint8_t buf = 0;
261 if (!m_inBuffer.Pop(buf))
262 {
263 if (!m_rcvCondition.Wait(&m_mutex, iTarget - iNow))
264 return false;
265 }
266
267 if (!bGotStart)
268 {
269 if (buf == MSGSTART)
270 bGotStart = true;
271 continue;
272 }
273 else if (buf == MSGSTART) //we found a msgstart before msgend, this is not right, remove
274 {
275 m_controller->AddLog(CEC_LOG_ERROR, "received MSGSTART before MSGEND");
276 msg.clear();
277 bGotStart = true;
278 }
279
280 if (buf == MSGEND)
281 {
282 bGotFullMessage = true;
283 }
284 else if (bNextIsEscaped)
285 {
286 msg.push_back(buf + (uint8_t)ESCOFFSET);
287 bNextIsEscaped = false;
288 }
289 else if (buf == MSGESC)
290 bNextIsEscaped = true;
291 else
292 msg.push_back(buf);
293 }
294
295 if (bGotFullMessage)
296 msg.state = ADAPTER_MESSAGE_STATE_RECEIVED;
297
298 return bGotFullMessage;
299 }
300
301 std::string CAdapterCommunication::GetError(void) const
302 {
303 return m_port->GetError();
304 }
305
306 bool CAdapterCommunication::StartBootloader(void)
307 {
308 if (!IsRunning())
309 return false;
310
311 m_controller->AddLog(CEC_LOG_DEBUG, "starting the bootloader");
312 CCECAdapterMessagePtr output(new CCECAdapterMessage);
313
314 output->push_back(MSGSTART);
315 output->push_escaped(MSGCODE_START_BOOTLOADER);
316 output->push_back(MSGEND);
317
318 if (!Write(output))
319 {
320 m_controller->AddLog(CEC_LOG_ERROR, "could not start the bootloader");
321 return false;
322 }
323 m_controller->AddLog(CEC_LOG_DEBUG, "bootloader start command transmitted");
324 return true;
325 }
326
327 bool CAdapterCommunication::SetAckMask(uint16_t iMask)
328 {
329 if (!IsRunning())
330 return false;
331
332 CStdString strLog;
333 strLog.Format("setting ackmask to %2x", iMask);
334 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
335
336 CCECAdapterMessagePtr output(new CCECAdapterMessage);
337
338 output->push_back(MSGSTART);
339 output->push_escaped(MSGCODE_SET_ACK_MASK);
340 output->push_escaped(iMask >> 8);
341 output->push_escaped((uint8_t)iMask);
342 output->push_back(MSGEND);
343
344 if (!Write(output))
345 {
346 m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask");
347 return false;
348 }
349
350 return true;
351 }
352
353 bool CAdapterCommunication::PingAdapter(void)
354 {
355 if (!IsRunning())
356 return false;
357
358 m_controller->AddLog(CEC_LOG_DEBUG, "sending ping");
359 CCECAdapterMessagePtr output(new CCECAdapterMessage);
360
361 output->push_back(MSGSTART);
362 output->push_escaped(MSGCODE_PING);
363 output->push_back(MSGEND);
364
365 if (!Write(output))
366 {
367 m_controller->AddLog(CEC_LOG_ERROR, "could not send ping command");
368 return false;
369 }
370
371 m_controller->AddLog(CEC_LOG_DEBUG, "ping tranmitted");
372
373 // TODO check for pong
374 return true;
375 }
376
377 bool CAdapterCommunication::IsOpen(void) const
378 {
379 return !IsStopped() && m_port->IsOpen() && IsRunning();
380 }