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)
65 bool CAdapterCommunication::Open(const char *strPort
, uint16_t iBaudRate
/* = 38400 */, uint64_t iTimeoutMs
/* = 10000 */)
67 CLockObject
lock(&m_commMutex
);
68 if (m_bStarted
|| !m_port
)
71 if (!m_port
->Open(strPort
, iBaudRate
))
74 strError
.Format("error opening serial port '%s': %s", strPort
, m_port
->GetError().c_str());
75 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
79 m_controller
->AddLog(CEC_LOG_DEBUG
, "connection opened");
81 //clear any input bytes
83 m_port
->Read(buff
, sizeof(buff
), 50);
85 Sleep(CEC_SETTLE_DOWN_TIME
);
89 m_controller
->AddLog(CEC_LOG_DEBUG
, "reader thread created");
95 m_controller
->AddLog(CEC_LOG_DEBUG
, "could not create a reader thread");
101 void CAdapterCommunication::Close(void)
103 CLockObject
lock(&m_commMutex
);
110 void *CAdapterCommunication::Process(void)
112 m_controller
->AddLog(CEC_LOG_DEBUG
, "communication thread started");
116 CLockObject
lock(&m_commMutex
);
117 if (!ReadFromDevice(250))
134 bool CAdapterCommunication::ReadFromDevice(uint64_t iTimeout
)
140 int32_t iBytesRead
= m_port
->Read(buff
, sizeof(buff
), iTimeout
);
141 if (iBytesRead
< 0 || iBytesRead
> 256)
144 strError
.Format("error reading from serial port: %s", m_port
->GetError().c_str());
145 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
148 else if (iBytesRead
> 0)
149 AddData(buff
, (uint8_t) iBytesRead
);
154 void CAdapterCommunication::AddData(uint8_t *data
, uint8_t iLen
)
156 CLockObject
lock(&m_bufferMutex
);
157 if (iLen
+ m_iInbufUsed
> m_iInbufSize
)
159 m_iInbufSize
= iLen
+ m_iInbufUsed
;
160 m_inbuf
= (uint8_t*)realloc(m_inbuf
, m_iInbufSize
);
163 memcpy(m_inbuf
+ m_iInbufUsed
, data
, iLen
);
164 m_iInbufUsed
+= iLen
;
166 m_condition
.Signal();
169 bool CAdapterCommunication::Write(const cec_frame
&data
)
171 CLockObject
lock(&m_commMutex
);
173 if (m_port
->Write(data
) != (int) data
.size())
176 strError
.Format("error writing to serial port: %s", m_port
->GetError().c_str());
177 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
181 m_controller
->AddLog(CEC_LOG_DEBUG
, "command sent");
183 Sleep((int) data
.size() * 24 /*data*/ + 5 /*start bit (4.5 ms)*/ + 50 /* to be on the safe side */);
188 bool CAdapterCommunication::Read(cec_frame
&msg
, uint64_t iTimeout
)
190 CLockObject
lock(&m_bufferMutex
);
192 if (m_iInbufUsed
< 1)
193 m_condition
.Wait(&m_bufferMutex
, iTimeout
);
195 if (m_iInbufUsed
< 1)
198 //search for first start of message
200 for (int i
= 0; i
< m_iInbufUsed
; i
++)
202 if (m_inbuf
[i
] == MSGSTART
)
212 //move anything from the first start of message to the beginning of the buffer
215 memmove(m_inbuf
, m_inbuf
+ startpos
, m_iInbufUsed
- startpos
);
216 m_iInbufUsed
-= startpos
;
219 if (m_iInbufUsed
< 2)
222 //look for end of message
225 for (int i
= 1; i
< m_iInbufUsed
; i
++)
227 if (m_inbuf
[i
] == MSGEND
)
232 else if (m_inbuf
[i
] == MSGSTART
)
239 if (startpos
> 0) //we found a msgstart before msgend, this is not right, remove
241 m_controller
->AddLog(CEC_LOG_ERROR
, "received MSGSTART before MSGEND");
242 memmove(m_inbuf
, m_inbuf
+ startpos
, m_iInbufUsed
- startpos
);
243 m_iInbufUsed
-= startpos
;
247 if (endpos
> 0) //found a MSGEND
251 for (int i
= 1; i
< endpos
; i
++)
255 msg
.push_back(m_inbuf
[i
] + (uint8_t)ESCOFFSET
);
258 else if (m_inbuf
[i
] == MSGESC
)
264 msg
.push_back(m_inbuf
[i
]);
268 if (endpos
+ 1 < m_iInbufUsed
)
269 memmove(m_inbuf
, m_inbuf
+ endpos
+ 1, m_iInbufUsed
- endpos
- 1);
271 m_iInbufUsed
-= endpos
+ 1;
279 std::string
CAdapterCommunication::GetError(void) const
281 return m_port
->GetError();
284 bool CAdapterCommunication::StartBootloader(void)
289 m_controller
->AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
291 output
.push_back(MSGSTART
);
292 PushEscaped(output
, MSGCODE_START_BOOTLOADER
);
293 output
.push_back(MSGEND
);
297 m_controller
->AddLog(CEC_LOG_ERROR
, "could not start the bootloader");
300 m_controller
->AddLog(CEC_LOG_DEBUG
, "bootloader start command transmitted");
304 void CAdapterCommunication::PushEscaped(cec_frame
&vec
, uint8_t byte
)
306 if (byte
>= MSGESC
&& byte
!= MSGSTART
)
308 vec
.push_back(MSGESC
);
309 vec
.push_back(byte
- ESCOFFSET
);
317 bool CAdapterCommunication::SetAckMask(uint16_t iMask
)
323 strLog
.Format("setting ackmask to %2x", iMask
);
324 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
328 output
.push_back(MSGSTART
);
329 PushEscaped(output
, MSGCODE_SET_ACK_MASK
);
330 PushEscaped(output
, iMask
>> 8);
331 PushEscaped(output
, (uint8_t)iMask
);
332 output
.push_back(MSGEND
);
336 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the ackmask");
343 bool CAdapterCommunication::PingAdapter(void)
348 m_controller
->AddLog(CEC_LOG_DEBUG
, "sending ping");
350 output
.push_back(MSGSTART
);
351 PushEscaped(output
, MSGCODE_PING
);
352 output
.push_back(MSGEND
);
356 m_controller
->AddLog(CEC_LOG_ERROR
, "could not send ping command");
360 m_controller
->AddLog(CEC_LOG_DEBUG
, "ping tranmitted");
362 // TODO check for pong