| 1 | /* |
| 2 | * This file is part of the libCEC(R) library. |
| 3 | * |
| 4 | * libCEC(R) is Copyright (C) 2011-2012 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 "CECProcessor.h" |
| 34 | |
| 35 | #include "adapter/USBCECAdapterCommunication.h" |
| 36 | #include "devices/CECBusDevice.h" |
| 37 | #include "devices/CECAudioSystem.h" |
| 38 | #include "devices/CECPlaybackDevice.h" |
| 39 | #include "devices/CECRecordingDevice.h" |
| 40 | #include "devices/CECTuner.h" |
| 41 | #include "devices/CECTV.h" |
| 42 | #include "implementations/CECCommandHandler.h" |
| 43 | #include "LibCEC.h" |
| 44 | #include "platform/util/timeutils.h" |
| 45 | |
| 46 | using namespace CEC; |
| 47 | using namespace std; |
| 48 | using namespace PLATFORM; |
| 49 | |
| 50 | CCECProcessor::CCECProcessor(CLibCEC *controller, const libcec_configuration *configuration) : |
| 51 | m_bInitialised(false), |
| 52 | m_iPhysicalAddress(configuration->iPhysicalAddress), |
| 53 | m_iHDMIPort(configuration->iHDMIPort), |
| 54 | m_iBaseDevice(configuration->baseDevice), |
| 55 | m_strDeviceName(configuration->strDeviceName), |
| 56 | m_types(configuration->deviceTypes), |
| 57 | m_communication(NULL), |
| 58 | m_controller(controller), |
| 59 | m_bMonitor(false), |
| 60 | m_iStandardLineTimeout(3), |
| 61 | m_iRetryLineTimeout(3), |
| 62 | m_iLastTransmission(0), |
| 63 | m_clientVersion(configuration->clientVersion) |
| 64 | { |
| 65 | m_logicalAddresses.Clear(); |
| 66 | CreateBusDevices(); |
| 67 | } |
| 68 | |
| 69 | CCECProcessor::CCECProcessor(CLibCEC *controller, const char *strDeviceName, const cec_device_type_list &types, uint16_t iPhysicalAddress, cec_client_version clientVersion) : |
| 70 | m_bInitialised(false), |
| 71 | m_iPhysicalAddress(iPhysicalAddress), |
| 72 | m_iHDMIPort(CEC_DEFAULT_HDMI_PORT), |
| 73 | m_iBaseDevice((cec_logical_address)CEC_DEFAULT_BASE_DEVICE), |
| 74 | m_strDeviceName(strDeviceName), |
| 75 | m_types(types), |
| 76 | m_communication(NULL), |
| 77 | m_controller(controller), |
| 78 | m_bMonitor(false), |
| 79 | m_iStandardLineTimeout(3), |
| 80 | m_iRetryLineTimeout(3), |
| 81 | m_iLastTransmission(0), |
| 82 | m_clientVersion(clientVersion) |
| 83 | { |
| 84 | m_logicalAddresses.Clear(); |
| 85 | CreateBusDevices(); |
| 86 | } |
| 87 | |
| 88 | void CCECProcessor::CreateBusDevices(void) |
| 89 | { |
| 90 | for (int iPtr = 0; iPtr < 16; iPtr++) |
| 91 | { |
| 92 | switch(iPtr) |
| 93 | { |
| 94 | case CECDEVICE_AUDIOSYSTEM: |
| 95 | m_busDevices[iPtr] = new CCECAudioSystem(this, (cec_logical_address) iPtr, 0xFFFF); |
| 96 | break; |
| 97 | case CECDEVICE_PLAYBACKDEVICE1: |
| 98 | case CECDEVICE_PLAYBACKDEVICE2: |
| 99 | case CECDEVICE_PLAYBACKDEVICE3: |
| 100 | m_busDevices[iPtr] = new CCECPlaybackDevice(this, (cec_logical_address) iPtr, 0xFFFF); |
| 101 | break; |
| 102 | case CECDEVICE_RECORDINGDEVICE1: |
| 103 | case CECDEVICE_RECORDINGDEVICE2: |
| 104 | case CECDEVICE_RECORDINGDEVICE3: |
| 105 | m_busDevices[iPtr] = new CCECRecordingDevice(this, (cec_logical_address) iPtr, 0xFFFF); |
| 106 | break; |
| 107 | case CECDEVICE_TUNER1: |
| 108 | case CECDEVICE_TUNER2: |
| 109 | case CECDEVICE_TUNER3: |
| 110 | case CECDEVICE_TUNER4: |
| 111 | m_busDevices[iPtr] = new CCECTuner(this, (cec_logical_address) iPtr, 0xFFFF); |
| 112 | break; |
| 113 | case CECDEVICE_TV: |
| 114 | m_busDevices[iPtr] = new CCECTV(this, (cec_logical_address) iPtr, 0); |
| 115 | break; |
| 116 | default: |
| 117 | m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, 0xFFFF); |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | CCECProcessor::~CCECProcessor(void) |
| 124 | { |
| 125 | Close(); |
| 126 | |
| 127 | for (unsigned int iPtr = 0; iPtr < 16; iPtr++) |
| 128 | delete m_busDevices[iPtr]; |
| 129 | } |
| 130 | |
| 131 | void CCECProcessor::Close(void) |
| 132 | { |
| 133 | StopThread(); |
| 134 | |
| 135 | CLockObject lock(m_mutex); |
| 136 | if (m_communication) |
| 137 | { |
| 138 | m_communication->Close(); |
| 139 | delete m_communication; |
| 140 | m_communication = NULL; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | bool CCECProcessor::OpenConnection(const char *strPort, uint16_t iBaudRate, uint32_t iTimeoutMs) |
| 145 | { |
| 146 | bool bReturn(false); |
| 147 | CLockObject lock(m_mutex); |
| 148 | if (m_communication) |
| 149 | { |
| 150 | CLibCEC::AddLog(CEC_LOG_WARNING, "existing connection handler found, deleting it"); |
| 151 | m_communication->Close(); |
| 152 | delete m_communication; |
| 153 | } |
| 154 | |
| 155 | m_communication = new CUSBCECAdapterCommunication(this, strPort, iBaudRate); |
| 156 | |
| 157 | /* check for an already opened connection */ |
| 158 | if (m_communication->IsOpen()) |
| 159 | { |
| 160 | CLibCEC::AddLog(CEC_LOG_ERROR, "connection already opened"); |
| 161 | return bReturn; |
| 162 | } |
| 163 | |
| 164 | uint64_t iNow = GetTimeMs(); |
| 165 | uint64_t iTarget = iTimeoutMs > 0 ? iNow + iTimeoutMs : iNow + CEC_DEFAULT_TRANSMIT_WAIT; |
| 166 | |
| 167 | /* open a new connection */ |
| 168 | unsigned iConnectTry(0); |
| 169 | while (iNow < iTarget && (bReturn = m_communication->Open(this, iTimeoutMs)) == false) |
| 170 | { |
| 171 | CLibCEC::AddLog(CEC_LOG_ERROR, "could not open a connection (try %d)", ++iConnectTry); |
| 172 | Sleep(500); |
| 173 | iNow = GetTimeMs(); |
| 174 | } |
| 175 | |
| 176 | if (bReturn) |
| 177 | CLibCEC::AddLog(CEC_LOG_NOTICE, "connected to the CEC adapter. firmware version = %d, client version = %s", m_communication->GetFirmwareVersion(), ToString(m_clientVersion)); |
| 178 | |
| 179 | return bReturn; |
| 180 | } |
| 181 | |
| 182 | bool CCECProcessor::IsInitialised(void) |
| 183 | { |
| 184 | CLockObject lock(m_mutex); |
| 185 | return m_bInitialised; |
| 186 | } |
| 187 | |
| 188 | void CCECProcessor::SetInitialised(bool bSetTo /* = true */) |
| 189 | { |
| 190 | CLockObject lock(m_mutex); |
| 191 | m_bInitialised = bSetTo; |
| 192 | } |
| 193 | |
| 194 | bool CCECProcessor::Initialise(void) |
| 195 | { |
| 196 | bool bReturn(false); |
| 197 | { |
| 198 | CLockObject lock(m_mutex); |
| 199 | if (!m_logicalAddresses.IsEmpty()) |
| 200 | m_logicalAddresses.Clear(); |
| 201 | |
| 202 | if (!FindLogicalAddresses()) |
| 203 | { |
| 204 | CLibCEC::AddLog(CEC_LOG_ERROR, "could not detect our logical addresses"); |
| 205 | return bReturn; |
| 206 | } |
| 207 | |
| 208 | /* only set our OSD name for the primary device */ |
| 209 | m_busDevices[m_logicalAddresses.primary]->m_strDeviceName = m_strDeviceName; |
| 210 | } |
| 211 | |
| 212 | /* get the vendor id from the TV, so we are using the correct handler */ |
| 213 | m_busDevices[CECDEVICE_TV]->RequestVendorId(); |
| 214 | ReplaceHandlers(); |
| 215 | |
| 216 | if (m_iPhysicalAddress != 0) |
| 217 | { |
| 218 | m_busDevices[m_logicalAddresses.primary]->m_iPhysicalAddress = m_iPhysicalAddress; |
| 219 | if ((bReturn = m_busDevices[m_logicalAddresses.primary]->TransmitPhysicalAddress()) == false) |
| 220 | CLibCEC::AddLog(CEC_LOG_ERROR, "unable to set the physical address to %4x", m_iPhysicalAddress); |
| 221 | } |
| 222 | else if (m_iPhysicalAddress == 0 && (bReturn = SetHDMIPort(m_iBaseDevice, m_iHDMIPort, true)) == false) |
| 223 | CLibCEC::AddLog(CEC_LOG_ERROR, "unable to set HDMI port %d on %s (%x)", m_iHDMIPort, ToString(m_iBaseDevice), (uint8_t)m_iBaseDevice); |
| 224 | |
| 225 | SetInitialised(bReturn); |
| 226 | |
| 227 | return bReturn; |
| 228 | } |
| 229 | |
| 230 | bool CCECProcessor::Start(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */) |
| 231 | { |
| 232 | bool bReturn(false); |
| 233 | |
| 234 | { |
| 235 | CLockObject lock(m_mutex); |
| 236 | if (!OpenConnection(strPort, iBaudRate, iTimeoutMs)) |
| 237 | return bReturn; |
| 238 | |
| 239 | /* create the processor thread */ |
| 240 | if (!CreateThread()) |
| 241 | { |
| 242 | CLibCEC::AddLog(CEC_LOG_ERROR, "could not create a processor thread"); |
| 243 | return bReturn; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | if ((bReturn = Initialise()) == false) |
| 248 | { |
| 249 | CLibCEC::AddLog(CEC_LOG_ERROR, "could not create a processor thread"); |
| 250 | StopThread(true); |
| 251 | } |
| 252 | else |
| 253 | { |
| 254 | CLibCEC::AddLog(CEC_LOG_DEBUG, "processor thread started"); |
| 255 | } |
| 256 | |
| 257 | return bReturn; |
| 258 | } |
| 259 | |
| 260 | bool CCECProcessor::TryLogicalAddress(cec_logical_address address) |
| 261 | { |
| 262 | if (m_busDevices[address]->TryLogicalAddress()) |
| 263 | { |
| 264 | m_logicalAddresses.Set(address); |
| 265 | return true; |
| 266 | } |
| 267 | |
| 268 | return false; |
| 269 | } |
| 270 | |
| 271 | bool CCECProcessor::FindLogicalAddressRecordingDevice(void) |
| 272 | { |
| 273 | CLibCEC::AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'recording device'"); |
| 274 | return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1) || |
| 275 | TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2) || |
| 276 | TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3); |
| 277 | } |
| 278 | |
| 279 | bool CCECProcessor::FindLogicalAddressTuner(void) |
| 280 | { |
| 281 | CLibCEC::AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'tuner'"); |
| 282 | return TryLogicalAddress(CECDEVICE_TUNER1) || |
| 283 | TryLogicalAddress(CECDEVICE_TUNER2) || |
| 284 | TryLogicalAddress(CECDEVICE_TUNER3) || |
| 285 | TryLogicalAddress(CECDEVICE_TUNER4); |
| 286 | } |
| 287 | |
| 288 | bool CCECProcessor::FindLogicalAddressPlaybackDevice(void) |
| 289 | { |
| 290 | CLibCEC::AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'playback device'"); |
| 291 | return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1) || |
| 292 | TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2) || |
| 293 | TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3); |
| 294 | } |
| 295 | |
| 296 | bool CCECProcessor::FindLogicalAddressAudioSystem(void) |
| 297 | { |
| 298 | CLibCEC::AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'audio'"); |
| 299 | return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM); |
| 300 | } |
| 301 | |
| 302 | bool CCECProcessor::ChangeDeviceType(cec_device_type from, cec_device_type to) |
| 303 | { |
| 304 | bool bChanged(false); |
| 305 | |
| 306 | CLibCEC::AddLog(CEC_LOG_NOTICE, "changing device type '%s' into '%s'", ToString(from), ToString(to)); |
| 307 | |
| 308 | CLockObject lock(m_mutex); |
| 309 | CCECBusDevice *previousDevice = GetDeviceByType(from); |
| 310 | m_logicalAddresses.primary = CECDEVICE_UNKNOWN; |
| 311 | |
| 312 | for (unsigned int iPtr = 0; iPtr < 5; iPtr++) |
| 313 | { |
| 314 | if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RESERVED) |
| 315 | continue; |
| 316 | |
| 317 | if (m_types.types[iPtr] == from) |
| 318 | { |
| 319 | bChanged = true; |
| 320 | m_types.types[iPtr] = to; |
| 321 | } |
| 322 | else if (m_types.types[iPtr] == to && bChanged) |
| 323 | { |
| 324 | m_types.types[iPtr] = CEC_DEVICE_TYPE_RESERVED; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | if (bChanged) |
| 329 | { |
| 330 | FindLogicalAddresses(); |
| 331 | |
| 332 | CCECBusDevice *newDevice = GetDeviceByType(to); |
| 333 | if (previousDevice && newDevice) |
| 334 | { |
| 335 | newDevice->SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC); |
| 336 | previousDevice->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT); |
| 337 | |
| 338 | newDevice->SetCecVersion(previousDevice->GetCecVersion(false)); |
| 339 | previousDevice->SetCecVersion(CEC_VERSION_UNKNOWN); |
| 340 | |
| 341 | newDevice->SetMenuLanguage(previousDevice->GetMenuLanguage(false)); |
| 342 | cec_menu_language lang; |
| 343 | lang.device = previousDevice->GetLogicalAddress(); |
| 344 | for (unsigned int iPtr = 0; iPtr < 4; iPtr++) |
| 345 | lang.language[iPtr] = '?'; |
| 346 | lang.language[3] = 0; |
| 347 | previousDevice->SetMenuLanguage(lang); |
| 348 | |
| 349 | newDevice->SetMenuState(previousDevice->GetMenuState()); |
| 350 | previousDevice->SetMenuState(CEC_MENU_STATE_DEACTIVATED); |
| 351 | |
| 352 | newDevice->SetOSDName(previousDevice->GetOSDName(false)); |
| 353 | previousDevice->SetOSDName(ToString(previousDevice->GetLogicalAddress())); |
| 354 | |
| 355 | newDevice->SetPhysicalAddress(previousDevice->GetPhysicalAddress(false)); |
| 356 | previousDevice->SetPhysicalAddress(0xFFFF); |
| 357 | |
| 358 | newDevice->SetPowerStatus(previousDevice->GetPowerStatus(false)); |
| 359 | previousDevice->SetPowerStatus(CEC_POWER_STATUS_UNKNOWN); |
| 360 | |
| 361 | newDevice->SetVendorId(previousDevice->GetVendorId(false)); |
| 362 | previousDevice->SetVendorId(CEC_VENDOR_UNKNOWN); |
| 363 | |
| 364 | if ((from == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || from == CEC_DEVICE_TYPE_RECORDING_DEVICE) && |
| 365 | (to == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || to == CEC_DEVICE_TYPE_RECORDING_DEVICE)) |
| 366 | { |
| 367 | ((CCECPlaybackDevice *) newDevice)->SetDeckControlMode(((CCECPlaybackDevice *) previousDevice)->GetDeckControlMode()); |
| 368 | ((CCECPlaybackDevice *) previousDevice)->SetDeckControlMode(CEC_DECK_CONTROL_MODE_STOP); |
| 369 | |
| 370 | ((CCECPlaybackDevice *) newDevice)->SetDeckStatus(((CCECPlaybackDevice *) previousDevice)->GetDeckStatus()); |
| 371 | ((CCECPlaybackDevice *) previousDevice)->SetDeckStatus(CEC_DECK_INFO_STOP); |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | return true; |
| 377 | } |
| 378 | |
| 379 | bool CCECProcessor::FindLogicalAddresses(void) |
| 380 | { |
| 381 | bool bReturn(true); |
| 382 | m_logicalAddresses.Clear(); |
| 383 | |
| 384 | for (unsigned int iPtr = 0; iPtr < 5; iPtr++) |
| 385 | { |
| 386 | if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RESERVED) |
| 387 | continue; |
| 388 | |
| 389 | CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - device %d: type %d", __FUNCTION__, iPtr, m_types.types[iPtr]); |
| 390 | |
| 391 | if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RECORDING_DEVICE) |
| 392 | bReturn &= FindLogicalAddressRecordingDevice(); |
| 393 | if (m_types.types[iPtr] == CEC_DEVICE_TYPE_TUNER) |
| 394 | bReturn &= FindLogicalAddressTuner(); |
| 395 | if (m_types.types[iPtr] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE) |
| 396 | bReturn &= FindLogicalAddressPlaybackDevice(); |
| 397 | if (m_types.types[iPtr] == CEC_DEVICE_TYPE_AUDIO_SYSTEM) |
| 398 | bReturn &= FindLogicalAddressAudioSystem(); |
| 399 | } |
| 400 | |
| 401 | if (bReturn) |
| 402 | SetAckMask(m_logicalAddresses.AckMask()); |
| 403 | |
| 404 | return bReturn; |
| 405 | } |
| 406 | |
| 407 | void CCECProcessor::ReplaceHandlers(void) |
| 408 | { |
| 409 | CLockObject lock(m_mutex); |
| 410 | if (!IsInitialised()) |
| 411 | return; |
| 412 | for (uint8_t iPtr = 0; iPtr <= CECDEVICE_PLAYBACKDEVICE3; iPtr++) |
| 413 | m_busDevices[iPtr]->ReplaceHandler(m_bInitialised); |
| 414 | } |
| 415 | |
| 416 | bool CCECProcessor::OnCommandReceived(const cec_command &command) |
| 417 | { |
| 418 | m_commandBuffer.Push(command); |
| 419 | return true; |
| 420 | } |
| 421 | |
| 422 | void *CCECProcessor::Process(void) |
| 423 | { |
| 424 | cec_command command; |
| 425 | CLibCEC::AddLog(CEC_LOG_DEBUG, "processor thread started"); |
| 426 | |
| 427 | while (!IsStopped() && m_communication->IsOpen()) |
| 428 | { |
| 429 | ReplaceHandlers(); |
| 430 | if (m_commandBuffer.Pop(command)) |
| 431 | ParseCommand(command); |
| 432 | |
| 433 | m_controller->CheckKeypressTimeout(); |
| 434 | Sleep(5); |
| 435 | } |
| 436 | |
| 437 | return NULL; |
| 438 | } |
| 439 | |
| 440 | bool CCECProcessor::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RESERVED */) |
| 441 | { |
| 442 | bool bReturn(false); |
| 443 | |
| 444 | if (!IsRunning()) |
| 445 | return bReturn; |
| 446 | |
| 447 | cec_logical_address addr = m_logicalAddresses.primary; |
| 448 | |
| 449 | if (type != CEC_DEVICE_TYPE_RESERVED) |
| 450 | { |
| 451 | for (uint8_t iPtr = 0; iPtr <= 11; iPtr++) |
| 452 | { |
| 453 | if (m_logicalAddresses[iPtr] && m_busDevices[iPtr]->m_type == type) |
| 454 | { |
| 455 | addr = (cec_logical_address) iPtr; |
| 456 | break; |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | m_busDevices[addr]->SetActiveSource(); |
| 462 | if (m_busDevices[addr]->GetPhysicalAddress(false) != 0xFFFF) |
| 463 | { |
| 464 | bReturn = m_busDevices[addr]->TransmitActiveSource(); |
| 465 | |
| 466 | if (bReturn) |
| 467 | { |
| 468 | m_busDevices[addr]->SetMenuState(CEC_MENU_STATE_ACTIVATED); |
| 469 | m_busDevices[addr]->TransmitMenuState(CECDEVICE_TV); |
| 470 | } |
| 471 | |
| 472 | if (bReturn && (m_busDevices[addr]->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || |
| 473 | m_busDevices[addr]->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE) && |
| 474 | m_busDevices[addr]->GetHandler()->SendDeckStatusUpdateOnActiveSource()) |
| 475 | { |
| 476 | bReturn = ((CCECPlaybackDevice *)m_busDevices[addr])->TransmitDeckStatus(CECDEVICE_TV); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | return bReturn; |
| 481 | } |
| 482 | |
| 483 | bool CCECProcessor::SetActiveSource(uint16_t iStreamPath) |
| 484 | { |
| 485 | bool bReturn(false); |
| 486 | |
| 487 | CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamPath); |
| 488 | if (device) |
| 489 | { |
| 490 | device->SetActiveSource(); |
| 491 | bReturn = true; |
| 492 | } |
| 493 | |
| 494 | return bReturn; |
| 495 | } |
| 496 | |
| 497 | void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout) |
| 498 | { |
| 499 | CLockObject lock(m_mutex); |
| 500 | m_iStandardLineTimeout = iTimeout; |
| 501 | } |
| 502 | |
| 503 | void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout) |
| 504 | { |
| 505 | CLockObject lock(m_mutex); |
| 506 | m_iRetryLineTimeout = iTimeout; |
| 507 | } |
| 508 | |
| 509 | bool CCECProcessor::SetActiveView(void) |
| 510 | { |
| 511 | return SetActiveSource(m_types.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED : m_types[0]); |
| 512 | } |
| 513 | |
| 514 | bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode, bool bSendUpdate /* = true */) |
| 515 | { |
| 516 | bool bReturn(false); |
| 517 | |
| 518 | CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE); |
| 519 | if (device) |
| 520 | { |
| 521 | ((CCECPlaybackDevice *) device)->SetDeckControlMode(mode); |
| 522 | if (bSendUpdate) |
| 523 | ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV); |
| 524 | bReturn = true; |
| 525 | } |
| 526 | |
| 527 | return bReturn; |
| 528 | } |
| 529 | |
| 530 | bool CCECProcessor::SetDeckInfo(cec_deck_info info, bool bSendUpdate /* = true */) |
| 531 | { |
| 532 | bool bReturn(false); |
| 533 | |
| 534 | CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE); |
| 535 | if (device) |
| 536 | { |
| 537 | ((CCECPlaybackDevice *) device)->SetDeckStatus(info); |
| 538 | if (bSendUpdate) |
| 539 | ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV); |
| 540 | bReturn = true; |
| 541 | } |
| 542 | |
| 543 | return bReturn; |
| 544 | } |
| 545 | |
| 546 | bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice, uint8_t iPort, bool bForce /* = false */) |
| 547 | { |
| 548 | bool bReturn(false); |
| 549 | CLockObject lock(m_mutex); |
| 550 | |
| 551 | m_iBaseDevice = iBaseDevice; |
| 552 | m_iHDMIPort = iPort; |
| 553 | if (!IsRunning() && !bForce) |
| 554 | return true; |
| 555 | |
| 556 | CLibCEC::AddLog(CEC_LOG_DEBUG, "setting HDMI port to %d on device %s (%d)", iPort, ToString(iBaseDevice), (int)iBaseDevice); |
| 557 | |
| 558 | uint16_t iPhysicalAddress(0); |
| 559 | if (iBaseDevice > CECDEVICE_TV) |
| 560 | { |
| 561 | lock.Unlock(); |
| 562 | iPhysicalAddress = m_busDevices[iBaseDevice]->GetPhysicalAddress(); |
| 563 | lock.Lock(); |
| 564 | } |
| 565 | |
| 566 | if (iPhysicalAddress < 0xffff) |
| 567 | { |
| 568 | if (iPhysicalAddress == 0) |
| 569 | iPhysicalAddress += 0x1000 * iPort; |
| 570 | else if (iPhysicalAddress % 0x1000 == 0) |
| 571 | iPhysicalAddress += 0x100 * iPort; |
| 572 | else if (iPhysicalAddress % 0x100 == 0) |
| 573 | iPhysicalAddress += 0x10 * iPort; |
| 574 | else if (iPhysicalAddress % 0x10 == 0) |
| 575 | iPhysicalAddress += iPort; |
| 576 | |
| 577 | bReturn = true; |
| 578 | } |
| 579 | |
| 580 | if (!bReturn) |
| 581 | CLibCEC::AddLog(CEC_LOG_ERROR, "failed to set the physical address"); |
| 582 | else |
| 583 | { |
| 584 | lock.Unlock(); |
| 585 | SetPhysicalAddress(iPhysicalAddress); |
| 586 | } |
| 587 | |
| 588 | return bReturn; |
| 589 | } |
| 590 | |
| 591 | bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress) |
| 592 | { |
| 593 | for (unsigned int iPtr = 0; iPtr < 15; iPtr++) |
| 594 | { |
| 595 | if (m_busDevices[iPtr]->GetPhysicalAddress(false) == iPhysicalAddress) |
| 596 | return true; |
| 597 | } |
| 598 | return false; |
| 599 | } |
| 600 | |
| 601 | bool CCECProcessor::TransmitInactiveSource(void) |
| 602 | { |
| 603 | if (!IsRunning()) |
| 604 | return false; |
| 605 | |
| 606 | if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary]) |
| 607 | return m_busDevices[m_logicalAddresses.primary]->TransmitInactiveSource(); |
| 608 | return false; |
| 609 | } |
| 610 | |
| 611 | void CCECProcessor::LogOutput(const cec_command &data) |
| 612 | { |
| 613 | CStdString strTx; |
| 614 | strTx.Format("<< %02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination); |
| 615 | if (data.opcode_set) |
| 616 | strTx.AppendFormat(":%02x", (uint8_t)data.opcode); |
| 617 | |
| 618 | for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++) |
| 619 | strTx.AppendFormat(":%02x", data.parameters[iPtr]); |
| 620 | CLibCEC::AddLog(CEC_LOG_TRAFFIC, strTx.c_str()); |
| 621 | } |
| 622 | |
| 623 | bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress) |
| 624 | { |
| 625 | CLockObject lock(m_mutex); |
| 626 | if (m_logicalAddresses.primary != iLogicalAddress) |
| 627 | { |
| 628 | CLibCEC::AddLog(CEC_LOG_NOTICE, "<< setting primary logical address to %1x", iLogicalAddress); |
| 629 | m_logicalAddresses.primary = iLogicalAddress; |
| 630 | m_logicalAddresses.Set(iLogicalAddress); |
| 631 | return SetAckMask(m_logicalAddresses.AckMask()); |
| 632 | } |
| 633 | |
| 634 | return true; |
| 635 | } |
| 636 | |
| 637 | bool CCECProcessor::SetMenuState(cec_menu_state state, bool bSendUpdate /* = true */) |
| 638 | { |
| 639 | for (uint8_t iPtr = 0; iPtr < 16; iPtr++) |
| 640 | { |
| 641 | if (m_logicalAddresses[iPtr]) |
| 642 | m_busDevices[iPtr]->SetMenuState(state); |
| 643 | } |
| 644 | |
| 645 | if (bSendUpdate) |
| 646 | m_busDevices[m_logicalAddresses.primary]->TransmitMenuState(CECDEVICE_TV); |
| 647 | |
| 648 | return true; |
| 649 | } |
| 650 | |
| 651 | bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress, bool bSendUpdate /* = true */) |
| 652 | { |
| 653 | bool bSendActiveView(false); |
| 654 | bool bReturn(false); |
| 655 | cec_logical_addresses sendUpdatesTo; |
| 656 | sendUpdatesTo.Clear(); |
| 657 | |
| 658 | { |
| 659 | CLockObject lock(m_mutex); |
| 660 | m_iPhysicalAddress = iPhysicalAddress; |
| 661 | |
| 662 | if (!m_logicalAddresses.IsEmpty()) |
| 663 | { |
| 664 | bool bWasActiveSource(false); |
| 665 | for (uint8_t iPtr = 0; iPtr < 15; iPtr++) |
| 666 | if (m_logicalAddresses[iPtr]) |
| 667 | { |
| 668 | bWasActiveSource |= m_busDevices[iPtr]->IsActiveSource(); |
| 669 | m_busDevices[iPtr]->SetInactiveSource(); |
| 670 | m_busDevices[iPtr]->SetPhysicalAddress(iPhysicalAddress); |
| 671 | if (bSendUpdate) |
| 672 | sendUpdatesTo.Set((cec_logical_address)iPtr); |
| 673 | } |
| 674 | |
| 675 | bSendActiveView = bWasActiveSource && bSendUpdate; |
| 676 | bReturn = true; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | for (uint8_t iPtr = 0; iPtr < 15; iPtr++) |
| 681 | if (sendUpdatesTo[iPtr]) |
| 682 | m_busDevices[iPtr]->TransmitPhysicalAddress(); |
| 683 | |
| 684 | if (bSendActiveView) |
| 685 | SetActiveView(); |
| 686 | |
| 687 | return bReturn; |
| 688 | } |
| 689 | |
| 690 | bool CCECProcessor::SwitchMonitoring(bool bEnable) |
| 691 | { |
| 692 | CLibCEC::AddLog(CEC_LOG_NOTICE, "== %s monitoring mode ==", bEnable ? "enabling" : "disabling"); |
| 693 | |
| 694 | { |
| 695 | CLockObject lock(m_mutex); |
| 696 | m_bMonitor = bEnable; |
| 697 | } |
| 698 | |
| 699 | if (bEnable) |
| 700 | return SetAckMask(0); |
| 701 | else |
| 702 | return SetAckMask(m_logicalAddresses.AckMask()); |
| 703 | } |
| 704 | |
| 705 | bool CCECProcessor::PollDevice(cec_logical_address iAddress) |
| 706 | { |
| 707 | if (iAddress != CECDEVICE_UNKNOWN && m_busDevices[iAddress]) |
| 708 | { |
| 709 | return m_logicalAddresses.primary == CECDEVICE_UNKNOWN ? |
| 710 | m_busDevices[iAddress]->TransmitPoll(iAddress) : |
| 711 | m_busDevices[m_logicalAddresses.primary]->TransmitPoll(iAddress); |
| 712 | } |
| 713 | return false; |
| 714 | } |
| 715 | |
| 716 | uint8_t CCECProcessor::VolumeUp(bool bSendRelease /* = true */) |
| 717 | { |
| 718 | uint8_t status = 0; |
| 719 | if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM)) |
| 720 | status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeUp(bSendRelease); |
| 721 | |
| 722 | return status; |
| 723 | } |
| 724 | |
| 725 | uint8_t CCECProcessor::VolumeDown(bool bSendRelease /* = true */) |
| 726 | { |
| 727 | uint8_t status = 0; |
| 728 | if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM)) |
| 729 | status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeDown(bSendRelease); |
| 730 | |
| 731 | return status; |
| 732 | } |
| 733 | |
| 734 | uint8_t CCECProcessor::MuteAudio(bool bSendRelease /* = true */) |
| 735 | { |
| 736 | uint8_t status = 0; |
| 737 | if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM)) |
| 738 | status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->MuteAudio(bSendRelease); |
| 739 | |
| 740 | return status; |
| 741 | } |
| 742 | |
| 743 | CCECBusDevice *CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bRefresh /* = false */) const |
| 744 | { |
| 745 | if (m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false) == iPhysicalAddress) |
| 746 | return m_busDevices[m_logicalAddresses.primary]; |
| 747 | |
| 748 | CCECBusDevice *device = NULL; |
| 749 | for (unsigned int iPtr = 0; iPtr < 16; iPtr++) |
| 750 | { |
| 751 | if (m_busDevices[iPtr]->GetPhysicalAddress(bRefresh) == iPhysicalAddress) |
| 752 | { |
| 753 | device = m_busDevices[iPtr]; |
| 754 | break; |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | return device; |
| 759 | } |
| 760 | |
| 761 | CCECBusDevice *CCECProcessor::GetDeviceByType(cec_device_type type) const |
| 762 | { |
| 763 | CCECBusDevice *device = NULL; |
| 764 | |
| 765 | for (uint8_t iPtr = 0; iPtr < 16; iPtr++) |
| 766 | { |
| 767 | if (m_busDevices[iPtr]->m_type == type && m_logicalAddresses[iPtr]) |
| 768 | { |
| 769 | device = m_busDevices[iPtr]; |
| 770 | break; |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | return device; |
| 775 | } |
| 776 | |
| 777 | CCECBusDevice *CCECProcessor::GetPrimaryDevice(void) const |
| 778 | { |
| 779 | CCECBusDevice *device(NULL); |
| 780 | cec_logical_address primary = m_logicalAddresses.primary; |
| 781 | if (primary != CECDEVICE_UNKNOWN) |
| 782 | device = m_busDevices[primary]; |
| 783 | return device; |
| 784 | } |
| 785 | |
| 786 | cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress) |
| 787 | { |
| 788 | return m_busDevices[iAddress]->GetCecVersion(); |
| 789 | } |
| 790 | |
| 791 | cec_osd_name CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress) |
| 792 | { |
| 793 | CStdString strOSDName = m_busDevices[iAddress]->GetOSDName(); |
| 794 | cec_osd_name retVal; |
| 795 | |
| 796 | snprintf(retVal.name, sizeof(retVal.name), "%s", strOSDName.c_str()); |
| 797 | retVal.device = iAddress; |
| 798 | |
| 799 | return retVal; |
| 800 | } |
| 801 | |
| 802 | bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language) |
| 803 | { |
| 804 | if (m_busDevices[iAddress]) |
| 805 | { |
| 806 | *language = m_busDevices[iAddress]->GetMenuLanguage(); |
| 807 | return (strcmp(language->language, "???") != 0); |
| 808 | } |
| 809 | return false; |
| 810 | } |
| 811 | |
| 812 | uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress) |
| 813 | { |
| 814 | if (m_busDevices[iAddress]) |
| 815 | return m_busDevices[iAddress]->GetVendorId(); |
| 816 | return false; |
| 817 | } |
| 818 | |
| 819 | uint16_t CCECProcessor::GetDevicePhysicalAddress(cec_logical_address iAddress) |
| 820 | { |
| 821 | if (m_busDevices[iAddress]) |
| 822 | return m_busDevices[iAddress]->GetPhysicalAddress(false); |
| 823 | return false; |
| 824 | } |
| 825 | |
| 826 | cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress) |
| 827 | { |
| 828 | if (m_busDevices[iAddress]) |
| 829 | return m_busDevices[iAddress]->GetPowerStatus(); |
| 830 | return CEC_POWER_STATUS_UNKNOWN; |
| 831 | } |
| 832 | |
| 833 | cec_logical_address CCECProcessor::GetActiveSource(void) |
| 834 | { |
| 835 | for (uint8_t iPtr = 0; iPtr <= 11; iPtr++) |
| 836 | { |
| 837 | if (m_busDevices[iPtr]->IsActiveSource()) |
| 838 | return (cec_logical_address)iPtr; |
| 839 | } |
| 840 | |
| 841 | return CECDEVICE_UNKNOWN; |
| 842 | } |
| 843 | |
| 844 | bool CCECProcessor::IsActiveSource(cec_logical_address iAddress) |
| 845 | { |
| 846 | return m_busDevices[iAddress]->IsActiveSource(); |
| 847 | } |
| 848 | |
| 849 | bool CCECProcessor::Transmit(const cec_command &data) |
| 850 | { |
| 851 | cec_adapter_message_state retVal(ADAPTER_MESSAGE_STATE_UNKNOWN); |
| 852 | { |
| 853 | CLockObject lock(m_mutex); |
| 854 | LogOutput(data); |
| 855 | m_iLastTransmission = GetTimeMs(); |
| 856 | if (!m_communication) |
| 857 | return false; |
| 858 | uint8_t iMaxTries = m_busDevices[data.initiator]->GetHandler()->GetTransmitRetries() + 1; |
| 859 | retVal = m_communication->Write(data, iMaxTries, m_iLineTimeout, m_iRetryLineTimeout); |
| 860 | } |
| 861 | |
| 862 | /* set to "not present" on failed ack */ |
| 863 | if (retVal == ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED && |
| 864 | data.destination != CECDEVICE_BROADCAST) |
| 865 | m_busDevices[data.destination]->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT); |
| 866 | |
| 867 | return retVal == ADAPTER_MESSAGE_STATE_SENT_ACKED; |
| 868 | } |
| 869 | |
| 870 | void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, cec_abort_reason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */) |
| 871 | { |
| 872 | CLibCEC::AddLog(CEC_LOG_DEBUG, "<< transmitting abort message"); |
| 873 | |
| 874 | cec_command command; |
| 875 | // TODO |
| 876 | cec_command::Format(command, m_logicalAddresses.primary, address, CEC_OPCODE_FEATURE_ABORT); |
| 877 | command.parameters.PushBack((uint8_t)opcode); |
| 878 | command.parameters.PushBack((uint8_t)reason); |
| 879 | |
| 880 | Transmit(command); |
| 881 | } |
| 882 | |
| 883 | void CCECProcessor::ParseCommand(const cec_command &command) |
| 884 | { |
| 885 | CStdString dataStr; |
| 886 | dataStr.Format(">> %1x%1x:%02x", command.initiator, command.destination, command.opcode); |
| 887 | for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++) |
| 888 | dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]); |
| 889 | CLibCEC::AddLog(CEC_LOG_TRAFFIC, dataStr.c_str()); |
| 890 | |
| 891 | if (!m_bMonitor && command.initiator >= CECDEVICE_TV && command.initiator <= CECDEVICE_BROADCAST) |
| 892 | m_busDevices[(uint8_t)command.initiator]->HandleCommand(command); |
| 893 | } |
| 894 | |
| 895 | cec_logical_addresses CCECProcessor::GetActiveDevices(void) |
| 896 | { |
| 897 | cec_logical_addresses addresses; |
| 898 | addresses.Clear(); |
| 899 | for (unsigned int iPtr = 0; iPtr < 15; iPtr++) |
| 900 | { |
| 901 | if (m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) |
| 902 | addresses.Set((cec_logical_address) iPtr); |
| 903 | } |
| 904 | return addresses; |
| 905 | } |
| 906 | |
| 907 | bool CCECProcessor::IsPresentDevice(cec_logical_address address) |
| 908 | { |
| 909 | return m_busDevices[address]->GetStatus() == CEC_DEVICE_STATUS_PRESENT; |
| 910 | } |
| 911 | |
| 912 | bool CCECProcessor::IsPresentDeviceType(cec_device_type type) |
| 913 | { |
| 914 | for (unsigned int iPtr = 0; iPtr < 15; iPtr++) |
| 915 | { |
| 916 | if (m_busDevices[iPtr]->GetType() == type && m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) |
| 917 | return true; |
| 918 | } |
| 919 | |
| 920 | return false; |
| 921 | } |
| 922 | |
| 923 | uint16_t CCECProcessor::GetPhysicalAddress(void) const |
| 924 | { |
| 925 | if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary]) |
| 926 | return m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false); |
| 927 | return false; |
| 928 | } |
| 929 | |
| 930 | bool CCECProcessor::SetAckMask(uint16_t iMask) |
| 931 | { |
| 932 | return m_communication->SetAckMask(iMask); |
| 933 | } |
| 934 | |
| 935 | bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */) |
| 936 | { |
| 937 | return m_busDevices[iDestination]->TransmitKeypress(key, bWait); |
| 938 | } |
| 939 | |
| 940 | bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination, bool bWait /* = true */) |
| 941 | { |
| 942 | return m_busDevices[iDestination]->TransmitKeyRelease(bWait); |
| 943 | } |
| 944 | |
| 945 | const char *CCECProcessor::ToString(const cec_device_type type) |
| 946 | { |
| 947 | switch (type) |
| 948 | { |
| 949 | case CEC_DEVICE_TYPE_AUDIO_SYSTEM: |
| 950 | return "audio system"; |
| 951 | case CEC_DEVICE_TYPE_PLAYBACK_DEVICE: |
| 952 | return "playback device"; |
| 953 | case CEC_DEVICE_TYPE_RECORDING_DEVICE: |
| 954 | return "recording device"; |
| 955 | case CEC_DEVICE_TYPE_RESERVED: |
| 956 | return "reserved"; |
| 957 | case CEC_DEVICE_TYPE_TUNER: |
| 958 | return "tuner"; |
| 959 | case CEC_DEVICE_TYPE_TV: |
| 960 | return "TV"; |
| 961 | default: |
| 962 | return "unknown"; |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | const char *CCECProcessor::ToString(const cec_menu_state state) |
| 967 | { |
| 968 | switch (state) |
| 969 | { |
| 970 | case CEC_MENU_STATE_ACTIVATED: |
| 971 | return "activated"; |
| 972 | case CEC_MENU_STATE_DEACTIVATED: |
| 973 | return "deactivated"; |
| 974 | default: |
| 975 | return "unknown"; |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | const char *CCECProcessor::ToString(const cec_version version) |
| 980 | { |
| 981 | switch (version) |
| 982 | { |
| 983 | case CEC_VERSION_1_2: |
| 984 | return "1.2"; |
| 985 | case CEC_VERSION_1_2A: |
| 986 | return "1.2a"; |
| 987 | case CEC_VERSION_1_3: |
| 988 | return "1.3"; |
| 989 | case CEC_VERSION_1_3A: |
| 990 | return "1.3a"; |
| 991 | case CEC_VERSION_1_4: |
| 992 | return "1.4"; |
| 993 | default: |
| 994 | return "unknown"; |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | const char *CCECProcessor::ToString(const cec_power_status status) |
| 999 | { |
| 1000 | switch (status) |
| 1001 | { |
| 1002 | case CEC_POWER_STATUS_ON: |
| 1003 | return "on"; |
| 1004 | case CEC_POWER_STATUS_STANDBY: |
| 1005 | return "standby"; |
| 1006 | case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY: |
| 1007 | return "in transition from on to standby"; |
| 1008 | case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON: |
| 1009 | return "in transition from standby to on"; |
| 1010 | default: |
| 1011 | return "unknown"; |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | const char *CCECProcessor::ToString(const cec_logical_address address) |
| 1016 | { |
| 1017 | switch(address) |
| 1018 | { |
| 1019 | case CECDEVICE_AUDIOSYSTEM: |
| 1020 | return "Audio"; |
| 1021 | case CECDEVICE_BROADCAST: |
| 1022 | return "Broadcast"; |
| 1023 | case CECDEVICE_FREEUSE: |
| 1024 | return "Free use"; |
| 1025 | case CECDEVICE_PLAYBACKDEVICE1: |
| 1026 | return "Playback 1"; |
| 1027 | case CECDEVICE_PLAYBACKDEVICE2: |
| 1028 | return "Playback 2"; |
| 1029 | case CECDEVICE_PLAYBACKDEVICE3: |
| 1030 | return "Playback 3"; |
| 1031 | case CECDEVICE_RECORDINGDEVICE1: |
| 1032 | return "Recorder 1"; |
| 1033 | case CECDEVICE_RECORDINGDEVICE2: |
| 1034 | return "Recorder 2"; |
| 1035 | case CECDEVICE_RECORDINGDEVICE3: |
| 1036 | return "Recorder 3"; |
| 1037 | case CECDEVICE_RESERVED1: |
| 1038 | return "Reserved 1"; |
| 1039 | case CECDEVICE_RESERVED2: |
| 1040 | return "Reserved 2"; |
| 1041 | case CECDEVICE_TUNER1: |
| 1042 | return "Tuner 1"; |
| 1043 | case CECDEVICE_TUNER2: |
| 1044 | return "Tuner 2"; |
| 1045 | case CECDEVICE_TUNER3: |
| 1046 | return "Tuner 3"; |
| 1047 | case CECDEVICE_TUNER4: |
| 1048 | return "Tuner 4"; |
| 1049 | case CECDEVICE_TV: |
| 1050 | return "TV"; |
| 1051 | default: |
| 1052 | return "unknown"; |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | const char *CCECProcessor::ToString(const cec_deck_control_mode mode) |
| 1057 | { |
| 1058 | switch (mode) |
| 1059 | { |
| 1060 | case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND: |
| 1061 | return "skip forward wind"; |
| 1062 | case CEC_DECK_CONTROL_MODE_EJECT: |
| 1063 | return "eject"; |
| 1064 | case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND: |
| 1065 | return "reverse rewind"; |
| 1066 | case CEC_DECK_CONTROL_MODE_STOP: |
| 1067 | return "stop"; |
| 1068 | default: |
| 1069 | return "unknown"; |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | const char *CCECProcessor::ToString(const cec_deck_info status) |
| 1074 | { |
| 1075 | switch (status) |
| 1076 | { |
| 1077 | case CEC_DECK_INFO_PLAY: |
| 1078 | return "play"; |
| 1079 | case CEC_DECK_INFO_RECORD: |
| 1080 | return "record"; |
| 1081 | case CEC_DECK_INFO_PLAY_REVERSE: |
| 1082 | return "play reverse"; |
| 1083 | case CEC_DECK_INFO_STILL: |
| 1084 | return "still"; |
| 1085 | case CEC_DECK_INFO_SLOW: |
| 1086 | return "slow"; |
| 1087 | case CEC_DECK_INFO_SLOW_REVERSE: |
| 1088 | return "slow reverse"; |
| 1089 | case CEC_DECK_INFO_FAST_FORWARD: |
| 1090 | return "fast forward"; |
| 1091 | case CEC_DECK_INFO_FAST_REVERSE: |
| 1092 | return "fast reverse"; |
| 1093 | case CEC_DECK_INFO_NO_MEDIA: |
| 1094 | return "no media"; |
| 1095 | case CEC_DECK_INFO_STOP: |
| 1096 | return "stop"; |
| 1097 | case CEC_DECK_INFO_SKIP_FORWARD_WIND: |
| 1098 | return "info skip forward wind"; |
| 1099 | case CEC_DECK_INFO_SKIP_REVERSE_REWIND: |
| 1100 | return "info skip reverse rewind"; |
| 1101 | case CEC_DECK_INFO_INDEX_SEARCH_FORWARD: |
| 1102 | return "info index search forward"; |
| 1103 | case CEC_DECK_INFO_INDEX_SEARCH_REVERSE: |
| 1104 | return "info index search reverse"; |
| 1105 | case CEC_DECK_INFO_OTHER_STATUS: |
| 1106 | return "other"; |
| 1107 | default: |
| 1108 | return "unknown"; |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | const char *CCECProcessor::ToString(const cec_opcode opcode) |
| 1113 | { |
| 1114 | switch (opcode) |
| 1115 | { |
| 1116 | case CEC_OPCODE_ACTIVE_SOURCE: |
| 1117 | return "active source"; |
| 1118 | case CEC_OPCODE_IMAGE_VIEW_ON: |
| 1119 | return "image view on"; |
| 1120 | case CEC_OPCODE_TEXT_VIEW_ON: |
| 1121 | return "text view on"; |
| 1122 | case CEC_OPCODE_INACTIVE_SOURCE: |
| 1123 | return "inactive source"; |
| 1124 | case CEC_OPCODE_REQUEST_ACTIVE_SOURCE: |
| 1125 | return "request active source"; |
| 1126 | case CEC_OPCODE_ROUTING_CHANGE: |
| 1127 | return "routing change"; |
| 1128 | case CEC_OPCODE_ROUTING_INFORMATION: |
| 1129 | return "routing information"; |
| 1130 | case CEC_OPCODE_SET_STREAM_PATH: |
| 1131 | return "set stream path"; |
| 1132 | case CEC_OPCODE_STANDBY: |
| 1133 | return "standby"; |
| 1134 | case CEC_OPCODE_RECORD_OFF: |
| 1135 | return "record off"; |
| 1136 | case CEC_OPCODE_RECORD_ON: |
| 1137 | return "record on"; |
| 1138 | case CEC_OPCODE_RECORD_STATUS: |
| 1139 | return "record status"; |
| 1140 | case CEC_OPCODE_RECORD_TV_SCREEN: |
| 1141 | return "record tv screen"; |
| 1142 | case CEC_OPCODE_CLEAR_ANALOGUE_TIMER: |
| 1143 | return "clear analogue timer"; |
| 1144 | case CEC_OPCODE_CLEAR_DIGITAL_TIMER: |
| 1145 | return "clear digital timer"; |
| 1146 | case CEC_OPCODE_CLEAR_EXTERNAL_TIMER: |
| 1147 | return "clear external timer"; |
| 1148 | case CEC_OPCODE_SET_ANALOGUE_TIMER: |
| 1149 | return "set analogue timer"; |
| 1150 | case CEC_OPCODE_SET_DIGITAL_TIMER: |
| 1151 | return "set digital timer"; |
| 1152 | case CEC_OPCODE_SET_EXTERNAL_TIMER: |
| 1153 | return "set external timer"; |
| 1154 | case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE: |
| 1155 | return "set timer program title"; |
| 1156 | case CEC_OPCODE_TIMER_CLEARED_STATUS: |
| 1157 | return "timer cleared status"; |
| 1158 | case CEC_OPCODE_TIMER_STATUS: |
| 1159 | return "timer status"; |
| 1160 | case CEC_OPCODE_CEC_VERSION: |
| 1161 | return "cec version"; |
| 1162 | case CEC_OPCODE_GET_CEC_VERSION: |
| 1163 | return "get cec version"; |
| 1164 | case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS: |
| 1165 | return "give physical address"; |
| 1166 | case CEC_OPCODE_GET_MENU_LANGUAGE: |
| 1167 | return "get menu language"; |
| 1168 | case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS: |
| 1169 | return "report physical address"; |
| 1170 | case CEC_OPCODE_SET_MENU_LANGUAGE: |
| 1171 | return "set menu language"; |
| 1172 | case CEC_OPCODE_DECK_CONTROL: |
| 1173 | return "deck control"; |
| 1174 | case CEC_OPCODE_DECK_STATUS: |
| 1175 | return "deck status"; |
| 1176 | case CEC_OPCODE_GIVE_DECK_STATUS: |
| 1177 | return "give deck status"; |
| 1178 | case CEC_OPCODE_PLAY: |
| 1179 | return "play"; |
| 1180 | case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS: |
| 1181 | return "give tuner status"; |
| 1182 | case CEC_OPCODE_SELECT_ANALOGUE_SERVICE: |
| 1183 | return "select analogue service"; |
| 1184 | case CEC_OPCODE_SELECT_DIGITAL_SERVICE: |
| 1185 | return "set digital service"; |
| 1186 | case CEC_OPCODE_TUNER_DEVICE_STATUS: |
| 1187 | return "tuner device status"; |
| 1188 | case CEC_OPCODE_TUNER_STEP_DECREMENT: |
| 1189 | return "tuner step decrement"; |
| 1190 | case CEC_OPCODE_TUNER_STEP_INCREMENT: |
| 1191 | return "tuner step increment"; |
| 1192 | case CEC_OPCODE_DEVICE_VENDOR_ID: |
| 1193 | return "device vendor id"; |
| 1194 | case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID: |
| 1195 | return "give device vendor id"; |
| 1196 | case CEC_OPCODE_VENDOR_COMMAND: |
| 1197 | return "vendor command"; |
| 1198 | case CEC_OPCODE_VENDOR_COMMAND_WITH_ID: |
| 1199 | return "vendor command with id"; |
| 1200 | case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN: |
| 1201 | return "vendor remote button down"; |
| 1202 | case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP: |
| 1203 | return "vendor remote button up"; |
| 1204 | case CEC_OPCODE_SET_OSD_STRING: |
| 1205 | return "set osd string"; |
| 1206 | case CEC_OPCODE_GIVE_OSD_NAME: |
| 1207 | return "give osd name"; |
| 1208 | case CEC_OPCODE_SET_OSD_NAME: |
| 1209 | return "set osd name"; |
| 1210 | case CEC_OPCODE_MENU_REQUEST: |
| 1211 | return "menu request"; |
| 1212 | case CEC_OPCODE_MENU_STATUS: |
| 1213 | return "menu status"; |
| 1214 | case CEC_OPCODE_USER_CONTROL_PRESSED: |
| 1215 | return "user control pressed"; |
| 1216 | case CEC_OPCODE_USER_CONTROL_RELEASE: |
| 1217 | return "user control release"; |
| 1218 | case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS: |
| 1219 | return "give device power status"; |
| 1220 | case CEC_OPCODE_REPORT_POWER_STATUS: |
| 1221 | return "report power status"; |
| 1222 | case CEC_OPCODE_FEATURE_ABORT: |
| 1223 | return "feature abort"; |
| 1224 | case CEC_OPCODE_ABORT: |
| 1225 | return "abort"; |
| 1226 | case CEC_OPCODE_GIVE_AUDIO_STATUS: |
| 1227 | return "give audio status"; |
| 1228 | case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS: |
| 1229 | return "give audio mode status"; |
| 1230 | case CEC_OPCODE_REPORT_AUDIO_STATUS: |
| 1231 | return "report audio status"; |
| 1232 | case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE: |
| 1233 | return "set system audio mode"; |
| 1234 | case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST: |
| 1235 | return "system audio mode request"; |
| 1236 | case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS: |
| 1237 | return "system audio mode status"; |
| 1238 | case CEC_OPCODE_SET_AUDIO_RATE: |
| 1239 | return "set audio rate"; |
| 1240 | default: |
| 1241 | return "UNKNOWN"; |
| 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | const char *CCECProcessor::ToString(const cec_system_audio_status mode) |
| 1246 | { |
| 1247 | switch(mode) |
| 1248 | { |
| 1249 | case CEC_SYSTEM_AUDIO_STATUS_ON: |
| 1250 | return "on"; |
| 1251 | case CEC_SYSTEM_AUDIO_STATUS_OFF: |
| 1252 | return "off"; |
| 1253 | default: |
| 1254 | return "unknown"; |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | const char *CCECProcessor::ToString(const cec_audio_status UNUSED(status)) |
| 1259 | { |
| 1260 | // TODO this is a mask |
| 1261 | return "TODO"; |
| 1262 | } |
| 1263 | |
| 1264 | const char *CCECProcessor::ToString(const cec_vendor_id vendor) |
| 1265 | { |
| 1266 | switch (vendor) |
| 1267 | { |
| 1268 | case CEC_VENDOR_SAMSUNG: |
| 1269 | return "Samsung"; |
| 1270 | case CEC_VENDOR_LG: |
| 1271 | return "LG"; |
| 1272 | case CEC_VENDOR_PANASONIC: |
| 1273 | return "Panasonic"; |
| 1274 | case CEC_VENDOR_PIONEER: |
| 1275 | return "Pioneer"; |
| 1276 | case CEC_VENDOR_ONKYO: |
| 1277 | return "Onkyo"; |
| 1278 | case CEC_VENDOR_YAMAHA: |
| 1279 | return "Yamaha"; |
| 1280 | case CEC_VENDOR_PHILIPS: |
| 1281 | return "Philips"; |
| 1282 | case CEC_VENDOR_SONY: |
| 1283 | return "Sony"; |
| 1284 | default: |
| 1285 | return "Unknown"; |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | const char *CCECProcessor::ToString(const cec_client_version version) |
| 1290 | { |
| 1291 | switch (version) |
| 1292 | { |
| 1293 | case CEC_CLIENT_VERSION_PRE_1_5: |
| 1294 | return "pre-1.5"; |
| 1295 | case CEC_CLIENT_VERSION_1_5_0: |
| 1296 | return "1.5.0"; |
| 1297 | default: |
| 1298 | return "Unknown"; |
| 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | void *CCECBusScan::Process(void) |
| 1303 | { |
| 1304 | CCECBusDevice *device(NULL); |
| 1305 | uint8_t iCounter(0); |
| 1306 | |
| 1307 | while (!IsStopped()) |
| 1308 | { |
| 1309 | if (++iCounter < 10) |
| 1310 | { |
| 1311 | Sleep(1000); |
| 1312 | continue; |
| 1313 | } |
| 1314 | for (unsigned int iPtr = 0; iPtr <= 11 && !IsStopped(); iPtr++) |
| 1315 | { |
| 1316 | device = m_processor->m_busDevices[iPtr]; |
| 1317 | WaitUntilIdle(); |
| 1318 | if (device && device->GetStatus(true) == CEC_DEVICE_STATUS_PRESENT) |
| 1319 | { |
| 1320 | WaitUntilIdle(); |
| 1321 | if (!IsStopped()) |
| 1322 | device->GetVendorId(); |
| 1323 | |
| 1324 | WaitUntilIdle(); |
| 1325 | if (!IsStopped()) |
| 1326 | device->GetPowerStatus(true); |
| 1327 | } |
| 1328 | } |
| 1329 | } |
| 1330 | |
| 1331 | return NULL; |
| 1332 | } |
| 1333 | |
| 1334 | void CCECBusScan::WaitUntilIdle(void) |
| 1335 | { |
| 1336 | if (IsStopped()) |
| 1337 | return; |
| 1338 | |
| 1339 | int32_t iWaitTime = 3000 - (int32_t)(GetTimeMs() - m_processor->GetLastTransmission()); |
| 1340 | while (iWaitTime > 0) |
| 1341 | { |
| 1342 | Sleep(iWaitTime); |
| 1343 | iWaitTime = 3000 - (int32_t)(GetTimeMs() - m_processor->GetLastTransmission()); |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | bool CCECProcessor::StartBootloader(void) |
| 1348 | { |
| 1349 | return m_communication->StartBootloader(); |
| 1350 | } |
| 1351 | |
| 1352 | bool CCECProcessor::PingAdapter(void) |
| 1353 | { |
| 1354 | return m_communication->PingAdapter(); |
| 1355 | } |
| 1356 | |
| 1357 | void CCECProcessor::HandlePoll(cec_logical_address initiator, cec_logical_address destination) |
| 1358 | { |
| 1359 | m_busDevices[initiator]->HandlePoll(destination); |
| 1360 | } |
| 1361 | |
| 1362 | bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator) |
| 1363 | { |
| 1364 | return !m_busDevices[initiator]->HandleReceiveFailed(); |
| 1365 | } |
| 1366 | |
| 1367 | bool CCECProcessor::SetStreamPath(uint16_t iPhysicalAddress) |
| 1368 | { |
| 1369 | // stream path changes are sent by the TV |
| 1370 | return m_busDevices[CECDEVICE_TV]->GetHandler()->TransmitSetStreamPath(iPhysicalAddress); |
| 1371 | } |
| 1372 | |
| 1373 | bool CCECProcessor::GetCurrentConfiguration(libcec_configuration *configuration) |
| 1374 | { |
| 1375 | configuration->iPhysicalAddress = m_iPhysicalAddress; |
| 1376 | configuration->iHDMIPort = m_iHDMIPort; |
| 1377 | configuration->baseDevice = m_iBaseDevice; |
| 1378 | snprintf(configuration->strDeviceName, 13, "%s", m_strDeviceName.c_str()); |
| 1379 | configuration->deviceTypes = m_types; |
| 1380 | return true; |
| 1381 | } |
| 1382 | |
| 1383 | bool CCECProcessor::CanPersistConfiguration(void) |
| 1384 | { |
| 1385 | return m_communication->GetFirmwareVersion() >= 2; |
| 1386 | } |
| 1387 | |
| 1388 | bool CCECProcessor::PersistConfiguration(libcec_configuration *configuration) |
| 1389 | { |
| 1390 | return m_communication->PersistConfiguration(configuration); |
| 1391 | } |