| 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 "devices/CECBusDevice.h" |
| 37 | #include "LibCEC.h" |
| 38 | #include "util/StdString.h" |
| 39 | #include "platform/timeutils.h" |
| 40 | |
| 41 | using namespace CEC; |
| 42 | using namespace std; |
| 43 | |
| 44 | CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm, const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) : |
| 45 | m_iLogicalAddress(CECDEVICE_UNKNOWN), |
| 46 | m_strDeviceName(strDeviceName), |
| 47 | m_communication(serComm), |
| 48 | m_controller(controller), |
| 49 | m_bMonitor(false) |
| 50 | { |
| 51 | for (int iPtr = 0; iPtr < 16; iPtr++) |
| 52 | m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, iPtr == iLogicalAddress ? iPhysicalAddress : 0); |
| 53 | } |
| 54 | |
| 55 | CCECProcessor::~CCECProcessor(void) |
| 56 | { |
| 57 | m_startCondition.Broadcast(); |
| 58 | StopThread(); |
| 59 | m_communication = NULL; |
| 60 | m_controller = NULL; |
| 61 | for (unsigned int iPtr = 0; iPtr < 16; iPtr++) |
| 62 | delete m_busDevices[iPtr]; |
| 63 | } |
| 64 | |
| 65 | bool CCECProcessor::Start(void) |
| 66 | { |
| 67 | CLockObject lock(&m_mutex); |
| 68 | if (!m_communication || !m_communication->IsOpen()) |
| 69 | { |
| 70 | m_controller->AddLog(CEC_LOG_ERROR, "connection is closed"); |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | if (!SetLogicalAddress(m_iLogicalAddress)) |
| 75 | { |
| 76 | m_controller->AddLog(CEC_LOG_ERROR, "could not set the logical address"); |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | if (CreateThread()) |
| 81 | { |
| 82 | if (!m_startCondition.Wait(&m_mutex)) |
| 83 | { |
| 84 | m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread"); |
| 85 | return false; |
| 86 | } |
| 87 | return true; |
| 88 | } |
| 89 | else |
| 90 | m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread"); |
| 91 | |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | void *CCECProcessor::Process(void) |
| 96 | { |
| 97 | { |
| 98 | CLockObject lock(&m_mutex); |
| 99 | m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started"); |
| 100 | m_startCondition.Signal(); |
| 101 | } |
| 102 | |
| 103 | cec_command command; |
| 104 | CCECAdapterMessage msg; |
| 105 | CCECAdapterMessagePtr msgPtr; |
| 106 | |
| 107 | m_communication->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress); |
| 108 | |
| 109 | while (!IsStopped()) |
| 110 | { |
| 111 | bool bParseFrame(false); |
| 112 | command.clear(); |
| 113 | msg.clear(); |
| 114 | |
| 115 | { |
| 116 | CLockObject lock(&m_mutex); |
| 117 | if (m_frameBuffer.Pop(msgPtr)) |
| 118 | bParseFrame = ParseMessage(msgPtr); |
| 119 | else if (m_communication->IsOpen() && m_communication->Read(msg, 50)) |
| 120 | { |
| 121 | msgPtr = CCECAdapterMessagePtr(new CCECAdapterMessage(msg)); |
| 122 | bParseFrame = ParseMessage(msgPtr); |
| 123 | } |
| 124 | |
| 125 | bParseFrame &= !IsStopped(); |
| 126 | if (bParseFrame) |
| 127 | command = m_currentframe; |
| 128 | } |
| 129 | |
| 130 | if (bParseFrame) |
| 131 | ParseCommand(command); |
| 132 | |
| 133 | m_controller->CheckKeypressTimeout(); |
| 134 | |
| 135 | for (unsigned int iDevicePtr = 0; iDevicePtr < 16; iDevicePtr++) |
| 136 | m_busDevices[iDevicePtr]->PollVendorId(); |
| 137 | |
| 138 | if (!IsStopped()) |
| 139 | Sleep(5); |
| 140 | } |
| 141 | |
| 142 | return NULL; |
| 143 | } |
| 144 | |
| 145 | bool CCECProcessor::SetActiveView(void) |
| 146 | { |
| 147 | if (!IsRunning()) |
| 148 | return false; |
| 149 | |
| 150 | return m_busDevices[m_iLogicalAddress]->BroadcastActiveView(); |
| 151 | } |
| 152 | |
| 153 | bool CCECProcessor::SetInactiveView(void) |
| 154 | { |
| 155 | if (!IsRunning()) |
| 156 | return false; |
| 157 | |
| 158 | return m_busDevices[m_iLogicalAddress]->BroadcastInactiveView(); |
| 159 | } |
| 160 | |
| 161 | void CCECProcessor::LogOutput(const cec_command &data) |
| 162 | { |
| 163 | CStdString strTx; |
| 164 | strTx.Format("<< %02x:%02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination, (uint8_t)data.opcode); |
| 165 | |
| 166 | for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++) |
| 167 | strTx.AppendFormat(":%02x", data.parameters[iPtr]); |
| 168 | m_controller->AddLog(CEC_LOG_TRAFFIC, strTx.c_str()); |
| 169 | } |
| 170 | |
| 171 | bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress) |
| 172 | { |
| 173 | if (m_iLogicalAddress != iLogicalAddress) |
| 174 | { |
| 175 | CStdString strLog; |
| 176 | strLog.Format("<< setting logical address to %1x", iLogicalAddress); |
| 177 | m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str()); |
| 178 | |
| 179 | m_iLogicalAddress = iLogicalAddress; |
| 180 | return m_communication && m_communication->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress); |
| 181 | } |
| 182 | |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress) |
| 187 | { |
| 188 | m_busDevices[m_iLogicalAddress]->SetPhysicalAddress(iPhysicalAddress); |
| 189 | return m_busDevices[m_iLogicalAddress]->BroadcastActiveView(); |
| 190 | } |
| 191 | |
| 192 | bool CCECProcessor::SwitchMonitoring(bool bEnable) |
| 193 | { |
| 194 | CStdString strLog; |
| 195 | strLog.Format("== %s monitoring mode ==", bEnable ? "enabling" : "disabling"); |
| 196 | m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str()); |
| 197 | |
| 198 | m_bMonitor = bEnable; |
| 199 | if (bEnable) |
| 200 | return m_communication && m_communication->SetAckMask(0); |
| 201 | else |
| 202 | return m_communication && m_communication->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress); |
| 203 | } |
| 204 | |
| 205 | cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress) |
| 206 | { |
| 207 | return m_busDevices[iAddress]->GetCecVersion(); |
| 208 | } |
| 209 | |
| 210 | bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language) |
| 211 | { |
| 212 | *language = m_busDevices[iAddress]->GetMenuLanguage(); |
| 213 | return (strcmp(language->language, "???")); |
| 214 | } |
| 215 | |
| 216 | uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress) |
| 217 | { |
| 218 | return m_busDevices[iAddress]->GetVendorId(); |
| 219 | } |
| 220 | |
| 221 | cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress) |
| 222 | { |
| 223 | return m_busDevices[iAddress]->GetPowerStatus(); |
| 224 | } |
| 225 | |
| 226 | bool CCECProcessor::Transmit(const cec_command &data) |
| 227 | { |
| 228 | bool bReturn(false); |
| 229 | LogOutput(data); |
| 230 | |
| 231 | CCECAdapterMessagePtr output(new CCECAdapterMessage(data)); |
| 232 | |
| 233 | CLockObject lock(&m_mutex); |
| 234 | { |
| 235 | CLockObject msgLock(&output->mutex); |
| 236 | if (!m_communication || !m_communication->Write(output)) |
| 237 | return bReturn; |
| 238 | else |
| 239 | { |
| 240 | output->condition.Wait(&output->mutex); |
| 241 | if (output->state != ADAPTER_MESSAGE_STATE_SENT) |
| 242 | { |
| 243 | m_controller->AddLog(CEC_LOG_ERROR, "command was not sent"); |
| 244 | return bReturn; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | if (data.ack_timeout > 0) |
| 249 | { |
| 250 | bool bError(false); |
| 251 | if ((bReturn = WaitForAck(&bError, output->size(), data.ack_timeout)) == false) |
| 252 | m_controller->AddLog(CEC_LOG_ERROR, "did not receive ack"); |
| 253 | } |
| 254 | else |
| 255 | { |
| 256 | bReturn = true; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | return bReturn; |
| 261 | } |
| 262 | |
| 263 | void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, ECecAbortReason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */) |
| 264 | { |
| 265 | m_controller->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message"); |
| 266 | |
| 267 | cec_command command; |
| 268 | cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_FEATURE_ABORT); |
| 269 | command.parameters.push_back((uint8_t)opcode); |
| 270 | command.parameters.push_back((uint8_t)reason); |
| 271 | |
| 272 | Transmit(command); |
| 273 | } |
| 274 | |
| 275 | bool CCECProcessor::WaitForAck(bool *bError, uint8_t iLength, uint32_t iTimeout /* = 1000 */) |
| 276 | { |
| 277 | bool bTransmitSucceeded = false; |
| 278 | uint8_t iPacketsLeft(iLength / 4); |
| 279 | *bError = false; |
| 280 | |
| 281 | int64_t iNow = GetTimeMs(); |
| 282 | int64_t iTargetTime = iNow + (uint64_t) iTimeout; |
| 283 | |
| 284 | while (!bTransmitSucceeded && !*bError && (iTimeout == 0 || iNow < iTargetTime)) |
| 285 | { |
| 286 | CCECAdapterMessage msg; |
| 287 | |
| 288 | if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000)) |
| 289 | { |
| 290 | iNow = GetTimeMs(); |
| 291 | continue; |
| 292 | } |
| 293 | |
| 294 | switch(msg.message()) |
| 295 | { |
| 296 | case MSGCODE_TIMEOUT_ERROR: |
| 297 | case MSGCODE_HIGH_ERROR: |
| 298 | case MSGCODE_LOW_ERROR: |
| 299 | { |
| 300 | CStdString logStr; |
| 301 | if (msg.message() == MSGCODE_TIMEOUT_ERROR) |
| 302 | logStr = "MSGCODE_TIMEOUT"; |
| 303 | else if (msg.message() == MSGCODE_HIGH_ERROR) |
| 304 | logStr = "MSGCODE_HIGH_ERROR"; |
| 305 | else |
| 306 | logStr = "MSGCODE_LOW_ERROR"; |
| 307 | |
| 308 | int iLine = (msg.size() >= 3) ? (msg[1] << 8) | (msg[2]) : 0; |
| 309 | uint32_t iTime = (msg.size() >= 7) ? (msg[3] << 24) | (msg[4] << 16) | (msg[5] << 8) | (msg[6]) : 0; |
| 310 | logStr.AppendFormat(" line:%i", iLine); |
| 311 | logStr.AppendFormat(" time:%u", iTime); |
| 312 | m_controller->AddLog(CEC_LOG_WARNING, logStr.c_str()); |
| 313 | *bError = true; |
| 314 | } |
| 315 | break; |
| 316 | case MSGCODE_COMMAND_ACCEPTED: |
| 317 | m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_COMMAND_ACCEPTED"); |
| 318 | iPacketsLeft--; |
| 319 | break; |
| 320 | case MSGCODE_TRANSMIT_SUCCEEDED: |
| 321 | m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_TRANSMIT_SUCCEEDED"); |
| 322 | bTransmitSucceeded = (iPacketsLeft == 0); |
| 323 | *bError = !bTransmitSucceeded; |
| 324 | break; |
| 325 | case MSGCODE_RECEIVE_FAILED: |
| 326 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_RECEIVE_FAILED"); |
| 327 | *bError = true; |
| 328 | break; |
| 329 | case MSGCODE_COMMAND_REJECTED: |
| 330 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_COMMAND_REJECTED"); |
| 331 | *bError = true; |
| 332 | break; |
| 333 | case MSGCODE_TRANSMIT_FAILED_LINE: |
| 334 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_LINE"); |
| 335 | *bError = true; |
| 336 | break; |
| 337 | case MSGCODE_TRANSMIT_FAILED_ACK: |
| 338 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_ACK"); |
| 339 | *bError = true; |
| 340 | break; |
| 341 | case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA: |
| 342 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA"); |
| 343 | *bError = true; |
| 344 | break; |
| 345 | case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE: |
| 346 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE"); |
| 347 | *bError = true; |
| 348 | break; |
| 349 | default: |
| 350 | CCECAdapterMessagePtr msgPtr = CCECAdapterMessagePtr(new CCECAdapterMessage(msg)); |
| 351 | m_frameBuffer.Push(msgPtr); |
| 352 | break; |
| 353 | } |
| 354 | |
| 355 | iNow = GetTimeMs(); |
| 356 | } |
| 357 | |
| 358 | return bTransmitSucceeded && !*bError; |
| 359 | } |
| 360 | |
| 361 | bool CCECProcessor::ParseMessage(CCECAdapterMessagePtr msg) |
| 362 | { |
| 363 | bool bEom = false; |
| 364 | |
| 365 | if (msg->empty()) |
| 366 | return bEom; |
| 367 | |
| 368 | CStdString logStr; |
| 369 | |
| 370 | switch(msg->message()) |
| 371 | { |
| 372 | case MSGCODE_NOTHING: |
| 373 | m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_NOTHING"); |
| 374 | break; |
| 375 | case MSGCODE_TIMEOUT_ERROR: |
| 376 | case MSGCODE_HIGH_ERROR: |
| 377 | case MSGCODE_LOW_ERROR: |
| 378 | { |
| 379 | if (msg->message() == MSGCODE_TIMEOUT_ERROR) |
| 380 | logStr = "MSGCODE_TIMEOUT"; |
| 381 | else if (msg->message() == MSGCODE_HIGH_ERROR) |
| 382 | logStr = "MSGCODE_HIGH_ERROR"; |
| 383 | else |
| 384 | logStr = "MSGCODE_LOW_ERROR"; |
| 385 | |
| 386 | int iLine = (msg->size() >= 3) ? (msg->at(1) << 8) | (msg->at(2)) : 0; |
| 387 | uint32_t iTime = (msg->size() >= 7) ? (msg->at(3) << 24) | (msg->at(4) << 16) | (msg->at(5) << 8) | (msg->at(6)) : 0; |
| 388 | logStr.AppendFormat(" line:%i", iLine); |
| 389 | logStr.AppendFormat(" time:%u", iTime); |
| 390 | m_controller->AddLog(CEC_LOG_WARNING, logStr.c_str()); |
| 391 | } |
| 392 | break; |
| 393 | case MSGCODE_FRAME_START: |
| 394 | { |
| 395 | logStr = "MSGCODE_FRAME_START"; |
| 396 | m_currentframe.clear(); |
| 397 | if (msg->size() >= 2) |
| 398 | { |
| 399 | logStr.AppendFormat(" initiator:%u destination:%u ack:%s %s", msg->initiator(), msg->destination(), msg->ack() ? "high" : "low", msg->eom() ? "eom" : ""); |
| 400 | m_currentframe.initiator = msg->initiator(); |
| 401 | m_currentframe.destination = msg->destination(); |
| 402 | m_currentframe.ack = msg->ack(); |
| 403 | m_currentframe.eom = msg->eom(); |
| 404 | } |
| 405 | m_controller->AddLog(CEC_LOG_DEBUG, logStr.c_str()); |
| 406 | } |
| 407 | break; |
| 408 | case MSGCODE_FRAME_DATA: |
| 409 | { |
| 410 | logStr = "MSGCODE_FRAME_DATA"; |
| 411 | if (msg->size() >= 2) |
| 412 | { |
| 413 | uint8_t iData = msg->at(1); |
| 414 | logStr.AppendFormat(" %02x", iData); |
| 415 | m_currentframe.push_back(iData); |
| 416 | m_currentframe.eom = msg->eom(); |
| 417 | } |
| 418 | m_controller->AddLog(CEC_LOG_DEBUG, logStr.c_str()); |
| 419 | |
| 420 | bEom = msg->eom(); |
| 421 | } |
| 422 | break; |
| 423 | case MSGCODE_COMMAND_ACCEPTED: |
| 424 | m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_COMMAND_ACCEPTED"); |
| 425 | break; |
| 426 | case MSGCODE_TRANSMIT_SUCCEEDED: |
| 427 | m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_TRANSMIT_SUCCEEDED"); |
| 428 | break; |
| 429 | case MSGCODE_RECEIVE_FAILED: |
| 430 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_RECEIVE_FAILED"); |
| 431 | break; |
| 432 | case MSGCODE_COMMAND_REJECTED: |
| 433 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_COMMAND_REJECTED"); |
| 434 | break; |
| 435 | case MSGCODE_TRANSMIT_FAILED_LINE: |
| 436 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_LINE"); |
| 437 | break; |
| 438 | case MSGCODE_TRANSMIT_FAILED_ACK: |
| 439 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_ACK"); |
| 440 | break; |
| 441 | case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA: |
| 442 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA"); |
| 443 | break; |
| 444 | case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE: |
| 445 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE"); |
| 446 | break; |
| 447 | default: |
| 448 | break; |
| 449 | } |
| 450 | |
| 451 | return bEom; |
| 452 | } |
| 453 | |
| 454 | void CCECProcessor::ParseCommand(cec_command &command) |
| 455 | { |
| 456 | CStdString dataStr; |
| 457 | dataStr.Format(">> %1x%1x:%02x", command.initiator, command.destination, command.opcode); |
| 458 | for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++) |
| 459 | dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]); |
| 460 | m_controller->AddLog(CEC_LOG_TRAFFIC, dataStr.c_str()); |
| 461 | |
| 462 | if (!m_bMonitor) |
| 463 | m_busDevices[(uint8_t)command.initiator]->HandleCommand(command); |
| 464 | } |
| 465 | |
| 466 | uint16_t CCECProcessor::GetPhysicalAddress(void) const |
| 467 | { |
| 468 | return m_busDevices[m_iLogicalAddress]->GetPhysicalAddress(); |
| 469 | } |
| 470 | |
| 471 | void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode) |
| 472 | { |
| 473 | m_controller->SetCurrentButton(iButtonCode); |
| 474 | } |
| 475 | |
| 476 | void CCECProcessor::AddCommand(const cec_command &command) |
| 477 | { |
| 478 | m_controller->AddCommand(command); |
| 479 | } |
| 480 | |
| 481 | void CCECProcessor::AddKey(void) |
| 482 | { |
| 483 | m_controller->AddKey(); |
| 484 | } |
| 485 | |
| 486 | void CCECProcessor::AddLog(cec_log_level level, const CStdString &strMessage) |
| 487 | { |
| 488 | m_controller->AddLog(level, strMessage); |
| 489 | } |