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 CCECAdapterMessage::CCECAdapterMessage(const cec_command
&command
)
47 //set ack polarity to high when transmitting to the broadcast address
48 //set ack polarity low when transmitting to any other address
50 push_escaped(MSGCODE_TRANSMIT_ACK_POLARITY
);
51 if (command
.destination
== CECDEVICE_BROADCAST
)
52 push_escaped(CEC_TRUE
);
54 push_escaped(CEC_FALSE
);
57 // add source and destination
59 push_escaped(MSGCODE_TRANSMIT
);
60 push_back(((uint8_t)command
.initiator
<< 4) + (uint8_t)command
.destination
);
65 push_escaped(command
.parameters
.empty() ? (uint8_t)MSGCODE_TRANSMIT_EOM
: (uint8_t)MSGCODE_TRANSMIT
);
66 push_back((uint8_t) command
.opcode
);
70 for (int8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
74 if (iPtr
== command
.parameters
.size
- 1)
75 push_escaped( MSGCODE_TRANSMIT_EOM
);
77 push_escaped(MSGCODE_TRANSMIT
);
79 push_escaped(command
.parameters
[iPtr
]);
85 CCECAdapterMessage
&CCECAdapterMessage::operator =(const CCECAdapterMessage
&msg
)
92 void CCECAdapterMessage::push_escaped(int16_t byte
)
94 if (byte
>= MSGESC
&& byte
!= MSGSTART
)
97 push_back((uint8_t) (byte
- ESCOFFSET
));
100 push_back((uint8_t) byte
);
103 CAdapterCommunication::CAdapterCommunication(CLibCEC
*controller
) :
105 m_controller(controller
)
107 m_port
= new CSerialPort
;
110 CAdapterCommunication::~CAdapterCommunication(void)
121 bool CAdapterCommunication::Open(const char *strPort
, uint16_t iBaudRate
/* = 38400 */, uint32_t iTimeoutMs
/* = 10000 */)
123 CLockObject
lock(&m_mutex
);
126 m_controller
->AddLog(CEC_LOG_ERROR
, "port is NULL");
132 m_controller
->AddLog(CEC_LOG_ERROR
, "port is already open");
135 if (!m_port
->Open(strPort
, iBaudRate
))
138 strError
.Format("error opening serial port '%s': %s", strPort
, m_port
->GetError().c_str());
139 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
143 m_controller
->AddLog(CEC_LOG_DEBUG
, "connection opened");
145 //clear any input bytes
147 m_port
->Read(buff
, sizeof(buff
), 500);
151 m_startCondition
.Wait(&m_mutex
);
152 m_controller
->AddLog(CEC_LOG_DEBUG
, "communication thread started");
157 m_controller
->AddLog(CEC_LOG_DEBUG
, "could not create a communication thread");
163 void CAdapterCommunication::Close(void)
165 CLockObject
lock(&m_mutex
);
166 m_startCondition
.Broadcast();
167 m_rcvCondition
.Broadcast();
171 void *CAdapterCommunication::Process(void)
174 CLockObject
lock(&m_mutex
);
175 m_startCondition
.Signal();
188 bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout
)
195 iBytesRead
= m_port
->Read(buff
, sizeof(buff
), iTimeout
);
196 if (iBytesRead
< 0 || iBytesRead
> 256)
199 strError
.Format("error reading from serial port: %s", m_port
->GetError().c_str());
200 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
203 else if (iBytesRead
> 0)
204 AddData(buff
, (uint8_t) iBytesRead
);
206 return iBytesRead
> 0;
209 void CAdapterCommunication::AddData(uint8_t *data
, uint8_t iLen
)
211 CLockObject
lock(&m_mutex
);
212 for (unsigned int iPtr
= 0; iPtr
< iLen
; iPtr
++)
213 m_inBuffer
.Push(data
[iPtr
]);
215 m_rcvCondition
.Signal();
218 void CAdapterCommunication::WriteNextCommand(void)
220 CCECAdapterMessagePtr msg
;
221 if (m_outBuffer
.Pop(msg
))
223 CLockObject
lock(&msg
->mutex
);
224 if (m_port
->Write(msg
) != (int32_t) msg
->size())
227 strError
.Format("error writing to serial port: %s", m_port
->GetError().c_str());
228 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
229 msg
->state
= ADAPTER_MESSAGE_STATE_ERROR
;
233 m_controller
->AddLog(CEC_LOG_DEBUG
, "command sent");
234 CCondition::Sleep((uint32_t) msg
->size() * (uint32_t)24 /*data*/ + (uint32_t)5 /*start bit (4.5 ms)*/);
235 msg
->state
= ADAPTER_MESSAGE_STATE_SENT
;
237 msg
->condition
.Signal();
241 bool CAdapterCommunication::Write(CCECAdapterMessagePtr data
)
243 data
->state
= ADAPTER_MESSAGE_STATE_WAITING
;
244 m_outBuffer
.Push(data
);
248 bool CAdapterCommunication::Read(CCECAdapterMessage
&msg
, uint32_t iTimeout
)
250 CLockObject
lock(&m_mutex
);
253 uint64_t iNow
= GetTimeMs();
254 uint64_t iTarget
= iNow
+ iTimeout
;
255 bool bGotFullMessage(false);
256 bool bNextIsEscaped(false);
257 bool bGotStart(false);
259 while(!bGotFullMessage
&& iNow
< iTarget
)
262 if (!m_inBuffer
.Pop(buf
))
264 if (!m_rcvCondition
.Wait(&m_mutex
, (uint32_t) (iTarget
- iNow
)))
274 else if (buf
== MSGSTART
) //we found a msgstart before msgend, this is not right, remove
276 m_controller
->AddLog(CEC_LOG_ERROR
, "received MSGSTART before MSGEND");
283 bGotFullMessage
= true;
285 else if (bNextIsEscaped
)
287 msg
.push_back(buf
+ (uint8_t)ESCOFFSET
);
288 bNextIsEscaped
= false;
290 else if (buf
== MSGESC
)
291 bNextIsEscaped
= true;
297 msg
.state
= ADAPTER_MESSAGE_STATE_RECEIVED
;
299 return bGotFullMessage
;
302 std::string
CAdapterCommunication::GetError(void) const
304 return m_port
->GetError();
307 bool CAdapterCommunication::StartBootloader(void)
312 m_controller
->AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
313 CCECAdapterMessagePtr
output(new CCECAdapterMessage
);
315 output
->push_back(MSGSTART
);
316 output
->push_escaped(MSGCODE_START_BOOTLOADER
);
317 output
->push_back(MSGEND
);
321 m_controller
->AddLog(CEC_LOG_ERROR
, "could not start the bootloader");
324 m_controller
->AddLog(CEC_LOG_DEBUG
, "bootloader start command transmitted");
328 bool CAdapterCommunication::SetAckMask(uint16_t iMask
)
334 strLog
.Format("setting ackmask to %2x", iMask
);
335 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
337 CCECAdapterMessagePtr
output(new CCECAdapterMessage
);
339 output
->push_back(MSGSTART
);
340 output
->push_escaped(MSGCODE_SET_ACK_MASK
);
341 output
->push_escaped(iMask
>> 8);
342 output
->push_escaped((uint8_t)iMask
);
343 output
->push_back(MSGEND
);
347 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the ackmask");
354 bool CAdapterCommunication::PingAdapter(void)
359 m_controller
->AddLog(CEC_LOG_DEBUG
, "sending ping");
360 CCECAdapterMessagePtr
output(new CCECAdapterMessage
);
362 output
->push_back(MSGSTART
);
363 output
->push_escaped(MSGCODE_PING
);
364 output
->push_back(MSGEND
);
368 m_controller
->AddLog(CEC_LOG_ERROR
, "could not send ping command");
372 m_controller
->AddLog(CEC_LOG_DEBUG
, "ping tranmitted");
374 // TODO check for pong
378 bool CAdapterCommunication::IsOpen(void) const
380 return !IsStopped() && m_port
->IsOpen() && IsRunning();