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