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