| 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 "../platform/util/timeutils.h" |
| 41 | |
| 42 | using namespace CEC; |
| 43 | using namespace PLATFORM; |
| 44 | |
| 45 | #define ToString(p) m_processor->ToString(p) |
| 46 | |
| 47 | CCECBusDevice::CCECBusDevice(CCECProcessor *processor, cec_logical_address iLogicalAddress, uint16_t iPhysicalAddress) : |
| 48 | m_type(CEC_DEVICE_TYPE_RESERVED), |
| 49 | m_iPhysicalAddress(iPhysicalAddress), |
| 50 | m_iStreamPath(0), |
| 51 | m_iLogicalAddress(iLogicalAddress), |
| 52 | m_powerStatus(CEC_POWER_STATUS_UNKNOWN), |
| 53 | m_processor(processor), |
| 54 | m_vendor(CEC_VENDOR_UNKNOWN), |
| 55 | m_bReplaceHandler(false), |
| 56 | m_menuState(CEC_MENU_STATE_ACTIVATED), |
| 57 | m_bActiveSource(false), |
| 58 | m_iLastActive(0), |
| 59 | m_iLastPowerStateUpdate(0), |
| 60 | m_cecVersion(CEC_VERSION_UNKNOWN), |
| 61 | m_deviceStatus(CEC_DEVICE_STATUS_UNKNOWN) |
| 62 | { |
| 63 | m_handler = new CCECCommandHandler(this); |
| 64 | |
| 65 | for (unsigned int iPtr = 0; iPtr < 4; iPtr++) |
| 66 | m_menuLanguage.language[iPtr] = '?'; |
| 67 | m_menuLanguage.language[3] = 0; |
| 68 | m_menuLanguage.device = iLogicalAddress; |
| 69 | |
| 70 | m_strDeviceName = ToString(m_iLogicalAddress); |
| 71 | } |
| 72 | |
| 73 | CCECBusDevice::~CCECBusDevice(void) |
| 74 | { |
| 75 | delete m_handler; |
| 76 | } |
| 77 | |
| 78 | bool CCECBusDevice::HandleCommand(const cec_command &command) |
| 79 | { |
| 80 | bool bHandled(false); |
| 81 | |
| 82 | /* update "last active" */ |
| 83 | { |
| 84 | CLockObject lock(m_mutex); |
| 85 | m_iLastActive = GetTimeMs(); |
| 86 | |
| 87 | if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC) |
| 88 | m_deviceStatus = CEC_DEVICE_STATUS_PRESENT; |
| 89 | } |
| 90 | |
| 91 | /* handle the command */ |
| 92 | bHandled = m_handler->HandleCommand(command); |
| 93 | |
| 94 | /* change status to present */ |
| 95 | if (bHandled) |
| 96 | { |
| 97 | CLockObject lock(m_mutex); |
| 98 | if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC) |
| 99 | { |
| 100 | if (m_deviceStatus != CEC_DEVICE_STATUS_PRESENT) |
| 101 | CLibCEC::AddLog(CEC_LOG_DEBUG, "device %s (%x) status changed to present after command %s", GetLogicalAddressName(), (uint8_t)GetLogicalAddress(), ToString(command.opcode)); |
| 102 | m_deviceStatus = CEC_DEVICE_STATUS_PRESENT; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return bHandled; |
| 107 | } |
| 108 | |
| 109 | bool CCECBusDevice::PowerOn(void) |
| 110 | { |
| 111 | CLibCEC::AddLog(CEC_LOG_DEBUG, "<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); |
| 112 | |
| 113 | if (m_handler->TransmitImageViewOn(GetMyLogicalAddress(), m_iLogicalAddress)) |
| 114 | { |
| 115 | { |
| 116 | CLockObject lock(m_mutex); |
| 117 | // m_powerStatus = CEC_POWER_STATUS_UNKNOWN; |
| 118 | m_powerStatus = CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON; |
| 119 | } |
| 120 | // cec_power_status status = GetPowerStatus(); |
| 121 | // if (status == CEC_POWER_STATUS_STANDBY || status == CEC_POWER_STATUS_UNKNOWN) |
| 122 | // { |
| 123 | // /* sending the normal power on command appears to have failed */ |
| 124 | // CStdString strLog; |
| 125 | // strLog.Format("<< sending power on keypress to '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); |
| 126 | // CLibCEC::AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
| 127 | // |
| 128 | // TransmitKeypress(CEC_USER_CONTROL_CODE_POWER); |
| 129 | // return TransmitKeyRelease(); |
| 130 | // } |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | bool CCECBusDevice::Standby(void) |
| 138 | { |
| 139 | CLibCEC::AddLog(CEC_LOG_DEBUG, "<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress); |
| 140 | return m_handler->TransmitStandby(GetMyLogicalAddress(), m_iLogicalAddress); |
| 141 | } |
| 142 | |
| 143 | /** @name Getters */ |
| 144 | //@{ |
| 145 | cec_version CCECBusDevice::GetCecVersion(bool bUpdate /* = false */) |
| 146 | { |
| 147 | bool bRequestUpdate(false); |
| 148 | { |
| 149 | CLockObject lock(m_mutex); |
| 150 | bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT && |
| 151 | (bUpdate || m_cecVersion == CEC_VERSION_UNKNOWN)); |
| 152 | } |
| 153 | |
| 154 | if (bRequestUpdate) |
| 155 | RequestCecVersion(); |
| 156 | |
| 157 | CLockObject lock(m_mutex); |
| 158 | return m_cecVersion; |
| 159 | } |
| 160 | |
| 161 | bool CCECBusDevice::RequestCecVersion(void) |
| 162 | { |
| 163 | bool bReturn(false); |
| 164 | |
| 165 | if (!MyLogicalAddressContains(m_iLogicalAddress)) |
| 166 | { |
| 167 | m_handler->MarkBusy(); |
| 168 | CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); |
| 169 | |
| 170 | bReturn = m_handler->TransmitRequestCecVersion(GetMyLogicalAddress(), m_iLogicalAddress); |
| 171 | m_handler->MarkReady(); |
| 172 | } |
| 173 | return bReturn; |
| 174 | } |
| 175 | |
| 176 | const char* CCECBusDevice::GetLogicalAddressName(void) const |
| 177 | { |
| 178 | return ToString(m_iLogicalAddress); |
| 179 | } |
| 180 | |
| 181 | cec_menu_language &CCECBusDevice::GetMenuLanguage(bool bUpdate /* = false */) |
| 182 | { |
| 183 | bool bRequestUpdate(false); |
| 184 | { |
| 185 | CLockObject lock(m_mutex); |
| 186 | bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT && |
| 187 | (bUpdate || !strcmp(m_menuLanguage.language, "???"))); |
| 188 | } |
| 189 | |
| 190 | if (bRequestUpdate) |
| 191 | RequestMenuLanguage(); |
| 192 | |
| 193 | CLockObject lock(m_mutex); |
| 194 | return m_menuLanguage; |
| 195 | } |
| 196 | |
| 197 | bool CCECBusDevice::RequestMenuLanguage(void) |
| 198 | { |
| 199 | bool bReturn(false); |
| 200 | |
| 201 | if (!MyLogicalAddressContains(m_iLogicalAddress) && |
| 202 | !IsUnsupportedFeature(CEC_OPCODE_GET_MENU_LANGUAGE)) |
| 203 | { |
| 204 | m_handler->MarkBusy(); |
| 205 | CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); |
| 206 | bReturn = m_handler->TransmitRequestMenuLanguage(GetMyLogicalAddress(), m_iLogicalAddress); |
| 207 | m_handler->MarkReady(); |
| 208 | } |
| 209 | return bReturn; |
| 210 | } |
| 211 | |
| 212 | cec_menu_state CCECBusDevice::GetMenuState(void) |
| 213 | { |
| 214 | CLockObject lock(m_mutex); |
| 215 | return m_menuState; |
| 216 | } |
| 217 | |
| 218 | cec_logical_address CCECBusDevice::GetMyLogicalAddress(void) const |
| 219 | { |
| 220 | return m_processor->GetLogicalAddress(); |
| 221 | } |
| 222 | |
| 223 | uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const |
| 224 | { |
| 225 | return m_processor->GetPhysicalAddress(); |
| 226 | } |
| 227 | |
| 228 | CStdString CCECBusDevice::GetOSDName(bool bUpdate /* = false */) |
| 229 | { |
| 230 | bool bRequestUpdate(false); |
| 231 | { |
| 232 | CLockObject lock(m_mutex); |
| 233 | bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT && |
| 234 | (bUpdate || m_strDeviceName.Equals(ToString(m_iLogicalAddress))) && |
| 235 | m_type != CEC_DEVICE_TYPE_TV); |
| 236 | } |
| 237 | |
| 238 | if (bRequestUpdate) |
| 239 | RequestOSDName(); |
| 240 | |
| 241 | CLockObject lock(m_mutex); |
| 242 | return m_strDeviceName; |
| 243 | } |
| 244 | |
| 245 | bool CCECBusDevice::RequestOSDName(void) |
| 246 | { |
| 247 | bool bReturn(false); |
| 248 | |
| 249 | if (!MyLogicalAddressContains(m_iLogicalAddress) && |
| 250 | !IsUnsupportedFeature(CEC_OPCODE_GIVE_OSD_NAME)) |
| 251 | { |
| 252 | m_handler->MarkBusy(); |
| 253 | CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting OSD name of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); |
| 254 | bReturn = m_handler->TransmitRequestOSDName(GetMyLogicalAddress(), m_iLogicalAddress); |
| 255 | m_handler->MarkReady(); |
| 256 | } |
| 257 | return bReturn; |
| 258 | } |
| 259 | |
| 260 | uint16_t CCECBusDevice::GetPhysicalAddress(bool bUpdate /* = false */) |
| 261 | { |
| 262 | bool bRequestUpdate(false); |
| 263 | { |
| 264 | CLockObject lock(m_mutex); |
| 265 | bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT && |
| 266 | (m_iPhysicalAddress == 0xFFFF || bUpdate)); |
| 267 | } |
| 268 | |
| 269 | if (bRequestUpdate && !RequestPhysicalAddress()) |
| 270 | CLibCEC::AddLog(CEC_LOG_ERROR, "failed to request the physical address"); |
| 271 | |
| 272 | CLockObject lock(m_mutex); |
| 273 | return m_iPhysicalAddress; |
| 274 | } |
| 275 | |
| 276 | bool CCECBusDevice::RequestPhysicalAddress(void) |
| 277 | { |
| 278 | bool bReturn(false); |
| 279 | |
| 280 | if (!MyLogicalAddressContains(m_iLogicalAddress)) |
| 281 | { |
| 282 | m_handler->MarkBusy(); |
| 283 | CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); |
| 284 | bReturn = m_handler->TransmitRequestPhysicalAddress(GetMyLogicalAddress(), m_iLogicalAddress); |
| 285 | m_handler->MarkReady(); |
| 286 | } |
| 287 | return bReturn; |
| 288 | } |
| 289 | |
| 290 | cec_power_status CCECBusDevice::GetPowerStatus(bool bUpdate /* = false */) |
| 291 | { |
| 292 | bool bRequestUpdate(false); |
| 293 | { |
| 294 | CLockObject lock(m_mutex); |
| 295 | bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT && |
| 296 | (bUpdate || m_powerStatus == CEC_POWER_STATUS_UNKNOWN || |
| 297 | m_powerStatus == CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON || |
| 298 | m_powerStatus == CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY || |
| 299 | GetTimeMs() - m_iLastPowerStateUpdate >= CEC_POWER_STATE_REFRESH_TIME)); |
| 300 | } |
| 301 | |
| 302 | if (bRequestUpdate) |
| 303 | RequestPowerStatus(); |
| 304 | |
| 305 | CLockObject lock(m_mutex); |
| 306 | return m_powerStatus; |
| 307 | } |
| 308 | |
| 309 | bool CCECBusDevice::RequestPowerStatus(void) |
| 310 | { |
| 311 | bool bReturn(false); |
| 312 | |
| 313 | if (!MyLogicalAddressContains(m_iLogicalAddress) && |
| 314 | !IsUnsupportedFeature(CEC_OPCODE_GIVE_DEVICE_POWER_STATUS)) |
| 315 | { |
| 316 | m_handler->MarkBusy(); |
| 317 | CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); |
| 318 | bReturn = m_handler->TransmitRequestPowerStatus(GetMyLogicalAddress(), m_iLogicalAddress); |
| 319 | m_handler->MarkReady(); |
| 320 | } |
| 321 | return bReturn; |
| 322 | } |
| 323 | |
| 324 | cec_vendor_id CCECBusDevice::GetVendorId(bool bUpdate /* = false */) |
| 325 | { |
| 326 | bool bRequestUpdate(false); |
| 327 | { |
| 328 | CLockObject lock(m_mutex); |
| 329 | bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT && |
| 330 | (bUpdate || m_vendor == CEC_VENDOR_UNKNOWN)); |
| 331 | } |
| 332 | |
| 333 | if (bRequestUpdate) |
| 334 | RequestVendorId(); |
| 335 | |
| 336 | CLockObject lock(m_mutex); |
| 337 | return m_vendor; |
| 338 | } |
| 339 | |
| 340 | bool CCECBusDevice::RequestVendorId(void) |
| 341 | { |
| 342 | bool bReturn(false); |
| 343 | |
| 344 | if (!MyLogicalAddressContains(m_iLogicalAddress)) |
| 345 | { |
| 346 | m_handler->MarkBusy(); |
| 347 | CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); |
| 348 | bReturn = m_handler->TransmitRequestVendorId(GetMyLogicalAddress(), m_iLogicalAddress); |
| 349 | m_handler->MarkReady(); |
| 350 | |
| 351 | ReplaceHandler(true); |
| 352 | } |
| 353 | return bReturn; |
| 354 | } |
| 355 | |
| 356 | const char *CCECBusDevice::GetVendorName(bool bUpdate /* = false */) |
| 357 | { |
| 358 | return ToString(GetVendorId(bUpdate)); |
| 359 | } |
| 360 | |
| 361 | bool CCECBusDevice::MyLogicalAddressContains(cec_logical_address address) const |
| 362 | { |
| 363 | return m_processor->HasLogicalAddress(address); |
| 364 | } |
| 365 | |
| 366 | bool CCECBusDevice::NeedsPoll(void) |
| 367 | { |
| 368 | bool bSendPoll(false); |
| 369 | switch (m_iLogicalAddress) |
| 370 | { |
| 371 | case CECDEVICE_PLAYBACKDEVICE3: |
| 372 | if (m_processor->m_busDevices[CECDEVICE_PLAYBACKDEVICE2]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) |
| 373 | bSendPoll = true; |
| 374 | break; |
| 375 | case CECDEVICE_PLAYBACKDEVICE2: |
| 376 | if (m_processor->m_busDevices[CECDEVICE_PLAYBACKDEVICE1]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) |
| 377 | bSendPoll = true; |
| 378 | break; |
| 379 | case CECDEVICE_RECORDINGDEVICE3: |
| 380 | if (m_processor->m_busDevices[CECDEVICE_RECORDINGDEVICE2]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) |
| 381 | bSendPoll = true; |
| 382 | break; |
| 383 | case CECDEVICE_RECORDINGDEVICE2: |
| 384 | if (m_processor->m_busDevices[CECDEVICE_RECORDINGDEVICE1]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) |
| 385 | bSendPoll = true; |
| 386 | break; |
| 387 | case CECDEVICE_TUNER4: |
| 388 | if (m_processor->m_busDevices[CECDEVICE_TUNER3]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) |
| 389 | bSendPoll = true; |
| 390 | break; |
| 391 | case CECDEVICE_TUNER3: |
| 392 | if (m_processor->m_busDevices[CECDEVICE_TUNER2]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) |
| 393 | bSendPoll = true; |
| 394 | break; |
| 395 | case CECDEVICE_TUNER2: |
| 396 | if (m_processor->m_busDevices[CECDEVICE_TUNER1]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) |
| 397 | bSendPoll = true; |
| 398 | break; |
| 399 | case CECDEVICE_AUDIOSYSTEM: |
| 400 | case CECDEVICE_PLAYBACKDEVICE1: |
| 401 | case CECDEVICE_RECORDINGDEVICE1: |
| 402 | case CECDEVICE_TUNER1: |
| 403 | case CECDEVICE_TV: |
| 404 | bSendPoll = true; |
| 405 | break; |
| 406 | default: |
| 407 | break; |
| 408 | } |
| 409 | |
| 410 | return bSendPoll; |
| 411 | } |
| 412 | |
| 413 | cec_bus_device_status CCECBusDevice::GetStatus(bool bForcePoll /* = false */) |
| 414 | { |
| 415 | CLockObject lock(m_mutex); |
| 416 | if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC && |
| 417 | (m_deviceStatus == CEC_DEVICE_STATUS_UNKNOWN || bForcePoll)) |
| 418 | { |
| 419 | lock.Unlock(); |
| 420 | bool bPollAcked(false); |
| 421 | if (bForcePoll || NeedsPoll()) |
| 422 | bPollAcked = m_processor->PollDevice(m_iLogicalAddress); |
| 423 | |
| 424 | lock.Lock(); |
| 425 | m_deviceStatus = bPollAcked ? CEC_DEVICE_STATUS_PRESENT : CEC_DEVICE_STATUS_NOT_PRESENT; |
| 426 | } |
| 427 | |
| 428 | return m_deviceStatus; |
| 429 | } |
| 430 | |
| 431 | //@} |
| 432 | |
| 433 | /** @name Setters */ |
| 434 | //@{ |
| 435 | void CCECBusDevice::SetCecVersion(const cec_version newVersion) |
| 436 | { |
| 437 | m_cecVersion = newVersion; |
| 438 | CLibCEC::AddLog(CEC_LOG_DEBUG, "%s (%X): CEC version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(newVersion)); |
| 439 | } |
| 440 | |
| 441 | void CCECBusDevice::SetMenuLanguage(const cec_menu_language &language) |
| 442 | { |
| 443 | CLockObject lock(m_mutex); |
| 444 | if (language.device == m_iLogicalAddress) |
| 445 | { |
| 446 | CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): menu language set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, language.language); |
| 447 | m_menuLanguage = language; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | void CCECBusDevice::SetOSDName(CStdString strName) |
| 452 | { |
| 453 | CLockObject lock(m_mutex); |
| 454 | if (m_strDeviceName != strName) |
| 455 | { |
| 456 | CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): osd name set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, strName.c_str()); |
| 457 | m_strDeviceName = strName; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | void CCECBusDevice::SetMenuState(const cec_menu_state state) |
| 462 | { |
| 463 | CLockObject lock(m_mutex); |
| 464 | if (m_menuState != state) |
| 465 | { |
| 466 | CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): menu state set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_menuState)); |
| 467 | m_menuState = state; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | void CCECBusDevice::SetInactiveSource(void) |
| 472 | { |
| 473 | { |
| 474 | CLockObject lock(m_mutex); |
| 475 | m_bActiveSource = false; |
| 476 | } |
| 477 | |
| 478 | if (MyLogicalAddressContains(m_iLogicalAddress)) |
| 479 | SetPowerStatus(CEC_POWER_STATUS_STANDBY); |
| 480 | } |
| 481 | |
| 482 | void CCECBusDevice::SetActiveSource(void) |
| 483 | { |
| 484 | CLockObject lock(m_mutex); |
| 485 | if (!m_bActiveSource) |
| 486 | CLibCEC::AddLog(CEC_LOG_DEBUG, "making %s (%x) the active source", GetLogicalAddressName(), m_iLogicalAddress); |
| 487 | |
| 488 | for (int iPtr = 0; iPtr < 16; iPtr++) |
| 489 | if (iPtr != m_iLogicalAddress) |
| 490 | m_processor->m_busDevices[iPtr]->SetInactiveSource(); |
| 491 | |
| 492 | m_bActiveSource = true; |
| 493 | m_powerStatus = CEC_POWER_STATUS_ON; |
| 494 | } |
| 495 | |
| 496 | bool CCECBusDevice::TryLogicalAddress(void) |
| 497 | { |
| 498 | CLibCEC::AddLog(CEC_LOG_DEBUG, "trying logical address '%s'", GetLogicalAddressName()); |
| 499 | |
| 500 | m_processor->SetAckMask(0); |
| 501 | if (!TransmitPoll(m_iLogicalAddress)) |
| 502 | { |
| 503 | CLibCEC::AddLog(CEC_LOG_NOTICE, "using logical address '%s'", GetLogicalAddressName()); |
| 504 | SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC); |
| 505 | |
| 506 | return true; |
| 507 | } |
| 508 | |
| 509 | CLibCEC::AddLog(CEC_LOG_DEBUG, "logical address '%s' already taken", GetLogicalAddressName()); |
| 510 | SetDeviceStatus(CEC_DEVICE_STATUS_PRESENT); |
| 511 | return false; |
| 512 | } |
| 513 | |
| 514 | void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus) |
| 515 | { |
| 516 | CLockObject lock(m_mutex); |
| 517 | switch (newStatus) |
| 518 | { |
| 519 | case CEC_DEVICE_STATUS_UNKNOWN: |
| 520 | m_iStreamPath = 0; |
| 521 | m_powerStatus = CEC_POWER_STATUS_UNKNOWN; |
| 522 | m_vendor = CEC_VENDOR_UNKNOWN; |
| 523 | m_menuState = CEC_MENU_STATE_ACTIVATED; |
| 524 | m_bActiveSource = false; |
| 525 | m_iLastActive = 0; |
| 526 | m_cecVersion = CEC_VERSION_UNKNOWN; |
| 527 | m_deviceStatus = newStatus; |
| 528 | break; |
| 529 | case CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC: |
| 530 | m_iStreamPath = 0; |
| 531 | m_powerStatus = CEC_POWER_STATUS_ON; |
| 532 | m_vendor = CEC_VENDOR_UNKNOWN; |
| 533 | m_menuState = CEC_MENU_STATE_ACTIVATED; |
| 534 | m_bActiveSource = false; |
| 535 | m_iLastActive = 0; |
| 536 | m_cecVersion = CEC_VERSION_1_3A; |
| 537 | m_deviceStatus = newStatus; |
| 538 | break; |
| 539 | case CEC_DEVICE_STATUS_PRESENT: |
| 540 | case CEC_DEVICE_STATUS_NOT_PRESENT: |
| 541 | m_deviceStatus = newStatus; |
| 542 | break; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress) |
| 547 | { |
| 548 | CLockObject lock(m_mutex); |
| 549 | if (iNewAddress > 0 && m_iPhysicalAddress != iNewAddress) |
| 550 | { |
| 551 | CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): physical address changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress, iNewAddress); |
| 552 | m_iPhysicalAddress = iNewAddress; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | void CCECBusDevice::SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress /* = 0 */) |
| 557 | { |
| 558 | CLockObject lock(m_mutex); |
| 559 | if (iNewAddress > 0) |
| 560 | { |
| 561 | CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, iOldAddress == 0 ? m_iStreamPath : iOldAddress, iNewAddress); |
| 562 | m_iStreamPath = iNewAddress; |
| 563 | |
| 564 | if (iNewAddress > 0) |
| 565 | { |
| 566 | lock.Unlock(); |
| 567 | SetPowerStatus(CEC_POWER_STATUS_ON); |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus) |
| 573 | { |
| 574 | CLockObject lock(m_mutex); |
| 575 | if (m_powerStatus != powerStatus) |
| 576 | { |
| 577 | m_iLastPowerStateUpdate = GetTimeMs(); |
| 578 | CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): power status changed from '%s' to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_powerStatus), ToString(powerStatus)); |
| 579 | m_powerStatus = powerStatus; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | bool CCECBusDevice::ReplaceHandler(bool bActivateSource /* = true */) |
| 584 | { |
| 585 | CLockObject lock(m_mutex); |
| 586 | CLockObject handlerLock(m_handlerMutex); |
| 587 | |
| 588 | if (m_vendor != m_handler->GetVendorId()) |
| 589 | { |
| 590 | if (CCECCommandHandler::HasSpecificHandler(m_vendor)) |
| 591 | { |
| 592 | CStdString strLog; |
| 593 | if (m_handler->InUse()) |
| 594 | { |
| 595 | CLibCEC::AddLog(CEC_LOG_DEBUG, "handler for device '%s' (%x) is being used. not replacing the command handler", GetLogicalAddressName(), GetLogicalAddress()); |
| 596 | return false; |
| 597 | } |
| 598 | |
| 599 | CLibCEC::AddLog(CEC_LOG_DEBUG, "replacing the command handler for device '%s' (%x)", GetLogicalAddressName(), GetLogicalAddress()); |
| 600 | delete m_handler; |
| 601 | |
| 602 | switch (m_vendor) |
| 603 | { |
| 604 | case CEC_VENDOR_SAMSUNG: |
| 605 | m_handler = new CANCommandHandler(this); |
| 606 | break; |
| 607 | case CEC_VENDOR_LG: |
| 608 | m_handler = new CSLCommandHandler(this); |
| 609 | break; |
| 610 | case CEC_VENDOR_PANASONIC: |
| 611 | m_handler = new CVLCommandHandler(this); |
| 612 | break; |
| 613 | default: |
| 614 | m_handler = new CCECCommandHandler(this); |
| 615 | break; |
| 616 | } |
| 617 | |
| 618 | m_handler->SetVendorId(m_vendor); |
| 619 | m_handler->InitHandler(); |
| 620 | |
| 621 | if (bActivateSource && m_processor->GetLogicalAddresses().IsSet(m_iLogicalAddress) && m_processor->IsInitialised() && IsActiveSource()) |
| 622 | m_handler->ActivateSource(); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | return true; |
| 627 | } |
| 628 | |
| 629 | bool CCECBusDevice::SetVendorId(uint64_t iVendorId) |
| 630 | { |
| 631 | bool bVendorChanged(false); |
| 632 | |
| 633 | { |
| 634 | CLockObject lock(m_mutex); |
| 635 | bVendorChanged = (m_vendor != (cec_vendor_id)iVendorId); |
| 636 | m_vendor = (cec_vendor_id)iVendorId; |
| 637 | } |
| 638 | |
| 639 | CLibCEC::AddLog(CEC_LOG_DEBUG, "%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_vendor), m_vendor); |
| 640 | |
| 641 | return bVendorChanged; |
| 642 | } |
| 643 | //@} |
| 644 | |
| 645 | /** @name Transmit methods */ |
| 646 | //@{ |
| 647 | bool CCECBusDevice::TransmitActiveSource(void) |
| 648 | { |
| 649 | bool bSendActiveSource(false); |
| 650 | |
| 651 | { |
| 652 | CLockObject lock(m_mutex); |
| 653 | if (m_powerStatus != CEC_POWER_STATUS_ON) |
| 654 | CLibCEC::AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress); |
| 655 | else if (m_bActiveSource) |
| 656 | { |
| 657 | CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): active source (%4x)", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress); |
| 658 | bSendActiveSource = true; |
| 659 | } |
| 660 | else |
| 661 | CLibCEC::AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress); |
| 662 | } |
| 663 | |
| 664 | if (bSendActiveSource) |
| 665 | { |
| 666 | m_handler->TransmitImageViewOn(m_iLogicalAddress, CECDEVICE_TV); |
| 667 | m_handler->TransmitActiveSource(m_iLogicalAddress, m_iPhysicalAddress); |
| 668 | return true; |
| 669 | } |
| 670 | |
| 671 | return false; |
| 672 | } |
| 673 | |
| 674 | bool CCECBusDevice::TransmitCECVersion(cec_logical_address dest) |
| 675 | { |
| 676 | cec_version version; |
| 677 | { |
| 678 | CLockObject lock(m_mutex); |
| 679 | CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): cec version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_cecVersion)); |
| 680 | version = m_cecVersion; |
| 681 | } |
| 682 | |
| 683 | return m_handler->TransmitCECVersion(m_iLogicalAddress, dest, version); |
| 684 | } |
| 685 | |
| 686 | bool CCECBusDevice::TransmitInactiveSource(void) |
| 687 | { |
| 688 | uint16_t iPhysicalAddress; |
| 689 | { |
| 690 | CLockObject lock(m_mutex); |
| 691 | CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): inactive source", GetLogicalAddressName(), m_iLogicalAddress); |
| 692 | iPhysicalAddress = m_iPhysicalAddress; |
| 693 | } |
| 694 | |
| 695 | return m_handler->TransmitInactiveSource(m_iLogicalAddress, iPhysicalAddress); |
| 696 | } |
| 697 | |
| 698 | bool CCECBusDevice::TransmitMenuState(cec_logical_address dest) |
| 699 | { |
| 700 | cec_menu_state menuState; |
| 701 | { |
| 702 | CLockObject lock(m_mutex); |
| 703 | CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): menu state '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_menuState)); |
| 704 | menuState = m_menuState; |
| 705 | } |
| 706 | |
| 707 | return m_handler->TransmitMenuState(m_iLogicalAddress, dest, menuState); |
| 708 | } |
| 709 | |
| 710 | bool CCECBusDevice::TransmitOSDName(cec_logical_address dest) |
| 711 | { |
| 712 | CStdString strDeviceName; |
| 713 | { |
| 714 | CLockObject lock(m_mutex); |
| 715 | CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, m_strDeviceName.c_str()); |
| 716 | strDeviceName = m_strDeviceName; |
| 717 | } |
| 718 | |
| 719 | return m_handler->TransmitOSDName(m_iLogicalAddress, dest, strDeviceName); |
| 720 | } |
| 721 | |
| 722 | bool CCECBusDevice::TransmitOSDString(cec_logical_address dest, cec_display_control duration, const char *strMessage) |
| 723 | { |
| 724 | if (!IsUnsupportedFeature(CEC_OPCODE_SET_OSD_STRING)) |
| 725 | { |
| 726 | CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): display OSD message '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, strMessage); |
| 727 | return m_handler->TransmitOSDString(m_iLogicalAddress, dest, duration, strMessage); |
| 728 | } |
| 729 | return false; |
| 730 | } |
| 731 | |
| 732 | bool CCECBusDevice::TransmitPhysicalAddress(void) |
| 733 | { |
| 734 | uint16_t iPhysicalAddress; |
| 735 | cec_device_type type; |
| 736 | { |
| 737 | CLockObject lock(m_mutex); |
| 738 | if (m_iPhysicalAddress == 0xffff) |
| 739 | return false; |
| 740 | |
| 741 | CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress); |
| 742 | iPhysicalAddress = m_iPhysicalAddress; |
| 743 | type = m_type; |
| 744 | } |
| 745 | |
| 746 | return m_handler->TransmitPhysicalAddress(m_iLogicalAddress, iPhysicalAddress, type); |
| 747 | } |
| 748 | |
| 749 | bool CCECBusDevice::TransmitPoll(cec_logical_address dest) |
| 750 | { |
| 751 | bool bReturn(false); |
| 752 | if (dest == CECDEVICE_UNKNOWN) |
| 753 | dest = m_iLogicalAddress; |
| 754 | |
| 755 | CCECBusDevice *destDevice = m_processor->m_busDevices[dest]; |
| 756 | if (destDevice->m_deviceStatus == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC) |
| 757 | return bReturn; |
| 758 | |
| 759 | CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest); |
| 760 | bReturn = m_handler->TransmitPoll(m_iLogicalAddress, dest); |
| 761 | CLibCEC::AddLog(CEC_LOG_DEBUG, bReturn ? ">> POLL sent" : ">> POLL not sent"); |
| 762 | |
| 763 | CLockObject lock(m_mutex); |
| 764 | if (bReturn) |
| 765 | { |
| 766 | m_iLastActive = GetTimeMs(); |
| 767 | destDevice->m_deviceStatus = CEC_DEVICE_STATUS_PRESENT; |
| 768 | } |
| 769 | else |
| 770 | destDevice->m_deviceStatus = CEC_DEVICE_STATUS_NOT_PRESENT; |
| 771 | |
| 772 | return bReturn; |
| 773 | } |
| 774 | |
| 775 | bool CCECBusDevice::TransmitPowerState(cec_logical_address dest) |
| 776 | { |
| 777 | cec_power_status state; |
| 778 | { |
| 779 | CLockObject lock(m_mutex); |
| 780 | CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_powerStatus)); |
| 781 | state = m_powerStatus; |
| 782 | } |
| 783 | |
| 784 | return m_handler->TransmitPowerState(m_iLogicalAddress, dest, state); |
| 785 | } |
| 786 | |
| 787 | bool CCECBusDevice::TransmitVendorID(cec_logical_address dest, bool bSendAbort /* = true */) |
| 788 | { |
| 789 | uint64_t iVendorId; |
| 790 | { |
| 791 | CLockObject lock(m_mutex); |
| 792 | iVendorId = (uint64_t)m_vendor; |
| 793 | } |
| 794 | |
| 795 | if (iVendorId == CEC_VENDOR_UNKNOWN) |
| 796 | { |
| 797 | if (bSendAbort) |
| 798 | { |
| 799 | CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest); |
| 800 | m_processor->TransmitAbort(dest, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID); |
| 801 | } |
| 802 | return false; |
| 803 | } |
| 804 | else |
| 805 | { |
| 806 | CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): vendor id %s (%x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString((cec_vendor_id)iVendorId), iVendorId); |
| 807 | return m_handler->TransmitVendorID(m_iLogicalAddress, iVendorId); |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | bool CCECBusDevice::TransmitKeypress(cec_user_control_code key, bool bWait /* = true */) |
| 812 | { |
| 813 | return m_handler->TransmitKeypress(m_processor->GetLogicalAddress(), m_iLogicalAddress, key, bWait); |
| 814 | } |
| 815 | |
| 816 | bool CCECBusDevice::TransmitKeyRelease(bool bWait /* = true */) |
| 817 | { |
| 818 | return m_handler->TransmitKeyRelease(m_processor->GetLogicalAddress(), m_iLogicalAddress, bWait); |
| 819 | } |
| 820 | |
| 821 | bool CCECBusDevice::IsUnsupportedFeature(cec_opcode opcode) const |
| 822 | { |
| 823 | return m_unsupportedFeatures.find(opcode) != m_unsupportedFeatures.end(); |
| 824 | } |
| 825 | |
| 826 | void CCECBusDevice::SetUnsupportedFeature(cec_opcode opcode) |
| 827 | { |
| 828 | m_unsupportedFeatures.insert(opcode); |
| 829 | } |
| 830 | |
| 831 | bool CCECBusDevice::ActivateSource(void) |
| 832 | { |
| 833 | CLockObject lock(m_mutex); |
| 834 | return m_handler->ActivateSource(); |
| 835 | } |
| 836 | |
| 837 | void CCECBusDevice::HandlePoll(cec_logical_address iDestination) |
| 838 | { |
| 839 | CLockObject lock(m_mutex); |
| 840 | CLibCEC::AddLog(CEC_LOG_DEBUG, "<< POLL: %s (%x) -> %s (%x)", ToString(m_iLogicalAddress), m_iLogicalAddress, ToString(iDestination), iDestination); |
| 841 | m_bAwaitingReceiveFailed = true; |
| 842 | } |
| 843 | |
| 844 | bool CCECBusDevice::HandleReceiveFailed(void) |
| 845 | { |
| 846 | CLockObject lock(m_handlerMutex); |
| 847 | bool bReturn = m_bAwaitingReceiveFailed; |
| 848 | m_bAwaitingReceiveFailed = false; |
| 849 | return bReturn; |
| 850 | } |
| 851 | |
| 852 | //@} |