cec: removed framebuffer in CCECProcessor. any other packet is an unexpected reply
[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 state = msg.state;
89 return *this;
90 }
91
92 void CCECAdapterMessage::push_escaped(int16_t byte)
93 {
94 if (byte >= MSGESC && byte != MSGSTART)
95 {
96 push_back(MSGESC);
97 push_back((uint8_t) (byte - ESCOFFSET));
98 }
99 else
100 push_back((uint8_t) byte);
101 }
102
103 CAdapterCommunication::CAdapterCommunication(CLibCEC *controller) :
104 m_port(NULL),
105 m_controller(controller)
106 {
107 m_port = new CSerialPort;
108 }
109
110 CAdapterCommunication::~CAdapterCommunication(void)
111 {
112 Close();
113
114 if (m_port)
115 {
116 delete m_port;
117 m_port = NULL;
118 }
119 }
120
121 bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */)
122 {
123 CLockObject lock(&m_mutex);
124 if (!m_port)
125 {
126 m_controller->AddLog(CEC_LOG_ERROR, "port is NULL");
127 return false;
128 }
129
130 if (IsOpen())
131 {
132 m_controller->AddLog(CEC_LOG_ERROR, "port is already open");
133 }
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());
139 m_controller->AddLog(CEC_LOG_ERROR, strError);
140 return false;
141 }
142
143 m_controller->AddLog(CEC_LOG_DEBUG, "connection opened");
144
145 //clear any input bytes
146 uint8_t buff[1024];
147 m_port->Read(buff, sizeof(buff), 500);
148
149 if (CreateThread())
150 {
151 m_startCondition.Wait(&m_mutex);
152 m_controller->AddLog(CEC_LOG_DEBUG, "communication thread started");
153 return true;
154 }
155 else
156 {
157 m_controller->AddLog(CEC_LOG_DEBUG, "could not create a communication thread");
158 }
159
160 return false;
161 }
162
163 void CAdapterCommunication::Close(void)
164 {
165 CLockObject lock(&m_mutex);
166 m_startCondition.Broadcast();
167 m_rcvCondition.Broadcast();
168 StopThread();
169 }
170
171 void *CAdapterCommunication::Process(void)
172 {
173 {
174 CLockObject lock(&m_mutex);
175 m_startCondition.Signal();
176 }
177
178 while (!IsStopped())
179 {
180 ReadFromDevice(500);
181 WriteNextCommand();
182 Sleep(5);
183 }
184
185 return NULL;
186 }
187
188 bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout)
189 {
190 int32_t iBytesRead;
191 uint8_t buff[1024];
192 if (!m_port)
193 return false;
194
195 iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout);
196 if (iBytesRead < 0 || iBytesRead > 256)
197 {
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);
201 return false;
202 }
203 else if (iBytesRead > 0)
204 AddData(buff, (uint8_t) iBytesRead);
205
206 return iBytesRead > 0;
207 }
208
209 void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen)
210 {
211 CLockObject lock(&m_mutex);
212 for (unsigned int iPtr = 0; iPtr < iLen; iPtr++)
213 m_inBuffer.Push(data[iPtr]);
214
215 m_rcvCondition.Signal();
216 }
217
218 void CAdapterCommunication::WriteNextCommand(void)
219 {
220 CCECAdapterMessagePtr msg;
221 if (m_outBuffer.Pop(msg))
222 {
223 CLockObject lock(&msg->mutex);
224 if (m_port->Write(msg) != (int32_t) msg->size())
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);
229 msg->state = ADAPTER_MESSAGE_STATE_ERROR;
230 }
231 else
232 {
233 m_controller->AddLog(CEC_LOG_DEBUG, "command sent");
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;
236 }
237 msg->condition.Signal();
238 }
239 }
240
241 bool CAdapterCommunication::Write(CCECAdapterMessagePtr data)
242 {
243 data->state = ADAPTER_MESSAGE_STATE_WAITING;
244 m_outBuffer.Push(data);
245 return true;
246 }
247
248 bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout)
249 {
250 CLockObject lock(&m_mutex);
251
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);
258
259 while(!bGotFullMessage && iNow < iTarget)
260 {
261 uint8_t buf = 0;
262 if (!m_inBuffer.Pop(buf))
263 {
264 if (!m_rcvCondition.Wait(&m_mutex, (uint32_t) (iTarget - iNow)))
265 return false;
266 }
267
268 if (!bGotStart)
269 {
270 if (buf == MSGSTART)
271 bGotStart = true;
272 continue;
273 }
274 else if (buf == MSGSTART) //we found a msgstart before msgend, this is not right, remove
275 {
276 m_controller->AddLog(CEC_LOG_ERROR, "received MSGSTART before MSGEND");
277 msg.clear();
278 bGotStart = true;
279 }
280
281 if (buf == MSGEND)
282 {
283 bGotFullMessage = true;
284 }
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);
294 }
295
296 if (bGotFullMessage)
297 msg.state = ADAPTER_MESSAGE_STATE_RECEIVED;
298
299 return bGotFullMessage;
300 }
301
302 std::string CAdapterCommunication::GetError(void) const
303 {
304 return m_port->GetError();
305 }
306
307 bool CAdapterCommunication::StartBootloader(void)
308 {
309 if (!IsRunning())
310 return false;
311
312 m_controller->AddLog(CEC_LOG_DEBUG, "starting the bootloader");
313 CCECAdapterMessagePtr output(new CCECAdapterMessage);
314
315 output->push_back(MSGSTART);
316 output->push_escaped(MSGCODE_START_BOOTLOADER);
317 output->push_back(MSGEND);
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
328 bool 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
337 CCECAdapterMessagePtr output(new CCECAdapterMessage);
338
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);
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
354 bool CAdapterCommunication::PingAdapter(void)
355 {
356 if (!IsRunning())
357 return false;
358
359 m_controller->AddLog(CEC_LOG_DEBUG, "sending ping");
360 CCECAdapterMessagePtr output(new CCECAdapterMessage);
361
362 output->push_back(MSGSTART);
363 output->push_escaped(MSGCODE_PING);
364 output->push_back(MSGEND);
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 }
377
378 bool CAdapterCommunication::IsOpen(void) const
379 {
380 return !IsStopped() && m_port->IsOpen() && IsRunning();
381 }