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
, uint16_t iBaudRate
/* = 38400 */, uint64_t 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 CLockObject
lock(&m_commMutex
);
124 bool CAdapterCommunication::ReadFromDevice(uint64_t iTimeout
)
127 CLockObject
lock(&m_commMutex
);
131 int32_t iBytesRead
= m_port
->Read(buff
, sizeof(buff
), iTimeout
);
133 if (iBytesRead
< 0 || iBytesRead
> 256)
136 strError
.Format("error reading from serial port: %s", m_port
->GetError().c_str());
137 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
140 else if (iBytesRead
> 0)
141 AddData(buff
, (uint8_t) iBytesRead
);
146 void CAdapterCommunication::AddData(uint8_t *data
, uint8_t iLen
)
148 CLockObject
lock(&m_bufferMutex
);
149 if (iLen
+ m_iInbufUsed
> m_iInbufSize
)
151 m_iInbufSize
= iLen
+ m_iInbufUsed
;
152 m_inbuf
= (uint8_t*)realloc(m_inbuf
, m_iInbufSize
);
155 memcpy(m_inbuf
+ m_iInbufUsed
, data
, iLen
);
156 m_iInbufUsed
+= iLen
;
158 m_condition
.Signal();
161 bool CAdapterCommunication::Write(const cec_frame
&data
)
163 CLockObject
lock(&m_commMutex
);
165 if (m_port
->Write(data
) != (int) data
.size())
168 strError
.Format("error writing to serial port: %s", m_port
->GetError().c_str());
169 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
173 m_controller
->AddLog(CEC_LOG_DEBUG
, "command sent");
175 CCondition::Sleep((int) data
.size() * 24 /*data*/ + 5 /*start bit (4.5 ms)*/ + 50 /* to be on the safe side */);
180 bool CAdapterCommunication::Read(cec_frame
&msg
, uint64_t iTimeout
)
182 CLockObject
lock(&m_bufferMutex
);
184 if (m_iInbufUsed
< 1)
185 m_condition
.Wait(&m_bufferMutex
, iTimeout
);
187 if (m_iInbufUsed
< 1)
190 //search for first start of message
192 for (int i
= 0; i
< m_iInbufUsed
; i
++)
194 if (m_inbuf
[i
] == MSGSTART
)
204 //move anything from the first start of message to the beginning of the buffer
207 memmove(m_inbuf
, m_inbuf
+ startpos
, m_iInbufUsed
- startpos
);
208 m_iInbufUsed
-= startpos
;
211 if (m_iInbufUsed
< 2)
214 //look for end of message
217 for (int i
= 1; i
< m_iInbufUsed
; i
++)
219 if (m_inbuf
[i
] == MSGEND
)
224 else if (m_inbuf
[i
] == MSGSTART
)
231 if (startpos
> 0) //we found a msgstart before msgend, this is not right, remove
233 m_controller
->AddLog(CEC_LOG_ERROR
, "received MSGSTART before MSGEND");
234 memmove(m_inbuf
, m_inbuf
+ startpos
, m_iInbufUsed
- startpos
);
235 m_iInbufUsed
-= startpos
;
239 if (endpos
> 0) //found a MSGEND
243 for (int i
= 1; i
< endpos
; i
++)
247 msg
.push_back(m_inbuf
[i
] + (uint8_t)ESCOFFSET
);
250 else if (m_inbuf
[i
] == MSGESC
)
256 msg
.push_back(m_inbuf
[i
]);
260 if (endpos
+ 1 < m_iInbufUsed
)
261 memmove(m_inbuf
, m_inbuf
+ endpos
+ 1, m_iInbufUsed
- endpos
- 1);
263 m_iInbufUsed
-= endpos
+ 1;
271 std::string
CAdapterCommunication::GetError(void) const
273 return m_port
->GetError();
276 bool CAdapterCommunication::StartBootloader(void)
281 m_controller
->AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
283 output
.push_back(MSGSTART
);
284 PushEscaped(output
, MSGCODE_START_BOOTLOADER
);
285 output
.push_back(MSGEND
);
289 m_controller
->AddLog(CEC_LOG_ERROR
, "could not start the bootloader");
292 m_controller
->AddLog(CEC_LOG_DEBUG
, "bootloader start command transmitted");
296 void CAdapterCommunication::PushEscaped(cec_frame
&vec
, uint8_t byte
)
298 if (byte
>= MSGESC
&& byte
!= MSGSTART
)
300 vec
.push_back(MSGESC
);
301 vec
.push_back(byte
- ESCOFFSET
);
309 bool CAdapterCommunication::SetAckMask(uint16_t iMask
)
315 strLog
.Format("setting ackmask to %2x", iMask
);
316 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
320 output
.push_back(MSGSTART
);
321 PushEscaped(output
, MSGCODE_SET_ACK_MASK
);
322 PushEscaped(output
, iMask
>> 8);
323 PushEscaped(output
, (uint8_t)iMask
);
324 output
.push_back(MSGEND
);
328 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the ackmask");
335 bool CAdapterCommunication::PingAdapter(void)
340 m_controller
->AddLog(CEC_LOG_DEBUG
, "sending ping");
342 output
.push_back(MSGSTART
);
343 PushEscaped(output
, MSGCODE_PING
);
344 output
.push_back(MSGEND
);
348 m_controller
->AddLog(CEC_LOG_ERROR
, "could not send ping command");
352 m_controller
->AddLog(CEC_LOG_DEBUG
, "ping tranmitted");
354 // TODO check for pong