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