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 | 34 | |
ef7696f5 | 35 | #include "AdapterMessage.h" |
1113cb7d | 36 | #include "CECProcessor.h" |
f00ff009 | 37 | #include "platform/serialport/serialport.h" |
a8f0bd18 LOK |
38 | |
39 | using namespace std; | |
40 | using namespace CEC; | |
f00ff009 | 41 | using namespace PLATFORM; |
a8f0bd18 | 42 | |
1113cb7d | 43 | CAdapterCommunication::CAdapterCommunication(CCECProcessor *processor) : |
12027dbe | 44 | m_port(NULL), |
a171d2fd LOK |
45 | m_processor(processor), |
46 | m_iLineTimeout(0) | |
a8f0bd18 | 47 | { |
f00ff009 | 48 | m_port = new PLATFORM::CSerialPort; |
a8f0bd18 LOK |
49 | } |
50 | ||
828682d3 | 51 | CAdapterCommunication::~CAdapterCommunication(void) |
a8f0bd18 | 52 | { |
12027dbe LOK |
53 | Close(); |
54 | ||
55 | if (m_port) | |
56 | { | |
57 | delete m_port; | |
58 | m_port = NULL; | |
59 | } | |
a8f0bd18 LOK |
60 | } |
61 | ||
25701fa6 | 62 | bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */) |
a8f0bd18 | 63 | { |
7b494bea LOK |
64 | uint64_t iNow = GetTimeMs(); |
65 | uint64_t iTimeout = iNow + iTimeoutMs; | |
66 | ||
f00ff009 | 67 | CLockObject lock(m_mutex); |
7b494bea | 68 | |
13fd6a66 LOK |
69 | if (!m_port) |
70 | { | |
1113cb7d | 71 | m_processor->AddLog(CEC_LOG_ERROR, "port is NULL"); |
a8f0bd18 | 72 | return false; |
13fd6a66 LOK |
73 | } |
74 | ||
75 | if (IsOpen()) | |
76 | { | |
1113cb7d | 77 | m_processor->AddLog(CEC_LOG_ERROR, "port is already open"); |
7b494bea | 78 | return true; |
13fd6a66 | 79 | } |
a8f0bd18 | 80 | |
7b494bea LOK |
81 | CStdString strError; |
82 | bool bConnected(false); | |
83 | while (!bConnected && iNow < iTimeout) | |
84 | { | |
d956c02e | 85 | if ((bConnected = m_port->Open(strPort, iBaudRate)) == false) |
7b494bea LOK |
86 | { |
87 | strError.Format("error opening serial port '%s': %s", strPort, m_port->GetError().c_str()); | |
88 | Sleep(250); | |
89 | iNow = GetTimeMs(); | |
90 | } | |
91 | } | |
92 | ||
93 | if (!bConnected) | |
a8f0bd18 | 94 | { |
1113cb7d | 95 | m_processor->AddLog(CEC_LOG_ERROR, strError); |
a8f0bd18 LOK |
96 | return false; |
97 | } | |
98 | ||
1113cb7d | 99 | m_processor->AddLog(CEC_LOG_DEBUG, "connection opened"); |
a8f0bd18 LOK |
100 | |
101 | //clear any input bytes | |
aee82ba0 LOK |
102 | uint8_t buff[1]; |
103 | while (m_port->Read(buff, 1, 5) == 1) {} | |
a8f0bd18 | 104 | |
828682d3 | 105 | if (CreateThread()) |
a8f0bd18 | 106 | { |
1113cb7d | 107 | m_processor->AddLog(CEC_LOG_DEBUG, "communication thread started"); |
a8f0bd18 LOK |
108 | return true; |
109 | } | |
110 | else | |
111 | { | |
ef7696f5 | 112 | m_processor->AddLog(CEC_LOG_ERROR, "could not create a communication thread"); |
a8f0bd18 LOK |
113 | } |
114 | ||
115 | return false; | |
116 | } | |
117 | ||
828682d3 | 118 | void CAdapterCommunication::Close(void) |
a8f0bd18 | 119 | { |
f00ff009 | 120 | CLockObject lock(m_mutex); |
d5bffd3c | 121 | m_rcvCondition.Broadcast(); |
b9eea66d | 122 | StopThread(); |
a8f0bd18 LOK |
123 | } |
124 | ||
828682d3 | 125 | void *CAdapterCommunication::Process(void) |
a8f0bd18 | 126 | { |
13fd6a66 | 127 | while (!IsStopped()) |
a8f0bd18 | 128 | { |
9e1cfa5c | 129 | ReadFromDevice(50); |
c02980af | 130 | Sleep(5); |
3c53ac93 | 131 | WriteNextCommand(); |
a8f0bd18 LOK |
132 | } |
133 | ||
ef7696f5 | 134 | CCECAdapterMessage *msg(NULL); |
a0878ee3 LOK |
135 | if (m_outBuffer.Pop(msg)) |
136 | msg->condition.Broadcast(); | |
137 | ||
a8f0bd18 LOK |
138 | return NULL; |
139 | } | |
140 | ||
28352a04 | 141 | bool CAdapterCommunication::Write(CCECAdapterMessage *data) |
3c53ac93 | 142 | { |
5dcf9f25 LOK |
143 | bool bReturn(false); |
144 | ||
145 | CLockObject lock(data->mutex); | |
146 | data->state = ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT; | |
3c53ac93 | 147 | m_outBuffer.Push(data); |
5dcf9f25 LOK |
148 | data->condition.Wait(data->mutex); |
149 | ||
150 | if (data->state != ADAPTER_MESSAGE_STATE_SENT) | |
151 | { | |
152 | m_processor->AddLog(CEC_LOG_ERROR, "command was not sent"); | |
153 | } | |
154 | if (WaitForTransmitSucceeded(data)) | |
155 | { | |
156 | if (data->isTransmission) | |
157 | data->state = ADAPTER_MESSAGE_STATE_SENT_ACKED; | |
158 | bReturn = true; | |
159 | } | |
160 | else | |
161 | { | |
162 | data->state = ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED; | |
163 | m_processor->AddLog(CEC_LOG_DEBUG, "did not receive ack"); | |
164 | } | |
165 | ||
166 | return bReturn; | |
a8f0bd18 LOK |
167 | } |
168 | ||
220537f2 | 169 | bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout) |
a8f0bd18 | 170 | { |
f00ff009 | 171 | CLockObject lock(m_mutex); |
a8f0bd18 | 172 | |
ef7696f5 | 173 | msg.Clear(); |
50aa01e6 LOK |
174 | uint64_t iNow = GetTimeMs(); |
175 | uint64_t iTarget = iNow + iTimeout; | |
176 | bool bGotFullMessage(false); | |
177 | bool bNextIsEscaped(false); | |
178 | bool bGotStart(false); | |
a8f0bd18 | 179 | |
50aa01e6 | 180 | while(!bGotFullMessage && iNow < iTarget) |
a8f0bd18 | 181 | { |
50aa01e6 LOK |
182 | uint8_t buf = 0; |
183 | if (!m_inBuffer.Pop(buf)) | |
a8f0bd18 | 184 | { |
f00ff009 | 185 | if (!m_rcvCondition.Wait(m_mutex, (uint32_t) (iTarget - iNow))) |
50aa01e6 | 186 | return false; |
a8f0bd18 | 187 | } |
a8f0bd18 | 188 | |
50aa01e6 | 189 | if (!bGotStart) |
a8f0bd18 | 190 | { |
50aa01e6 LOK |
191 | if (buf == MSGSTART) |
192 | bGotStart = true; | |
193 | continue; | |
a8f0bd18 | 194 | } |
50aa01e6 | 195 | else if (buf == MSGSTART) //we found a msgstart before msgend, this is not right, remove |
a8f0bd18 | 196 | { |
ef7696f5 | 197 | if (msg.Size() > 0) |
1113cb7d | 198 | m_processor->AddLog(CEC_LOG_WARNING, "received MSGSTART before MSGEND, removing previous buffer contents"); |
ef7696f5 | 199 | msg.Clear(); |
50aa01e6 | 200 | bGotStart = true; |
a8f0bd18 | 201 | } |
a8f0bd18 | 202 | |
50aa01e6 | 203 | if (buf == MSGEND) |
a8f0bd18 | 204 | { |
50aa01e6 | 205 | bGotFullMessage = true; |
a8f0bd18 | 206 | } |
50aa01e6 LOK |
207 | else if (bNextIsEscaped) |
208 | { | |
ef7696f5 | 209 | msg.PushBack(buf + (uint8_t)ESCOFFSET); |
50aa01e6 LOK |
210 | bNextIsEscaped = false; |
211 | } | |
212 | else if (buf == MSGESC) | |
213 | bNextIsEscaped = true; | |
214 | else | |
ef7696f5 | 215 | msg.PushBack(buf); |
a8f0bd18 LOK |
216 | } |
217 | ||
0e31a62c | 218 | if (bGotFullMessage) |
5dcf9f25 | 219 | msg.state = ADAPTER_MESSAGE_STATE_INCOMING; |
0e31a62c | 220 | |
50aa01e6 | 221 | return bGotFullMessage; |
a8f0bd18 LOK |
222 | } |
223 | ||
828682d3 | 224 | std::string CAdapterCommunication::GetError(void) const |
a8f0bd18 LOK |
225 | { |
226 | return m_port->GetError(); | |
227 | } | |
2abe74eb LOK |
228 | |
229 | bool CAdapterCommunication::StartBootloader(void) | |
230 | { | |
28352a04 | 231 | bool bReturn(false); |
2abe74eb | 232 | if (!IsRunning()) |
28352a04 | 233 | return bReturn; |
2abe74eb | 234 | |
1113cb7d | 235 | m_processor->AddLog(CEC_LOG_DEBUG, "starting the bootloader"); |
28352a04 | 236 | CCECAdapterMessage *output = new CCECAdapterMessage; |
25701fa6 | 237 | |
ef7696f5 LOK |
238 | output->PushBack(MSGSTART); |
239 | output->PushEscaped(MSGCODE_START_BOOTLOADER); | |
240 | output->PushBack(MSGEND); | |
5dcf9f25 | 241 | output->isTransmission = false; |
2abe74eb | 242 | |
5dcf9f25 LOK |
243 | if ((bReturn = Write(output)) == false) |
244 | m_processor->AddLog(CEC_LOG_ERROR, "could not start the bootloader"); | |
28352a04 LOK |
245 | delete output; |
246 | ||
247 | return bReturn; | |
2abe74eb LOK |
248 | } |
249 | ||
2abe74eb LOK |
250 | bool CAdapterCommunication::PingAdapter(void) |
251 | { | |
28352a04 | 252 | bool bReturn(false); |
2abe74eb | 253 | if (!IsRunning()) |
28352a04 | 254 | return bReturn; |
2abe74eb | 255 | |
1113cb7d | 256 | m_processor->AddLog(CEC_LOG_DEBUG, "sending ping"); |
28352a04 | 257 | CCECAdapterMessage *output = new CCECAdapterMessage; |
25701fa6 | 258 | |
ef7696f5 LOK |
259 | output->PushBack(MSGSTART); |
260 | output->PushEscaped(MSGCODE_PING); | |
261 | output->PushBack(MSGEND); | |
5dcf9f25 | 262 | output->isTransmission = false; |
2abe74eb | 263 | |
5dcf9f25 LOK |
264 | if ((bReturn = Write(output)) == false) |
265 | m_processor->AddLog(CEC_LOG_ERROR, "could not ping the adapter"); | |
28352a04 LOK |
266 | delete output; |
267 | ||
268 | return bReturn; | |
2abe74eb | 269 | } |
13fd6a66 | 270 | |
a171d2fd LOK |
271 | bool CAdapterCommunication::SetLineTimeout(uint8_t iTimeout) |
272 | { | |
273 | bool bReturn(m_iLineTimeout != iTimeout); | |
274 | ||
275 | if (!bReturn) | |
276 | { | |
277 | CCECAdapterMessage *output = new CCECAdapterMessage; | |
278 | ||
ef7696f5 LOK |
279 | output->PushBack(MSGSTART); |
280 | output->PushEscaped(MSGCODE_TRANSMIT_IDLETIME); | |
281 | output->PushEscaped(iTimeout); | |
282 | output->PushBack(MSGEND); | |
5dcf9f25 | 283 | output->isTransmission = false; |
a171d2fd LOK |
284 | |
285 | if ((bReturn = Write(output)) == false) | |
286 | m_processor->AddLog(CEC_LOG_ERROR, "could not set the idletime"); | |
287 | delete output; | |
288 | } | |
289 | ||
290 | return bReturn; | |
291 | } | |
292 | ||
5dcf9f25 LOK |
293 | bool CAdapterCommunication::SetAckMask(uint16_t iMask) |
294 | { | |
295 | bool bReturn(false); | |
296 | CStdString strLog; | |
297 | strLog.Format("setting ackmask to %2x", iMask); | |
298 | m_processor->AddLog(CEC_LOG_DEBUG, strLog.c_str()); | |
299 | ||
300 | CCECAdapterMessage *output = new CCECAdapterMessage; | |
301 | ||
302 | output->PushBack(MSGSTART); | |
303 | output->PushEscaped(MSGCODE_SET_ACK_MASK); | |
304 | output->PushEscaped(iMask >> 8); | |
305 | output->PushEscaped((uint8_t)iMask); | |
306 | output->PushBack(MSGEND); | |
307 | output->isTransmission = false; | |
308 | ||
309 | if ((bReturn = Write(output)) == false) | |
310 | m_processor->AddLog(CEC_LOG_ERROR, "could not set the ackmask"); | |
311 | delete output; | |
312 | ||
313 | return bReturn; | |
314 | } | |
315 | ||
f00ff009 | 316 | bool CAdapterCommunication::IsOpen(void) |
13fd6a66 | 317 | { |
b9eea66d | 318 | return !IsStopped() && m_port->IsOpen() && IsRunning(); |
13fd6a66 | 319 | } |
ef7696f5 | 320 | |
6729ac71 LOK |
321 | bool CAdapterCommunication::WaitForTransmitSucceeded(CCECAdapterMessage *message) |
322 | { | |
323 | bool bError(false); | |
324 | bool bTransmitSucceeded(false); | |
325 | uint8_t iPacketsLeft(message->Size() / 4); | |
326 | ||
327 | int64_t iNow = GetTimeMs(); | |
328 | int64_t iTargetTime = iNow + message->transmit_timeout; | |
329 | ||
330 | while (!bTransmitSucceeded && !bError && (message->transmit_timeout == 0 || iNow < iTargetTime)) | |
331 | { | |
332 | CCECAdapterMessage msg; | |
333 | ||
334 | if (!Read(msg, message->transmit_timeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000)) | |
335 | { | |
336 | iNow = GetTimeMs(); | |
337 | continue; | |
338 | } | |
339 | ||
340 | if (msg.Message() == MSGCODE_FRAME_START && msg.IsACK()) | |
341 | { | |
342 | m_processor->HandlePoll(msg.Initiator(), msg.Destination()); | |
343 | iNow = GetTimeMs(); | |
344 | continue; | |
345 | } | |
346 | ||
347 | if (msg.Message() == MSGCODE_RECEIVE_FAILED && | |
348 | m_processor->HandleReceiveFailed()) | |
349 | { | |
350 | iNow = GetTimeMs(); | |
351 | continue; | |
352 | } | |
353 | ||
354 | bError = msg.IsError(); | |
355 | if (bError) | |
356 | { | |
357 | message->reply = msg.Message(); | |
358 | m_processor->AddLog(CEC_LOG_DEBUG, msg.ToString()); | |
359 | } | |
360 | else | |
361 | { | |
362 | switch(msg.Message()) | |
363 | { | |
364 | case MSGCODE_COMMAND_ACCEPTED: | |
365 | m_processor->AddLog(CEC_LOG_DEBUG, msg.ToString()); | |
366 | if (iPacketsLeft > 0) | |
367 | iPacketsLeft--; | |
5dcf9f25 LOK |
368 | if (!message->isTransmission && iPacketsLeft == 0) |
369 | bTransmitSucceeded = true; | |
6729ac71 LOK |
370 | break; |
371 | case MSGCODE_TRANSMIT_SUCCEEDED: | |
372 | m_processor->AddLog(CEC_LOG_DEBUG, msg.ToString()); | |
373 | bTransmitSucceeded = (iPacketsLeft == 0); | |
374 | bError = !bTransmitSucceeded; | |
375 | message->reply = MSGCODE_TRANSMIT_SUCCEEDED; | |
376 | break; | |
377 | default: | |
378 | // ignore other data while waiting | |
379 | break; | |
380 | } | |
381 | ||
382 | iNow = GetTimeMs(); | |
383 | } | |
384 | } | |
385 | ||
386 | return bTransmitSucceeded && !bError; | |
387 | } | |
388 | ||
ef7696f5 LOK |
389 | void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen) |
390 | { | |
391 | CLockObject lock(m_mutex); | |
392 | for (uint8_t iPtr = 0; iPtr < iLen; iPtr++) | |
393 | m_inBuffer.Push(data[iPtr]); | |
394 | ||
395 | m_rcvCondition.Signal(); | |
396 | } | |
397 | ||
398 | bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout) | |
399 | { | |
400 | int32_t iBytesRead; | |
401 | uint8_t buff[1024]; | |
402 | if (!m_port) | |
403 | return false; | |
404 | ||
405 | iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout); | |
406 | if (iBytesRead < 0 || iBytesRead > 256) | |
407 | { | |
408 | CStdString strError; | |
409 | strError.Format("error reading from serial port: %s", m_port->GetError().c_str()); | |
410 | m_processor->AddLog(CEC_LOG_ERROR, strError); | |
411 | return false; | |
412 | } | |
413 | else if (iBytesRead > 0) | |
414 | AddData(buff, (uint8_t) iBytesRead); | |
415 | ||
416 | return iBytesRead > 0; | |
417 | } | |
418 | ||
419 | void CAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage *msg) | |
420 | { | |
421 | CLockObject lock(msg->mutex); | |
422 | if (m_port->Write(msg->packet.data, msg->Size()) != (int32_t) msg->Size()) | |
423 | { | |
424 | CStdString strError; | |
425 | strError.Format("error writing to serial port: %s", m_port->GetError().c_str()); | |
426 | m_processor->AddLog(CEC_LOG_ERROR, strError); | |
427 | msg->state = ADAPTER_MESSAGE_STATE_ERROR; | |
428 | } | |
429 | else | |
430 | { | |
431 | m_processor->AddLog(CEC_LOG_DEBUG, "command sent"); | |
432 | msg->state = ADAPTER_MESSAGE_STATE_SENT; | |
433 | } | |
434 | msg->condition.Signal(); | |
435 | } | |
436 | ||
437 | void CAdapterCommunication::WriteNextCommand(void) | |
438 | { | |
439 | CCECAdapterMessage *msg(NULL); | |
440 | if (m_outBuffer.Pop(msg)) | |
441 | SendMessageToAdapter(msg); | |
442 | } |