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 */)
63 CLockObject
lock(&m_commMutex
);
66 m_controller
->AddLog(CEC_LOG_ERROR
, "port is NULL");
72 m_controller
->AddLog(CEC_LOG_ERROR
, "port is already open");
75 if (!m_port
->Open(strPort
, iBaudRate
))
78 strError
.Format("error opening serial port '%s': %s", strPort
, m_port
->GetError().c_str());
79 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
83 m_controller
->AddLog(CEC_LOG_DEBUG
, "connection opened");
85 //clear any input bytes
87 m_port
->Read(buff
, sizeof(buff
), 500);
91 m_controller
->AddLog(CEC_LOG_DEBUG
, "communication thread created");
96 m_controller
->AddLog(CEC_LOG_DEBUG
, "could not create a communication thread");
102 void CAdapterCommunication::Close(void)
104 CLockObject
lock(&m_commMutex
);
107 m_rcvCondition
.Broadcast();
110 void *CAdapterCommunication::Process(void)
112 m_controller
->AddLog(CEC_LOG_DEBUG
, "communication thread started");
124 bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout
)
126 CLockObject
lock(&m_commMutex
);
132 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
);
143 return iBytesRead
> 0;
146 void CAdapterCommunication::AddData(uint8_t *data
, uint8_t iLen
)
148 CLockObject
lock(&m_bufferMutex
);
149 for (unsigned int iPtr
= 0; iPtr
< iLen
; iPtr
++)
150 m_inBuffer
.Push(data
[iPtr
]);
152 m_rcvCondition
.Signal();
155 void CAdapterCommunication::WriteNextCommand(void)
157 CLockObject
lock(&m_commMutex
);
158 cec_adapter_message msg
;
159 if (m_outBuffer
.Pop(msg
))
161 if (m_port
->Write(msg
) != (int32_t) msg
.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");
170 CCondition::Sleep((uint32_t) msg
.size() * (uint32_t)24 /*data*/ + (uint32_t)5 /*start bit (4.5 ms)*/);
175 bool CAdapterCommunication::Write(const cec_adapter_message
&data
)
177 m_outBuffer
.Push(data
);
181 bool CAdapterCommunication::Read(cec_adapter_message
&msg
, uint32_t iTimeout
)
183 CLockObject
lock(&m_bufferMutex
);
186 uint64_t iNow
= GetTimeMs();
187 uint64_t iTarget
= iNow
+ iTimeout
;
188 bool bGotFullMessage(false);
189 bool bNextIsEscaped(false);
190 bool bGotStart(false);
192 while(!bGotFullMessage
&& iNow
< iTarget
)
195 if (!m_inBuffer
.Pop(buf
))
197 if (!m_rcvCondition
.Wait(&m_bufferMutex
, iTarget
- iNow
))
207 else if (buf
== MSGSTART
) //we found a msgstart before msgend, this is not right, remove
209 m_controller
->AddLog(CEC_LOG_ERROR
, "received MSGSTART before MSGEND");
216 bGotFullMessage
= true;
218 else if (bNextIsEscaped
)
220 msg
.push_back(buf
+ (uint8_t)ESCOFFSET
);
221 bNextIsEscaped
= false;
223 else if (buf
== MSGESC
)
224 bNextIsEscaped
= true;
229 return bGotFullMessage
;
232 std::string
CAdapterCommunication::GetError(void) const
234 return m_port
->GetError();
237 bool CAdapterCommunication::StartBootloader(void)
242 m_controller
->AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
243 cec_adapter_message output
;
246 output
.push_back(MSGSTART
);
247 PushEscaped(output
, MSGCODE_START_BOOTLOADER
);
248 output
.push_back(MSGEND
);
252 m_controller
->AddLog(CEC_LOG_ERROR
, "could not start the bootloader");
255 m_controller
->AddLog(CEC_LOG_DEBUG
, "bootloader start command transmitted");
259 void CAdapterCommunication::PushEscaped(cec_adapter_message
&vec
, uint8_t byte
)
261 if (byte
>= MSGESC
&& byte
!= MSGSTART
)
263 vec
.push_back(MSGESC
);
264 vec
.push_back(byte
- ESCOFFSET
);
272 bool CAdapterCommunication::SetAckMask(uint16_t iMask
)
278 strLog
.Format("setting ackmask to %2x", iMask
);
279 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
281 cec_adapter_message output
;
284 output
.push_back(MSGSTART
);
285 PushEscaped(output
, MSGCODE_SET_ACK_MASK
);
286 PushEscaped(output
, iMask
>> 8);
287 PushEscaped(output
, (uint8_t)iMask
);
288 output
.push_back(MSGEND
);
292 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the ackmask");
299 bool CAdapterCommunication::PingAdapter(void)
304 m_controller
->AddLog(CEC_LOG_DEBUG
, "sending ping");
305 cec_adapter_message output
;
308 output
.push_back(MSGSTART
);
309 PushEscaped(output
, MSGCODE_PING
);
310 output
.push_back(MSGEND
);
314 m_controller
->AddLog(CEC_LOG_ERROR
, "could not send ping command");
318 m_controller
->AddLog(CEC_LOG_DEBUG
, "ping tranmitted");
320 // TODO check for pong
324 bool CAdapterCommunication::IsOpen(void) const
326 return !IsStopped() && m_port
->IsOpen();
329 void CAdapterCommunication::FormatAdapterMessage(const cec_command
&command
, cec_adapter_message
&packet
)
333 //set ack polarity to high when transmitting to the broadcast address
334 //set ack polarity low when transmitting to any other address
335 packet
.push_back(MSGSTART
);
336 PushEscaped(packet
, MSGCODE_TRANSMIT_ACK_POLARITY
);
337 if (command
.destination
== CECDEVICE_BROADCAST
)
338 PushEscaped(packet
, CEC_TRUE
);
340 PushEscaped(packet
, CEC_FALSE
);
341 packet
.push_back(MSGEND
);
343 // add source and destination
344 packet
.push_back(MSGSTART
);
345 PushEscaped(packet
, MSGCODE_TRANSMIT
);
346 packet
.push_back(((uint8_t)command
.initiator
<< 4) + (uint8_t)command
.destination
);
347 packet
.push_back(MSGEND
);
350 packet
.push_back(MSGSTART
);
351 PushEscaped(packet
, command
.parameters
.empty() ? (uint8_t)MSGCODE_TRANSMIT_EOM
: (uint8_t)MSGCODE_TRANSMIT
);
352 packet
.push_back((uint8_t) command
.opcode
);
353 packet
.push_back(MSGEND
);
356 for (int8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
358 packet
.push_back(MSGSTART
);
360 if (iPtr
== command
.parameters
.size
- 1)
361 PushEscaped(packet
, MSGCODE_TRANSMIT_EOM
);
363 PushEscaped(packet
, MSGCODE_TRANSMIT
);
365 PushEscaped(packet
, command
.parameters
[iPtr
]);
367 packet
.push_back(MSGEND
);