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 | |
ae54110f LOK |
43 | #define CEC_ADAPTER_PING_TIMEOUT 15000 |
44 | ||
83554890 LOK |
45 | void *CUSBCECAdapterProcessor::Process(void) |
46 | { | |
47 | cec_command command; | |
48 | while (!IsStopped()) | |
49 | { | |
50 | if (m_inBuffer.Pop(command)) | |
51 | m_callback->OnCommandReceived(command); | |
52 | Sleep(5); | |
53 | } | |
54 | ||
55 | return NULL; | |
56 | } | |
57 | ||
58 | void CUSBCECAdapterProcessor::AddCommand(cec_command command) | |
59 | { | |
60 | m_inBuffer.Push(command); | |
61 | } | |
62 | ||
7bb4ed43 | 63 | CUSBCECAdapterCommunication::CUSBCECAdapterCommunication(CCECProcessor *processor, const char *strPort, uint16_t iBaudRate /* = 38400 */) : |
12027dbe | 64 | m_port(NULL), |
a171d2fd | 65 | m_processor(processor), |
960f33c6 | 66 | m_bHasData(false), |
1fc16cfd | 67 | m_iLineTimeout(0), |
7bb4ed43 LOK |
68 | m_iFirmwareVersion(CEC_FW_VERSION_UNKNOWN), |
69 | m_lastInitiator(CECDEVICE_UNKNOWN), | |
70 | m_bNextIsEscaped(false), | |
83554890 | 71 | m_bGotStart(false), |
cccd2724 LOK |
72 | m_messageProcessor(NULL), |
73 | m_bInitialised(false) | |
a8f0bd18 | 74 | { |
089f0e9d | 75 | m_port = new CSerialPort(strPort, iBaudRate); |
a8f0bd18 LOK |
76 | } |
77 | ||
7bb4ed43 | 78 | CUSBCECAdapterCommunication::~CUSBCECAdapterCommunication(void) |
a8f0bd18 | 79 | { |
12027dbe | 80 | Close(); |
a8f0bd18 LOK |
81 | } |
82 | ||
efed01e1 | 83 | bool CUSBCECAdapterCommunication::CheckAdapter(uint32_t iTimeoutMs /* = 10000 */) |
a8f0bd18 | 84 | { |
efed01e1 | 85 | bool bReturn(false); |
7b494bea | 86 | uint64_t iNow = GetTimeMs(); |
efed01e1 | 87 | uint64_t iTarget = iTimeoutMs > 0 ? iNow + iTimeoutMs : iNow + CEC_DEFAULT_TRANSMIT_WAIT; |
7b494bea | 88 | |
efed01e1 LOK |
89 | /* try to ping the adapter */ |
90 | bool bPinged(false); | |
91 | unsigned iPingTry(0); | |
92 | while (iNow < iTarget && (bPinged = PingAdapter()) == false) | |
13fd6a66 | 93 | { |
efed01e1 | 94 | CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter did not respond correctly to a ping (try %d)", ++iPingTry); |
befa3a23 | 95 | CEvent::Sleep(500); |
efed01e1 | 96 | iNow = GetTimeMs(); |
13fd6a66 LOK |
97 | } |
98 | ||
efed01e1 LOK |
99 | /* try to read the firmware version */ |
100 | m_iFirmwareVersion = CEC_FW_VERSION_UNKNOWN; | |
101 | unsigned iFwVersionTry(0); | |
9aae458a | 102 | while (bPinged && iNow < iTarget && (m_iFirmwareVersion = GetFirmwareVersion()) == CEC_FW_VERSION_UNKNOWN && iFwVersionTry < 3) |
13fd6a66 | 103 | { |
9aae458a | 104 | CLibCEC::AddLog(CEC_LOG_WARNING, "the adapter did not respond with a correct firmware version (try %d)", ++iFwVersionTry); |
befa3a23 | 105 | CEvent::Sleep(500); |
efed01e1 | 106 | iNow = GetTimeMs(); |
13fd6a66 | 107 | } |
a8f0bd18 | 108 | |
9aae458a LOK |
109 | if (m_iFirmwareVersion == CEC_FW_VERSION_UNKNOWN) |
110 | { | |
111 | CLibCEC::AddLog(CEC_LOG_DEBUG, "defaulting to firmware version 1"); | |
112 | m_iFirmwareVersion = 1; | |
113 | } | |
114 | ||
efed01e1 | 115 | if (m_iFirmwareVersion >= 2) |
7b494bea | 116 | { |
efed01e1 LOK |
117 | /* try to set controlled mode */ |
118 | unsigned iControlledTry(0); | |
119 | bool bControlled(false); | |
120 | while (iNow < iTarget && (bControlled = SetControlledMode(true)) == false) | |
7b494bea | 121 | { |
efed01e1 | 122 | CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter did not respond correctly to setting controlled mode (try %d)", ++iControlledTry); |
befa3a23 | 123 | CEvent::Sleep(500); |
7b494bea LOK |
124 | iNow = GetTimeMs(); |
125 | } | |
efed01e1 | 126 | bReturn = bControlled; |
7b494bea | 127 | } |
efed01e1 LOK |
128 | else |
129 | bReturn = true; | |
7b494bea | 130 | |
cccd2724 LOK |
131 | { |
132 | CLockObject lock(m_mutex); | |
133 | m_bInitialised = bReturn; | |
134 | } | |
135 | ||
efed01e1 LOK |
136 | return bReturn; |
137 | } | |
a8f0bd18 | 138 | |
a2198e5e | 139 | bool CUSBCECAdapterCommunication::Open(IAdapterCommunicationCallback *cb, uint32_t iTimeoutMs /* = 10000 */, bool bSkipChecks /* = false */) |
efed01e1 LOK |
140 | { |
141 | uint64_t iNow = GetTimeMs(); | |
142 | uint64_t iTimeout = iNow + iTimeoutMs; | |
a8f0bd18 | 143 | |
2c780401 | 144 | { |
efed01e1 LOK |
145 | CLockObject lock(m_mutex); |
146 | ||
147 | if (!m_port) | |
148 | { | |
149 | CLibCEC::AddLog(CEC_LOG_ERROR, "port is NULL"); | |
150 | return false; | |
151 | } | |
152 | ||
153 | if (IsOpen()) | |
154 | { | |
155 | CLibCEC::AddLog(CEC_LOG_ERROR, "port is already open"); | |
156 | return true; | |
157 | } | |
158 | ||
159 | m_callback = cb; | |
160 | CStdString strError; | |
161 | bool bConnected(false); | |
162 | while (!bConnected && iNow < iTimeout) | |
163 | { | |
164 | if ((bConnected = m_port->Open(iTimeout)) == false) | |
165 | { | |
166 | strError.Format("error opening serial port '%s': %s", m_port->GetName().c_str(), m_port->GetError().c_str()); | |
167 | Sleep(250); | |
168 | iNow = GetTimeMs(); | |
169 | } | |
170 | } | |
171 | ||
172 | if (!bConnected) | |
173 | { | |
174 | CLibCEC::AddLog(CEC_LOG_ERROR, strError); | |
175 | return false; | |
176 | } | |
177 | ||
178 | CLibCEC::AddLog(CEC_LOG_DEBUG, "connection opened, clearing any previous input and waiting for active transmissions to end before starting"); | |
179 | ||
a2198e5e | 180 | if (!bSkipChecks) |
efed01e1 | 181 | { |
a2198e5e LOK |
182 | //clear any input bytes |
183 | uint8_t buff[1024]; | |
184 | while (m_port->Read(buff, 1024, 100) > 0) | |
185 | { | |
186 | CLibCEC::AddLog(CEC_LOG_DEBUG, "data received, clearing it"); | |
187 | Sleep(250); | |
188 | } | |
efed01e1 | 189 | } |
2c780401 | 190 | } |
a8f0bd18 | 191 | |
befa3a23 | 192 | if (!bSkipChecks && !CheckAdapter()) |
a8f0bd18 | 193 | { |
befa3a23 LOK |
194 | CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter failed to pass basic checks"); |
195 | return false; | |
196 | } | |
197 | else | |
198 | { | |
199 | if (CreateThread()) | |
efed01e1 | 200 | { |
befa3a23 LOK |
201 | CLibCEC::AddLog(CEC_LOG_DEBUG, "communication thread started"); |
202 | return true; | |
efed01e1 LOK |
203 | } |
204 | else | |
205 | { | |
befa3a23 | 206 | CLibCEC::AddLog(CEC_LOG_ERROR, "could not create a communication thread"); |
efed01e1 | 207 | } |
a8f0bd18 LOK |
208 | } |
209 | ||
210 | return false; | |
211 | } | |
212 | ||
7bb4ed43 | 213 | void CUSBCECAdapterCommunication::Close(void) |
a8f0bd18 | 214 | { |
b9eea66d | 215 | StopThread(); |
a8f0bd18 LOK |
216 | } |
217 | ||
7bb4ed43 | 218 | void *CUSBCECAdapterCommunication::Process(void) |
a8f0bd18 | 219 | { |
83554890 LOK |
220 | m_messageProcessor = new CUSBCECAdapterProcessor(m_callback); |
221 | m_messageProcessor->CreateThread(); | |
222 | ||
b1f94db1 | 223 | cec_command command; |
32553784 | 224 | command.Clear(); |
efed01e1 | 225 | bool bCommandReceived(false); |
ae54110f | 226 | CTimeout pingTimeout(CEC_ADAPTER_PING_TIMEOUT); |
13fd6a66 | 227 | while (!IsStopped()) |
a8f0bd18 | 228 | { |
efed01e1 LOK |
229 | { |
230 | CLockObject lock(m_mutex); | |
231 | ReadFromDevice(50); | |
cccd2724 | 232 | bCommandReceived = m_callback && Read(command, 0) && m_bInitialised; |
efed01e1 | 233 | } |
b1f94db1 LOK |
234 | |
235 | /* push the next command to the callback method if there is one */ | |
efed01e1 | 236 | if (!IsStopped() && bCommandReceived) |
83554890 | 237 | m_messageProcessor->AddCommand(command); |
b1f94db1 | 238 | |
ae54110f LOK |
239 | /* ping the adapter every 15 seconds */ |
240 | if (pingTimeout.TimeLeft() == 0) | |
241 | { | |
242 | pingTimeout.Init(CEC_ADAPTER_PING_TIMEOUT); | |
243 | PingAdapter(); | |
244 | } | |
245 | ||
efed01e1 LOK |
246 | if (!IsStopped()) |
247 | { | |
248 | Sleep(5); | |
249 | WriteNextCommand(); | |
250 | } | |
a8f0bd18 LOK |
251 | } |
252 | ||
83554890 LOK |
253 | /* stop the message processor */ |
254 | m_messageProcessor->StopThread(); | |
255 | delete m_messageProcessor; | |
256 | ||
f9e01dac | 257 | /* notify all threads that are waiting on messages to be sent */ |
ef7696f5 | 258 | CCECAdapterMessage *msg(NULL); |
f9e01dac | 259 | while (m_outBuffer.Pop(msg)) |
960f33c6 | 260 | msg->event.Broadcast(); |
a0878ee3 | 261 | |
f9e01dac LOK |
262 | /* set the ackmask to 0 before closing the connection */ |
263 | SetAckMaskInternal(0, true); | |
264 | ||
3c30c490 LOK |
265 | if (m_iFirmwareVersion >= 2) |
266 | SetControlledMode(false); | |
267 | ||
9f9c8c82 LOK |
268 | if (m_port) |
269 | { | |
270 | delete m_port; | |
271 | m_port = NULL; | |
272 | } | |
273 | ||
a8f0bd18 LOK |
274 | return NULL; |
275 | } | |
276 | ||
7bb4ed43 LOK |
277 | cec_adapter_message_state CUSBCECAdapterCommunication::Write(const cec_command &data, uint8_t iMaxTries, uint8_t iLineTimeout /* = 3 */, uint8_t iRetryLineTimeout /* = 3 */) |
278 | { | |
279 | cec_adapter_message_state retVal(ADAPTER_MESSAGE_STATE_UNKNOWN); | |
9f68cc28 LOK |
280 | if (!IsRunning()) |
281 | return retVal; | |
7bb4ed43 LOK |
282 | |
283 | CCECAdapterMessage *output = new CCECAdapterMessage(data); | |
284 | ||
285 | /* set the number of retries */ | |
286 | if (data.opcode == CEC_OPCODE_NONE) //TODO | |
287 | output->maxTries = 1; | |
288 | else if (data.initiator != CECDEVICE_BROADCAST) | |
289 | output->maxTries = iMaxTries; | |
290 | ||
291 | output->lineTimeout = iLineTimeout; | |
292 | output->retryTimeout = iRetryLineTimeout; | |
293 | output->tries = 0; | |
294 | ||
295 | bool bRetry(true); | |
296 | while (bRetry && ++output->tries < output->maxTries) | |
297 | { | |
298 | bRetry = (!Write(output) || output->NeedsRetry()) && output->transmit_timeout > 0; | |
299 | if (bRetry) | |
300 | Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT); | |
301 | } | |
302 | retVal = output->state; | |
303 | ||
304 | delete output; | |
305 | return retVal; | |
306 | } | |
307 | ||
308 | bool CUSBCECAdapterCommunication::Write(CCECAdapterMessage *data) | |
3c53ac93 | 309 | { |
5dcf9f25 | 310 | data->state = ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT; |
3c53ac93 | 311 | m_outBuffer.Push(data); |
f9e01dac | 312 | data->event.Wait(5000); |
5dcf9f25 | 313 | |
b1f94db1 LOK |
314 | if ((data->expectControllerAck && data->state != ADAPTER_MESSAGE_STATE_SENT_ACKED) || |
315 | (!data->expectControllerAck && data->state != ADAPTER_MESSAGE_STATE_SENT)) | |
5dcf9f25 | 316 | { |
b1f94db1 LOK |
317 | CLibCEC::AddLog(CEC_LOG_DEBUG, "command was not %s", data->state == ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED ? "acked" : "sent"); |
318 | return false; | |
5dcf9f25 LOK |
319 | } |
320 | ||
b1f94db1 | 321 | return true; |
a8f0bd18 LOK |
322 | } |
323 | ||
7bb4ed43 | 324 | bool CUSBCECAdapterCommunication::Read(cec_command &command, uint32_t iTimeout) |
a8f0bd18 | 325 | { |
9f68cc28 LOK |
326 | if (!IsRunning()) |
327 | return false; | |
328 | ||
7bb4ed43 LOK |
329 | CCECAdapterMessage msg; |
330 | if (Read(msg, iTimeout)) | |
a8f0bd18 | 331 | { |
7bb4ed43 | 332 | if (ParseMessage(msg)) |
a8f0bd18 | 333 | { |
7bb4ed43 LOK |
334 | command = m_currentframe; |
335 | m_currentframe.Clear(); | |
336 | return true; | |
a8f0bd18 | 337 | } |
7bb4ed43 LOK |
338 | } |
339 | return false; | |
340 | } | |
a8f0bd18 | 341 | |
7bb4ed43 LOK |
342 | bool CUSBCECAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout) |
343 | { | |
344 | CLockObject lock(m_mutex); | |
a8f0bd18 | 345 | |
7bb4ed43 LOK |
346 | msg.Clear(); |
347 | CCECAdapterMessage *buf(NULL); | |
a8f0bd18 | 348 | |
7bb4ed43 LOK |
349 | if (!m_inBuffer.Pop(buf)) |
350 | { | |
960f33c6 | 351 | if (iTimeout == 0 || !m_rcvCondition.Wait(m_mutex, m_bHasData, iTimeout)) |
7bb4ed43 LOK |
352 | return false; |
353 | m_inBuffer.Pop(buf); | |
120d4ca8 | 354 | m_bHasData = !m_inBuffer.IsEmpty(); |
7bb4ed43 | 355 | } |
0e31a62c | 356 | |
7bb4ed43 LOK |
357 | if (buf) |
358 | { | |
359 | msg.packet = buf->packet; | |
66e5bd72 | 360 | msg.state = ADAPTER_MESSAGE_STATE_INCOMING; |
7bb4ed43 LOK |
361 | delete buf; |
362 | return true; | |
363 | } | |
364 | return false; | |
a8f0bd18 LOK |
365 | } |
366 | ||
7bb4ed43 | 367 | CStdString CUSBCECAdapterCommunication::GetError(void) const |
a8f0bd18 | 368 | { |
ba65909d LOK |
369 | CStdString strError; |
370 | strError = m_port->GetError(); | |
371 | return strError; | |
a8f0bd18 | 372 | } |
2abe74eb | 373 | |
7bb4ed43 | 374 | bool CUSBCECAdapterCommunication::StartBootloader(void) |
2abe74eb | 375 | { |
28352a04 | 376 | bool bReturn(false); |
2abe74eb | 377 | if (!IsRunning()) |
28352a04 | 378 | return bReturn; |
2abe74eb | 379 | |
5477a250 | 380 | CLibCEC::AddLog(CEC_LOG_DEBUG, "starting the bootloader"); |
2abe74eb | 381 | |
089f0e9d LOK |
382 | CCECAdapterMessage params; |
383 | return SendCommand(MSGCODE_START_BOOTLOADER, params, false); | |
2abe74eb LOK |
384 | } |
385 | ||
7bb4ed43 | 386 | bool CUSBCECAdapterCommunication::PingAdapter(void) |
2abe74eb | 387 | { |
befa3a23 | 388 | CLockObject lock(m_mutex); |
5477a250 | 389 | CLibCEC::AddLog(CEC_LOG_DEBUG, "sending ping"); |
befa3a23 | 390 | |
089f0e9d LOK |
391 | CCECAdapterMessage params; |
392 | return SendCommand(MSGCODE_PING, params); | |
2abe74eb | 393 | } |
13fd6a66 | 394 | |
7bb4ed43 LOK |
395 | bool CUSBCECAdapterCommunication::ParseMessage(const CCECAdapterMessage &msg) |
396 | { | |
397 | bool bEom(false); | |
398 | bool bIsError(msg.IsError()); | |
399 | ||
400 | if (msg.IsEmpty()) | |
401 | return bEom; | |
402 | ||
24dd566c | 403 | CLockObject adapterLock(m_mutex); |
7bb4ed43 LOK |
404 | switch(msg.Message()) |
405 | { | |
406 | case MSGCODE_FRAME_START: | |
407 | { | |
408 | m_currentframe.Clear(); | |
409 | if (msg.Size() >= 2) | |
410 | { | |
411 | m_currentframe.initiator = msg.Initiator(); | |
412 | m_currentframe.destination = msg.Destination(); | |
413 | m_currentframe.ack = msg.IsACK(); | |
414 | m_currentframe.eom = msg.IsEOM(); | |
415 | } | |
416 | if (m_currentframe.ack == 0x1) | |
417 | { | |
d297cbd4 LOK |
418 | m_lastInitiator = m_currentframe.initiator; |
419 | m_currentframe.eom = 1; | |
420 | bEom = true; | |
7bb4ed43 LOK |
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()); | |
d297cbd4 | 445 | return msg.IsEOM() || bEom; |
7bb4ed43 LOK |
446 | } |
447 | ||
448 | uint16_t CUSBCECAdapterCommunication::GetFirmwareVersion(void) | |
1fc16cfd LOK |
449 | { |
450 | uint16_t iReturn(m_iFirmwareVersion); | |
1fc16cfd LOK |
451 | |
452 | if (iReturn == CEC_FW_VERSION_UNKNOWN) | |
453 | { | |
006b76b9 | 454 | CLockObject lock(m_mutex); |
5477a250 | 455 | CLibCEC::AddLog(CEC_LOG_DEBUG, "requesting the firmware version"); |
90008d10 | 456 | cec_datapacket response = GetSetting(MSGCODE_FIRMWARE_VERSION, 2); |
d4db0c6f | 457 | if (response.size == 2) |
1fc16cfd | 458 | { |
d4db0c6f LOK |
459 | m_iFirmwareVersion = (response[0] << 8 | response[1]); |
460 | iReturn = m_iFirmwareVersion; | |
461 | CLibCEC::AddLog(CEC_LOG_DEBUG, "firmware version %d", m_iFirmwareVersion); | |
efed01e1 | 462 | } |
1fc16cfd LOK |
463 | } |
464 | ||
465 | return iReturn; | |
466 | } | |
467 | ||
7bb4ed43 | 468 | bool CUSBCECAdapterCommunication::SetLineTimeout(uint8_t iTimeout) |
a171d2fd | 469 | { |
7bb4ed43 | 470 | m_iLineTimeout = iTimeout; |
089f0e9d LOK |
471 | bool bReturn(m_iLineTimeout != iTimeout); |
472 | ||
473 | if (!bReturn) | |
474 | { | |
475 | CLibCEC::AddLog(CEC_LOG_DEBUG, "setting the line timeout to %d", iTimeout); | |
476 | CCECAdapterMessage params; | |
477 | params.PushEscaped(iTimeout); | |
478 | bReturn = SendCommand(MSGCODE_TRANSMIT_IDLETIME, params); | |
479 | } | |
480 | ||
481 | return bReturn; | |
a171d2fd LOK |
482 | } |
483 | ||
7bb4ed43 | 484 | bool CUSBCECAdapterCommunication::SetAckMask(uint16_t iMask) |
f9e01dac | 485 | { |
089f0e9d | 486 | return SetAckMaskInternal(iMask, IsRunning()); |
f9e01dac LOK |
487 | } |
488 | ||
489 | bool CUSBCECAdapterCommunication::SetAckMaskInternal(uint16_t iMask, bool bWriteDirectly /* = false */) | |
5dcf9f25 | 490 | { |
bca69ca1 | 491 | CLibCEC::AddLog(CEC_LOG_DEBUG, "setting ackmask to %2x", iMask); |
5dcf9f25 | 492 | |
089f0e9d LOK |
493 | CCECAdapterMessage params; |
494 | params.PushEscaped(iMask >> 8); | |
495 | params.PushEscaped((uint8_t)iMask); | |
496 | return SendCommand(MSGCODE_SET_ACK_MASK, params, true, false, bWriteDirectly); | |
5dcf9f25 LOK |
497 | } |
498 | ||
c214d197 LOK |
499 | bool CUSBCECAdapterCommunication::PersistConfiguration(libcec_configuration *configuration) |
500 | { | |
cb4ee028 LOK |
501 | if (m_iFirmwareVersion < 2) |
502 | return false; | |
503 | ||
ffdfabf0 | 504 | bool bReturn(true); |
12a36be9 LOK |
505 | bReturn &= SetSettingAutoEnabled(true); |
506 | bReturn &= SetSettingDeviceType(CLibCEC::GetType(configuration->logicalAddresses.primary)); | |
507 | bReturn &= SetSettingDefaultLogicalAddress(configuration->logicalAddresses.primary); | |
508 | bReturn &= SetSettingLogicalAddressMask(CLibCEC::GetMaskForType(configuration->logicalAddresses.primary)); | |
509 | bReturn &= SetSettingPhysicalAddress(configuration->iPhysicalAddress); | |
510 | bReturn &= SetSettingCECVersion(CEC_VERSION_1_3A); | |
511 | bReturn &= SetSettingOSDName(configuration->strDeviceName); | |
ffdfabf0 LOK |
512 | if (bReturn) |
513 | bReturn = WriteEEPROM(); | |
514 | return bReturn; | |
c214d197 | 515 | } |
b057edad | 516 | |
12a36be9 LOK |
517 | bool CUSBCECAdapterCommunication::GetConfiguration(libcec_configuration *configuration) |
518 | { | |
519 | if (m_iFirmwareVersion < 2) | |
520 | return false; | |
521 | ||
522 | bool bReturn(true); | |
523 | cec_device_type type; | |
524 | if (GetSettingDeviceType(type)) | |
525 | { | |
526 | CLibCEC::AddLog(CEC_LOG_DEBUG, "using persisted device type setting %s", m_processor->ToString(type)); | |
527 | configuration->deviceTypes.Clear(); | |
528 | configuration->deviceTypes.Add(type); | |
529 | } | |
530 | else | |
531 | { | |
532 | CLibCEC::AddLog(CEC_LOG_DEBUG, "no persisted device type setting"); | |
533 | bReturn = false; | |
534 | } | |
535 | ||
536 | if (GetSettingPhysicalAddress(configuration->iPhysicalAddress)) | |
537 | { | |
538 | CLibCEC::AddLog(CEC_LOG_DEBUG, "using persisted physical address setting %4x", configuration->iPhysicalAddress); | |
539 | } | |
540 | else | |
541 | { | |
542 | CLibCEC::AddLog(CEC_LOG_DEBUG, "no persisted physical address setting"); | |
543 | bReturn = false; | |
544 | } | |
545 | ||
546 | CStdString strDeviceName; | |
547 | if (GetSettingOSDName(strDeviceName)) | |
548 | { | |
549 | snprintf(configuration->strDeviceName, 13, "%s", strDeviceName.c_str()); | |
550 | CLibCEC::AddLog(CEC_LOG_DEBUG, "using persisted device name setting %s", configuration->strDeviceName); | |
551 | } | |
552 | else | |
553 | { | |
554 | CLibCEC::AddLog(CEC_LOG_DEBUG, "no persisted device name setting"); | |
555 | bReturn = false; | |
556 | } | |
557 | ||
558 | // don't read the following settings: | |
559 | // - auto enabled (always enabled) | |
560 | // - default logical address (autodetected) | |
561 | // - logical address mask (autodetected) | |
562 | // - CEC version (1.3a) | |
563 | ||
564 | // TODO to be added to the firmware: | |
d28b4393 LOK |
565 | // - base device (4 bits) |
566 | // - HDMI port number (4 bits) | |
567 | // - TV vendor id (12 bits) | |
568 | // - wake devices (8 bits) | |
569 | // - standby devices (8 bits) | |
12a36be9 LOK |
570 | // - use TV menu language (1 bit) |
571 | // - activate source (1 bit) | |
572 | // - power off screensaver (1 bit) | |
573 | // - power off on standby (1 bit) | |
574 | // - send inactive source (1 bit) | |
575 | return bReturn; | |
576 | } | |
577 | ||
b057edad BL |
578 | bool CUSBCECAdapterCommunication::SetControlledMode(bool controlled) |
579 | { | |
befa3a23 | 580 | CLockObject lock(m_mutex); |
bca69ca1 | 581 | CLibCEC::AddLog(CEC_LOG_DEBUG, "turning controlled mode %s", controlled ? "on" : "off"); |
b057edad | 582 | |
089f0e9d LOK |
583 | CCECAdapterMessage params; |
584 | params.PushEscaped(controlled ? 1 : 0); | |
585 | return SendCommand(MSGCODE_SET_CONTROLLED, params); | |
b057edad BL |
586 | } |
587 | ||
12a36be9 | 588 | bool CUSBCECAdapterCommunication::SetSettingAutoEnabled(bool enabled) |
c214d197 LOK |
589 | { |
590 | CLockObject lock(m_mutex); | |
591 | CLibCEC::AddLog(CEC_LOG_DEBUG, "turning autonomous mode %s", enabled ? "on" : "off"); | |
592 | ||
c9c282a4 LOK |
593 | CCECAdapterMessage params; |
594 | params.PushEscaped(enabled ? 1 : 0); | |
595 | return SendCommand(MSGCODE_SET_AUTO_ENABLED, params); | |
c214d197 LOK |
596 | } |
597 | ||
12a36be9 LOK |
598 | bool CUSBCECAdapterCommunication::GetSettingAutoEnabled(bool &enabled) |
599 | { | |
600 | CLockObject lock(m_mutex); | |
601 | CLibCEC::AddLog(CEC_LOG_DEBUG, "requesting autonomous mode setting"); | |
602 | ||
90008d10 | 603 | cec_datapacket response = GetSetting(MSGCODE_GET_AUTO_ENABLED, 1); |
12a36be9 LOK |
604 | if (response.size == 1) |
605 | { | |
606 | enabled = response[0] == 1; | |
607 | return true; | |
608 | } | |
609 | return false; | |
610 | } | |
611 | ||
612 | bool CUSBCECAdapterCommunication::SetSettingDeviceType(cec_device_type type) | |
9878069e LOK |
613 | { |
614 | CLockObject lock(m_mutex); | |
615 | CLibCEC::AddLog(CEC_LOG_DEBUG, "setting the device type to %1X", (uint8_t)type); | |
616 | ||
c9c282a4 LOK |
617 | CCECAdapterMessage params; |
618 | params.PushEscaped((uint8_t)type); | |
619 | return SendCommand(MSGCODE_SET_DEVICE_TYPE, params); | |
9878069e LOK |
620 | } |
621 | ||
12a36be9 LOK |
622 | bool CUSBCECAdapterCommunication::GetSettingDeviceType(cec_device_type &value) |
623 | { | |
624 | CLockObject lock(m_mutex); | |
625 | CLibCEC::AddLog(CEC_LOG_DEBUG, "requesting device type setting"); | |
626 | ||
90008d10 | 627 | cec_datapacket response = GetSetting(MSGCODE_GET_DEVICE_TYPE, 1); |
12a36be9 LOK |
628 | if (response.size == 1) |
629 | { | |
630 | value = (cec_device_type)response[0]; | |
631 | return true; | |
632 | } | |
633 | return false; | |
634 | } | |
635 | ||
636 | bool CUSBCECAdapterCommunication::SetSettingDefaultLogicalAddress(cec_logical_address address) | |
c214d197 LOK |
637 | { |
638 | CLockObject lock(m_mutex); | |
639 | CLibCEC::AddLog(CEC_LOG_DEBUG, "setting the default logical address to %1X", address); | |
640 | ||
c9c282a4 LOK |
641 | CCECAdapterMessage params; |
642 | params.PushEscaped((uint8_t)address); | |
643 | return SendCommand(MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS, params); | |
c214d197 LOK |
644 | } |
645 | ||
12a36be9 LOK |
646 | bool CUSBCECAdapterCommunication::GetSettingDefaultLogicalAddress(cec_logical_address &address) |
647 | { | |
648 | CLockObject lock(m_mutex); | |
649 | CLibCEC::AddLog(CEC_LOG_DEBUG, "requesting default logical address setting"); | |
650 | ||
90008d10 | 651 | cec_datapacket response = GetSetting(MSGCODE_GET_DEFAULT_LOGICAL_ADDRESS, 1); |
12a36be9 LOK |
652 | if (response.size == 1) |
653 | { | |
654 | address = (cec_logical_address)response[0]; | |
655 | return true; | |
656 | } | |
657 | return false; | |
658 | } | |
659 | ||
660 | bool CUSBCECAdapterCommunication::SetSettingLogicalAddressMask(uint16_t iMask) | |
c214d197 LOK |
661 | { |
662 | CLockObject lock(m_mutex); | |
663 | CLibCEC::AddLog(CEC_LOG_DEBUG, "setting the logical address mask to %2X", iMask); | |
664 | ||
c9c282a4 LOK |
665 | CCECAdapterMessage params; |
666 | params.PushEscaped(iMask >> 8); | |
667 | params.PushEscaped((uint8_t)iMask); | |
668 | return SendCommand(MSGCODE_SET_LOGICAL_ADDRESS_MASK, params); | |
c214d197 LOK |
669 | } |
670 | ||
12a36be9 LOK |
671 | bool CUSBCECAdapterCommunication::GetSettingLogicalAddressMask(uint16_t &iMask) |
672 | { | |
673 | CLockObject lock(m_mutex); | |
674 | CLibCEC::AddLog(CEC_LOG_DEBUG, "requesting logical address mask setting"); | |
675 | ||
90008d10 | 676 | cec_datapacket response = GetSetting(MSGCODE_GET_LOGICAL_ADDRESS_MASK, 2); |
12a36be9 LOK |
677 | if (response.size == 2) |
678 | { | |
679 | iMask = ((uint16_t)response[0] << 8) | ((uint16_t)response[1]); | |
680 | return true; | |
681 | } | |
682 | return false; | |
683 | } | |
684 | ||
685 | bool CUSBCECAdapterCommunication::SetSettingPhysicalAddress(uint16_t iPhysicalAddress) | |
c214d197 LOK |
686 | { |
687 | CLockObject lock(m_mutex); | |
9ff1aa80 | 688 | CLibCEC::AddLog(CEC_LOG_DEBUG, "setting the physical address to %04X", iPhysicalAddress); |
c214d197 | 689 | |
c9c282a4 LOK |
690 | CCECAdapterMessage params; |
691 | params.PushEscaped(iPhysicalAddress >> 8); | |
692 | params.PushEscaped((uint8_t)iPhysicalAddress); | |
693 | return SendCommand(MSGCODE_SET_PHYSICAL_ADDRESS, params); | |
c214d197 LOK |
694 | } |
695 | ||
12a36be9 LOK |
696 | bool CUSBCECAdapterCommunication::GetSettingPhysicalAddress(uint16_t &iPhysicalAddress) |
697 | { | |
698 | CLockObject lock(m_mutex); | |
699 | CLibCEC::AddLog(CEC_LOG_DEBUG, "requesting physical address setting"); | |
700 | ||
90008d10 | 701 | cec_datapacket response = GetSetting(MSGCODE_GET_PHYSICAL_ADDRESS, 2); |
12a36be9 LOK |
702 | if (response.size == 2) |
703 | { | |
704 | iPhysicalAddress = ((uint16_t)response[0] << 8) | ((uint16_t)response[1]); | |
705 | return true; | |
706 | } | |
707 | return false; | |
708 | } | |
709 | ||
710 | bool CUSBCECAdapterCommunication::SetSettingCECVersion(cec_version version) | |
c214d197 LOK |
711 | { |
712 | CLockObject lock(m_mutex); | |
713 | CLibCEC::AddLog(CEC_LOG_DEBUG, "setting the CEC version to %s", CLibCEC::GetInstance()->ToString(version)); | |
714 | ||
c9c282a4 LOK |
715 | CCECAdapterMessage params; |
716 | params.PushEscaped((uint8_t)version); | |
717 | return SendCommand(MSGCODE_SET_HDMI_VERSION, params); | |
c214d197 LOK |
718 | } |
719 | ||
12a36be9 LOK |
720 | bool CUSBCECAdapterCommunication::GetSettingCECVersion(cec_version &version) |
721 | { | |
722 | CLockObject lock(m_mutex); | |
723 | CLibCEC::AddLog(CEC_LOG_DEBUG, "requesting CEC version setting"); | |
724 | ||
90008d10 | 725 | cec_datapacket response = GetSetting(MSGCODE_GET_HDMI_VERSION, 1); |
12a36be9 LOK |
726 | if (response.size == 1) |
727 | { | |
728 | version = (cec_version)response[0]; | |
729 | return true; | |
730 | } | |
731 | return false; | |
732 | } | |
733 | ||
734 | bool CUSBCECAdapterCommunication::SetSettingOSDName(const char *strOSDName) | |
c214d197 LOK |
735 | { |
736 | CLockObject lock(m_mutex); | |
737 | CLibCEC::AddLog(CEC_LOG_DEBUG, "setting the OSD name to %s", strOSDName); | |
738 | ||
c9c282a4 | 739 | CCECAdapterMessage params; |
c214d197 | 740 | for (size_t iPtr = 0; iPtr < strlen(strOSDName); iPtr++) |
c9c282a4 LOK |
741 | params.PushEscaped(strOSDName[iPtr]); |
742 | return SendCommand(MSGCODE_SET_OSD_NAME, params); | |
c214d197 LOK |
743 | } |
744 | ||
12a36be9 LOK |
745 | bool CUSBCECAdapterCommunication::GetSettingOSDName(CStdString &strOSDName) |
746 | { | |
747 | CLockObject lock(m_mutex); | |
748 | CLibCEC::AddLog(CEC_LOG_DEBUG, "requesting OSD name setting"); | |
749 | ||
90008d10 | 750 | cec_datapacket response = GetSetting(MSGCODE_GET_OSD_NAME, 13); |
12a36be9 LOK |
751 | if (response.size == 0) |
752 | return false; | |
753 | ||
90008d10 LOK |
754 | char buf[14]; |
755 | for (uint8_t iPtr = 0; iPtr < response.size && iPtr < 13; iPtr++) | |
12a36be9 LOK |
756 | buf[iPtr] = (char)response[iPtr]; |
757 | buf[response.size] = 0; | |
758 | ||
759 | strOSDName.Format("%s", buf); | |
760 | return true; | |
761 | } | |
762 | ||
c214d197 LOK |
763 | bool CUSBCECAdapterCommunication::WriteEEPROM(void) |
764 | { | |
765 | CLockObject lock(m_mutex); | |
766 | CLibCEC::AddLog(CEC_LOG_DEBUG, "writing settings in the EEPROM"); | |
767 | ||
c9c282a4 LOK |
768 | CCECAdapterMessage params; |
769 | return SendCommand(MSGCODE_WRITE_EEPROM, params); | |
c214d197 LOK |
770 | } |
771 | ||
7bb4ed43 | 772 | bool CUSBCECAdapterCommunication::IsOpen(void) |
13fd6a66 | 773 | { |
b9eea66d | 774 | return !IsStopped() && m_port->IsOpen() && IsRunning(); |
13fd6a66 | 775 | } |
ef7696f5 | 776 | |
7bb4ed43 | 777 | bool CUSBCECAdapterCommunication::WaitForAck(CCECAdapterMessage &message) |
6729ac71 LOK |
778 | { |
779 | bool bError(false); | |
780 | bool bTransmitSucceeded(false); | |
99f1d66e | 781 | uint8_t iPacketsLeft(message.isTransmission ? message.Size() / 4 : 1); |
6729ac71 LOK |
782 | |
783 | int64_t iNow = GetTimeMs(); | |
b1f94db1 | 784 | int64_t iTargetTime = iNow + (message.transmit_timeout <= 5 ? CEC_DEFAULT_TRANSMIT_WAIT : message.transmit_timeout); |
6729ac71 | 785 | |
b1f94db1 | 786 | while (!bTransmitSucceeded && !bError && iNow < iTargetTime) |
6729ac71 | 787 | { |
b1f94db1 | 788 | ReadFromDevice(50); |
6729ac71 | 789 | CCECAdapterMessage msg; |
b1f94db1 | 790 | if (!Read(msg, 0)) |
6729ac71 LOK |
791 | { |
792 | iNow = GetTimeMs(); | |
793 | continue; | |
794 | } | |
795 | ||
796 | if (msg.Message() == MSGCODE_FRAME_START && msg.IsACK()) | |
797 | { | |
798 | m_processor->HandlePoll(msg.Initiator(), msg.Destination()); | |
7bb4ed43 | 799 | m_lastInitiator = msg.Initiator(); |
6729ac71 LOK |
800 | iNow = GetTimeMs(); |
801 | continue; | |
802 | } | |
803 | ||
804 | if (msg.Message() == MSGCODE_RECEIVE_FAILED && | |
7bb4ed43 LOK |
805 | m_lastInitiator != CECDEVICE_UNKNOWN && |
806 | m_processor->HandleReceiveFailed(m_lastInitiator)) | |
6729ac71 LOK |
807 | { |
808 | iNow = GetTimeMs(); | |
809 | continue; | |
810 | } | |
811 | ||
812 | bError = msg.IsError(); | |
813 | if (bError) | |
814 | { | |
b2f0b1ab | 815 | message.reply = msg.Message(); |
5477a250 | 816 | CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString()); |
6729ac71 LOK |
817 | } |
818 | else | |
819 | { | |
820 | switch(msg.Message()) | |
821 | { | |
822 | case MSGCODE_COMMAND_ACCEPTED: | |
6729ac71 LOK |
823 | if (iPacketsLeft > 0) |
824 | iPacketsLeft--; | |
b2f0b1ab | 825 | if (!message.isTransmission && iPacketsLeft == 0) |
5dcf9f25 | 826 | bTransmitSucceeded = true; |
befa3a23 | 827 | CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - waiting for %d more", msg.ToString().c_str(), iPacketsLeft); |
6729ac71 LOK |
828 | break; |
829 | case MSGCODE_TRANSMIT_SUCCEEDED: | |
5477a250 | 830 | CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString()); |
6729ac71 LOK |
831 | bTransmitSucceeded = (iPacketsLeft == 0); |
832 | bError = !bTransmitSucceeded; | |
b2f0b1ab | 833 | message.reply = MSGCODE_TRANSMIT_SUCCEEDED; |
6729ac71 LOK |
834 | break; |
835 | default: | |
836 | // ignore other data while waiting | |
837 | break; | |
838 | } | |
839 | ||
840 | iNow = GetTimeMs(); | |
841 | } | |
842 | } | |
843 | ||
b1f94db1 LOK |
844 | message.state = bTransmitSucceeded && !bError ? |
845 | ADAPTER_MESSAGE_STATE_SENT_ACKED : | |
846 | ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED; | |
847 | ||
6729ac71 LOK |
848 | return bTransmitSucceeded && !bError; |
849 | } | |
850 | ||
7bb4ed43 | 851 | void CUSBCECAdapterCommunication::AddData(uint8_t *data, size_t iLen) |
ef7696f5 LOK |
852 | { |
853 | CLockObject lock(m_mutex); | |
99666519 | 854 | for (size_t iPtr = 0; iPtr < iLen; iPtr++) |
7bb4ed43 LOK |
855 | { |
856 | if (!m_bGotStart) | |
857 | { | |
858 | if (data[iPtr] == MSGSTART) | |
859 | m_bGotStart = true; | |
860 | } | |
861 | else if (data[iPtr] == MSGSTART) //we found a msgstart before msgend, this is not right, remove | |
862 | { | |
863 | if (m_currentAdapterMessage.Size() > 0) | |
864 | CLibCEC::AddLog(CEC_LOG_WARNING, "received MSGSTART before MSGEND, removing previous buffer contents"); | |
865 | m_currentAdapterMessage.Clear(); | |
866 | m_bGotStart = true; | |
867 | } | |
868 | else if (data[iPtr] == MSGEND) | |
869 | { | |
870 | CCECAdapterMessage *newMessage = new CCECAdapterMessage; | |
871 | newMessage->packet = m_currentAdapterMessage.packet; | |
872 | m_inBuffer.Push(newMessage); | |
873 | m_currentAdapterMessage.Clear(); | |
874 | m_bGotStart = false; | |
875 | m_bNextIsEscaped = false; | |
960f33c6 | 876 | m_bHasData = true; |
24dd566c | 877 | m_rcvCondition.Broadcast(); |
7bb4ed43 LOK |
878 | } |
879 | else if (m_bNextIsEscaped) | |
880 | { | |
881 | m_currentAdapterMessage.PushBack(data[iPtr] + (uint8_t)ESCOFFSET); | |
882 | m_bNextIsEscaped = false; | |
883 | } | |
884 | else if (data[iPtr] == MSGESC) | |
885 | { | |
886 | m_bNextIsEscaped = true; | |
887 | } | |
888 | else | |
889 | { | |
890 | m_currentAdapterMessage.PushBack(data[iPtr]); | |
891 | } | |
892 | } | |
ef7696f5 LOK |
893 | } |
894 | ||
b1f94db1 | 895 | bool CUSBCECAdapterCommunication::ReadFromDevice(uint32_t iTimeout, size_t iSize /* = 256 */) |
ef7696f5 | 896 | { |
99666519 LOK |
897 | ssize_t iBytesRead; |
898 | uint8_t buff[256]; | |
ef7696f5 LOK |
899 | if (!m_port) |
900 | return false; | |
b1f94db1 LOK |
901 | if (iSize > 256) |
902 | iSize = 256; | |
ef7696f5 | 903 | |
1fc16cfd | 904 | CLockObject lock(m_mutex); |
b1f94db1 | 905 | iBytesRead = m_port->Read(buff, sizeof(uint8_t) * iSize, iTimeout); |
ef7696f5 LOK |
906 | if (iBytesRead < 0 || iBytesRead > 256) |
907 | { | |
99666519 | 908 | CLibCEC::AddLog(CEC_LOG_ERROR, "error reading from serial port: %s", m_port->GetError().c_str()); |
60383b11 | 909 | StopThread(false); |
ef7696f5 LOK |
910 | return false; |
911 | } | |
912 | else if (iBytesRead > 0) | |
99666519 LOK |
913 | { |
914 | AddData(buff, iBytesRead); | |
915 | } | |
ef7696f5 LOK |
916 | |
917 | return iBytesRead > 0; | |
918 | } | |
919 | ||
7bb4ed43 | 920 | void CUSBCECAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage *msg) |
ef7696f5 | 921 | { |
1fc16cfd | 922 | CLockObject adapterLock(m_mutex); |
f9e01dac LOK |
923 | if (!m_port->IsOpen()) |
924 | { | |
925 | CLibCEC::AddLog(CEC_LOG_ERROR, "error writing to serial port: the connection is closed"); | |
926 | msg->state = ADAPTER_MESSAGE_STATE_ERROR; | |
927 | return; | |
928 | } | |
929 | ||
089f0e9d LOK |
930 | if (msg->isTransmission && (msg->Size() < 2 || msg->At(1) != MSGCODE_TRANSMIT_IDLETIME)) |
931 | { | |
932 | if (msg->tries == 1) | |
933 | SetLineTimeout(msg->lineTimeout); | |
934 | else | |
935 | SetLineTimeout(msg->retryTimeout); | |
936 | } | |
7bb4ed43 | 937 | |
99666519 | 938 | if (m_port->Write(msg->packet.data, msg->Size()) != (ssize_t) msg->Size()) |
ef7696f5 | 939 | { |
b74fd339 | 940 | CLibCEC::AddLog(CEC_LOG_ERROR, "error writing to serial port: %s", m_port->GetError().c_str()); |
ef7696f5 LOK |
941 | msg->state = ADAPTER_MESSAGE_STATE_ERROR; |
942 | } | |
943 | else | |
944 | { | |
5477a250 | 945 | CLibCEC::AddLog(CEC_LOG_DEBUG, "command sent"); |
ef7696f5 | 946 | msg->state = ADAPTER_MESSAGE_STATE_SENT; |
b1f94db1 LOK |
947 | |
948 | if (msg->expectControllerAck) | |
949 | { | |
950 | if (!WaitForAck(*msg)) | |
951 | CLibCEC::AddLog(CEC_LOG_DEBUG, "did not receive ack"); | |
952 | } | |
ef7696f5 | 953 | } |
960f33c6 | 954 | msg->event.Signal(); |
ef7696f5 LOK |
955 | } |
956 | ||
7bb4ed43 | 957 | void CUSBCECAdapterCommunication::WriteNextCommand(void) |
ef7696f5 LOK |
958 | { |
959 | CCECAdapterMessage *msg(NULL); | |
960 | if (m_outBuffer.Pop(msg)) | |
961 | SendMessageToAdapter(msg); | |
962 | } | |
cba904a6 LOK |
963 | |
964 | CStdString CUSBCECAdapterCommunication::GetPortName(void) | |
965 | { | |
966 | CStdString strName; | |
967 | strName = m_port->GetName(); | |
968 | return strName; | |
969 | } | |
c9c282a4 | 970 | |
9b175d9c | 971 | bool CUSBCECAdapterCommunication::SendCommand(cec_adapter_messagecode msgCode, CCECAdapterMessage ¶ms, bool bExpectAck /* = true */, bool bIsTransmission /* = false */, bool bSendDirectly /* = true */, bool bIsRetry /* = false */) |
c9c282a4 LOK |
972 | { |
973 | CLockObject lock(m_mutex); | |
974 | ||
975 | CCECAdapterMessage *output = new CCECAdapterMessage; | |
976 | ||
977 | output->PushBack(MSGSTART); | |
edd18f05 | 978 | output->PushEscaped((uint8_t)msgCode); |
c9c282a4 LOK |
979 | output->Append(params); |
980 | output->PushBack(MSGEND); | |
089f0e9d LOK |
981 | output->isTransmission = bIsTransmission; |
982 | output->expectControllerAck = bExpectAck; | |
983 | ||
984 | if (bSendDirectly) | |
985 | SendMessageToAdapter(output); | |
986 | else | |
987 | Write(output); | |
c9c282a4 | 988 | |
089f0e9d | 989 | bool bWriteOk = output->state == (output->expectControllerAck ? ADAPTER_MESSAGE_STATE_SENT_ACKED : ADAPTER_MESSAGE_STATE_SENT); |
c9c282a4 LOK |
990 | if (!bWriteOk) |
991 | { | |
7a87e02e | 992 | CLibCEC::AddLog(CEC_LOG_ERROR, "'%s' failed", CCECAdapterMessage::ToString(msgCode)); |
c9c282a4 | 993 | delete output; |
9b175d9c LOK |
994 | |
995 | if (!bIsRetry && output->reply == MSGCODE_COMMAND_REJECTED && msgCode != MSGCODE_SET_CONTROLLED) | |
996 | { | |
997 | CLibCEC::AddLog(CEC_LOG_DEBUG, "setting controlled mode and retrying"); | |
998 | if (SetControlledMode(true)) | |
999 | return SendCommand(msgCode, params, bExpectAck, bIsTransmission, bSendDirectly, true); | |
1000 | } | |
c9c282a4 LOK |
1001 | return false; |
1002 | } | |
1003 | ||
1004 | delete output; | |
1005 | return true; | |
1006 | } | |
d4db0c6f | 1007 | |
90008d10 | 1008 | cec_datapacket CUSBCECAdapterCommunication::GetSetting(cec_adapter_messagecode msgCode, uint8_t iResponseLength) |
d4db0c6f LOK |
1009 | { |
1010 | cec_datapacket retVal; | |
1011 | retVal.Clear(); | |
1012 | ||
1013 | CCECAdapterMessage params; | |
1014 | if (!SendCommand(msgCode, params, false)) | |
1015 | { | |
1016 | CLibCEC::AddLog(CEC_LOG_ERROR, "%s failed", CCECAdapterMessage::ToString(msgCode)); | |
1017 | return retVal; | |
1018 | } | |
1019 | ||
1020 | Sleep(250); // TODO ReadFromDevice() isn't waiting for the timeout to pass on win32 | |
90008d10 | 1021 | ReadFromDevice(CEC_DEFAULT_TRANSMIT_WAIT, iResponseLength + 3 /* start + msgcode + iResponseLength + end */); |
d4db0c6f LOK |
1022 | CCECAdapterMessage input; |
1023 | if (Read(input, 0)) | |
1024 | { | |
1025 | if (input.Message() != msgCode) | |
1026 | CLibCEC::AddLog(CEC_LOG_ERROR, "invalid response to %s received (%s)", CCECAdapterMessage::ToString(msgCode), CCECAdapterMessage::ToString(input.Message())); | |
1027 | else | |
1028 | { | |
1029 | for (uint8_t iPtr = 1; iPtr < input.Size(); iPtr++) | |
1030 | retVal.PushBack(input[iPtr]); | |
1031 | } | |
1032 | } | |
1033 | else | |
1034 | { | |
1035 | CLibCEC::AddLog(CEC_LOG_ERROR, "no response to %s received", CCECAdapterMessage::ToString(msgCode)); | |
1036 | } | |
1037 | ||
1038 | return retVal; | |
1039 | } |