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");
157 if (data
->expectControllerAck
)
159 bReturn
= WaitForTransmitSucceeded(data
);
162 if (data
->isTransmission
)
163 data
->state
= ADAPTER_MESSAGE_STATE_SENT_ACKED
;
167 data
->state
= ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED
;
168 CLibCEC::AddLog(CEC_LOG_DEBUG
, "did not receive ack");
179 bool CAdapterCommunication::Read(CCECAdapterMessage
&msg
, uint32_t iTimeout
)
181 CLockObject
lock(m_mutex
);
184 uint64_t iNow
= GetTimeMs();
185 uint64_t iTarget
= iNow
+ iTimeout
;
186 bool bGotFullMessage(false);
187 bool bNextIsEscaped(false);
188 bool bGotStart(false);
190 while(!bGotFullMessage
&& iNow
< iTarget
)
193 if (!m_inBuffer
.Pop(buf
))
195 if (!m_rcvCondition
.Wait(m_mutex
, (uint32_t) (iTarget
- iNow
)))
205 else if (buf
== MSGSTART
) //we found a msgstart before msgend, this is not right, remove
208 CLibCEC::AddLog(CEC_LOG_WARNING
, "received MSGSTART before MSGEND, removing previous buffer contents");
215 bGotFullMessage
= true;
217 else if (bNextIsEscaped
)
219 msg
.PushBack(buf
+ (uint8_t)ESCOFFSET
);
220 bNextIsEscaped
= false;
222 else if (buf
== MSGESC
)
223 bNextIsEscaped
= true;
229 msg
.state
= ADAPTER_MESSAGE_STATE_INCOMING
;
231 return bGotFullMessage
;
234 std::string
CAdapterCommunication::GetError(void) const
236 return m_port
->GetError();
239 bool CAdapterCommunication::StartBootloader(void)
245 CLibCEC::AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
246 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
248 output
->PushBack(MSGSTART
);
249 output
->PushEscaped(MSGCODE_START_BOOTLOADER
);
250 output
->PushBack(MSGEND
);
251 output
->isTransmission
= false;
252 output
->expectControllerAck
= false;
254 if ((bReturn
= Write(output
)) == false)
255 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not start the bootloader");
261 bool CAdapterCommunication::PingAdapter(void)
267 CLibCEC::AddLog(CEC_LOG_DEBUG
, "sending ping");
268 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
270 output
->PushBack(MSGSTART
);
271 output
->PushEscaped(MSGCODE_PING
);
272 output
->PushBack(MSGEND
);
273 output
->isTransmission
= false;
275 if ((bReturn
= Write(output
)) == false)
276 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not ping the adapter");
282 uint16_t CAdapterCommunication::GetFirmwareVersion(void)
284 uint16_t iReturn(m_iFirmwareVersion
);
288 if (iReturn
== CEC_FW_VERSION_UNKNOWN
)
290 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting the firmware version");
291 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
293 output
->PushBack(MSGSTART
);
294 output
->PushEscaped(MSGCODE_FIRMWARE_VERSION
);
295 output
->PushBack(MSGEND
);
296 output
->isTransmission
= false;
297 output
->expectControllerAck
= false;
299 SendMessageToAdapter(output
);
302 CCECAdapterMessage input
;
303 if (!Read(input
, CEC_DEFAULT_TRANSMIT_WAIT
) || input
.Message() != MSGCODE_FIRMWARE_VERSION
|| input
.Size() != 3)
304 CLibCEC::AddLog(CEC_LOG_ERROR
, "no or invalid firmware version");
307 m_iFirmwareVersion
= (input
[1] << 8 | input
[2]);
308 iReturn
= m_iFirmwareVersion
;
315 bool CAdapterCommunication::SetLineTimeout(uint8_t iTimeout
)
317 bool bReturn(m_iLineTimeout
!= iTimeout
);
321 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
323 output
->PushBack(MSGSTART
);
324 output
->PushEscaped(MSGCODE_TRANSMIT_IDLETIME
);
325 output
->PushEscaped(iTimeout
);
326 output
->PushBack(MSGEND
);
327 output
->isTransmission
= false;
329 if ((bReturn
= Write(output
)) == false)
330 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not set the idletime");
337 bool CAdapterCommunication::SetAckMask(uint16_t iMask
)
341 strLog
.Format("setting ackmask to %2x", iMask
);
342 CLibCEC::AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
344 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
346 output
->PushBack(MSGSTART
);
347 output
->PushEscaped(MSGCODE_SET_ACK_MASK
);
348 output
->PushEscaped(iMask
>> 8);
349 output
->PushEscaped((uint8_t)iMask
);
350 output
->PushBack(MSGEND
);
351 output
->isTransmission
= false;
353 if ((bReturn
= Write(output
)) == false)
354 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not set the ackmask");
360 bool CAdapterCommunication::IsOpen(void)
362 return !IsStopped() && m_port
->IsOpen() && IsRunning();
365 bool CAdapterCommunication::WaitForTransmitSucceeded(CCECAdapterMessage
*message
)
368 bool bTransmitSucceeded(false);
369 uint8_t iPacketsLeft(message
->Size() / 4);
371 int64_t iNow
= GetTimeMs();
372 int64_t iTargetTime
= iNow
+ message
->transmit_timeout
;
374 while (!bTransmitSucceeded
&& !bError
&& (message
->transmit_timeout
== 0 || iNow
< iTargetTime
))
376 CCECAdapterMessage msg
;
377 int32_t iWait
= (int32_t)(iTargetTime
- iNow
);
378 if (iWait
<= 5 || message
->transmit_timeout
<= 5)
379 iWait
= CEC_DEFAULT_TRANSMIT_WAIT
;
381 if (!Read(msg
, iWait
))
387 if (msg
.Message() == MSGCODE_FRAME_START
&& msg
.IsACK())
389 m_processor
->HandlePoll(msg
.Initiator(), msg
.Destination());
394 if (msg
.Message() == MSGCODE_RECEIVE_FAILED
&&
395 m_processor
->HandleReceiveFailed())
401 bError
= msg
.IsError();
404 message
->reply
= msg
.Message();
405 CLibCEC::AddLog(CEC_LOG_DEBUG
, msg
.ToString());
409 switch(msg
.Message())
411 case MSGCODE_COMMAND_ACCEPTED
:
412 CLibCEC::AddLog(CEC_LOG_DEBUG
, msg
.ToString());
413 if (iPacketsLeft
> 0)
415 if (!message
->isTransmission
&& iPacketsLeft
== 0)
416 bTransmitSucceeded
= true;
418 case MSGCODE_TRANSMIT_SUCCEEDED
:
419 CLibCEC::AddLog(CEC_LOG_DEBUG
, msg
.ToString());
420 bTransmitSucceeded
= (iPacketsLeft
== 0);
421 bError
= !bTransmitSucceeded
;
422 message
->reply
= MSGCODE_TRANSMIT_SUCCEEDED
;
425 // ignore other data while waiting
433 return bTransmitSucceeded
&& !bError
;
436 void CAdapterCommunication::AddData(uint8_t *data
, uint8_t iLen
)
438 CLockObject
lock(m_mutex
);
439 for (uint8_t iPtr
= 0; iPtr
< iLen
; iPtr
++)
440 m_inBuffer
.Push(data
[iPtr
]);
442 m_rcvCondition
.Signal();
445 bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout
)
452 CLockObject
lock(m_mutex
);
453 iBytesRead
= m_port
->Read(buff
, sizeof(buff
), iTimeout
);
454 if (iBytesRead
< 0 || iBytesRead
> 256)
457 strError
.Format("error reading from serial port: %s", m_port
->GetError().c_str());
458 CLibCEC::AddLog(CEC_LOG_ERROR
, strError
);
461 else if (iBytesRead
> 0)
462 AddData(buff
, (uint8_t) iBytesRead
);
464 return iBytesRead
> 0;
467 void CAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage
*msg
)
469 CLockObject
adapterLock(m_mutex
);
470 CLockObject
lock(msg
->mutex
);
471 if (m_port
->Write(msg
->packet
.data
, msg
->Size()) != (int32_t) msg
->Size())
474 strError
.Format("error writing to serial port: %s", m_port
->GetError().c_str());
475 CLibCEC::AddLog(CEC_LOG_ERROR
, strError
);
476 msg
->state
= ADAPTER_MESSAGE_STATE_ERROR
;
480 CLibCEC::AddLog(CEC_LOG_DEBUG
, "command sent");
481 msg
->state
= ADAPTER_MESSAGE_STATE_SENT
;
483 msg
->condition
.Signal();
486 void CAdapterCommunication::WriteNextCommand(void)
488 CCECAdapterMessage
*msg(NULL
);
489 if (m_outBuffer
.Pop(msg
))
490 SendMessageToAdapter(msg
);