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 "libPlatform/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))
113 CCondition::Sleep(50);
116 m_controller
->AddLog(CEC_LOG_DEBUG
, "reader thread terminated");
118 CLockObject
lock(&m_commMutex
);
123 bool CAdapterCommunication::ReadFromDevice(int iTimeout
)
126 CLockObject
lock(&m_commMutex
);
127 int iBytesRead
= m_port
->Read(buff
, sizeof(buff
), iTimeout
);
132 strError
.Format("error reading from serial port: %s", m_port
->GetError().c_str());
133 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
136 else if (iBytesRead
> 0)
137 AddData(buff
, iBytesRead
);
142 void CAdapterCommunication::AddData(uint8_t *data
, int iLen
)
144 CLockObject
lock(&m_bufferMutex
);
145 if (iLen
+ m_iInbufUsed
> m_iInbufSize
)
147 m_iInbufSize
= iLen
+ m_iInbufUsed
;
148 m_inbuf
= (uint8_t*)realloc(m_inbuf
, m_iInbufSize
);
151 memcpy(m_inbuf
+ m_iInbufUsed
, data
, iLen
);
152 m_iInbufUsed
+= iLen
;
154 m_condition
.Signal();
157 bool CAdapterCommunication::Write(const cec_frame
&data
)
159 CLockObject
lock(&m_commMutex
);
161 if (m_port
->Write(data
) != data
.size())
164 strError
.Format("error writing to serial port: %s", m_port
->GetError().c_str());
165 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
169 m_controller
->AddLog(CEC_LOG_DEBUG
, "command sent");
171 CCondition::Sleep((int) data
.size() * 24 /*data*/ + 5 /*start bit (4.5 ms)*/ + 50 /* to be on the safe side */);
176 bool CAdapterCommunication::Read(cec_frame
&msg
, int iTimeout
)
178 CLockObject
lock(&m_bufferMutex
);
180 if (m_iInbufUsed
< 1)
181 m_condition
.Wait(&m_bufferMutex
, iTimeout
);
183 if (m_iInbufUsed
< 1)
186 //search for first start of message
188 for (int i
= 0; i
< m_iInbufUsed
; i
++)
190 if (m_inbuf
[i
] == MSGSTART
)
200 //move anything from the first start of message to the beginning of the buffer
203 memmove(m_inbuf
, m_inbuf
+ startpos
, m_iInbufUsed
- startpos
);
204 m_iInbufUsed
-= startpos
;
207 if (m_iInbufUsed
< 2)
210 //look for end of message
213 for (int i
= 1; i
< m_iInbufUsed
; i
++)
215 if (m_inbuf
[i
] == MSGEND
)
220 else if (m_inbuf
[i
] == MSGSTART
)
227 if (startpos
> 0) //we found a msgstart before msgend, this is not right, remove
229 m_controller
->AddLog(CEC_LOG_ERROR
, "received MSGSTART before MSGEND");
230 memmove(m_inbuf
, m_inbuf
+ startpos
, m_iInbufUsed
- startpos
);
231 m_iInbufUsed
-= startpos
;
235 if (endpos
> 0) //found a MSGEND
239 for (int i
= 1; i
< endpos
; i
++)
243 msg
.push_back(m_inbuf
[i
] + (uint8_t)ESCOFFSET
);
246 else if (m_inbuf
[i
] == MSGESC
)
252 msg
.push_back(m_inbuf
[i
]);
256 if (endpos
+ 1 < m_iInbufUsed
)
257 memmove(m_inbuf
, m_inbuf
+ endpos
+ 1, m_iInbufUsed
- endpos
- 1);
259 m_iInbufUsed
-= endpos
+ 1;
267 std::string
CAdapterCommunication::GetError(void) const
269 return m_port
->GetError();
272 bool CAdapterCommunication::StartBootloader(void)
277 m_controller
->AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
279 output
.push_back(MSGSTART
);
280 PushEscaped(output
, MSGCODE_START_BOOTLOADER
);
281 output
.push_back(MSGEND
);
285 m_controller
->AddLog(CEC_LOG_ERROR
, "could not start the bootloader");
288 m_controller
->AddLog(CEC_LOG_DEBUG
, "bootloader start command transmitted");
292 void CAdapterCommunication::PushEscaped(cec_frame
&vec
, uint8_t byte
)
294 if (byte
>= MSGESC
&& byte
!= MSGSTART
)
296 vec
.push_back(MSGESC
);
297 vec
.push_back(byte
- ESCOFFSET
);
305 bool CAdapterCommunication::SetAckMask(uint16_t iMask
)
311 strLog
.Format("setting ackmask to %2x", iMask
);
312 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
316 output
.push_back(MSGSTART
);
317 PushEscaped(output
, MSGCODE_SET_ACK_MASK
);
318 PushEscaped(output
, iMask
>> 8);
319 PushEscaped(output
, (uint8_t)iMask
);
320 output
.push_back(MSGEND
);
324 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the ackmask");
331 bool CAdapterCommunication::PingAdapter(void)
336 m_controller
->AddLog(CEC_LOG_DEBUG
, "sending ping");
338 output
.push_back(MSGSTART
);
339 PushEscaped(output
, MSGCODE_PING
);
340 output
.push_back(MSGEND
);
344 m_controller
->AddLog(CEC_LOG_ERROR
, "could not send ping command");
348 m_controller
->AddLog(CEC_LOG_DEBUG
, "ping tranmitted");
350 // TODO check for pong