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)
61 bool CAdapterCommunication::Open(const char *strPort
, int iBaudRate
/* = 38400 */, int iTimeoutMs
/* = 10000 */)
63 CLockObject
lock(&m_commMutex
);
67 if (!m_port
->Open(strPort
, iBaudRate
))
70 strError
.Format("error opening serial port '%s': %s", strPort
, m_port
->GetError().c_str());
71 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
75 m_controller
->AddLog(CEC_LOG_DEBUG
, "connection opened");
77 //clear any input bytes
79 m_port
->Read(buff
, sizeof(buff
), 50);
81 CCondition::Sleep(CEC_SETTLE_DOWN_TIME
);
88 m_controller
->AddLog(CEC_LOG_DEBUG
, "reader thread created");
93 m_controller
->AddLog(CEC_LOG_DEBUG
, "could not create a reader thread");
99 void CAdapterCommunication::Close(void)
105 void *CAdapterCommunication::Process(void)
109 if (!ReadFromDevice(250))
116 CCondition::Sleep(50);
119 m_controller
->AddLog(CEC_LOG_DEBUG
, "reader thread terminated");
121 CLockObject
lock(&m_commMutex
);
126 bool CAdapterCommunication::ReadFromDevice(int iTimeout
)
129 CLockObject
lock(&m_commMutex
);
130 int iBytesRead
= m_port
->Read(buff
, sizeof(buff
), iTimeout
);
135 strError
.Format("error reading from serial port: %s", m_port
->GetError().c_str());
136 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
139 else if (iBytesRead
> 0)
140 AddData(buff
, iBytesRead
);
145 void CAdapterCommunication::AddData(uint8_t *data
, int iLen
)
147 CLockObject
lock(&m_bufferMutex
);
148 if (iLen
+ m_iInbufUsed
> m_iInbufSize
)
150 m_iInbufSize
= iLen
+ m_iInbufUsed
;
151 m_inbuf
= (uint8_t*)realloc(m_inbuf
, m_iInbufSize
);
154 memcpy(m_inbuf
+ m_iInbufUsed
, data
, iLen
);
155 m_iInbufUsed
+= iLen
;
157 m_condition
.Signal();
160 bool CAdapterCommunication::Write(const cec_frame
&data
)
162 CLockObject
lock(&m_commMutex
);
164 if (m_port
->Write(data
) != data
.size())
167 strError
.Format("error writing to serial port: %s", m_port
->GetError().c_str());
168 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
172 m_controller
->AddLog(CEC_LOG_DEBUG
, "command sent");
174 CCondition::Sleep((int) data
.size() * 24 /*data*/ + 5 /*start bit (4.5 ms)*/ + 50 /* to be on the safe side */);
179 bool CAdapterCommunication::Read(cec_frame
&msg
, int iTimeout
)
181 CLockObject
lock(&m_bufferMutex
);
183 if (m_iInbufUsed
< 1)
184 m_condition
.Wait(&m_bufferMutex
, iTimeout
);
186 if (m_iInbufUsed
< 1)
189 //search for first start of message
191 for (int i
= 0; i
< m_iInbufUsed
; i
++)
193 if (m_inbuf
[i
] == MSGSTART
)
203 //move anything from the first start of message to the beginning of the buffer
206 memmove(m_inbuf
, m_inbuf
+ startpos
, m_iInbufUsed
- startpos
);
207 m_iInbufUsed
-= startpos
;
210 if (m_iInbufUsed
< 2)
213 //look for end of message
216 for (int i
= 1; i
< m_iInbufUsed
; i
++)
218 if (m_inbuf
[i
] == MSGEND
)
223 else if (m_inbuf
[i
] == MSGSTART
)
230 if (startpos
> 0) //we found a msgstart before msgend, this is not right, remove
232 m_controller
->AddLog(CEC_LOG_ERROR
, "received MSGSTART before MSGEND");
233 memmove(m_inbuf
, m_inbuf
+ startpos
, m_iInbufUsed
- startpos
);
234 m_iInbufUsed
-= startpos
;
238 if (endpos
> 0) //found a MSGEND
242 for (int i
= 1; i
< endpos
; i
++)
246 msg
.push_back(m_inbuf
[i
] + (uint8_t)ESCOFFSET
);
249 else if (m_inbuf
[i
] == MSGESC
)
255 msg
.push_back(m_inbuf
[i
]);
259 if (endpos
+ 1 < m_iInbufUsed
)
260 memmove(m_inbuf
, m_inbuf
+ endpos
+ 1, m_iInbufUsed
- endpos
- 1);
262 m_iInbufUsed
-= endpos
+ 1;
270 std::string
CAdapterCommunication::GetError(void) const
272 return m_port
->GetError();
275 bool CAdapterCommunication::StartBootloader(void)
280 m_controller
->AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
282 output
.push_back(MSGSTART
);
283 PushEscaped(output
, MSGCODE_START_BOOTLOADER
);
284 output
.push_back(MSGEND
);
288 m_controller
->AddLog(CEC_LOG_ERROR
, "could not start the bootloader");
291 m_controller
->AddLog(CEC_LOG_DEBUG
, "bootloader start command transmitted");
295 void CAdapterCommunication::PushEscaped(cec_frame
&vec
, uint8_t byte
)
297 if (byte
>= MSGESC
&& byte
!= MSGSTART
)
299 vec
.push_back(MSGESC
);
300 vec
.push_back(byte
- ESCOFFSET
);
308 bool CAdapterCommunication::SetAckMask(uint16_t iMask
)
314 strLog
.Format("setting ackmask to %2x", iMask
);
315 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
319 output
.push_back(MSGSTART
);
320 PushEscaped(output
, MSGCODE_SET_ACK_MASK
);
321 PushEscaped(output
, iMask
>> 8);
322 PushEscaped(output
, (uint8_t)iMask
);
323 output
.push_back(MSGEND
);
327 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the ackmask");
334 bool CAdapterCommunication::PingAdapter(void)
339 m_controller
->AddLog(CEC_LOG_DEBUG
, "sending ping");
341 output
.push_back(MSGSTART
);
342 PushEscaped(output
, MSGCODE_PING
);
343 output
.push_back(MSGEND
);
347 m_controller
->AddLog(CEC_LOG_ERROR
, "could not send ping command");
351 m_controller
->AddLog(CEC_LOG_DEBUG
, "ping tranmitted");
353 // TODO check for pong