| 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 | CUSBCECAdapterCommunication::CUSBCECAdapterCommunication(CCECProcessor *processor, const char *strPort, uint16_t iBaudRate /* = 38400 */) : |
| 44 | m_port(NULL), |
| 45 | m_processor(processor), |
| 46 | m_bHasData(false), |
| 47 | m_iLineTimeout(0), |
| 48 | m_iFirmwareVersion(CEC_FW_VERSION_UNKNOWN), |
| 49 | m_lastInitiator(CECDEVICE_UNKNOWN), |
| 50 | m_bNextIsEscaped(false), |
| 51 | m_bGotStart(false) |
| 52 | { |
| 53 | m_port = new PLATFORM::CSerialPort(strPort, iBaudRate); |
| 54 | } |
| 55 | |
| 56 | CUSBCECAdapterCommunication::~CUSBCECAdapterCommunication(void) |
| 57 | { |
| 58 | Close(); |
| 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 | 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) |
| 81 | { |
| 82 | CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter did not respond with a correct firmware version (try %d)", ++iFwVersionTry); |
| 83 | Sleep(500); |
| 84 | iNow = GetTimeMs(); |
| 85 | } |
| 86 | |
| 87 | if (m_iFirmwareVersion >= 2) |
| 88 | { |
| 89 | /* try to set controlled mode */ |
| 90 | unsigned iControlledTry(0); |
| 91 | bool bControlled(false); |
| 92 | while (iNow < iTarget && (bControlled = SetControlledMode(true)) == false) |
| 93 | { |
| 94 | CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter did not respond correctly to setting controlled mode (try %d)", ++iControlledTry); |
| 95 | Sleep(500); |
| 96 | iNow = GetTimeMs(); |
| 97 | } |
| 98 | bReturn = bControlled; |
| 99 | } |
| 100 | else |
| 101 | bReturn = true; |
| 102 | |
| 103 | return bReturn; |
| 104 | } |
| 105 | |
| 106 | bool CUSBCECAdapterCommunication::Open(IAdapterCommunicationCallback *cb, uint32_t iTimeoutMs /* = 10000 */) |
| 107 | { |
| 108 | uint64_t iNow = GetTimeMs(); |
| 109 | uint64_t iTimeout = iNow + iTimeoutMs; |
| 110 | |
| 111 | { |
| 112 | CLockObject lock(m_mutex); |
| 113 | |
| 114 | if (!m_port) |
| 115 | { |
| 116 | CLibCEC::AddLog(CEC_LOG_ERROR, "port is NULL"); |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | if (IsOpen()) |
| 121 | { |
| 122 | CLibCEC::AddLog(CEC_LOG_ERROR, "port is already open"); |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | m_callback = cb; |
| 127 | CStdString strError; |
| 128 | bool bConnected(false); |
| 129 | while (!bConnected && iNow < iTimeout) |
| 130 | { |
| 131 | if ((bConnected = m_port->Open(iTimeout)) == false) |
| 132 | { |
| 133 | strError.Format("error opening serial port '%s': %s", m_port->GetName().c_str(), m_port->GetError().c_str()); |
| 134 | Sleep(250); |
| 135 | iNow = GetTimeMs(); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | if (!bConnected) |
| 140 | { |
| 141 | CLibCEC::AddLog(CEC_LOG_ERROR, strError); |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | CLibCEC::AddLog(CEC_LOG_DEBUG, "connection opened, clearing any previous input and waiting for active transmissions to end before starting"); |
| 146 | |
| 147 | //clear any input bytes |
| 148 | uint8_t buff[1024]; |
| 149 | while (m_port->Read(buff, 1024, 100) > 0) |
| 150 | { |
| 151 | CLibCEC::AddLog(CEC_LOG_DEBUG, "data received, clearing it"); |
| 152 | Sleep(250); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | if (CreateThread()) |
| 157 | { |
| 158 | if (!CheckAdapter()) |
| 159 | { |
| 160 | StopThread(); |
| 161 | CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter failed to pass basic checks"); |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | CLibCEC::AddLog(CEC_LOG_DEBUG, "communication thread started"); |
| 166 | return true; |
| 167 | } |
| 168 | } |
| 169 | CLibCEC::AddLog(CEC_LOG_ERROR, "could not create a communication thread"); |
| 170 | |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | void CUSBCECAdapterCommunication::Close(void) |
| 175 | { |
| 176 | StopThread(); |
| 177 | } |
| 178 | |
| 179 | void *CUSBCECAdapterCommunication::Process(void) |
| 180 | { |
| 181 | cec_command command; |
| 182 | bool bCommandReceived(false); |
| 183 | while (!IsStopped()) |
| 184 | { |
| 185 | { |
| 186 | CLockObject lock(m_mutex); |
| 187 | ReadFromDevice(50); |
| 188 | bCommandReceived = m_callback && Read(command, 0); |
| 189 | } |
| 190 | |
| 191 | /* push the next command to the callback method if there is one */ |
| 192 | if (!IsStopped() && bCommandReceived) |
| 193 | m_callback->OnCommandReceived(command); |
| 194 | |
| 195 | if (!IsStopped()) |
| 196 | { |
| 197 | Sleep(5); |
| 198 | WriteNextCommand(); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /* notify all threads that are waiting on messages to be sent */ |
| 203 | CCECAdapterMessage *msg(NULL); |
| 204 | while (m_outBuffer.Pop(msg)) |
| 205 | msg->event.Broadcast(); |
| 206 | |
| 207 | /* set the ackmask to 0 before closing the connection */ |
| 208 | SetAckMaskInternal(0, true); |
| 209 | |
| 210 | if (m_port) |
| 211 | { |
| 212 | delete m_port; |
| 213 | m_port = NULL; |
| 214 | } |
| 215 | |
| 216 | return NULL; |
| 217 | } |
| 218 | |
| 219 | cec_adapter_message_state CUSBCECAdapterCommunication::Write(const cec_command &data, uint8_t iMaxTries, uint8_t iLineTimeout /* = 3 */, uint8_t iRetryLineTimeout /* = 3 */) |
| 220 | { |
| 221 | cec_adapter_message_state retVal(ADAPTER_MESSAGE_STATE_UNKNOWN); |
| 222 | if (!IsRunning()) |
| 223 | return retVal; |
| 224 | |
| 225 | CCECAdapterMessage *output = new CCECAdapterMessage(data); |
| 226 | |
| 227 | /* set the number of retries */ |
| 228 | if (data.opcode == CEC_OPCODE_NONE) //TODO |
| 229 | output->maxTries = 1; |
| 230 | else if (data.initiator != CECDEVICE_BROADCAST) |
| 231 | output->maxTries = iMaxTries; |
| 232 | |
| 233 | output->lineTimeout = iLineTimeout; |
| 234 | output->retryTimeout = iRetryLineTimeout; |
| 235 | output->tries = 0; |
| 236 | |
| 237 | bool bRetry(true); |
| 238 | while (bRetry && ++output->tries < output->maxTries) |
| 239 | { |
| 240 | bRetry = (!Write(output) || output->NeedsRetry()) && output->transmit_timeout > 0; |
| 241 | if (bRetry) |
| 242 | Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT); |
| 243 | } |
| 244 | retVal = output->state; |
| 245 | |
| 246 | delete output; |
| 247 | return retVal; |
| 248 | } |
| 249 | |
| 250 | bool CUSBCECAdapterCommunication::Write(CCECAdapterMessage *data) |
| 251 | { |
| 252 | data->state = ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT; |
| 253 | m_outBuffer.Push(data); |
| 254 | data->event.Wait(5000); |
| 255 | |
| 256 | if ((data->expectControllerAck && data->state != ADAPTER_MESSAGE_STATE_SENT_ACKED) || |
| 257 | (!data->expectControllerAck && data->state != ADAPTER_MESSAGE_STATE_SENT)) |
| 258 | { |
| 259 | CLibCEC::AddLog(CEC_LOG_DEBUG, "command was not %s", data->state == ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED ? "acked" : "sent"); |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | return true; |
| 264 | } |
| 265 | |
| 266 | bool CUSBCECAdapterCommunication::Read(cec_command &command, uint32_t iTimeout) |
| 267 | { |
| 268 | if (!IsRunning()) |
| 269 | return false; |
| 270 | |
| 271 | CCECAdapterMessage msg; |
| 272 | if (Read(msg, iTimeout)) |
| 273 | { |
| 274 | if (ParseMessage(msg)) |
| 275 | { |
| 276 | command = m_currentframe; |
| 277 | m_currentframe.Clear(); |
| 278 | return true; |
| 279 | } |
| 280 | } |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | bool CUSBCECAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout) |
| 285 | { |
| 286 | CLockObject lock(m_mutex); |
| 287 | |
| 288 | msg.Clear(); |
| 289 | CCECAdapterMessage *buf(NULL); |
| 290 | |
| 291 | if (!m_inBuffer.Pop(buf)) |
| 292 | { |
| 293 | if (iTimeout == 0 || !m_rcvCondition.Wait(m_mutex, m_bHasData, iTimeout)) |
| 294 | return false; |
| 295 | m_inBuffer.Pop(buf); |
| 296 | m_bHasData = m_inBuffer.Size() > 0; |
| 297 | } |
| 298 | |
| 299 | if (buf) |
| 300 | { |
| 301 | msg.packet = buf->packet; |
| 302 | msg.state = ADAPTER_MESSAGE_STATE_INCOMING; |
| 303 | delete buf; |
| 304 | return true; |
| 305 | } |
| 306 | return false; |
| 307 | } |
| 308 | |
| 309 | CStdString CUSBCECAdapterCommunication::GetError(void) const |
| 310 | { |
| 311 | CStdString strError; |
| 312 | strError = m_port->GetError(); |
| 313 | return strError; |
| 314 | } |
| 315 | |
| 316 | bool CUSBCECAdapterCommunication::StartBootloader(void) |
| 317 | { |
| 318 | bool bReturn(false); |
| 319 | if (!IsRunning()) |
| 320 | return bReturn; |
| 321 | |
| 322 | CLibCEC::AddLog(CEC_LOG_DEBUG, "starting the bootloader"); |
| 323 | CCECAdapterMessage *output = new CCECAdapterMessage; |
| 324 | |
| 325 | output->PushBack(MSGSTART); |
| 326 | output->PushEscaped(MSGCODE_START_BOOTLOADER); |
| 327 | output->PushBack(MSGEND); |
| 328 | output->isTransmission = false; |
| 329 | output->expectControllerAck = false; |
| 330 | |
| 331 | if ((bReturn = Write(output)) == false) |
| 332 | CLibCEC::AddLog(CEC_LOG_ERROR, "could not start the bootloader"); |
| 333 | delete output; |
| 334 | |
| 335 | return bReturn; |
| 336 | } |
| 337 | |
| 338 | bool CUSBCECAdapterCommunication::PingAdapter(void) |
| 339 | { |
| 340 | bool bReturn(false); |
| 341 | if (!IsRunning()) |
| 342 | return bReturn; |
| 343 | |
| 344 | CLibCEC::AddLog(CEC_LOG_DEBUG, "sending ping"); |
| 345 | CCECAdapterMessage *output = new CCECAdapterMessage; |
| 346 | |
| 347 | output->PushBack(MSGSTART); |
| 348 | output->PushEscaped(MSGCODE_PING); |
| 349 | output->PushBack(MSGEND); |
| 350 | output->isTransmission = false; |
| 351 | |
| 352 | if ((bReturn = Write(output)) == false) |
| 353 | CLibCEC::AddLog(CEC_LOG_ERROR, "could not ping the adapter"); |
| 354 | delete output; |
| 355 | |
| 356 | return bReturn; |
| 357 | } |
| 358 | |
| 359 | bool CUSBCECAdapterCommunication::ParseMessage(const CCECAdapterMessage &msg) |
| 360 | { |
| 361 | bool bEom(false); |
| 362 | bool bIsError(msg.IsError()); |
| 363 | |
| 364 | if (msg.IsEmpty()) |
| 365 | return bEom; |
| 366 | |
| 367 | switch(msg.Message()) |
| 368 | { |
| 369 | case MSGCODE_FRAME_START: |
| 370 | { |
| 371 | m_currentframe.Clear(); |
| 372 | if (msg.Size() >= 2) |
| 373 | { |
| 374 | m_currentframe.initiator = msg.Initiator(); |
| 375 | m_currentframe.destination = msg.Destination(); |
| 376 | m_currentframe.ack = msg.IsACK(); |
| 377 | m_currentframe.eom = msg.IsEOM(); |
| 378 | } |
| 379 | if (m_currentframe.ack == 0x1) |
| 380 | { |
| 381 | m_lastInitiator = m_currentframe.initiator; |
| 382 | m_processor->HandlePoll(m_currentframe.initiator, m_currentframe.destination); |
| 383 | } |
| 384 | } |
| 385 | break; |
| 386 | case MSGCODE_RECEIVE_FAILED: |
| 387 | { |
| 388 | m_currentframe.Clear(); |
| 389 | if (m_lastInitiator != CECDEVICE_UNKNOWN) |
| 390 | bIsError = m_processor->HandleReceiveFailed(m_lastInitiator); |
| 391 | } |
| 392 | break; |
| 393 | case MSGCODE_FRAME_DATA: |
| 394 | { |
| 395 | if (msg.Size() >= 2) |
| 396 | { |
| 397 | m_currentframe.PushBack(msg[1]); |
| 398 | m_currentframe.eom = msg.IsEOM(); |
| 399 | } |
| 400 | bEom = msg.IsEOM(); |
| 401 | } |
| 402 | break; |
| 403 | default: |
| 404 | break; |
| 405 | } |
| 406 | |
| 407 | CLibCEC::AddLog(bIsError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString()); |
| 408 | return bEom; |
| 409 | } |
| 410 | |
| 411 | uint16_t CUSBCECAdapterCommunication::GetFirmwareVersion(void) |
| 412 | { |
| 413 | uint16_t iReturn(m_iFirmwareVersion); |
| 414 | if (!IsRunning()) |
| 415 | return iReturn; |
| 416 | |
| 417 | if (iReturn == CEC_FW_VERSION_UNKNOWN) |
| 418 | { |
| 419 | CLockObject lock(m_mutex); |
| 420 | CLibCEC::AddLog(CEC_LOG_DEBUG, "requesting the firmware version"); |
| 421 | CCECAdapterMessage *output = new CCECAdapterMessage; |
| 422 | |
| 423 | output->PushBack(MSGSTART); |
| 424 | output->PushEscaped(MSGCODE_FIRMWARE_VERSION); |
| 425 | output->PushBack(MSGEND); |
| 426 | output->isTransmission = false; |
| 427 | output->expectControllerAck = false; |
| 428 | |
| 429 | SendMessageToAdapter(output); |
| 430 | bool bWriteOk = output->state == ADAPTER_MESSAGE_STATE_SENT; |
| 431 | delete output; |
| 432 | if (!bWriteOk) |
| 433 | { |
| 434 | CLibCEC::AddLog(CEC_LOG_ERROR, "could not request the firmware version"); |
| 435 | return iReturn; |
| 436 | } |
| 437 | |
| 438 | Sleep(250); // TODO ReadFromDevice() isn't waiting for the timeout to pass on win32 |
| 439 | ReadFromDevice(CEC_DEFAULT_TRANSMIT_WAIT, 5 /* start + msgcode + 2 bytes for fw version + end */); |
| 440 | CCECAdapterMessage input; |
| 441 | if (Read(input, 0)) |
| 442 | { |
| 443 | if (input.Message() != MSGCODE_FIRMWARE_VERSION || input.Size() != 3) |
| 444 | CLibCEC::AddLog(CEC_LOG_ERROR, "invalid firmware version (size = %d, message = %d)", input.Size(), input.Message()); |
| 445 | else |
| 446 | { |
| 447 | m_iFirmwareVersion = (input[1] << 8 | input[2]); |
| 448 | iReturn = m_iFirmwareVersion; |
| 449 | } |
| 450 | } |
| 451 | else |
| 452 | { |
| 453 | CLibCEC::AddLog(CEC_LOG_ERROR, "no firmware version received"); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | return iReturn; |
| 458 | } |
| 459 | |
| 460 | bool CUSBCECAdapterCommunication::SetLineTimeout(uint8_t iTimeout) |
| 461 | { |
| 462 | m_iLineTimeout = iTimeout; |
| 463 | return true; |
| 464 | //TODO |
| 465 | // bool bReturn(m_iLineTimeout != iTimeout); |
| 466 | // |
| 467 | // if (!bReturn) |
| 468 | // { |
| 469 | // CCECAdapterMessage *output = new CCECAdapterMessage; |
| 470 | // |
| 471 | // output->PushBack(MSGSTART); |
| 472 | // output->PushEscaped(MSGCODE_TRANSMIT_IDLETIME); |
| 473 | // output->PushEscaped(iTimeout); |
| 474 | // output->PushBack(MSGEND); |
| 475 | // output->isTransmission = false; |
| 476 | // |
| 477 | // if ((bReturn = Write(output)) == false) |
| 478 | // CLibCEC::AddLog(CEC_LOG_ERROR, "could not set the idletime"); |
| 479 | // delete output; |
| 480 | // } |
| 481 | // |
| 482 | // return bReturn; |
| 483 | } |
| 484 | |
| 485 | bool CUSBCECAdapterCommunication::SetAckMask(uint16_t iMask) |
| 486 | { |
| 487 | return SetAckMaskInternal(iMask, false); |
| 488 | } |
| 489 | |
| 490 | bool CUSBCECAdapterCommunication::SetAckMaskInternal(uint16_t iMask, bool bWriteDirectly /* = false */) |
| 491 | { |
| 492 | bool bReturn(false); |
| 493 | CLibCEC::AddLog(CEC_LOG_DEBUG, "setting ackmask to %2x", iMask); |
| 494 | |
| 495 | CCECAdapterMessage *output = new CCECAdapterMessage; |
| 496 | |
| 497 | output->PushBack(MSGSTART); |
| 498 | output->PushEscaped(MSGCODE_SET_ACK_MASK); |
| 499 | output->PushEscaped(iMask >> 8); |
| 500 | output->PushEscaped((uint8_t)iMask); |
| 501 | output->PushBack(MSGEND); |
| 502 | output->isTransmission = false; |
| 503 | |
| 504 | if (bWriteDirectly) |
| 505 | SendMessageToAdapter(output); |
| 506 | else if ((bReturn = Write(output)) == false) |
| 507 | CLibCEC::AddLog(CEC_LOG_ERROR, "could not set the ackmask"); |
| 508 | delete output; |
| 509 | |
| 510 | return bReturn; |
| 511 | } |
| 512 | |
| 513 | |
| 514 | bool CUSBCECAdapterCommunication::SetControlledMode(bool controlled) |
| 515 | { |
| 516 | bool bReturn(false); |
| 517 | CLibCEC::AddLog(CEC_LOG_DEBUG, "turning controlled mode %s", controlled ? "on" : "off"); |
| 518 | |
| 519 | CCECAdapterMessage *output = new CCECAdapterMessage; |
| 520 | |
| 521 | output->PushBack(MSGSTART); |
| 522 | output->PushEscaped(MSGCODE_SET_CONTROLLED); |
| 523 | output->PushEscaped(controlled); |
| 524 | output->PushBack(MSGEND); |
| 525 | output->isTransmission = false; |
| 526 | |
| 527 | if ((bReturn = Write(output)) == false) |
| 528 | CLibCEC::AddLog(CEC_LOG_ERROR, "could not set controlled mode"); |
| 529 | delete output; |
| 530 | |
| 531 | return bReturn; |
| 532 | } |
| 533 | |
| 534 | bool CUSBCECAdapterCommunication::IsOpen(void) |
| 535 | { |
| 536 | return !IsStopped() && m_port->IsOpen() && IsRunning(); |
| 537 | } |
| 538 | |
| 539 | bool CUSBCECAdapterCommunication::WaitForAck(CCECAdapterMessage &message) |
| 540 | { |
| 541 | bool bError(false); |
| 542 | bool bTransmitSucceeded(false); |
| 543 | uint8_t iPacketsLeft(message.Size() / 4); |
| 544 | |
| 545 | int64_t iNow = GetTimeMs(); |
| 546 | int64_t iTargetTime = iNow + (message.transmit_timeout <= 5 ? CEC_DEFAULT_TRANSMIT_WAIT : message.transmit_timeout); |
| 547 | |
| 548 | while (!bTransmitSucceeded && !bError && iNow < iTargetTime) |
| 549 | { |
| 550 | ReadFromDevice(50); |
| 551 | CCECAdapterMessage msg; |
| 552 | if (!Read(msg, 0)) |
| 553 | { |
| 554 | iNow = GetTimeMs(); |
| 555 | continue; |
| 556 | } |
| 557 | |
| 558 | if (msg.Message() == MSGCODE_FRAME_START && msg.IsACK()) |
| 559 | { |
| 560 | m_processor->HandlePoll(msg.Initiator(), msg.Destination()); |
| 561 | m_lastInitiator = msg.Initiator(); |
| 562 | iNow = GetTimeMs(); |
| 563 | continue; |
| 564 | } |
| 565 | |
| 566 | if (msg.Message() == MSGCODE_RECEIVE_FAILED && |
| 567 | m_lastInitiator != CECDEVICE_UNKNOWN && |
| 568 | m_processor->HandleReceiveFailed(m_lastInitiator)) |
| 569 | { |
| 570 | iNow = GetTimeMs(); |
| 571 | continue; |
| 572 | } |
| 573 | |
| 574 | bError = msg.IsError(); |
| 575 | if (bError) |
| 576 | { |
| 577 | message.reply = msg.Message(); |
| 578 | CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString()); |
| 579 | } |
| 580 | else |
| 581 | { |
| 582 | switch(msg.Message()) |
| 583 | { |
| 584 | case MSGCODE_COMMAND_ACCEPTED: |
| 585 | CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString()); |
| 586 | if (iPacketsLeft > 0) |
| 587 | iPacketsLeft--; |
| 588 | if (!message.isTransmission && iPacketsLeft == 0) |
| 589 | bTransmitSucceeded = true; |
| 590 | break; |
| 591 | case MSGCODE_TRANSMIT_SUCCEEDED: |
| 592 | CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString()); |
| 593 | bTransmitSucceeded = (iPacketsLeft == 0); |
| 594 | bError = !bTransmitSucceeded; |
| 595 | message.reply = MSGCODE_TRANSMIT_SUCCEEDED; |
| 596 | break; |
| 597 | default: |
| 598 | // ignore other data while waiting |
| 599 | break; |
| 600 | } |
| 601 | |
| 602 | iNow = GetTimeMs(); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | message.state = bTransmitSucceeded && !bError ? |
| 607 | ADAPTER_MESSAGE_STATE_SENT_ACKED : |
| 608 | ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED; |
| 609 | |
| 610 | return bTransmitSucceeded && !bError; |
| 611 | } |
| 612 | |
| 613 | void CUSBCECAdapterCommunication::AddData(uint8_t *data, size_t iLen) |
| 614 | { |
| 615 | CLockObject lock(m_mutex); |
| 616 | for (size_t iPtr = 0; iPtr < iLen; iPtr++) |
| 617 | { |
| 618 | if (!m_bGotStart) |
| 619 | { |
| 620 | if (data[iPtr] == MSGSTART) |
| 621 | m_bGotStart = true; |
| 622 | } |
| 623 | else if (data[iPtr] == MSGSTART) //we found a msgstart before msgend, this is not right, remove |
| 624 | { |
| 625 | if (m_currentAdapterMessage.Size() > 0) |
| 626 | CLibCEC::AddLog(CEC_LOG_WARNING, "received MSGSTART before MSGEND, removing previous buffer contents"); |
| 627 | m_currentAdapterMessage.Clear(); |
| 628 | m_bGotStart = true; |
| 629 | } |
| 630 | else if (data[iPtr] == MSGEND) |
| 631 | { |
| 632 | CCECAdapterMessage *newMessage = new CCECAdapterMessage; |
| 633 | newMessage->packet = m_currentAdapterMessage.packet; |
| 634 | m_inBuffer.Push(newMessage); |
| 635 | m_currentAdapterMessage.Clear(); |
| 636 | m_bGotStart = false; |
| 637 | m_bNextIsEscaped = false; |
| 638 | m_bHasData = true; |
| 639 | m_rcvCondition.Signal(); |
| 640 | } |
| 641 | else if (m_bNextIsEscaped) |
| 642 | { |
| 643 | m_currentAdapterMessage.PushBack(data[iPtr] + (uint8_t)ESCOFFSET); |
| 644 | m_bNextIsEscaped = false; |
| 645 | } |
| 646 | else if (data[iPtr] == MSGESC) |
| 647 | { |
| 648 | m_bNextIsEscaped = true; |
| 649 | } |
| 650 | else |
| 651 | { |
| 652 | m_currentAdapterMessage.PushBack(data[iPtr]); |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | bool CUSBCECAdapterCommunication::ReadFromDevice(uint32_t iTimeout, size_t iSize /* = 256 */) |
| 658 | { |
| 659 | ssize_t iBytesRead; |
| 660 | uint8_t buff[256]; |
| 661 | if (!m_port) |
| 662 | return false; |
| 663 | if (iSize > 256) |
| 664 | iSize = 256; |
| 665 | |
| 666 | CLockObject lock(m_mutex); |
| 667 | iBytesRead = m_port->Read(buff, sizeof(uint8_t) * iSize, iTimeout); |
| 668 | if (iBytesRead < 0 || iBytesRead > 256) |
| 669 | { |
| 670 | CLibCEC::AddLog(CEC_LOG_ERROR, "error reading from serial port: %s", m_port->GetError().c_str()); |
| 671 | StopThread(false); |
| 672 | return false; |
| 673 | } |
| 674 | else if (iBytesRead > 0) |
| 675 | { |
| 676 | AddData(buff, iBytesRead); |
| 677 | } |
| 678 | |
| 679 | return iBytesRead > 0; |
| 680 | } |
| 681 | |
| 682 | void CUSBCECAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage *msg) |
| 683 | { |
| 684 | CLockObject adapterLock(m_mutex); |
| 685 | if (!m_port->IsOpen()) |
| 686 | { |
| 687 | CLibCEC::AddLog(CEC_LOG_ERROR, "error writing to serial port: the connection is closed"); |
| 688 | msg->state = ADAPTER_MESSAGE_STATE_ERROR; |
| 689 | return; |
| 690 | } |
| 691 | |
| 692 | if (msg->tries == 1) |
| 693 | SetLineTimeout(msg->lineTimeout); |
| 694 | else |
| 695 | SetLineTimeout(msg->retryTimeout); |
| 696 | |
| 697 | if (m_port->Write(msg->packet.data, msg->Size()) != (ssize_t) msg->Size()) |
| 698 | { |
| 699 | CLibCEC::AddLog(CEC_LOG_ERROR, "error writing to serial port: %s", m_port->GetError().c_str()); |
| 700 | msg->state = ADAPTER_MESSAGE_STATE_ERROR; |
| 701 | } |
| 702 | else |
| 703 | { |
| 704 | CLibCEC::AddLog(CEC_LOG_DEBUG, "command sent"); |
| 705 | msg->state = ADAPTER_MESSAGE_STATE_SENT; |
| 706 | |
| 707 | if (msg->expectControllerAck) |
| 708 | { |
| 709 | if (!WaitForAck(*msg)) |
| 710 | CLibCEC::AddLog(CEC_LOG_DEBUG, "did not receive ack"); |
| 711 | } |
| 712 | } |
| 713 | msg->event.Signal(); |
| 714 | } |
| 715 | |
| 716 | void CUSBCECAdapterCommunication::WriteNextCommand(void) |
| 717 | { |
| 718 | CCECAdapterMessage *msg(NULL); |
| 719 | if (m_outBuffer.Pop(msg)) |
| 720 | SendMessageToAdapter(msg); |
| 721 | } |
| 722 | |
| 723 | CStdString CUSBCECAdapterCommunication::GetPortName(void) |
| 724 | { |
| 725 | CStdString strName; |
| 726 | strName = m_port->GetName(); |
| 727 | return strName; |
| 728 | } |