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"
35 #include "AdapterMessage.h"
36 #include "CECProcessor.h"
37 #include "platform/serialport/serialport.h"
41 using namespace PLATFORM
;
43 CAdapterCommunication::CAdapterCommunication(CCECProcessor
*processor
) :
45 m_processor(processor
),
48 m_port
= new PLATFORM::CSerialPort
;
51 CAdapterCommunication::~CAdapterCommunication(void)
62 bool CAdapterCommunication::Open(const char *strPort
, uint16_t iBaudRate
/* = 38400 */, uint32_t iTimeoutMs
/* = 10000 */)
64 uint64_t iNow
= GetTimeMs();
65 uint64_t iTimeout
= iNow
+ iTimeoutMs
;
67 CLockObject
lock(m_mutex
);
71 m_processor
->AddLog(CEC_LOG_ERROR
, "port is NULL");
77 m_processor
->AddLog(CEC_LOG_ERROR
, "port is already open");
82 bool bConnected(false);
83 while (!bConnected
&& iNow
< iTimeout
)
85 if ((bConnected
= m_port
->Open(strPort
, iBaudRate
)) == false)
87 strError
.Format("error opening serial port '%s': %s", strPort
, m_port
->GetError().c_str());
95 m_processor
->AddLog(CEC_LOG_ERROR
, strError
);
99 m_processor
->AddLog(CEC_LOG_DEBUG
, "connection opened");
101 //clear any input bytes
103 while (m_port
->Read(buff
, 1, 5) == 1) {}
107 m_processor
->AddLog(CEC_LOG_DEBUG
, "communication thread started");
112 m_processor
->AddLog(CEC_LOG_ERROR
, "could not create a communication thread");
118 void CAdapterCommunication::Close(void)
120 CLockObject
lock(m_mutex
);
121 m_rcvCondition
.Broadcast();
125 void *CAdapterCommunication::Process(void)
134 CCECAdapterMessage
*msg(NULL
);
135 if (m_outBuffer
.Pop(msg
))
136 msg
->condition
.Broadcast();
141 bool CAdapterCommunication::Write(CCECAdapterMessage
*data
)
145 CLockObject
lock(data
->mutex
);
146 data
->state
= ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT
;
147 m_outBuffer
.Push(data
);
148 data
->condition
.Wait(data
->mutex
);
150 if (data
->state
!= ADAPTER_MESSAGE_STATE_SENT
)
152 m_processor
->AddLog(CEC_LOG_ERROR
, "command was not sent");
154 if (data
->expectControllerAck
&& WaitForTransmitSucceeded(data
))
156 if (data
->isTransmission
)
157 data
->state
= ADAPTER_MESSAGE_STATE_SENT_ACKED
;
162 data
->state
= ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED
;
163 m_processor
->AddLog(CEC_LOG_DEBUG
, "did not receive ack");
169 bool CAdapterCommunication::Read(CCECAdapterMessage
&msg
, uint32_t iTimeout
)
171 CLockObject
lock(m_mutex
);
174 uint64_t iNow
= GetTimeMs();
175 uint64_t iTarget
= iNow
+ iTimeout
;
176 bool bGotFullMessage(false);
177 bool bNextIsEscaped(false);
178 bool bGotStart(false);
180 while(!bGotFullMessage
&& iNow
< iTarget
)
183 if (!m_inBuffer
.Pop(buf
))
185 if (!m_rcvCondition
.Wait(m_mutex
, (uint32_t) (iTarget
- iNow
)))
195 else if (buf
== MSGSTART
) //we found a msgstart before msgend, this is not right, remove
198 m_processor
->AddLog(CEC_LOG_WARNING
, "received MSGSTART before MSGEND, removing previous buffer contents");
205 bGotFullMessage
= true;
207 else if (bNextIsEscaped
)
209 msg
.PushBack(buf
+ (uint8_t)ESCOFFSET
);
210 bNextIsEscaped
= false;
212 else if (buf
== MSGESC
)
213 bNextIsEscaped
= true;
219 msg
.state
= ADAPTER_MESSAGE_STATE_INCOMING
;
221 return bGotFullMessage
;
224 std::string
CAdapterCommunication::GetError(void) const
226 return m_port
->GetError();
229 bool CAdapterCommunication::StartBootloader(void)
235 m_processor
->AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
236 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
238 output
->PushBack(MSGSTART
);
239 output
->PushEscaped(MSGCODE_START_BOOTLOADER
);
240 output
->PushBack(MSGEND
);
241 output
->isTransmission
= false;
242 output
->expectControllerAck
= false;
244 if ((bReturn
= Write(output
)) == false)
245 m_processor
->AddLog(CEC_LOG_ERROR
, "could not start the bootloader");
251 bool CAdapterCommunication::PingAdapter(void)
257 m_processor
->AddLog(CEC_LOG_DEBUG
, "sending ping");
258 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
260 output
->PushBack(MSGSTART
);
261 output
->PushEscaped(MSGCODE_PING
);
262 output
->PushBack(MSGEND
);
263 output
->isTransmission
= false;
265 if ((bReturn
= Write(output
)) == false)
266 m_processor
->AddLog(CEC_LOG_ERROR
, "could not ping the adapter");
272 bool CAdapterCommunication::SetLineTimeout(uint8_t iTimeout
)
274 bool bReturn(m_iLineTimeout
!= iTimeout
);
278 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
280 output
->PushBack(MSGSTART
);
281 output
->PushEscaped(MSGCODE_TRANSMIT_IDLETIME
);
282 output
->PushEscaped(iTimeout
);
283 output
->PushBack(MSGEND
);
284 output
->isTransmission
= false;
286 if ((bReturn
= Write(output
)) == false)
287 m_processor
->AddLog(CEC_LOG_ERROR
, "could not set the idletime");
294 bool CAdapterCommunication::SetAckMask(uint16_t iMask
)
298 strLog
.Format("setting ackmask to %2x", iMask
);
299 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
301 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
303 output
->PushBack(MSGSTART
);
304 output
->PushEscaped(MSGCODE_SET_ACK_MASK
);
305 output
->PushEscaped(iMask
>> 8);
306 output
->PushEscaped((uint8_t)iMask
);
307 output
->PushBack(MSGEND
);
308 output
->isTransmission
= false;
310 if ((bReturn
= Write(output
)) == false)
311 m_processor
->AddLog(CEC_LOG_ERROR
, "could not set the ackmask");
317 bool CAdapterCommunication::IsOpen(void)
319 return !IsStopped() && m_port
->IsOpen() && IsRunning();
322 bool CAdapterCommunication::WaitForTransmitSucceeded(CCECAdapterMessage
*message
)
325 bool bTransmitSucceeded(false);
326 uint8_t iPacketsLeft(message
->Size() / 4);
328 int64_t iNow
= GetTimeMs();
329 int64_t iTargetTime
= iNow
+ message
->transmit_timeout
;
331 while (!bTransmitSucceeded
&& !bError
&& (message
->transmit_timeout
== 0 || iNow
< iTargetTime
))
333 CCECAdapterMessage msg
;
335 if (!Read(msg
, message
->transmit_timeout
> 0 ? (int32_t)(iTargetTime
- iNow
) : 1000))
341 if (msg
.Message() == MSGCODE_FRAME_START
&& msg
.IsACK())
343 m_processor
->HandlePoll(msg
.Initiator(), msg
.Destination());
348 if (msg
.Message() == MSGCODE_RECEIVE_FAILED
&&
349 m_processor
->HandleReceiveFailed())
355 bError
= msg
.IsError();
358 message
->reply
= msg
.Message();
359 m_processor
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
363 switch(msg
.Message())
365 case MSGCODE_COMMAND_ACCEPTED
:
366 m_processor
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
367 if (iPacketsLeft
> 0)
369 if (!message
->isTransmission
&& iPacketsLeft
== 0)
370 bTransmitSucceeded
= true;
372 case MSGCODE_TRANSMIT_SUCCEEDED
:
373 m_processor
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
374 bTransmitSucceeded
= (iPacketsLeft
== 0);
375 bError
= !bTransmitSucceeded
;
376 message
->reply
= MSGCODE_TRANSMIT_SUCCEEDED
;
379 // ignore other data while waiting
387 return bTransmitSucceeded
&& !bError
;
390 void CAdapterCommunication::AddData(uint8_t *data
, uint8_t iLen
)
392 CLockObject
lock(m_mutex
);
393 for (uint8_t iPtr
= 0; iPtr
< iLen
; iPtr
++)
394 m_inBuffer
.Push(data
[iPtr
]);
396 m_rcvCondition
.Signal();
399 bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout
)
406 iBytesRead
= m_port
->Read(buff
, sizeof(buff
), iTimeout
);
407 if (iBytesRead
< 0 || iBytesRead
> 256)
410 strError
.Format("error reading from serial port: %s", m_port
->GetError().c_str());
411 m_processor
->AddLog(CEC_LOG_ERROR
, strError
);
414 else if (iBytesRead
> 0)
415 AddData(buff
, (uint8_t) iBytesRead
);
417 return iBytesRead
> 0;
420 void CAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage
*msg
)
422 CLockObject
lock(msg
->mutex
);
423 if (m_port
->Write(msg
->packet
.data
, msg
->Size()) != (int32_t) msg
->Size())
426 strError
.Format("error writing to serial port: %s", m_port
->GetError().c_str());
427 m_processor
->AddLog(CEC_LOG_ERROR
, strError
);
428 msg
->state
= ADAPTER_MESSAGE_STATE_ERROR
;
432 m_processor
->AddLog(CEC_LOG_DEBUG
, "command sent");
433 msg
->state
= ADAPTER_MESSAGE_STATE_SENT
;
435 msg
->condition
.Signal();
438 void CAdapterCommunication::WriteNextCommand(void)
440 CCECAdapterMessage
*msg(NULL
);
441 if (m_outBuffer
.Pop(msg
))
442 SendMessageToAdapter(msg
);