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 | |
83554890 LOK |
43 | void *CUSBCECAdapterProcessor::Process(void) |
44 | { | |
45 | cec_command command; | |
46 | while (!IsStopped()) | |
47 | { | |
48 | if (m_inBuffer.Pop(command)) | |
49 | m_callback->OnCommandReceived(command); | |
50 | Sleep(5); | |
51 | } | |
52 | ||
53 | return NULL; | |
54 | } | |
55 | ||
56 | void CUSBCECAdapterProcessor::AddCommand(cec_command command) | |
57 | { | |
58 | m_inBuffer.Push(command); | |
59 | } | |
60 | ||
7bb4ed43 | 61 | CUSBCECAdapterCommunication::CUSBCECAdapterCommunication(CCECProcessor *processor, const char *strPort, uint16_t iBaudRate /* = 38400 */) : |
12027dbe | 62 | m_port(NULL), |
a171d2fd | 63 | m_processor(processor), |
960f33c6 | 64 | m_bHasData(false), |
1fc16cfd | 65 | m_iLineTimeout(0), |
7bb4ed43 LOK |
66 | m_iFirmwareVersion(CEC_FW_VERSION_UNKNOWN), |
67 | m_lastInitiator(CECDEVICE_UNKNOWN), | |
68 | m_bNextIsEscaped(false), | |
83554890 | 69 | m_bGotStart(false), |
cccd2724 LOK |
70 | m_messageProcessor(NULL), |
71 | m_bInitialised(false) | |
a8f0bd18 | 72 | { |
99666519 | 73 | m_port = new PLATFORM::CSerialPort(strPort, iBaudRate); |
a8f0bd18 LOK |
74 | } |
75 | ||
7bb4ed43 | 76 | CUSBCECAdapterCommunication::~CUSBCECAdapterCommunication(void) |
a8f0bd18 | 77 | { |
12027dbe | 78 | Close(); |
a8f0bd18 LOK |
79 | } |
80 | ||
efed01e1 | 81 | bool CUSBCECAdapterCommunication::CheckAdapter(uint32_t iTimeoutMs /* = 10000 */) |
a8f0bd18 | 82 | { |
efed01e1 | 83 | bool bReturn(false); |
7b494bea | 84 | uint64_t iNow = GetTimeMs(); |
efed01e1 | 85 | uint64_t iTarget = iTimeoutMs > 0 ? iNow + iTimeoutMs : iNow + CEC_DEFAULT_TRANSMIT_WAIT; |
7b494bea | 86 | |
efed01e1 LOK |
87 | /* try to ping the adapter */ |
88 | bool bPinged(false); | |
89 | unsigned iPingTry(0); | |
90 | while (iNow < iTarget && (bPinged = PingAdapter()) == false) | |
13fd6a66 | 91 | { |
efed01e1 | 92 | CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter did not respond correctly to a ping (try %d)", ++iPingTry); |
befa3a23 | 93 | CEvent::Sleep(500); |
efed01e1 | 94 | iNow = GetTimeMs(); |
13fd6a66 LOK |
95 | } |
96 | ||
efed01e1 LOK |
97 | /* try to read the firmware version */ |
98 | m_iFirmwareVersion = CEC_FW_VERSION_UNKNOWN; | |
99 | unsigned iFwVersionTry(0); | |
9aae458a | 100 | while (bPinged && iNow < iTarget && (m_iFirmwareVersion = GetFirmwareVersion()) == CEC_FW_VERSION_UNKNOWN && iFwVersionTry < 3) |
13fd6a66 | 101 | { |
9aae458a | 102 | CLibCEC::AddLog(CEC_LOG_WARNING, "the adapter did not respond with a correct firmware version (try %d)", ++iFwVersionTry); |
befa3a23 | 103 | CEvent::Sleep(500); |
efed01e1 | 104 | iNow = GetTimeMs(); |
13fd6a66 | 105 | } |
a8f0bd18 | 106 | |
9aae458a LOK |
107 | if (m_iFirmwareVersion == CEC_FW_VERSION_UNKNOWN) |
108 | { | |
109 | CLibCEC::AddLog(CEC_LOG_DEBUG, "defaulting to firmware version 1"); | |
110 | m_iFirmwareVersion = 1; | |
111 | } | |
112 | ||
efed01e1 | 113 | if (m_iFirmwareVersion >= 2) |
7b494bea | 114 | { |
efed01e1 LOK |
115 | /* try to set controlled mode */ |
116 | unsigned iControlledTry(0); | |
117 | bool bControlled(false); | |
118 | while (iNow < iTarget && (bControlled = SetControlledMode(true)) == false) | |
7b494bea | 119 | { |
efed01e1 | 120 | CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter did not respond correctly to setting controlled mode (try %d)", ++iControlledTry); |
befa3a23 | 121 | CEvent::Sleep(500); |
7b494bea LOK |
122 | iNow = GetTimeMs(); |
123 | } | |
efed01e1 | 124 | bReturn = bControlled; |
7b494bea | 125 | } |
efed01e1 LOK |
126 | else |
127 | bReturn = true; | |
7b494bea | 128 | |
cccd2724 LOK |
129 | { |
130 | CLockObject lock(m_mutex); | |
131 | m_bInitialised = bReturn; | |
132 | } | |
133 | ||
efed01e1 LOK |
134 | return bReturn; |
135 | } | |
a8f0bd18 | 136 | |
a2198e5e | 137 | bool CUSBCECAdapterCommunication::Open(IAdapterCommunicationCallback *cb, uint32_t iTimeoutMs /* = 10000 */, bool bSkipChecks /* = false */) |
efed01e1 LOK |
138 | { |
139 | uint64_t iNow = GetTimeMs(); | |
140 | uint64_t iTimeout = iNow + iTimeoutMs; | |
a8f0bd18 | 141 | |
2c780401 | 142 | { |
efed01e1 LOK |
143 | CLockObject lock(m_mutex); |
144 | ||
145 | if (!m_port) | |
146 | { | |
147 | CLibCEC::AddLog(CEC_LOG_ERROR, "port is NULL"); | |
148 | return false; | |
149 | } | |
150 | ||
151 | if (IsOpen()) | |
152 | { | |
153 | CLibCEC::AddLog(CEC_LOG_ERROR, "port is already open"); | |
154 | return true; | |
155 | } | |
156 | ||
157 | m_callback = cb; | |
158 | CStdString strError; | |
159 | bool bConnected(false); | |
160 | while (!bConnected && iNow < iTimeout) | |
161 | { | |
162 | if ((bConnected = m_port->Open(iTimeout)) == false) | |
163 | { | |
164 | strError.Format("error opening serial port '%s': %s", m_port->GetName().c_str(), m_port->GetError().c_str()); | |
165 | Sleep(250); | |
166 | iNow = GetTimeMs(); | |
167 | } | |
168 | } | |
169 | ||
170 | if (!bConnected) | |
171 | { | |
172 | CLibCEC::AddLog(CEC_LOG_ERROR, strError); | |
173 | return false; | |
174 | } | |
175 | ||
176 | CLibCEC::AddLog(CEC_LOG_DEBUG, "connection opened, clearing any previous input and waiting for active transmissions to end before starting"); | |
177 | ||
a2198e5e | 178 | if (!bSkipChecks) |
efed01e1 | 179 | { |
a2198e5e LOK |
180 | //clear any input bytes |
181 | uint8_t buff[1024]; | |
182 | while (m_port->Read(buff, 1024, 100) > 0) | |
183 | { | |
184 | CLibCEC::AddLog(CEC_LOG_DEBUG, "data received, clearing it"); | |
185 | Sleep(250); | |
186 | } | |
efed01e1 | 187 | } |
2c780401 | 188 | } |
a8f0bd18 | 189 | |
befa3a23 | 190 | if (!bSkipChecks && !CheckAdapter()) |
a8f0bd18 | 191 | { |
befa3a23 LOK |
192 | CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter failed to pass basic checks"); |
193 | return false; | |
194 | } | |
195 | else | |
196 | { | |
197 | if (CreateThread()) | |
efed01e1 | 198 | { |
befa3a23 LOK |
199 | CLibCEC::AddLog(CEC_LOG_DEBUG, "communication thread started"); |
200 | return true; | |
efed01e1 LOK |
201 | } |
202 | else | |
203 | { | |
befa3a23 | 204 | CLibCEC::AddLog(CEC_LOG_ERROR, "could not create a communication thread"); |
efed01e1 | 205 | } |
a8f0bd18 LOK |
206 | } |
207 | ||
208 | return false; | |
209 | } | |
210 | ||
7bb4ed43 | 211 | void CUSBCECAdapterCommunication::Close(void) |
a8f0bd18 | 212 | { |
b9eea66d | 213 | StopThread(); |
a8f0bd18 LOK |
214 | } |
215 | ||
7bb4ed43 | 216 | void *CUSBCECAdapterCommunication::Process(void) |
a8f0bd18 | 217 | { |
83554890 LOK |
218 | m_messageProcessor = new CUSBCECAdapterProcessor(m_callback); |
219 | m_messageProcessor->CreateThread(); | |
220 | ||
b1f94db1 | 221 | cec_command command; |
32553784 | 222 | command.Clear(); |
efed01e1 | 223 | bool bCommandReceived(false); |
13fd6a66 | 224 | while (!IsStopped()) |
a8f0bd18 | 225 | { |
efed01e1 LOK |
226 | { |
227 | CLockObject lock(m_mutex); | |
228 | ReadFromDevice(50); | |
cccd2724 | 229 | bCommandReceived = m_callback && Read(command, 0) && m_bInitialised; |
efed01e1 | 230 | } |
b1f94db1 LOK |
231 | |
232 | /* push the next command to the callback method if there is one */ | |
efed01e1 | 233 | if (!IsStopped() && bCommandReceived) |
83554890 | 234 | m_messageProcessor->AddCommand(command); |
b1f94db1 | 235 | |
efed01e1 LOK |
236 | if (!IsStopped()) |
237 | { | |
238 | Sleep(5); | |
239 | WriteNextCommand(); | |
240 | } | |
a8f0bd18 LOK |
241 | } |
242 | ||
83554890 LOK |
243 | /* stop the message processor */ |
244 | m_messageProcessor->StopThread(); | |
245 | delete m_messageProcessor; | |
246 | ||
f9e01dac | 247 | /* notify all threads that are waiting on messages to be sent */ |
ef7696f5 | 248 | CCECAdapterMessage *msg(NULL); |
f9e01dac | 249 | while (m_outBuffer.Pop(msg)) |
960f33c6 | 250 | msg->event.Broadcast(); |
a0878ee3 | 251 | |
f9e01dac LOK |
252 | /* set the ackmask to 0 before closing the connection */ |
253 | SetAckMaskInternal(0, true); | |
254 | ||
3c30c490 LOK |
255 | if (m_iFirmwareVersion >= 2) |
256 | SetControlledMode(false); | |
257 | ||
9f9c8c82 LOK |
258 | if (m_port) |
259 | { | |
260 | delete m_port; | |
261 | m_port = NULL; | |
262 | } | |
263 | ||
a8f0bd18 LOK |
264 | return NULL; |
265 | } | |
266 | ||
7bb4ed43 LOK |
267 | cec_adapter_message_state CUSBCECAdapterCommunication::Write(const cec_command &data, uint8_t iMaxTries, uint8_t iLineTimeout /* = 3 */, uint8_t iRetryLineTimeout /* = 3 */) |
268 | { | |
269 | cec_adapter_message_state retVal(ADAPTER_MESSAGE_STATE_UNKNOWN); | |
9f68cc28 LOK |
270 | if (!IsRunning()) |
271 | return retVal; | |
7bb4ed43 LOK |
272 | |
273 | CCECAdapterMessage *output = new CCECAdapterMessage(data); | |
274 | ||
275 | /* set the number of retries */ | |
276 | if (data.opcode == CEC_OPCODE_NONE) //TODO | |
277 | output->maxTries = 1; | |
278 | else if (data.initiator != CECDEVICE_BROADCAST) | |
279 | output->maxTries = iMaxTries; | |
280 | ||
281 | output->lineTimeout = iLineTimeout; | |
282 | output->retryTimeout = iRetryLineTimeout; | |
283 | output->tries = 0; | |
284 | ||
285 | bool bRetry(true); | |
286 | while (bRetry && ++output->tries < output->maxTries) | |
287 | { | |
288 | bRetry = (!Write(output) || output->NeedsRetry()) && output->transmit_timeout > 0; | |
289 | if (bRetry) | |
290 | Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT); | |
291 | } | |
292 | retVal = output->state; | |
293 | ||
294 | delete output; | |
295 | return retVal; | |
296 | } | |
297 | ||
298 | bool CUSBCECAdapterCommunication::Write(CCECAdapterMessage *data) | |
3c53ac93 | 299 | { |
5dcf9f25 | 300 | data->state = ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT; |
3c53ac93 | 301 | m_outBuffer.Push(data); |
f9e01dac | 302 | data->event.Wait(5000); |
5dcf9f25 | 303 | |
b1f94db1 LOK |
304 | if ((data->expectControllerAck && data->state != ADAPTER_MESSAGE_STATE_SENT_ACKED) || |
305 | (!data->expectControllerAck && data->state != ADAPTER_MESSAGE_STATE_SENT)) | |
5dcf9f25 | 306 | { |
b1f94db1 LOK |
307 | CLibCEC::AddLog(CEC_LOG_DEBUG, "command was not %s", data->state == ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED ? "acked" : "sent"); |
308 | return false; | |
5dcf9f25 LOK |
309 | } |
310 | ||
b1f94db1 | 311 | return true; |
a8f0bd18 LOK |
312 | } |
313 | ||
7bb4ed43 | 314 | bool CUSBCECAdapterCommunication::Read(cec_command &command, uint32_t iTimeout) |
a8f0bd18 | 315 | { |
9f68cc28 LOK |
316 | if (!IsRunning()) |
317 | return false; | |
318 | ||
7bb4ed43 LOK |
319 | CCECAdapterMessage msg; |
320 | if (Read(msg, iTimeout)) | |
a8f0bd18 | 321 | { |
7bb4ed43 | 322 | if (ParseMessage(msg)) |
a8f0bd18 | 323 | { |
7bb4ed43 LOK |
324 | command = m_currentframe; |
325 | m_currentframe.Clear(); | |
326 | return true; | |
a8f0bd18 | 327 | } |
7bb4ed43 LOK |
328 | } |
329 | return false; | |
330 | } | |
a8f0bd18 | 331 | |
7bb4ed43 LOK |
332 | bool CUSBCECAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout) |
333 | { | |
334 | CLockObject lock(m_mutex); | |
a8f0bd18 | 335 | |
7bb4ed43 LOK |
336 | msg.Clear(); |
337 | CCECAdapterMessage *buf(NULL); | |
a8f0bd18 | 338 | |
7bb4ed43 LOK |
339 | if (!m_inBuffer.Pop(buf)) |
340 | { | |
960f33c6 | 341 | if (iTimeout == 0 || !m_rcvCondition.Wait(m_mutex, m_bHasData, iTimeout)) |
7bb4ed43 LOK |
342 | return false; |
343 | m_inBuffer.Pop(buf); | |
120d4ca8 | 344 | m_bHasData = !m_inBuffer.IsEmpty(); |
7bb4ed43 | 345 | } |
0e31a62c | 346 | |
7bb4ed43 LOK |
347 | if (buf) |
348 | { | |
349 | msg.packet = buf->packet; | |
66e5bd72 | 350 | msg.state = ADAPTER_MESSAGE_STATE_INCOMING; |
7bb4ed43 LOK |
351 | delete buf; |
352 | return true; | |
353 | } | |
354 | return false; | |
a8f0bd18 LOK |
355 | } |
356 | ||
7bb4ed43 | 357 | CStdString CUSBCECAdapterCommunication::GetError(void) const |
a8f0bd18 | 358 | { |
ba65909d LOK |
359 | CStdString strError; |
360 | strError = m_port->GetError(); | |
361 | return strError; | |
a8f0bd18 | 362 | } |
2abe74eb | 363 | |
7bb4ed43 | 364 | bool CUSBCECAdapterCommunication::StartBootloader(void) |
2abe74eb | 365 | { |
28352a04 | 366 | bool bReturn(false); |
2abe74eb | 367 | if (!IsRunning()) |
28352a04 | 368 | return bReturn; |
2abe74eb | 369 | |
5477a250 | 370 | CLibCEC::AddLog(CEC_LOG_DEBUG, "starting the bootloader"); |
28352a04 | 371 | CCECAdapterMessage *output = new CCECAdapterMessage; |
25701fa6 | 372 | |
ef7696f5 LOK |
373 | output->PushBack(MSGSTART); |
374 | output->PushEscaped(MSGCODE_START_BOOTLOADER); | |
375 | output->PushBack(MSGEND); | |
5dcf9f25 | 376 | output->isTransmission = false; |
71c4a2f5 | 377 | output->expectControllerAck = false; |
2abe74eb | 378 | |
5dcf9f25 | 379 | if ((bReturn = Write(output)) == false) |
5477a250 | 380 | CLibCEC::AddLog(CEC_LOG_ERROR, "could not start the bootloader"); |
28352a04 LOK |
381 | delete output; |
382 | ||
383 | return bReturn; | |
2abe74eb LOK |
384 | } |
385 | ||
7bb4ed43 | 386 | bool CUSBCECAdapterCommunication::PingAdapter(void) |
2abe74eb | 387 | { |
befa3a23 | 388 | CLockObject lock(m_mutex); |
5477a250 | 389 | CLibCEC::AddLog(CEC_LOG_DEBUG, "sending ping"); |
befa3a23 | 390 | |
28352a04 | 391 | CCECAdapterMessage *output = new CCECAdapterMessage; |
25701fa6 | 392 | |
ef7696f5 LOK |
393 | output->PushBack(MSGSTART); |
394 | output->PushEscaped(MSGCODE_PING); | |
395 | output->PushBack(MSGEND); | |
5dcf9f25 | 396 | output->isTransmission = false; |
2abe74eb | 397 | |
befa3a23 LOK |
398 | SendMessageToAdapter(output); |
399 | bool bWriteOk = output->state == ADAPTER_MESSAGE_STATE_SENT_ACKED; | |
28352a04 | 400 | delete output; |
befa3a23 LOK |
401 | if (!bWriteOk) |
402 | { | |
403 | CLibCEC::AddLog(CEC_LOG_ERROR, "could not ping the adapter"); | |
404 | return false; | |
405 | } | |
28352a04 | 406 | |
befa3a23 | 407 | return true; |
2abe74eb | 408 | } |
13fd6a66 | 409 | |
7bb4ed43 LOK |
410 | bool CUSBCECAdapterCommunication::ParseMessage(const CCECAdapterMessage &msg) |
411 | { | |
412 | bool bEom(false); | |
413 | bool bIsError(msg.IsError()); | |
414 | ||
415 | if (msg.IsEmpty()) | |
416 | return bEom; | |
417 | ||
24dd566c | 418 | CLockObject adapterLock(m_mutex); |
7bb4ed43 LOK |
419 | switch(msg.Message()) |
420 | { | |
421 | case MSGCODE_FRAME_START: | |
422 | { | |
423 | m_currentframe.Clear(); | |
424 | if (msg.Size() >= 2) | |
425 | { | |
426 | m_currentframe.initiator = msg.Initiator(); | |
427 | m_currentframe.destination = msg.Destination(); | |
428 | m_currentframe.ack = msg.IsACK(); | |
429 | m_currentframe.eom = msg.IsEOM(); | |
430 | } | |
431 | if (m_currentframe.ack == 0x1) | |
432 | { | |
433 | m_lastInitiator = m_currentframe.initiator; | |
434 | m_processor->HandlePoll(m_currentframe.initiator, m_currentframe.destination); | |
435 | } | |
436 | } | |
437 | break; | |
438 | case MSGCODE_RECEIVE_FAILED: | |
439 | { | |
440 | m_currentframe.Clear(); | |
441 | if (m_lastInitiator != CECDEVICE_UNKNOWN) | |
442 | bIsError = m_processor->HandleReceiveFailed(m_lastInitiator); | |
443 | } | |
444 | break; | |
445 | case MSGCODE_FRAME_DATA: | |
446 | { | |
447 | if (msg.Size() >= 2) | |
448 | { | |
449 | m_currentframe.PushBack(msg[1]); | |
450 | m_currentframe.eom = msg.IsEOM(); | |
451 | } | |
7bb4ed43 LOK |
452 | } |
453 | break; | |
454 | default: | |
455 | break; | |
456 | } | |
457 | ||
458 | CLibCEC::AddLog(bIsError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString()); | |
d507711c | 459 | return msg.IsEOM(); |
7bb4ed43 LOK |
460 | } |
461 | ||
462 | uint16_t CUSBCECAdapterCommunication::GetFirmwareVersion(void) | |
1fc16cfd LOK |
463 | { |
464 | uint16_t iReturn(m_iFirmwareVersion); | |
1fc16cfd LOK |
465 | |
466 | if (iReturn == CEC_FW_VERSION_UNKNOWN) | |
467 | { | |
006b76b9 | 468 | CLockObject lock(m_mutex); |
5477a250 | 469 | CLibCEC::AddLog(CEC_LOG_DEBUG, "requesting the firmware version"); |
1fc16cfd LOK |
470 | CCECAdapterMessage *output = new CCECAdapterMessage; |
471 | ||
472 | output->PushBack(MSGSTART); | |
473 | output->PushEscaped(MSGCODE_FIRMWARE_VERSION); | |
474 | output->PushBack(MSGEND); | |
475 | output->isTransmission = false; | |
476 | output->expectControllerAck = false; | |
477 | ||
efed01e1 LOK |
478 | SendMessageToAdapter(output); |
479 | bool bWriteOk = output->state == ADAPTER_MESSAGE_STATE_SENT; | |
1fc16cfd | 480 | delete output; |
b1f94db1 LOK |
481 | if (!bWriteOk) |
482 | { | |
483 | CLibCEC::AddLog(CEC_LOG_ERROR, "could not request the firmware version"); | |
efed01e1 | 484 | return iReturn; |
b1f94db1 | 485 | } |
efed01e1 LOK |
486 | |
487 | Sleep(250); // TODO ReadFromDevice() isn't waiting for the timeout to pass on win32 | |
488 | ReadFromDevice(CEC_DEFAULT_TRANSMIT_WAIT, 5 /* start + msgcode + 2 bytes for fw version + end */); | |
489 | CCECAdapterMessage input; | |
490 | if (Read(input, 0)) | |
1fc16cfd | 491 | { |
efed01e1 LOK |
492 | if (input.Message() != MSGCODE_FIRMWARE_VERSION || input.Size() != 3) |
493 | CLibCEC::AddLog(CEC_LOG_ERROR, "invalid firmware version (size = %d, message = %d)", input.Size(), input.Message()); | |
b1f94db1 LOK |
494 | else |
495 | { | |
496 | m_iFirmwareVersion = (input[1] << 8 | input[2]); | |
497 | iReturn = m_iFirmwareVersion; | |
498 | } | |
1fc16cfd | 499 | } |
efed01e1 LOK |
500 | else |
501 | { | |
502 | CLibCEC::AddLog(CEC_LOG_ERROR, "no firmware version received"); | |
503 | } | |
1fc16cfd LOK |
504 | } |
505 | ||
506 | return iReturn; | |
507 | } | |
508 | ||
7bb4ed43 | 509 | bool CUSBCECAdapterCommunication::SetLineTimeout(uint8_t iTimeout) |
a171d2fd | 510 | { |
7bb4ed43 LOK |
511 | m_iLineTimeout = iTimeout; |
512 | return true; | |
513 | //TODO | |
514 | // bool bReturn(m_iLineTimeout != iTimeout); | |
515 | // | |
516 | // if (!bReturn) | |
517 | // { | |
518 | // CCECAdapterMessage *output = new CCECAdapterMessage; | |
519 | // | |
520 | // output->PushBack(MSGSTART); | |
521 | // output->PushEscaped(MSGCODE_TRANSMIT_IDLETIME); | |
522 | // output->PushEscaped(iTimeout); | |
523 | // output->PushBack(MSGEND); | |
524 | // output->isTransmission = false; | |
525 | // | |
526 | // if ((bReturn = Write(output)) == false) | |
527 | // CLibCEC::AddLog(CEC_LOG_ERROR, "could not set the idletime"); | |
528 | // delete output; | |
529 | // } | |
530 | // | |
531 | // return bReturn; | |
a171d2fd LOK |
532 | } |
533 | ||
7bb4ed43 | 534 | bool CUSBCECAdapterCommunication::SetAckMask(uint16_t iMask) |
f9e01dac LOK |
535 | { |
536 | return SetAckMaskInternal(iMask, false); | |
537 | } | |
538 | ||
539 | bool CUSBCECAdapterCommunication::SetAckMaskInternal(uint16_t iMask, bool bWriteDirectly /* = false */) | |
5dcf9f25 LOK |
540 | { |
541 | bool bReturn(false); | |
bca69ca1 | 542 | CLibCEC::AddLog(CEC_LOG_DEBUG, "setting ackmask to %2x", iMask); |
5dcf9f25 LOK |
543 | |
544 | CCECAdapterMessage *output = new CCECAdapterMessage; | |
545 | ||
546 | output->PushBack(MSGSTART); | |
547 | output->PushEscaped(MSGCODE_SET_ACK_MASK); | |
548 | output->PushEscaped(iMask >> 8); | |
549 | output->PushEscaped((uint8_t)iMask); | |
550 | output->PushBack(MSGEND); | |
551 | output->isTransmission = false; | |
552 | ||
f9e01dac LOK |
553 | if (bWriteDirectly) |
554 | SendMessageToAdapter(output); | |
555 | else if ((bReturn = Write(output)) == false) | |
5477a250 | 556 | CLibCEC::AddLog(CEC_LOG_ERROR, "could not set the ackmask"); |
5dcf9f25 LOK |
557 | delete output; |
558 | ||
559 | return bReturn; | |
560 | } | |
561 | ||
b057edad BL |
562 | |
563 | bool CUSBCECAdapterCommunication::SetControlledMode(bool controlled) | |
564 | { | |
befa3a23 | 565 | CLockObject lock(m_mutex); |
bca69ca1 | 566 | CLibCEC::AddLog(CEC_LOG_DEBUG, "turning controlled mode %s", controlled ? "on" : "off"); |
b057edad BL |
567 | |
568 | CCECAdapterMessage *output = new CCECAdapterMessage; | |
569 | ||
570 | output->PushBack(MSGSTART); | |
571 | output->PushEscaped(MSGCODE_SET_CONTROLLED); | |
572 | output->PushEscaped(controlled); | |
573 | output->PushBack(MSGEND); | |
574 | output->isTransmission = false; | |
575 | ||
befa3a23 | 576 | SendMessageToAdapter(output); |
ccc06150 | 577 | bool bWriteOk = output->state == ADAPTER_MESSAGE_STATE_SENT_ACKED; |
b057edad | 578 | delete output; |
befa3a23 LOK |
579 | if (!bWriteOk) |
580 | { | |
581 | CLibCEC::AddLog(CEC_LOG_ERROR, "could not set controlled mode"); | |
582 | return false; | |
583 | } | |
b057edad | 584 | |
befa3a23 | 585 | return true; |
b057edad BL |
586 | } |
587 | ||
7bb4ed43 | 588 | bool CUSBCECAdapterCommunication::IsOpen(void) |
13fd6a66 | 589 | { |
b9eea66d | 590 | return !IsStopped() && m_port->IsOpen() && IsRunning(); |
13fd6a66 | 591 | } |
ef7696f5 | 592 | |
7bb4ed43 | 593 | bool CUSBCECAdapterCommunication::WaitForAck(CCECAdapterMessage &message) |
6729ac71 LOK |
594 | { |
595 | bool bError(false); | |
596 | bool bTransmitSucceeded(false); | |
b2f0b1ab | 597 | uint8_t iPacketsLeft(message.Size() / 4); |
6729ac71 LOK |
598 | |
599 | int64_t iNow = GetTimeMs(); | |
b1f94db1 | 600 | int64_t iTargetTime = iNow + (message.transmit_timeout <= 5 ? CEC_DEFAULT_TRANSMIT_WAIT : message.transmit_timeout); |
6729ac71 | 601 | |
b1f94db1 | 602 | while (!bTransmitSucceeded && !bError && iNow < iTargetTime) |
6729ac71 | 603 | { |
b1f94db1 | 604 | ReadFromDevice(50); |
6729ac71 | 605 | CCECAdapterMessage msg; |
b1f94db1 | 606 | if (!Read(msg, 0)) |
6729ac71 LOK |
607 | { |
608 | iNow = GetTimeMs(); | |
609 | continue; | |
610 | } | |
611 | ||
612 | if (msg.Message() == MSGCODE_FRAME_START && msg.IsACK()) | |
613 | { | |
614 | m_processor->HandlePoll(msg.Initiator(), msg.Destination()); | |
7bb4ed43 | 615 | m_lastInitiator = msg.Initiator(); |
6729ac71 LOK |
616 | iNow = GetTimeMs(); |
617 | continue; | |
618 | } | |
619 | ||
620 | if (msg.Message() == MSGCODE_RECEIVE_FAILED && | |
7bb4ed43 LOK |
621 | m_lastInitiator != CECDEVICE_UNKNOWN && |
622 | m_processor->HandleReceiveFailed(m_lastInitiator)) | |
6729ac71 LOK |
623 | { |
624 | iNow = GetTimeMs(); | |
625 | continue; | |
626 | } | |
627 | ||
628 | bError = msg.IsError(); | |
629 | if (bError) | |
630 | { | |
b2f0b1ab | 631 | message.reply = msg.Message(); |
5477a250 | 632 | CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString()); |
6729ac71 LOK |
633 | } |
634 | else | |
635 | { | |
636 | switch(msg.Message()) | |
637 | { | |
638 | case MSGCODE_COMMAND_ACCEPTED: | |
6729ac71 LOK |
639 | if (iPacketsLeft > 0) |
640 | iPacketsLeft--; | |
b2f0b1ab | 641 | if (!message.isTransmission && iPacketsLeft == 0) |
5dcf9f25 | 642 | bTransmitSucceeded = true; |
befa3a23 | 643 | CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - waiting for %d more", msg.ToString().c_str(), iPacketsLeft); |
6729ac71 LOK |
644 | break; |
645 | case MSGCODE_TRANSMIT_SUCCEEDED: | |
5477a250 | 646 | CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString()); |
6729ac71 LOK |
647 | bTransmitSucceeded = (iPacketsLeft == 0); |
648 | bError = !bTransmitSucceeded; | |
b2f0b1ab | 649 | message.reply = MSGCODE_TRANSMIT_SUCCEEDED; |
6729ac71 LOK |
650 | break; |
651 | default: | |
652 | // ignore other data while waiting | |
653 | break; | |
654 | } | |
655 | ||
656 | iNow = GetTimeMs(); | |
657 | } | |
658 | } | |
659 | ||
b1f94db1 LOK |
660 | message.state = bTransmitSucceeded && !bError ? |
661 | ADAPTER_MESSAGE_STATE_SENT_ACKED : | |
662 | ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED; | |
663 | ||
6729ac71 LOK |
664 | return bTransmitSucceeded && !bError; |
665 | } | |
666 | ||
7bb4ed43 | 667 | void CUSBCECAdapterCommunication::AddData(uint8_t *data, size_t iLen) |
ef7696f5 LOK |
668 | { |
669 | CLockObject lock(m_mutex); | |
99666519 | 670 | for (size_t iPtr = 0; iPtr < iLen; iPtr++) |
7bb4ed43 LOK |
671 | { |
672 | if (!m_bGotStart) | |
673 | { | |
674 | if (data[iPtr] == MSGSTART) | |
675 | m_bGotStart = true; | |
676 | } | |
677 | else if (data[iPtr] == MSGSTART) //we found a msgstart before msgend, this is not right, remove | |
678 | { | |
679 | if (m_currentAdapterMessage.Size() > 0) | |
680 | CLibCEC::AddLog(CEC_LOG_WARNING, "received MSGSTART before MSGEND, removing previous buffer contents"); | |
681 | m_currentAdapterMessage.Clear(); | |
682 | m_bGotStart = true; | |
683 | } | |
684 | else if (data[iPtr] == MSGEND) | |
685 | { | |
686 | CCECAdapterMessage *newMessage = new CCECAdapterMessage; | |
687 | newMessage->packet = m_currentAdapterMessage.packet; | |
688 | m_inBuffer.Push(newMessage); | |
689 | m_currentAdapterMessage.Clear(); | |
690 | m_bGotStart = false; | |
691 | m_bNextIsEscaped = false; | |
960f33c6 | 692 | m_bHasData = true; |
24dd566c | 693 | m_rcvCondition.Broadcast(); |
7bb4ed43 LOK |
694 | } |
695 | else if (m_bNextIsEscaped) | |
696 | { | |
697 | m_currentAdapterMessage.PushBack(data[iPtr] + (uint8_t)ESCOFFSET); | |
698 | m_bNextIsEscaped = false; | |
699 | } | |
700 | else if (data[iPtr] == MSGESC) | |
701 | { | |
702 | m_bNextIsEscaped = true; | |
703 | } | |
704 | else | |
705 | { | |
706 | m_currentAdapterMessage.PushBack(data[iPtr]); | |
707 | } | |
708 | } | |
ef7696f5 LOK |
709 | } |
710 | ||
b1f94db1 | 711 | bool CUSBCECAdapterCommunication::ReadFromDevice(uint32_t iTimeout, size_t iSize /* = 256 */) |
ef7696f5 | 712 | { |
99666519 LOK |
713 | ssize_t iBytesRead; |
714 | uint8_t buff[256]; | |
ef7696f5 LOK |
715 | if (!m_port) |
716 | return false; | |
b1f94db1 LOK |
717 | if (iSize > 256) |
718 | iSize = 256; | |
ef7696f5 | 719 | |
1fc16cfd | 720 | CLockObject lock(m_mutex); |
b1f94db1 | 721 | iBytesRead = m_port->Read(buff, sizeof(uint8_t) * iSize, iTimeout); |
ef7696f5 LOK |
722 | if (iBytesRead < 0 || iBytesRead > 256) |
723 | { | |
99666519 | 724 | CLibCEC::AddLog(CEC_LOG_ERROR, "error reading from serial port: %s", m_port->GetError().c_str()); |
60383b11 | 725 | StopThread(false); |
ef7696f5 LOK |
726 | return false; |
727 | } | |
728 | else if (iBytesRead > 0) | |
99666519 LOK |
729 | { |
730 | AddData(buff, iBytesRead); | |
731 | } | |
ef7696f5 LOK |
732 | |
733 | return iBytesRead > 0; | |
734 | } | |
735 | ||
7bb4ed43 | 736 | void CUSBCECAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage *msg) |
ef7696f5 | 737 | { |
1fc16cfd | 738 | CLockObject adapterLock(m_mutex); |
f9e01dac LOK |
739 | if (!m_port->IsOpen()) |
740 | { | |
741 | CLibCEC::AddLog(CEC_LOG_ERROR, "error writing to serial port: the connection is closed"); | |
742 | msg->state = ADAPTER_MESSAGE_STATE_ERROR; | |
743 | return; | |
744 | } | |
745 | ||
7bb4ed43 LOK |
746 | if (msg->tries == 1) |
747 | SetLineTimeout(msg->lineTimeout); | |
748 | else | |
749 | SetLineTimeout(msg->retryTimeout); | |
750 | ||
99666519 | 751 | if (m_port->Write(msg->packet.data, msg->Size()) != (ssize_t) msg->Size()) |
ef7696f5 | 752 | { |
b74fd339 | 753 | CLibCEC::AddLog(CEC_LOG_ERROR, "error writing to serial port: %s", m_port->GetError().c_str()); |
ef7696f5 LOK |
754 | msg->state = ADAPTER_MESSAGE_STATE_ERROR; |
755 | } | |
756 | else | |
757 | { | |
5477a250 | 758 | CLibCEC::AddLog(CEC_LOG_DEBUG, "command sent"); |
ef7696f5 | 759 | msg->state = ADAPTER_MESSAGE_STATE_SENT; |
b1f94db1 LOK |
760 | |
761 | if (msg->expectControllerAck) | |
762 | { | |
763 | if (!WaitForAck(*msg)) | |
764 | CLibCEC::AddLog(CEC_LOG_DEBUG, "did not receive ack"); | |
765 | } | |
ef7696f5 | 766 | } |
960f33c6 | 767 | msg->event.Signal(); |
ef7696f5 LOK |
768 | } |
769 | ||
7bb4ed43 | 770 | void CUSBCECAdapterCommunication::WriteNextCommand(void) |
ef7696f5 LOK |
771 | { |
772 | CCECAdapterMessage *msg(NULL); | |
773 | if (m_outBuffer.Pop(msg)) | |
774 | SendMessageToAdapter(msg); | |
775 | } | |
cba904a6 LOK |
776 | |
777 | CStdString CUSBCECAdapterCommunication::GetPortName(void) | |
778 | { | |
779 | CStdString strName; | |
780 | strName = m_port->GetName(); | |
781 | return strName; | |
782 | } |