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