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