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");
103 //clear any input bytes
105 while (m_port
->Read(buff
, 1, 5) == 1) {}
109 CLibCEC::AddLog(CEC_LOG_DEBUG
, "communication thread started");
114 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not create a communication thread");
120 void CAdapterCommunication::Close(void)
122 CLockObject
lock(m_mutex
);
123 m_rcvCondition
.Broadcast();
127 void *CAdapterCommunication::Process(void)
136 CCECAdapterMessage
*msg(NULL
);
137 if (m_outBuffer
.Pop(msg
))
138 msg
->condition
.Broadcast();
143 bool CAdapterCommunication::Write(CCECAdapterMessage
*data
)
147 CLockObject
lock(data
->mutex
);
148 data
->state
= ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT
;
149 m_outBuffer
.Push(data
);
150 data
->condition
.Wait(data
->mutex
);
152 if (data
->state
!= ADAPTER_MESSAGE_STATE_SENT
)
154 CLibCEC::AddLog(CEC_LOG_ERROR
, "command was not sent");
156 else if (data
->expectControllerAck
)
158 bReturn
= WaitForAck(*data
);
161 if (data
->isTransmission
)
162 data
->state
= ADAPTER_MESSAGE_STATE_SENT_ACKED
;
166 data
->state
= ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED
;
167 CLibCEC::AddLog(CEC_LOG_DEBUG
, "did not receive ack");
178 bool CAdapterCommunication::Read(CCECAdapterMessage
&msg
, uint32_t iTimeout
)
180 CLockObject
lock(m_mutex
);
183 uint64_t iNow
= GetTimeMs();
184 uint64_t iTarget
= iNow
+ iTimeout
;
185 bool bGotFullMessage(false);
186 bool bNextIsEscaped(false);
187 bool bGotStart(false);
189 while(!bGotFullMessage
&& iNow
< iTarget
)
192 if (!m_inBuffer
.Pop(buf
))
194 if (!m_rcvCondition
.Wait(m_mutex
, (uint32_t) (iTarget
- iNow
)))
204 else if (buf
== MSGSTART
) //we found a msgstart before msgend, this is not right, remove
207 CLibCEC::AddLog(CEC_LOG_WARNING
, "received MSGSTART before MSGEND, removing previous buffer contents");
214 bGotFullMessage
= true;
216 else if (bNextIsEscaped
)
218 msg
.PushBack(buf
+ (uint8_t)ESCOFFSET
);
219 bNextIsEscaped
= false;
221 else if (buf
== MSGESC
)
222 bNextIsEscaped
= true;
228 msg
.state
= ADAPTER_MESSAGE_STATE_INCOMING
;
230 return bGotFullMessage
;
233 std::string
CAdapterCommunication::GetError(void) const
235 return m_port
->GetError();
238 bool CAdapterCommunication::StartBootloader(void)
244 CLibCEC::AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
245 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
247 output
->PushBack(MSGSTART
);
248 output
->PushEscaped(MSGCODE_START_BOOTLOADER
);
249 output
->PushBack(MSGEND
);
250 output
->isTransmission
= false;
251 output
->expectControllerAck
= false;
253 if ((bReturn
= Write(output
)) == false)
254 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not start the bootloader");
260 bool CAdapterCommunication::PingAdapter(void)
266 CLibCEC::AddLog(CEC_LOG_DEBUG
, "sending ping");
267 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
269 output
->PushBack(MSGSTART
);
270 output
->PushEscaped(MSGCODE_PING
);
271 output
->PushBack(MSGEND
);
272 output
->isTransmission
= false;
274 if ((bReturn
= Write(output
)) == false)
275 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not ping the adapter");
281 uint16_t CAdapterCommunication::GetFirmwareVersion(void)
283 uint16_t iReturn(m_iFirmwareVersion
);
287 if (iReturn
== CEC_FW_VERSION_UNKNOWN
)
289 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting the firmware version");
290 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
292 output
->PushBack(MSGSTART
);
293 output
->PushEscaped(MSGCODE_FIRMWARE_VERSION
);
294 output
->PushBack(MSGEND
);
295 output
->isTransmission
= false;
296 output
->expectControllerAck
= false;
298 SendMessageToAdapter(output
);
301 CCECAdapterMessage input
;
302 if (!Read(input
, CEC_DEFAULT_TRANSMIT_WAIT
) || input
.Message() != MSGCODE_FIRMWARE_VERSION
|| input
.Size() != 3)
303 CLibCEC::AddLog(CEC_LOG_ERROR
, "no or invalid firmware version");
306 m_iFirmwareVersion
= (input
[1] << 8 | input
[2]);
307 iReturn
= m_iFirmwareVersion
;
314 bool CAdapterCommunication::SetLineTimeout(uint8_t iTimeout
)
316 bool bReturn(m_iLineTimeout
!= iTimeout
);
320 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
322 output
->PushBack(MSGSTART
);
323 output
->PushEscaped(MSGCODE_TRANSMIT_IDLETIME
);
324 output
->PushEscaped(iTimeout
);
325 output
->PushBack(MSGEND
);
326 output
->isTransmission
= false;
328 if ((bReturn
= Write(output
)) == false)
329 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not set the idletime");
336 bool CAdapterCommunication::SetAckMask(uint16_t iMask
)
340 strLog
.Format("setting ackmask to %2x", iMask
);
341 CLibCEC::AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
343 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
345 output
->PushBack(MSGSTART
);
346 output
->PushEscaped(MSGCODE_SET_ACK_MASK
);
347 output
->PushEscaped(iMask
>> 8);
348 output
->PushEscaped((uint8_t)iMask
);
349 output
->PushBack(MSGEND
);
350 output
->isTransmission
= false;
352 if ((bReturn
= Write(output
)) == false)
353 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not set the ackmask");
359 bool CAdapterCommunication::IsOpen(void)
361 return !IsStopped() && m_port
->IsOpen() && IsRunning();
364 bool CAdapterCommunication::WaitForAck(CCECAdapterMessage
&message
)
367 bool bTransmitSucceeded(false);
368 uint8_t iPacketsLeft(message
.Size() / 4);
370 int64_t iNow
= GetTimeMs();
371 int64_t iTargetTime
= iNow
+ message
.transmit_timeout
;
373 while (!bTransmitSucceeded
&& !bError
&& (message
.transmit_timeout
== 0 || iNow
< iTargetTime
))
375 CCECAdapterMessage msg
;
376 int32_t iWait
= (int32_t)(iTargetTime
- iNow
);
377 if (iWait
<= 5 || message
.transmit_timeout
<= 5)
378 iWait
= CEC_DEFAULT_TRANSMIT_WAIT
;
380 if (!Read(msg
, iWait
))
386 if (msg
.Message() == MSGCODE_FRAME_START
&& msg
.IsACK())
388 m_processor
->HandlePoll(msg
.Initiator(), msg
.Destination());
393 if (msg
.Message() == MSGCODE_RECEIVE_FAILED
&&
394 m_processor
->HandleReceiveFailed())
400 bError
= msg
.IsError();
403 message
.reply
= msg
.Message();
404 CLibCEC::AddLog(CEC_LOG_DEBUG
, msg
.ToString());
408 switch(msg
.Message())
410 case MSGCODE_COMMAND_ACCEPTED
:
411 CLibCEC::AddLog(CEC_LOG_DEBUG
, msg
.ToString());
412 if (iPacketsLeft
> 0)
414 if (!message
.isTransmission
&& iPacketsLeft
== 0)
415 bTransmitSucceeded
= true;
417 case MSGCODE_TRANSMIT_SUCCEEDED
:
418 CLibCEC::AddLog(CEC_LOG_DEBUG
, msg
.ToString());
419 bTransmitSucceeded
= (iPacketsLeft
== 0);
420 bError
= !bTransmitSucceeded
;
421 message
.reply
= MSGCODE_TRANSMIT_SUCCEEDED
;
424 // ignore other data while waiting
432 return bTransmitSucceeded
&& !bError
;
435 void CAdapterCommunication::AddData(uint8_t *data
, uint8_t iLen
)
437 CLockObject
lock(m_mutex
);
438 for (uint8_t iPtr
= 0; iPtr
< iLen
; iPtr
++)
439 m_inBuffer
.Push(data
[iPtr
]);
441 m_rcvCondition
.Signal();
444 bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout
)
451 CLockObject
lock(m_mutex
);
452 iBytesRead
= m_port
->Read(buff
, sizeof(buff
), iTimeout
);
453 if (iBytesRead
< 0 || iBytesRead
> 256)
456 strError
.Format("error reading from serial port: %s", m_port
->GetError().c_str());
457 CLibCEC::AddLog(CEC_LOG_ERROR
, strError
);
460 else if (iBytesRead
> 0)
461 AddData(buff
, (uint8_t) iBytesRead
);
463 return iBytesRead
> 0;
466 void CAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage
*msg
)
468 CLockObject
adapterLock(m_mutex
);
469 CLockObject
lock(msg
->mutex
);
470 if (m_port
->Write(msg
->packet
.data
, msg
->Size()) != (int32_t) msg
->Size())
473 strError
.Format("error writing to serial port: %s", m_port
->GetError().c_str());
474 CLibCEC::AddLog(CEC_LOG_ERROR
, strError
);
475 msg
->state
= ADAPTER_MESSAGE_STATE_ERROR
;
479 CLibCEC::AddLog(CEC_LOG_DEBUG
, "command sent");
480 msg
->state
= ADAPTER_MESSAGE_STATE_SENT
;
482 msg
->condition
.Signal();
485 void CAdapterCommunication::WriteNextCommand(void)
487 CCECAdapterMessage
*msg(NULL
);
488 if (m_outBuffer
.Pop(msg
))
489 SendMessageToAdapter(msg
);