| 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(iLogicalAddress), |
| 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 | StopThread(); |
| 58 | m_communication = NULL; |
| 59 | m_controller = NULL; |
| 60 | for (unsigned int iPtr = 0; iPtr < 16; iPtr++) |
| 61 | delete m_busDevices[iPtr]; |
| 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 | CCECAdapterMessage msg; |
| 92 | |
| 93 | while (!IsStopped()) |
| 94 | { |
| 95 | bool bParseFrame(false); |
| 96 | command.clear(); |
| 97 | msg.clear(); |
| 98 | |
| 99 | { |
| 100 | CLockObject lock(&m_mutex); |
| 101 | if (m_communication->IsOpen() && m_communication->Read(msg, 50)) |
| 102 | bParseFrame = ParseMessage(msg); |
| 103 | |
| 104 | bParseFrame &= !IsStopped(); |
| 105 | if (bParseFrame) |
| 106 | command = m_currentframe; |
| 107 | } |
| 108 | |
| 109 | if (bParseFrame) |
| 110 | ParseCommand(command); |
| 111 | |
| 112 | m_controller->CheckKeypressTimeout(); |
| 113 | |
| 114 | for (unsigned int iDevicePtr = 0; iDevicePtr < 16; iDevicePtr++) |
| 115 | m_busDevices[iDevicePtr]->PollVendorId(); |
| 116 | |
| 117 | if (!IsStopped()) |
| 118 | Sleep(5); |
| 119 | } |
| 120 | |
| 121 | return NULL; |
| 122 | } |
| 123 | |
| 124 | bool CCECProcessor::SetActiveView(void) |
| 125 | { |
| 126 | if (!IsRunning()) |
| 127 | return false; |
| 128 | |
| 129 | return m_busDevices[m_iLogicalAddress]->BroadcastActiveView(); |
| 130 | } |
| 131 | |
| 132 | bool CCECProcessor::SetInactiveView(void) |
| 133 | { |
| 134 | if (!IsRunning()) |
| 135 | return false; |
| 136 | |
| 137 | return m_busDevices[m_iLogicalAddress]->BroadcastInactiveView(); |
| 138 | } |
| 139 | |
| 140 | void CCECProcessor::LogOutput(const cec_command &data) |
| 141 | { |
| 142 | CStdString strTx; |
| 143 | strTx.Format("<< %02x:%02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination, (uint8_t)data.opcode); |
| 144 | |
| 145 | for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++) |
| 146 | strTx.AppendFormat(":%02x", data.parameters[iPtr]); |
| 147 | m_controller->AddLog(CEC_LOG_TRAFFIC, strTx.c_str()); |
| 148 | } |
| 149 | |
| 150 | bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress) |
| 151 | { |
| 152 | CStdString strLog; |
| 153 | strLog.Format("<< setting logical address to %1x", iLogicalAddress); |
| 154 | m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str()); |
| 155 | |
| 156 | m_iLogicalAddress = iLogicalAddress; |
| 157 | return m_communication && m_communication->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress); |
| 158 | } |
| 159 | |
| 160 | bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress) |
| 161 | { |
| 162 | m_busDevices[m_iLogicalAddress]->SetPhysicalAddress(iPhysicalAddress); |
| 163 | return m_busDevices[m_iLogicalAddress]->BroadcastActiveView(); |
| 164 | } |
| 165 | |
| 166 | bool CCECProcessor::SwitchMonitoring(bool bEnable) |
| 167 | { |
| 168 | CStdString strLog; |
| 169 | strLog.Format("== %s monitoring mode ==", bEnable ? "enabling" : "disabling"); |
| 170 | m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str()); |
| 171 | |
| 172 | m_bMonitor = bEnable; |
| 173 | if (bEnable) |
| 174 | return m_communication && m_communication->SetAckMask(0); |
| 175 | else |
| 176 | return m_communication && m_communication->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress); |
| 177 | } |
| 178 | |
| 179 | bool CCECProcessor::Transmit(const cec_command &data, bool bWaitForAck /* = true */) |
| 180 | { |
| 181 | bool bReturn(false); |
| 182 | LogOutput(data); |
| 183 | |
| 184 | CCECAdapterMessage output(data); |
| 185 | |
| 186 | CLockObject lock(&m_mutex); |
| 187 | if (!m_communication || !m_communication->Write(output)) |
| 188 | return bReturn; |
| 189 | |
| 190 | if (bWaitForAck) |
| 191 | { |
| 192 | uint64_t now = GetTimeMs(); |
| 193 | uint64_t target = now + 1000; |
| 194 | bool bError(false); |
| 195 | |
| 196 | while (!bReturn && now < target && !bError) |
| 197 | { |
| 198 | bReturn = WaitForAck(&bError, output.size(), (uint32_t) (target - now)); |
| 199 | now = GetTimeMs(); |
| 200 | } |
| 201 | |
| 202 | if (!bReturn) |
| 203 | m_controller->AddLog(CEC_LOG_ERROR, "did not receive ack"); |
| 204 | } |
| 205 | else |
| 206 | { |
| 207 | bReturn = true; |
| 208 | } |
| 209 | |
| 210 | return bReturn; |
| 211 | } |
| 212 | |
| 213 | void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, ECecAbortReason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */) |
| 214 | { |
| 215 | m_controller->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message"); |
| 216 | |
| 217 | cec_command command; |
| 218 | cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_FEATURE_ABORT); |
| 219 | command.parameters.push_back((uint8_t)opcode); |
| 220 | command.parameters.push_back((uint8_t)reason); |
| 221 | |
| 222 | Transmit(command); |
| 223 | } |
| 224 | |
| 225 | bool CCECProcessor::WaitForAck(bool *bError, uint8_t iLength, uint32_t iTimeout /* = 1000 */) |
| 226 | { |
| 227 | bool bTransmitSucceeded = false; |
| 228 | uint8_t iPacketsLeft(iLength / 4); |
| 229 | *bError = false; |
| 230 | |
| 231 | int64_t iNow = GetTimeMs(); |
| 232 | int64_t iTargetTime = iNow + (uint64_t) iTimeout; |
| 233 | |
| 234 | while (!bTransmitSucceeded && !*bError && (iTimeout == 0 || iNow < iTargetTime)) |
| 235 | { |
| 236 | CCECAdapterMessage msg; |
| 237 | |
| 238 | if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000)) |
| 239 | { |
| 240 | iNow = GetTimeMs(); |
| 241 | continue; |
| 242 | } |
| 243 | |
| 244 | switch(msg.message()) |
| 245 | { |
| 246 | case MSGCODE_TIMEOUT_ERROR: |
| 247 | case MSGCODE_HIGH_ERROR: |
| 248 | case MSGCODE_LOW_ERROR: |
| 249 | { |
| 250 | CStdString logStr; |
| 251 | if (msg.message() == MSGCODE_TIMEOUT_ERROR) |
| 252 | logStr = "MSGCODE_TIMEOUT"; |
| 253 | else if (msg.message() == MSGCODE_HIGH_ERROR) |
| 254 | logStr = "MSGCODE_HIGH_ERROR"; |
| 255 | else |
| 256 | logStr = "MSGCODE_LOW_ERROR"; |
| 257 | |
| 258 | int iLine = (msg.size() >= 3) ? (msg[1] << 8) | (msg[2]) : 0; |
| 259 | uint32_t iTime = (msg.size() >= 7) ? (msg[3] << 24) | (msg[4] << 16) | (msg[5] << 8) | (msg[6]) : 0; |
| 260 | logStr.AppendFormat(" line:%i", iLine); |
| 261 | logStr.AppendFormat(" time:%u", iTime); |
| 262 | m_controller->AddLog(CEC_LOG_WARNING, logStr.c_str()); |
| 263 | *bError = true; |
| 264 | } |
| 265 | break; |
| 266 | case MSGCODE_COMMAND_ACCEPTED: |
| 267 | m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_COMMAND_ACCEPTED"); |
| 268 | iPacketsLeft--; |
| 269 | break; |
| 270 | case MSGCODE_TRANSMIT_SUCCEEDED: |
| 271 | m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_TRANSMIT_SUCCEEDED"); |
| 272 | bTransmitSucceeded = (iPacketsLeft == 0); |
| 273 | *bError = !bTransmitSucceeded; |
| 274 | break; |
| 275 | case MSGCODE_RECEIVE_FAILED: |
| 276 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_RECEIVE_FAILED"); |
| 277 | *bError = true; |
| 278 | break; |
| 279 | case MSGCODE_COMMAND_REJECTED: |
| 280 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_COMMAND_REJECTED"); |
| 281 | *bError = true; |
| 282 | break; |
| 283 | case MSGCODE_TRANSMIT_FAILED_LINE: |
| 284 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_LINE"); |
| 285 | *bError = true; |
| 286 | break; |
| 287 | case MSGCODE_TRANSMIT_FAILED_ACK: |
| 288 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_ACK"); |
| 289 | *bError = true; |
| 290 | break; |
| 291 | case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA: |
| 292 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA"); |
| 293 | *bError = true; |
| 294 | break; |
| 295 | case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE: |
| 296 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE"); |
| 297 | *bError = true; |
| 298 | break; |
| 299 | default: |
| 300 | m_frameBuffer.Push(msg); |
| 301 | break; |
| 302 | } |
| 303 | |
| 304 | iNow = GetTimeMs(); |
| 305 | } |
| 306 | |
| 307 | return bTransmitSucceeded && !*bError; |
| 308 | } |
| 309 | |
| 310 | bool CCECProcessor::ParseMessage(CCECAdapterMessage &msg) |
| 311 | { |
| 312 | bool bEom = false; |
| 313 | |
| 314 | if (msg.empty()) |
| 315 | return bEom; |
| 316 | |
| 317 | CStdString logStr; |
| 318 | |
| 319 | switch(msg.message()) |
| 320 | { |
| 321 | case MSGCODE_NOTHING: |
| 322 | m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_NOTHING"); |
| 323 | break; |
| 324 | case MSGCODE_TIMEOUT_ERROR: |
| 325 | case MSGCODE_HIGH_ERROR: |
| 326 | case MSGCODE_LOW_ERROR: |
| 327 | { |
| 328 | if (msg.message() == MSGCODE_TIMEOUT_ERROR) |
| 329 | logStr = "MSGCODE_TIMEOUT"; |
| 330 | else if (msg.message() == MSGCODE_HIGH_ERROR) |
| 331 | logStr = "MSGCODE_HIGH_ERROR"; |
| 332 | else |
| 333 | logStr = "MSGCODE_LOW_ERROR"; |
| 334 | |
| 335 | int iLine = (msg.size() >= 3) ? (msg[1] << 8) | (msg[2]) : 0; |
| 336 | uint32_t iTime = (msg.size() >= 7) ? (msg[3] << 24) | (msg[4] << 16) | (msg[5] << 8) | (msg[6]) : 0; |
| 337 | logStr.AppendFormat(" line:%i", iLine); |
| 338 | logStr.AppendFormat(" time:%u", iTime); |
| 339 | m_controller->AddLog(CEC_LOG_WARNING, logStr.c_str()); |
| 340 | } |
| 341 | break; |
| 342 | case MSGCODE_FRAME_START: |
| 343 | { |
| 344 | logStr = "MSGCODE_FRAME_START"; |
| 345 | m_currentframe.clear(); |
| 346 | if (msg.size() >= 2) |
| 347 | { |
| 348 | logStr.AppendFormat(" initiator:%u destination:%u ack:%s %s", msg.initiator(), msg.destination(), msg.ack() ? "high" : "low", msg.eom() ? "eom" : ""); |
| 349 | m_currentframe.initiator = msg.initiator(); |
| 350 | m_currentframe.destination = msg.destination(); |
| 351 | m_currentframe.ack = msg.ack(); |
| 352 | m_currentframe.eom = msg.eom(); |
| 353 | } |
| 354 | m_controller->AddLog(CEC_LOG_DEBUG, logStr.c_str()); |
| 355 | } |
| 356 | break; |
| 357 | case MSGCODE_FRAME_DATA: |
| 358 | { |
| 359 | logStr = "MSGCODE_FRAME_DATA"; |
| 360 | if (msg.size() >= 2) |
| 361 | { |
| 362 | uint8_t iData = msg[1]; |
| 363 | logStr.AppendFormat(" %02x", iData); |
| 364 | m_currentframe.push_back(iData); |
| 365 | m_currentframe.eom = msg.eom(); |
| 366 | } |
| 367 | m_controller->AddLog(CEC_LOG_DEBUG, logStr.c_str()); |
| 368 | |
| 369 | bEom = msg.eom(); |
| 370 | } |
| 371 | break; |
| 372 | case MSGCODE_COMMAND_ACCEPTED: |
| 373 | m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_COMMAND_ACCEPTED"); |
| 374 | break; |
| 375 | case MSGCODE_TRANSMIT_SUCCEEDED: |
| 376 | m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_TRANSMIT_SUCCEEDED"); |
| 377 | break; |
| 378 | case MSGCODE_RECEIVE_FAILED: |
| 379 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_RECEIVE_FAILED"); |
| 380 | break; |
| 381 | case MSGCODE_COMMAND_REJECTED: |
| 382 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_COMMAND_REJECTED"); |
| 383 | break; |
| 384 | case MSGCODE_TRANSMIT_FAILED_LINE: |
| 385 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_LINE"); |
| 386 | break; |
| 387 | case MSGCODE_TRANSMIT_FAILED_ACK: |
| 388 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_ACK"); |
| 389 | break; |
| 390 | case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA: |
| 391 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA"); |
| 392 | break; |
| 393 | case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE: |
| 394 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE"); |
| 395 | break; |
| 396 | default: |
| 397 | break; |
| 398 | } |
| 399 | |
| 400 | return bEom; |
| 401 | } |
| 402 | |
| 403 | void CCECProcessor::ParseCommand(cec_command &command) |
| 404 | { |
| 405 | CStdString dataStr; |
| 406 | dataStr.Format(">> %1x%1x:%02x", command.initiator, command.destination, command.opcode); |
| 407 | for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++) |
| 408 | dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]); |
| 409 | m_controller->AddLog(CEC_LOG_TRAFFIC, dataStr.c_str()); |
| 410 | |
| 411 | if (!m_bMonitor) |
| 412 | m_busDevices[(uint8_t)command.initiator]->HandleCommand(command); |
| 413 | } |
| 414 | |
| 415 | uint16_t CCECProcessor::GetPhysicalAddress(void) const |
| 416 | { |
| 417 | return m_busDevices[m_iLogicalAddress]->GetPhysicalAddress(); |
| 418 | } |
| 419 | |
| 420 | void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode) |
| 421 | { |
| 422 | m_controller->SetCurrentButton(iButtonCode); |
| 423 | } |
| 424 | |
| 425 | void CCECProcessor::AddCommand(const cec_command &command) |
| 426 | { |
| 427 | m_controller->AddCommand(command); |
| 428 | } |
| 429 | |
| 430 | void CCECProcessor::AddKey(void) |
| 431 | { |
| 432 | m_controller->AddKey(); |
| 433 | } |
| 434 | |
| 435 | void CCECProcessor::AddLog(cec_log_level level, const CStdString &strMessage) |
| 436 | { |
| 437 | m_controller->AddLog(level, strMessage); |
| 438 | } |