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
),
47 m_iFirmwareVersion(CEC_FW_VERSION_UNKNOWN
)
49 m_port
= new PLATFORM::CSerialPort
;
52 CAdapterCommunication::~CAdapterCommunication(void)
63 bool CAdapterCommunication::Open(const char *strPort
, uint16_t iBaudRate
/* = 38400 */, uint32_t iTimeoutMs
/* = 10000 */)
65 uint64_t iNow
= GetTimeMs();
66 uint64_t iTimeout
= iNow
+ iTimeoutMs
;
68 CLockObject
lock(m_mutex
);
72 m_processor
->AddLog(CEC_LOG_ERROR
, "port is NULL");
78 m_processor
->AddLog(CEC_LOG_ERROR
, "port is already open");
83 bool bConnected(false);
84 while (!bConnected
&& iNow
< iTimeout
)
86 if ((bConnected
= m_port
->Open(strPort
, iBaudRate
)) == false)
88 strError
.Format("error opening serial port '%s': %s", strPort
, m_port
->GetError().c_str());
96 m_processor
->AddLog(CEC_LOG_ERROR
, strError
);
100 m_processor
->AddLog(CEC_LOG_DEBUG
, "connection opened");
102 //clear any input bytes
104 while (m_port
->Read(buff
, 1, 5) == 1) {}
108 m_processor
->AddLog(CEC_LOG_DEBUG
, "communication thread started");
113 m_processor
->AddLog(CEC_LOG_ERROR
, "could not create a communication thread");
119 void CAdapterCommunication::Close(void)
121 CLockObject
lock(m_mutex
);
122 m_rcvCondition
.Broadcast();
126 void *CAdapterCommunication::Process(void)
135 CCECAdapterMessage
*msg(NULL
);
136 if (m_outBuffer
.Pop(msg
))
137 msg
->condition
.Broadcast();
142 bool CAdapterCommunication::Write(CCECAdapterMessage
*data
)
146 CLockObject
lock(data
->mutex
);
147 data
->state
= ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT
;
148 m_outBuffer
.Push(data
);
149 data
->condition
.Wait(data
->mutex
);
151 if (data
->state
!= ADAPTER_MESSAGE_STATE_SENT
)
153 m_processor
->AddLog(CEC_LOG_ERROR
, "command was not sent");
155 if (data
->expectControllerAck
&& WaitForTransmitSucceeded(data
))
157 if (data
->isTransmission
)
158 data
->state
= ADAPTER_MESSAGE_STATE_SENT_ACKED
;
163 data
->state
= ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED
;
164 m_processor
->AddLog(CEC_LOG_DEBUG
, "did not receive ack");
170 bool CAdapterCommunication::Read(CCECAdapterMessage
&msg
, uint32_t iTimeout
)
172 CLockObject
lock(m_mutex
);
175 uint64_t iNow
= GetTimeMs();
176 uint64_t iTarget
= iNow
+ iTimeout
;
177 bool bGotFullMessage(false);
178 bool bNextIsEscaped(false);
179 bool bGotStart(false);
181 while(!bGotFullMessage
&& iNow
< iTarget
)
184 if (!m_inBuffer
.Pop(buf
))
186 if (!m_rcvCondition
.Wait(m_mutex
, (uint32_t) (iTarget
- iNow
)))
196 else if (buf
== MSGSTART
) //we found a msgstart before msgend, this is not right, remove
199 m_processor
->AddLog(CEC_LOG_WARNING
, "received MSGSTART before MSGEND, removing previous buffer contents");
206 bGotFullMessage
= true;
208 else if (bNextIsEscaped
)
210 msg
.PushBack(buf
+ (uint8_t)ESCOFFSET
);
211 bNextIsEscaped
= false;
213 else if (buf
== MSGESC
)
214 bNextIsEscaped
= true;
220 msg
.state
= ADAPTER_MESSAGE_STATE_INCOMING
;
222 return bGotFullMessage
;
225 std::string
CAdapterCommunication::GetError(void) const
227 return m_port
->GetError();
230 bool CAdapterCommunication::StartBootloader(void)
236 m_processor
->AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
237 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
239 output
->PushBack(MSGSTART
);
240 output
->PushEscaped(MSGCODE_START_BOOTLOADER
);
241 output
->PushBack(MSGEND
);
242 output
->isTransmission
= false;
243 output
->expectControllerAck
= false;
245 if ((bReturn
= Write(output
)) == false)
246 m_processor
->AddLog(CEC_LOG_ERROR
, "could not start the bootloader");
252 bool CAdapterCommunication::PingAdapter(void)
258 m_processor
->AddLog(CEC_LOG_DEBUG
, "sending ping");
259 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
261 output
->PushBack(MSGSTART
);
262 output
->PushEscaped(MSGCODE_PING
);
263 output
->PushBack(MSGEND
);
264 output
->isTransmission
= false;
266 if ((bReturn
= Write(output
)) == false)
267 m_processor
->AddLog(CEC_LOG_ERROR
, "could not ping the adapter");
273 uint16_t CAdapterCommunication::GetFirmwareVersion(void)
275 uint16_t iReturn(m_iFirmwareVersion
);
279 if (iReturn
== CEC_FW_VERSION_UNKNOWN
)
281 m_processor
->AddLog(CEC_LOG_DEBUG
, "requesting the firmware version");
282 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
284 output
->PushBack(MSGSTART
);
285 output
->PushEscaped(MSGCODE_FIRMWARE_VERSION
);
286 output
->PushBack(MSGEND
);
287 output
->isTransmission
= false;
288 output
->expectControllerAck
= false;
290 SendMessageToAdapter(output
);
293 CCECAdapterMessage input
;
294 if (!Read(input
, CEC_DEFAULT_TRANSMIT_WAIT
) || input
.Message() != MSGCODE_FIRMWARE_VERSION
|| input
.Size() != 3)
295 m_processor
->AddLog(CEC_LOG_ERROR
, "no or invalid firmware version");
298 m_iFirmwareVersion
= (input
[1] << 8 | input
[2]);
299 iReturn
= m_iFirmwareVersion
;
306 bool CAdapterCommunication::SetLineTimeout(uint8_t iTimeout
)
308 bool bReturn(m_iLineTimeout
!= iTimeout
);
312 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
314 output
->PushBack(MSGSTART
);
315 output
->PushEscaped(MSGCODE_TRANSMIT_IDLETIME
);
316 output
->PushEscaped(iTimeout
);
317 output
->PushBack(MSGEND
);
318 output
->isTransmission
= false;
320 if ((bReturn
= Write(output
)) == false)
321 m_processor
->AddLog(CEC_LOG_ERROR
, "could not set the idletime");
328 bool CAdapterCommunication::SetAckMask(uint16_t iMask
)
332 strLog
.Format("setting ackmask to %2x", iMask
);
333 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
335 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
337 output
->PushBack(MSGSTART
);
338 output
->PushEscaped(MSGCODE_SET_ACK_MASK
);
339 output
->PushEscaped(iMask
>> 8);
340 output
->PushEscaped((uint8_t)iMask
);
341 output
->PushBack(MSGEND
);
342 output
->isTransmission
= false;
344 if ((bReturn
= Write(output
)) == false)
345 m_processor
->AddLog(CEC_LOG_ERROR
, "could not set the ackmask");
351 bool CAdapterCommunication::IsOpen(void)
353 return !IsStopped() && m_port
->IsOpen() && IsRunning();
356 bool CAdapterCommunication::WaitForTransmitSucceeded(CCECAdapterMessage
*message
)
359 bool bTransmitSucceeded(false);
360 uint8_t iPacketsLeft(message
->Size() / 4);
362 int64_t iNow
= GetTimeMs();
363 int64_t iTargetTime
= iNow
+ message
->transmit_timeout
;
365 while (!bTransmitSucceeded
&& !bError
&& (message
->transmit_timeout
== 0 || iNow
< iTargetTime
))
367 CCECAdapterMessage msg
;
369 if (!Read(msg
, message
->transmit_timeout
> 0 ? (int32_t)(iTargetTime
- iNow
) : 1000))
375 if (msg
.Message() == MSGCODE_FRAME_START
&& msg
.IsACK())
377 m_processor
->HandlePoll(msg
.Initiator(), msg
.Destination());
382 if (msg
.Message() == MSGCODE_RECEIVE_FAILED
&&
383 m_processor
->HandleReceiveFailed())
389 bError
= msg
.IsError();
392 message
->reply
= msg
.Message();
393 m_processor
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
397 switch(msg
.Message())
399 case MSGCODE_COMMAND_ACCEPTED
:
400 m_processor
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
401 if (iPacketsLeft
> 0)
403 if (!message
->isTransmission
&& iPacketsLeft
== 0)
404 bTransmitSucceeded
= true;
406 case MSGCODE_TRANSMIT_SUCCEEDED
:
407 m_processor
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
408 bTransmitSucceeded
= (iPacketsLeft
== 0);
409 bError
= !bTransmitSucceeded
;
410 message
->reply
= MSGCODE_TRANSMIT_SUCCEEDED
;
413 // ignore other data while waiting
421 return bTransmitSucceeded
&& !bError
;
424 void CAdapterCommunication::AddData(uint8_t *data
, uint8_t iLen
)
426 CLockObject
lock(m_mutex
);
427 for (uint8_t iPtr
= 0; iPtr
< iLen
; iPtr
++)
428 m_inBuffer
.Push(data
[iPtr
]);
430 m_rcvCondition
.Signal();
433 bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout
)
440 CLockObject
lock(m_mutex
);
441 iBytesRead
= m_port
->Read(buff
, sizeof(buff
), iTimeout
);
442 if (iBytesRead
< 0 || iBytesRead
> 256)
445 strError
.Format("error reading from serial port: %s", m_port
->GetError().c_str());
446 m_processor
->AddLog(CEC_LOG_ERROR
, strError
);
449 else if (iBytesRead
> 0)
450 AddData(buff
, (uint8_t) iBytesRead
);
452 return iBytesRead
> 0;
455 void CAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage
*msg
)
457 CLockObject
adapterLock(m_mutex
);
458 CLockObject
lock(msg
->mutex
);
459 if (m_port
->Write(msg
->packet
.data
, msg
->Size()) != (int32_t) msg
->Size())
462 strError
.Format("error writing to serial port: %s", m_port
->GetError().c_str());
463 m_processor
->AddLog(CEC_LOG_ERROR
, strError
);
464 msg
->state
= ADAPTER_MESSAGE_STATE_ERROR
;
468 m_processor
->AddLog(CEC_LOG_DEBUG
, "command sent");
469 msg
->state
= ADAPTER_MESSAGE_STATE_SENT
;
471 msg
->condition
.Signal();
474 void CAdapterCommunication::WriteNextCommand(void)
476 CCECAdapterMessage
*msg(NULL
);
477 if (m_outBuffer
.Pop(msg
))
478 SendMessageToAdapter(msg
);