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