| 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 "CECProcessor.h" |
| 34 | |
| 35 | #include "AdapterCommunication.h" |
| 36 | #include "LibCEC.h" |
| 37 | #include "util/StdString.h" |
| 38 | #include "platform/timeutils.h" |
| 39 | |
| 40 | using namespace CEC; |
| 41 | using namespace std; |
| 42 | |
| 43 | CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm, const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) : |
| 44 | m_physicaladdress(iPhysicalAddress), |
| 45 | m_iLogicalAddress(iLogicalAddress), |
| 46 | m_strDeviceName(strDeviceName), |
| 47 | m_communication(serComm), |
| 48 | m_controller(controller) |
| 49 | { |
| 50 | for (uint8_t iPtr = 0; iPtr < 16; iPtr++) |
| 51 | m_vendorIds[iPtr] = CEC_VENDOR_UNKNOWN; |
| 52 | for (uint8_t iPtr = 0; iPtr < 16; iPtr++) |
| 53 | m_vendorClasses[iPtr] = (uint8_t) 0; |
| 54 | } |
| 55 | |
| 56 | CCECProcessor::~CCECProcessor(void) |
| 57 | { |
| 58 | StopThread(); |
| 59 | m_communication = NULL; |
| 60 | m_controller = NULL; |
| 61 | } |
| 62 | |
| 63 | bool CCECProcessor::Start(void) |
| 64 | { |
| 65 | if (!m_communication || !m_communication->IsOpen()) |
| 66 | { |
| 67 | m_controller->AddLog(CEC_LOG_ERROR, "connection is closed"); |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | if (!SetLogicalAddress(m_iLogicalAddress)) |
| 72 | { |
| 73 | m_controller->AddLog(CEC_LOG_ERROR, "could not set the logical address"); |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | if (CreateThread()) |
| 78 | return true; |
| 79 | else |
| 80 | m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread"); |
| 81 | |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | void *CCECProcessor::Process(void) |
| 86 | { |
| 87 | m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started"); |
| 88 | |
| 89 | while (!IsStopped()) |
| 90 | { |
| 91 | bool bParseFrame(false); |
| 92 | cec_frame msg; |
| 93 | msg.clear(); |
| 94 | |
| 95 | { |
| 96 | CLockObject lock(&m_mutex); |
| 97 | if (m_communication->IsOpen() && m_communication->Read(msg, 50)) |
| 98 | bParseFrame = ParseMessage(msg) && !IsStopped(); |
| 99 | |
| 100 | if (bParseFrame) |
| 101 | { |
| 102 | msg.clear(); |
| 103 | msg = m_currentframe; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if (bParseFrame) |
| 108 | ParseCurrentFrame(msg); |
| 109 | |
| 110 | m_controller->CheckKeypressTimeout(); |
| 111 | |
| 112 | if (!IsStopped()) |
| 113 | Sleep(50); |
| 114 | } |
| 115 | |
| 116 | return NULL; |
| 117 | } |
| 118 | |
| 119 | bool CCECProcessor::PowerOnDevices(cec_logical_address address /* = CECDEVICE_TV */) |
| 120 | { |
| 121 | if (!IsRunning()) |
| 122 | return false; |
| 123 | |
| 124 | CStdString strLog; |
| 125 | strLog.Format("<< powering on device with logical address %d", (int8_t)address); |
| 126 | m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
| 127 | cec_frame frame; |
| 128 | frame.clear(); |
| 129 | |
| 130 | frame.push_back(GetSourceDestination(address)); |
| 131 | frame.push_back((uint8_t) CEC_OPCODE_IMAGE_VIEW_ON); |
| 132 | return Transmit(frame); |
| 133 | } |
| 134 | |
| 135 | bool CCECProcessor::StandbyDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */) |
| 136 | { |
| 137 | if (!IsRunning()) |
| 138 | return false; |
| 139 | |
| 140 | CStdString strLog; |
| 141 | strLog.Format("<< putting device with logical address %d in standby mode", (int8_t)address); |
| 142 | m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
| 143 | cec_frame frame; |
| 144 | frame.clear(); |
| 145 | |
| 146 | frame.push_back(GetSourceDestination(address)); |
| 147 | frame.push_back((uint8_t) CEC_OPCODE_STANDBY); |
| 148 | return Transmit(frame); |
| 149 | } |
| 150 | |
| 151 | bool CCECProcessor::SetActiveView(void) |
| 152 | { |
| 153 | if (!IsRunning()) |
| 154 | return false; |
| 155 | |
| 156 | m_controller->AddLog(CEC_LOG_DEBUG, "<< setting active view"); |
| 157 | cec_frame frame; |
| 158 | frame.clear(); |
| 159 | |
| 160 | frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST)); |
| 161 | frame.push_back((uint8_t) CEC_OPCODE_ACTIVE_SOURCE); |
| 162 | frame.push_back((m_physicaladdress >> 8) & 0xFF); |
| 163 | frame.push_back(m_physicaladdress & 0xFF); |
| 164 | return Transmit(frame); |
| 165 | } |
| 166 | |
| 167 | bool CCECProcessor::SetInactiveView(void) |
| 168 | { |
| 169 | if (!IsRunning()) |
| 170 | return false; |
| 171 | |
| 172 | m_controller->AddLog(CEC_LOG_DEBUG, "<< setting inactive view"); |
| 173 | cec_frame frame; |
| 174 | frame.clear(); |
| 175 | |
| 176 | frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST)); |
| 177 | frame.push_back((uint8_t) CEC_OPCODE_INACTIVE_SOURCE); |
| 178 | frame.push_back((m_physicaladdress >> 8) & 0xFF); |
| 179 | frame.push_back(m_physicaladdress & 0xFF); |
| 180 | return Transmit(frame); |
| 181 | } |
| 182 | |
| 183 | bool CCECProcessor::Transmit(const cec_frame &data, bool bWaitForAck /* = true */) |
| 184 | { |
| 185 | CStdString txStr = "transmit "; |
| 186 | for (unsigned int i = 0; i < data.size; i++) |
| 187 | txStr.AppendFormat(" %02x", data.data[i]); |
| 188 | m_controller->AddLog(CEC_LOG_DEBUG, txStr.c_str()); |
| 189 | |
| 190 | if (data.size == 0) |
| 191 | { |
| 192 | m_controller->AddLog(CEC_LOG_WARNING, "transmit buffer is empty"); |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | cec_frame output; |
| 197 | output.clear(); |
| 198 | |
| 199 | //set ack polarity to high when transmitting to the broadcast address |
| 200 | //set ack polarity low when transmitting to any other address |
| 201 | output.push_back(MSGSTART); |
| 202 | CAdapterCommunication::PushEscaped(output, MSGCODE_TRANSMIT_ACK_POLARITY); |
| 203 | |
| 204 | if ((data.data[0] & 0xF) == 0xF) |
| 205 | CAdapterCommunication::PushEscaped(output, CEC_TRUE); |
| 206 | else |
| 207 | CAdapterCommunication::PushEscaped(output, CEC_FALSE); |
| 208 | |
| 209 | output.push_back(MSGEND); |
| 210 | |
| 211 | for (int8_t i = 0; i < data.size; i++) |
| 212 | { |
| 213 | output.push_back(MSGSTART); |
| 214 | |
| 215 | if (i == (int8_t)data.size - 1) |
| 216 | CAdapterCommunication::PushEscaped(output, MSGCODE_TRANSMIT_EOM); |
| 217 | else |
| 218 | CAdapterCommunication::PushEscaped(output, MSGCODE_TRANSMIT); |
| 219 | |
| 220 | CAdapterCommunication::PushEscaped(output, data.data[i]); |
| 221 | |
| 222 | output.push_back(MSGEND); |
| 223 | } |
| 224 | |
| 225 | return TransmitFormatted(output, bWaitForAck); |
| 226 | } |
| 227 | |
| 228 | bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress) |
| 229 | { |
| 230 | CStdString strLog; |
| 231 | strLog.Format("<< setting logical address to %d", iLogicalAddress); |
| 232 | m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str()); |
| 233 | |
| 234 | m_iLogicalAddress = iLogicalAddress; |
| 235 | return m_communication && m_communication->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress); |
| 236 | } |
| 237 | |
| 238 | bool CCECProcessor::TransmitFormatted(const cec_frame &data, bool bWaitForAck /* = true */) |
| 239 | { |
| 240 | CLockObject lock(&m_mutex); |
| 241 | if (!m_communication || !m_communication->Write(data)) |
| 242 | return false; |
| 243 | |
| 244 | if (bWaitForAck) |
| 245 | { |
| 246 | uint64_t now = GetTimeMs(); |
| 247 | uint64_t target = now + 1000; |
| 248 | bool bError(false); |
| 249 | bool bGotAck(false); |
| 250 | |
| 251 | while (!bGotAck && now < target) |
| 252 | { |
| 253 | bGotAck = WaitForAck(&bError, (uint64_t) (target - now)); |
| 254 | now = GetTimeMs(); |
| 255 | |
| 256 | if (bError && now < target) |
| 257 | { |
| 258 | m_controller->AddLog(CEC_LOG_ERROR, "retransmitting previous frame"); |
| 259 | if (!m_communication->Write(data)) |
| 260 | return false; |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | return true; |
| 266 | } |
| 267 | |
| 268 | void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, ECecAbortReason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */) |
| 269 | { |
| 270 | m_controller->AddLog(CEC_LOG_DEBUG, "transmitting abort message"); |
| 271 | cec_frame frame; |
| 272 | frame.clear(); |
| 273 | |
| 274 | frame.push_back(GetSourceDestination(address)); |
| 275 | frame.push_back((uint8_t) CEC_OPCODE_FEATURE_ABORT); |
| 276 | frame.push_back((uint8_t) opcode); |
| 277 | frame.push_back((uint8_t) reason); |
| 278 | Transmit(frame); |
| 279 | } |
| 280 | |
| 281 | void CCECProcessor::ReportCECVersion(cec_logical_address address /* = CECDEVICE_TV */) |
| 282 | { |
| 283 | cec_frame frame; |
| 284 | frame.clear(); |
| 285 | |
| 286 | m_controller->AddLog(CEC_LOG_NOTICE, "<< reporting CEC version as 1.3a"); |
| 287 | frame.push_back(GetSourceDestination(address)); |
| 288 | frame.push_back((uint8_t) CEC_OPCODE_CEC_VERSION); |
| 289 | frame.push_back((uint8_t) CEC_VERSION_1_3A); |
| 290 | Transmit(frame); |
| 291 | } |
| 292 | |
| 293 | void CCECProcessor::ReportPowerState(cec_logical_address address /*= CECDEVICE_TV */, bool bOn /* = true */) |
| 294 | { |
| 295 | cec_frame frame; |
| 296 | frame.clear(); |
| 297 | |
| 298 | if (bOn) |
| 299 | m_controller->AddLog(CEC_LOG_NOTICE, "<< reporting \"On\" power status"); |
| 300 | else |
| 301 | m_controller->AddLog(CEC_LOG_NOTICE, "<< reporting \"Off\" power status"); |
| 302 | |
| 303 | frame.push_back(GetSourceDestination(address)); |
| 304 | frame.push_back((uint8_t) CEC_OPCODE_REPORT_POWER_STATUS); |
| 305 | frame.push_back(bOn ? (uint8_t) CEC_POWER_STATUS_ON : (uint8_t) CEC_POWER_STATUS_STANDBY); |
| 306 | Transmit(frame); |
| 307 | } |
| 308 | |
| 309 | void CCECProcessor::ReportMenuState(cec_logical_address address /* = CECDEVICE_TV */, bool bActive /* = true */) |
| 310 | { |
| 311 | cec_frame frame; |
| 312 | frame.clear(); |
| 313 | |
| 314 | if (bActive) |
| 315 | m_controller->AddLog(CEC_LOG_NOTICE, "<< reporting menu state as active"); |
| 316 | else |
| 317 | m_controller->AddLog(CEC_LOG_NOTICE, "<< reporting menu state as inactive"); |
| 318 | |
| 319 | frame.push_back(GetSourceDestination(address)); |
| 320 | frame.push_back((uint8_t) CEC_OPCODE_MENU_STATUS); |
| 321 | frame.push_back(bActive ? (uint8_t) CEC_MENU_STATE_ACTIVATED : (uint8_t) CEC_MENU_STATE_DEACTIVATED); |
| 322 | Transmit(frame); |
| 323 | } |
| 324 | |
| 325 | void CCECProcessor::ReportVendorID(cec_logical_address address /* = CECDEVICE_TV */) |
| 326 | { |
| 327 | m_controller->AddLog(CEC_LOG_NOTICE, "<< vendor ID requested, feature abort"); |
| 328 | TransmitAbort(address, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID); |
| 329 | } |
| 330 | |
| 331 | void CCECProcessor::ReportOSDName(cec_logical_address address /* = CECDEVICE_TV */) |
| 332 | { |
| 333 | cec_frame frame; |
| 334 | frame.clear(); |
| 335 | |
| 336 | const char *osdname = m_strDeviceName.c_str(); |
| 337 | CStdString strLog; |
| 338 | strLog.Format("<< reporting OSD name as %s", osdname); |
| 339 | m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str()); |
| 340 | frame.push_back(GetSourceDestination(address)); |
| 341 | frame.push_back((uint8_t) CEC_OPCODE_SET_OSD_NAME); |
| 342 | |
| 343 | for (unsigned int i = 0; i < strlen(osdname); i++) |
| 344 | frame.push_back(osdname[i]); |
| 345 | |
| 346 | Transmit(frame); |
| 347 | } |
| 348 | |
| 349 | void CCECProcessor::ReportPhysicalAddress(void) |
| 350 | { |
| 351 | cec_frame frame; |
| 352 | frame.clear(); |
| 353 | |
| 354 | CStdString strLog; |
| 355 | strLog.Format("<< reporting physical address as %04x", m_physicaladdress); |
| 356 | m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str()); |
| 357 | frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST)); |
| 358 | frame.push_back((uint8_t) CEC_OPCODE_REPORT_PHYSICAL_ADDRESS); |
| 359 | frame.push_back((uint8_t) ((m_physicaladdress >> 8) & 0xFF)); |
| 360 | frame.push_back((uint8_t) (m_physicaladdress & 0xFF)); |
| 361 | frame.push_back((uint8_t) CEC_DEVICE_TYPE_PLAYBACK_DEVICE); |
| 362 | Transmit(frame); |
| 363 | } |
| 364 | |
| 365 | void CCECProcessor::BroadcastActiveSource(void) |
| 366 | { |
| 367 | cec_frame frame; |
| 368 | frame.clear(); |
| 369 | |
| 370 | m_controller->AddLog(CEC_LOG_NOTICE, "<< broadcasting active source"); |
| 371 | frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST)); |
| 372 | frame.push_back((uint8_t) CEC_OPCODE_ACTIVE_SOURCE); |
| 373 | frame.push_back((uint8_t) ((m_physicaladdress >> 8) & 0xFF)); |
| 374 | frame.push_back((uint8_t) (m_physicaladdress & 0xFF)); |
| 375 | Transmit(frame); |
| 376 | } |
| 377 | |
| 378 | uint8_t CCECProcessor::GetSourceDestination(cec_logical_address destination /* = CECDEVICE_BROADCAST */) const |
| 379 | { |
| 380 | return ((uint8_t)m_iLogicalAddress << 4) + (uint8_t)destination; |
| 381 | } |
| 382 | |
| 383 | bool CCECProcessor::WaitForAck(bool *bError, uint32_t iTimeout /* = 1000 */) |
| 384 | { |
| 385 | bool bGotAck(false); |
| 386 | *bError = false; |
| 387 | |
| 388 | int64_t iNow = GetTimeMs(); |
| 389 | int64_t iTargetTime = iNow + (uint64_t) iTimeout; |
| 390 | |
| 391 | while (!bGotAck && !*bError && (iTimeout == 0 || iNow < iTargetTime)) |
| 392 | { |
| 393 | cec_frame msg; |
| 394 | msg.clear(); |
| 395 | |
| 396 | if (!m_communication->Read(msg, iTimeout > 0 ? iTargetTime - iNow : 1000)) |
| 397 | { |
| 398 | iNow = GetTimeMs(); |
| 399 | continue; |
| 400 | } |
| 401 | |
| 402 | uint8_t iCode = msg.data[0] & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK); |
| 403 | |
| 404 | switch (iCode) |
| 405 | { |
| 406 | case MSGCODE_COMMAND_ACCEPTED: |
| 407 | m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_COMMAND_ACCEPTED"); |
| 408 | break; |
| 409 | case MSGCODE_TRANSMIT_SUCCEEDED: |
| 410 | m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_TRANSMIT_SUCCEEDED"); |
| 411 | bGotAck = true; |
| 412 | break; |
| 413 | case MSGCODE_RECEIVE_FAILED: |
| 414 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_RECEIVE_FAILED"); |
| 415 | *bError = true; |
| 416 | break; |
| 417 | case MSGCODE_COMMAND_REJECTED: |
| 418 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_COMMAND_REJECTED"); |
| 419 | *bError = true; |
| 420 | break; |
| 421 | case MSGCODE_TRANSMIT_FAILED_LINE: |
| 422 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_LINE"); |
| 423 | *bError = true; |
| 424 | break; |
| 425 | case MSGCODE_TRANSMIT_FAILED_ACK: |
| 426 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_ACK"); |
| 427 | *bError = true; |
| 428 | break; |
| 429 | case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA: |
| 430 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA"); |
| 431 | *bError = true; |
| 432 | break; |
| 433 | case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE: |
| 434 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE"); |
| 435 | *bError = true; |
| 436 | break; |
| 437 | default: |
| 438 | m_frameBuffer.Push(msg); |
| 439 | break; |
| 440 | } |
| 441 | |
| 442 | iNow = GetTimeMs(); |
| 443 | } |
| 444 | |
| 445 | return bGotAck && !*bError; |
| 446 | } |
| 447 | |
| 448 | bool CCECProcessor::ParseMessage(cec_frame &msg) |
| 449 | { |
| 450 | bool bReturn(false); |
| 451 | |
| 452 | if (msg.size == 0) |
| 453 | return bReturn; |
| 454 | |
| 455 | CStdString logStr; |
| 456 | uint8_t iCode = msg.data[0] & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK); |
| 457 | bool bEom = (msg.data[0] & MSGCODE_FRAME_EOM) != 0; |
| 458 | bool bAck = (msg.data[0] & MSGCODE_FRAME_ACK) != 0; |
| 459 | |
| 460 | switch(iCode) |
| 461 | { |
| 462 | case MSGCODE_NOTHING: |
| 463 | m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_NOTHING"); |
| 464 | break; |
| 465 | case MSGCODE_TIMEOUT_ERROR: |
| 466 | case MSGCODE_HIGH_ERROR: |
| 467 | case MSGCODE_LOW_ERROR: |
| 468 | { |
| 469 | if (iCode == MSGCODE_TIMEOUT_ERROR) |
| 470 | logStr = "MSGCODE_TIMEOUT"; |
| 471 | else if (iCode == MSGCODE_HIGH_ERROR) |
| 472 | logStr = "MSGCODE_HIGH_ERROR"; |
| 473 | else |
| 474 | logStr = "MSGCODE_LOW_ERROR"; |
| 475 | |
| 476 | int iLine = (msg.size >= 3) ? (msg.data[1] << 8) | (msg.data[2]) : 0; |
| 477 | uint32_t iTime = (msg.size >= 7) ? (msg.data[3] << 24) | (msg.data[4] << 16) | (msg.data[5] << 8) | (msg.data[6]) : 0; |
| 478 | logStr.AppendFormat(" line:%i", iLine); |
| 479 | logStr.AppendFormat(" time:%u", iTime); |
| 480 | m_controller->AddLog(CEC_LOG_WARNING, logStr.c_str()); |
| 481 | } |
| 482 | break; |
| 483 | case MSGCODE_FRAME_START: |
| 484 | { |
| 485 | logStr = "MSGCODE_FRAME_START"; |
| 486 | m_currentframe.clear(); |
| 487 | if (msg.size >= 2) |
| 488 | { |
| 489 | int iInitiator = msg.data[1] >> 4; |
| 490 | int iDestination = msg.data[1] & 0xF; |
| 491 | logStr.AppendFormat(" initiator:%u destination:%u ack:%s %s", iInitiator, iDestination, bAck ? "high" : "low", bEom ? "eom" : ""); |
| 492 | |
| 493 | m_currentframe.push_back(msg.data[1]); |
| 494 | } |
| 495 | m_controller->AddLog(CEC_LOG_DEBUG, logStr.c_str()); |
| 496 | } |
| 497 | break; |
| 498 | case MSGCODE_FRAME_DATA: |
| 499 | { |
| 500 | logStr = "MSGCODE_FRAME_DATA"; |
| 501 | if (msg.size >= 2) |
| 502 | { |
| 503 | uint8_t iData = msg.data[1]; |
| 504 | logStr.AppendFormat(" %02x", iData); |
| 505 | m_currentframe.push_back(iData); |
| 506 | } |
| 507 | m_controller->AddLog(CEC_LOG_DEBUG, logStr.c_str()); |
| 508 | } |
| 509 | if (bEom) |
| 510 | bReturn = true; |
| 511 | break; |
| 512 | default: |
| 513 | break; |
| 514 | } |
| 515 | |
| 516 | return bReturn; |
| 517 | } |
| 518 | |
| 519 | void CCECProcessor::ParseVendorId(cec_logical_address device, cec_frame data) |
| 520 | { |
| 521 | if (data.size < 3) |
| 522 | { |
| 523 | m_controller->AddLog(CEC_LOG_WARNING, "invalid vendor ID received"); |
| 524 | return; |
| 525 | } |
| 526 | |
| 527 | uint64_t iVendorId = ((uint64_t)data.data[0] << 3) + |
| 528 | ((uint64_t)data.data[1] << 2) + |
| 529 | (uint64_t)data.data[2]; |
| 530 | |
| 531 | m_vendorIds[(uint8_t)device] = iVendorId; |
| 532 | m_vendorClasses[(uint8_t)device] = data.size >= 4 ? data.data[3] : 0; |
| 533 | |
| 534 | CStdString strLog; |
| 535 | strLog.Format("device %d: vendor = %s (%lld) class = %2x", (uint8_t)device, CECVendorIdToString(m_vendorIds[(uint8_t)device]), iVendorId, m_vendorClasses[(uint8_t)device]); |
| 536 | m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
| 537 | } |
| 538 | |
| 539 | void CCECProcessor::ParseCurrentFrame(cec_frame &frame) |
| 540 | { |
| 541 | uint8_t initiator = frame.data[0] >> 4; |
| 542 | uint8_t destination = frame.data[0] & 0xF; |
| 543 | |
| 544 | CStdString dataStr; |
| 545 | dataStr.Format(">> received frame: initiator: %u destination: %u", initiator, destination); |
| 546 | |
| 547 | if (frame.size > 1) |
| 548 | { |
| 549 | dataStr += " data:"; |
| 550 | for (unsigned int i = 1; i < frame.size; i++) |
| 551 | dataStr.AppendFormat(" %02x", frame.data[i]); |
| 552 | } |
| 553 | m_controller->AddLog(CEC_LOG_DEBUG, dataStr.c_str()); |
| 554 | |
| 555 | if (frame.size <= 1) |
| 556 | return; |
| 557 | |
| 558 | cec_opcode opCode = (cec_opcode) frame.data[1]; |
| 559 | if (destination == (uint16_t) m_iLogicalAddress) |
| 560 | { |
| 561 | switch(opCode) |
| 562 | { |
| 563 | case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS: |
| 564 | ReportPhysicalAddress(); |
| 565 | break; |
| 566 | case CEC_OPCODE_GIVE_OSD_NAME: |
| 567 | ReportOSDName((cec_logical_address)initiator); |
| 568 | break; |
| 569 | case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID: |
| 570 | ReportVendorID((cec_logical_address)initiator); |
| 571 | break; |
| 572 | case CEC_OPCODE_VENDOR_COMMAND_WITH_ID: |
| 573 | frame.shift(2); |
| 574 | ParseVendorId((cec_logical_address)initiator, frame); |
| 575 | TransmitAbort((cec_logical_address)initiator, CEC_OPCODE_VENDOR_COMMAND_WITH_ID); |
| 576 | break; |
| 577 | case CEC_OPCODE_GIVE_DECK_STATUS: |
| 578 | // need to support opcodes play and deck control before doing anything with this |
| 579 | TransmitAbort((cec_logical_address)initiator, CEC_OPCODE_GIVE_DECK_STATUS); |
| 580 | break; |
| 581 | case CEC_OPCODE_MENU_REQUEST: |
| 582 | frame.shift(2); |
| 583 | if (frame.data[0] == CEC_MENU_REQUEST_TYPE_QUERY) |
| 584 | ReportMenuState((cec_logical_address)initiator); |
| 585 | break; |
| 586 | case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS: |
| 587 | ReportPowerState((cec_logical_address)initiator); |
| 588 | SetActiveView(); |
| 589 | break; |
| 590 | case CEC_OPCODE_GET_CEC_VERSION: |
| 591 | ReportCECVersion((cec_logical_address)initiator); |
| 592 | break; |
| 593 | case CEC_OPCODE_USER_CONTROL_PRESSED: |
| 594 | if (frame.size > 2) |
| 595 | { |
| 596 | m_controller->AddKey(); |
| 597 | |
| 598 | if (frame.data[2] <= CEC_USER_CONTROL_CODE_MAX) |
| 599 | m_controller->SetCurrentButton((cec_user_control_code) frame.data[2]); |
| 600 | } |
| 601 | break; |
| 602 | case CEC_OPCODE_USER_CONTROL_RELEASE: |
| 603 | m_controller->AddKey(); |
| 604 | break; |
| 605 | default: |
| 606 | frame.shift(2); |
| 607 | m_controller->AddCommand((cec_logical_address) initiator, (cec_logical_address) destination, opCode, &frame); |
| 608 | break; |
| 609 | } |
| 610 | } |
| 611 | else if (destination == (uint8_t) CECDEVICE_BROADCAST) |
| 612 | { |
| 613 | CStdString strLog; |
| 614 | if (opCode == CEC_OPCODE_REQUEST_ACTIVE_SOURCE) |
| 615 | { |
| 616 | strLog.Format(">> %i requests active source", initiator); |
| 617 | m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
| 618 | BroadcastActiveSource(); |
| 619 | } |
| 620 | else if (opCode == CEC_OPCODE_SET_STREAM_PATH) |
| 621 | { |
| 622 | if (frame.size >= 4) |
| 623 | { |
| 624 | int streamaddr = ((int)frame.data[2] << 8) | ((int)frame.data[3]); |
| 625 | strLog.Format(">> %i requests stream path from physical address %04x", initiator, streamaddr); |
| 626 | m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
| 627 | if (streamaddr == m_physicaladdress) |
| 628 | BroadcastActiveSource(); |
| 629 | } |
| 630 | } |
| 631 | else |
| 632 | { |
| 633 | cec_frame params = frame; |
| 634 | frame.shift(2); |
| 635 | m_controller->AddCommand((cec_logical_address) initiator, (cec_logical_address) destination, opCode, ¶ms); |
| 636 | } |
| 637 | } |
| 638 | else |
| 639 | { |
| 640 | CStdString strLog; |
| 641 | strLog.Format("ignoring frame: destination: %u != %u", destination, (uint16_t)m_iLogicalAddress); |
| 642 | m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
| 643 | } |
| 644 | } |