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