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