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