| 1 | /* |
| 2 | * This file is part of the libCEC(R) library. |
| 3 | * |
| 4 | * libCEC(R) is Copyright (C) 2011-2012 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 "adapter/USBCECAdapterCommunication.h" |
| 36 | #include "devices/CECBusDevice.h" |
| 37 | #include "devices/CECAudioSystem.h" |
| 38 | #include "devices/CECPlaybackDevice.h" |
| 39 | #include "devices/CECRecordingDevice.h" |
| 40 | #include "devices/CECTuner.h" |
| 41 | #include "devices/CECTV.h" |
| 42 | #include "implementations/CECCommandHandler.h" |
| 43 | #include "LibCEC.h" |
| 44 | #include "CECClient.h" |
| 45 | #include "platform/util/timeutils.h" |
| 46 | |
| 47 | using namespace CEC; |
| 48 | using namespace std; |
| 49 | using namespace PLATFORM; |
| 50 | |
| 51 | #define CEC_PROCESSOR_SIGNAL_WAIT_TIME 1000 |
| 52 | |
| 53 | #define ToString(x) m_libcec->ToString(x) |
| 54 | |
| 55 | CCECProcessor::CCECProcessor(CLibCEC *libcec) : |
| 56 | m_bInitialised(false), |
| 57 | m_communication(NULL), |
| 58 | m_libcec(libcec), |
| 59 | m_bMonitor(false), |
| 60 | m_iPreviousAckMask(0), |
| 61 | m_iStandardLineTimeout(3), |
| 62 | m_iRetryLineTimeout(3), |
| 63 | m_iLastTransmission(0) |
| 64 | { |
| 65 | m_busDevices = new CCECDeviceMap(this); |
| 66 | } |
| 67 | |
| 68 | CCECProcessor::~CCECProcessor(void) |
| 69 | { |
| 70 | Close(); |
| 71 | delete m_busDevices; |
| 72 | } |
| 73 | |
| 74 | bool CCECProcessor::Start(const char *strPort, uint16_t iBaudRate /* = CEC_SERIAL_DEFAULT_BAUDRATE */, uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */) |
| 75 | { |
| 76 | CLockObject lock(m_mutex); |
| 77 | if (!OpenConnection(strPort, iBaudRate, iTimeoutMs)) |
| 78 | return false; |
| 79 | |
| 80 | /* create the processor thread */ |
| 81 | if (!IsRunning()) |
| 82 | { |
| 83 | if (CreateThread()) |
| 84 | m_libcec->AddLog(CEC_LOG_DEBUG, "processor thread started"); |
| 85 | else |
| 86 | { |
| 87 | m_libcec->AddLog(CEC_LOG_ERROR, "could not create a processor thread"); |
| 88 | return false; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | SetCECInitialised(true); |
| 93 | |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | void CCECProcessor::Close(void) |
| 98 | { |
| 99 | SetCECInitialised(false); |
| 100 | StopThread(); |
| 101 | |
| 102 | if (m_communication) |
| 103 | { |
| 104 | delete m_communication; |
| 105 | m_communication = NULL; |
| 106 | } |
| 107 | |
| 108 | m_bMonitor = false; |
| 109 | m_iPreviousAckMask = 0; |
| 110 | m_iStandardLineTimeout = 3; |
| 111 | m_iRetryLineTimeout = 3; |
| 112 | m_iLastTransmission = 0; |
| 113 | m_busDevices->ResetDeviceStatus(); |
| 114 | } |
| 115 | |
| 116 | bool CCECProcessor::OpenConnection(const char *strPort, uint16_t iBaudRate, uint32_t iTimeoutMs, bool bStartListening /* = true */) |
| 117 | { |
| 118 | bool bReturn(false); |
| 119 | Close(); |
| 120 | |
| 121 | { |
| 122 | CLockObject lock(m_mutex); |
| 123 | if (m_communication && m_communication->IsOpen()) |
| 124 | { |
| 125 | m_libcec->AddLog(CEC_LOG_ERROR, "connection already opened"); |
| 126 | return true; |
| 127 | } |
| 128 | else if (!m_communication) |
| 129 | m_communication = new CUSBCECAdapterCommunication(this, strPort, iBaudRate); |
| 130 | } |
| 131 | |
| 132 | CTimeout timeout(iTimeoutMs > 0 ? iTimeoutMs : CEC_DEFAULT_TRANSMIT_WAIT); |
| 133 | |
| 134 | /* open a new connection */ |
| 135 | unsigned iConnectTry(0); |
| 136 | while (timeout.TimeLeft() > 0 && (bReturn = m_communication->Open((timeout.TimeLeft() / CEC_CONNECT_TRIES), false, bStartListening)) == false) |
| 137 | { |
| 138 | m_libcec->AddLog(CEC_LOG_ERROR, "could not open a connection (try %d)", ++iConnectTry); |
| 139 | m_communication->Close(); |
| 140 | CEvent::Sleep(CEC_DEFAULT_CONNECT_RETRY_WAIT); |
| 141 | } |
| 142 | |
| 143 | m_libcec->AddLog(CEC_LOG_NOTICE, "connection opened"); |
| 144 | |
| 145 | return bReturn; |
| 146 | } |
| 147 | |
| 148 | bool CCECProcessor::CECInitialised(void) |
| 149 | { |
| 150 | CLockObject lock(m_threadMutex); |
| 151 | return m_bInitialised; |
| 152 | } |
| 153 | |
| 154 | void CCECProcessor::SetCECInitialised(bool bSetTo /* = true */) |
| 155 | { |
| 156 | CLockObject lock(m_mutex); |
| 157 | m_bInitialised = bSetTo; |
| 158 | if (!bSetTo) |
| 159 | UnregisterClients(); |
| 160 | } |
| 161 | |
| 162 | bool CCECProcessor::TryLogicalAddress(cec_logical_address address) |
| 163 | { |
| 164 | CCECBusDevice *device = m_busDevices->At(address); |
| 165 | if (device) |
| 166 | { |
| 167 | if (device->IsPresent() || device->IsHandledByLibCEC()) |
| 168 | return false; |
| 169 | |
| 170 | SetAckMask(0); |
| 171 | return device->TryLogicalAddress(); |
| 172 | } |
| 173 | |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | void CCECProcessor::ReplaceHandlers(void) |
| 178 | { |
| 179 | if (!CECInitialised()) |
| 180 | return; |
| 181 | |
| 182 | for (CECDEVICEMAP::iterator it = m_busDevices->Begin(); it != m_busDevices->End(); it++) |
| 183 | it->second->ReplaceHandler(true); |
| 184 | } |
| 185 | |
| 186 | bool CCECProcessor::OnCommandReceived(const cec_command &command) |
| 187 | { |
| 188 | return m_inBuffer.Push(command); |
| 189 | } |
| 190 | |
| 191 | void *CCECProcessor::Process(void) |
| 192 | { |
| 193 | m_libcec->AddLog(CEC_LOG_DEBUG, "processor thread started"); |
| 194 | |
| 195 | cec_command command; |
| 196 | command.Clear(); |
| 197 | |
| 198 | while (!IsStopped() && m_communication->IsOpen()) |
| 199 | { |
| 200 | if (m_inBuffer.Pop(command, CEC_PROCESSOR_SIGNAL_WAIT_TIME)) |
| 201 | ParseCommand(command); |
| 202 | |
| 203 | if (CECInitialised()) |
| 204 | { |
| 205 | ReplaceHandlers(); |
| 206 | |
| 207 | m_libcec->CheckKeypressTimeout(); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | return NULL; |
| 212 | } |
| 213 | |
| 214 | bool CCECProcessor::SetActiveSource(uint16_t iStreamPath) |
| 215 | { |
| 216 | bool bReturn(false); |
| 217 | |
| 218 | CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamPath); |
| 219 | if (device) |
| 220 | bReturn = device->ActivateSource(); |
| 221 | else |
| 222 | m_libcec->AddLog(CEC_LOG_DEBUG, "device with PA '%04x' not found", iStreamPath); |
| 223 | |
| 224 | return bReturn; |
| 225 | } |
| 226 | |
| 227 | void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout) |
| 228 | { |
| 229 | CLockObject lock(m_mutex); |
| 230 | m_iStandardLineTimeout = iTimeout; |
| 231 | } |
| 232 | |
| 233 | void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout) |
| 234 | { |
| 235 | CLockObject lock(m_mutex); |
| 236 | m_iRetryLineTimeout = iTimeout; |
| 237 | } |
| 238 | |
| 239 | bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress) |
| 240 | { |
| 241 | CCECBusDevice *device = GetDeviceByPhysicalAddress(iPhysicalAddress); |
| 242 | return device != NULL; |
| 243 | } |
| 244 | |
| 245 | void CCECProcessor::LogOutput(const cec_command &data) |
| 246 | { |
| 247 | CStdString strTx; |
| 248 | strTx.Format("<< %02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination); |
| 249 | if (data.opcode_set) |
| 250 | strTx.AppendFormat(":%02x", (uint8_t)data.opcode); |
| 251 | |
| 252 | for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++) |
| 253 | strTx.AppendFormat(":%02x", data.parameters[iPtr]); |
| 254 | m_libcec->AddLog(CEC_LOG_TRAFFIC, strTx.c_str()); |
| 255 | } |
| 256 | |
| 257 | bool CCECProcessor::SwitchMonitoring(bool bEnable) |
| 258 | { |
| 259 | m_libcec->AddLog(CEC_LOG_NOTICE, "== %s monitoring mode ==", bEnable ? "enabling" : "disabling"); |
| 260 | |
| 261 | { |
| 262 | CLockObject lock(m_mutex); |
| 263 | m_bMonitor = bEnable; |
| 264 | m_iPreviousAckMask = m_communication->GetAckMask(); |
| 265 | } |
| 266 | |
| 267 | if (bEnable) |
| 268 | return SetAckMask(0); |
| 269 | else |
| 270 | return SetAckMask(m_iPreviousAckMask); |
| 271 | } |
| 272 | |
| 273 | bool CCECProcessor::PollDevice(cec_logical_address iAddress) |
| 274 | { |
| 275 | CCECBusDevice *device = m_busDevices->At(iAddress); |
| 276 | CCECBusDevice *primary = GetPrimaryDevice(); |
| 277 | if (device) |
| 278 | { |
| 279 | return primary ? |
| 280 | primary->TransmitPoll(iAddress) : |
| 281 | device->TransmitPoll(iAddress); |
| 282 | } |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | CCECBusDevice *CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bSuppressUpdate /* = true */) |
| 287 | { |
| 288 | return m_busDevices ? |
| 289 | m_busDevices->GetDeviceByPhysicalAddress(iPhysicalAddress, bSuppressUpdate) : |
| 290 | NULL; |
| 291 | } |
| 292 | |
| 293 | CCECBusDevice *CCECProcessor::GetDevice(cec_logical_address address) const |
| 294 | { |
| 295 | return m_busDevices ? |
| 296 | m_busDevices->At(address) : |
| 297 | NULL; |
| 298 | } |
| 299 | |
| 300 | cec_logical_address CCECProcessor::GetActiveSource(bool bRequestActiveSource /* = true */) |
| 301 | { |
| 302 | // get the device that is marked as active source from the device map |
| 303 | CCECBusDevice *activeSource = m_busDevices->GetActiveSource(); |
| 304 | if (activeSource) |
| 305 | return activeSource->GetLogicalAddress(); |
| 306 | |
| 307 | if (bRequestActiveSource) |
| 308 | { |
| 309 | // request the active source from the bus |
| 310 | CCECBusDevice *primary = GetPrimaryDevice(); |
| 311 | if (primary) |
| 312 | { |
| 313 | primary->RequestActiveSource(); |
| 314 | return GetActiveSource(false); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | // unknown or none |
| 319 | return CECDEVICE_UNKNOWN; |
| 320 | } |
| 321 | |
| 322 | bool CCECProcessor::IsActiveSource(cec_logical_address iAddress) |
| 323 | { |
| 324 | CCECBusDevice *device = m_busDevices->At(iAddress); |
| 325 | return device && device->IsActiveSource(); |
| 326 | } |
| 327 | |
| 328 | bool CCECProcessor::Transmit(const cec_command &data) |
| 329 | { |
| 330 | CCECBusDevice *initiator = m_busDevices->At(data.initiator); |
| 331 | if (!initiator) |
| 332 | { |
| 333 | m_libcec->AddLog(CEC_LOG_WARNING, "invalid initiator"); |
| 334 | return false; |
| 335 | } |
| 336 | |
| 337 | if (data.destination != CECDEVICE_BROADCAST) |
| 338 | { |
| 339 | CCECBusDevice *destination = m_busDevices->At(data.destination); |
| 340 | if (destination && destination->IsHandledByLibCEC()) |
| 341 | { |
| 342 | m_libcec->AddLog(CEC_LOG_WARNING, "not sending data to myself!"); |
| 343 | return false; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | uint8_t iMaxTries(0); |
| 348 | { |
| 349 | CLockObject lock(m_mutex); |
| 350 | if (IsStopped()) |
| 351 | return false; |
| 352 | LogOutput(data); |
| 353 | m_iLastTransmission = GetTimeMs(); |
| 354 | if (!m_communication || !m_communication->IsOpen()) |
| 355 | { |
| 356 | m_libcec->AddLog(CEC_LOG_ERROR, "cannot transmit command: connection closed"); |
| 357 | return false; |
| 358 | } |
| 359 | iMaxTries = initiator->GetHandler()->GetTransmitRetries() + 1; |
| 360 | } |
| 361 | |
| 362 | bool bRetry(true); |
| 363 | uint8_t iTries(0); |
| 364 | uint8_t iLineTimeout = m_iStandardLineTimeout; |
| 365 | cec_adapter_message_state adapterState = ADAPTER_MESSAGE_STATE_UNKNOWN; |
| 366 | |
| 367 | while (bRetry && ++iTries < iMaxTries) |
| 368 | { |
| 369 | if (initiator->IsUnsupportedFeature(data.opcode)) |
| 370 | return false; |
| 371 | |
| 372 | adapterState = m_communication->Write(data, bRetry, iLineTimeout); |
| 373 | iLineTimeout = m_iRetryLineTimeout; |
| 374 | } |
| 375 | |
| 376 | return adapterState == ADAPTER_MESSAGE_STATE_SENT_ACKED; |
| 377 | } |
| 378 | |
| 379 | void CCECProcessor::TransmitAbort(cec_logical_address source, cec_logical_address destination, cec_opcode opcode, cec_abort_reason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */) |
| 380 | { |
| 381 | m_libcec->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message"); |
| 382 | |
| 383 | cec_command command; |
| 384 | cec_command::Format(command, source, destination, CEC_OPCODE_FEATURE_ABORT); |
| 385 | command.parameters.PushBack((uint8_t)opcode); |
| 386 | command.parameters.PushBack((uint8_t)reason); |
| 387 | |
| 388 | Transmit(command); |
| 389 | } |
| 390 | |
| 391 | void CCECProcessor::ParseCommand(const cec_command &command) |
| 392 | { |
| 393 | CStdString dataStr; |
| 394 | dataStr.Format(">> %1x%1x", command.initiator, command.destination); |
| 395 | if (command.opcode_set == 1) |
| 396 | dataStr.AppendFormat(":%02x", command.opcode); |
| 397 | for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++) |
| 398 | dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]); |
| 399 | m_libcec->AddLog(CEC_LOG_TRAFFIC, dataStr.c_str()); |
| 400 | |
| 401 | if (!m_bMonitor) |
| 402 | { |
| 403 | CCECBusDevice *device = m_busDevices->At(command.initiator); |
| 404 | if (device) |
| 405 | device->HandleCommand(command); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | bool CCECProcessor::IsPresentDevice(cec_logical_address address) |
| 410 | { |
| 411 | CCECBusDevice *device = m_busDevices->At(address); |
| 412 | return device && device->GetStatus() == CEC_DEVICE_STATUS_PRESENT; |
| 413 | } |
| 414 | |
| 415 | bool CCECProcessor::IsPresentDeviceType(cec_device_type type) |
| 416 | { |
| 417 | CECDEVICEVEC devices; |
| 418 | m_busDevices->GetByType(type, devices); |
| 419 | CCECDeviceMap::FilterActive(devices); |
| 420 | return !devices.empty(); |
| 421 | } |
| 422 | |
| 423 | uint16_t CCECProcessor::GetDetectedPhysicalAddress(void) const |
| 424 | { |
| 425 | return m_communication ? m_communication->GetPhysicalAddress() : CEC_INVALID_PHYSICAL_ADDRESS; |
| 426 | } |
| 427 | |
| 428 | bool CCECProcessor::SetAckMask(uint16_t iMask) |
| 429 | { |
| 430 | return m_communication ? m_communication->SetAckMask(iMask) : false; |
| 431 | } |
| 432 | |
| 433 | bool CCECProcessor::StandbyDevices(const cec_logical_address initiator, const CECDEVICEVEC &devices) |
| 434 | { |
| 435 | bool bReturn(true); |
| 436 | for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++) |
| 437 | bReturn &= (*it)->Standby(initiator); |
| 438 | return bReturn; |
| 439 | } |
| 440 | |
| 441 | bool CCECProcessor::StandbyDevice(const cec_logical_address initiator, cec_logical_address address) |
| 442 | { |
| 443 | CCECBusDevice *device = m_busDevices->At(address); |
| 444 | return device ? device->Standby(initiator) : false; |
| 445 | } |
| 446 | |
| 447 | bool CCECProcessor::PowerOnDevices(const cec_logical_address initiator, const CECDEVICEVEC &devices) |
| 448 | { |
| 449 | bool bReturn(true); |
| 450 | for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++) |
| 451 | bReturn &= (*it)->PowerOn(initiator); |
| 452 | return bReturn; |
| 453 | } |
| 454 | |
| 455 | bool CCECProcessor::PowerOnDevice(const cec_logical_address initiator, cec_logical_address address) |
| 456 | { |
| 457 | CCECBusDevice *device = m_busDevices->At(address); |
| 458 | return device ? device->PowerOn(initiator) : false; |
| 459 | } |
| 460 | |
| 461 | bool CCECProcessor::StartBootloader(const char *strPort /* = NULL */) |
| 462 | { |
| 463 | bool bReturn(false); |
| 464 | if (!m_communication && strPort) |
| 465 | { |
| 466 | IAdapterCommunication *comm = new CUSBCECAdapterCommunication(this, strPort); |
| 467 | CTimeout timeout(CEC_DEFAULT_CONNECT_TIMEOUT); |
| 468 | int iConnectTry(0); |
| 469 | while (timeout.TimeLeft() > 0 && (bReturn = comm->Open(timeout.TimeLeft() / CEC_CONNECT_TRIES, true)) == false) |
| 470 | { |
| 471 | m_libcec->AddLog(CEC_LOG_ERROR, "could not open a connection (try %d)", ++iConnectTry); |
| 472 | comm->Close(); |
| 473 | Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT); |
| 474 | } |
| 475 | if (comm->IsOpen()) |
| 476 | { |
| 477 | bReturn = comm->StartBootloader(); |
| 478 | delete comm; |
| 479 | } |
| 480 | return bReturn; |
| 481 | } |
| 482 | else |
| 483 | { |
| 484 | m_communication->StartBootloader(); |
| 485 | Close(); |
| 486 | bReturn = true; |
| 487 | } |
| 488 | |
| 489 | return bReturn; |
| 490 | } |
| 491 | |
| 492 | bool CCECProcessor::PingAdapter(void) |
| 493 | { |
| 494 | return m_communication->PingAdapter(); |
| 495 | } |
| 496 | |
| 497 | void CCECProcessor::HandlePoll(cec_logical_address initiator, cec_logical_address destination) |
| 498 | { |
| 499 | CCECBusDevice *device = m_busDevices->At(destination); |
| 500 | if (device) |
| 501 | device->HandlePollFrom(initiator); |
| 502 | } |
| 503 | |
| 504 | bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator) |
| 505 | { |
| 506 | CCECBusDevice *device = m_busDevices->At(initiator); |
| 507 | return !device || !device->HandleReceiveFailed(); |
| 508 | } |
| 509 | |
| 510 | bool CCECProcessor::SetStreamPath(uint16_t iPhysicalAddress) |
| 511 | { |
| 512 | // stream path changes are sent by the TV |
| 513 | return GetTV()->GetHandler()->TransmitSetStreamPath(iPhysicalAddress); |
| 514 | } |
| 515 | |
| 516 | bool CCECProcessor::CanPersistConfiguration(void) |
| 517 | { |
| 518 | return m_communication ? m_communication->GetFirmwareVersion() >= 2 : false; |
| 519 | } |
| 520 | |
| 521 | bool CCECProcessor::PersistConfiguration(libcec_configuration *configuration) |
| 522 | { |
| 523 | return m_communication ? m_communication->PersistConfiguration(configuration) : false; |
| 524 | } |
| 525 | |
| 526 | void CCECProcessor::RescanActiveDevices(void) |
| 527 | { |
| 528 | for (CECDEVICEMAP::iterator it = m_busDevices->Begin(); it != m_busDevices->End(); it++) |
| 529 | it->second->GetStatus(true); |
| 530 | } |
| 531 | |
| 532 | bool CCECProcessor::GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */) |
| 533 | { |
| 534 | if (!OpenConnection(strPort, CEC_SERIAL_DEFAULT_BAUDRATE, iTimeoutMs, false)) |
| 535 | return false; |
| 536 | |
| 537 | config->iFirmwareVersion = m_communication->GetFirmwareVersion(); |
| 538 | config->iPhysicalAddress = m_communication->GetPhysicalAddress(); |
| 539 | config->iFirmwareBuildDate = m_communication->GetFirmwareBuildDate(); |
| 540 | |
| 541 | return true; |
| 542 | } |
| 543 | |
| 544 | bool CCECProcessor::TransmitPendingActiveSourceCommands(void) |
| 545 | { |
| 546 | bool bReturn(true); |
| 547 | for (CECDEVICEMAP::iterator it = m_busDevices->Begin(); it != m_busDevices->End(); it++) |
| 548 | bReturn &= it->second->TransmitPendingActiveSourceCommands(); |
| 549 | return bReturn; |
| 550 | } |
| 551 | |
| 552 | CCECTV *CCECProcessor::GetTV(void) const |
| 553 | { |
| 554 | return CCECBusDevice::AsTV(m_busDevices->At(CECDEVICE_TV)); |
| 555 | } |
| 556 | |
| 557 | CCECAudioSystem *CCECProcessor::GetAudioSystem(void) const |
| 558 | { |
| 559 | return CCECBusDevice::AsAudioSystem(m_busDevices->At(CECDEVICE_AUDIOSYSTEM)); |
| 560 | } |
| 561 | |
| 562 | CCECPlaybackDevice *CCECProcessor::GetPlaybackDevice(cec_logical_address address) const |
| 563 | { |
| 564 | return CCECBusDevice::AsPlaybackDevice(m_busDevices->At(address)); |
| 565 | } |
| 566 | |
| 567 | CCECRecordingDevice *CCECProcessor::GetRecordingDevice(cec_logical_address address) const |
| 568 | { |
| 569 | return CCECBusDevice::AsRecordingDevice(m_busDevices->At(address)); |
| 570 | } |
| 571 | |
| 572 | CCECTuner *CCECProcessor::GetTuner(cec_logical_address address) const |
| 573 | { |
| 574 | return CCECBusDevice::AsTuner(m_busDevices->At(address)); |
| 575 | } |
| 576 | |
| 577 | bool CCECProcessor::RegisterClient(CCECClient *client) |
| 578 | { |
| 579 | if (!client) |
| 580 | return false; |
| 581 | |
| 582 | libcec_configuration &configuration = *client->GetConfiguration(); |
| 583 | m_libcec->AddLog(CEC_LOG_NOTICE, "registering new CEC client - v%s", ToString((cec_client_version)configuration.clientVersion)); |
| 584 | |
| 585 | client->SetRegistered(false); |
| 586 | client->SetInitialised(false); |
| 587 | |
| 588 | uint16_t iPreviousMask(m_communication->GetAckMask()); |
| 589 | |
| 590 | // find logical addresses for this client |
| 591 | if (!client->FindLogicalAddresses()) |
| 592 | { |
| 593 | SetAckMask(iPreviousMask); |
| 594 | return false; |
| 595 | } |
| 596 | |
| 597 | // register this client on the new addresses |
| 598 | CECDEVICEVEC devices; |
| 599 | m_busDevices->GetByLogicalAddresses(devices, configuration.logicalAddresses); |
| 600 | for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++) |
| 601 | { |
| 602 | CLockObject lock(m_mutex); |
| 603 | m_clients.erase((*it)->GetLogicalAddress()); |
| 604 | m_clients.insert(make_pair<cec_logical_address, CCECClient *>((*it)->GetLogicalAddress(), client)); |
| 605 | client->SetRegistered(true); |
| 606 | } |
| 607 | |
| 608 | // get the settings from the rom |
| 609 | if (configuration.bGetSettingsFromROM == 1) |
| 610 | { |
| 611 | libcec_configuration config; |
| 612 | m_communication->GetConfiguration(&config); |
| 613 | |
| 614 | CLockObject lock(m_mutex); |
| 615 | if (!config.deviceTypes.IsEmpty()) |
| 616 | configuration.deviceTypes = config.deviceTypes; |
| 617 | if (CLibCEC::IsValidPhysicalAddress(config.iPhysicalAddress)) |
| 618 | configuration.iPhysicalAddress = config.iPhysicalAddress; |
| 619 | snprintf(configuration.strDeviceName, 13, "%s", config.strDeviceName); |
| 620 | } |
| 621 | |
| 622 | // set the new ack mask |
| 623 | bool bReturn = SetAckMask(GetLogicalAddresses().AckMask()) && |
| 624 | client->Initialise(); |
| 625 | |
| 626 | // set the firmware version and build date |
| 627 | configuration.serverVersion = LIBCEC_VERSION_CURRENT; |
| 628 | configuration.iFirmwareVersion = m_communication->GetFirmwareVersion(); |
| 629 | configuration.iFirmwareBuildDate = m_communication->GetFirmwareBuildDate(); |
| 630 | |
| 631 | CStdString strLog; |
| 632 | if (bReturn) |
| 633 | strLog = "CEC client registered."; |
| 634 | else |
| 635 | strLog = "failed to register the CEC client."; |
| 636 | strLog.AppendFormat(" libCEC version = %s, client version = %s, firmware version = %d", ToString((cec_server_version)configuration.serverVersion), ToString((cec_client_version)configuration.clientVersion), configuration.iFirmwareVersion); |
| 637 | if (configuration.iFirmwareBuildDate != CEC_FW_BUILD_UNKNOWN) |
| 638 | { |
| 639 | time_t buildTime = (time_t)configuration.iFirmwareBuildDate; |
| 640 | strLog.AppendFormat(", firmware build date: %s", asctime(gmtime(&buildTime))); |
| 641 | strLog = strLog.Left((int)strLog.length() - 1); // strip \n added by asctime |
| 642 | strLog.append(" +0000"); |
| 643 | } |
| 644 | |
| 645 | m_libcec->AddLog(bReturn ? CEC_LOG_NOTICE : CEC_LOG_ERROR, strLog); |
| 646 | |
| 647 | if (bReturn) |
| 648 | { |
| 649 | strLog = "Using logical address(es): "; |
| 650 | CECDEVICEVEC devices; |
| 651 | m_busDevices->GetByLogicalAddresses(devices, configuration.logicalAddresses); |
| 652 | for (CECDEVICEVEC::iterator it = devices.begin(); it != devices.end(); it++) |
| 653 | strLog.AppendFormat("%s (%X) ", (*it)->GetLogicalAddressName(), (*it)->GetLogicalAddress()); |
| 654 | m_libcec->AddLog(CEC_LOG_NOTICE, strLog); |
| 655 | } |
| 656 | |
| 657 | // display a warning if the firmware can be upgraded |
| 658 | if (!m_communication->IsRunningLatestFirmware()) |
| 659 | { |
| 660 | const char *strUpgradeMessage = "The firmware of this adapter can be upgraded. Please visit http://blog.pulse-eight.com/ for more information."; |
| 661 | m_libcec->AddLog(CEC_LOG_WARNING, strUpgradeMessage); |
| 662 | client->Alert(CEC_ALERT_SERVICE_DEVICE, libcec_parameter(strUpgradeMessage)); |
| 663 | } |
| 664 | else |
| 665 | { |
| 666 | m_libcec->AddLog(CEC_LOG_DEBUG, "the adapter is using the latest (known) firmware version"); |
| 667 | } |
| 668 | |
| 669 | if (bReturn) |
| 670 | { |
| 671 | /* get the vendor id from the TV, so we are using the correct handler */ |
| 672 | GetTV()->GetVendorId(configuration.logicalAddresses.primary); |
| 673 | } |
| 674 | |
| 675 | return bReturn; |
| 676 | } |
| 677 | |
| 678 | void CCECProcessor::UnregisterClient(CCECClient *client) |
| 679 | { |
| 680 | CLockObject lock(m_mutex); |
| 681 | CECDEVICEVEC devices; |
| 682 | m_busDevices->GetByLogicalAddresses(devices, client->GetConfiguration()->logicalAddresses); |
| 683 | for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++) |
| 684 | { |
| 685 | map<cec_logical_address, CCECClient *>::iterator entry = m_clients.find((*it)->GetLogicalAddress()); |
| 686 | if (entry != m_clients.end()) |
| 687 | m_clients.erase(entry); |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | void CCECProcessor::UnregisterClients(void) |
| 692 | { |
| 693 | CLockObject lock(m_mutex); |
| 694 | for (map<cec_logical_address, CCECClient *>::iterator client = m_clients.begin(); client != m_clients.end(); client++) |
| 695 | client->second->OnUnregister(); |
| 696 | m_clients.clear(); |
| 697 | } |
| 698 | |
| 699 | CCECClient *CCECProcessor::GetClient(const cec_logical_address address) |
| 700 | { |
| 701 | CLockObject lock(m_mutex); |
| 702 | map<cec_logical_address, CCECClient *>::const_iterator client = m_clients.find(address); |
| 703 | if (client != m_clients.end()) |
| 704 | return client->second; |
| 705 | return NULL; |
| 706 | } |
| 707 | |
| 708 | CCECClient *CCECProcessor::GetPrimaryClient(void) |
| 709 | { |
| 710 | CLockObject lock(m_mutex); |
| 711 | map<cec_logical_address, CCECClient *>::const_iterator client = m_clients.begin(); |
| 712 | if (client != m_clients.end()) |
| 713 | return client->second; |
| 714 | return NULL; |
| 715 | } |
| 716 | |
| 717 | CCECBusDevice *CCECProcessor::GetPrimaryDevice(void) const |
| 718 | { |
| 719 | return m_busDevices->At(GetLogicalAddress()); |
| 720 | } |
| 721 | |
| 722 | cec_logical_address CCECProcessor::GetLogicalAddress(void) const |
| 723 | { |
| 724 | cec_logical_addresses addresses = GetLogicalAddresses(); |
| 725 | return addresses.primary; |
| 726 | } |
| 727 | |
| 728 | cec_logical_addresses CCECProcessor::GetLogicalAddresses(void) const |
| 729 | { |
| 730 | cec_logical_addresses addresses; |
| 731 | for (map<cec_logical_address, CCECClient *>::const_iterator client = m_clients.begin(); client != m_clients.end(); client++) |
| 732 | addresses.Set(client->first); |
| 733 | |
| 734 | return addresses; |
| 735 | } |
| 736 | |
| 737 | bool CCECProcessor::IsHandledByLibCEC(const cec_logical_address address) const |
| 738 | { |
| 739 | CCECBusDevice *device = GetDevice(address); |
| 740 | return device && device->IsHandledByLibCEC(); |
| 741 | } |