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"
38 #include "../LibCEC.h"
42 using namespace PLATFORM
;
44 CAdapterCommunication::CAdapterCommunication(CCECProcessor
*processor
) :
46 m_processor(processor
),
48 m_iFirmwareVersion(CEC_FW_VERSION_UNKNOWN
)
50 m_port
= new PLATFORM::CSerialPort
;
53 CAdapterCommunication::~CAdapterCommunication(void)
64 bool CAdapterCommunication::Open(const char *strPort
, uint16_t iBaudRate
/* = 38400 */, uint32_t iTimeoutMs
/* = 10000 */)
66 uint64_t iNow
= GetTimeMs();
67 uint64_t iTimeout
= iNow
+ iTimeoutMs
;
69 CLockObject
lock(m_mutex
);
73 CLibCEC::AddLog(CEC_LOG_ERROR
, "port is NULL");
79 CLibCEC::AddLog(CEC_LOG_ERROR
, "port is already open");
84 bool bConnected(false);
85 while (!bConnected
&& iNow
< iTimeout
)
87 if ((bConnected
= m_port
->Open(strPort
, iBaudRate
)) == false)
89 strError
.Format("error opening serial port '%s': %s", strPort
, m_port
->GetError().c_str());
97 CLibCEC::AddLog(CEC_LOG_ERROR
, strError
);
101 CLibCEC::AddLog(CEC_LOG_DEBUG
, "connection opened, clearing any previous input and waiting for active transmissions to end before starting");
103 //clear any input bytes
105 while (m_port
->Read(buff
, 1024, 100) > 0)
107 CLibCEC::AddLog(CEC_LOG_DEBUG
, "data received, clearing it");
113 CLibCEC::AddLog(CEC_LOG_DEBUG
, "communication thread started");
118 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not create a communication thread");
124 void CAdapterCommunication::Close(void)
126 CLockObject
lock(m_mutex
);
127 m_rcvCondition
.Broadcast();
131 void *CAdapterCommunication::Process(void)
140 CCECAdapterMessage
*msg(NULL
);
141 if (m_outBuffer
.Pop(msg
))
142 msg
->condition
.Broadcast();
147 bool CAdapterCommunication::Write(CCECAdapterMessage
*data
)
151 CLockObject
lock(data
->mutex
);
152 data
->state
= ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT
;
153 m_outBuffer
.Push(data
);
154 data
->condition
.Wait(data
->mutex
);
156 if (data
->state
!= ADAPTER_MESSAGE_STATE_SENT
)
158 CLibCEC::AddLog(CEC_LOG_ERROR
, "command was not sent");
160 else if (data
->expectControllerAck
)
162 bReturn
= WaitForAck(*data
);
165 if (data
->isTransmission
)
166 data
->state
= ADAPTER_MESSAGE_STATE_SENT_ACKED
;
170 data
->state
= ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED
;
171 CLibCEC::AddLog(CEC_LOG_DEBUG
, "did not receive ack");
182 bool CAdapterCommunication::Read(CCECAdapterMessage
&msg
, uint32_t iTimeout
)
184 CLockObject
lock(m_mutex
);
187 uint64_t iNow
= GetTimeMs();
188 uint64_t iTarget
= iNow
+ iTimeout
;
189 bool bGotFullMessage(false);
190 bool bNextIsEscaped(false);
191 bool bGotStart(false);
193 while(!bGotFullMessage
&& iNow
< iTarget
)
196 if (!m_inBuffer
.Pop(buf
))
198 if (!m_rcvCondition
.Wait(m_mutex
, (uint32_t) (iTarget
- iNow
)))
208 else if (buf
== MSGSTART
) //we found a msgstart before msgend, this is not right, remove
211 CLibCEC::AddLog(CEC_LOG_WARNING
, "received MSGSTART before MSGEND, removing previous buffer contents");
218 bGotFullMessage
= true;
220 else if (bNextIsEscaped
)
222 msg
.PushBack(buf
+ (uint8_t)ESCOFFSET
);
223 bNextIsEscaped
= false;
225 else if (buf
== MSGESC
)
226 bNextIsEscaped
= true;
232 msg
.state
= ADAPTER_MESSAGE_STATE_INCOMING
;
234 return bGotFullMessage
;
237 std::string
CAdapterCommunication::GetError(void) const
239 return m_port
->GetError();
242 bool CAdapterCommunication::StartBootloader(void)
248 CLibCEC::AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
249 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
251 output
->PushBack(MSGSTART
);
252 output
->PushEscaped(MSGCODE_START_BOOTLOADER
);
253 output
->PushBack(MSGEND
);
254 output
->isTransmission
= false;
255 output
->expectControllerAck
= false;
257 if ((bReturn
= Write(output
)) == false)
258 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not start the bootloader");
264 bool CAdapterCommunication::PingAdapter(void)
270 CLibCEC::AddLog(CEC_LOG_DEBUG
, "sending ping");
271 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
273 output
->PushBack(MSGSTART
);
274 output
->PushEscaped(MSGCODE_PING
);
275 output
->PushBack(MSGEND
);
276 output
->isTransmission
= false;
278 if ((bReturn
= Write(output
)) == false)
279 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not ping the adapter");
285 uint16_t CAdapterCommunication::GetFirmwareVersion(void)
287 uint16_t iReturn(m_iFirmwareVersion
);
291 if (iReturn
== CEC_FW_VERSION_UNKNOWN
)
293 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting the firmware version");
294 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
296 output
->PushBack(MSGSTART
);
297 output
->PushEscaped(MSGCODE_FIRMWARE_VERSION
);
298 output
->PushBack(MSGEND
);
299 output
->isTransmission
= false;
300 output
->expectControllerAck
= false;
302 SendMessageToAdapter(output
);
305 CCECAdapterMessage input
;
306 if (!Read(input
, CEC_DEFAULT_TRANSMIT_WAIT
) || input
.Message() != MSGCODE_FIRMWARE_VERSION
|| input
.Size() != 3)
307 CLibCEC::AddLog(CEC_LOG_ERROR
, "no or invalid firmware version (size = %d, message = %d)", input
.Size(), input
.Message());
310 m_iFirmwareVersion
= (input
[1] << 8 | input
[2]);
311 iReturn
= m_iFirmwareVersion
;
318 bool CAdapterCommunication::SetLineTimeout(uint8_t iTimeout
)
320 bool bReturn(m_iLineTimeout
!= iTimeout
);
324 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
326 output
->PushBack(MSGSTART
);
327 output
->PushEscaped(MSGCODE_TRANSMIT_IDLETIME
);
328 output
->PushEscaped(iTimeout
);
329 output
->PushBack(MSGEND
);
330 output
->isTransmission
= false;
332 if ((bReturn
= Write(output
)) == false)
333 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not set the idletime");
340 bool CAdapterCommunication::SetAckMask(uint16_t iMask
)
344 strLog
.Format("setting ackmask to %2x", iMask
);
345 CLibCEC::AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
347 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
349 output
->PushBack(MSGSTART
);
350 output
->PushEscaped(MSGCODE_SET_ACK_MASK
);
351 output
->PushEscaped(iMask
>> 8);
352 output
->PushEscaped((uint8_t)iMask
);
353 output
->PushBack(MSGEND
);
354 output
->isTransmission
= false;
356 if ((bReturn
= Write(output
)) == false)
357 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not set the ackmask");
363 bool CAdapterCommunication::IsOpen(void)
365 return !IsStopped() && m_port
->IsOpen() && IsRunning();
368 bool CAdapterCommunication::WaitForAck(CCECAdapterMessage
&message
)
371 bool bTransmitSucceeded(false);
372 uint8_t iPacketsLeft(message
.Size() / 4);
374 int64_t iNow
= GetTimeMs();
375 int64_t iTargetTime
= iNow
+ message
.transmit_timeout
;
377 while (!bTransmitSucceeded
&& !bError
&& (message
.transmit_timeout
== 0 || iNow
< iTargetTime
))
379 CCECAdapterMessage msg
;
380 int32_t iWait
= (int32_t)(iTargetTime
- iNow
);
381 if (iWait
<= 5 || message
.transmit_timeout
<= 5)
382 iWait
= CEC_DEFAULT_TRANSMIT_WAIT
;
384 if (!Read(msg
, iWait
))
390 if (msg
.Message() == MSGCODE_FRAME_START
&& msg
.IsACK())
392 m_processor
->HandlePoll(msg
.Initiator(), msg
.Destination());
397 if (msg
.Message() == MSGCODE_RECEIVE_FAILED
&&
398 m_processor
->HandleReceiveFailed())
404 bError
= msg
.IsError();
407 message
.reply
= msg
.Message();
408 CLibCEC::AddLog(CEC_LOG_DEBUG
, msg
.ToString());
412 switch(msg
.Message())
414 case MSGCODE_COMMAND_ACCEPTED
:
415 CLibCEC::AddLog(CEC_LOG_DEBUG
, msg
.ToString());
416 if (iPacketsLeft
> 0)
418 if (!message
.isTransmission
&& iPacketsLeft
== 0)
419 bTransmitSucceeded
= true;
421 case MSGCODE_TRANSMIT_SUCCEEDED
:
422 CLibCEC::AddLog(CEC_LOG_DEBUG
, msg
.ToString());
423 bTransmitSucceeded
= (iPacketsLeft
== 0);
424 bError
= !bTransmitSucceeded
;
425 message
.reply
= MSGCODE_TRANSMIT_SUCCEEDED
;
428 // ignore other data while waiting
436 return bTransmitSucceeded
&& !bError
;
439 void CAdapterCommunication::AddData(uint8_t *data
, uint8_t iLen
)
441 CLockObject
lock(m_mutex
);
442 for (uint8_t iPtr
= 0; iPtr
< iLen
; iPtr
++)
443 m_inBuffer
.Push(data
[iPtr
]);
445 m_rcvCondition
.Signal();
448 bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout
)
455 CLockObject
lock(m_mutex
);
456 iBytesRead
= m_port
->Read(buff
, sizeof(buff
), iTimeout
);
457 if (iBytesRead
< 0 || iBytesRead
> 256)
460 strError
.Format("error reading from serial port: %s", m_port
->GetError().c_str());
461 CLibCEC::AddLog(CEC_LOG_ERROR
, strError
);
464 else if (iBytesRead
> 0)
465 AddData(buff
, (uint8_t) iBytesRead
);
467 return iBytesRead
> 0;
470 void CAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage
*msg
)
472 CLockObject
adapterLock(m_mutex
);
473 CLockObject
lock(msg
->mutex
);
474 if (m_port
->Write(msg
->packet
.data
, msg
->Size()) != (int32_t) msg
->Size())
477 strError
.Format("error writing to serial port: %s", m_port
->GetError().c_str());
478 CLibCEC::AddLog(CEC_LOG_ERROR
, strError
);
479 msg
->state
= ADAPTER_MESSAGE_STATE_ERROR
;
483 CLibCEC::AddLog(CEC_LOG_DEBUG
, "command sent");
484 msg
->state
= ADAPTER_MESSAGE_STATE_SENT
;
486 msg
->condition
.Signal();
489 void CAdapterCommunication::WriteNextCommand(void)
491 CCECAdapterMessage
*msg(NULL
);
492 if (m_outBuffer
.Pop(msg
))
493 SendMessageToAdapter(msg
);