Commit | Line | Data |
---|---|---|
a8f0bd18 LOK |
1 | /* |
2 | * This file is part of the libCEC(R) library. | |
3 | * | |
b492c10e | 4 | * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved. |
a8f0bd18 LOK |
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 | ||
7bb4ed43 | 33 | #include "USBCECAdapterCommunication.h" |
ba65909d LOK |
34 | #include "../platform/sockets/serialport.h" |
35 | #include "../platform/util/timeutils.h" | |
5477a250 | 36 | #include "../LibCEC.h" |
7bb4ed43 | 37 | #include "../CECProcessor.h" |
a8f0bd18 LOK |
38 | |
39 | using namespace std; | |
40 | using namespace CEC; | |
f00ff009 | 41 | using namespace PLATFORM; |
a8f0bd18 | 42 | |
7bb4ed43 | 43 | CUSBCECAdapterCommunication::CUSBCECAdapterCommunication(CCECProcessor *processor, const char *strPort, uint16_t iBaudRate /* = 38400 */) : |
12027dbe | 44 | m_port(NULL), |
a171d2fd | 45 | m_processor(processor), |
1fc16cfd | 46 | m_iLineTimeout(0), |
7bb4ed43 LOK |
47 | m_iFirmwareVersion(CEC_FW_VERSION_UNKNOWN), |
48 | m_lastInitiator(CECDEVICE_UNKNOWN), | |
49 | m_bNextIsEscaped(false), | |
50 | m_bGotStart(false) | |
a8f0bd18 | 51 | { |
99666519 | 52 | m_port = new PLATFORM::CSerialPort(strPort, iBaudRate); |
a8f0bd18 LOK |
53 | } |
54 | ||
7bb4ed43 | 55 | CUSBCECAdapterCommunication::~CUSBCECAdapterCommunication(void) |
a8f0bd18 | 56 | { |
12027dbe | 57 | Close(); |
a8f0bd18 LOK |
58 | } |
59 | ||
7bb4ed43 | 60 | bool CUSBCECAdapterCommunication::Open(uint32_t iTimeoutMs /* = 10000 */) |
a8f0bd18 | 61 | { |
7b494bea LOK |
62 | uint64_t iNow = GetTimeMs(); |
63 | uint64_t iTimeout = iNow + iTimeoutMs; | |
64 | ||
f00ff009 | 65 | CLockObject lock(m_mutex); |
7b494bea | 66 | |
13fd6a66 LOK |
67 | if (!m_port) |
68 | { | |
5477a250 | 69 | CLibCEC::AddLog(CEC_LOG_ERROR, "port is NULL"); |
a8f0bd18 | 70 | return false; |
13fd6a66 LOK |
71 | } |
72 | ||
73 | if (IsOpen()) | |
74 | { | |
5477a250 | 75 | CLibCEC::AddLog(CEC_LOG_ERROR, "port is already open"); |
7b494bea | 76 | return true; |
13fd6a66 | 77 | } |
a8f0bd18 | 78 | |
7b494bea LOK |
79 | CStdString strError; |
80 | bool bConnected(false); | |
81 | while (!bConnected && iNow < iTimeout) | |
82 | { | |
99666519 | 83 | if ((bConnected = m_port->Open(iTimeout)) == false) |
7b494bea | 84 | { |
99666519 | 85 | strError.Format("error opening serial port '%s': %s", m_port->GetName().c_str(), m_port->GetError().c_str()); |
7b494bea LOK |
86 | Sleep(250); |
87 | iNow = GetTimeMs(); | |
88 | } | |
89 | } | |
90 | ||
91 | if (!bConnected) | |
a8f0bd18 | 92 | { |
5477a250 | 93 | CLibCEC::AddLog(CEC_LOG_ERROR, strError); |
a8f0bd18 LOK |
94 | return false; |
95 | } | |
96 | ||
2c780401 | 97 | CLibCEC::AddLog(CEC_LOG_DEBUG, "connection opened, clearing any previous input and waiting for active transmissions to end before starting"); |
a8f0bd18 LOK |
98 | |
99 | //clear any input bytes | |
2c780401 LOK |
100 | uint8_t buff[1024]; |
101 | while (m_port->Read(buff, 1024, 100) > 0) | |
102 | { | |
103 | CLibCEC::AddLog(CEC_LOG_DEBUG, "data received, clearing it"); | |
104 | Sleep(250); | |
105 | } | |
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 | ||
7bb4ed43 | 120 | void CUSBCECAdapterCommunication::Close(void) |
a8f0bd18 | 121 | { |
f00ff009 | 122 | CLockObject lock(m_mutex); |
d5bffd3c | 123 | m_rcvCondition.Broadcast(); |
b9eea66d | 124 | StopThread(); |
a8f0bd18 LOK |
125 | } |
126 | ||
7bb4ed43 | 127 | void *CUSBCECAdapterCommunication::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 | ||
9f9c8c82 LOK |
140 | if (m_port) |
141 | { | |
142 | delete m_port; | |
143 | m_port = NULL; | |
144 | } | |
145 | ||
a8f0bd18 LOK |
146 | return NULL; |
147 | } | |
148 | ||
7bb4ed43 LOK |
149 | cec_adapter_message_state CUSBCECAdapterCommunication::Write(const cec_command &data, uint8_t iMaxTries, uint8_t iLineTimeout /* = 3 */, uint8_t iRetryLineTimeout /* = 3 */) |
150 | { | |
151 | cec_adapter_message_state retVal(ADAPTER_MESSAGE_STATE_UNKNOWN); | |
152 | ||
153 | CCECAdapterMessage *output = new CCECAdapterMessage(data); | |
154 | ||
155 | /* set the number of retries */ | |
156 | if (data.opcode == CEC_OPCODE_NONE) //TODO | |
157 | output->maxTries = 1; | |
158 | else if (data.initiator != CECDEVICE_BROADCAST) | |
159 | output->maxTries = iMaxTries; | |
160 | ||
161 | output->lineTimeout = iLineTimeout; | |
162 | output->retryTimeout = iRetryLineTimeout; | |
163 | output->tries = 0; | |
164 | ||
165 | bool bRetry(true); | |
166 | while (bRetry && ++output->tries < output->maxTries) | |
167 | { | |
168 | bRetry = (!Write(output) || output->NeedsRetry()) && output->transmit_timeout > 0; | |
169 | if (bRetry) | |
170 | Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT); | |
171 | } | |
172 | retVal = output->state; | |
173 | ||
174 | delete output; | |
175 | return retVal; | |
176 | } | |
177 | ||
178 | bool CUSBCECAdapterCommunication::Write(CCECAdapterMessage *data) | |
3c53ac93 | 179 | { |
5dcf9f25 LOK |
180 | bool bReturn(false); |
181 | ||
182 | CLockObject lock(data->mutex); | |
183 | data->state = ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT; | |
3c53ac93 | 184 | m_outBuffer.Push(data); |
5dcf9f25 LOK |
185 | data->condition.Wait(data->mutex); |
186 | ||
187 | if (data->state != ADAPTER_MESSAGE_STATE_SENT) | |
188 | { | |
5477a250 | 189 | CLibCEC::AddLog(CEC_LOG_ERROR, "command was not sent"); |
5dcf9f25 | 190 | } |
b2f0b1ab | 191 | else if (data->expectControllerAck) |
5dcf9f25 | 192 | { |
b2f0b1ab | 193 | bReturn = WaitForAck(*data); |
4060d525 LOK |
194 | if (bReturn) |
195 | { | |
196 | if (data->isTransmission) | |
197 | data->state = ADAPTER_MESSAGE_STATE_SENT_ACKED; | |
198 | } | |
199 | else | |
200 | { | |
201 | data->state = ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED; | |
5477a250 | 202 | CLibCEC::AddLog(CEC_LOG_DEBUG, "did not receive ack"); |
4060d525 | 203 | } |
5dcf9f25 LOK |
204 | } |
205 | else | |
206 | { | |
4060d525 | 207 | bReturn = true; |
5dcf9f25 LOK |
208 | } |
209 | ||
210 | return bReturn; | |
a8f0bd18 LOK |
211 | } |
212 | ||
7bb4ed43 | 213 | bool CUSBCECAdapterCommunication::Read(cec_command &command, uint32_t iTimeout) |
a8f0bd18 | 214 | { |
7bb4ed43 LOK |
215 | CCECAdapterMessage msg; |
216 | if (Read(msg, iTimeout)) | |
a8f0bd18 | 217 | { |
7bb4ed43 | 218 | if (ParseMessage(msg)) |
a8f0bd18 | 219 | { |
7bb4ed43 LOK |
220 | command = m_currentframe; |
221 | m_currentframe.Clear(); | |
222 | return true; | |
a8f0bd18 | 223 | } |
7bb4ed43 LOK |
224 | } |
225 | return false; | |
226 | } | |
a8f0bd18 | 227 | |
7bb4ed43 LOK |
228 | bool CUSBCECAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout) |
229 | { | |
230 | CLockObject lock(m_mutex); | |
a8f0bd18 | 231 | |
7bb4ed43 LOK |
232 | msg.Clear(); |
233 | CCECAdapterMessage *buf(NULL); | |
a8f0bd18 | 234 | |
7bb4ed43 LOK |
235 | if (!m_inBuffer.Pop(buf)) |
236 | { | |
237 | if (!m_rcvCondition.Wait(m_mutex, iTimeout)) | |
238 | return false; | |
239 | m_inBuffer.Pop(buf); | |
240 | } | |
0e31a62c | 241 | |
7bb4ed43 LOK |
242 | if (buf) |
243 | { | |
244 | msg.packet = buf->packet; | |
245 | msg.state = msg.state = ADAPTER_MESSAGE_STATE_INCOMING; | |
246 | delete buf; | |
247 | return true; | |
248 | } | |
249 | return false; | |
a8f0bd18 LOK |
250 | } |
251 | ||
7bb4ed43 | 252 | CStdString CUSBCECAdapterCommunication::GetError(void) const |
a8f0bd18 | 253 | { |
ba65909d LOK |
254 | CStdString strError; |
255 | strError = m_port->GetError(); | |
256 | return strError; | |
a8f0bd18 | 257 | } |
2abe74eb | 258 | |
7bb4ed43 | 259 | bool CUSBCECAdapterCommunication::StartBootloader(void) |
2abe74eb | 260 | { |
28352a04 | 261 | bool bReturn(false); |
2abe74eb | 262 | if (!IsRunning()) |
28352a04 | 263 | return bReturn; |
2abe74eb | 264 | |
5477a250 | 265 | CLibCEC::AddLog(CEC_LOG_DEBUG, "starting the bootloader"); |
28352a04 | 266 | CCECAdapterMessage *output = new CCECAdapterMessage; |
25701fa6 | 267 | |
ef7696f5 LOK |
268 | output->PushBack(MSGSTART); |
269 | output->PushEscaped(MSGCODE_START_BOOTLOADER); | |
270 | output->PushBack(MSGEND); | |
5dcf9f25 | 271 | output->isTransmission = false; |
71c4a2f5 | 272 | output->expectControllerAck = false; |
2abe74eb | 273 | |
5dcf9f25 | 274 | if ((bReturn = Write(output)) == false) |
5477a250 | 275 | CLibCEC::AddLog(CEC_LOG_ERROR, "could not start the bootloader"); |
28352a04 LOK |
276 | delete output; |
277 | ||
278 | return bReturn; | |
2abe74eb LOK |
279 | } |
280 | ||
7bb4ed43 | 281 | bool CUSBCECAdapterCommunication::PingAdapter(void) |
2abe74eb | 282 | { |
28352a04 | 283 | bool bReturn(false); |
2abe74eb | 284 | if (!IsRunning()) |
28352a04 | 285 | return bReturn; |
2abe74eb | 286 | |
5477a250 | 287 | CLibCEC::AddLog(CEC_LOG_DEBUG, "sending ping"); |
28352a04 | 288 | CCECAdapterMessage *output = new CCECAdapterMessage; |
25701fa6 | 289 | |
ef7696f5 LOK |
290 | output->PushBack(MSGSTART); |
291 | output->PushEscaped(MSGCODE_PING); | |
292 | output->PushBack(MSGEND); | |
5dcf9f25 | 293 | output->isTransmission = false; |
2abe74eb | 294 | |
5dcf9f25 | 295 | if ((bReturn = Write(output)) == false) |
5477a250 | 296 | CLibCEC::AddLog(CEC_LOG_ERROR, "could not ping the adapter"); |
28352a04 LOK |
297 | delete output; |
298 | ||
299 | return bReturn; | |
2abe74eb | 300 | } |
13fd6a66 | 301 | |
7bb4ed43 LOK |
302 | bool CUSBCECAdapterCommunication::ParseMessage(const CCECAdapterMessage &msg) |
303 | { | |
304 | bool bEom(false); | |
305 | bool bIsError(msg.IsError()); | |
306 | ||
307 | if (msg.IsEmpty()) | |
308 | return bEom; | |
309 | ||
310 | switch(msg.Message()) | |
311 | { | |
312 | case MSGCODE_FRAME_START: | |
313 | { | |
314 | m_currentframe.Clear(); | |
315 | if (msg.Size() >= 2) | |
316 | { | |
317 | m_currentframe.initiator = msg.Initiator(); | |
318 | m_currentframe.destination = msg.Destination(); | |
319 | m_currentframe.ack = msg.IsACK(); | |
320 | m_currentframe.eom = msg.IsEOM(); | |
321 | } | |
322 | if (m_currentframe.ack == 0x1) | |
323 | { | |
324 | m_lastInitiator = m_currentframe.initiator; | |
325 | m_processor->HandlePoll(m_currentframe.initiator, m_currentframe.destination); | |
326 | } | |
327 | } | |
328 | break; | |
329 | case MSGCODE_RECEIVE_FAILED: | |
330 | { | |
331 | m_currentframe.Clear(); | |
332 | if (m_lastInitiator != CECDEVICE_UNKNOWN) | |
333 | bIsError = m_processor->HandleReceiveFailed(m_lastInitiator); | |
334 | } | |
335 | break; | |
336 | case MSGCODE_FRAME_DATA: | |
337 | { | |
338 | if (msg.Size() >= 2) | |
339 | { | |
340 | m_currentframe.PushBack(msg[1]); | |
341 | m_currentframe.eom = msg.IsEOM(); | |
342 | } | |
343 | bEom = msg.IsEOM(); | |
344 | } | |
345 | break; | |
346 | default: | |
347 | break; | |
348 | } | |
349 | ||
350 | CLibCEC::AddLog(bIsError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString()); | |
351 | return bEom; | |
352 | } | |
353 | ||
354 | uint16_t CUSBCECAdapterCommunication::GetFirmwareVersion(void) | |
1fc16cfd LOK |
355 | { |
356 | uint16_t iReturn(m_iFirmwareVersion); | |
357 | if (!IsRunning()) | |
358 | return iReturn; | |
359 | ||
360 | if (iReturn == CEC_FW_VERSION_UNKNOWN) | |
361 | { | |
5477a250 | 362 | CLibCEC::AddLog(CEC_LOG_DEBUG, "requesting the firmware version"); |
1fc16cfd LOK |
363 | CCECAdapterMessage *output = new CCECAdapterMessage; |
364 | ||
365 | output->PushBack(MSGSTART); | |
366 | output->PushEscaped(MSGCODE_FIRMWARE_VERSION); | |
367 | output->PushBack(MSGEND); | |
368 | output->isTransmission = false; | |
369 | output->expectControllerAck = false; | |
370 | ||
371 | SendMessageToAdapter(output); | |
372 | delete output; | |
373 | ||
374 | CCECAdapterMessage input; | |
375 | if (!Read(input, CEC_DEFAULT_TRANSMIT_WAIT) || input.Message() != MSGCODE_FIRMWARE_VERSION || input.Size() != 3) | |
2c780401 | 376 | CLibCEC::AddLog(CEC_LOG_ERROR, "no or invalid firmware version (size = %d, message = %d)", input.Size(), input.Message()); |
1fc16cfd LOK |
377 | else |
378 | { | |
379 | m_iFirmwareVersion = (input[1] << 8 | input[2]); | |
380 | iReturn = m_iFirmwareVersion; | |
381 | } | |
382 | } | |
383 | ||
384 | return iReturn; | |
385 | } | |
386 | ||
7bb4ed43 | 387 | bool CUSBCECAdapterCommunication::SetLineTimeout(uint8_t iTimeout) |
a171d2fd | 388 | { |
7bb4ed43 LOK |
389 | m_iLineTimeout = iTimeout; |
390 | return true; | |
391 | //TODO | |
392 | // bool bReturn(m_iLineTimeout != iTimeout); | |
393 | // | |
394 | // if (!bReturn) | |
395 | // { | |
396 | // CCECAdapterMessage *output = new CCECAdapterMessage; | |
397 | // | |
398 | // output->PushBack(MSGSTART); | |
399 | // output->PushEscaped(MSGCODE_TRANSMIT_IDLETIME); | |
400 | // output->PushEscaped(iTimeout); | |
401 | // output->PushBack(MSGEND); | |
402 | // output->isTransmission = false; | |
403 | // | |
404 | // if ((bReturn = Write(output)) == false) | |
405 | // CLibCEC::AddLog(CEC_LOG_ERROR, "could not set the idletime"); | |
406 | // delete output; | |
407 | // } | |
408 | // | |
409 | // return bReturn; | |
a171d2fd LOK |
410 | } |
411 | ||
7bb4ed43 | 412 | bool CUSBCECAdapterCommunication::SetAckMask(uint16_t iMask) |
5dcf9f25 LOK |
413 | { |
414 | bool bReturn(false); | |
bca69ca1 | 415 | CLibCEC::AddLog(CEC_LOG_DEBUG, "setting ackmask to %2x", iMask); |
5dcf9f25 LOK |
416 | |
417 | CCECAdapterMessage *output = new CCECAdapterMessage; | |
418 | ||
419 | output->PushBack(MSGSTART); | |
420 | output->PushEscaped(MSGCODE_SET_ACK_MASK); | |
421 | output->PushEscaped(iMask >> 8); | |
422 | output->PushEscaped((uint8_t)iMask); | |
423 | output->PushBack(MSGEND); | |
424 | output->isTransmission = false; | |
425 | ||
426 | if ((bReturn = Write(output)) == false) | |
5477a250 | 427 | CLibCEC::AddLog(CEC_LOG_ERROR, "could not set the ackmask"); |
5dcf9f25 LOK |
428 | delete output; |
429 | ||
430 | return bReturn; | |
431 | } | |
432 | ||
b057edad BL |
433 | |
434 | bool CUSBCECAdapterCommunication::SetControlledMode(bool controlled) | |
435 | { | |
436 | bool bReturn(false); | |
bca69ca1 | 437 | CLibCEC::AddLog(CEC_LOG_DEBUG, "turning controlled mode %s", controlled ? "on" : "off"); |
b057edad BL |
438 | |
439 | CCECAdapterMessage *output = new CCECAdapterMessage; | |
440 | ||
441 | output->PushBack(MSGSTART); | |
442 | output->PushEscaped(MSGCODE_SET_CONTROLLED); | |
443 | output->PushEscaped(controlled); | |
444 | output->PushBack(MSGEND); | |
445 | output->isTransmission = false; | |
446 | ||
447 | if ((bReturn = Write(output)) == false) | |
448 | CLibCEC::AddLog(CEC_LOG_ERROR, "could not set controlled mode"); | |
449 | delete output; | |
450 | ||
451 | return bReturn; | |
452 | } | |
453 | ||
7bb4ed43 | 454 | bool CUSBCECAdapterCommunication::IsOpen(void) |
13fd6a66 | 455 | { |
b9eea66d | 456 | return !IsStopped() && m_port->IsOpen() && IsRunning(); |
13fd6a66 | 457 | } |
ef7696f5 | 458 | |
7bb4ed43 | 459 | bool CUSBCECAdapterCommunication::WaitForAck(CCECAdapterMessage &message) |
6729ac71 LOK |
460 | { |
461 | bool bError(false); | |
462 | bool bTransmitSucceeded(false); | |
b2f0b1ab | 463 | uint8_t iPacketsLeft(message.Size() / 4); |
6729ac71 LOK |
464 | |
465 | int64_t iNow = GetTimeMs(); | |
b2f0b1ab | 466 | int64_t iTargetTime = iNow + message.transmit_timeout; |
6729ac71 | 467 | |
b2f0b1ab | 468 | while (!bTransmitSucceeded && !bError && (message.transmit_timeout == 0 || iNow < iTargetTime)) |
6729ac71 LOK |
469 | { |
470 | CCECAdapterMessage msg; | |
e2800c15 | 471 | int32_t iWait = (int32_t)(iTargetTime - iNow); |
b2f0b1ab | 472 | if (iWait <= 5 || message.transmit_timeout <= 5) |
e2800c15 | 473 | iWait = CEC_DEFAULT_TRANSMIT_WAIT; |
6729ac71 | 474 | |
e2800c15 | 475 | if (!Read(msg, iWait)) |
6729ac71 LOK |
476 | { |
477 | iNow = GetTimeMs(); | |
478 | continue; | |
479 | } | |
480 | ||
481 | if (msg.Message() == MSGCODE_FRAME_START && msg.IsACK()) | |
482 | { | |
483 | m_processor->HandlePoll(msg.Initiator(), msg.Destination()); | |
7bb4ed43 | 484 | m_lastInitiator = msg.Initiator(); |
6729ac71 LOK |
485 | iNow = GetTimeMs(); |
486 | continue; | |
487 | } | |
488 | ||
489 | if (msg.Message() == MSGCODE_RECEIVE_FAILED && | |
7bb4ed43 LOK |
490 | m_lastInitiator != CECDEVICE_UNKNOWN && |
491 | m_processor->HandleReceiveFailed(m_lastInitiator)) | |
6729ac71 LOK |
492 | { |
493 | iNow = GetTimeMs(); | |
494 | continue; | |
495 | } | |
496 | ||
497 | bError = msg.IsError(); | |
498 | if (bError) | |
499 | { | |
b2f0b1ab | 500 | message.reply = msg.Message(); |
5477a250 | 501 | CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString()); |
6729ac71 LOK |
502 | } |
503 | else | |
504 | { | |
505 | switch(msg.Message()) | |
506 | { | |
507 | case MSGCODE_COMMAND_ACCEPTED: | |
5477a250 | 508 | CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString()); |
6729ac71 LOK |
509 | if (iPacketsLeft > 0) |
510 | iPacketsLeft--; | |
b2f0b1ab | 511 | if (!message.isTransmission && iPacketsLeft == 0) |
5dcf9f25 | 512 | bTransmitSucceeded = true; |
6729ac71 LOK |
513 | break; |
514 | case MSGCODE_TRANSMIT_SUCCEEDED: | |
5477a250 | 515 | CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString()); |
6729ac71 LOK |
516 | bTransmitSucceeded = (iPacketsLeft == 0); |
517 | bError = !bTransmitSucceeded; | |
b2f0b1ab | 518 | message.reply = MSGCODE_TRANSMIT_SUCCEEDED; |
6729ac71 LOK |
519 | break; |
520 | default: | |
521 | // ignore other data while waiting | |
522 | break; | |
523 | } | |
524 | ||
525 | iNow = GetTimeMs(); | |
526 | } | |
527 | } | |
528 | ||
529 | return bTransmitSucceeded && !bError; | |
530 | } | |
531 | ||
7bb4ed43 | 532 | void CUSBCECAdapterCommunication::AddData(uint8_t *data, size_t iLen) |
ef7696f5 LOK |
533 | { |
534 | CLockObject lock(m_mutex); | |
99666519 | 535 | for (size_t iPtr = 0; iPtr < iLen; iPtr++) |
7bb4ed43 LOK |
536 | { |
537 | if (!m_bGotStart) | |
538 | { | |
539 | if (data[iPtr] == MSGSTART) | |
540 | m_bGotStart = true; | |
541 | } | |
542 | else if (data[iPtr] == MSGSTART) //we found a msgstart before msgend, this is not right, remove | |
543 | { | |
544 | if (m_currentAdapterMessage.Size() > 0) | |
545 | CLibCEC::AddLog(CEC_LOG_WARNING, "received MSGSTART before MSGEND, removing previous buffer contents"); | |
546 | m_currentAdapterMessage.Clear(); | |
547 | m_bGotStart = true; | |
548 | } | |
549 | else if (data[iPtr] == MSGEND) | |
550 | { | |
551 | CCECAdapterMessage *newMessage = new CCECAdapterMessage; | |
552 | newMessage->packet = m_currentAdapterMessage.packet; | |
553 | m_inBuffer.Push(newMessage); | |
554 | m_currentAdapterMessage.Clear(); | |
555 | m_bGotStart = false; | |
556 | m_bNextIsEscaped = false; | |
557 | m_rcvCondition.Signal(); | |
558 | } | |
559 | else if (m_bNextIsEscaped) | |
560 | { | |
561 | m_currentAdapterMessage.PushBack(data[iPtr] + (uint8_t)ESCOFFSET); | |
562 | m_bNextIsEscaped = false; | |
563 | } | |
564 | else if (data[iPtr] == MSGESC) | |
565 | { | |
566 | m_bNextIsEscaped = true; | |
567 | } | |
568 | else | |
569 | { | |
570 | m_currentAdapterMessage.PushBack(data[iPtr]); | |
571 | } | |
572 | } | |
ef7696f5 LOK |
573 | } |
574 | ||
7bb4ed43 | 575 | bool CUSBCECAdapterCommunication::ReadFromDevice(uint32_t iTimeout) |
ef7696f5 | 576 | { |
99666519 LOK |
577 | ssize_t iBytesRead; |
578 | uint8_t buff[256]; | |
ef7696f5 LOK |
579 | if (!m_port) |
580 | return false; | |
581 | ||
1fc16cfd | 582 | CLockObject lock(m_mutex); |
ef7696f5 LOK |
583 | iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout); |
584 | if (iBytesRead < 0 || iBytesRead > 256) | |
585 | { | |
99666519 | 586 | CLibCEC::AddLog(CEC_LOG_ERROR, "error reading from serial port: %s", m_port->GetError().c_str()); |
ef7696f5 LOK |
587 | return false; |
588 | } | |
589 | else if (iBytesRead > 0) | |
99666519 LOK |
590 | { |
591 | AddData(buff, iBytesRead); | |
592 | } | |
ef7696f5 LOK |
593 | |
594 | return iBytesRead > 0; | |
595 | } | |
596 | ||
7bb4ed43 | 597 | void CUSBCECAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage *msg) |
ef7696f5 | 598 | { |
1fc16cfd | 599 | CLockObject adapterLock(m_mutex); |
ef7696f5 | 600 | CLockObject lock(msg->mutex); |
7bb4ed43 LOK |
601 | if (msg->tries == 1) |
602 | SetLineTimeout(msg->lineTimeout); | |
603 | else | |
604 | SetLineTimeout(msg->retryTimeout); | |
605 | ||
99666519 | 606 | if (m_port->Write(msg->packet.data, msg->Size()) != (ssize_t) msg->Size()) |
ef7696f5 | 607 | { |
b74fd339 | 608 | CLibCEC::AddLog(CEC_LOG_ERROR, "error writing to serial port: %s", m_port->GetError().c_str()); |
ef7696f5 LOK |
609 | msg->state = ADAPTER_MESSAGE_STATE_ERROR; |
610 | } | |
611 | else | |
612 | { | |
5477a250 | 613 | CLibCEC::AddLog(CEC_LOG_DEBUG, "command sent"); |
ef7696f5 LOK |
614 | msg->state = ADAPTER_MESSAGE_STATE_SENT; |
615 | } | |
616 | msg->condition.Signal(); | |
617 | } | |
618 | ||
7bb4ed43 | 619 | void CUSBCECAdapterCommunication::WriteNextCommand(void) |
ef7696f5 LOK |
620 | { |
621 | CCECAdapterMessage *msg(NULL); | |
622 | if (m_outBuffer.Pop(msg)) | |
623 | SendMessageToAdapter(msg); | |
624 | } |