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