| 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 "CECBusDevice.h" |
| 34 | #include "../CECProcessor.h" |
| 35 | #include "../implementations/ANCommandHandler.h" |
| 36 | #include "../implementations/CECCommandHandler.h" |
| 37 | #include "../implementations/SLCommandHandler.h" |
| 38 | #include "../implementations/VLCommandHandler.h" |
| 39 | #include "../LibCEC.h" |
| 40 | #include "../CECTypeUtils.h" |
| 41 | #include "../platform/util/timeutils.h" |
| 42 | #include "../platform/util/util.h" |
| 43 | |
| 44 | #include "CECAudioSystem.h" |
| 45 | #include "CECPlaybackDevice.h" |
| 46 | #include "CECRecordingDevice.h" |
| 47 | #include "CECTuner.h" |
| 48 | #include "CECTV.h" |
| 49 | |
| 50 | using namespace std; |
| 51 | using namespace CEC; |
| 52 | using namespace PLATFORM; |
| 53 | |
| 54 | #define LIB_CEC m_processor->GetLib() |
| 55 | #define ToString(p) CCECTypeUtils::ToString(p) |
| 56 | |
| 57 | CCECBusDevice::CCECBusDevice(CCECProcessor *processor, cec_logical_address iLogicalAddress, uint16_t iPhysicalAddress /* = CEC_INVALID_PHYSICAL_ADDRESS */) : |
| 58 | m_type (CEC_DEVICE_TYPE_RESERVED), |
| 59 | m_iPhysicalAddress (iPhysicalAddress), |
| 60 | m_iStreamPath (CEC_INVALID_PHYSICAL_ADDRESS), |
| 61 | m_iLogicalAddress (iLogicalAddress), |
| 62 | m_powerStatus (CEC_POWER_STATUS_UNKNOWN), |
| 63 | m_processor (processor), |
| 64 | m_vendor (CEC_VENDOR_UNKNOWN), |
| 65 | m_bReplaceHandler (false), |
| 66 | m_menuState (CEC_MENU_STATE_ACTIVATED), |
| 67 | m_bActiveSource (false), |
| 68 | m_iLastActive (0), |
| 69 | m_iLastPowerStateUpdate (0), |
| 70 | m_cecVersion (CEC_VERSION_UNKNOWN), |
| 71 | m_deviceStatus (CEC_DEVICE_STATUS_UNKNOWN), |
| 72 | m_iHandlerUseCount (0), |
| 73 | m_bAwaitingReceiveFailed(false), |
| 74 | m_bVendorIdRequested (false) |
| 75 | { |
| 76 | m_handler = new CCECCommandHandler(this); |
| 77 | |
| 78 | for (unsigned int iPtr = 0; iPtr < 4; iPtr++) |
| 79 | m_menuLanguage.language[iPtr] = '?'; |
| 80 | m_menuLanguage.language[3] = 0; |
| 81 | m_menuLanguage.device = iLogicalAddress; |
| 82 | |
| 83 | m_strDeviceName = ToString(m_iLogicalAddress); |
| 84 | } |
| 85 | |
| 86 | CCECBusDevice::~CCECBusDevice(void) |
| 87 | { |
| 88 | DELETE_AND_NULL(m_handler); |
| 89 | } |
| 90 | |
| 91 | bool CCECBusDevice::ReplaceHandler(bool bActivateSource /* = true */) |
| 92 | { |
| 93 | bool bInitHandler(false); |
| 94 | { |
| 95 | CTryLockObject lock(m_mutex); |
| 96 | if (!lock.IsLocked()) |
| 97 | return false; |
| 98 | |
| 99 | CLockObject handlerLock(m_handlerMutex); |
| 100 | if (m_iHandlerUseCount > 0) |
| 101 | return false; |
| 102 | |
| 103 | MarkBusy(); |
| 104 | |
| 105 | if (m_vendor != m_handler->GetVendorId()) |
| 106 | { |
| 107 | if (CCECCommandHandler::HasSpecificHandler(m_vendor)) |
| 108 | { |
| 109 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "replacing the command handler for device '%s' (%x)", GetLogicalAddressName(), GetLogicalAddress()); |
| 110 | DELETE_AND_NULL(m_handler); |
| 111 | |
| 112 | switch (m_vendor) |
| 113 | { |
| 114 | case CEC_VENDOR_SAMSUNG: |
| 115 | m_handler = new CANCommandHandler(this); |
| 116 | break; |
| 117 | case CEC_VENDOR_LG: |
| 118 | m_handler = new CSLCommandHandler(this); |
| 119 | break; |
| 120 | case CEC_VENDOR_PANASONIC: |
| 121 | m_handler = new CVLCommandHandler(this); |
| 122 | break; |
| 123 | default: |
| 124 | m_handler = new CCECCommandHandler(this); |
| 125 | break; |
| 126 | } |
| 127 | |
| 128 | m_handler->SetVendorId(m_vendor); |
| 129 | bInitHandler = true; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | if (bInitHandler) |
| 135 | { |
| 136 | CCECBusDevice *primary = GetProcessor()->GetPrimaryDevice(); |
| 137 | if (primary->GetLogicalAddress() != CECDEVICE_UNREGISTERED) |
| 138 | { |
| 139 | m_handler->InitHandler(); |
| 140 | |
| 141 | if (bActivateSource && IsHandledByLibCEC() && IsActiveSource()) |
| 142 | m_handler->ActivateSource(); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | MarkReady(); |
| 147 | |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | bool CCECBusDevice::HandleCommand(const cec_command &command) |
| 152 | { |
| 153 | bool bHandled(false); |
| 154 | |
| 155 | /* update "last active" */ |
| 156 | { |
| 157 | CLockObject lock(m_mutex); |
| 158 | m_iLastActive = GetTimeMs(); |
| 159 | |
| 160 | /* don't call GetStatus() here, just read the value with the mutex locked */ |
| 161 | if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC && command.opcode_set == 1) |
| 162 | m_deviceStatus = CEC_DEVICE_STATUS_PRESENT; |
| 163 | |
| 164 | MarkBusy(); |
| 165 | } |
| 166 | |
| 167 | /* handle the command */ |
| 168 | bHandled = m_handler->HandleCommand(command); |
| 169 | |
| 170 | /* change status to present */ |
| 171 | if (bHandled && GetLogicalAddress() != CECDEVICE_BROADCAST) |
| 172 | { |
| 173 | CLockObject lock(m_mutex); |
| 174 | if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC) |
| 175 | { |
| 176 | if (m_deviceStatus != CEC_DEVICE_STATUS_PRESENT) |
| 177 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "device %s (%x) status changed to present after command %s", GetLogicalAddressName(), (uint8_t)GetLogicalAddress(), ToString(command.opcode)); |
| 178 | m_deviceStatus = CEC_DEVICE_STATUS_PRESENT; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | MarkReady(); |
| 183 | return bHandled; |
| 184 | } |
| 185 | |
| 186 | const char* CCECBusDevice::GetLogicalAddressName(void) const |
| 187 | { |
| 188 | return ToString(m_iLogicalAddress); |
| 189 | } |
| 190 | |
| 191 | bool CCECBusDevice::IsPresent(void) |
| 192 | { |
| 193 | CLockObject lock(m_mutex); |
| 194 | return m_deviceStatus == CEC_DEVICE_STATUS_PRESENT; |
| 195 | } |
| 196 | |
| 197 | bool CCECBusDevice::IsHandledByLibCEC(void) |
| 198 | { |
| 199 | CLockObject lock(m_mutex); |
| 200 | return m_deviceStatus == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC; |
| 201 | } |
| 202 | |
| 203 | void CCECBusDevice::SetUnsupportedFeature(cec_opcode opcode) |
| 204 | { |
| 205 | // some commands should never be marked as unsupported |
| 206 | if (opcode == CEC_OPCODE_VENDOR_COMMAND || |
| 207 | opcode == CEC_OPCODE_VENDOR_COMMAND_WITH_ID || |
| 208 | opcode == CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN || |
| 209 | opcode == CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP || |
| 210 | opcode == CEC_OPCODE_ABORT || |
| 211 | opcode == CEC_OPCODE_FEATURE_ABORT || |
| 212 | opcode == CEC_OPCODE_NONE) |
| 213 | return; |
| 214 | |
| 215 | { |
| 216 | CLockObject lock(m_mutex); |
| 217 | if (m_unsupportedFeatures.find(opcode) == m_unsupportedFeatures.end()) |
| 218 | { |
| 219 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "marking opcode '%s' as unsupported feature for device '%s'", ToString(opcode), GetLogicalAddressName()); |
| 220 | m_unsupportedFeatures.insert(opcode); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | // signal threads that are waiting for a reponse |
| 225 | MarkBusy(); |
| 226 | m_handler->SignalOpcode(cec_command::GetResponseOpcode(opcode)); |
| 227 | MarkReady(); |
| 228 | } |
| 229 | |
| 230 | bool CCECBusDevice::IsUnsupportedFeature(cec_opcode opcode) |
| 231 | { |
| 232 | CLockObject lock(m_mutex); |
| 233 | bool bUnsupported = (m_unsupportedFeatures.find(opcode) != m_unsupportedFeatures.end()); |
| 234 | if (bUnsupported) |
| 235 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "'%s' is marked as unsupported feature for device '%s'", ToString(opcode), GetLogicalAddressName()); |
| 236 | return bUnsupported; |
| 237 | } |
| 238 | |
| 239 | bool CCECBusDevice::TransmitKeypress(const cec_logical_address initiator, cec_user_control_code key, bool bWait /* = true */) |
| 240 | { |
| 241 | MarkBusy(); |
| 242 | bool bReturn = m_handler->TransmitKeypress(initiator, m_iLogicalAddress, key, bWait); |
| 243 | MarkReady(); |
| 244 | return bReturn; |
| 245 | } |
| 246 | |
| 247 | bool CCECBusDevice::TransmitKeyRelease(const cec_logical_address initiator, bool bWait /* = true */) |
| 248 | { |
| 249 | MarkBusy(); |
| 250 | bool bReturn = m_handler->TransmitKeyRelease(initiator, m_iLogicalAddress, bWait); |
| 251 | MarkReady(); |
| 252 | return bReturn; |
| 253 | } |
| 254 | |
| 255 | cec_version CCECBusDevice::GetCecVersion(const cec_logical_address initiator, bool bUpdate /* = false */) |
| 256 | { |
| 257 | bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT); |
| 258 | bool bRequestUpdate(false); |
| 259 | { |
| 260 | CLockObject lock(m_mutex); |
| 261 | bRequestUpdate = bIsPresent && |
| 262 | (bUpdate || m_cecVersion == CEC_VERSION_UNKNOWN); |
| 263 | } |
| 264 | |
| 265 | if (bRequestUpdate) |
| 266 | { |
| 267 | CheckVendorIdRequested(initiator); |
| 268 | RequestCecVersion(initiator); |
| 269 | } |
| 270 | |
| 271 | CLockObject lock(m_mutex); |
| 272 | return m_cecVersion; |
| 273 | } |
| 274 | |
| 275 | void CCECBusDevice::SetCecVersion(const cec_version newVersion) |
| 276 | { |
| 277 | CLockObject lock(m_mutex); |
| 278 | if (m_cecVersion != newVersion) |
| 279 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): CEC version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(newVersion)); |
| 280 | m_cecVersion = newVersion; |
| 281 | } |
| 282 | |
| 283 | bool CCECBusDevice::RequestCecVersion(const cec_logical_address initiator, bool bWaitForResponse /* = true */) |
| 284 | { |
| 285 | bool bReturn(false); |
| 286 | |
| 287 | if (!IsHandledByLibCEC() && |
| 288 | !IsUnsupportedFeature(CEC_OPCODE_GET_CEC_VERSION)) |
| 289 | { |
| 290 | MarkBusy(); |
| 291 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); |
| 292 | bReturn = m_handler->TransmitRequestCecVersion(initiator, m_iLogicalAddress, bWaitForResponse); |
| 293 | MarkReady(); |
| 294 | } |
| 295 | return bReturn; |
| 296 | } |
| 297 | |
| 298 | bool CCECBusDevice::TransmitCECVersion(const cec_logical_address destination) |
| 299 | { |
| 300 | cec_version version; |
| 301 | { |
| 302 | CLockObject lock(m_mutex); |
| 303 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): cec version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination, ToString(m_cecVersion)); |
| 304 | version = m_cecVersion; |
| 305 | } |
| 306 | |
| 307 | MarkBusy(); |
| 308 | bool bReturn = m_handler->TransmitCECVersion(m_iLogicalAddress, destination, version); |
| 309 | MarkReady(); |
| 310 | return bReturn; |
| 311 | } |
| 312 | |
| 313 | cec_menu_language &CCECBusDevice::GetMenuLanguage(const cec_logical_address initiator, bool bUpdate /* = false */) |
| 314 | { |
| 315 | bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT); |
| 316 | bool bRequestUpdate(false); |
| 317 | { |
| 318 | CLockObject lock(m_mutex); |
| 319 | bRequestUpdate = (bIsPresent && |
| 320 | (bUpdate || !strcmp(m_menuLanguage.language, "???"))); |
| 321 | } |
| 322 | |
| 323 | if (bRequestUpdate) |
| 324 | { |
| 325 | CheckVendorIdRequested(initiator); |
| 326 | RequestMenuLanguage(initiator); |
| 327 | } |
| 328 | |
| 329 | CLockObject lock(m_mutex); |
| 330 | return m_menuLanguage; |
| 331 | } |
| 332 | |
| 333 | void CCECBusDevice::SetMenuLanguage(const char *strLanguage) |
| 334 | { |
| 335 | if (!strLanguage) |
| 336 | return; |
| 337 | |
| 338 | CLockObject lock(m_mutex); |
| 339 | if (strcmp(strLanguage, m_menuLanguage.language)) |
| 340 | { |
| 341 | memcpy(m_menuLanguage.language, strLanguage, 3); |
| 342 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): menu language set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, m_menuLanguage.language); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | void CCECBusDevice::SetMenuLanguage(const cec_menu_language &language) |
| 347 | { |
| 348 | if (language.device == m_iLogicalAddress) |
| 349 | SetMenuLanguage(language.language); |
| 350 | } |
| 351 | |
| 352 | bool CCECBusDevice::RequestMenuLanguage(const cec_logical_address initiator, bool bWaitForResponse /* = true */) |
| 353 | { |
| 354 | bool bReturn(false); |
| 355 | |
| 356 | if (!IsHandledByLibCEC() && |
| 357 | !IsUnsupportedFeature(CEC_OPCODE_GET_MENU_LANGUAGE)) |
| 358 | { |
| 359 | MarkBusy(); |
| 360 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); |
| 361 | bReturn = m_handler->TransmitRequestMenuLanguage(initiator, m_iLogicalAddress, bWaitForResponse); |
| 362 | MarkReady(); |
| 363 | } |
| 364 | return bReturn; |
| 365 | } |
| 366 | |
| 367 | bool CCECBusDevice::TransmitSetMenuLanguage(const cec_logical_address destination) |
| 368 | { |
| 369 | bool bReturn(false); |
| 370 | cec_menu_language language; |
| 371 | { |
| 372 | CLockObject lock(m_mutex); |
| 373 | language = m_menuLanguage; |
| 374 | } |
| 375 | |
| 376 | char lang[3]; |
| 377 | { |
| 378 | CLockObject lock(m_mutex); |
| 379 | lang[0] = language.language[0]; |
| 380 | lang[1] = language.language[1]; |
| 381 | lang[2] = language.language[2]; |
| 382 | } |
| 383 | |
| 384 | MarkBusy(); |
| 385 | if (lang[0] == '?' && lang[1] == '?' && lang[2] == '?') |
| 386 | { |
| 387 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): Menu language feature abort", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination); |
| 388 | m_processor->TransmitAbort(m_iLogicalAddress, destination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID); |
| 389 | bReturn = true; |
| 390 | } |
| 391 | else |
| 392 | { |
| 393 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): Menu language '%s'", GetLogicalAddressName(), m_iLogicalAddress, lang); |
| 394 | bReturn = m_handler->TransmitSetMenuLanguage(m_iLogicalAddress, lang); |
| 395 | } |
| 396 | MarkReady(); |
| 397 | return bReturn; |
| 398 | } |
| 399 | |
| 400 | bool CCECBusDevice::TransmitOSDString(const cec_logical_address destination, cec_display_control duration, const char *strMessage) |
| 401 | { |
| 402 | bool bReturn(false); |
| 403 | if (!m_processor->GetDevice(destination)->IsUnsupportedFeature(CEC_OPCODE_SET_OSD_STRING)) |
| 404 | { |
| 405 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): display OSD message '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination, strMessage); |
| 406 | MarkBusy(); |
| 407 | bReturn = m_handler->TransmitOSDString(m_iLogicalAddress, destination, duration, strMessage); |
| 408 | MarkReady(); |
| 409 | } |
| 410 | return bReturn; |
| 411 | } |
| 412 | |
| 413 | CStdString CCECBusDevice::GetCurrentOSDName(void) |
| 414 | { |
| 415 | CLockObject lock(m_mutex); |
| 416 | return m_strDeviceName; |
| 417 | } |
| 418 | |
| 419 | CStdString CCECBusDevice::GetOSDName(const cec_logical_address initiator, bool bUpdate /* = false */) |
| 420 | { |
| 421 | bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT); |
| 422 | bool bRequestUpdate(false); |
| 423 | { |
| 424 | CLockObject lock(m_mutex); |
| 425 | bRequestUpdate = bIsPresent && |
| 426 | (bUpdate || m_strDeviceName.Equals(ToString(m_iLogicalAddress))) && |
| 427 | m_type != CEC_DEVICE_TYPE_TV; |
| 428 | } |
| 429 | |
| 430 | if (bRequestUpdate) |
| 431 | { |
| 432 | CheckVendorIdRequested(initiator); |
| 433 | RequestOSDName(initiator); |
| 434 | } |
| 435 | |
| 436 | CLockObject lock(m_mutex); |
| 437 | return m_strDeviceName; |
| 438 | } |
| 439 | |
| 440 | void CCECBusDevice::SetOSDName(CStdString strName) |
| 441 | { |
| 442 | CLockObject lock(m_mutex); |
| 443 | if (m_strDeviceName != strName) |
| 444 | { |
| 445 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): osd name set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, strName.c_str()); |
| 446 | m_strDeviceName = strName; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | bool CCECBusDevice::RequestOSDName(const cec_logical_address initiator, bool bWaitForResponse /* = true */) |
| 451 | { |
| 452 | bool bReturn(false); |
| 453 | |
| 454 | if (!IsHandledByLibCEC() && |
| 455 | !IsUnsupportedFeature(CEC_OPCODE_GIVE_OSD_NAME)) |
| 456 | { |
| 457 | MarkBusy(); |
| 458 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< requesting OSD name of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); |
| 459 | bReturn = m_handler->TransmitRequestOSDName(initiator, m_iLogicalAddress, bWaitForResponse); |
| 460 | MarkReady(); |
| 461 | } |
| 462 | return bReturn; |
| 463 | } |
| 464 | |
| 465 | bool CCECBusDevice::TransmitOSDName(const cec_logical_address destination) |
| 466 | { |
| 467 | CStdString strDeviceName; |
| 468 | { |
| 469 | CLockObject lock(m_mutex); |
| 470 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination, m_strDeviceName.c_str()); |
| 471 | strDeviceName = m_strDeviceName; |
| 472 | } |
| 473 | |
| 474 | MarkBusy(); |
| 475 | bool bReturn = m_handler->TransmitOSDName(m_iLogicalAddress, destination, strDeviceName); |
| 476 | MarkReady(); |
| 477 | return bReturn; |
| 478 | } |
| 479 | |
| 480 | bool CCECBusDevice::HasValidPhysicalAddress(void) |
| 481 | { |
| 482 | CLockObject lock(m_mutex); |
| 483 | return CLibCEC::IsValidPhysicalAddress(m_iPhysicalAddress); |
| 484 | } |
| 485 | |
| 486 | uint16_t CCECBusDevice::GetCurrentPhysicalAddress(void) |
| 487 | { |
| 488 | CLockObject lock(m_mutex); |
| 489 | return m_iPhysicalAddress; |
| 490 | } |
| 491 | |
| 492 | uint16_t CCECBusDevice::GetPhysicalAddress(const cec_logical_address initiator, bool bSuppressUpdate /* = false */) |
| 493 | { |
| 494 | if (!bSuppressUpdate) |
| 495 | { |
| 496 | bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT); |
| 497 | bool bRequestUpdate(false); |
| 498 | { |
| 499 | CLockObject lock(m_mutex); |
| 500 | bRequestUpdate = bIsPresent && m_iPhysicalAddress == CEC_INVALID_PHYSICAL_ADDRESS; |
| 501 | } |
| 502 | |
| 503 | if (bRequestUpdate) |
| 504 | { |
| 505 | CheckVendorIdRequested(initiator); |
| 506 | if (!RequestPhysicalAddress(initiator)) |
| 507 | LIB_CEC->AddLog(CEC_LOG_ERROR, "failed to request the physical address"); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | CLockObject lock(m_mutex); |
| 512 | return m_iPhysicalAddress; |
| 513 | } |
| 514 | |
| 515 | bool CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress) |
| 516 | { |
| 517 | CLockObject lock(m_mutex); |
| 518 | if (iNewAddress > 0 && m_iPhysicalAddress != iNewAddress) |
| 519 | { |
| 520 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): physical address changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress, iNewAddress); |
| 521 | m_iPhysicalAddress = iNewAddress; |
| 522 | } |
| 523 | return true; |
| 524 | } |
| 525 | |
| 526 | bool CCECBusDevice::RequestPhysicalAddress(const cec_logical_address initiator, bool bWaitForResponse /* = true */) |
| 527 | { |
| 528 | bool bReturn(false); |
| 529 | |
| 530 | if (!IsHandledByLibCEC()) |
| 531 | { |
| 532 | MarkBusy(); |
| 533 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); |
| 534 | bReturn = m_handler->TransmitRequestPhysicalAddress(initiator, m_iLogicalAddress, bWaitForResponse); |
| 535 | MarkReady(); |
| 536 | } |
| 537 | return bReturn; |
| 538 | } |
| 539 | |
| 540 | bool CCECBusDevice::TransmitPhysicalAddress(void) |
| 541 | { |
| 542 | uint16_t iPhysicalAddress; |
| 543 | cec_device_type type; |
| 544 | { |
| 545 | CLockObject lock(m_mutex); |
| 546 | if (m_iPhysicalAddress == CEC_INVALID_PHYSICAL_ADDRESS) |
| 547 | return false; |
| 548 | |
| 549 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress); |
| 550 | iPhysicalAddress = m_iPhysicalAddress; |
| 551 | type = m_type; |
| 552 | } |
| 553 | |
| 554 | MarkBusy(); |
| 555 | bool bReturn = m_handler->TransmitPhysicalAddress(m_iLogicalAddress, iPhysicalAddress, type); |
| 556 | MarkReady(); |
| 557 | return bReturn; |
| 558 | } |
| 559 | |
| 560 | cec_power_status CCECBusDevice::GetCurrentPowerStatus(void) |
| 561 | { |
| 562 | CLockObject lock(m_mutex); |
| 563 | return m_powerStatus; |
| 564 | } |
| 565 | |
| 566 | cec_power_status CCECBusDevice::GetPowerStatus(const cec_logical_address initiator, bool bUpdate /* = false */) |
| 567 | { |
| 568 | bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT); |
| 569 | bool bRequestUpdate(false); |
| 570 | { |
| 571 | CLockObject lock(m_mutex); |
| 572 | bRequestUpdate = (bIsPresent && |
| 573 | (bUpdate || m_powerStatus == CEC_POWER_STATUS_UNKNOWN || |
| 574 | m_powerStatus == CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON || |
| 575 | m_powerStatus == CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY || |
| 576 | GetTimeMs() - m_iLastPowerStateUpdate >= CEC_POWER_STATE_REFRESH_TIME)); |
| 577 | } |
| 578 | |
| 579 | if (bRequestUpdate) |
| 580 | { |
| 581 | CheckVendorIdRequested(initiator); |
| 582 | RequestPowerStatus(initiator); |
| 583 | } |
| 584 | |
| 585 | CLockObject lock(m_mutex); |
| 586 | return m_powerStatus; |
| 587 | } |
| 588 | |
| 589 | void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus) |
| 590 | { |
| 591 | CLockObject lock(m_mutex); |
| 592 | if (m_powerStatus != powerStatus) |
| 593 | { |
| 594 | m_iLastPowerStateUpdate = GetTimeMs(); |
| 595 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): power status changed from '%s' to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_powerStatus), ToString(powerStatus)); |
| 596 | m_powerStatus = powerStatus; |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | bool CCECBusDevice::RequestPowerStatus(const cec_logical_address initiator, bool bWaitForResponse /* = true */) |
| 601 | { |
| 602 | bool bReturn(false); |
| 603 | |
| 604 | if (!IsHandledByLibCEC() && |
| 605 | !IsUnsupportedFeature(CEC_OPCODE_GIVE_DEVICE_POWER_STATUS)) |
| 606 | { |
| 607 | MarkBusy(); |
| 608 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); |
| 609 | bReturn = m_handler->TransmitRequestPowerStatus(initiator, m_iLogicalAddress, bWaitForResponse); |
| 610 | MarkReady(); |
| 611 | } |
| 612 | return bReturn; |
| 613 | } |
| 614 | |
| 615 | bool CCECBusDevice::TransmitPowerState(const cec_logical_address destination) |
| 616 | { |
| 617 | cec_power_status state; |
| 618 | { |
| 619 | CLockObject lock(m_mutex); |
| 620 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination, ToString(m_powerStatus)); |
| 621 | state = m_powerStatus; |
| 622 | } |
| 623 | |
| 624 | MarkBusy(); |
| 625 | bool bReturn = m_handler->TransmitPowerState(m_iLogicalAddress, destination, state); |
| 626 | MarkReady(); |
| 627 | return bReturn; |
| 628 | } |
| 629 | |
| 630 | cec_vendor_id CCECBusDevice::GetCurrentVendorId(void) |
| 631 | { |
| 632 | CLockObject lock(m_mutex); |
| 633 | return m_vendor; |
| 634 | } |
| 635 | |
| 636 | cec_vendor_id CCECBusDevice::GetVendorId(const cec_logical_address initiator, bool bUpdate /* = false */) |
| 637 | { |
| 638 | bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT); |
| 639 | bool bRequestUpdate(false); |
| 640 | { |
| 641 | CLockObject lock(m_mutex); |
| 642 | bRequestUpdate = (bIsPresent && |
| 643 | (bUpdate || m_vendor == CEC_VENDOR_UNKNOWN)); |
| 644 | } |
| 645 | |
| 646 | if (bRequestUpdate) |
| 647 | RequestVendorId(initiator); |
| 648 | |
| 649 | CLockObject lock(m_mutex); |
| 650 | return m_vendor; |
| 651 | } |
| 652 | |
| 653 | const char *CCECBusDevice::GetVendorName(const cec_logical_address initiator, bool bUpdate /* = false */) |
| 654 | { |
| 655 | return ToString(GetVendorId(initiator, bUpdate)); |
| 656 | } |
| 657 | |
| 658 | bool CCECBusDevice::SetVendorId(uint64_t iVendorId) |
| 659 | { |
| 660 | bool bVendorChanged(false); |
| 661 | |
| 662 | { |
| 663 | CLockObject lock(m_mutex); |
| 664 | bVendorChanged = (m_vendor != (cec_vendor_id)iVendorId); |
| 665 | m_vendor = (cec_vendor_id)iVendorId; |
| 666 | } |
| 667 | |
| 668 | if (bVendorChanged) |
| 669 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_vendor), m_vendor); |
| 670 | |
| 671 | return bVendorChanged; |
| 672 | } |
| 673 | |
| 674 | bool CCECBusDevice::RequestVendorId(const cec_logical_address initiator, bool bWaitForResponse /* = true */) |
| 675 | { |
| 676 | bool bReturn(false); |
| 677 | |
| 678 | if (!IsHandledByLibCEC() && initiator != CECDEVICE_UNKNOWN) |
| 679 | { |
| 680 | MarkBusy(); |
| 681 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); |
| 682 | bReturn = m_handler->TransmitRequestVendorId(initiator, m_iLogicalAddress, bWaitForResponse); |
| 683 | MarkReady(); |
| 684 | |
| 685 | if (bWaitForResponse) |
| 686 | ReplaceHandler(true); |
| 687 | } |
| 688 | return bReturn; |
| 689 | } |
| 690 | |
| 691 | bool CCECBusDevice::TransmitVendorID(const cec_logical_address destination, bool bSendAbort /* = true */) |
| 692 | { |
| 693 | bool bReturn(false); |
| 694 | uint64_t iVendorId; |
| 695 | { |
| 696 | CLockObject lock(m_mutex); |
| 697 | iVendorId = (uint64_t)m_vendor; |
| 698 | } |
| 699 | |
| 700 | MarkBusy(); |
| 701 | if (iVendorId == CEC_VENDOR_UNKNOWN) |
| 702 | { |
| 703 | if (bSendAbort) |
| 704 | { |
| 705 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination); |
| 706 | m_processor->TransmitAbort(m_iLogicalAddress, destination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID); |
| 707 | bReturn = true; |
| 708 | } |
| 709 | } |
| 710 | else |
| 711 | { |
| 712 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): vendor id %s (%x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination, ToString((cec_vendor_id)iVendorId), iVendorId); |
| 713 | bReturn = m_handler->TransmitVendorID(m_iLogicalAddress, iVendorId); |
| 714 | } |
| 715 | MarkReady(); |
| 716 | return bReturn; |
| 717 | } |
| 718 | |
| 719 | cec_bus_device_status CCECBusDevice::GetStatus(bool bForcePoll /* = false */, bool bSuppressPoll /* = false */) |
| 720 | { |
| 721 | cec_bus_device_status status(CEC_DEVICE_STATUS_UNKNOWN); |
| 722 | bool bNeedsPoll(false); |
| 723 | |
| 724 | { |
| 725 | CLockObject lock(m_mutex); |
| 726 | status = m_deviceStatus; |
| 727 | bNeedsPoll = !bSuppressPoll && |
| 728 | (bForcePoll || m_deviceStatus == CEC_DEVICE_STATUS_UNKNOWN); |
| 729 | } |
| 730 | |
| 731 | if (bNeedsPoll) |
| 732 | { |
| 733 | bool bPollAcked(false); |
| 734 | if (bNeedsPoll && NeedsPoll()) |
| 735 | bPollAcked = m_processor->PollDevice(m_iLogicalAddress); |
| 736 | |
| 737 | status = bPollAcked ? CEC_DEVICE_STATUS_PRESENT : CEC_DEVICE_STATUS_NOT_PRESENT; |
| 738 | SetDeviceStatus(status); |
| 739 | } |
| 740 | |
| 741 | return status; |
| 742 | } |
| 743 | |
| 744 | void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus) |
| 745 | { |
| 746 | { |
| 747 | CLockObject lock(m_mutex); |
| 748 | switch (newStatus) |
| 749 | { |
| 750 | case CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC: |
| 751 | if (m_deviceStatus != newStatus) |
| 752 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): device status changed into 'handled by libCEC'", GetLogicalAddressName(), m_iLogicalAddress); |
| 753 | SetPowerStatus (CEC_POWER_STATUS_ON); |
| 754 | SetVendorId (CEC_VENDOR_UNKNOWN); |
| 755 | SetMenuState (CEC_MENU_STATE_ACTIVATED); |
| 756 | SetCecVersion (CEC_VERSION_1_3A); |
| 757 | SetStreamPath (CEC_INVALID_PHYSICAL_ADDRESS); |
| 758 | MarkAsInactiveSource(); |
| 759 | m_iLastActive = 0; |
| 760 | m_deviceStatus = newStatus; |
| 761 | break; |
| 762 | case CEC_DEVICE_STATUS_PRESENT: |
| 763 | if (m_deviceStatus != newStatus) |
| 764 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): device status changed into 'present'", GetLogicalAddressName(), m_iLogicalAddress); |
| 765 | m_deviceStatus = newStatus; |
| 766 | break; |
| 767 | case CEC_DEVICE_STATUS_NOT_PRESENT: |
| 768 | if (m_deviceStatus != newStatus) |
| 769 | { |
| 770 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): device status changed into 'not present'", GetLogicalAddressName(), m_iLogicalAddress); |
| 771 | ResetDeviceStatus(); |
| 772 | m_deviceStatus = newStatus; |
| 773 | } |
| 774 | break; |
| 775 | default: |
| 776 | ResetDeviceStatus(); |
| 777 | break; |
| 778 | } |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | void CCECBusDevice::ResetDeviceStatus(void) |
| 783 | { |
| 784 | CLockObject lock(m_mutex); |
| 785 | SetPowerStatus (CEC_POWER_STATUS_UNKNOWN); |
| 786 | SetVendorId (CEC_VENDOR_UNKNOWN); |
| 787 | SetMenuState (CEC_MENU_STATE_ACTIVATED); |
| 788 | SetCecVersion (CEC_VERSION_UNKNOWN); |
| 789 | SetStreamPath (CEC_INVALID_PHYSICAL_ADDRESS); |
| 790 | SetOSDName (ToString(m_iLogicalAddress)); |
| 791 | MarkAsInactiveSource(); |
| 792 | |
| 793 | m_iLastActive = 0; |
| 794 | m_bVendorIdRequested = false; |
| 795 | m_unsupportedFeatures.clear(); |
| 796 | |
| 797 | if (m_deviceStatus != CEC_DEVICE_STATUS_UNKNOWN) |
| 798 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): device status changed into 'unknown'", GetLogicalAddressName(), m_iLogicalAddress); |
| 799 | m_deviceStatus = CEC_DEVICE_STATUS_UNKNOWN; |
| 800 | } |
| 801 | |
| 802 | bool CCECBusDevice::TransmitPoll(const cec_logical_address dest) |
| 803 | { |
| 804 | bool bReturn(false); |
| 805 | cec_logical_address destination(dest); |
| 806 | if (destination == CECDEVICE_UNKNOWN) |
| 807 | destination = m_iLogicalAddress; |
| 808 | |
| 809 | CCECBusDevice *destDevice = m_processor->GetDevice(destination); |
| 810 | if (destDevice->m_deviceStatus == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC) |
| 811 | return bReturn; |
| 812 | |
| 813 | MarkBusy(); |
| 814 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest); |
| 815 | bReturn = m_handler->TransmitPoll(m_iLogicalAddress, destination); |
| 816 | LIB_CEC->AddLog(CEC_LOG_DEBUG, bReturn ? ">> POLL sent" : ">> POLL not sent"); |
| 817 | |
| 818 | CLockObject lock(m_mutex); |
| 819 | if (bReturn) |
| 820 | { |
| 821 | m_iLastActive = GetTimeMs(); |
| 822 | destDevice->m_deviceStatus = CEC_DEVICE_STATUS_PRESENT; |
| 823 | } |
| 824 | else |
| 825 | destDevice->m_deviceStatus = CEC_DEVICE_STATUS_NOT_PRESENT; |
| 826 | |
| 827 | MarkReady(); |
| 828 | return bReturn; |
| 829 | } |
| 830 | |
| 831 | void CCECBusDevice::HandlePoll(const cec_logical_address destination) |
| 832 | { |
| 833 | if (destination >= 0 && destination < CECDEVICE_BROADCAST) |
| 834 | { |
| 835 | CCECBusDevice *device = m_processor->GetDevice(destination); |
| 836 | if (device) |
| 837 | device->HandlePollFrom(m_iLogicalAddress); |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | void CCECBusDevice::HandlePollFrom(const cec_logical_address initiator) |
| 842 | { |
| 843 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< POLL: %s (%x) -> %s (%x)", ToString(initiator), initiator, ToString(m_iLogicalAddress), m_iLogicalAddress); |
| 844 | m_bAwaitingReceiveFailed = true; |
| 845 | } |
| 846 | |
| 847 | bool CCECBusDevice::HandleReceiveFailed(void) |
| 848 | { |
| 849 | bool bReturn = m_bAwaitingReceiveFailed; |
| 850 | m_bAwaitingReceiveFailed = false; |
| 851 | return bReturn; |
| 852 | } |
| 853 | |
| 854 | cec_menu_state CCECBusDevice::GetMenuState(const cec_logical_address UNUSED(initiator)) |
| 855 | { |
| 856 | CLockObject lock(m_mutex); |
| 857 | return m_menuState; |
| 858 | } |
| 859 | |
| 860 | void CCECBusDevice::SetMenuState(const cec_menu_state state) |
| 861 | { |
| 862 | CLockObject lock(m_mutex); |
| 863 | if (m_menuState != state) |
| 864 | { |
| 865 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): menu state set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_menuState)); |
| 866 | m_menuState = state; |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | bool CCECBusDevice::TransmitMenuState(const cec_logical_address dest) |
| 871 | { |
| 872 | cec_menu_state menuState; |
| 873 | { |
| 874 | CLockObject lock(m_mutex); |
| 875 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): menu state '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_menuState)); |
| 876 | menuState = m_menuState; |
| 877 | } |
| 878 | |
| 879 | MarkBusy(); |
| 880 | bool bReturn = m_handler->TransmitMenuState(m_iLogicalAddress, dest, menuState); |
| 881 | MarkReady(); |
| 882 | return bReturn; |
| 883 | } |
| 884 | |
| 885 | bool CCECBusDevice::ActivateSource(void) |
| 886 | { |
| 887 | MarkAsActiveSource(); |
| 888 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "activating source '%s'", ToString(m_iLogicalAddress)); |
| 889 | MarkBusy(); |
| 890 | bool bReturn = m_handler->ActivateSource(); |
| 891 | MarkReady(); |
| 892 | return bReturn; |
| 893 | } |
| 894 | |
| 895 | bool CCECBusDevice::RequestActiveSource(bool bWaitForResponse /* = true */) |
| 896 | { |
| 897 | bool bReturn(false); |
| 898 | |
| 899 | if (IsHandledByLibCEC()) |
| 900 | { |
| 901 | MarkBusy(); |
| 902 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< requesting active source"); |
| 903 | |
| 904 | bReturn = m_handler->TransmitRequestActiveSource(m_iLogicalAddress, bWaitForResponse); |
| 905 | MarkReady(); |
| 906 | } |
| 907 | return bReturn; |
| 908 | } |
| 909 | |
| 910 | void CCECBusDevice::MarkAsActiveSource(void) |
| 911 | { |
| 912 | CLockObject lock(m_mutex); |
| 913 | if (!m_bActiveSource) |
| 914 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "making %s (%x) the active source", GetLogicalAddressName(), m_iLogicalAddress); |
| 915 | else |
| 916 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%x) was already marked as active source", GetLogicalAddressName(), m_iLogicalAddress); |
| 917 | |
| 918 | CECDEVICEVEC devices; |
| 919 | m_processor->GetDevices()->Get(devices); |
| 920 | for (CECDEVICEVEC::iterator it = devices.begin(); it != devices.end(); it++) |
| 921 | if ((*it)->GetLogicalAddress() != m_iLogicalAddress) |
| 922 | (*it)->MarkAsInactiveSource(); |
| 923 | |
| 924 | m_bActiveSource = true; |
| 925 | SetPowerStatus(CEC_POWER_STATUS_ON); |
| 926 | } |
| 927 | |
| 928 | void CCECBusDevice::MarkAsInactiveSource(void) |
| 929 | { |
| 930 | { |
| 931 | CLockObject lock(m_mutex); |
| 932 | if (m_bActiveSource) |
| 933 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "marking %s (%X) as inactive source", GetLogicalAddressName(), m_iLogicalAddress); |
| 934 | m_bActiveSource = false; |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | bool CCECBusDevice::TransmitActiveSource(void) |
| 939 | { |
| 940 | bool bSendActiveSource(false); |
| 941 | |
| 942 | { |
| 943 | CLockObject lock(m_mutex); |
| 944 | if (m_powerStatus != CEC_POWER_STATUS_ON && m_powerStatus != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON) |
| 945 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress); |
| 946 | else if (m_bActiveSource) |
| 947 | { |
| 948 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): active source (%4x)", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress); |
| 949 | bSendActiveSource = true; |
| 950 | } |
| 951 | else |
| 952 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress); |
| 953 | } |
| 954 | |
| 955 | if (bSendActiveSource) |
| 956 | { |
| 957 | MarkBusy(); |
| 958 | m_handler->TransmitActiveSource(m_iLogicalAddress, m_iPhysicalAddress); |
| 959 | MarkReady(); |
| 960 | return true; |
| 961 | } |
| 962 | |
| 963 | return false; |
| 964 | } |
| 965 | |
| 966 | bool CCECBusDevice::TransmitImageViewOn(void) |
| 967 | { |
| 968 | { |
| 969 | CLockObject lock(m_mutex); |
| 970 | if (m_powerStatus != CEC_POWER_STATUS_ON && m_powerStatus != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON) |
| 971 | { |
| 972 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress); |
| 973 | return false; |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | MarkBusy(); |
| 978 | m_handler->TransmitImageViewOn(m_iLogicalAddress, CECDEVICE_TV); |
| 979 | MarkReady(); |
| 980 | return true; |
| 981 | } |
| 982 | |
| 983 | bool CCECBusDevice::TransmitInactiveSource(void) |
| 984 | { |
| 985 | uint16_t iPhysicalAddress; |
| 986 | { |
| 987 | CLockObject lock(m_mutex); |
| 988 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): inactive source", GetLogicalAddressName(), m_iLogicalAddress); |
| 989 | iPhysicalAddress = m_iPhysicalAddress; |
| 990 | } |
| 991 | |
| 992 | MarkBusy(); |
| 993 | bool bReturn = m_handler->TransmitInactiveSource(m_iLogicalAddress, iPhysicalAddress); |
| 994 | MarkReady(); |
| 995 | return bReturn; |
| 996 | } |
| 997 | |
| 998 | bool CCECBusDevice::TransmitPendingActiveSourceCommands(void) |
| 999 | { |
| 1000 | MarkBusy(); |
| 1001 | bool bReturn = m_handler->TransmitPendingActiveSourceCommands(); |
| 1002 | MarkReady(); |
| 1003 | return bReturn; |
| 1004 | } |
| 1005 | |
| 1006 | void CCECBusDevice::SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress /* = CEC_INVALID_PHYSICAL_ADDRESS */) |
| 1007 | { |
| 1008 | CLockObject lock(m_mutex); |
| 1009 | if (iNewAddress != m_iStreamPath) |
| 1010 | { |
| 1011 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, iOldAddress == 0 ? m_iStreamPath : iOldAddress, iNewAddress); |
| 1012 | m_iStreamPath = iNewAddress; |
| 1013 | } |
| 1014 | |
| 1015 | if (!LIB_CEC->IsValidPhysicalAddress(iNewAddress)) |
| 1016 | return; |
| 1017 | |
| 1018 | CCECBusDevice *device = m_processor->GetDeviceByPhysicalAddress(iNewAddress); |
| 1019 | if (device) |
| 1020 | { |
| 1021 | // if a device is found with the new physical address, mark it as active, which will automatically mark all other devices as inactive |
| 1022 | device->MarkAsActiveSource(); |
| 1023 | } |
| 1024 | else |
| 1025 | { |
| 1026 | // try to find the device with the old address, and mark it as inactive when found |
| 1027 | device = m_processor->GetDeviceByPhysicalAddress(iOldAddress); |
| 1028 | if (device) |
| 1029 | device->MarkAsInactiveSource(); |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | bool CCECBusDevice::PowerOn(const cec_logical_address initiator) |
| 1034 | { |
| 1035 | bool bReturn(false); |
| 1036 | GetVendorId(initiator); // ensure that we got the vendor id, because the implementations vary per vendor |
| 1037 | |
| 1038 | MarkBusy(); |
| 1039 | cec_power_status currentStatus = GetPowerStatus(initiator, false); |
| 1040 | if (currentStatus != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON && |
| 1041 | currentStatus != CEC_POWER_STATUS_ON) |
| 1042 | { |
| 1043 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); |
| 1044 | if (m_handler->PowerOn(initiator, m_iLogicalAddress)) |
| 1045 | { |
| 1046 | SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); |
| 1047 | bReturn = true; |
| 1048 | } |
| 1049 | } |
| 1050 | else |
| 1051 | { |
| 1052 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "'%s' (%X) is already '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(currentStatus)); |
| 1053 | } |
| 1054 | |
| 1055 | MarkReady(); |
| 1056 | return bReturn; |
| 1057 | } |
| 1058 | |
| 1059 | bool CCECBusDevice::Standby(const cec_logical_address initiator) |
| 1060 | { |
| 1061 | GetVendorId(initiator); // ensure that we got the vendor id, because the implementations vary per vendor |
| 1062 | |
| 1063 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress); |
| 1064 | MarkBusy(); |
| 1065 | bool bReturn = m_handler->TransmitStandby(initiator, m_iLogicalAddress); |
| 1066 | MarkReady(); |
| 1067 | return bReturn; |
| 1068 | } |
| 1069 | |
| 1070 | bool CCECBusDevice::NeedsPoll(void) |
| 1071 | { |
| 1072 | bool bSendPoll(false); |
| 1073 | cec_logical_address pollAddress(CECDEVICE_UNKNOWN); |
| 1074 | switch (m_iLogicalAddress) |
| 1075 | { |
| 1076 | case CECDEVICE_PLAYBACKDEVICE3: |
| 1077 | pollAddress = CECDEVICE_PLAYBACKDEVICE2; |
| 1078 | break; |
| 1079 | case CECDEVICE_PLAYBACKDEVICE2: |
| 1080 | pollAddress = CECDEVICE_PLAYBACKDEVICE1; |
| 1081 | break; |
| 1082 | case CECDEVICE_RECORDINGDEVICE3: |
| 1083 | pollAddress = CECDEVICE_RECORDINGDEVICE2; |
| 1084 | break; |
| 1085 | case CECDEVICE_RECORDINGDEVICE2: |
| 1086 | pollAddress = CECDEVICE_RECORDINGDEVICE1; |
| 1087 | break; |
| 1088 | case CECDEVICE_TUNER4: |
| 1089 | pollAddress = CECDEVICE_TUNER3; |
| 1090 | break; |
| 1091 | case CECDEVICE_TUNER3: |
| 1092 | pollAddress = CECDEVICE_TUNER2; |
| 1093 | break; |
| 1094 | case CECDEVICE_TUNER2: |
| 1095 | pollAddress = CECDEVICE_TUNER1; |
| 1096 | break; |
| 1097 | case CECDEVICE_AUDIOSYSTEM: |
| 1098 | case CECDEVICE_PLAYBACKDEVICE1: |
| 1099 | case CECDEVICE_RECORDINGDEVICE1: |
| 1100 | case CECDEVICE_TUNER1: |
| 1101 | case CECDEVICE_TV: |
| 1102 | bSendPoll = true; |
| 1103 | break; |
| 1104 | default: |
| 1105 | break; |
| 1106 | } |
| 1107 | |
| 1108 | if (!bSendPoll && pollAddress != CECDEVICE_UNKNOWN) |
| 1109 | { |
| 1110 | CCECBusDevice *device = m_processor->GetDevice(pollAddress); |
| 1111 | if (device) |
| 1112 | { |
| 1113 | cec_bus_device_status status = device->GetStatus(); |
| 1114 | bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC); |
| 1115 | } |
| 1116 | else |
| 1117 | { |
| 1118 | bSendPoll = true; |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | return bSendPoll; |
| 1123 | } |
| 1124 | |
| 1125 | void CCECBusDevice::CheckVendorIdRequested(const cec_logical_address initiator) |
| 1126 | { |
| 1127 | bool bRequestVendorId(false); |
| 1128 | { |
| 1129 | CLockObject lock(m_mutex); |
| 1130 | bRequestVendorId = !m_bVendorIdRequested; |
| 1131 | m_bVendorIdRequested = true; |
| 1132 | } |
| 1133 | |
| 1134 | if (bRequestVendorId) |
| 1135 | { |
| 1136 | ReplaceHandler(false); |
| 1137 | GetVendorId(initiator); |
| 1138 | } |
| 1139 | } |
| 1140 | //@} |
| 1141 | |
| 1142 | CCECAudioSystem *CCECBusDevice::AsAudioSystem(void) |
| 1143 | { |
| 1144 | return AsAudioSystem(this); |
| 1145 | } |
| 1146 | |
| 1147 | CCECPlaybackDevice *CCECBusDevice::AsPlaybackDevice(void) |
| 1148 | { |
| 1149 | return AsPlaybackDevice(this); |
| 1150 | } |
| 1151 | |
| 1152 | CCECRecordingDevice *CCECBusDevice::AsRecordingDevice(void) |
| 1153 | { |
| 1154 | return AsRecordingDevice(this); |
| 1155 | } |
| 1156 | |
| 1157 | CCECTuner *CCECBusDevice::AsTuner(void) |
| 1158 | { |
| 1159 | return AsTuner(this); |
| 1160 | } |
| 1161 | |
| 1162 | CCECTV *CCECBusDevice::AsTV(void) |
| 1163 | { |
| 1164 | return AsTV(this); |
| 1165 | } |
| 1166 | |
| 1167 | CCECAudioSystem *CCECBusDevice::AsAudioSystem(CCECBusDevice *device) |
| 1168 | { |
| 1169 | if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM) |
| 1170 | return static_cast<CCECAudioSystem *>(device); |
| 1171 | return NULL; |
| 1172 | } |
| 1173 | |
| 1174 | CCECPlaybackDevice *CCECBusDevice::AsPlaybackDevice(CCECBusDevice *device) |
| 1175 | { |
| 1176 | if (device && |
| 1177 | (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || |
| 1178 | device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE)) |
| 1179 | return static_cast<CCECPlaybackDevice *>(device); |
| 1180 | return NULL; |
| 1181 | } |
| 1182 | |
| 1183 | CCECRecordingDevice *CCECBusDevice::AsRecordingDevice(CCECBusDevice *device) |
| 1184 | { |
| 1185 | if (device && device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE) |
| 1186 | return static_cast<CCECRecordingDevice *>(device); |
| 1187 | return NULL; |
| 1188 | } |
| 1189 | |
| 1190 | CCECTuner *CCECBusDevice::AsTuner(CCECBusDevice *device) |
| 1191 | { |
| 1192 | if (device && device->GetType() == CEC_DEVICE_TYPE_TUNER) |
| 1193 | return static_cast<CCECTuner *>(device); |
| 1194 | return NULL; |
| 1195 | } |
| 1196 | |
| 1197 | CCECTV *CCECBusDevice::AsTV(CCECBusDevice *device) |
| 1198 | { |
| 1199 | if (device && device->GetType() == CEC_DEVICE_TYPE_TV) |
| 1200 | return static_cast<CCECTV *>(device); |
| 1201 | return NULL; |
| 1202 | } |
| 1203 | |
| 1204 | void CCECBusDevice::MarkBusy(void) |
| 1205 | { |
| 1206 | CLockObject handlerLock(m_handlerMutex); |
| 1207 | ++m_iHandlerUseCount; |
| 1208 | } |
| 1209 | |
| 1210 | void CCECBusDevice::MarkReady(void) |
| 1211 | { |
| 1212 | CLockObject handlerLock(m_handlerMutex); |
| 1213 | if (m_iHandlerUseCount > 0) |
| 1214 | --m_iHandlerUseCount; |
| 1215 | } |
| 1216 | |
| 1217 | bool CCECBusDevice::TryLogicalAddress(void) |
| 1218 | { |
| 1219 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "trying logical address '%s'", GetLogicalAddressName()); |
| 1220 | |
| 1221 | if (!TransmitPoll(m_iLogicalAddress)) |
| 1222 | { |
| 1223 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "using logical address '%s'", GetLogicalAddressName()); |
| 1224 | SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC); |
| 1225 | |
| 1226 | return true; |
| 1227 | } |
| 1228 | |
| 1229 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "logical address '%s' already taken", GetLogicalAddressName()); |
| 1230 | SetDeviceStatus(CEC_DEVICE_STATUS_PRESENT); |
| 1231 | return false; |
| 1232 | } |
| 1233 | |
| 1234 | CCECClient *CCECBusDevice::GetClient(void) |
| 1235 | { |
| 1236 | return m_processor->GetClient(m_iLogicalAddress); |
| 1237 | } |