win32: fix build
[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);
56701628 96 push_back((uint8_t) (byte - ESCOFFSET));
ad24cbaf
LOK
97 }
98 else
56701628 99 push_back((uint8_t) byte);
ad24cbaf
LOK
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{
f077e8ed 122 CLockObject lock(&m_mutex);
13fd6a66
LOK
123 if (!m_port)
124 {
125 m_controller->AddLog(CEC_LOG_ERROR, "port is NULL");
a8f0bd18 126 return false;
13fd6a66
LOK
127 }
128
129 if (IsOpen())
130 {
131 m_controller->AddLog(CEC_LOG_ERROR, "port is already open");
132 }
a8f0bd18
LOK
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());
2abe74eb 138 m_controller->AddLog(CEC_LOG_ERROR, strError);
a8f0bd18
LOK
139 return false;
140 }
141
2abe74eb 142 m_controller->AddLog(CEC_LOG_DEBUG, "connection opened");
a8f0bd18
LOK
143
144 //clear any input bytes
145 uint8_t buff[1024];
59c9774c 146 m_port->Read(buff, sizeof(buff), 500);
a8f0bd18 147
828682d3 148 if (CreateThread())
a8f0bd18 149 {
b9eea66d
LOK
150 m_startCondition.Wait(&m_mutex);
151 m_controller->AddLog(CEC_LOG_DEBUG, "communication thread started");
a8f0bd18
LOK
152 return true;
153 }
154 else
155 {
13fd6a66 156 m_controller->AddLog(CEC_LOG_DEBUG, "could not create a communication thread");
a8f0bd18
LOK
157 }
158
159 return false;
160}
161
828682d3 162void CAdapterCommunication::Close(void)
a8f0bd18 163{
f077e8ed 164 CLockObject lock(&m_mutex);
b9eea66d 165 m_startCondition.Broadcast();
d5bffd3c 166 m_rcvCondition.Broadcast();
b9eea66d 167 StopThread();
a8f0bd18
LOK
168}
169
828682d3 170void *CAdapterCommunication::Process(void)
a8f0bd18 171{
b9eea66d
LOK
172 {
173 CLockObject lock(&m_mutex);
174 m_startCondition.Signal();
175 }
12027dbe 176
13fd6a66 177 while (!IsStopped())
a8f0bd18 178 {
b9abf920 179 ReadFromDevice(500);
3c53ac93
LOK
180 WriteNextCommand();
181 Sleep(5);
a8f0bd18
LOK
182 }
183
a8f0bd18
LOK
184 return NULL;
185}
186
25701fa6 187bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout)
a8f0bd18 188{
25701fa6 189 int32_t iBytesRead;
13fd6a66
LOK
190 uint8_t buff[1024];
191 if (!m_port)
192 return false;
b6c82769 193
13fd6a66
LOK
194 iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout);
195 if (iBytesRead < 0 || iBytesRead > 256)
a8f0bd18 196 {
13fd6a66
LOK
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);
13fd6a66 200 return false;
a8f0bd18 201 }
13fd6a66
LOK
202 else if (iBytesRead > 0)
203 AddData(buff, (uint8_t) iBytesRead);
25701fa6 204
13fd6a66 205 return iBytesRead > 0;
a8f0bd18
LOK
206}
207
8bca69de 208void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen)
a8f0bd18 209{
f077e8ed 210 CLockObject lock(&m_mutex);
50aa01e6
LOK
211 for (unsigned int iPtr = 0; iPtr < iLen; iPtr++)
212 m_inBuffer.Push(data[iPtr]);
d5bffd3c
LOK
213
214 m_rcvCondition.Signal();
a8f0bd18
LOK
215}
216
3c53ac93 217void CAdapterCommunication::WriteNextCommand(void)
a8f0bd18 218{
5a1e87c8 219 CCECAdapterMessagePtr msg;
3c53ac93 220 if (m_outBuffer.Pop(msg))
a8f0bd18 221 {
eb35e4ca 222 CLockObject lock(&msg->mutex);
0e31a62c 223 if (m_port->Write(msg) != (int32_t) msg->size())
3c53ac93
LOK
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);
0e31a62c 228 msg->state = ADAPTER_MESSAGE_STATE_ERROR;
3c53ac93
LOK
229 }
230 else
231 {
232 m_controller->AddLog(CEC_LOG_DEBUG, "command sent");
0e31a62c
LOK
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;
3c53ac93 235 }
eb35e4ca 236 msg->condition.Signal();
25701fa6 237 }
3c53ac93 238}
2abe74eb 239
5a1e87c8 240bool CAdapterCommunication::Write(CCECAdapterMessagePtr data)
3c53ac93 241{
0e31a62c 242 data->state = ADAPTER_MESSAGE_STATE_WAITING;
3c53ac93 243 m_outBuffer.Push(data);
a8f0bd18
LOK
244 return true;
245}
246
220537f2 247bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout)
a8f0bd18 248{
f077e8ed 249 CLockObject lock(&m_mutex);
a8f0bd18 250
50aa01e6
LOK
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);
a8f0bd18 257
50aa01e6 258 while(!bGotFullMessage && iNow < iTarget)
a8f0bd18 259 {
50aa01e6
LOK
260 uint8_t buf = 0;
261 if (!m_inBuffer.Pop(buf))
a8f0bd18 262 {
56701628 263 if (!m_rcvCondition.Wait(&m_mutex, (uint32_t) (iTarget - iNow)))
50aa01e6 264 return false;
a8f0bd18 265 }
a8f0bd18 266
50aa01e6 267 if (!bGotStart)
a8f0bd18 268 {
50aa01e6
LOK
269 if (buf == MSGSTART)
270 bGotStart = true;
271 continue;
a8f0bd18 272 }
50aa01e6 273 else if (buf == MSGSTART) //we found a msgstart before msgend, this is not right, remove
a8f0bd18 274 {
50aa01e6
LOK
275 m_controller->AddLog(CEC_LOG_ERROR, "received MSGSTART before MSGEND");
276 msg.clear();
277 bGotStart = true;
a8f0bd18 278 }
a8f0bd18 279
50aa01e6 280 if (buf == MSGEND)
a8f0bd18 281 {
50aa01e6 282 bGotFullMessage = true;
a8f0bd18 283 }
50aa01e6
LOK
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);
a8f0bd18
LOK
293 }
294
0e31a62c
LOK
295 if (bGotFullMessage)
296 msg.state = ADAPTER_MESSAGE_STATE_RECEIVED;
297
50aa01e6 298 return bGotFullMessage;
a8f0bd18
LOK
299}
300
828682d3 301std::string CAdapterCommunication::GetError(void) const
a8f0bd18
LOK
302{
303 return m_port->GetError();
304}
2abe74eb
LOK
305
306bool CAdapterCommunication::StartBootloader(void)
307{
308 if (!IsRunning())
309 return false;
310
311 m_controller->AddLog(CEC_LOG_DEBUG, "starting the bootloader");
5a1e87c8 312 CCECAdapterMessagePtr output(new CCECAdapterMessage);
25701fa6 313
5a1e87c8
LOK
314 output->push_back(MSGSTART);
315 output->push_escaped(MSGCODE_START_BOOTLOADER);
316 output->push_back(MSGEND);
2abe74eb
LOK
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
2abe74eb
LOK
327bool 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
5a1e87c8 336 CCECAdapterMessagePtr output(new CCECAdapterMessage);
2abe74eb 337
5a1e87c8
LOK
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);
2abe74eb
LOK
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
353bool CAdapterCommunication::PingAdapter(void)
354{
355 if (!IsRunning())
356 return false;
357
358 m_controller->AddLog(CEC_LOG_DEBUG, "sending ping");
5a1e87c8 359 CCECAdapterMessagePtr output(new CCECAdapterMessage);
25701fa6 360
5a1e87c8
LOK
361 output->push_back(MSGSTART);
362 output->push_escaped(MSGCODE_PING);
363 output->push_back(MSGEND);
2abe74eb
LOK
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}
13fd6a66
LOK
376
377bool CAdapterCommunication::IsOpen(void) const
378{
b9eea66d 379 return !IsStopped() && m_port->IsOpen() && IsRunning();
13fd6a66 380}