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
),
51 m_port
= new CSerialPort
;
54 CAdapterCommunication::~CAdapterCommunication(void)
68 bool CAdapterCommunication::Open(const char *strPort
, uint16_t iBaudRate
/* = 38400 */, uint32_t iTimeoutMs
/* = 10000 */)
70 if (m_bStarted
|| !m_port
)
73 if (!m_port
->Open(strPort
, iBaudRate
))
76 strError
.Format("error opening serial port '%s': %s", strPort
, m_port
->GetError().c_str());
77 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
81 m_controller
->AddLog(CEC_LOG_DEBUG
, "connection opened");
83 //clear any input bytes
85 m_port
->Read(buff
, sizeof(buff
), 50);
87 Sleep(CEC_SETTLE_DOWN_TIME
);
91 m_controller
->AddLog(CEC_LOG_DEBUG
, "reader thread created");
97 m_controller
->AddLog(CEC_LOG_DEBUG
, "could not create a reader thread");
103 void CAdapterCommunication::Close(void)
106 m_rcvCondition
.Broadcast();
114 void *CAdapterCommunication::Process(void)
116 m_controller
->AddLog(CEC_LOG_DEBUG
, "communication thread started");
120 if (!ReadFromDevice(1000))
134 bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout
)
139 CLockObject
lock(&m_mutex
);
145 iBytesRead
= m_port
->Read(buff
, sizeof(buff
), iTimeout
);
146 if (iBytesRead
< 0 || iBytesRead
> 256)
149 strError
.Format("error reading from serial port: %s", m_port
->GetError().c_str());
150 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
153 else if (iBytesRead
> 0)
154 AddData(buff
, (uint8_t) iBytesRead
);
158 m_rcvCondition
.Signal();
163 void CAdapterCommunication::AddData(uint8_t *data
, uint8_t iLen
)
165 if (m_iInbufUsed
+ iLen
> m_iInbufSize
)
167 m_iInbufSize
= m_iInbufUsed
+ iLen
;
168 m_inbuf
= (uint8_t*)realloc(m_inbuf
, m_iInbufSize
);
171 memcpy(m_inbuf
+ m_iInbufUsed
, data
, iLen
);
172 m_iInbufUsed
+= iLen
;
175 bool CAdapterCommunication::Write(const cec_frame
&data
)
178 CLockObject
lock(&m_mutex
);
179 if (m_port
->Write(data
) != (int32_t) data
.size
)
182 strError
.Format("error writing to serial port: %s", m_port
->GetError().c_str());
183 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
187 m_controller
->AddLog(CEC_LOG_DEBUG
, "command sent");
189 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 */);
195 bool CAdapterCommunication::Read(cec_frame
&msg
, uint32_t iTimeout
)
197 CLockObject
lock(&m_mutex
);
199 if (m_iInbufUsed
< 1)
200 m_rcvCondition
.Wait(&m_mutex
, iTimeout
);
202 if (m_iInbufUsed
< 1 || m_bStop
)
205 //search for first start of message
206 int16_t startpos
= -1;
207 for (int16_t iPtr
= 0; iPtr
< m_iInbufUsed
; iPtr
++)
209 if (m_inbuf
[iPtr
] == MSGSTART
)
219 //move anything from the first start of message to the beginning of the buffer
222 memmove(m_inbuf
, m_inbuf
+ startpos
, m_iInbufUsed
- startpos
);
223 m_iInbufUsed
-= startpos
;
226 if (m_iInbufUsed
< 2)
229 //look for end of message
232 for (int16_t iPtr
= 1; iPtr
< m_iInbufUsed
; iPtr
++)
234 if (m_inbuf
[iPtr
] == MSGEND
)
239 else if (m_inbuf
[iPtr
] == MSGSTART
)
246 if (startpos
> 0) //we found a msgstart before msgend, this is not right, remove
248 m_controller
->AddLog(CEC_LOG_ERROR
, "received MSGSTART before MSGEND");
249 memmove(m_inbuf
, m_inbuf
+ startpos
, m_iInbufUsed
- startpos
);
250 m_iInbufUsed
-= startpos
;
254 if (endpos
> 0) //found a MSGEND
258 for (int16_t iPtr
= 1; iPtr
< endpos
; iPtr
++)
262 msg
.push_back(m_inbuf
[iPtr
] + (uint8_t)ESCOFFSET
);
265 else if (m_inbuf
[iPtr
] == MSGESC
)
271 msg
.push_back(m_inbuf
[iPtr
]);
275 if (endpos
+ 1 < m_iInbufUsed
)
276 memmove(m_inbuf
, m_inbuf
+ endpos
+ 1, m_iInbufUsed
- endpos
- 1);
278 m_iInbufUsed
-= endpos
+ 1;
286 std::string
CAdapterCommunication::GetError(void) const
288 return m_port
->GetError();
291 bool CAdapterCommunication::StartBootloader(void)
296 m_controller
->AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
300 output
.push_back(MSGSTART
);
301 PushEscaped(output
, MSGCODE_START_BOOTLOADER
);
302 output
.push_back(MSGEND
);
306 m_controller
->AddLog(CEC_LOG_ERROR
, "could not start the bootloader");
309 m_controller
->AddLog(CEC_LOG_DEBUG
, "bootloader start command transmitted");
313 void CAdapterCommunication::PushEscaped(cec_frame
&vec
, uint8_t byte
)
315 if (byte
>= MSGESC
&& byte
!= MSGSTART
)
317 vec
.push_back(MSGESC
);
318 vec
.push_back(byte
- ESCOFFSET
);
326 bool CAdapterCommunication::SetAckMask(uint16_t iMask
)
332 strLog
.Format("setting ackmask to %2x", iMask
);
333 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
338 output
.push_back(MSGSTART
);
339 PushEscaped(output
, MSGCODE_SET_ACK_MASK
);
340 PushEscaped(output
, iMask
>> 8);
341 PushEscaped(output
, (uint8_t)iMask
);
342 output
.push_back(MSGEND
);
346 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the ackmask");
353 bool CAdapterCommunication::PingAdapter(void)
358 m_controller
->AddLog(CEC_LOG_DEBUG
, "sending ping");
362 output
.push_back(MSGSTART
);
363 PushEscaped(output
, MSGCODE_PING
);
364 output
.push_back(MSGEND
);
368 m_controller
->AddLog(CEC_LOG_ERROR
, "could not send ping command");
372 m_controller
->AddLog(CEC_LOG_DEBUG
, "ping tranmitted");
374 // TODO check for pong