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