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
) :
43 m_controller(controller
),
50 m_port
= new CSerialPort
;
53 CAdapterCommunication::~CAdapterCommunication(void)
59 bool CAdapterCommunication::Open(const char *strPort
, int iBaudRate
/* = 38400 */, int iTimeoutMs
/* = 10000 */)
61 CLockObject
lock(&m_commMutex
);
65 if (!m_port
->Open(strPort
, iBaudRate
))
68 strError
.Format("error opening serial port '%s': %s", strPort
, m_port
->GetError().c_str());
69 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
73 m_controller
->AddLog(CEC_LOG_DEBUG
, "connection opened");
75 //clear any input bytes
77 m_port
->Read(buff
, sizeof(buff
), 50);
79 CCondition::Sleep(CEC_SETTLE_DOWN_TIME
);
86 m_controller
->AddLog(CEC_LOG_DEBUG
, "reader thread created");
91 m_controller
->AddLog(CEC_LOG_DEBUG
, "could not create a reader thread");
97 void CAdapterCommunication::Close(void)
103 void *CAdapterCommunication::Process(void)
107 if (!ReadFromDevice(250))
114 CCondition::Sleep(50);
117 m_controller
->AddLog(CEC_LOG_DEBUG
, "reader thread terminated");
119 CLockObject
lock(&m_commMutex
);
124 bool CAdapterCommunication::ReadFromDevice(int iTimeout
)
127 CLockObject
lock(&m_commMutex
);
128 int iBytesRead
= m_port
->Read(buff
, sizeof(buff
), iTimeout
);
133 strError
.Format("error reading from serial port: %s", m_port
->GetError().c_str());
134 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
137 else if (iBytesRead
> 0)
138 AddData(buff
, iBytesRead
);
143 void CAdapterCommunication::AddData(uint8_t *data
, int iLen
)
145 CLockObject
lock(&m_bufferMutex
);
146 if (iLen
+ m_iInbufUsed
> m_iInbufSize
)
148 m_iInbufSize
= iLen
+ m_iInbufUsed
;
149 m_inbuf
= (uint8_t*)realloc(m_inbuf
, m_iInbufSize
);
152 memcpy(m_inbuf
+ m_iInbufUsed
, data
, iLen
);
153 m_iInbufUsed
+= iLen
;
155 m_condition
.Signal();
158 bool CAdapterCommunication::Write(const cec_frame
&data
)
160 CLockObject
lock(&m_commMutex
);
162 if (m_port
->Write(data
) != data
.size())
165 strError
.Format("error writing to serial port: %s", m_port
->GetError().c_str());
166 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
170 m_controller
->AddLog(CEC_LOG_DEBUG
, "command sent");
172 CCondition::Sleep((int) data
.size() * 24 /*data*/ + 5 /*start bit (4.5 ms)*/ + 50 /* to be on the safe side */);
177 bool CAdapterCommunication::Read(cec_frame
&msg
, int iTimeout
)
179 CLockObject
lock(&m_bufferMutex
);
181 if (m_iInbufUsed
< 1)
182 m_condition
.Wait(&m_bufferMutex
, iTimeout
);
184 if (m_iInbufUsed
< 1)
187 //search for first start of message
189 for (int i
= 0; i
< m_iInbufUsed
; i
++)
191 if (m_inbuf
[i
] == MSGSTART
)
201 //move anything from the first start of message to the beginning of the buffer
204 memmove(m_inbuf
, m_inbuf
+ startpos
, m_iInbufUsed
- startpos
);
205 m_iInbufUsed
-= startpos
;
208 if (m_iInbufUsed
< 2)
211 //look for end of message
214 for (int i
= 1; i
< m_iInbufUsed
; i
++)
216 if (m_inbuf
[i
] == MSGEND
)
221 else if (m_inbuf
[i
] == MSGSTART
)
228 if (startpos
> 0) //we found a msgstart before msgend, this is not right, remove
230 m_controller
->AddLog(CEC_LOG_ERROR
, "received MSGSTART before MSGEND");
231 memmove(m_inbuf
, m_inbuf
+ startpos
, m_iInbufUsed
- startpos
);
232 m_iInbufUsed
-= startpos
;
236 if (endpos
> 0) //found a MSGEND
240 for (int i
= 1; i
< endpos
; i
++)
244 msg
.push_back(m_inbuf
[i
] + (uint8_t)ESCOFFSET
);
247 else if (m_inbuf
[i
] == MSGESC
)
253 msg
.push_back(m_inbuf
[i
]);
257 if (endpos
+ 1 < m_iInbufUsed
)
258 memmove(m_inbuf
, m_inbuf
+ endpos
+ 1, m_iInbufUsed
- endpos
- 1);
260 m_iInbufUsed
-= endpos
+ 1;
268 std::string
CAdapterCommunication::GetError(void) const
270 return m_port
->GetError();
273 bool CAdapterCommunication::StartBootloader(void)
278 m_controller
->AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
280 output
.push_back(MSGSTART
);
281 PushEscaped(output
, MSGCODE_START_BOOTLOADER
);
282 output
.push_back(MSGEND
);
286 m_controller
->AddLog(CEC_LOG_ERROR
, "could not start the bootloader");
289 m_controller
->AddLog(CEC_LOG_DEBUG
, "bootloader start command transmitted");
293 void CAdapterCommunication::PushEscaped(cec_frame
&vec
, uint8_t byte
)
295 if (byte
>= MSGESC
&& byte
!= MSGSTART
)
297 vec
.push_back(MSGESC
);
298 vec
.push_back(byte
- ESCOFFSET
);
306 bool CAdapterCommunication::SetAckMask(uint16_t iMask
)
312 strLog
.Format("setting ackmask to %2x", iMask
);
313 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
317 output
.push_back(MSGSTART
);
318 PushEscaped(output
, MSGCODE_SET_ACK_MASK
);
319 PushEscaped(output
, iMask
>> 8);
320 PushEscaped(output
, (uint8_t)iMask
);
321 output
.push_back(MSGEND
);
325 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the ackmask");
332 bool CAdapterCommunication::PingAdapter(void)
337 m_controller
->AddLog(CEC_LOG_DEBUG
, "sending ping");
339 output
.push_back(MSGSTART
);
340 PushEscaped(output
, MSGCODE_PING
);
341 output
.push_back(MSGEND
);
345 m_controller
->AddLog(CEC_LOG_ERROR
, "could not send ping command");
349 m_controller
->AddLog(CEC_LOG_DEBUG
, "ping tranmitted");
351 // TODO check for pong