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
)
91 void CCECAdapterMessage::push_escaped(int16_t byte
)
93 if (byte
>= MSGESC
&& byte
!= MSGSTART
)
96 push_back(byte
- ESCOFFSET
);
102 CAdapterCommunication::CAdapterCommunication(CLibCEC
*controller
) :
104 m_controller(controller
)
106 m_port
= new CSerialPort
;
109 CAdapterCommunication::~CAdapterCommunication(void)
120 bool CAdapterCommunication::Open(const char *strPort
, uint16_t iBaudRate
/* = 38400 */, uint32_t iTimeoutMs
/* = 10000 */)
122 CLockObject
lock(&m_mutex
);
125 m_controller
->AddLog(CEC_LOG_ERROR
, "port is NULL");
131 m_controller
->AddLog(CEC_LOG_ERROR
, "port is already open");
134 if (!m_port
->Open(strPort
, iBaudRate
))
137 strError
.Format("error opening serial port '%s': %s", strPort
, m_port
->GetError().c_str());
138 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
142 m_controller
->AddLog(CEC_LOG_DEBUG
, "connection opened");
144 //clear any input bytes
146 m_port
->Read(buff
, sizeof(buff
), 500);
150 m_controller
->AddLog(CEC_LOG_DEBUG
, "communication thread created");
155 m_controller
->AddLog(CEC_LOG_DEBUG
, "could not create a communication thread");
161 void CAdapterCommunication::Close(void)
163 CLockObject
lock(&m_mutex
);
166 m_rcvCondition
.Broadcast();
169 void *CAdapterCommunication::Process(void)
171 m_controller
->AddLog(CEC_LOG_DEBUG
, "communication thread started");
183 bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout
)
190 iBytesRead
= m_port
->Read(buff
, sizeof(buff
), iTimeout
);
191 if (iBytesRead
< 0 || iBytesRead
> 256)
194 strError
.Format("error reading from serial port: %s", m_port
->GetError().c_str());
195 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
198 else if (iBytesRead
> 0)
199 AddData(buff
, (uint8_t) iBytesRead
);
201 return iBytesRead
> 0;
204 void CAdapterCommunication::AddData(uint8_t *data
, uint8_t iLen
)
206 CLockObject
lock(&m_mutex
);
207 for (unsigned int iPtr
= 0; iPtr
< iLen
; iPtr
++)
208 m_inBuffer
.Push(data
[iPtr
]);
210 m_rcvCondition
.Signal();
213 void CAdapterCommunication::WriteNextCommand(void)
215 CCECAdapterMessagePtr msg
;
216 if (m_outBuffer
.Pop(msg
))
218 if (m_port
->Write(msg
) != (int32_t) msg
.get()->size())
221 strError
.Format("error writing to serial port: %s", m_port
->GetError().c_str());
222 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
226 m_controller
->AddLog(CEC_LOG_DEBUG
, "command sent");
227 CCondition::Sleep((uint32_t) msg
.get()->size() * (uint32_t)24 /*data*/ + (uint32_t)5 /*start bit (4.5 ms)*/);
232 bool CAdapterCommunication::Write(CCECAdapterMessagePtr data
)
234 m_outBuffer
.Push(data
);
238 bool CAdapterCommunication::Read(CCECAdapterMessage
&msg
, uint32_t iTimeout
)
240 CLockObject
lock(&m_mutex
);
243 uint64_t iNow
= GetTimeMs();
244 uint64_t iTarget
= iNow
+ iTimeout
;
245 bool bGotFullMessage(false);
246 bool bNextIsEscaped(false);
247 bool bGotStart(false);
249 while(!bGotFullMessage
&& iNow
< iTarget
)
252 if (!m_inBuffer
.Pop(buf
))
254 if (!m_rcvCondition
.Wait(&m_mutex
, iTarget
- iNow
))
264 else if (buf
== MSGSTART
) //we found a msgstart before msgend, this is not right, remove
266 m_controller
->AddLog(CEC_LOG_ERROR
, "received MSGSTART before MSGEND");
273 bGotFullMessage
= true;
275 else if (bNextIsEscaped
)
277 msg
.push_back(buf
+ (uint8_t)ESCOFFSET
);
278 bNextIsEscaped
= false;
280 else if (buf
== MSGESC
)
281 bNextIsEscaped
= true;
286 return bGotFullMessage
;
289 std::string
CAdapterCommunication::GetError(void) const
291 return m_port
->GetError();
294 bool CAdapterCommunication::StartBootloader(void)
299 m_controller
->AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
300 CCECAdapterMessagePtr
output(new CCECAdapterMessage
);
302 output
->push_back(MSGSTART
);
303 output
->push_escaped(MSGCODE_START_BOOTLOADER
);
304 output
->push_back(MSGEND
);
308 m_controller
->AddLog(CEC_LOG_ERROR
, "could not start the bootloader");
311 m_controller
->AddLog(CEC_LOG_DEBUG
, "bootloader start command transmitted");
315 bool CAdapterCommunication::SetAckMask(uint16_t iMask
)
321 strLog
.Format("setting ackmask to %2x", iMask
);
322 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
324 CCECAdapterMessagePtr
output(new CCECAdapterMessage
);
326 output
->push_back(MSGSTART
);
327 output
->push_escaped(MSGCODE_SET_ACK_MASK
);
328 output
->push_escaped(iMask
>> 8);
329 output
->push_escaped((uint8_t)iMask
);
330 output
->push_back(MSGEND
);
334 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the ackmask");
341 bool CAdapterCommunication::PingAdapter(void)
346 m_controller
->AddLog(CEC_LOG_DEBUG
, "sending ping");
347 CCECAdapterMessagePtr
output(new CCECAdapterMessage
);
349 output
->push_back(MSGSTART
);
350 output
->push_escaped(MSGCODE_PING
);
351 output
->push_back(MSGEND
);
355 m_controller
->AddLog(CEC_LOG_ERROR
, "could not send ping command");
359 m_controller
->AddLog(CEC_LOG_DEBUG
, "ping tranmitted");
361 // TODO check for pong
365 bool CAdapterCommunication::IsOpen(void) const
367 return !IsStopped() && m_port
->IsOpen();