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"
38 #include "platform/timeutils.h"
43 CAdapterCommunication::CAdapterCommunication(CLibCEC
*controller
) :
45 m_controller(controller
)
47 m_port
= new CSerialPort
;
50 CAdapterCommunication::~CAdapterCommunication(void)
61 bool CAdapterCommunication::Open(const char *strPort
, uint16_t iBaudRate
/* = 38400 */, uint32_t iTimeoutMs
/* = 10000 */)
65 m_controller
->AddLog(CEC_LOG_ERROR
, "port is NULL");
71 m_controller
->AddLog(CEC_LOG_ERROR
, "port is already open");
74 if (!m_port
->Open(strPort
, iBaudRate
))
77 strError
.Format("error opening serial port '%s': %s", strPort
, m_port
->GetError().c_str());
78 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
82 m_controller
->AddLog(CEC_LOG_DEBUG
, "connection opened");
84 //clear any input bytes
86 m_port
->Read(buff
, sizeof(buff
), 500);
90 m_controller
->AddLog(CEC_LOG_DEBUG
, "communication thread created");
95 m_controller
->AddLog(CEC_LOG_DEBUG
, "could not create a communication thread");
101 void CAdapterCommunication::Close(void)
105 m_rcvCondition
.Broadcast();
108 void *CAdapterCommunication::Process(void)
110 m_controller
->AddLog(CEC_LOG_DEBUG
, "communication thread started");
122 bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout
)
129 iBytesRead
= m_port
->Read(buff
, sizeof(buff
), iTimeout
);
130 if (iBytesRead
< 0 || iBytesRead
> 256)
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
, (uint8_t) iBytesRead
);
140 return iBytesRead
> 0;
143 void CAdapterCommunication::AddData(uint8_t *data
, uint8_t iLen
)
145 CLockObject
lock(&m_bufferMutex
);
146 for (unsigned int iPtr
= 0; iPtr
< iLen
; iPtr
++)
147 m_inBuffer
.Push(data
[iPtr
]);
149 m_rcvCondition
.Signal();
152 void CAdapterCommunication::WriteNextCommand(void)
154 CCECAdapterMessage msg
;
155 if (m_outBuffer
.Pop(msg
))
157 if (m_port
->Write(msg
) != (int32_t) msg
.size())
160 strError
.Format("error writing to serial port: %s", m_port
->GetError().c_str());
161 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
165 m_controller
->AddLog(CEC_LOG_DEBUG
, "command sent");
166 CCondition::Sleep((uint32_t) msg
.size() * (uint32_t)24 /*data*/ + (uint32_t)5 /*start bit (4.5 ms)*/);
171 bool CAdapterCommunication::Write(const CCECAdapterMessage
&data
)
173 m_outBuffer
.Push(data
);
177 bool CAdapterCommunication::Read(CCECAdapterMessage
&msg
, uint32_t iTimeout
)
179 CLockObject
lock(&m_bufferMutex
);
182 uint64_t iNow
= GetTimeMs();
183 uint64_t iTarget
= iNow
+ iTimeout
;
184 bool bGotFullMessage(false);
185 bool bNextIsEscaped(false);
186 bool bGotStart(false);
188 while(!bGotFullMessage
&& iNow
< iTarget
)
191 if (!m_inBuffer
.Pop(buf
))
193 if (!m_rcvCondition
.Wait(&m_bufferMutex
, iTarget
- iNow
))
203 else if (buf
== MSGSTART
) //we found a msgstart before msgend, this is not right, remove
205 m_controller
->AddLog(CEC_LOG_ERROR
, "received MSGSTART before MSGEND");
212 bGotFullMessage
= true;
214 else if (bNextIsEscaped
)
216 msg
.push_back(buf
+ (uint8_t)ESCOFFSET
);
217 bNextIsEscaped
= false;
219 else if (buf
== MSGESC
)
220 bNextIsEscaped
= true;
225 return bGotFullMessage
;
228 std::string
CAdapterCommunication::GetError(void) const
230 return m_port
->GetError();
233 bool CAdapterCommunication::StartBootloader(void)
238 m_controller
->AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
239 CCECAdapterMessage output
;
241 output
.push_back(MSGSTART
);
242 output
.push_escaped(MSGCODE_START_BOOTLOADER
);
243 output
.push_back(MSGEND
);
247 m_controller
->AddLog(CEC_LOG_ERROR
, "could not start the bootloader");
250 m_controller
->AddLog(CEC_LOG_DEBUG
, "bootloader start command transmitted");
254 bool CAdapterCommunication::SetAckMask(uint16_t iMask
)
260 strLog
.Format("setting ackmask to %2x", iMask
);
261 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
263 CCECAdapterMessage output
;
265 output
.push_back(MSGSTART
);
266 output
.push_escaped(MSGCODE_SET_ACK_MASK
);
267 output
.push_escaped(iMask
>> 8);
268 output
.push_escaped((uint8_t)iMask
);
269 output
.push_back(MSGEND
);
273 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the ackmask");
280 bool CAdapterCommunication::PingAdapter(void)
285 m_controller
->AddLog(CEC_LOG_DEBUG
, "sending ping");
286 CCECAdapterMessage output
;
288 output
.push_back(MSGSTART
);
289 output
.push_escaped(MSGCODE_PING
);
290 output
.push_back(MSGEND
);
294 m_controller
->AddLog(CEC_LOG_ERROR
, "could not send ping command");
298 m_controller
->AddLog(CEC_LOG_DEBUG
, "ping tranmitted");
300 // TODO check for pong
304 bool CAdapterCommunication::IsOpen(void) const
306 return !IsStopped() && m_port
->IsOpen();