| 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 "libPlatform/serialport.h" |
| 37 | #include "util/StdString.h" |
| 38 | |
| 39 | using namespace std; |
| 40 | using namespace CEC; |
| 41 | |
| 42 | CAdapterCommunication::CAdapterCommunication(CLibCEC *controller) : |
| 43 | m_controller(controller), |
| 44 | m_inbuf(NULL), |
| 45 | m_iInbufSize(0), |
| 46 | m_iInbufUsed(0), |
| 47 | m_bStarted(false), |
| 48 | m_bStop(false) |
| 49 | { |
| 50 | m_port = new CSerialPort; |
| 51 | } |
| 52 | |
| 53 | CAdapterCommunication::~CAdapterCommunication(void) |
| 54 | { |
| 55 | m_port->Close(); |
| 56 | m_port = NULL; |
| 57 | } |
| 58 | |
| 59 | bool CAdapterCommunication::Open(const char *strPort, int iBaudRate /* = 38400 */, int iTimeoutMs /* = 10000 */) |
| 60 | { |
| 61 | CLockObject lock(&m_commMutex); |
| 62 | if (m_bStarted) |
| 63 | return false; |
| 64 | |
| 65 | if (!m_port->Open(strPort, iBaudRate)) |
| 66 | { |
| 67 | CStdString strError; |
| 68 | strError.Format("error opening serial port '%s': %s", strPort, m_port->GetError().c_str()); |
| 69 | m_controller->AddLog(CEC_LOG_ERROR, strError); |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | m_controller->AddLog(CEC_LOG_DEBUG, "connection opened"); |
| 74 | |
| 75 | //clear any input bytes |
| 76 | uint8_t buff[1024]; |
| 77 | m_port->Read(buff, sizeof(buff), 50); |
| 78 | |
| 79 | CCondition::Sleep(CEC_SETTLE_DOWN_TIME); |
| 80 | |
| 81 | m_bStop = false; |
| 82 | m_bStarted = true; |
| 83 | |
| 84 | if (CreateThread()) |
| 85 | { |
| 86 | m_controller->AddLog(CEC_LOG_DEBUG, "reader thread created"); |
| 87 | return true; |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | m_controller->AddLog(CEC_LOG_DEBUG, "could not create a reader thread"); |
| 92 | } |
| 93 | |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | void CAdapterCommunication::Close(void) |
| 98 | { |
| 99 | StopThread(); |
| 100 | m_port->Close(); |
| 101 | } |
| 102 | |
| 103 | void *CAdapterCommunication::Process(void) |
| 104 | { |
| 105 | while (!m_bStop) |
| 106 | { |
| 107 | if (!ReadFromDevice(250)) |
| 108 | { |
| 109 | m_bStarted = false; |
| 110 | break; |
| 111 | } |
| 112 | |
| 113 | CCondition::Sleep(50); |
| 114 | } |
| 115 | |
| 116 | m_controller->AddLog(CEC_LOG_DEBUG, "reader thread terminated"); |
| 117 | |
| 118 | CLockObject lock(&m_commMutex); |
| 119 | m_bStarted = false; |
| 120 | return NULL; |
| 121 | } |
| 122 | |
| 123 | bool CAdapterCommunication::ReadFromDevice(int iTimeout) |
| 124 | { |
| 125 | uint8_t buff[1024]; |
| 126 | CLockObject lock(&m_commMutex); |
| 127 | int iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout); |
| 128 | lock.Leave(); |
| 129 | if (iBytesRead < 0) |
| 130 | { |
| 131 | CStdString strError; |
| 132 | strError.Format("error reading from serial port: %s", m_port->GetError().c_str()); |
| 133 | m_controller->AddLog(CEC_LOG_ERROR, strError); |
| 134 | return false; |
| 135 | } |
| 136 | else if (iBytesRead > 0) |
| 137 | AddData(buff, iBytesRead); |
| 138 | |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | void CAdapterCommunication::AddData(uint8_t *data, int iLen) |
| 143 | { |
| 144 | CLockObject lock(&m_bufferMutex); |
| 145 | if (iLen + m_iInbufUsed > m_iInbufSize) |
| 146 | { |
| 147 | m_iInbufSize = iLen + m_iInbufUsed; |
| 148 | m_inbuf = (uint8_t*)realloc(m_inbuf, m_iInbufSize); |
| 149 | } |
| 150 | |
| 151 | memcpy(m_inbuf + m_iInbufUsed, data, iLen); |
| 152 | m_iInbufUsed += iLen; |
| 153 | lock.Leave(); |
| 154 | m_condition.Signal(); |
| 155 | } |
| 156 | |
| 157 | bool CAdapterCommunication::Write(const cec_frame &data) |
| 158 | { |
| 159 | CLockObject lock(&m_commMutex); |
| 160 | |
| 161 | if (m_port->Write(data) != data.size()) |
| 162 | { |
| 163 | CStdString strError; |
| 164 | strError.Format("error writing to serial port: %s", m_port->GetError().c_str()); |
| 165 | m_controller->AddLog(CEC_LOG_ERROR, strError); |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | m_controller->AddLog(CEC_LOG_DEBUG, "command sent"); |
| 170 | |
| 171 | CCondition::Sleep((int) data.size() * 24 /*data*/ + 5 /*start bit (4.5 ms)*/ + 50 /* to be on the safe side */); |
| 172 | |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | bool CAdapterCommunication::Read(cec_frame &msg, int iTimeout) |
| 177 | { |
| 178 | CLockObject lock(&m_bufferMutex); |
| 179 | |
| 180 | if (m_iInbufUsed < 1) |
| 181 | m_condition.Wait(&m_bufferMutex, iTimeout); |
| 182 | |
| 183 | if (m_iInbufUsed < 1) |
| 184 | return false; |
| 185 | |
| 186 | //search for first start of message |
| 187 | int startpos = -1; |
| 188 | for (int i = 0; i < m_iInbufUsed; i++) |
| 189 | { |
| 190 | if (m_inbuf[i] == MSGSTART) |
| 191 | { |
| 192 | startpos = i; |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | if (startpos == -1) |
| 198 | return false; |
| 199 | |
| 200 | //move anything from the first start of message to the beginning of the buffer |
| 201 | if (startpos > 0) |
| 202 | { |
| 203 | memmove(m_inbuf, m_inbuf + startpos, m_iInbufUsed - startpos); |
| 204 | m_iInbufUsed -= startpos; |
| 205 | } |
| 206 | |
| 207 | if (m_iInbufUsed < 2) |
| 208 | return false; |
| 209 | |
| 210 | //look for end of message |
| 211 | startpos = -1; |
| 212 | int endpos = -1; |
| 213 | for (int i = 1; i < m_iInbufUsed; i++) |
| 214 | { |
| 215 | if (m_inbuf[i] == MSGEND) |
| 216 | { |
| 217 | endpos = i; |
| 218 | break; |
| 219 | } |
| 220 | else if (m_inbuf[i] == MSGSTART) |
| 221 | { |
| 222 | startpos = i; |
| 223 | break; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | if (startpos > 0) //we found a msgstart before msgend, this is not right, remove |
| 228 | { |
| 229 | m_controller->AddLog(CEC_LOG_ERROR, "received MSGSTART before MSGEND"); |
| 230 | memmove(m_inbuf, m_inbuf + startpos, m_iInbufUsed - startpos); |
| 231 | m_iInbufUsed -= startpos; |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | if (endpos > 0) //found a MSGEND |
| 236 | { |
| 237 | msg.clear(); |
| 238 | bool isesc = false; |
| 239 | for (int i = 1; i < endpos; i++) |
| 240 | { |
| 241 | if (isesc) |
| 242 | { |
| 243 | msg.push_back(m_inbuf[i] + (uint8_t)ESCOFFSET); |
| 244 | isesc = false; |
| 245 | } |
| 246 | else if (m_inbuf[i] == MSGESC) |
| 247 | { |
| 248 | isesc = true; |
| 249 | } |
| 250 | else |
| 251 | { |
| 252 | msg.push_back(m_inbuf[i]); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | if (endpos + 1 < m_iInbufUsed) |
| 257 | memmove(m_inbuf, m_inbuf + endpos + 1, m_iInbufUsed - endpos - 1); |
| 258 | |
| 259 | m_iInbufUsed -= endpos + 1; |
| 260 | |
| 261 | return true; |
| 262 | } |
| 263 | |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | std::string CAdapterCommunication::GetError(void) const |
| 268 | { |
| 269 | return m_port->GetError(); |
| 270 | } |
| 271 | |
| 272 | bool CAdapterCommunication::StartBootloader(void) |
| 273 | { |
| 274 | if (!IsRunning()) |
| 275 | return false; |
| 276 | |
| 277 | m_controller->AddLog(CEC_LOG_DEBUG, "starting the bootloader"); |
| 278 | cec_frame output; |
| 279 | output.push_back(MSGSTART); |
| 280 | PushEscaped(output, MSGCODE_START_BOOTLOADER); |
| 281 | output.push_back(MSGEND); |
| 282 | |
| 283 | if (!Write(output)) |
| 284 | { |
| 285 | m_controller->AddLog(CEC_LOG_ERROR, "could not start the bootloader"); |
| 286 | return false; |
| 287 | } |
| 288 | m_controller->AddLog(CEC_LOG_DEBUG, "bootloader start command transmitted"); |
| 289 | return true; |
| 290 | } |
| 291 | |
| 292 | void CAdapterCommunication::PushEscaped(cec_frame &vec, uint8_t byte) |
| 293 | { |
| 294 | if (byte >= MSGESC && byte != MSGSTART) |
| 295 | { |
| 296 | vec.push_back(MSGESC); |
| 297 | vec.push_back(byte - ESCOFFSET); |
| 298 | } |
| 299 | else |
| 300 | { |
| 301 | vec.push_back(byte); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | bool CAdapterCommunication::SetAckMask(uint16_t iMask) |
| 306 | { |
| 307 | if (!IsRunning()) |
| 308 | return false; |
| 309 | |
| 310 | CStdString strLog; |
| 311 | strLog.Format("setting ackmask to %2x", iMask); |
| 312 | m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
| 313 | |
| 314 | cec_frame output; |
| 315 | |
| 316 | output.push_back(MSGSTART); |
| 317 | PushEscaped(output, MSGCODE_SET_ACK_MASK); |
| 318 | PushEscaped(output, iMask >> 8); |
| 319 | PushEscaped(output, (uint8_t)iMask); |
| 320 | output.push_back(MSGEND); |
| 321 | |
| 322 | if (!Write(output)) |
| 323 | { |
| 324 | m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask"); |
| 325 | return false; |
| 326 | } |
| 327 | |
| 328 | return true; |
| 329 | } |
| 330 | |
| 331 | bool CAdapterCommunication::PingAdapter(void) |
| 332 | { |
| 333 | if (!IsRunning()) |
| 334 | return false; |
| 335 | |
| 336 | m_controller->AddLog(CEC_LOG_DEBUG, "sending ping"); |
| 337 | cec_frame output; |
| 338 | output.push_back(MSGSTART); |
| 339 | PushEscaped(output, MSGCODE_PING); |
| 340 | output.push_back(MSGEND); |
| 341 | |
| 342 | if (!Write(output)) |
| 343 | { |
| 344 | m_controller->AddLog(CEC_LOG_ERROR, "could not send ping command"); |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | m_controller->AddLog(CEC_LOG_DEBUG, "ping tranmitted"); |
| 349 | |
| 350 | // TODO check for pong |
| 351 | return true; |
| 352 | } |