| 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 "LibCEC.h" |
| 36 | #include "platform/serialport.h" |
| 37 | #include "util/StdString.h" |
| 38 | |
| 39 | using namespace std; |
| 40 | using namespace CEC; |
| 41 | |
| 42 | CAdapterCommunication::CAdapterCommunication(CLibCEC *controller) : |
| 43 | m_port(NULL), |
| 44 | m_controller(controller), |
| 45 | m_inbuf(NULL), |
| 46 | m_iInbufSize(0), |
| 47 | m_iInbufUsed(0) |
| 48 | { |
| 49 | m_port = new CSerialPort; |
| 50 | } |
| 51 | |
| 52 | CAdapterCommunication::~CAdapterCommunication(void) |
| 53 | { |
| 54 | Close(); |
| 55 | |
| 56 | if (m_port) |
| 57 | { |
| 58 | delete m_port; |
| 59 | m_port = NULL; |
| 60 | } |
| 61 | |
| 62 | if (m_inbuf) |
| 63 | free(m_inbuf); |
| 64 | } |
| 65 | |
| 66 | bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */) |
| 67 | { |
| 68 | CLockObject lock(&m_commMutex); |
| 69 | if (!m_port) |
| 70 | { |
| 71 | m_controller->AddLog(CEC_LOG_ERROR, "port is NULL"); |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | if (IsOpen()) |
| 76 | { |
| 77 | m_controller->AddLog(CEC_LOG_ERROR, "port is already open"); |
| 78 | } |
| 79 | |
| 80 | if (!m_port->Open(strPort, iBaudRate)) |
| 81 | { |
| 82 | CStdString strError; |
| 83 | strError.Format("error opening serial port '%s': %s", strPort, m_port->GetError().c_str()); |
| 84 | m_controller->AddLog(CEC_LOG_ERROR, strError); |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | m_controller->AddLog(CEC_LOG_DEBUG, "connection opened"); |
| 89 | |
| 90 | //clear any input bytes |
| 91 | uint8_t buff[1024]; |
| 92 | m_port->Read(buff, sizeof(buff), 50); |
| 93 | |
| 94 | Sleep(CEC_SETTLE_DOWN_TIME); |
| 95 | |
| 96 | if (CreateThread()) |
| 97 | { |
| 98 | m_controller->AddLog(CEC_LOG_DEBUG, "communication thread created"); |
| 99 | return true; |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | m_controller->AddLog(CEC_LOG_DEBUG, "could not create a communication thread"); |
| 104 | } |
| 105 | |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | void CAdapterCommunication::Close(void) |
| 110 | { |
| 111 | CLockObject lock(&m_commMutex); |
| 112 | StopThread(); |
| 113 | |
| 114 | if (m_inbuf) |
| 115 | { |
| 116 | free(m_inbuf); |
| 117 | m_inbuf = NULL; |
| 118 | m_iInbufSize = 0; |
| 119 | m_iInbufUsed = 0; |
| 120 | } |
| 121 | |
| 122 | m_rcvCondition.Broadcast(); |
| 123 | } |
| 124 | |
| 125 | void *CAdapterCommunication::Process(void) |
| 126 | { |
| 127 | m_controller->AddLog(CEC_LOG_DEBUG, "communication thread started"); |
| 128 | |
| 129 | while (!IsStopped()) |
| 130 | { |
| 131 | { |
| 132 | CLockObject lock(&m_commMutex); |
| 133 | ReadFromDevice(100); |
| 134 | } |
| 135 | |
| 136 | if (!IsStopped()) |
| 137 | Sleep(5); |
| 138 | } |
| 139 | |
| 140 | return NULL; |
| 141 | } |
| 142 | |
| 143 | bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout) |
| 144 | { |
| 145 | int32_t iBytesRead; |
| 146 | uint8_t buff[1024]; |
| 147 | if (!m_port) |
| 148 | return false; |
| 149 | |
| 150 | iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout); |
| 151 | if (iBytesRead < 0 || iBytesRead > 256) |
| 152 | { |
| 153 | CStdString strError; |
| 154 | strError.Format("error reading from serial port: %s", m_port->GetError().c_str()); |
| 155 | m_controller->AddLog(CEC_LOG_ERROR, strError); |
| 156 | return false; |
| 157 | } |
| 158 | else if (iBytesRead > 0) |
| 159 | AddData(buff, (uint8_t) iBytesRead); |
| 160 | |
| 161 | return iBytesRead > 0; |
| 162 | } |
| 163 | |
| 164 | void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen) |
| 165 | { |
| 166 | CLockObject lock(&m_bufferMutex); |
| 167 | if (m_iInbufUsed + iLen > m_iInbufSize) |
| 168 | { |
| 169 | m_iInbufSize = m_iInbufUsed + iLen; |
| 170 | m_inbuf = (uint8_t*)realloc(m_inbuf, m_iInbufSize); |
| 171 | } |
| 172 | |
| 173 | memcpy(m_inbuf + m_iInbufUsed, data, iLen); |
| 174 | m_iInbufUsed += iLen; |
| 175 | |
| 176 | m_rcvCondition.Signal(); |
| 177 | } |
| 178 | |
| 179 | bool CAdapterCommunication::Write(const cec_adapter_message &data) |
| 180 | { |
| 181 | CLockObject lock(&m_commMutex); |
| 182 | if (m_port->Write(data) != (int32_t) data.size()) |
| 183 | { |
| 184 | CStdString strError; |
| 185 | strError.Format("error writing to serial port: %s", m_port->GetError().c_str()); |
| 186 | m_controller->AddLog(CEC_LOG_ERROR, strError); |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | m_controller->AddLog(CEC_LOG_DEBUG, "command sent"); |
| 191 | CCondition::Sleep((uint32_t) data.size() * (uint32_t)24 /*data*/ + (uint32_t)5 /*start bit (4.5 ms)*/); |
| 192 | |
| 193 | return true; |
| 194 | } |
| 195 | |
| 196 | bool CAdapterCommunication::Read(cec_adapter_message &msg, uint32_t iTimeout) |
| 197 | { |
| 198 | CLockObject lock(&m_bufferMutex); |
| 199 | |
| 200 | if (m_iInbufUsed < 1) |
| 201 | { |
| 202 | if (!m_rcvCondition.Wait(&m_bufferMutex, iTimeout)) |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | if (m_iInbufUsed < 1 || IsStopped()) |
| 207 | return false; |
| 208 | |
| 209 | //search for first start of message |
| 210 | int16_t startpos = -1; |
| 211 | for (int16_t iPtr = 0; iPtr < m_iInbufUsed; iPtr++) |
| 212 | { |
| 213 | if (m_inbuf[iPtr] == MSGSTART) |
| 214 | { |
| 215 | startpos = iPtr; |
| 216 | break; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | if (startpos == -1) |
| 221 | return false; |
| 222 | |
| 223 | //move anything from the first start of message to the beginning of the buffer |
| 224 | if (startpos > 0) |
| 225 | { |
| 226 | memmove(m_inbuf, m_inbuf + startpos, m_iInbufUsed - startpos); |
| 227 | m_iInbufUsed -= startpos; |
| 228 | } |
| 229 | |
| 230 | if (m_iInbufUsed < 2) |
| 231 | return false; |
| 232 | |
| 233 | //look for end of message |
| 234 | startpos = -1; |
| 235 | int16_t endpos = -1; |
| 236 | for (int16_t iPtr = 1; iPtr < m_iInbufUsed; iPtr++) |
| 237 | { |
| 238 | if (m_inbuf[iPtr] == MSGEND) |
| 239 | { |
| 240 | endpos = iPtr; |
| 241 | break; |
| 242 | } |
| 243 | else if (m_inbuf[iPtr] == MSGSTART) |
| 244 | { |
| 245 | startpos = iPtr; |
| 246 | break; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | if (startpos > 0) //we found a msgstart before msgend, this is not right, remove |
| 251 | { |
| 252 | m_controller->AddLog(CEC_LOG_ERROR, "received MSGSTART before MSGEND"); |
| 253 | memmove(m_inbuf, m_inbuf + startpos, m_iInbufUsed - startpos); |
| 254 | m_iInbufUsed -= startpos; |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | if (endpos > 0) //found a MSGEND |
| 259 | { |
| 260 | msg.clear(); |
| 261 | bool isesc = false; |
| 262 | for (int16_t iPtr = 1; iPtr < endpos; iPtr++) |
| 263 | { |
| 264 | if (isesc) |
| 265 | { |
| 266 | msg.push_back(m_inbuf[iPtr] + (uint8_t)ESCOFFSET); |
| 267 | isesc = false; |
| 268 | } |
| 269 | else if (m_inbuf[iPtr] == MSGESC) |
| 270 | { |
| 271 | isesc = true; |
| 272 | } |
| 273 | else |
| 274 | { |
| 275 | msg.push_back(m_inbuf[iPtr]); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | if (endpos + 1 < m_iInbufUsed) |
| 280 | memmove(m_inbuf, m_inbuf + endpos + 1, m_iInbufUsed - endpos - 1); |
| 281 | |
| 282 | m_iInbufUsed -= endpos + 1; |
| 283 | |
| 284 | return true; |
| 285 | } |
| 286 | |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | std::string CAdapterCommunication::GetError(void) const |
| 291 | { |
| 292 | return m_port->GetError(); |
| 293 | } |
| 294 | |
| 295 | bool CAdapterCommunication::StartBootloader(void) |
| 296 | { |
| 297 | if (!IsRunning()) |
| 298 | return false; |
| 299 | |
| 300 | m_controller->AddLog(CEC_LOG_DEBUG, "starting the bootloader"); |
| 301 | cec_adapter_message output; |
| 302 | output.clear(); |
| 303 | |
| 304 | output.push_back(MSGSTART); |
| 305 | PushEscaped(output, MSGCODE_START_BOOTLOADER); |
| 306 | output.push_back(MSGEND); |
| 307 | |
| 308 | if (!Write(output)) |
| 309 | { |
| 310 | m_controller->AddLog(CEC_LOG_ERROR, "could not start the bootloader"); |
| 311 | return false; |
| 312 | } |
| 313 | m_controller->AddLog(CEC_LOG_DEBUG, "bootloader start command transmitted"); |
| 314 | return true; |
| 315 | } |
| 316 | |
| 317 | void CAdapterCommunication::PushEscaped(cec_adapter_message &vec, uint8_t byte) |
| 318 | { |
| 319 | if (byte >= MSGESC && byte != MSGSTART) |
| 320 | { |
| 321 | vec.push_back(MSGESC); |
| 322 | vec.push_back(byte - ESCOFFSET); |
| 323 | } |
| 324 | else |
| 325 | { |
| 326 | vec.push_back(byte); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | bool CAdapterCommunication::SetAckMask(uint16_t iMask) |
| 331 | { |
| 332 | if (!IsRunning()) |
| 333 | return false; |
| 334 | |
| 335 | CStdString strLog; |
| 336 | strLog.Format("setting ackmask to %2x", iMask); |
| 337 | m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
| 338 | |
| 339 | cec_adapter_message output; |
| 340 | output.clear(); |
| 341 | |
| 342 | output.push_back(MSGSTART); |
| 343 | PushEscaped(output, MSGCODE_SET_ACK_MASK); |
| 344 | PushEscaped(output, iMask >> 8); |
| 345 | PushEscaped(output, (uint8_t)iMask); |
| 346 | output.push_back(MSGEND); |
| 347 | |
| 348 | if (!Write(output)) |
| 349 | { |
| 350 | m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask"); |
| 351 | return false; |
| 352 | } |
| 353 | |
| 354 | return true; |
| 355 | } |
| 356 | |
| 357 | bool CAdapterCommunication::PingAdapter(void) |
| 358 | { |
| 359 | if (!IsRunning()) |
| 360 | return false; |
| 361 | |
| 362 | m_controller->AddLog(CEC_LOG_DEBUG, "sending ping"); |
| 363 | cec_adapter_message output; |
| 364 | output.clear(); |
| 365 | |
| 366 | output.push_back(MSGSTART); |
| 367 | PushEscaped(output, MSGCODE_PING); |
| 368 | output.push_back(MSGEND); |
| 369 | |
| 370 | if (!Write(output)) |
| 371 | { |
| 372 | m_controller->AddLog(CEC_LOG_ERROR, "could not send ping command"); |
| 373 | return false; |
| 374 | } |
| 375 | |
| 376 | m_controller->AddLog(CEC_LOG_DEBUG, "ping tranmitted"); |
| 377 | |
| 378 | // TODO check for pong |
| 379 | return true; |
| 380 | } |
| 381 | |
| 382 | bool CAdapterCommunication::IsOpen(void) const |
| 383 | { |
| 384 | return !IsStopped() && m_port->IsOpen(); |
| 385 | } |
| 386 | |
| 387 | void CAdapterCommunication::FormatAdapterMessage(const cec_command &command, cec_adapter_message &packet) |
| 388 | { |
| 389 | packet.clear(); |
| 390 | |
| 391 | //set ack polarity to high when transmitting to the broadcast address |
| 392 | //set ack polarity low when transmitting to any other address |
| 393 | packet.push_back(MSGSTART); |
| 394 | PushEscaped(packet, MSGCODE_TRANSMIT_ACK_POLARITY); |
| 395 | if (command.destination == CECDEVICE_BROADCAST) |
| 396 | PushEscaped(packet, CEC_TRUE); |
| 397 | else |
| 398 | PushEscaped(packet, CEC_FALSE); |
| 399 | packet.push_back(MSGEND); |
| 400 | |
| 401 | // add source and destination |
| 402 | packet.push_back(MSGSTART); |
| 403 | PushEscaped(packet, MSGCODE_TRANSMIT); |
| 404 | packet.push_back(((uint8_t)command.initiator << 4) + (uint8_t)command.destination); |
| 405 | packet.push_back(MSGEND); |
| 406 | |
| 407 | // add opcode |
| 408 | packet.push_back(MSGSTART); |
| 409 | PushEscaped(packet, command.parameters.empty() ? (uint8_t)MSGCODE_TRANSMIT_EOM : (uint8_t)MSGCODE_TRANSMIT); |
| 410 | packet.push_back((uint8_t) command.opcode); |
| 411 | packet.push_back(MSGEND); |
| 412 | |
| 413 | // add parameters |
| 414 | for (int8_t iPtr = 0; iPtr < command.parameters.size; iPtr++) |
| 415 | { |
| 416 | packet.push_back(MSGSTART); |
| 417 | |
| 418 | if (iPtr == command.parameters.size - 1) |
| 419 | PushEscaped(packet, MSGCODE_TRANSMIT_EOM); |
| 420 | else |
| 421 | PushEscaped(packet, MSGCODE_TRANSMIT); |
| 422 | |
| 423 | PushEscaped(packet, command.parameters[iPtr]); |
| 424 | |
| 425 | packet.push_back(MSGEND); |
| 426 | } |
| 427 | } |
| 428 | |