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