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