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"
34 #include "CECParser.h"
35 #include "libPlatform/serialport.h"
36 #include "util/StdString.h"
41 CAdapterCommunication::CAdapterCommunication(CCECParser
*parser
) :
49 m_port
= new CSerialPort
;
52 CAdapterCommunication::~CAdapterCommunication(void)
58 bool CAdapterCommunication::Open(const char *strPort
, int iBaudRate
/* = 38400 */, int iTimeoutMs
/* = 10000 */)
60 CLockObject
lock(&m_commMutex
);
64 if (!m_port
->Open(strPort
, iBaudRate
))
67 strError
.Format("error opening serial port '%s': %s", strPort
, m_port
->GetError().c_str());
68 m_parser
->AddLog(CEC_LOG_ERROR
, strError
);
72 m_parser
->AddLog(CEC_LOG_DEBUG
, "connection opened");
74 //clear any input bytes
76 m_port
->Read(buff
, sizeof(buff
), 50);
78 CCondition::Sleep(CEC_SETTLE_DOWN_TIME
);
85 m_parser
->AddLog(CEC_LOG_DEBUG
, "reader thread created");
90 m_parser
->AddLog(CEC_LOG_DEBUG
, "could not create a reader thread");
96 void CAdapterCommunication::Close(void)
102 void *CAdapterCommunication::Process(void)
106 if (!ReadFromDevice(250))
112 CCondition::Sleep(50);
115 m_parser
->AddLog(CEC_LOG_DEBUG
, "reader thread terminated");
117 CLockObject
lock(&m_commMutex
);
122 bool CAdapterCommunication::ReadFromDevice(int iTimeout
)
125 CLockObject
lock(&m_commMutex
);
126 int iBytesRead
= m_port
->Read(buff
, sizeof(buff
), iTimeout
);
131 strError
.Format("error reading from serial port: %s", m_port
->GetError().c_str());
132 m_parser
->AddLog(CEC_LOG_ERROR
, strError
);
135 else if (iBytesRead
> 0)
136 AddData(buff
, iBytesRead
);
141 void CAdapterCommunication::AddData(uint8_t *data
, int iLen
)
143 CLockObject
lock(&m_bufferMutex
);
144 if (iLen
+ m_iInbufUsed
> m_iInbufSize
)
146 m_iInbufSize
= iLen
+ m_iInbufUsed
;
147 m_inbuf
= (uint8_t*)realloc(m_inbuf
, m_iInbufSize
);
150 memcpy(m_inbuf
+ m_iInbufUsed
, data
, iLen
);
151 m_iInbufUsed
+= iLen
;
153 m_condition
.Signal();
156 bool CAdapterCommunication::Write(const cec_frame
&data
)
158 CLockObject
lock(&m_commMutex
);
160 if (m_port
->Write(data
) != data
.size())
163 strError
.Format("error writing to serial port: %s", m_port
->GetError().c_str());
164 m_parser
->AddLog(CEC_LOG_ERROR
, strError
);
168 m_parser
->AddLog(CEC_LOG_DEBUG
, "command sent");
172 bool CAdapterCommunication::Read(cec_frame
&msg
, int iTimeout
)
174 CLockObject
lock(&m_bufferMutex
);
176 if (m_iInbufUsed
< 1)
177 m_condition
.Wait(&m_bufferMutex
, iTimeout
);
179 if (m_iInbufUsed
< 1)
182 //search for first start of message
184 for (int i
= 0; i
< m_iInbufUsed
; i
++)
186 if (m_inbuf
[i
] == MSGSTART
)
196 //move anything from the first start of message to the beginning of the buffer
199 memmove(m_inbuf
, m_inbuf
+ startpos
, m_iInbufUsed
- startpos
);
200 m_iInbufUsed
-= startpos
;
203 if (m_iInbufUsed
< 2)
206 //look for end of message
209 for (int i
= 1; i
< m_iInbufUsed
; i
++)
211 if (m_inbuf
[i
] == MSGEND
)
216 else if (m_inbuf
[i
] == MSGSTART
)
223 if (startpos
> 0) //we found a msgstart before msgend, this is not right, remove
225 m_parser
->AddLog(CEC_LOG_ERROR
, "received MSGSTART before MSGEND");
226 memmove(m_inbuf
, m_inbuf
+ startpos
, m_iInbufUsed
- startpos
);
227 m_iInbufUsed
-= startpos
;
231 if (endpos
> 0) //found a MSGEND
235 for (int i
= 1; i
< endpos
; i
++)
239 msg
.push_back(m_inbuf
[i
] + (uint8_t)ESCOFFSET
);
242 else if (m_inbuf
[i
] == MSGESC
)
248 msg
.push_back(m_inbuf
[i
]);
252 if (endpos
+ 1 < m_iInbufUsed
)
253 memmove(m_inbuf
, m_inbuf
+ endpos
+ 1, m_iInbufUsed
- endpos
- 1);
255 m_iInbufUsed
-= endpos
+ 1;
263 std::string
CAdapterCommunication::GetError(void) const
265 return m_port
->GetError();