Commit | Line | Data |
---|---|---|
a8f0bd18 LOK |
1 | /* |
2 | * This file is part of the libCEC(R) library. | |
3 | * | |
4 | * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved. | |
5 | * libCEC(R) is an original work, containing original code. | |
6 | * | |
7 | * libCEC(R) is a trademark of Pulse-Eight Limited. | |
8 | * | |
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. | |
13 | * | |
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. | |
18 | * | |
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. | |
22 | * | |
23 | * | |
24 | * Alternatively, you can license this library under a commercial license, | |
25 | * please contact Pulse-Eight Licensing for more information. | |
26 | * | |
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/ | |
31 | */ | |
32 | ||
828682d3 | 33 | #include "AdapterCommunication.h" |
2abe74eb LOK |
34 | |
35 | #include "LibCEC.h" | |
b9187cc6 | 36 | #include "platform/serialport.h" |
a8f0bd18 LOK |
37 | #include "util/StdString.h" |
38 | ||
39 | using namespace std; | |
40 | using namespace CEC; | |
41 | ||
2abe74eb | 42 | CAdapterCommunication::CAdapterCommunication(CLibCEC *controller) : |
12027dbe | 43 | m_port(NULL), |
2abe74eb | 44 | m_controller(controller), |
a8f0bd18 LOK |
45 | m_inbuf(NULL), |
46 | m_iInbufSize(0), | |
13fd6a66 | 47 | m_iInbufUsed(0) |
a8f0bd18 LOK |
48 | { |
49 | m_port = new CSerialPort; | |
50 | } | |
51 | ||
828682d3 | 52 | CAdapterCommunication::~CAdapterCommunication(void) |
a8f0bd18 | 53 | { |
12027dbe LOK |
54 | Close(); |
55 | ||
56 | if (m_port) | |
57 | { | |
58 | delete m_port; | |
59 | m_port = NULL; | |
60 | } | |
25701fa6 LOK |
61 | |
62 | if (m_inbuf) | |
63 | free(m_inbuf); | |
a8f0bd18 LOK |
64 | } |
65 | ||
25701fa6 | 66 | bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */) |
a8f0bd18 | 67 | { |
13fd6a66 LOK |
68 | CLockObject lock(&m_mutex); |
69 | if (!m_port) | |
70 | { | |
71 | m_controller->AddLog(CEC_LOG_ERROR, "port is NULL"); | |
a8f0bd18 | 72 | return false; |
13fd6a66 LOK |
73 | } |
74 | ||
75 | if (IsOpen()) | |
76 | { | |
77 | m_controller->AddLog(CEC_LOG_ERROR, "port is already open"); | |
78 | } | |
a8f0bd18 LOK |
79 | |
80 | if (!m_port->Open(strPort, iBaudRate)) | |
81 | { | |
82 | CStdString strError; | |
83 | strError.Format("error opening serial port '%s': %s", strPort, m_port->GetError().c_str()); | |
2abe74eb | 84 | m_controller->AddLog(CEC_LOG_ERROR, strError); |
a8f0bd18 LOK |
85 | return false; |
86 | } | |
87 | ||
2abe74eb | 88 | m_controller->AddLog(CEC_LOG_DEBUG, "connection opened"); |
a8f0bd18 LOK |
89 | |
90 | //clear any input bytes | |
91 | uint8_t buff[1024]; | |
92 | m_port->Read(buff, sizeof(buff), 50); | |
93 | ||
12027dbe | 94 | Sleep(CEC_SETTLE_DOWN_TIME); |
828682d3 LOK |
95 | |
96 | if (CreateThread()) | |
a8f0bd18 | 97 | { |
13fd6a66 | 98 | m_controller->AddLog(CEC_LOG_DEBUG, "communication thread created"); |
a8f0bd18 LOK |
99 | return true; |
100 | } | |
101 | else | |
102 | { | |
13fd6a66 | 103 | m_controller->AddLog(CEC_LOG_DEBUG, "could not create a communication thread"); |
a8f0bd18 LOK |
104 | } |
105 | ||
106 | return false; | |
107 | } | |
108 | ||
828682d3 | 109 | void CAdapterCommunication::Close(void) |
a8f0bd18 | 110 | { |
25701fa6 | 111 | m_rcvCondition.Broadcast(); |
12027dbe | 112 | |
13fd6a66 | 113 | CLockObject lock(&m_mutex); |
828682d3 | 114 | StopThread(); |
25701fa6 | 115 | |
13fd6a66 LOK |
116 | if (m_inbuf) |
117 | { | |
118 | free(m_inbuf); | |
119 | m_inbuf = NULL; | |
120 | m_iInbufSize = 0; | |
121 | m_iInbufUsed = 0; | |
122 | } | |
a8f0bd18 LOK |
123 | } |
124 | ||
828682d3 | 125 | void *CAdapterCommunication::Process(void) |
a8f0bd18 | 126 | { |
12027dbe LOK |
127 | m_controller->AddLog(CEC_LOG_DEBUG, "communication thread started"); |
128 | ||
13fd6a66 | 129 | while (!IsStopped()) |
a8f0bd18 | 130 | { |
13fd6a66 | 131 | bool bSignal(false); |
a8f0bd18 | 132 | { |
13fd6a66 LOK |
133 | CLockObject lock(&m_mutex, true); |
134 | if (lock.IsLocked()) | |
76321de4 | 135 | bSignal = ReadFromDevice(50); |
a8f0bd18 LOK |
136 | } |
137 | ||
13fd6a66 LOK |
138 | if (bSignal) |
139 | m_rcvCondition.Signal(); | |
140 | ||
141 | if (!IsStopped()) | |
12027dbe | 142 | Sleep(50); |
a8f0bd18 LOK |
143 | } |
144 | ||
a8f0bd18 LOK |
145 | return NULL; |
146 | } | |
147 | ||
25701fa6 | 148 | bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout) |
a8f0bd18 | 149 | { |
25701fa6 | 150 | int32_t iBytesRead; |
13fd6a66 LOK |
151 | uint8_t buff[1024]; |
152 | if (!m_port) | |
153 | return false; | |
b6c82769 | 154 | |
13fd6a66 LOK |
155 | iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout); |
156 | if (iBytesRead < 0 || iBytesRead > 256) | |
a8f0bd18 | 157 | { |
13fd6a66 LOK |
158 | CStdString strError; |
159 | strError.Format("error reading from serial port: %s", m_port->GetError().c_str()); | |
160 | m_controller->AddLog(CEC_LOG_ERROR, strError); | |
161 | StopThread(false); | |
162 | return false; | |
a8f0bd18 | 163 | } |
13fd6a66 LOK |
164 | else if (iBytesRead > 0) |
165 | AddData(buff, (uint8_t) iBytesRead); | |
25701fa6 | 166 | |
13fd6a66 | 167 | return iBytesRead > 0; |
a8f0bd18 LOK |
168 | } |
169 | ||
8bca69de | 170 | void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen) |
a8f0bd18 | 171 | { |
25701fa6 | 172 | if (m_iInbufUsed + iLen > m_iInbufSize) |
a8f0bd18 | 173 | { |
25701fa6 | 174 | m_iInbufSize = m_iInbufUsed + iLen; |
a8f0bd18 LOK |
175 | m_inbuf = (uint8_t*)realloc(m_inbuf, m_iInbufSize); |
176 | } | |
177 | ||
178 | memcpy(m_inbuf + m_iInbufUsed, data, iLen); | |
179 | m_iInbufUsed += iLen; | |
a8f0bd18 LOK |
180 | } |
181 | ||
828682d3 | 182 | bool CAdapterCommunication::Write(const cec_frame &data) |
a8f0bd18 | 183 | { |
a8f0bd18 | 184 | { |
25701fa6 LOK |
185 | CLockObject lock(&m_mutex); |
186 | if (m_port->Write(data) != (int32_t) data.size) | |
187 | { | |
188 | CStdString strError; | |
189 | strError.Format("error writing to serial port: %s", m_port->GetError().c_str()); | |
190 | m_controller->AddLog(CEC_LOG_ERROR, strError); | |
191 | return false; | |
192 | } | |
a8f0bd18 | 193 | |
25701fa6 | 194 | m_controller->AddLog(CEC_LOG_DEBUG, "command sent"); |
2abe74eb | 195 | |
40339852 | 196 | CCondition::Sleep((uint32_t) data.size * (uint32_t)24 /*data*/ + (uint32_t)5 /*start bit (4.5 ms)*/); |
25701fa6 | 197 | } |
2abe74eb | 198 | |
a8f0bd18 LOK |
199 | return true; |
200 | } | |
201 | ||
25701fa6 | 202 | bool CAdapterCommunication::Read(cec_frame &msg, uint32_t iTimeout) |
a8f0bd18 | 203 | { |
25701fa6 | 204 | CLockObject lock(&m_mutex); |
a8f0bd18 | 205 | |
60fa4578 | 206 | if (m_iInbufUsed < 1) |
13fd6a66 LOK |
207 | { |
208 | if (!m_rcvCondition.Wait(&m_mutex, iTimeout)) | |
209 | return false; | |
210 | } | |
a8f0bd18 | 211 | |
13fd6a66 | 212 | if (m_iInbufUsed < 1 || IsStopped()) |
a8f0bd18 LOK |
213 | return false; |
214 | ||
215 | //search for first start of message | |
25701fa6 LOK |
216 | int16_t startpos = -1; |
217 | for (int16_t iPtr = 0; iPtr < m_iInbufUsed; iPtr++) | |
a8f0bd18 | 218 | { |
25701fa6 | 219 | if (m_inbuf[iPtr] == MSGSTART) |
a8f0bd18 | 220 | { |
25701fa6 | 221 | startpos = iPtr; |
a8f0bd18 LOK |
222 | break; |
223 | } | |
224 | } | |
225 | ||
226 | if (startpos == -1) | |
227 | return false; | |
228 | ||
229 | //move anything from the first start of message to the beginning of the buffer | |
230 | if (startpos > 0) | |
231 | { | |
232 | memmove(m_inbuf, m_inbuf + startpos, m_iInbufUsed - startpos); | |
233 | m_iInbufUsed -= startpos; | |
234 | } | |
235 | ||
236 | if (m_iInbufUsed < 2) | |
237 | return false; | |
238 | ||
239 | //look for end of message | |
240 | startpos = -1; | |
25701fa6 LOK |
241 | int16_t endpos = -1; |
242 | for (int16_t iPtr = 1; iPtr < m_iInbufUsed; iPtr++) | |
a8f0bd18 | 243 | { |
25701fa6 | 244 | if (m_inbuf[iPtr] == MSGEND) |
a8f0bd18 | 245 | { |
25701fa6 | 246 | endpos = iPtr; |
a8f0bd18 LOK |
247 | break; |
248 | } | |
25701fa6 | 249 | else if (m_inbuf[iPtr] == MSGSTART) |
a8f0bd18 | 250 | { |
25701fa6 | 251 | startpos = iPtr; |
a8f0bd18 LOK |
252 | break; |
253 | } | |
254 | } | |
255 | ||
256 | if (startpos > 0) //we found a msgstart before msgend, this is not right, remove | |
257 | { | |
2abe74eb | 258 | m_controller->AddLog(CEC_LOG_ERROR, "received MSGSTART before MSGEND"); |
a8f0bd18 LOK |
259 | memmove(m_inbuf, m_inbuf + startpos, m_iInbufUsed - startpos); |
260 | m_iInbufUsed -= startpos; | |
261 | return false; | |
262 | } | |
263 | ||
264 | if (endpos > 0) //found a MSGEND | |
265 | { | |
266 | msg.clear(); | |
267 | bool isesc = false; | |
25701fa6 | 268 | for (int16_t iPtr = 1; iPtr < endpos; iPtr++) |
a8f0bd18 LOK |
269 | { |
270 | if (isesc) | |
271 | { | |
25701fa6 | 272 | msg.push_back(m_inbuf[iPtr] + (uint8_t)ESCOFFSET); |
a8f0bd18 LOK |
273 | isesc = false; |
274 | } | |
25701fa6 | 275 | else if (m_inbuf[iPtr] == MSGESC) |
a8f0bd18 LOK |
276 | { |
277 | isesc = true; | |
278 | } | |
279 | else | |
280 | { | |
25701fa6 | 281 | msg.push_back(m_inbuf[iPtr]); |
a8f0bd18 LOK |
282 | } |
283 | } | |
284 | ||
285 | if (endpos + 1 < m_iInbufUsed) | |
286 | memmove(m_inbuf, m_inbuf + endpos + 1, m_iInbufUsed - endpos - 1); | |
287 | ||
288 | m_iInbufUsed -= endpos + 1; | |
289 | ||
290 | return true; | |
291 | } | |
292 | ||
293 | return false; | |
294 | } | |
295 | ||
828682d3 | 296 | std::string CAdapterCommunication::GetError(void) const |
a8f0bd18 LOK |
297 | { |
298 | return m_port->GetError(); | |
299 | } | |
2abe74eb LOK |
300 | |
301 | bool CAdapterCommunication::StartBootloader(void) | |
302 | { | |
303 | if (!IsRunning()) | |
304 | return false; | |
305 | ||
306 | m_controller->AddLog(CEC_LOG_DEBUG, "starting the bootloader"); | |
307 | cec_frame output; | |
25701fa6 LOK |
308 | output.clear(); |
309 | ||
2abe74eb LOK |
310 | output.push_back(MSGSTART); |
311 | PushEscaped(output, MSGCODE_START_BOOTLOADER); | |
312 | output.push_back(MSGEND); | |
313 | ||
314 | if (!Write(output)) | |
315 | { | |
316 | m_controller->AddLog(CEC_LOG_ERROR, "could not start the bootloader"); | |
317 | return false; | |
318 | } | |
319 | m_controller->AddLog(CEC_LOG_DEBUG, "bootloader start command transmitted"); | |
320 | return true; | |
321 | } | |
322 | ||
323 | void CAdapterCommunication::PushEscaped(cec_frame &vec, uint8_t byte) | |
324 | { | |
325 | if (byte >= MSGESC && byte != MSGSTART) | |
326 | { | |
327 | vec.push_back(MSGESC); | |
328 | vec.push_back(byte - ESCOFFSET); | |
329 | } | |
330 | else | |
331 | { | |
332 | vec.push_back(byte); | |
333 | } | |
334 | } | |
335 | ||
336 | bool CAdapterCommunication::SetAckMask(uint16_t iMask) | |
337 | { | |
338 | if (!IsRunning()) | |
339 | return false; | |
340 | ||
341 | CStdString strLog; | |
342 | strLog.Format("setting ackmask to %2x", iMask); | |
343 | m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str()); | |
344 | ||
345 | cec_frame output; | |
25701fa6 | 346 | output.clear(); |
2abe74eb LOK |
347 | |
348 | output.push_back(MSGSTART); | |
349 | PushEscaped(output, MSGCODE_SET_ACK_MASK); | |
350 | PushEscaped(output, iMask >> 8); | |
351 | PushEscaped(output, (uint8_t)iMask); | |
352 | output.push_back(MSGEND); | |
353 | ||
354 | if (!Write(output)) | |
355 | { | |
356 | m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask"); | |
357 | return false; | |
358 | } | |
359 | ||
360 | return true; | |
361 | } | |
362 | ||
363 | bool CAdapterCommunication::PingAdapter(void) | |
364 | { | |
365 | if (!IsRunning()) | |
366 | return false; | |
367 | ||
368 | m_controller->AddLog(CEC_LOG_DEBUG, "sending ping"); | |
369 | cec_frame output; | |
25701fa6 LOK |
370 | output.clear(); |
371 | ||
2abe74eb LOK |
372 | output.push_back(MSGSTART); |
373 | PushEscaped(output, MSGCODE_PING); | |
374 | output.push_back(MSGEND); | |
375 | ||
376 | if (!Write(output)) | |
377 | { | |
378 | m_controller->AddLog(CEC_LOG_ERROR, "could not send ping command"); | |
379 | return false; | |
380 | } | |
381 | ||
382 | m_controller->AddLog(CEC_LOG_DEBUG, "ping tranmitted"); | |
383 | ||
384 | // TODO check for pong | |
385 | return true; | |
386 | } | |
13fd6a66 LOK |
387 | |
388 | bool CAdapterCommunication::IsOpen(void) const | |
389 | { | |
390 | return !IsStopped() && m_port->IsOpen(); | |
391 | } |