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