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