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