cec: removed framebuffer in CCECProcessor. any other packet is an unexpected reply
[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;
88c43521 88 state = msg.state;
ad24cbaf
LOK
89 return *this;
90}
91
92void CCECAdapterMessage::push_escaped(int16_t byte)
93{
94 if (byte >= MSGESC && byte != MSGSTART)
95 {
96 push_back(MSGESC);
56701628 97 push_back((uint8_t) (byte - ESCOFFSET));
ad24cbaf
LOK
98 }
99 else
56701628 100 push_back((uint8_t) byte);
ad24cbaf
LOK
101}
102
2abe74eb 103CAdapterCommunication::CAdapterCommunication(CLibCEC *controller) :
12027dbe 104 m_port(NULL),
50aa01e6 105 m_controller(controller)
a8f0bd18
LOK
106{
107 m_port = new CSerialPort;
108}
109
828682d3 110CAdapterCommunication::~CAdapterCommunication(void)
a8f0bd18 111{
12027dbe
LOK
112 Close();
113
114 if (m_port)
115 {
116 delete m_port;
117 m_port = NULL;
118 }
a8f0bd18
LOK
119}
120
25701fa6 121bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */)
a8f0bd18 122{
f077e8ed 123 CLockObject lock(&m_mutex);
13fd6a66
LOK
124 if (!m_port)
125 {
126 m_controller->AddLog(CEC_LOG_ERROR, "port is NULL");
a8f0bd18 127 return false;
13fd6a66
LOK
128 }
129
130 if (IsOpen())
131 {
132 m_controller->AddLog(CEC_LOG_ERROR, "port is already open");
133 }
a8f0bd18
LOK
134
135 if (!m_port->Open(strPort, iBaudRate))
136 {
137 CStdString strError;
138 strError.Format("error opening serial port '%s': %s", strPort, m_port->GetError().c_str());
2abe74eb 139 m_controller->AddLog(CEC_LOG_ERROR, strError);
a8f0bd18
LOK
140 return false;
141 }
142
2abe74eb 143 m_controller->AddLog(CEC_LOG_DEBUG, "connection opened");
a8f0bd18
LOK
144
145 //clear any input bytes
146 uint8_t buff[1024];
59c9774c 147 m_port->Read(buff, sizeof(buff), 500);
a8f0bd18 148
828682d3 149 if (CreateThread())
a8f0bd18 150 {
b9eea66d
LOK
151 m_startCondition.Wait(&m_mutex);
152 m_controller->AddLog(CEC_LOG_DEBUG, "communication thread started");
a8f0bd18
LOK
153 return true;
154 }
155 else
156 {
13fd6a66 157 m_controller->AddLog(CEC_LOG_DEBUG, "could not create a communication thread");
a8f0bd18
LOK
158 }
159
160 return false;
161}
162
828682d3 163void CAdapterCommunication::Close(void)
a8f0bd18 164{
f077e8ed 165 CLockObject lock(&m_mutex);
b9eea66d 166 m_startCondition.Broadcast();
d5bffd3c 167 m_rcvCondition.Broadcast();
b9eea66d 168 StopThread();
a8f0bd18
LOK
169}
170
828682d3 171void *CAdapterCommunication::Process(void)
a8f0bd18 172{
b9eea66d
LOK
173 {
174 CLockObject lock(&m_mutex);
175 m_startCondition.Signal();
176 }
12027dbe 177
13fd6a66 178 while (!IsStopped())
a8f0bd18 179 {
b9abf920 180 ReadFromDevice(500);
3c53ac93
LOK
181 WriteNextCommand();
182 Sleep(5);
a8f0bd18
LOK
183 }
184
a8f0bd18
LOK
185 return NULL;
186}
187
25701fa6 188bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout)
a8f0bd18 189{
25701fa6 190 int32_t iBytesRead;
13fd6a66
LOK
191 uint8_t buff[1024];
192 if (!m_port)
193 return false;
b6c82769 194
13fd6a66
LOK
195 iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout);
196 if (iBytesRead < 0 || iBytesRead > 256)
a8f0bd18 197 {
13fd6a66
LOK
198 CStdString strError;
199 strError.Format("error reading from serial port: %s", m_port->GetError().c_str());
200 m_controller->AddLog(CEC_LOG_ERROR, strError);
13fd6a66 201 return false;
a8f0bd18 202 }
13fd6a66
LOK
203 else if (iBytesRead > 0)
204 AddData(buff, (uint8_t) iBytesRead);
25701fa6 205
13fd6a66 206 return iBytesRead > 0;
a8f0bd18
LOK
207}
208
8bca69de 209void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen)
a8f0bd18 210{
f077e8ed 211 CLockObject lock(&m_mutex);
50aa01e6
LOK
212 for (unsigned int iPtr = 0; iPtr < iLen; iPtr++)
213 m_inBuffer.Push(data[iPtr]);
d5bffd3c
LOK
214
215 m_rcvCondition.Signal();
a8f0bd18
LOK
216}
217
3c53ac93 218void CAdapterCommunication::WriteNextCommand(void)
a8f0bd18 219{
5a1e87c8 220 CCECAdapterMessagePtr msg;
3c53ac93 221 if (m_outBuffer.Pop(msg))
a8f0bd18 222 {
eb35e4ca 223 CLockObject lock(&msg->mutex);
0e31a62c 224 if (m_port->Write(msg) != (int32_t) msg->size())
3c53ac93
LOK
225 {
226 CStdString strError;
227 strError.Format("error writing to serial port: %s", m_port->GetError().c_str());
228 m_controller->AddLog(CEC_LOG_ERROR, strError);
0e31a62c 229 msg->state = ADAPTER_MESSAGE_STATE_ERROR;
3c53ac93
LOK
230 }
231 else
232 {
233 m_controller->AddLog(CEC_LOG_DEBUG, "command sent");
0e31a62c
LOK
234 CCondition::Sleep((uint32_t) msg->size() * (uint32_t)24 /*data*/ + (uint32_t)5 /*start bit (4.5 ms)*/);
235 msg->state = ADAPTER_MESSAGE_STATE_SENT;
3c53ac93 236 }
eb35e4ca 237 msg->condition.Signal();
25701fa6 238 }
3c53ac93 239}
2abe74eb 240
5a1e87c8 241bool CAdapterCommunication::Write(CCECAdapterMessagePtr data)
3c53ac93 242{
0e31a62c 243 data->state = ADAPTER_MESSAGE_STATE_WAITING;
3c53ac93 244 m_outBuffer.Push(data);
a8f0bd18
LOK
245 return true;
246}
247
220537f2 248bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout)
a8f0bd18 249{
f077e8ed 250 CLockObject lock(&m_mutex);
a8f0bd18 251
50aa01e6
LOK
252 msg.clear();
253 uint64_t iNow = GetTimeMs();
254 uint64_t iTarget = iNow + iTimeout;
255 bool bGotFullMessage(false);
256 bool bNextIsEscaped(false);
257 bool bGotStart(false);
a8f0bd18 258
50aa01e6 259 while(!bGotFullMessage && iNow < iTarget)
a8f0bd18 260 {
50aa01e6
LOK
261 uint8_t buf = 0;
262 if (!m_inBuffer.Pop(buf))
a8f0bd18 263 {
56701628 264 if (!m_rcvCondition.Wait(&m_mutex, (uint32_t) (iTarget - iNow)))
50aa01e6 265 return false;
a8f0bd18 266 }
a8f0bd18 267
50aa01e6 268 if (!bGotStart)
a8f0bd18 269 {
50aa01e6
LOK
270 if (buf == MSGSTART)
271 bGotStart = true;
272 continue;
a8f0bd18 273 }
50aa01e6 274 else if (buf == MSGSTART) //we found a msgstart before msgend, this is not right, remove
a8f0bd18 275 {
50aa01e6
LOK
276 m_controller->AddLog(CEC_LOG_ERROR, "received MSGSTART before MSGEND");
277 msg.clear();
278 bGotStart = true;
a8f0bd18 279 }
a8f0bd18 280
50aa01e6 281 if (buf == MSGEND)
a8f0bd18 282 {
50aa01e6 283 bGotFullMessage = true;
a8f0bd18 284 }
50aa01e6
LOK
285 else if (bNextIsEscaped)
286 {
287 msg.push_back(buf + (uint8_t)ESCOFFSET);
288 bNextIsEscaped = false;
289 }
290 else if (buf == MSGESC)
291 bNextIsEscaped = true;
292 else
293 msg.push_back(buf);
a8f0bd18
LOK
294 }
295
0e31a62c
LOK
296 if (bGotFullMessage)
297 msg.state = ADAPTER_MESSAGE_STATE_RECEIVED;
298
50aa01e6 299 return bGotFullMessage;
a8f0bd18
LOK
300}
301
828682d3 302std::string CAdapterCommunication::GetError(void) const
a8f0bd18
LOK
303{
304 return m_port->GetError();
305}
2abe74eb
LOK
306
307bool CAdapterCommunication::StartBootloader(void)
308{
309 if (!IsRunning())
310 return false;
311
312 m_controller->AddLog(CEC_LOG_DEBUG, "starting the bootloader");
5a1e87c8 313 CCECAdapterMessagePtr output(new CCECAdapterMessage);
25701fa6 314
5a1e87c8
LOK
315 output->push_back(MSGSTART);
316 output->push_escaped(MSGCODE_START_BOOTLOADER);
317 output->push_back(MSGEND);
2abe74eb
LOK
318
319 if (!Write(output))
320 {
321 m_controller->AddLog(CEC_LOG_ERROR, "could not start the bootloader");
322 return false;
323 }
324 m_controller->AddLog(CEC_LOG_DEBUG, "bootloader start command transmitted");
325 return true;
326}
327
2abe74eb
LOK
328bool CAdapterCommunication::SetAckMask(uint16_t iMask)
329{
330 if (!IsRunning())
331 return false;
332
333 CStdString strLog;
334 strLog.Format("setting ackmask to %2x", iMask);
335 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
336
5a1e87c8 337 CCECAdapterMessagePtr output(new CCECAdapterMessage);
2abe74eb 338
5a1e87c8
LOK
339 output->push_back(MSGSTART);
340 output->push_escaped(MSGCODE_SET_ACK_MASK);
341 output->push_escaped(iMask >> 8);
342 output->push_escaped((uint8_t)iMask);
343 output->push_back(MSGEND);
2abe74eb
LOK
344
345 if (!Write(output))
346 {
347 m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask");
348 return false;
349 }
350
351 return true;
352}
353
354bool CAdapterCommunication::PingAdapter(void)
355{
356 if (!IsRunning())
357 return false;
358
359 m_controller->AddLog(CEC_LOG_DEBUG, "sending ping");
5a1e87c8 360 CCECAdapterMessagePtr output(new CCECAdapterMessage);
25701fa6 361
5a1e87c8
LOK
362 output->push_back(MSGSTART);
363 output->push_escaped(MSGCODE_PING);
364 output->push_back(MSGEND);
2abe74eb
LOK
365
366 if (!Write(output))
367 {
368 m_controller->AddLog(CEC_LOG_ERROR, "could not send ping command");
369 return false;
370 }
371
372 m_controller->AddLog(CEC_LOG_DEBUG, "ping tranmitted");
373
374 // TODO check for pong
375 return true;
376}
13fd6a66
LOK
377
378bool CAdapterCommunication::IsOpen(void) const
379{
b9eea66d 380 return !IsStopped() && m_port->IsOpen() && IsRunning();
13fd6a66 381}