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 "Communication.h"
34 #include "CECParser.h"
35 #include "libPlatform/serialport.h"
36 #include "util/StdString.h"
41 CCommunication::CCommunication(CCECParser
*parser
) :
49 m_port
= new CSerialPort
;
52 CCommunication::~CCommunication(void)
58 bool CCommunication::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
);
82 if (pthread_create(&m_thread
, NULL
, (void *(*) (void *))&CCommunication::ReaderThreadHandler
, (void *)this) == 0)
84 m_parser
->AddLog(CEC_LOG_DEBUG
, "reader thread created");
85 pthread_detach(m_thread
);
90 m_parser
->AddLog(CEC_LOG_DEBUG
, "could not create a reader thread");
96 void *CCommunication::ReaderThreadHandler(CCommunication
*comm
)
99 comm
->ReaderProcess();
104 void CCommunication::Close(void)
107 pthread_join(m_thread
, NULL
);
111 void *CCommunication::ReaderProcess(void)
115 if (!ReadFromDevice(250))
121 CCondition::Sleep(50);
124 m_parser
->AddLog(CEC_LOG_DEBUG
, "reader thread terminated");
126 CLockObject
lock(&m_commMutex
);
131 bool CCommunication::ReadFromDevice(int iTimeout
)
134 CLockObject
lock(&m_commMutex
);
135 int iBytesRead
= m_port
->Read(buff
, sizeof(buff
), iTimeout
);
140 strError
.Format("error reading from serial port: %s", m_port
->GetError().c_str());
141 m_parser
->AddLog(CEC_LOG_ERROR
, strError
);
144 else if (iBytesRead
> 0)
145 AddData(buff
, iBytesRead
);
150 void CCommunication::AddData(uint8_t *data
, int iLen
)
152 CLockObject
lock(&m_bufferMutex
);
153 if (iLen
+ m_iInbufUsed
> m_iInbufSize
)
155 m_iInbufSize
= iLen
+ m_iInbufUsed
;
156 m_inbuf
= (uint8_t*)realloc(m_inbuf
, m_iInbufSize
);
159 memcpy(m_inbuf
+ m_iInbufUsed
, data
, iLen
);
160 m_iInbufUsed
+= iLen
;
162 m_condition
.Signal();
165 bool CCommunication::Write(const cec_frame
&data
)
167 CLockObject
lock(&m_commMutex
);
169 if (m_port
->Write(data
) != data
.size())
172 strError
.Format("error writing to serial port: %s", m_port
->GetError().c_str());
173 m_parser
->AddLog(CEC_LOG_ERROR
, strError
);
177 m_parser
->AddLog(CEC_LOG_DEBUG
, "command sent");
181 bool CCommunication::Read(cec_frame
&msg
, int iTimeout
)
183 CLockObject
lock(&m_bufferMutex
);
185 while (m_iInbufUsed
< 1)
186 m_condition
.Wait(&m_bufferMutex
, iTimeout
);
188 if (m_iInbufUsed
< 1)
191 //search for first start of message
193 for (int i
= 0; i
< m_iInbufUsed
; i
++)
195 if (m_inbuf
[i
] == MSGSTART
)
205 //move anything from the first start of message to the beginning of the buffer
208 memmove(m_inbuf
, m_inbuf
+ startpos
, m_iInbufUsed
- startpos
);
209 m_iInbufUsed
-= startpos
;
212 if (m_iInbufUsed
< 2)
215 //look for end of message
218 for (int i
= 1; i
< m_iInbufUsed
; i
++)
220 if (m_inbuf
[i
] == MSGEND
)
225 else if (m_inbuf
[i
] == MSGSTART
)
232 if (startpos
> 0) //we found a msgstart before msgend, this is not right, remove
234 m_parser
->AddLog(CEC_LOG_ERROR
, "received MSGSTART before MSGEND");
235 memmove(m_inbuf
, m_inbuf
+ startpos
, m_iInbufUsed
- startpos
);
236 m_iInbufUsed
-= startpos
;
240 if (endpos
> 0) //found a MSGEND
244 for (int i
= 1; i
< endpos
; i
++)
248 msg
.push_back(m_inbuf
[i
] + (uint8_t)ESCOFFSET
);
251 else if (m_inbuf
[i
] == MSGESC
)
257 msg
.push_back(m_inbuf
[i
]);
261 if (endpos
+ 1 < m_iInbufUsed
)
262 memmove(m_inbuf
, m_inbuf
+ endpos
+ 1, m_iInbufUsed
- endpos
- 1);
264 m_iInbufUsed
-= endpos
+ 1;
272 std::string
CCommunication::GetError(void) const
274 return m_port
->GetError();