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