| 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 "CECProcessor.h" |
| 36 | #include "platform/serialport.h" |
| 37 | #include "util/StdString.h" |
| 38 | #include "platform/timeutils.h" |
| 39 | |
| 40 | using namespace std; |
| 41 | using namespace CEC; |
| 42 | |
| 43 | CCECAdapterMessage::CCECAdapterMessage(const cec_command &command) |
| 44 | { |
| 45 | clear(); |
| 46 | |
| 47 | //set ack polarity to high when transmitting to the broadcast address |
| 48 | //set ack polarity low when transmitting to any other address |
| 49 | push_back(MSGSTART); |
| 50 | push_escaped(MSGCODE_TRANSMIT_ACK_POLARITY); |
| 51 | if (command.destination == CECDEVICE_BROADCAST) |
| 52 | push_escaped(CEC_TRUE); |
| 53 | else |
| 54 | push_escaped(CEC_FALSE); |
| 55 | push_back(MSGEND); |
| 56 | |
| 57 | // add source and destination |
| 58 | push_back(MSGSTART); |
| 59 | push_escaped(command.opcode_set == 0 ? (uint8_t)MSGCODE_TRANSMIT_EOM : (uint8_t)MSGCODE_TRANSMIT); |
| 60 | push_back(((uint8_t)command.initiator << 4) + (uint8_t)command.destination); |
| 61 | push_back(MSGEND); |
| 62 | |
| 63 | // add opcode |
| 64 | if (command.opcode_set == 1) |
| 65 | { |
| 66 | push_back(MSGSTART); |
| 67 | push_escaped(command.parameters.IsEmpty() ? (uint8_t)MSGCODE_TRANSMIT_EOM : (uint8_t)MSGCODE_TRANSMIT); |
| 68 | push_back((uint8_t) command.opcode); |
| 69 | push_back(MSGEND); |
| 70 | |
| 71 | // add parameters |
| 72 | for (int8_t iPtr = 0; iPtr < command.parameters.size; iPtr++) |
| 73 | { |
| 74 | push_back(MSGSTART); |
| 75 | |
| 76 | if (iPtr == command.parameters.size - 1) |
| 77 | push_escaped( MSGCODE_TRANSMIT_EOM); |
| 78 | else |
| 79 | push_escaped(MSGCODE_TRANSMIT); |
| 80 | |
| 81 | push_escaped(command.parameters[iPtr]); |
| 82 | |
| 83 | push_back(MSGEND); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // set timeout |
| 88 | transmit_timeout = command.transmit_timeout; |
| 89 | } |
| 90 | |
| 91 | CCECAdapterMessage &CCECAdapterMessage::operator =(const CCECAdapterMessage &msg) |
| 92 | { |
| 93 | packet = msg.packet; |
| 94 | state = msg.state; |
| 95 | return *this; |
| 96 | } |
| 97 | |
| 98 | CStdString CCECAdapterMessage::MessageCodeAsString(void) const |
| 99 | { |
| 100 | CStdString strMsg; |
| 101 | switch (message()) |
| 102 | { |
| 103 | case MSGCODE_NOTHING: |
| 104 | strMsg = "NOTHING"; |
| 105 | break; |
| 106 | case MSGCODE_PING: |
| 107 | strMsg = "PING"; |
| 108 | break; |
| 109 | case MSGCODE_TIMEOUT_ERROR: |
| 110 | strMsg = "TIMEOUT"; |
| 111 | break; |
| 112 | case MSGCODE_HIGH_ERROR: |
| 113 | strMsg = "HIGH_ERROR"; |
| 114 | break; |
| 115 | case MSGCODE_LOW_ERROR: |
| 116 | strMsg = "LOW_ERROR"; |
| 117 | break; |
| 118 | case MSGCODE_FRAME_START: |
| 119 | strMsg = "FRAME_START"; |
| 120 | break; |
| 121 | case MSGCODE_FRAME_DATA: |
| 122 | strMsg = "FRAME_DATA"; |
| 123 | break; |
| 124 | case MSGCODE_RECEIVE_FAILED: |
| 125 | strMsg = "RECEIVE_FAILED"; |
| 126 | break; |
| 127 | case MSGCODE_COMMAND_ACCEPTED: |
| 128 | strMsg = "COMMAND_ACCEPTED"; |
| 129 | break; |
| 130 | case MSGCODE_COMMAND_REJECTED: |
| 131 | strMsg = "COMMAND_REJECTED"; |
| 132 | break; |
| 133 | case MSGCODE_SET_ACK_MASK: |
| 134 | strMsg = "SET_ACK_MASK"; |
| 135 | break; |
| 136 | case MSGCODE_TRANSMIT: |
| 137 | strMsg = "TRANSMIT"; |
| 138 | break; |
| 139 | case MSGCODE_TRANSMIT_EOM: |
| 140 | strMsg = "TRANSMIT_EOM"; |
| 141 | break; |
| 142 | case MSGCODE_TRANSMIT_IDLETIME: |
| 143 | strMsg = "TRANSMIT_IDLETIME"; |
| 144 | break; |
| 145 | case MSGCODE_TRANSMIT_ACK_POLARITY: |
| 146 | strMsg = "TRANSMIT_ACK_POLARITY"; |
| 147 | break; |
| 148 | case MSGCODE_TRANSMIT_LINE_TIMEOUT: |
| 149 | strMsg = "TRANSMIT_LINE_TIMEOUT"; |
| 150 | break; |
| 151 | case MSGCODE_TRANSMIT_SUCCEEDED: |
| 152 | strMsg = "TRANSMIT_SUCCEEDED"; |
| 153 | break; |
| 154 | case MSGCODE_TRANSMIT_FAILED_LINE: |
| 155 | strMsg = "TRANSMIT_FAILED_LINE"; |
| 156 | break; |
| 157 | case MSGCODE_TRANSMIT_FAILED_ACK: |
| 158 | strMsg = "TRANSMIT_FAILED_ACK"; |
| 159 | break; |
| 160 | case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA: |
| 161 | strMsg = "TRANSMIT_FAILED_TIMEOUT_DATA"; |
| 162 | break; |
| 163 | case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE: |
| 164 | strMsg = "TRANSMIT_FAILED_TIMEOUT_LINE"; |
| 165 | break; |
| 166 | case MSGCODE_FIRMWARE_VERSION: |
| 167 | strMsg = "FIRMWARE_VERSION"; |
| 168 | break; |
| 169 | case MSGCODE_START_BOOTLOADER: |
| 170 | strMsg = "START_BOOTLOADER"; |
| 171 | break; |
| 172 | case MSGCODE_FRAME_EOM: |
| 173 | strMsg = "FRAME_EOM"; |
| 174 | break; |
| 175 | case MSGCODE_FRAME_ACK: |
| 176 | strMsg = "FRAME_ACK"; |
| 177 | break; |
| 178 | } |
| 179 | |
| 180 | return strMsg; |
| 181 | } |
| 182 | |
| 183 | CStdString CCECAdapterMessage::ToString(void) const |
| 184 | { |
| 185 | CStdString strMsg; |
| 186 | if (size() == 0) |
| 187 | { |
| 188 | strMsg = "empty message"; |
| 189 | } |
| 190 | else |
| 191 | { |
| 192 | strMsg = MessageCodeAsString(); |
| 193 | |
| 194 | switch (message()) |
| 195 | { |
| 196 | case MSGCODE_TIMEOUT_ERROR: |
| 197 | case MSGCODE_HIGH_ERROR: |
| 198 | case MSGCODE_LOW_ERROR: |
| 199 | { |
| 200 | int iLine = (size() >= 3) ? (at(1) << 8) | at(2) : 0; |
| 201 | uint32_t iTime = (size() >= 7) ? (at(3) << 24) | (at(4) << 16) | (at(5) << 8) | at(6) : 0; |
| 202 | strMsg.AppendFormat(" line:%i", iLine); |
| 203 | strMsg.AppendFormat(" time:%u", iTime); |
| 204 | } |
| 205 | break; |
| 206 | case MSGCODE_FRAME_START: |
| 207 | if (size() >= 2) |
| 208 | strMsg.AppendFormat(" initiator:%1x destination:%1x ack:%s %s", initiator(), destination(), ack() ? "high" : "low", eom() ? "eom" : ""); |
| 209 | break; |
| 210 | case MSGCODE_FRAME_DATA: |
| 211 | if (size() >= 2) |
| 212 | strMsg.AppendFormat(" %02x %s", at(1), eom() ? "eom" : ""); |
| 213 | break; |
| 214 | default: |
| 215 | break; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | return strMsg; |
| 220 | } |
| 221 | |
| 222 | bool CCECAdapterMessage::is_error(void) const |
| 223 | { |
| 224 | cec_adapter_messagecode code = message(); |
| 225 | return (code == MSGCODE_HIGH_ERROR || |
| 226 | code == MSGCODE_LOW_ERROR || |
| 227 | code == MSGCODE_RECEIVE_FAILED || |
| 228 | code == MSGCODE_COMMAND_REJECTED || |
| 229 | code == MSGCODE_TRANSMIT_LINE_TIMEOUT || |
| 230 | code == MSGCODE_TRANSMIT_FAILED_LINE || |
| 231 | code == MSGCODE_TRANSMIT_FAILED_ACK || |
| 232 | code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA || |
| 233 | code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE); |
| 234 | } |
| 235 | |
| 236 | void CCECAdapterMessage::push_escaped(uint8_t byte) |
| 237 | { |
| 238 | if (byte >= MSGESC) |
| 239 | { |
| 240 | push_back(MSGESC); |
| 241 | push_back(byte - ESCOFFSET); |
| 242 | } |
| 243 | else |
| 244 | push_back(byte); |
| 245 | } |
| 246 | |
| 247 | CAdapterCommunication::CAdapterCommunication(CCECProcessor *processor) : |
| 248 | m_port(NULL), |
| 249 | m_processor(processor), |
| 250 | m_iLineTimeout(0) |
| 251 | { |
| 252 | m_port = new CSerialPort; |
| 253 | } |
| 254 | |
| 255 | CAdapterCommunication::~CAdapterCommunication(void) |
| 256 | { |
| 257 | Close(); |
| 258 | |
| 259 | if (m_port) |
| 260 | { |
| 261 | delete m_port; |
| 262 | m_port = NULL; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */) |
| 267 | { |
| 268 | CLockObject lock(&m_mutex); |
| 269 | if (!m_port) |
| 270 | { |
| 271 | m_processor->AddLog(CEC_LOG_ERROR, "port is NULL"); |
| 272 | return false; |
| 273 | } |
| 274 | |
| 275 | if (IsOpen()) |
| 276 | { |
| 277 | m_processor->AddLog(CEC_LOG_ERROR, "port is already open"); |
| 278 | } |
| 279 | |
| 280 | if (!m_port->Open(strPort, iBaudRate)) |
| 281 | { |
| 282 | CStdString strError; |
| 283 | strError.Format("error opening serial port '%s': %s", strPort, m_port->GetError().c_str()); |
| 284 | m_processor->AddLog(CEC_LOG_ERROR, strError); |
| 285 | return false; |
| 286 | } |
| 287 | |
| 288 | m_processor->AddLog(CEC_LOG_DEBUG, "connection opened"); |
| 289 | |
| 290 | //clear any input bytes |
| 291 | uint8_t buff[1]; |
| 292 | while (m_port->Read(buff, 1, 5) == 1) {} |
| 293 | |
| 294 | if (CreateThread()) |
| 295 | { |
| 296 | m_startCondition.Wait(&m_mutex); |
| 297 | m_processor->AddLog(CEC_LOG_DEBUG, "communication thread started"); |
| 298 | return true; |
| 299 | } |
| 300 | else |
| 301 | { |
| 302 | m_processor->AddLog(CEC_LOG_DEBUG, "could not create a communication thread"); |
| 303 | } |
| 304 | |
| 305 | //TODO implement the timeout. use the variable for now to silence the compiler warning |
| 306 | iTimeoutMs = 0; |
| 307 | |
| 308 | return false; |
| 309 | } |
| 310 | |
| 311 | void CAdapterCommunication::Close(void) |
| 312 | { |
| 313 | CLockObject lock(&m_mutex); |
| 314 | m_startCondition.Broadcast(); |
| 315 | m_rcvCondition.Broadcast(); |
| 316 | StopThread(); |
| 317 | } |
| 318 | |
| 319 | void *CAdapterCommunication::Process(void) |
| 320 | { |
| 321 | { |
| 322 | CLockObject lock(&m_mutex); |
| 323 | m_startCondition.Signal(); |
| 324 | } |
| 325 | |
| 326 | while (!IsStopped()) |
| 327 | { |
| 328 | ReadFromDevice(50); |
| 329 | Sleep(5); |
| 330 | WriteNextCommand(); |
| 331 | } |
| 332 | |
| 333 | CCECAdapterMessage *msg; |
| 334 | if (m_outBuffer.Pop(msg)) |
| 335 | msg->condition.Broadcast(); |
| 336 | |
| 337 | return NULL; |
| 338 | } |
| 339 | |
| 340 | bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout) |
| 341 | { |
| 342 | int32_t iBytesRead; |
| 343 | uint8_t buff[1024]; |
| 344 | if (!m_port) |
| 345 | return false; |
| 346 | |
| 347 | iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout); |
| 348 | if (iBytesRead < 0 || iBytesRead > 256) |
| 349 | { |
| 350 | CStdString strError; |
| 351 | strError.Format("error reading from serial port: %s", m_port->GetError().c_str()); |
| 352 | m_processor->AddLog(CEC_LOG_ERROR, strError); |
| 353 | return false; |
| 354 | } |
| 355 | else if (iBytesRead > 0) |
| 356 | AddData(buff, (uint8_t) iBytesRead); |
| 357 | |
| 358 | return iBytesRead > 0; |
| 359 | } |
| 360 | |
| 361 | void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen) |
| 362 | { |
| 363 | CLockObject lock(&m_mutex); |
| 364 | for (unsigned int iPtr = 0; iPtr < iLen; iPtr++) |
| 365 | m_inBuffer.Push(data[iPtr]); |
| 366 | |
| 367 | m_rcvCondition.Signal(); |
| 368 | } |
| 369 | |
| 370 | void CAdapterCommunication::WriteNextCommand(void) |
| 371 | { |
| 372 | CCECAdapterMessage *msg; |
| 373 | if (m_outBuffer.Pop(msg)) |
| 374 | SendMessageToAdapter(msg); |
| 375 | } |
| 376 | |
| 377 | void CAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage *msg) |
| 378 | { |
| 379 | CLockObject lock(&msg->mutex); |
| 380 | if (m_port->Write(msg) != (int32_t) msg->size()) |
| 381 | { |
| 382 | CStdString strError; |
| 383 | strError.Format("error writing to serial port: %s", m_port->GetError().c_str()); |
| 384 | m_processor->AddLog(CEC_LOG_ERROR, strError); |
| 385 | msg->state = ADAPTER_MESSAGE_STATE_ERROR; |
| 386 | } |
| 387 | else |
| 388 | { |
| 389 | m_processor->AddLog(CEC_LOG_DEBUG, "command sent"); |
| 390 | msg->state = ADAPTER_MESSAGE_STATE_SENT; |
| 391 | } |
| 392 | msg->condition.Signal(); |
| 393 | } |
| 394 | |
| 395 | bool CAdapterCommunication::Write(CCECAdapterMessage *data) |
| 396 | { |
| 397 | data->state = ADAPTER_MESSAGE_STATE_WAITING; |
| 398 | m_outBuffer.Push(data); |
| 399 | return true; |
| 400 | } |
| 401 | |
| 402 | bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout) |
| 403 | { |
| 404 | CLockObject lock(&m_mutex); |
| 405 | |
| 406 | msg.clear(); |
| 407 | uint64_t iNow = GetTimeMs(); |
| 408 | uint64_t iTarget = iNow + iTimeout; |
| 409 | bool bGotFullMessage(false); |
| 410 | bool bNextIsEscaped(false); |
| 411 | bool bGotStart(false); |
| 412 | |
| 413 | while(!bGotFullMessage && iNow < iTarget) |
| 414 | { |
| 415 | uint8_t buf = 0; |
| 416 | if (!m_inBuffer.Pop(buf)) |
| 417 | { |
| 418 | if (!m_rcvCondition.Wait(&m_mutex, (uint32_t) (iTarget - iNow))) |
| 419 | return false; |
| 420 | } |
| 421 | |
| 422 | if (!bGotStart) |
| 423 | { |
| 424 | if (buf == MSGSTART) |
| 425 | bGotStart = true; |
| 426 | continue; |
| 427 | } |
| 428 | else if (buf == MSGSTART) //we found a msgstart before msgend, this is not right, remove |
| 429 | { |
| 430 | if (msg.size() > 0) |
| 431 | m_processor->AddLog(CEC_LOG_WARNING, "received MSGSTART before MSGEND, removing previous buffer contents"); |
| 432 | msg.clear(); |
| 433 | bGotStart = true; |
| 434 | } |
| 435 | |
| 436 | if (buf == MSGEND) |
| 437 | { |
| 438 | bGotFullMessage = true; |
| 439 | } |
| 440 | else if (bNextIsEscaped) |
| 441 | { |
| 442 | msg.push_back(buf + (uint8_t)ESCOFFSET); |
| 443 | bNextIsEscaped = false; |
| 444 | } |
| 445 | else if (buf == MSGESC) |
| 446 | bNextIsEscaped = true; |
| 447 | else |
| 448 | msg.push_back(buf); |
| 449 | } |
| 450 | |
| 451 | if (bGotFullMessage) |
| 452 | msg.state = ADAPTER_MESSAGE_STATE_RECEIVED; |
| 453 | |
| 454 | return bGotFullMessage; |
| 455 | } |
| 456 | |
| 457 | std::string CAdapterCommunication::GetError(void) const |
| 458 | { |
| 459 | return m_port->GetError(); |
| 460 | } |
| 461 | |
| 462 | bool CAdapterCommunication::StartBootloader(void) |
| 463 | { |
| 464 | bool bReturn(false); |
| 465 | if (!IsRunning()) |
| 466 | return bReturn; |
| 467 | |
| 468 | m_processor->AddLog(CEC_LOG_DEBUG, "starting the bootloader"); |
| 469 | CCECAdapterMessage *output = new CCECAdapterMessage; |
| 470 | |
| 471 | output->push_back(MSGSTART); |
| 472 | output->push_escaped(MSGCODE_START_BOOTLOADER); |
| 473 | output->push_back(MSGEND); |
| 474 | |
| 475 | CLockObject lock(&output->mutex); |
| 476 | if (Write(output)) |
| 477 | output->condition.Wait(&output->mutex); |
| 478 | bReturn = output->state == ADAPTER_MESSAGE_STATE_SENT; |
| 479 | delete output; |
| 480 | |
| 481 | return bReturn; |
| 482 | } |
| 483 | |
| 484 | bool CAdapterCommunication::PingAdapter(void) |
| 485 | { |
| 486 | bool bReturn(false); |
| 487 | if (!IsRunning()) |
| 488 | return bReturn; |
| 489 | |
| 490 | m_processor->AddLog(CEC_LOG_DEBUG, "sending ping"); |
| 491 | CCECAdapterMessage *output = new CCECAdapterMessage; |
| 492 | |
| 493 | output->push_back(MSGSTART); |
| 494 | output->push_escaped(MSGCODE_PING); |
| 495 | output->push_back(MSGEND); |
| 496 | |
| 497 | CLockObject lock(&output->mutex); |
| 498 | if (Write(output)) |
| 499 | output->condition.Wait(&output->mutex); |
| 500 | bReturn = output->state == ADAPTER_MESSAGE_STATE_SENT; |
| 501 | delete output; |
| 502 | |
| 503 | return bReturn; |
| 504 | } |
| 505 | |
| 506 | bool CAdapterCommunication::SetLineTimeout(uint8_t iTimeout) |
| 507 | { |
| 508 | bool bReturn(m_iLineTimeout != iTimeout); |
| 509 | |
| 510 | if (!bReturn) |
| 511 | { |
| 512 | CCECAdapterMessage *output = new CCECAdapterMessage; |
| 513 | |
| 514 | output->push_back(MSGSTART); |
| 515 | output->push_escaped(MSGCODE_TRANSMIT_IDLETIME); |
| 516 | output->push_escaped(iTimeout); |
| 517 | output->push_back(MSGEND); |
| 518 | |
| 519 | if ((bReturn = Write(output)) == false) |
| 520 | m_processor->AddLog(CEC_LOG_ERROR, "could not set the idletime"); |
| 521 | delete output; |
| 522 | } |
| 523 | |
| 524 | return bReturn; |
| 525 | } |
| 526 | |
| 527 | bool CAdapterCommunication::IsOpen(void) const |
| 528 | { |
| 529 | return !IsStopped() && m_port->IsOpen() && IsRunning(); |
| 530 | } |