| 1 | /* |
| 2 | * This file is part of the libCEC(R) library. |
| 3 | * |
| 4 | * libCEC(R) is Copyright (C) 2011 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 "AdapterCommunication.h" |
| 34 | |
| 35 | #include "AdapterMessage.h" |
| 36 | #include "../CECProcessor.h" |
| 37 | #include "../platform/sockets/serialport.h" |
| 38 | #include "../platform/util/timeutils.h" |
| 39 | #include "../LibCEC.h" |
| 40 | |
| 41 | using namespace std; |
| 42 | using namespace CEC; |
| 43 | using namespace PLATFORM; |
| 44 | |
| 45 | CAdapterCommunication::CAdapterCommunication(CCECProcessor *processor) : |
| 46 | m_port(NULL), |
| 47 | m_processor(processor), |
| 48 | m_iLineTimeout(0), |
| 49 | m_iFirmwareVersion(CEC_FW_VERSION_UNKNOWN) |
| 50 | { |
| 51 | m_port = new PLATFORM::CSerialPort; |
| 52 | } |
| 53 | |
| 54 | CAdapterCommunication::~CAdapterCommunication(void) |
| 55 | { |
| 56 | Close(); |
| 57 | |
| 58 | if (m_port) |
| 59 | { |
| 60 | delete m_port; |
| 61 | m_port = NULL; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */) |
| 66 | { |
| 67 | uint64_t iNow = GetTimeMs(); |
| 68 | uint64_t iTimeout = iNow + iTimeoutMs; |
| 69 | |
| 70 | CLockObject lock(m_mutex); |
| 71 | |
| 72 | if (!m_port) |
| 73 | { |
| 74 | CLibCEC::AddLog(CEC_LOG_ERROR, "port is NULL"); |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | if (IsOpen()) |
| 79 | { |
| 80 | CLibCEC::AddLog(CEC_LOG_ERROR, "port is already open"); |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | CStdString strError; |
| 85 | bool bConnected(false); |
| 86 | while (!bConnected && iNow < iTimeout) |
| 87 | { |
| 88 | if ((bConnected = m_port->Open(strPort, iBaudRate)) == false) |
| 89 | { |
| 90 | strError.Format("error opening serial port '%s': %s", strPort, m_port->GetError().c_str()); |
| 91 | Sleep(250); |
| 92 | iNow = GetTimeMs(); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | if (!bConnected) |
| 97 | { |
| 98 | CLibCEC::AddLog(CEC_LOG_ERROR, strError); |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | CLibCEC::AddLog(CEC_LOG_DEBUG, "connection opened, clearing any previous input and waiting for active transmissions to end before starting"); |
| 103 | |
| 104 | //clear any input bytes |
| 105 | uint8_t buff[1024]; |
| 106 | while (m_port->Read(buff, 1024, 100) > 0) |
| 107 | { |
| 108 | CLibCEC::AddLog(CEC_LOG_DEBUG, "data received, clearing it"); |
| 109 | Sleep(250); |
| 110 | } |
| 111 | |
| 112 | if (CreateThread()) |
| 113 | { |
| 114 | CLibCEC::AddLog(CEC_LOG_DEBUG, "communication thread started"); |
| 115 | return true; |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | CLibCEC::AddLog(CEC_LOG_ERROR, "could not create a communication thread"); |
| 120 | } |
| 121 | |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | void CAdapterCommunication::Close(void) |
| 126 | { |
| 127 | CLockObject lock(m_mutex); |
| 128 | m_rcvCondition.Broadcast(); |
| 129 | StopThread(); |
| 130 | } |
| 131 | |
| 132 | void *CAdapterCommunication::Process(void) |
| 133 | { |
| 134 | while (!IsStopped()) |
| 135 | { |
| 136 | ReadFromDevice(50); |
| 137 | Sleep(5); |
| 138 | WriteNextCommand(); |
| 139 | } |
| 140 | |
| 141 | CCECAdapterMessage *msg(NULL); |
| 142 | if (m_outBuffer.Pop(msg)) |
| 143 | msg->condition.Broadcast(); |
| 144 | |
| 145 | return NULL; |
| 146 | } |
| 147 | |
| 148 | bool CAdapterCommunication::Write(CCECAdapterMessage *data) |
| 149 | { |
| 150 | bool bReturn(false); |
| 151 | |
| 152 | CLockObject lock(data->mutex); |
| 153 | data->state = ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT; |
| 154 | m_outBuffer.Push(data); |
| 155 | data->condition.Wait(data->mutex); |
| 156 | |
| 157 | if (data->state != ADAPTER_MESSAGE_STATE_SENT) |
| 158 | { |
| 159 | CLibCEC::AddLog(CEC_LOG_ERROR, "command was not sent"); |
| 160 | } |
| 161 | else if (data->expectControllerAck) |
| 162 | { |
| 163 | bReturn = WaitForAck(*data); |
| 164 | if (bReturn) |
| 165 | { |
| 166 | if (data->isTransmission) |
| 167 | data->state = ADAPTER_MESSAGE_STATE_SENT_ACKED; |
| 168 | } |
| 169 | else |
| 170 | { |
| 171 | data->state = ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED; |
| 172 | CLibCEC::AddLog(CEC_LOG_DEBUG, "did not receive ack"); |
| 173 | } |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | bReturn = true; |
| 178 | } |
| 179 | |
| 180 | return bReturn; |
| 181 | } |
| 182 | |
| 183 | bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout) |
| 184 | { |
| 185 | CLockObject lock(m_mutex); |
| 186 | |
| 187 | msg.Clear(); |
| 188 | uint64_t iNow = GetTimeMs(); |
| 189 | uint64_t iTarget = iNow + iTimeout; |
| 190 | bool bGotFullMessage(false); |
| 191 | bool bNextIsEscaped(false); |
| 192 | bool bGotStart(false); |
| 193 | |
| 194 | while(!bGotFullMessage && iNow < iTarget) |
| 195 | { |
| 196 | uint8_t buf = 0; |
| 197 | if (!m_inBuffer.Pop(buf)) |
| 198 | { |
| 199 | if (!m_rcvCondition.Wait(m_mutex, (uint32_t) (iTarget - iNow))) |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | if (!bGotStart) |
| 204 | { |
| 205 | if (buf == MSGSTART) |
| 206 | bGotStart = true; |
| 207 | continue; |
| 208 | } |
| 209 | else if (buf == MSGSTART) //we found a msgstart before msgend, this is not right, remove |
| 210 | { |
| 211 | if (msg.Size() > 0) |
| 212 | CLibCEC::AddLog(CEC_LOG_WARNING, "received MSGSTART before MSGEND, removing previous buffer contents"); |
| 213 | msg.Clear(); |
| 214 | bGotStart = true; |
| 215 | } |
| 216 | |
| 217 | if (buf == MSGEND) |
| 218 | { |
| 219 | bGotFullMessage = true; |
| 220 | } |
| 221 | else if (bNextIsEscaped) |
| 222 | { |
| 223 | msg.PushBack(buf + (uint8_t)ESCOFFSET); |
| 224 | bNextIsEscaped = false; |
| 225 | } |
| 226 | else if (buf == MSGESC) |
| 227 | bNextIsEscaped = true; |
| 228 | else |
| 229 | msg.PushBack(buf); |
| 230 | } |
| 231 | |
| 232 | if (bGotFullMessage) |
| 233 | msg.state = ADAPTER_MESSAGE_STATE_INCOMING; |
| 234 | |
| 235 | return bGotFullMessage; |
| 236 | } |
| 237 | |
| 238 | CStdString CAdapterCommunication::GetError(void) const |
| 239 | { |
| 240 | CStdString strError; |
| 241 | strError = m_port->GetError(); |
| 242 | return strError; |
| 243 | } |
| 244 | |
| 245 | bool CAdapterCommunication::StartBootloader(void) |
| 246 | { |
| 247 | bool bReturn(false); |
| 248 | if (!IsRunning()) |
| 249 | return bReturn; |
| 250 | |
| 251 | CLibCEC::AddLog(CEC_LOG_DEBUG, "starting the bootloader"); |
| 252 | CCECAdapterMessage *output = new CCECAdapterMessage; |
| 253 | |
| 254 | output->PushBack(MSGSTART); |
| 255 | output->PushEscaped(MSGCODE_START_BOOTLOADER); |
| 256 | output->PushBack(MSGEND); |
| 257 | output->isTransmission = false; |
| 258 | output->expectControllerAck = false; |
| 259 | |
| 260 | if ((bReturn = Write(output)) == false) |
| 261 | CLibCEC::AddLog(CEC_LOG_ERROR, "could not start the bootloader"); |
| 262 | delete output; |
| 263 | |
| 264 | return bReturn; |
| 265 | } |
| 266 | |
| 267 | bool CAdapterCommunication::PingAdapter(void) |
| 268 | { |
| 269 | bool bReturn(false); |
| 270 | if (!IsRunning()) |
| 271 | return bReturn; |
| 272 | |
| 273 | CLibCEC::AddLog(CEC_LOG_DEBUG, "sending ping"); |
| 274 | CCECAdapterMessage *output = new CCECAdapterMessage; |
| 275 | |
| 276 | output->PushBack(MSGSTART); |
| 277 | output->PushEscaped(MSGCODE_PING); |
| 278 | output->PushBack(MSGEND); |
| 279 | output->isTransmission = false; |
| 280 | |
| 281 | if ((bReturn = Write(output)) == false) |
| 282 | CLibCEC::AddLog(CEC_LOG_ERROR, "could not ping the adapter"); |
| 283 | delete output; |
| 284 | |
| 285 | return bReturn; |
| 286 | } |
| 287 | |
| 288 | uint16_t CAdapterCommunication::GetFirmwareVersion(void) |
| 289 | { |
| 290 | uint16_t iReturn(m_iFirmwareVersion); |
| 291 | if (!IsRunning()) |
| 292 | return iReturn; |
| 293 | |
| 294 | if (iReturn == CEC_FW_VERSION_UNKNOWN) |
| 295 | { |
| 296 | CLibCEC::AddLog(CEC_LOG_DEBUG, "requesting the firmware version"); |
| 297 | CCECAdapterMessage *output = new CCECAdapterMessage; |
| 298 | |
| 299 | output->PushBack(MSGSTART); |
| 300 | output->PushEscaped(MSGCODE_FIRMWARE_VERSION); |
| 301 | output->PushBack(MSGEND); |
| 302 | output->isTransmission = false; |
| 303 | output->expectControllerAck = false; |
| 304 | |
| 305 | SendMessageToAdapter(output); |
| 306 | delete output; |
| 307 | |
| 308 | CCECAdapterMessage input; |
| 309 | if (!Read(input, CEC_DEFAULT_TRANSMIT_WAIT) || input.Message() != MSGCODE_FIRMWARE_VERSION || input.Size() != 3) |
| 310 | CLibCEC::AddLog(CEC_LOG_ERROR, "no or invalid firmware version (size = %d, message = %d)", input.Size(), input.Message()); |
| 311 | else |
| 312 | { |
| 313 | m_iFirmwareVersion = (input[1] << 8 | input[2]); |
| 314 | iReturn = m_iFirmwareVersion; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | return iReturn; |
| 319 | } |
| 320 | |
| 321 | bool CAdapterCommunication::SetLineTimeout(uint8_t iTimeout) |
| 322 | { |
| 323 | bool bReturn(m_iLineTimeout != iTimeout); |
| 324 | |
| 325 | if (!bReturn) |
| 326 | { |
| 327 | CCECAdapterMessage *output = new CCECAdapterMessage; |
| 328 | |
| 329 | output->PushBack(MSGSTART); |
| 330 | output->PushEscaped(MSGCODE_TRANSMIT_IDLETIME); |
| 331 | output->PushEscaped(iTimeout); |
| 332 | output->PushBack(MSGEND); |
| 333 | output->isTransmission = false; |
| 334 | |
| 335 | if ((bReturn = Write(output)) == false) |
| 336 | CLibCEC::AddLog(CEC_LOG_ERROR, "could not set the idletime"); |
| 337 | delete output; |
| 338 | } |
| 339 | |
| 340 | return bReturn; |
| 341 | } |
| 342 | |
| 343 | bool CAdapterCommunication::SetAckMask(uint16_t iMask) |
| 344 | { |
| 345 | bool bReturn(false); |
| 346 | CStdString strLog; |
| 347 | strLog.Format("setting ackmask to %2x", iMask); |
| 348 | CLibCEC::AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
| 349 | |
| 350 | CCECAdapterMessage *output = new CCECAdapterMessage; |
| 351 | |
| 352 | output->PushBack(MSGSTART); |
| 353 | output->PushEscaped(MSGCODE_SET_ACK_MASK); |
| 354 | output->PushEscaped(iMask >> 8); |
| 355 | output->PushEscaped((uint8_t)iMask); |
| 356 | output->PushBack(MSGEND); |
| 357 | output->isTransmission = false; |
| 358 | |
| 359 | if ((bReturn = Write(output)) == false) |
| 360 | CLibCEC::AddLog(CEC_LOG_ERROR, "could not set the ackmask"); |
| 361 | delete output; |
| 362 | |
| 363 | return bReturn; |
| 364 | } |
| 365 | |
| 366 | bool CAdapterCommunication::IsOpen(void) |
| 367 | { |
| 368 | return !IsStopped() && m_port->IsOpen() && IsRunning(); |
| 369 | } |
| 370 | |
| 371 | bool CAdapterCommunication::WaitForAck(CCECAdapterMessage &message) |
| 372 | { |
| 373 | bool bError(false); |
| 374 | bool bTransmitSucceeded(false); |
| 375 | uint8_t iPacketsLeft(message.Size() / 4); |
| 376 | |
| 377 | int64_t iNow = GetTimeMs(); |
| 378 | int64_t iTargetTime = iNow + message.transmit_timeout; |
| 379 | |
| 380 | while (!bTransmitSucceeded && !bError && (message.transmit_timeout == 0 || iNow < iTargetTime)) |
| 381 | { |
| 382 | CCECAdapterMessage msg; |
| 383 | int32_t iWait = (int32_t)(iTargetTime - iNow); |
| 384 | if (iWait <= 5 || message.transmit_timeout <= 5) |
| 385 | iWait = CEC_DEFAULT_TRANSMIT_WAIT; |
| 386 | |
| 387 | if (!Read(msg, iWait)) |
| 388 | { |
| 389 | iNow = GetTimeMs(); |
| 390 | continue; |
| 391 | } |
| 392 | |
| 393 | if (msg.Message() == MSGCODE_FRAME_START && msg.IsACK()) |
| 394 | { |
| 395 | m_processor->HandlePoll(msg.Initiator(), msg.Destination()); |
| 396 | iNow = GetTimeMs(); |
| 397 | continue; |
| 398 | } |
| 399 | |
| 400 | if (msg.Message() == MSGCODE_RECEIVE_FAILED && |
| 401 | m_processor->HandleReceiveFailed()) |
| 402 | { |
| 403 | iNow = GetTimeMs(); |
| 404 | continue; |
| 405 | } |
| 406 | |
| 407 | bError = msg.IsError(); |
| 408 | if (bError) |
| 409 | { |
| 410 | message.reply = msg.Message(); |
| 411 | CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString()); |
| 412 | } |
| 413 | else |
| 414 | { |
| 415 | switch(msg.Message()) |
| 416 | { |
| 417 | case MSGCODE_COMMAND_ACCEPTED: |
| 418 | CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString()); |
| 419 | if (iPacketsLeft > 0) |
| 420 | iPacketsLeft--; |
| 421 | if (!message.isTransmission && iPacketsLeft == 0) |
| 422 | bTransmitSucceeded = true; |
| 423 | break; |
| 424 | case MSGCODE_TRANSMIT_SUCCEEDED: |
| 425 | CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString()); |
| 426 | bTransmitSucceeded = (iPacketsLeft == 0); |
| 427 | bError = !bTransmitSucceeded; |
| 428 | message.reply = MSGCODE_TRANSMIT_SUCCEEDED; |
| 429 | break; |
| 430 | default: |
| 431 | // ignore other data while waiting |
| 432 | break; |
| 433 | } |
| 434 | |
| 435 | iNow = GetTimeMs(); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | return bTransmitSucceeded && !bError; |
| 440 | } |
| 441 | |
| 442 | void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen) |
| 443 | { |
| 444 | CLockObject lock(m_mutex); |
| 445 | for (uint8_t iPtr = 0; iPtr < iLen; iPtr++) |
| 446 | m_inBuffer.Push(data[iPtr]); |
| 447 | |
| 448 | m_rcvCondition.Signal(); |
| 449 | } |
| 450 | |
| 451 | bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout) |
| 452 | { |
| 453 | int32_t iBytesRead; |
| 454 | uint8_t buff[1024]; |
| 455 | if (!m_port) |
| 456 | return false; |
| 457 | |
| 458 | CLockObject lock(m_mutex); |
| 459 | iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout); |
| 460 | if (iBytesRead < 0 || iBytesRead > 256) |
| 461 | { |
| 462 | CStdString strError; |
| 463 | strError.Format("error reading from serial port: %s", m_port->GetError().c_str()); |
| 464 | CLibCEC::AddLog(CEC_LOG_ERROR, strError); |
| 465 | return false; |
| 466 | } |
| 467 | else if (iBytesRead > 0) |
| 468 | AddData(buff, (uint8_t) iBytesRead); |
| 469 | |
| 470 | return iBytesRead > 0; |
| 471 | } |
| 472 | |
| 473 | void CAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage *msg) |
| 474 | { |
| 475 | CLockObject adapterLock(m_mutex); |
| 476 | CLockObject lock(msg->mutex); |
| 477 | if (m_port->Write(msg->packet.data, msg->Size()) != (int32_t) msg->Size()) |
| 478 | { |
| 479 | CStdString strError; |
| 480 | strError.Format("error writing to serial port: %s", m_port->GetError().c_str()); |
| 481 | CLibCEC::AddLog(CEC_LOG_ERROR, strError); |
| 482 | msg->state = ADAPTER_MESSAGE_STATE_ERROR; |
| 483 | } |
| 484 | else |
| 485 | { |
| 486 | CLibCEC::AddLog(CEC_LOG_DEBUG, "command sent"); |
| 487 | msg->state = ADAPTER_MESSAGE_STATE_SENT; |
| 488 | } |
| 489 | msg->condition.Signal(); |
| 490 | } |
| 491 | |
| 492 | void CAdapterCommunication::WriteNextCommand(void) |
| 493 | { |
| 494 | CCECAdapterMessage *msg(NULL); |
| 495 | if (m_outBuffer.Pop(msg)) |
| 496 | SendMessageToAdapter(msg); |
| 497 | } |