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