2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
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.
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.
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.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
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/
33 #include "AdapterCommunication.h"
36 #include "platform/serialport.h"
37 #include "util/StdString.h"
42 CAdapterCommunication::CAdapterCommunication(CLibCEC
*controller
) :
44 m_controller(controller
),
49 m_port
= new CSerialPort
;
52 CAdapterCommunication::~CAdapterCommunication(void)
66 bool CAdapterCommunication::Open(const char *strPort
, uint16_t iBaudRate
/* = 38400 */, uint32_t iTimeoutMs
/* = 10000 */)
68 CLockObject
lock(&m_mutex
);
71 m_controller
->AddLog(CEC_LOG_ERROR
, "port is NULL");
77 m_controller
->AddLog(CEC_LOG_ERROR
, "port is already open");
80 if (!m_port
->Open(strPort
, iBaudRate
))
83 strError
.Format("error opening serial port '%s': %s", strPort
, m_port
->GetError().c_str());
84 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
88 m_controller
->AddLog(CEC_LOG_DEBUG
, "connection opened");
90 //clear any input bytes
92 m_port
->Read(buff
, sizeof(buff
), 50);
94 Sleep(CEC_SETTLE_DOWN_TIME
);
98 m_controller
->AddLog(CEC_LOG_DEBUG
, "communication thread created");
103 m_controller
->AddLog(CEC_LOG_DEBUG
, "could not create a communication thread");
109 void CAdapterCommunication::Close(void)
111 m_rcvCondition
.Broadcast();
113 CLockObject
lock(&m_mutex
);
125 void *CAdapterCommunication::Process(void)
127 m_controller
->AddLog(CEC_LOG_DEBUG
, "communication thread started");
133 CLockObject
lock(&m_mutex
, true);
135 bSignal
= ReadFromDevice(50);
139 m_rcvCondition
.Signal();
148 bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout
)
155 iBytesRead
= m_port
->Read(buff
, sizeof(buff
), iTimeout
);
156 if (iBytesRead
< 0 || iBytesRead
> 256)
159 strError
.Format("error reading from serial port: %s", m_port
->GetError().c_str());
160 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
164 else if (iBytesRead
> 0)
165 AddData(buff
, (uint8_t) iBytesRead
);
167 return iBytesRead
> 0;
170 void CAdapterCommunication::AddData(uint8_t *data
, uint8_t iLen
)
172 if (m_iInbufUsed
+ iLen
> m_iInbufSize
)
174 m_iInbufSize
= m_iInbufUsed
+ iLen
;
175 m_inbuf
= (uint8_t*)realloc(m_inbuf
, m_iInbufSize
);
178 memcpy(m_inbuf
+ m_iInbufUsed
, data
, iLen
);
179 m_iInbufUsed
+= iLen
;
182 bool CAdapterCommunication::Write(const cec_frame
&data
)
185 CLockObject
lock(&m_mutex
);
186 if (m_port
->Write(data
) != (int32_t) data
.size
)
189 strError
.Format("error writing to serial port: %s", m_port
->GetError().c_str());
190 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
194 m_controller
->AddLog(CEC_LOG_DEBUG
, "command sent");
196 CCondition::Sleep((uint32_t) data
.size
* (uint32_t)24 /*data*/ + (uint32_t)5 /*start bit (4.5 ms)*/ + (uint32_t)50 /* to be on the safe side */);
202 bool CAdapterCommunication::Read(cec_frame
&msg
, uint32_t iTimeout
)
204 CLockObject
lock(&m_mutex
);
206 if (m_iInbufUsed
< 1)
208 if (!m_rcvCondition
.Wait(&m_mutex
, iTimeout
))
212 if (m_iInbufUsed
< 1 || IsStopped())
215 //search for first start of message
216 int16_t startpos
= -1;
217 for (int16_t iPtr
= 0; iPtr
< m_iInbufUsed
; iPtr
++)
219 if (m_inbuf
[iPtr
] == MSGSTART
)
229 //move anything from the first start of message to the beginning of the buffer
232 memmove(m_inbuf
, m_inbuf
+ startpos
, m_iInbufUsed
- startpos
);
233 m_iInbufUsed
-= startpos
;
236 if (m_iInbufUsed
< 2)
239 //look for end of message
242 for (int16_t iPtr
= 1; iPtr
< m_iInbufUsed
; iPtr
++)
244 if (m_inbuf
[iPtr
] == MSGEND
)
249 else if (m_inbuf
[iPtr
] == MSGSTART
)
256 if (startpos
> 0) //we found a msgstart before msgend, this is not right, remove
258 m_controller
->AddLog(CEC_LOG_ERROR
, "received MSGSTART before MSGEND");
259 memmove(m_inbuf
, m_inbuf
+ startpos
, m_iInbufUsed
- startpos
);
260 m_iInbufUsed
-= startpos
;
264 if (endpos
> 0) //found a MSGEND
268 for (int16_t iPtr
= 1; iPtr
< endpos
; iPtr
++)
272 msg
.push_back(m_inbuf
[iPtr
] + (uint8_t)ESCOFFSET
);
275 else if (m_inbuf
[iPtr
] == MSGESC
)
281 msg
.push_back(m_inbuf
[iPtr
]);
285 if (endpos
+ 1 < m_iInbufUsed
)
286 memmove(m_inbuf
, m_inbuf
+ endpos
+ 1, m_iInbufUsed
- endpos
- 1);
288 m_iInbufUsed
-= endpos
+ 1;
296 std::string
CAdapterCommunication::GetError(void) const
298 return m_port
->GetError();
301 bool CAdapterCommunication::StartBootloader(void)
306 m_controller
->AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
310 output
.push_back(MSGSTART
);
311 PushEscaped(output
, MSGCODE_START_BOOTLOADER
);
312 output
.push_back(MSGEND
);
316 m_controller
->AddLog(CEC_LOG_ERROR
, "could not start the bootloader");
319 m_controller
->AddLog(CEC_LOG_DEBUG
, "bootloader start command transmitted");
323 void CAdapterCommunication::PushEscaped(cec_frame
&vec
, uint8_t byte
)
325 if (byte
>= MSGESC
&& byte
!= MSGSTART
)
327 vec
.push_back(MSGESC
);
328 vec
.push_back(byte
- ESCOFFSET
);
336 bool CAdapterCommunication::SetAckMask(uint16_t iMask
)
342 strLog
.Format("setting ackmask to %2x", iMask
);
343 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
348 output
.push_back(MSGSTART
);
349 PushEscaped(output
, MSGCODE_SET_ACK_MASK
);
350 PushEscaped(output
, iMask
>> 8);
351 PushEscaped(output
, (uint8_t)iMask
);
352 output
.push_back(MSGEND
);
356 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the ackmask");
363 bool CAdapterCommunication::PingAdapter(void)
368 m_controller
->AddLog(CEC_LOG_DEBUG
, "sending ping");
372 output
.push_back(MSGSTART
);
373 PushEscaped(output
, MSGCODE_PING
);
374 output
.push_back(MSGEND
);
378 m_controller
->AddLog(CEC_LOG_ERROR
, "could not send ping command");
382 m_controller
->AddLog(CEC_LOG_DEBUG
, "ping tranmitted");
384 // TODO check for pong
388 bool CAdapterCommunication::IsOpen(void) const
390 return !IsStopped() && m_port
->IsOpen();