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