| 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 "CECParser.h" |
| 34 | |
| 35 | #include <algorithm> |
| 36 | #include <cstdio> |
| 37 | #include <cstdlib> |
| 38 | #include <cstring> |
| 39 | #include <sys/stat.h> |
| 40 | #include "util/StdString.h" |
| 41 | #include "libPlatform/serialport.h" |
| 42 | #include "util/threads.h" |
| 43 | #include "util/timeutils.h" |
| 44 | #include "CECDetect.h" |
| 45 | #include "Communication.h" |
| 46 | |
| 47 | using namespace CEC; |
| 48 | using namespace std; |
| 49 | |
| 50 | #define CEC_MAX_RETRY 5 |
| 51 | #define CEC_BUTTON_TIMEOUT 500 |
| 52 | |
| 53 | /*! |
| 54 | * ICECDevice implementation |
| 55 | */ |
| 56 | //@{ |
| 57 | CCECParser::CCECParser(const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, int iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) : |
| 58 | m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN), |
| 59 | m_physicaladdress(iPhysicalAddress), |
| 60 | m_iLogicalAddress(iLogicalAddress), |
| 61 | m_strDeviceName(strDeviceName), |
| 62 | m_bRunning(false) |
| 63 | { |
| 64 | m_communication = new CCommunication(this); |
| 65 | } |
| 66 | |
| 67 | CCECParser::~CCECParser(void) |
| 68 | { |
| 69 | Close(0); |
| 70 | m_communication->Close(); |
| 71 | delete m_communication; |
| 72 | } |
| 73 | |
| 74 | bool CCECParser::Open(const char *strPort, int iTimeoutMs /* = 10000 */) |
| 75 | { |
| 76 | if (!m_communication) |
| 77 | return false; |
| 78 | |
| 79 | if (m_communication->IsOpen()) |
| 80 | { |
| 81 | AddLog(CEC_LOG_ERROR, "connection already open"); |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | if (!m_communication->Open(strPort, 38400, iTimeoutMs)) |
| 86 | { |
| 87 | AddLog(CEC_LOG_ERROR, "could not open a connection"); |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | if (!SetLogicalAddress(m_iLogicalAddress)) |
| 92 | { |
| 93 | AddLog(CEC_LOG_ERROR, "could not set the logical address"); |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | if (pthread_create(&m_thread, NULL, (void *(*) (void *))&CCECParser::ThreadHandler, (void *)this) == 0) |
| 98 | { |
| 99 | m_bRunning = true; |
| 100 | AddLog(CEC_LOG_DEBUG, "processor thread created"); |
| 101 | pthread_detach(m_thread); |
| 102 | return true; |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | AddLog(CEC_LOG_ERROR, "could not create a processor thread"); |
| 107 | m_bRunning = false; |
| 108 | } |
| 109 | |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | bool CCECParser::Close(int iTimeoutMs /* = 2000 */) |
| 114 | { |
| 115 | m_bRunning = false; |
| 116 | bool bExit(false); |
| 117 | if (iTimeoutMs > 0) |
| 118 | { |
| 119 | bExit = m_exitCondition.Wait(&m_mutex, iTimeoutMs); |
| 120 | m_mutex.Unlock(); |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | pthread_join(m_thread, NULL); |
| 125 | bExit = true; |
| 126 | } |
| 127 | |
| 128 | return bExit; |
| 129 | } |
| 130 | |
| 131 | void *CCECParser::ThreadHandler(CCECParser *parser) |
| 132 | { |
| 133 | if (parser) |
| 134 | parser->Process(); |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | bool CCECParser::Process(void) |
| 139 | { |
| 140 | int64_t now = GetTimeMs(); |
| 141 | while (m_bRunning) |
| 142 | { |
| 143 | cec_frame msg; |
| 144 | while (m_bRunning && m_communication->IsOpen() && m_communication->Read(msg, CEC_BUTTON_TIMEOUT)) |
| 145 | ParseMessage(msg); |
| 146 | |
| 147 | now = GetTimeMs(); |
| 148 | CheckKeypressTimeout(now); |
| 149 | CCondition::Sleep(50); |
| 150 | } |
| 151 | |
| 152 | AddLog(CEC_LOG_DEBUG, "processor thread terminated"); |
| 153 | m_bRunning = false; |
| 154 | m_exitCondition.Signal(); |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | bool CCECParser::Ping(void) |
| 159 | { |
| 160 | if (!m_bRunning) |
| 161 | return false; |
| 162 | |
| 163 | AddLog(CEC_LOG_DEBUG, "sending ping"); |
| 164 | cec_frame output; |
| 165 | output.push_back(MSGSTART); |
| 166 | PushEscaped(output, MSGCODE_PING); |
| 167 | output.push_back(MSGEND); |
| 168 | |
| 169 | if (!TransmitFormatted(output, false)) |
| 170 | { |
| 171 | AddLog(CEC_LOG_ERROR, "could not send ping command"); |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | AddLog(CEC_LOG_DEBUG, "ping tranmitted"); |
| 176 | |
| 177 | // TODO check for pong |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | bool CCECParser::StartBootloader(void) |
| 182 | { |
| 183 | if (!m_bRunning) |
| 184 | return false; |
| 185 | |
| 186 | AddLog(CEC_LOG_DEBUG, "starting the bootloader"); |
| 187 | cec_frame output; |
| 188 | output.push_back(MSGSTART); |
| 189 | PushEscaped(output, MSGCODE_START_BOOTLOADER); |
| 190 | output.push_back(MSGEND); |
| 191 | |
| 192 | if (!TransmitFormatted(output, false)) |
| 193 | { |
| 194 | AddLog(CEC_LOG_ERROR, "could not start the bootloader"); |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | AddLog(CEC_LOG_DEBUG, "bootloader start command transmitted"); |
| 199 | return true; |
| 200 | } |
| 201 | |
| 202 | uint8_t CCECParser::GetSourceDestination(cec_logical_address destination /* = CECDEVICE_BROADCAST */) |
| 203 | { |
| 204 | return ((uint8_t)m_iLogicalAddress << 4) + (uint8_t)destination; |
| 205 | } |
| 206 | |
| 207 | bool CCECParser::PowerOffDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */) |
| 208 | { |
| 209 | return StandbyDevices(address); |
| 210 | } |
| 211 | |
| 212 | bool CCECParser::PowerOnDevices(cec_logical_address address /* = CECDEVICE_TV */) |
| 213 | { |
| 214 | if (!m_bRunning) |
| 215 | return false; |
| 216 | |
| 217 | CStdString strLog; |
| 218 | strLog.Format("powering on devices with logical address %d", (int8_t)address); |
| 219 | AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
| 220 | cec_frame frame; |
| 221 | frame.push_back(GetSourceDestination(address)); |
| 222 | frame.push_back(CEC_OPCODE_TEXT_VIEW_ON); |
| 223 | return Transmit(frame); |
| 224 | } |
| 225 | |
| 226 | bool CCECParser::StandbyDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */) |
| 227 | { |
| 228 | if (!m_bRunning) |
| 229 | return false; |
| 230 | |
| 231 | CStdString strLog; |
| 232 | strLog.Format("putting all devices with logical address %d in standby mode", (int8_t)address); |
| 233 | AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
| 234 | cec_frame frame; |
| 235 | frame.push_back(GetSourceDestination(address)); |
| 236 | frame.push_back(CEC_OPCODE_STANDBY); |
| 237 | return Transmit(frame); |
| 238 | } |
| 239 | |
| 240 | bool CCECParser::SetActiveView(void) |
| 241 | { |
| 242 | if (!m_bRunning) |
| 243 | return false; |
| 244 | |
| 245 | AddLog(CEC_LOG_DEBUG, "setting active view"); |
| 246 | cec_frame frame; |
| 247 | frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST)); |
| 248 | frame.push_back(CEC_OPCODE_ACTIVE_SOURCE); |
| 249 | frame.push_back((m_physicaladdress >> 8) & 0xFF); |
| 250 | frame.push_back(m_physicaladdress & 0xFF); |
| 251 | return Transmit(frame); |
| 252 | } |
| 253 | |
| 254 | bool CCECParser::SetInactiveView(void) |
| 255 | { |
| 256 | if (!m_bRunning) |
| 257 | return false; |
| 258 | |
| 259 | AddLog(CEC_LOG_DEBUG, "setting inactive view"); |
| 260 | cec_frame frame; |
| 261 | frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST)); |
| 262 | frame.push_back(CEC_OPCODE_INACTIVE_SOURCE); |
| 263 | frame.push_back((m_physicaladdress >> 8) & 0xFF); |
| 264 | frame.push_back(m_physicaladdress & 0xFF); |
| 265 | return Transmit(frame); |
| 266 | } |
| 267 | |
| 268 | bool CCECParser::GetNextLogMessage(cec_log_message *message) |
| 269 | { |
| 270 | return m_logBuffer.Pop(*message); |
| 271 | } |
| 272 | |
| 273 | bool CCECParser::GetNextKeypress(cec_keypress *key) |
| 274 | { |
| 275 | return m_bRunning ? m_keyBuffer.Pop(*key) : false; |
| 276 | } |
| 277 | |
| 278 | bool CCECParser::GetNextCommand(cec_command *command) |
| 279 | { |
| 280 | return m_bRunning ? m_commandBuffer.Pop(*command) : false; |
| 281 | } |
| 282 | //@} |
| 283 | |
| 284 | void CCECParser::TransmitAbort(cec_logical_address address, cec_opcode opcode, ECecAbortReason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */) |
| 285 | { |
| 286 | AddLog(CEC_LOG_DEBUG, "transmitting abort message"); |
| 287 | cec_frame frame; |
| 288 | frame.push_back(GetSourceDestination(address)); |
| 289 | frame.push_back(CEC_OPCODE_FEATURE_ABORT); |
| 290 | frame.push_back(opcode); |
| 291 | frame.push_back(reason); |
| 292 | Transmit(frame); |
| 293 | } |
| 294 | |
| 295 | void CCECParser::ReportCECVersion(cec_logical_address address /* = CECDEVICE_TV */) |
| 296 | { |
| 297 | cec_frame frame; |
| 298 | AddLog(CEC_LOG_NOTICE, "reporting CEC version as 1.3a"); |
| 299 | frame.push_back(GetSourceDestination(address)); |
| 300 | frame.push_back(CEC_OPCODE_CEC_VERSION); |
| 301 | frame.push_back(CEC_VERSION_1_3A); |
| 302 | Transmit(frame); |
| 303 | } |
| 304 | |
| 305 | void CCECParser::ReportPowerState(cec_logical_address address /*= CECDEVICE_TV */, bool bOn /* = true */) |
| 306 | { |
| 307 | cec_frame frame; |
| 308 | if (bOn) |
| 309 | AddLog(CEC_LOG_NOTICE, "reporting \"On\" power status"); |
| 310 | else |
| 311 | AddLog(CEC_LOG_NOTICE, "reporting \"Off\" power status"); |
| 312 | |
| 313 | frame.push_back(GetSourceDestination(address)); |
| 314 | frame.push_back(CEC_OPCODE_REPORT_POWER_STATUS); |
| 315 | frame.push_back(bOn ? CEC_POWER_STATUS_ON : CEC_POWER_STATUS_STANDBY); |
| 316 | Transmit(frame); |
| 317 | } |
| 318 | |
| 319 | void CCECParser::ReportMenuState(cec_logical_address address /* = CECDEVICE_TV */, bool bActive /* = true */) |
| 320 | { |
| 321 | cec_frame frame; |
| 322 | if (bActive) |
| 323 | AddLog(CEC_LOG_NOTICE, "reporting menu state as active"); |
| 324 | else |
| 325 | AddLog(CEC_LOG_NOTICE, "reporting menu state as inactive"); |
| 326 | |
| 327 | frame.push_back(GetSourceDestination(address)); |
| 328 | frame.push_back(CEC_OPCODE_MENU_STATUS); |
| 329 | frame.push_back(bActive ? CEC_MENU_STATE_ACTIVATED : CEC_MENU_STATE_DEACTIVATED); |
| 330 | Transmit(frame); |
| 331 | } |
| 332 | |
| 333 | void CCECParser::ReportVendorID(cec_logical_address address /* = CECDEVICE_TV */) |
| 334 | { |
| 335 | AddLog(CEC_LOG_NOTICE, "vendor ID requested, feature abort"); |
| 336 | TransmitAbort(address, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID); |
| 337 | } |
| 338 | |
| 339 | void CCECParser::ReportOSDName(cec_logical_address address /* = CECDEVICE_TV */) |
| 340 | { |
| 341 | cec_frame frame; |
| 342 | const char *osdname = m_strDeviceName.c_str(); |
| 343 | CStdString strLog; |
| 344 | strLog.Format("reporting OSD name as %s", osdname); |
| 345 | AddLog(CEC_LOG_NOTICE, strLog.c_str()); |
| 346 | frame.push_back(GetSourceDestination(address)); |
| 347 | frame.push_back(CEC_OPCODE_SET_OSD_NAME); |
| 348 | |
| 349 | for (unsigned int i = 0; i < strlen(osdname); i++) |
| 350 | frame.push_back(osdname[i]); |
| 351 | |
| 352 | Transmit(frame); |
| 353 | } |
| 354 | |
| 355 | void CCECParser::ReportPhysicalAddress(void) |
| 356 | { |
| 357 | cec_frame frame; |
| 358 | CStdString strLog; |
| 359 | strLog.Format("reporting physical address as %04x", m_physicaladdress); |
| 360 | AddLog(CEC_LOG_NOTICE, strLog.c_str()); |
| 361 | frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST)); |
| 362 | frame.push_back(CEC_OPCODE_REPORT_PHYSICAL_ADDRESS); |
| 363 | frame.push_back((m_physicaladdress >> 8) & 0xFF); |
| 364 | frame.push_back(m_physicaladdress & 0xFF); |
| 365 | frame.push_back(CEC_DEVICE_TYPE_PLAYBACK_DEVICE); |
| 366 | Transmit(frame); |
| 367 | } |
| 368 | |
| 369 | void CCECParser::BroadcastActiveSource(void) |
| 370 | { |
| 371 | cec_frame frame; |
| 372 | AddLog(CEC_LOG_NOTICE, "broadcasting active source"); |
| 373 | frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST)); |
| 374 | frame.push_back(CEC_OPCODE_ACTIVE_SOURCE); |
| 375 | frame.push_back((m_physicaladdress >> 8) & 0xFF); |
| 376 | frame.push_back(m_physicaladdress & 0xFF); |
| 377 | Transmit(frame); |
| 378 | } |
| 379 | |
| 380 | bool CCECParser::TransmitFormatted(const cec_frame &data, bool bWaitForAck /* = true */) |
| 381 | { |
| 382 | if (!m_communication || !m_communication->Write(data)) |
| 383 | return false; |
| 384 | |
| 385 | CCondition::Sleep((int) data.size() * 24 /*data*/ + 5 /*start bit (4.5 ms)*/ + 50 /* to be on the safe side */); |
| 386 | if (bWaitForAck && !WaitForAck()) |
| 387 | { |
| 388 | AddLog(CEC_LOG_DEBUG, "did not receive ACK"); |
| 389 | return false; |
| 390 | } |
| 391 | |
| 392 | return true; |
| 393 | } |
| 394 | |
| 395 | bool CCECParser::Transmit(const cec_frame &data, bool bWaitForAck /* = true */) |
| 396 | { |
| 397 | CStdString txStr = "transmit "; |
| 398 | for (unsigned int i = 0; i < data.size(); i++) |
| 399 | txStr.AppendFormat(" %02x", data[i]); |
| 400 | AddLog(CEC_LOG_DEBUG, txStr.c_str()); |
| 401 | |
| 402 | if (data.empty()) |
| 403 | { |
| 404 | AddLog(CEC_LOG_WARNING, "transmit buffer is empty"); |
| 405 | return false; |
| 406 | } |
| 407 | |
| 408 | cec_frame output; |
| 409 | |
| 410 | //set ack polarity to high when transmitting to the broadcast address |
| 411 | //set ack polarity low when transmitting to any other address |
| 412 | output.push_back(MSGSTART); |
| 413 | PushEscaped(output, MSGCODE_TRANSMIT_ACK_POLARITY); |
| 414 | |
| 415 | if ((data[0] & 0xF) == 0xF) |
| 416 | PushEscaped(output, CEC_TRUE); |
| 417 | else |
| 418 | PushEscaped(output, CEC_FALSE); |
| 419 | |
| 420 | output.push_back(MSGEND); |
| 421 | |
| 422 | for (unsigned int i = 0; i < data.size(); i++) |
| 423 | { |
| 424 | output.push_back(MSGSTART); |
| 425 | |
| 426 | if (i == data.size() - 1) |
| 427 | PushEscaped(output, MSGCODE_TRANSMIT_EOM); |
| 428 | else |
| 429 | PushEscaped(output, MSGCODE_TRANSMIT); |
| 430 | |
| 431 | PushEscaped(output, data[i]); |
| 432 | |
| 433 | output.push_back(MSGEND); |
| 434 | } |
| 435 | |
| 436 | return TransmitFormatted(output, bWaitForAck); |
| 437 | } |
| 438 | |
| 439 | bool CCECParser::WaitForAck(int iTimeout /* = 1000 */) |
| 440 | { |
| 441 | bool bGotAck(false); |
| 442 | bool bError(false); |
| 443 | |
| 444 | int64_t iNow = GetTimeMs(); |
| 445 | int64_t iTargetTime = iNow + (int64_t) iTimeout; |
| 446 | |
| 447 | while (!bGotAck && !bError && (iTimeout <= 0 || iNow < iTargetTime)) |
| 448 | { |
| 449 | cec_frame msg; |
| 450 | while (!bGotAck && !bError && m_communication->Read(msg, iTimeout)) |
| 451 | { |
| 452 | uint8_t iCode = msg[0] & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK); |
| 453 | |
| 454 | switch (iCode) |
| 455 | { |
| 456 | case MSGCODE_COMMAND_ACCEPTED: |
| 457 | AddLog(CEC_LOG_DEBUG, "MSGCODE_COMMAND_ACCEPTED"); |
| 458 | break; |
| 459 | case MSGCODE_TRANSMIT_SUCCEEDED: |
| 460 | AddLog(CEC_LOG_DEBUG, "MSGCODE_TRANSMIT_SUCCEEDED"); |
| 461 | // TODO |
| 462 | bGotAck = true; |
| 463 | break; |
| 464 | case MSGCODE_RECEIVE_FAILED: |
| 465 | AddLog(CEC_LOG_WARNING, "MSGCODE_RECEIVE_FAILED"); |
| 466 | bError = true; |
| 467 | break; |
| 468 | case MSGCODE_COMMAND_REJECTED: |
| 469 | AddLog(CEC_LOG_WARNING, "MSGCODE_COMMAND_REJECTED"); |
| 470 | bError = true; |
| 471 | break; |
| 472 | case MSGCODE_TRANSMIT_FAILED_LINE: |
| 473 | AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_LINE"); |
| 474 | bError = true; |
| 475 | break; |
| 476 | case MSGCODE_TRANSMIT_FAILED_ACK: |
| 477 | AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_ACK"); |
| 478 | bError = true; |
| 479 | break; |
| 480 | case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA: |
| 481 | AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA"); |
| 482 | bError = true; |
| 483 | break; |
| 484 | case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE: |
| 485 | AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE"); |
| 486 | bError = true; |
| 487 | break; |
| 488 | default: |
| 489 | m_frameBuffer.Push(msg); |
| 490 | bGotAck = (msg[0] & MSGCODE_FRAME_ACK) != 0; |
| 491 | break; |
| 492 | } |
| 493 | iNow = GetTimeMs(); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | return bGotAck && !bError; |
| 498 | } |
| 499 | |
| 500 | void CCECParser::ParseMessage(cec_frame &msg) |
| 501 | { |
| 502 | if (msg.empty()) |
| 503 | return; |
| 504 | |
| 505 | CStdString logStr; |
| 506 | uint8_t iCode = msg[0] & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK); |
| 507 | bool bEom = (msg[0] & MSGCODE_FRAME_EOM) != 0; |
| 508 | bool bAck = (msg[0] & MSGCODE_FRAME_ACK) != 0; |
| 509 | |
| 510 | switch(iCode) |
| 511 | { |
| 512 | case MSGCODE_NOTHING: |
| 513 | AddLog(CEC_LOG_DEBUG, "MSGCODE_NOTHING"); |
| 514 | break; |
| 515 | case MSGCODE_TIMEOUT_ERROR: |
| 516 | case MSGCODE_HIGH_ERROR: |
| 517 | case MSGCODE_LOW_ERROR: |
| 518 | { |
| 519 | if (iCode == MSGCODE_TIMEOUT_ERROR) |
| 520 | logStr = "MSGCODE_TIMEOUT"; |
| 521 | else if (iCode == MSGCODE_HIGH_ERROR) |
| 522 | logStr = "MSGCODE_HIGH_ERROR"; |
| 523 | else |
| 524 | logStr = "MSGCODE_LOW_ERROR"; |
| 525 | |
| 526 | int iLine = (msg.size() >= 3) ? (msg[1] << 8) | (msg[2]) : 0; |
| 527 | uint32_t iTime = (msg.size() >= 7) ? (msg[3] << 24) | (msg[4] << 16) | (msg[5] << 8) | (msg[6]) : 0; |
| 528 | logStr.AppendFormat(" line:%i", iLine); |
| 529 | logStr.AppendFormat(" time:%u", iTime); |
| 530 | AddLog(CEC_LOG_WARNING, logStr.c_str()); |
| 531 | } |
| 532 | break; |
| 533 | case MSGCODE_FRAME_START: |
| 534 | { |
| 535 | logStr = "MSGCODE_FRAME_START"; |
| 536 | m_currentframe.clear(); |
| 537 | if (msg.size() >= 2) |
| 538 | { |
| 539 | int iInitiator = msg[1] >> 4; |
| 540 | int iDestination = msg[1] & 0xF; |
| 541 | logStr.AppendFormat(" initiator:%u destination:%u ack:%s %s", iInitiator, iDestination, bAck ? "high" : "low", bEom ? "eom" : ""); |
| 542 | |
| 543 | m_currentframe.push_back(msg[1]); |
| 544 | } |
| 545 | AddLog(CEC_LOG_DEBUG, logStr.c_str()); |
| 546 | } |
| 547 | break; |
| 548 | case MSGCODE_FRAME_DATA: |
| 549 | { |
| 550 | logStr = "MSGCODE_FRAME_DATA"; |
| 551 | if (msg.size() >= 2) |
| 552 | { |
| 553 | uint8_t iData = msg[1]; |
| 554 | logStr.AppendFormat(" %02x", iData); |
| 555 | m_currentframe.push_back(iData); |
| 556 | } |
| 557 | AddLog(CEC_LOG_DEBUG, logStr.c_str()); |
| 558 | } |
| 559 | if (bEom) |
| 560 | ParseCurrentFrame(); |
| 561 | break; |
| 562 | default: |
| 563 | break; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | void CCECParser::ParseCurrentFrame(void) |
| 568 | { |
| 569 | uint8_t initiator = m_currentframe[0] >> 4; |
| 570 | uint8_t destination = m_currentframe[0] & 0xF; |
| 571 | |
| 572 | CStdString dataStr; |
| 573 | dataStr.Format("received frame: initiator: %u destination: %u", initiator, destination); |
| 574 | |
| 575 | if (m_currentframe.size() > 1) |
| 576 | { |
| 577 | dataStr += " data:"; |
| 578 | for (unsigned int i = 1; i < m_currentframe.size(); i++) |
| 579 | dataStr.AppendFormat(" %02x", m_currentframe[i]); |
| 580 | } |
| 581 | AddLog(CEC_LOG_DEBUG, dataStr.c_str()); |
| 582 | |
| 583 | if (m_currentframe.size() <= 1) |
| 584 | return; |
| 585 | |
| 586 | vector<uint8_t> tx; |
| 587 | cec_opcode opCode = (cec_opcode) m_currentframe[1]; |
| 588 | if (destination == (uint16_t) m_iLogicalAddress) |
| 589 | { |
| 590 | switch(opCode) |
| 591 | { |
| 592 | case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS: |
| 593 | ReportPhysicalAddress(); |
| 594 | SetActiveView(); |
| 595 | break; |
| 596 | case CEC_OPCODE_GIVE_OSD_NAME: |
| 597 | ReportOSDName((cec_logical_address)initiator); |
| 598 | break; |
| 599 | case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID: |
| 600 | ReportVendorID((cec_logical_address)initiator); |
| 601 | break; |
| 602 | case CEC_OPCODE_MENU_REQUEST: |
| 603 | ReportMenuState((cec_logical_address)initiator); |
| 604 | break; |
| 605 | case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS: |
| 606 | ReportPowerState((cec_logical_address)initiator); |
| 607 | break; |
| 608 | case CEC_OPCODE_GET_CEC_VERSION: |
| 609 | ReportCECVersion((cec_logical_address)initiator); |
| 610 | break; |
| 611 | case CEC_OPCODE_USER_CONTROL_PRESSED: |
| 612 | if (m_currentframe.size() > 2) |
| 613 | { |
| 614 | AddKey(); |
| 615 | |
| 616 | if (m_currentframe[2] <= CEC_USER_CONTROL_CODE_MAX) |
| 617 | { |
| 618 | m_iCurrentButton = (cec_user_control_code) m_currentframe[2]; |
| 619 | m_buttontime = GetTimeMs(); |
| 620 | } |
| 621 | } |
| 622 | break; |
| 623 | case CEC_OPCODE_USER_CONTROL_RELEASE: |
| 624 | AddKey(); |
| 625 | break; |
| 626 | default: |
| 627 | cec_frame params = m_currentframe; |
| 628 | params.erase(params.begin(), params.begin() + 2); |
| 629 | AddCommand((cec_logical_address) initiator, (cec_logical_address) destination, opCode, ¶ms); |
| 630 | break; |
| 631 | } |
| 632 | } |
| 633 | else if (destination == (uint8_t) CECDEVICE_BROADCAST) |
| 634 | { |
| 635 | if (opCode == CEC_OPCODE_REQUEST_ACTIVE_SOURCE) |
| 636 | { |
| 637 | BroadcastActiveSource(); |
| 638 | } |
| 639 | else if (opCode == CEC_OPCODE_SET_STREAM_PATH) |
| 640 | { |
| 641 | if (m_currentframe.size() >= 4) |
| 642 | { |
| 643 | int streamaddr = ((int)m_currentframe[2] << 8) | ((int)m_currentframe[3]); |
| 644 | CStdString strLog; |
| 645 | strLog.Format("%i requests stream path from physical address %04x", initiator, streamaddr); |
| 646 | AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
| 647 | if (streamaddr == m_physicaladdress) |
| 648 | BroadcastActiveSource(); |
| 649 | } |
| 650 | } |
| 651 | else |
| 652 | { |
| 653 | cec_frame params = m_currentframe; |
| 654 | params.erase(params.begin(), params.begin() + 2); |
| 655 | AddCommand((cec_logical_address) initiator, (cec_logical_address) destination, opCode, ¶ms); |
| 656 | } |
| 657 | } |
| 658 | else |
| 659 | { |
| 660 | CStdString strLog; |
| 661 | strLog.Format("ignoring frame: destination: %u != %u", destination, (uint16_t)m_iLogicalAddress); |
| 662 | AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | void CCECParser::PushEscaped(cec_frame &vec, uint8_t byte) |
| 667 | { |
| 668 | if (byte >= MSGESC && byte != MSGSTART) |
| 669 | { |
| 670 | vec.push_back(MSGESC); |
| 671 | vec.push_back(byte - ESCOFFSET); |
| 672 | } |
| 673 | else |
| 674 | { |
| 675 | vec.push_back(byte); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | void CCECParser::CheckKeypressTimeout(int64_t now) |
| 680 | { |
| 681 | if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN && now - m_buttontime > CEC_BUTTON_TIMEOUT) |
| 682 | { |
| 683 | AddKey(); |
| 684 | m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN; |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | bool CCECParser::SetLogicalAddress(cec_logical_address iLogicalAddress) |
| 689 | { |
| 690 | CStdString strLog; |
| 691 | strLog.Format("setting logical address to %d", iLogicalAddress); |
| 692 | AddLog(CEC_LOG_NOTICE, strLog.c_str()); |
| 693 | |
| 694 | m_iLogicalAddress = iLogicalAddress; |
| 695 | return SetAckMask(0x1 << (uint8_t)m_iLogicalAddress); |
| 696 | } |
| 697 | |
| 698 | bool CCECParser::SetAckMask(uint16_t iMask) |
| 699 | { |
| 700 | CStdString strLog; |
| 701 | strLog.Format("setting ackmask to %2x", iMask); |
| 702 | AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
| 703 | |
| 704 | cec_frame output; |
| 705 | |
| 706 | output.push_back(MSGSTART); |
| 707 | PushEscaped(output, MSGCODE_SET_ACK_MASK); |
| 708 | PushEscaped(output, iMask >> 8); |
| 709 | PushEscaped(output, (uint8_t)iMask); |
| 710 | output.push_back(MSGEND); |
| 711 | |
| 712 | if (!m_communication->Write(output)) |
| 713 | { |
| 714 | AddLog(CEC_LOG_ERROR, "could not set the ackmask"); |
| 715 | return false; |
| 716 | } |
| 717 | |
| 718 | return true; |
| 719 | } |
| 720 | |
| 721 | void CCECParser::AddLog(cec_log_level level, const string &strMessage) |
| 722 | { |
| 723 | cec_log_message message; |
| 724 | message.level = level; |
| 725 | message.message.assign(strMessage.c_str()); |
| 726 | m_logBuffer.Push(message); |
| 727 | } |
| 728 | |
| 729 | void CCECParser::AddKey(void) |
| 730 | { |
| 731 | if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN) |
| 732 | { |
| 733 | cec_keypress key; |
| 734 | key.duration = (unsigned int) (GetTimeMs() - m_buttontime); |
| 735 | key.keycode = m_iCurrentButton; |
| 736 | m_keyBuffer.Push(key); |
| 737 | m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN; |
| 738 | m_buttontime = 0; |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | void CCECParser::AddCommand(cec_logical_address source, cec_logical_address destination, cec_opcode opcode, cec_frame *parameters) |
| 743 | { |
| 744 | cec_command command; |
| 745 | command.source = source; |
| 746 | command.destination = destination; |
| 747 | command.opcode = opcode; |
| 748 | if (parameters) |
| 749 | command.parameters = *parameters; |
| 750 | if (m_commandBuffer.Push(command)) |
| 751 | { |
| 752 | CStdString strDebug; |
| 753 | strDebug.Format("stored command '%d' in the command buffer. buffer size = %d", opcode, m_commandBuffer.Size()); |
| 754 | AddLog(CEC_LOG_DEBUG, strDebug); |
| 755 | } |
| 756 | else |
| 757 | { |
| 758 | AddLog(CEC_LOG_WARNING, "command buffer is full"); |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | int CCECParser::GetMinVersion(void) |
| 763 | { |
| 764 | return CEC_MIN_VERSION; |
| 765 | } |
| 766 | |
| 767 | int CCECParser::GetLibVersion(void) |
| 768 | { |
| 769 | return CEC_LIB_VERSION; |
| 770 | } |
| 771 | |
| 772 | int CCECParser::FindDevices(std::vector<cec_device> &deviceList, const char *strDevicePath /* = NULL */) |
| 773 | { |
| 774 | CStdString strDebug; |
| 775 | if (strDevicePath) |
| 776 | strDebug.Format("trying to autodetect the com port for device path '%s'", strDevicePath); |
| 777 | else |
| 778 | strDebug.Format("trying to autodetect all CEC adapters"); |
| 779 | AddLog(CEC_LOG_DEBUG, strDebug); |
| 780 | |
| 781 | return CCECDetect::FindDevices(deviceList, strDevicePath); |
| 782 | } |
| 783 | |
| 784 | DECLSPEC void * CECCreate(const char *strDeviceName, CEC::cec_logical_address iLogicalAddress /*= CEC::CECDEVICE_PLAYBACKDEVICE1 */, int iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */) |
| 785 | { |
| 786 | return static_cast< void* > (new CCECParser(strDeviceName, iLogicalAddress, iPhysicalAddress)); |
| 787 | } |