cec: use boost::shared_ptr for messages
[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
ad24cbaf
LOK
43CCECAdapterMessage::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
85CCECAdapterMessage &CCECAdapterMessage::operator =(const CCECAdapterMessage &msg)
86{
87 packet = msg.packet;
88 return *this;
89}
90
91void 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
2abe74eb 102CAdapterCommunication::CAdapterCommunication(CLibCEC *controller) :
12027dbe 103 m_port(NULL),
50aa01e6 104 m_controller(controller)
a8f0bd18
LOK
105{
106 m_port = new CSerialPort;
107}
108
828682d3 109CAdapterCommunication::~CAdapterCommunication(void)
a8f0bd18 110{
12027dbe
LOK
111 Close();
112
113 if (m_port)
114 {
115 delete m_port;
116 m_port = NULL;
117 }
a8f0bd18
LOK
118}
119
25701fa6 120bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */)
a8f0bd18 121{
13fd6a66
LOK
122 if (!m_port)
123 {
124 m_controller->AddLog(CEC_LOG_ERROR, "port is NULL");
a8f0bd18 125 return false;
13fd6a66
LOK
126 }
127
128 if (IsOpen())
129 {
130 m_controller->AddLog(CEC_LOG_ERROR, "port is already open");
131 }
a8f0bd18
LOK
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());
2abe74eb 137 m_controller->AddLog(CEC_LOG_ERROR, strError);
a8f0bd18
LOK
138 return false;
139 }
140
2abe74eb 141 m_controller->AddLog(CEC_LOG_DEBUG, "connection opened");
a8f0bd18
LOK
142
143 //clear any input bytes
144 uint8_t buff[1024];
59c9774c 145 m_port->Read(buff, sizeof(buff), 500);
a8f0bd18 146
828682d3 147 if (CreateThread())
a8f0bd18 148 {
13fd6a66 149 m_controller->AddLog(CEC_LOG_DEBUG, "communication thread created");
a8f0bd18
LOK
150 return true;
151 }
152 else
153 {
13fd6a66 154 m_controller->AddLog(CEC_LOG_DEBUG, "could not create a communication thread");
a8f0bd18
LOK
155 }
156
157 return false;
158}
159
828682d3 160void CAdapterCommunication::Close(void)
a8f0bd18 161{
828682d3 162 StopThread();
25701fa6 163
d5bffd3c 164 m_rcvCondition.Broadcast();
a8f0bd18
LOK
165}
166
828682d3 167void *CAdapterCommunication::Process(void)
a8f0bd18 168{
12027dbe
LOK
169 m_controller->AddLog(CEC_LOG_DEBUG, "communication thread started");
170
13fd6a66 171 while (!IsStopped())
a8f0bd18 172 {
b9abf920 173 ReadFromDevice(500);
3c53ac93
LOK
174 WriteNextCommand();
175 Sleep(5);
a8f0bd18
LOK
176 }
177
a8f0bd18
LOK
178 return NULL;
179}
180
25701fa6 181bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout)
a8f0bd18 182{
25701fa6 183 int32_t iBytesRead;
13fd6a66
LOK
184 uint8_t buff[1024];
185 if (!m_port)
186 return false;
b6c82769 187
13fd6a66
LOK
188 iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout);
189 if (iBytesRead < 0 || iBytesRead > 256)
a8f0bd18 190 {
13fd6a66
LOK
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);
13fd6a66 194 return false;
a8f0bd18 195 }
13fd6a66
LOK
196 else if (iBytesRead > 0)
197 AddData(buff, (uint8_t) iBytesRead);
25701fa6 198
13fd6a66 199 return iBytesRead > 0;
a8f0bd18
LOK
200}
201
8bca69de 202void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen)
a8f0bd18 203{
d5bffd3c 204 CLockObject lock(&m_bufferMutex);
50aa01e6
LOK
205 for (unsigned int iPtr = 0; iPtr < iLen; iPtr++)
206 m_inBuffer.Push(data[iPtr]);
d5bffd3c
LOK
207
208 m_rcvCondition.Signal();
a8f0bd18
LOK
209}
210
3c53ac93 211void CAdapterCommunication::WriteNextCommand(void)
a8f0bd18 212{
5a1e87c8 213 CCECAdapterMessagePtr msg;
3c53ac93 214 if (m_outBuffer.Pop(msg))
a8f0bd18 215 {
5a1e87c8 216 if (m_port->Write(msg) != (int32_t) msg.get()->size())
3c53ac93
LOK
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");
5a1e87c8 225 CCondition::Sleep((uint32_t) msg.get()->size() * (uint32_t)24 /*data*/ + (uint32_t)5 /*start bit (4.5 ms)*/);
3c53ac93 226 }
25701fa6 227 }
3c53ac93 228}
2abe74eb 229
5a1e87c8 230bool CAdapterCommunication::Write(CCECAdapterMessagePtr data)
3c53ac93
LOK
231{
232 m_outBuffer.Push(data);
a8f0bd18
LOK
233 return true;
234}
235
220537f2 236bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout)
a8f0bd18 237{
d5bffd3c 238 CLockObject lock(&m_bufferMutex);
a8f0bd18 239
50aa01e6
LOK
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);
a8f0bd18 246
50aa01e6 247 while(!bGotFullMessage && iNow < iTarget)
a8f0bd18 248 {
50aa01e6
LOK
249 uint8_t buf = 0;
250 if (!m_inBuffer.Pop(buf))
a8f0bd18 251 {
50aa01e6
LOK
252 if (!m_rcvCondition.Wait(&m_bufferMutex, iTarget - iNow))
253 return false;
a8f0bd18 254 }
a8f0bd18 255
50aa01e6 256 if (!bGotStart)
a8f0bd18 257 {
50aa01e6
LOK
258 if (buf == MSGSTART)
259 bGotStart = true;
260 continue;
a8f0bd18 261 }
50aa01e6 262 else if (buf == MSGSTART) //we found a msgstart before msgend, this is not right, remove
a8f0bd18 263 {
50aa01e6
LOK
264 m_controller->AddLog(CEC_LOG_ERROR, "received MSGSTART before MSGEND");
265 msg.clear();
266 bGotStart = true;
a8f0bd18 267 }
a8f0bd18 268
50aa01e6 269 if (buf == MSGEND)
a8f0bd18 270 {
50aa01e6 271 bGotFullMessage = true;
a8f0bd18 272 }
50aa01e6
LOK
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);
a8f0bd18
LOK
282 }
283
50aa01e6 284 return bGotFullMessage;
a8f0bd18
LOK
285}
286
828682d3 287std::string CAdapterCommunication::GetError(void) const
a8f0bd18
LOK
288{
289 return m_port->GetError();
290}
2abe74eb
LOK
291
292bool CAdapterCommunication::StartBootloader(void)
293{
294 if (!IsRunning())
295 return false;
296
297 m_controller->AddLog(CEC_LOG_DEBUG, "starting the bootloader");
5a1e87c8 298 CCECAdapterMessagePtr output(new CCECAdapterMessage);
25701fa6 299
5a1e87c8
LOK
300 output->push_back(MSGSTART);
301 output->push_escaped(MSGCODE_START_BOOTLOADER);
302 output->push_back(MSGEND);
2abe74eb
LOK
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
2abe74eb
LOK
313bool 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
5a1e87c8 322 CCECAdapterMessagePtr output(new CCECAdapterMessage);
2abe74eb 323
5a1e87c8
LOK
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);
2abe74eb
LOK
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
339bool CAdapterCommunication::PingAdapter(void)
340{
341 if (!IsRunning())
342 return false;
343
344 m_controller->AddLog(CEC_LOG_DEBUG, "sending ping");
5a1e87c8 345 CCECAdapterMessagePtr output(new CCECAdapterMessage);
25701fa6 346
5a1e87c8
LOK
347 output->push_back(MSGSTART);
348 output->push_escaped(MSGCODE_PING);
349 output->push_back(MSGEND);
2abe74eb
LOK
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}
13fd6a66
LOK
362
363bool CAdapterCommunication::IsOpen(void) const
364{
365 return !IsStopped() && m_port->IsOpen();
366}