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