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