| 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 "CECClient.h" |
| 34 | #include "CECProcessor.h" |
| 35 | #include "LibCEC.h" |
| 36 | #include "devices/CECPlaybackDevice.h" |
| 37 | #include "devices/CECAudioSystem.h" |
| 38 | #include "devices/CECTV.h" |
| 39 | |
| 40 | using namespace CEC; |
| 41 | using namespace PLATFORM; |
| 42 | |
| 43 | #define LIB_CEC m_processor->GetLib() |
| 44 | #define ToString(x) LIB_CEC->ToString(x) |
| 45 | |
| 46 | CCECClient::CCECClient(CCECProcessor *processor, const libcec_configuration &configuration) : |
| 47 | m_processor(processor), |
| 48 | m_bInitialised(false), |
| 49 | m_bRegistered(false), |
| 50 | m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN), |
| 51 | m_buttontime(0) |
| 52 | { |
| 53 | // set the initial configuration |
| 54 | SetConfiguration(configuration); |
| 55 | } |
| 56 | |
| 57 | CCECClient::~CCECClient(void) |
| 58 | { |
| 59 | // unregister the client |
| 60 | if (m_processor && IsRegistered()) |
| 61 | m_processor->UnregisterClient(this); |
| 62 | } |
| 63 | |
| 64 | bool CCECClient::IsInitialised(void) |
| 65 | { |
| 66 | CLockObject lock(m_mutex); |
| 67 | return m_bInitialised && m_processor; |
| 68 | } |
| 69 | |
| 70 | void CCECClient::SetInitialised(bool bSetTo) |
| 71 | { |
| 72 | CLockObject lock(m_mutex); |
| 73 | m_bInitialised = bSetTo; |
| 74 | } |
| 75 | |
| 76 | bool CCECClient::IsRegistered(void) |
| 77 | { |
| 78 | CLockObject lock(m_mutex); |
| 79 | return m_bRegistered && m_processor; |
| 80 | } |
| 81 | |
| 82 | void CCECClient::SetRegistered(bool bSetTo) |
| 83 | { |
| 84 | CLockObject lock(m_mutex); |
| 85 | m_bRegistered = bSetTo; |
| 86 | } |
| 87 | |
| 88 | bool CCECClient::OnRegister(void) |
| 89 | { |
| 90 | // return false if already initialised |
| 91 | if (IsInitialised()) |
| 92 | return true; |
| 93 | |
| 94 | // get all device we control |
| 95 | CECDEVICEVEC devices; |
| 96 | m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses); |
| 97 | |
| 98 | // return false when no devices were found |
| 99 | if (devices.empty()) |
| 100 | { |
| 101 | LIB_CEC->AddLog(CEC_LOG_WARNING, "cannot find the primary device (logical address %x)", GetPrimaryLogicalAdddress()); |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | // mark as initialised |
| 106 | SetInitialised(true); |
| 107 | |
| 108 | // configure all devices |
| 109 | for (CECDEVICEVEC::iterator it = devices.begin(); it != devices.end(); it++) |
| 110 | { |
| 111 | // only set our OSD name for the primary device |
| 112 | if ((*it)->GetLogicalAddress() == GetPrimaryLogicalAdddress()) |
| 113 | (*it)->SetOSDName(m_configuration.strDeviceName); |
| 114 | |
| 115 | // set the default menu language for devices we control |
| 116 | (*it)->SetMenuLanguage(m_configuration.strDeviceLanguage); |
| 117 | } |
| 118 | |
| 119 | // set the physical address |
| 120 | SetPhysicalAddress(m_configuration); |
| 121 | |
| 122 | // ensure that we know the vendor id of the TV, so we are using the correct handler |
| 123 | m_processor->GetTV()->GetVendorId(GetPrimaryLogicalAdddress()); |
| 124 | |
| 125 | // make the primary device the active source if the option is set |
| 126 | if (m_configuration.bActivateSource == 1) |
| 127 | GetPrimaryDevice()->ActivateSource(); |
| 128 | |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | bool CCECClient::SetHDMIPort(const cec_logical_address iBaseDevice, const uint8_t iPort, bool bForce /* = false */) |
| 133 | { |
| 134 | bool bReturn(false); |
| 135 | |
| 136 | // limit the HDMI port range to 1-15 |
| 137 | if (iPort < CEC_MIN_HDMI_PORTNUMBER || |
| 138 | iPort > CEC_MAX_HDMI_PORTNUMBER) |
| 139 | return bReturn; |
| 140 | |
| 141 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "setting HDMI port to %d on device %s (%d)", iPort, ToString(iBaseDevice), (int)iBaseDevice); |
| 142 | |
| 143 | // update the configuration |
| 144 | { |
| 145 | CLockObject lock(m_mutex); |
| 146 | m_configuration.baseDevice = iBaseDevice; |
| 147 | m_configuration.iHDMIPort = iPort; |
| 148 | } |
| 149 | |
| 150 | // don't continue if the connection isn't opened |
| 151 | if (!m_processor->CECInitialised() && !bForce) |
| 152 | return true; |
| 153 | |
| 154 | // get the PA of the base device |
| 155 | uint16_t iPhysicalAddress(CEC_INVALID_PHYSICAL_ADDRESS); |
| 156 | CCECBusDevice *baseDevice = m_processor->GetDevice(iBaseDevice); |
| 157 | if (baseDevice) |
| 158 | iPhysicalAddress = baseDevice->GetPhysicalAddress(GetPrimaryLogicalAdddress()); |
| 159 | |
| 160 | // add our port number |
| 161 | if (iPhysicalAddress <= CEC_MAX_PHYSICAL_ADDRESS) |
| 162 | { |
| 163 | if (iPhysicalAddress == 0) |
| 164 | iPhysicalAddress += 0x1000 * iPort; |
| 165 | else if (iPhysicalAddress % 0x1000 == 0) |
| 166 | iPhysicalAddress += 0x100 * iPort; |
| 167 | else if (iPhysicalAddress % 0x100 == 0) |
| 168 | iPhysicalAddress += 0x10 * iPort; |
| 169 | else if (iPhysicalAddress % 0x10 == 0) |
| 170 | iPhysicalAddress += iPort; |
| 171 | |
| 172 | bReturn = true; |
| 173 | } |
| 174 | |
| 175 | // set the default address when something went wrong |
| 176 | if (!bReturn) |
| 177 | { |
| 178 | LIB_CEC->AddLog(CEC_LOG_WARNING, "failed to set the physical address to %04X, setting it to the default value %04X", iPhysicalAddress, CEC_DEFAULT_PHYSICAL_ADDRESS); |
| 179 | iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS; |
| 180 | } |
| 181 | |
| 182 | // and set the address |
| 183 | SetDevicePhysicalAddress(iPhysicalAddress); |
| 184 | |
| 185 | ConfigurationChanged(m_configuration); |
| 186 | |
| 187 | return bReturn; |
| 188 | } |
| 189 | |
| 190 | void CCECClient::ResetPhysicalAddress(void) |
| 191 | { |
| 192 | SetPhysicalAddress(m_configuration); |
| 193 | } |
| 194 | |
| 195 | void CCECClient::SetPhysicalAddress(const libcec_configuration &configuration) |
| 196 | { |
| 197 | // try to autodetect the address |
| 198 | bool bPASet(false); |
| 199 | if (m_processor->CECInitialised() && configuration.bAutodetectAddress == 1) |
| 200 | bPASet = AutodetectPhysicalAddress(); |
| 201 | |
| 202 | // try to use physical address setting |
| 203 | if (!bPASet && CLibCEC::IsValidPhysicalAddress(configuration.iPhysicalAddress)) |
| 204 | bPASet = SetPhysicalAddress(configuration.iPhysicalAddress); |
| 205 | |
| 206 | // use the base device + hdmi port settings |
| 207 | if (!bPASet) |
| 208 | bPASet = SetHDMIPort(configuration.baseDevice, configuration.iHDMIPort); |
| 209 | |
| 210 | // reset to defaults if something went wrong |
| 211 | if (!bPASet) |
| 212 | { |
| 213 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - resetting HDMI port and base device to defaults", __FUNCTION__); |
| 214 | m_configuration.baseDevice = CECDEVICE_UNKNOWN; |
| 215 | m_configuration.iHDMIPort = CEC_HDMI_PORTNUMBER_NONE; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | bool CCECClient::SetPhysicalAddress(const uint16_t iPhysicalAddress) |
| 220 | { |
| 221 | // update the configuration |
| 222 | { |
| 223 | CLockObject lock(m_mutex); |
| 224 | if (m_configuration.iPhysicalAddress == iPhysicalAddress) |
| 225 | { |
| 226 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "physical address unchanged (%04X)", iPhysicalAddress); |
| 227 | return true; |
| 228 | } |
| 229 | else |
| 230 | { |
| 231 | m_configuration.iPhysicalAddress = iPhysicalAddress; |
| 232 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "setting physical address to '%04X'", iPhysicalAddress); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | // persist the new configuration |
| 237 | m_processor->PersistConfiguration(m_configuration); |
| 238 | |
| 239 | // set the physical address for each device |
| 240 | SetDevicePhysicalAddress(iPhysicalAddress); |
| 241 | |
| 242 | // and send back the updated configuration |
| 243 | ConfigurationChanged(m_configuration); |
| 244 | |
| 245 | return true; |
| 246 | } |
| 247 | |
| 248 | bool CCECClient::AllocateLogicalAddresses(void) |
| 249 | { |
| 250 | // reset all previous LAs that were set |
| 251 | m_configuration.logicalAddresses.Clear(); |
| 252 | |
| 253 | // display an error if no device types are set |
| 254 | if (m_configuration.deviceTypes.IsEmpty()) |
| 255 | { |
| 256 | LIB_CEC->AddLog(CEC_LOG_ERROR, "no device types given"); |
| 257 | return false; |
| 258 | } |
| 259 | |
| 260 | // check each entry of the list |
| 261 | for (uint8_t iPtr = 0; iPtr < 5; iPtr++) |
| 262 | { |
| 263 | if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_RESERVED) |
| 264 | continue; |
| 265 | |
| 266 | // find an LA for this type |
| 267 | cec_logical_address address(CECDEVICE_UNKNOWN); |
| 268 | if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_RECORDING_DEVICE) |
| 269 | address = AllocateLogicalAddressRecordingDevice(); |
| 270 | if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_TUNER) |
| 271 | address = AllocateLogicalAddressTuner(); |
| 272 | if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE) |
| 273 | address = AllocateLogicalAddressPlaybackDevice(); |
| 274 | if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_AUDIO_SYSTEM) |
| 275 | address = AllocateLogicalAddressAudioSystem(); |
| 276 | |
| 277 | // display an error if no LA could be allocated |
| 278 | if (address == CECDEVICE_UNKNOWN) |
| 279 | { |
| 280 | LIB_CEC->AddLog(CEC_LOG_ERROR, "%s - failed to allocate device '%d', type '%s'", __FUNCTION__, iPtr, ToString(m_configuration.deviceTypes.types[iPtr])); |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | // display the registered LA |
| 285 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - device '%d', type '%s', LA '%X'", __FUNCTION__, iPtr, ToString(m_configuration.deviceTypes.types[iPtr]), address); |
| 286 | m_configuration.logicalAddresses.Set(address); |
| 287 | } |
| 288 | |
| 289 | return true; |
| 290 | } |
| 291 | |
| 292 | cec_logical_address CCECClient::AllocateLogicalAddressRecordingDevice(void) |
| 293 | { |
| 294 | cec_logical_address retVal(CECDEVICE_UNKNOWN); |
| 295 | |
| 296 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'recording device'"); |
| 297 | if (m_processor->TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1)) |
| 298 | retVal = CECDEVICE_RECORDINGDEVICE1; |
| 299 | else if (m_processor->TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2)) |
| 300 | retVal = CECDEVICE_RECORDINGDEVICE2; |
| 301 | else if (m_processor->TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3)) |
| 302 | retVal = CECDEVICE_RECORDINGDEVICE3; |
| 303 | |
| 304 | return retVal; |
| 305 | } |
| 306 | |
| 307 | cec_logical_address CCECClient::AllocateLogicalAddressTuner(void) |
| 308 | { |
| 309 | cec_logical_address retVal(CECDEVICE_UNKNOWN); |
| 310 | |
| 311 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'tuner'"); |
| 312 | if (m_processor->TryLogicalAddress(CECDEVICE_TUNER1)) |
| 313 | retVal = CECDEVICE_TUNER1; |
| 314 | else if (m_processor->TryLogicalAddress(CECDEVICE_TUNER2)) |
| 315 | retVal = CECDEVICE_TUNER2; |
| 316 | else if (m_processor->TryLogicalAddress(CECDEVICE_TUNER3)) |
| 317 | retVal = CECDEVICE_TUNER3; |
| 318 | else if (m_processor->TryLogicalAddress(CECDEVICE_TUNER4)) |
| 319 | retVal = CECDEVICE_TUNER4; |
| 320 | |
| 321 | return retVal; |
| 322 | } |
| 323 | |
| 324 | cec_logical_address CCECClient::AllocateLogicalAddressPlaybackDevice(void) |
| 325 | { |
| 326 | cec_logical_address retVal(CECDEVICE_UNKNOWN); |
| 327 | |
| 328 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'playback device'"); |
| 329 | if (m_processor->TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1)) |
| 330 | retVal = CECDEVICE_PLAYBACKDEVICE1; |
| 331 | else if (m_processor->TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2)) |
| 332 | retVal = CECDEVICE_PLAYBACKDEVICE2; |
| 333 | else if (m_processor->TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3)) |
| 334 | retVal = CECDEVICE_PLAYBACKDEVICE3; |
| 335 | |
| 336 | return retVal; |
| 337 | } |
| 338 | |
| 339 | cec_logical_address CCECClient::AllocateLogicalAddressAudioSystem(void) |
| 340 | { |
| 341 | cec_logical_address retVal(CECDEVICE_UNKNOWN); |
| 342 | |
| 343 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'audiosystem'"); |
| 344 | if (m_processor->TryLogicalAddress(CECDEVICE_AUDIOSYSTEM)) |
| 345 | retVal = CECDEVICE_AUDIOSYSTEM; |
| 346 | |
| 347 | return retVal; |
| 348 | } |
| 349 | |
| 350 | CCECBusDevice *CCECClient::GetDeviceByType(const cec_device_type type) const |
| 351 | { |
| 352 | // get all devices that match our logical addresses |
| 353 | CECDEVICEVEC devices; |
| 354 | m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses); |
| 355 | |
| 356 | // filter the type we need |
| 357 | CCECDeviceMap::FilterType(type, devices); |
| 358 | |
| 359 | return devices.empty() ? |
| 360 | NULL : |
| 361 | *devices.begin(); |
| 362 | } |
| 363 | |
| 364 | bool CCECClient::ChangeDeviceType(const cec_device_type from, const cec_device_type to) |
| 365 | { |
| 366 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "changing device type '%s' into '%s'", ToString(from), ToString(to)); |
| 367 | |
| 368 | CLockObject lock(m_mutex); |
| 369 | |
| 370 | // get the previous device that was allocated |
| 371 | CCECBusDevice *previousDevice = GetDeviceByType(from); |
| 372 | if (!previousDevice) |
| 373 | return false; |
| 374 | |
| 375 | // change the type in the device type list |
| 376 | bool bChanged(false); |
| 377 | for (uint8_t iPtr = 0; iPtr < 5; iPtr++) |
| 378 | { |
| 379 | if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_RESERVED) |
| 380 | continue; |
| 381 | |
| 382 | if (m_configuration.deviceTypes.types[iPtr] == from) |
| 383 | { |
| 384 | bChanged = true; |
| 385 | m_configuration.deviceTypes.types[iPtr] = to; |
| 386 | } |
| 387 | else if (m_configuration.deviceTypes.types[iPtr] == to && bChanged) |
| 388 | { |
| 389 | // ensure that dupes are removed |
| 390 | m_configuration.deviceTypes.types[iPtr] = CEC_DEVICE_TYPE_RESERVED; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | // re-register the client to set the new ackmask |
| 395 | if (!m_processor->RegisterClient(this)) |
| 396 | return false; |
| 397 | |
| 398 | return true; |
| 399 | } |
| 400 | |
| 401 | bool CCECClient::SetLogicalAddress(const cec_logical_address iLogicalAddress) |
| 402 | { |
| 403 | CLockObject lock(m_mutex); |
| 404 | if (GetPrimaryLogicalAdddress() != iLogicalAddress) |
| 405 | { |
| 406 | { |
| 407 | CLockObject lock(m_mutex); |
| 408 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< setting primary logical address to %1x", iLogicalAddress); |
| 409 | m_configuration.logicalAddresses.primary = iLogicalAddress; |
| 410 | m_configuration.logicalAddresses.Set(iLogicalAddress); |
| 411 | } |
| 412 | return m_processor->RegisterClient(this); |
| 413 | } |
| 414 | |
| 415 | return true; |
| 416 | } |
| 417 | |
| 418 | bool CCECClient::Transmit(const cec_command &data) |
| 419 | { |
| 420 | return m_processor ? m_processor->Transmit(data) : false; |
| 421 | } |
| 422 | |
| 423 | bool CCECClient::SendPowerOnDevices(const cec_logical_address address /* = CECDEVICE_TV */) |
| 424 | { |
| 425 | // if the broadcast address if set as destination and the client version >=1.5.0, read the wakeDevices setting |
| 426 | if (address == CECDEVICE_BROADCAST && m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_0) |
| 427 | { |
| 428 | CECDEVICEVEC devices; |
| 429 | m_processor->GetDevices()->GetWakeDevices(m_configuration, devices); |
| 430 | return m_processor->PowerOnDevices(GetPrimaryLogicalAdddress(), devices); |
| 431 | } |
| 432 | |
| 433 | return m_processor->PowerOnDevice(GetPrimaryLogicalAdddress(), address); |
| 434 | } |
| 435 | |
| 436 | bool CCECClient::SendStandbyDevices(const cec_logical_address address /* = CECDEVICE_BROADCAST */) |
| 437 | { |
| 438 | // if the broadcast address if set as destination and the client version >=1.5.0, read the standbyDevices setting |
| 439 | if (address == CECDEVICE_BROADCAST && m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_0) |
| 440 | { |
| 441 | CECDEVICEVEC devices; |
| 442 | m_processor->GetDevices()->GetPowerOffDevices(m_configuration, devices); |
| 443 | return m_processor->StandbyDevices(GetPrimaryLogicalAdddress(), devices); |
| 444 | } |
| 445 | |
| 446 | return m_processor->StandbyDevice(GetPrimaryLogicalAdddress(), address); |
| 447 | } |
| 448 | |
| 449 | bool CCECClient::SendSetActiveSource(const cec_device_type type /* = CEC_DEVICE_TYPE_RESERVED */) |
| 450 | { |
| 451 | // get the devices that are controlled by us |
| 452 | CECDEVICEVEC devices; |
| 453 | m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses); |
| 454 | |
| 455 | // filter out the device that matches the given type |
| 456 | if (type != CEC_DEVICE_TYPE_RESERVED) |
| 457 | CCECDeviceMap::FilterType(type, devices); |
| 458 | |
| 459 | // no devices left, re-fetch the list of devices that are controlled by us |
| 460 | if (devices.empty()) |
| 461 | m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses); |
| 462 | |
| 463 | if (!devices.empty()) |
| 464 | { |
| 465 | // get the first device from the list |
| 466 | CCECBusDevice *device = *devices.begin(); |
| 467 | |
| 468 | // and activate it |
| 469 | if (!m_processor->CECInitialised()) |
| 470 | device->MarkAsActiveSource(); |
| 471 | else if (device->HasValidPhysicalAddress()) |
| 472 | return device->ActivateSource(); |
| 473 | } |
| 474 | |
| 475 | return false; |
| 476 | } |
| 477 | |
| 478 | CCECPlaybackDevice *CCECClient::GetPlaybackDevice(void) |
| 479 | { |
| 480 | CCECPlaybackDevice *device(NULL); |
| 481 | CECDEVICEVEC devices; |
| 482 | |
| 483 | // get the playback devices |
| 484 | m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses); |
| 485 | CCECDeviceMap::FilterType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE, devices); |
| 486 | |
| 487 | // no matches, get the recording devices |
| 488 | if (devices.empty()) |
| 489 | { |
| 490 | m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses); |
| 491 | CCECDeviceMap::FilterType(CEC_DEVICE_TYPE_RECORDING_DEVICE, devices); |
| 492 | } |
| 493 | |
| 494 | // get the first device that matches, and cast it to CCECPlaybackDevice |
| 495 | if (!devices.empty()) |
| 496 | device = (*devices.begin())->AsPlaybackDevice(); |
| 497 | |
| 498 | return device; |
| 499 | } |
| 500 | |
| 501 | cec_logical_address CCECClient::GetPrimaryLogicalAdddress(void) |
| 502 | { |
| 503 | CLockObject lock(m_mutex); |
| 504 | return m_configuration.logicalAddresses.primary; |
| 505 | } |
| 506 | |
| 507 | CCECBusDevice *CCECClient::GetPrimaryDevice(void) |
| 508 | { |
| 509 | return m_processor->GetDevice(GetPrimaryLogicalAdddress()); |
| 510 | } |
| 511 | |
| 512 | bool CCECClient::SendSetDeckControlMode(const cec_deck_control_mode mode, bool bSendUpdate /* = true */) |
| 513 | { |
| 514 | // find a playback device that we control |
| 515 | CCECPlaybackDevice *device = GetPlaybackDevice(); |
| 516 | if (device) |
| 517 | { |
| 518 | // and set the deck control mode if there is a match |
| 519 | device->SetDeckControlMode(mode); |
| 520 | if (bSendUpdate) |
| 521 | return device->TransmitDeckStatus(CECDEVICE_TV); |
| 522 | return true; |
| 523 | } |
| 524 | |
| 525 | // no match |
| 526 | return false; |
| 527 | } |
| 528 | |
| 529 | bool CCECClient::SendSetDeckInfo(const cec_deck_info info, bool bSendUpdate /* = true */) |
| 530 | { |
| 531 | // find a playback device that we control |
| 532 | CCECPlaybackDevice *device = GetPlaybackDevice(); |
| 533 | if (device) |
| 534 | { |
| 535 | // and set the deck status if there is a match |
| 536 | device->SetDeckStatus(info); |
| 537 | if (bSendUpdate) |
| 538 | return device->AsPlaybackDevice()->TransmitDeckStatus(CECDEVICE_TV); |
| 539 | return true; |
| 540 | } |
| 541 | |
| 542 | // no match |
| 543 | return false; |
| 544 | } |
| 545 | |
| 546 | bool CCECClient::SendSetMenuState(const cec_menu_state state, bool bSendUpdate /* = true */) |
| 547 | { |
| 548 | CECDEVICEVEC devices; |
| 549 | |
| 550 | // set the menu state for all devices that are controlled by us |
| 551 | m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses); |
| 552 | for (CECDEVICEVEC::iterator it = devices.begin(); it != devices.end(); it++) |
| 553 | { |
| 554 | (*it)->SetMenuState(state); |
| 555 | if (bSendUpdate) |
| 556 | (*it)->TransmitMenuState(CECDEVICE_TV); |
| 557 | } |
| 558 | |
| 559 | return true; |
| 560 | } |
| 561 | |
| 562 | bool CCECClient::SendSetInactiveView(void) |
| 563 | { |
| 564 | CECDEVICEVEC devices; |
| 565 | |
| 566 | // mark all devices that are controlled by us as inactive source |
| 567 | m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses); |
| 568 | for (CECDEVICEVEC::iterator it = devices.begin(); it != devices.end(); it++) |
| 569 | { |
| 570 | if ((*it)->IsActiveSource()) |
| 571 | { |
| 572 | (*it)->MarkAsInactiveSource(); |
| 573 | return (*it)->TransmitInactiveSource(); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | return true; |
| 578 | } |
| 579 | |
| 580 | bool CCECClient::SendSetOSDString(const cec_logical_address iLogicalAddress, const cec_display_control duration, const char *strMessage) |
| 581 | { |
| 582 | CCECBusDevice *primary = GetPrimaryDevice(); |
| 583 | if (primary) |
| 584 | return primary->TransmitOSDString(iLogicalAddress, duration, strMessage); |
| 585 | |
| 586 | return false; |
| 587 | } |
| 588 | |
| 589 | cec_version CCECClient::GetDeviceCecVersion(const cec_logical_address iAddress) |
| 590 | { |
| 591 | CCECBusDevice *device = m_processor->GetDevice(iAddress); |
| 592 | if (device) |
| 593 | return device->GetCecVersion(GetPrimaryLogicalAdddress()); |
| 594 | return CEC_VERSION_UNKNOWN; |
| 595 | } |
| 596 | |
| 597 | bool CCECClient::GetDeviceMenuLanguage(const cec_logical_address iAddress, cec_menu_language &language) |
| 598 | { |
| 599 | CCECBusDevice *device = m_processor->GetDevice(iAddress); |
| 600 | if (device) |
| 601 | { |
| 602 | language = device->GetMenuLanguage(GetPrimaryLogicalAdddress()); |
| 603 | return (strcmp(language.language, "???") != 0); |
| 604 | } |
| 605 | return false; |
| 606 | } |
| 607 | |
| 608 | cec_osd_name CCECClient::GetDeviceOSDName(const cec_logical_address iAddress) |
| 609 | { |
| 610 | cec_osd_name retVal; |
| 611 | retVal.device = iAddress; |
| 612 | retVal.name[0] = 0; |
| 613 | |
| 614 | CCECBusDevice *device = m_processor->GetDevice(iAddress); |
| 615 | if (device) |
| 616 | { |
| 617 | CStdString strOSDName = device->GetOSDName(GetPrimaryLogicalAdddress()); |
| 618 | snprintf(retVal.name, sizeof(retVal.name), "%s", strOSDName.c_str()); |
| 619 | retVal.device = iAddress; |
| 620 | } |
| 621 | |
| 622 | return retVal; |
| 623 | } |
| 624 | |
| 625 | uint16_t CCECClient::GetDevicePhysicalAddress(const cec_logical_address iAddress) |
| 626 | { |
| 627 | CCECBusDevice *device = m_processor->GetDevice(iAddress); |
| 628 | if (device) |
| 629 | return device->GetPhysicalAddress(GetPrimaryLogicalAdddress()); |
| 630 | return CEC_INVALID_PHYSICAL_ADDRESS; |
| 631 | } |
| 632 | |
| 633 | cec_power_status CCECClient::GetDevicePowerStatus(const cec_logical_address iAddress) |
| 634 | { |
| 635 | CCECBusDevice *device = m_processor->GetDevice(iAddress); |
| 636 | if (device) |
| 637 | return device->GetPowerStatus(GetPrimaryLogicalAdddress()); |
| 638 | return CEC_POWER_STATUS_UNKNOWN; |
| 639 | } |
| 640 | |
| 641 | uint64_t CCECClient::GetDeviceVendorId(const cec_logical_address iAddress) |
| 642 | { |
| 643 | CCECBusDevice *device = m_processor->GetDevice(iAddress); |
| 644 | if (device) |
| 645 | return device->GetVendorId(GetPrimaryLogicalAdddress()); |
| 646 | return CEC_VENDOR_UNKNOWN; |
| 647 | } |
| 648 | |
| 649 | uint8_t CCECClient::SendVolumeUp(bool bSendRelease /* = true */) |
| 650 | { |
| 651 | CCECBusDevice *device = GetPrimaryDevice(); |
| 652 | CCECAudioSystem *audio = m_processor->GetAudioSystem(); |
| 653 | |
| 654 | return device && audio && audio->IsPresent() ? |
| 655 | audio->VolumeUp(device->GetLogicalAddress(), bSendRelease) : |
| 656 | (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN; |
| 657 | } |
| 658 | |
| 659 | uint8_t CCECClient::SendVolumeDown(bool bSendRelease /* = true */) |
| 660 | { |
| 661 | CCECBusDevice *device = GetPrimaryDevice(); |
| 662 | CCECAudioSystem *audio = m_processor->GetAudioSystem(); |
| 663 | |
| 664 | return device && audio && audio->IsPresent() ? |
| 665 | audio->VolumeDown(device->GetLogicalAddress(), bSendRelease) : |
| 666 | (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN; |
| 667 | } |
| 668 | |
| 669 | uint8_t CCECClient::SendMuteAudio(void) |
| 670 | { |
| 671 | CCECBusDevice *device = GetPrimaryDevice(); |
| 672 | CCECAudioSystem *audio = m_processor->GetAudioSystem(); |
| 673 | |
| 674 | return device && audio && audio->IsPresent() ? |
| 675 | audio->MuteAudio(device->GetLogicalAddress()) : |
| 676 | (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN; |
| 677 | } |
| 678 | |
| 679 | bool CCECClient::SendKeypress(const cec_logical_address iDestination, const cec_user_control_code key, bool bWait /* = true */) |
| 680 | { |
| 681 | CCECBusDevice *dest = m_processor->GetDevice(iDestination); |
| 682 | |
| 683 | return dest ? |
| 684 | dest->TransmitKeypress(GetPrimaryLogicalAdddress(), key, bWait) : |
| 685 | false; |
| 686 | } |
| 687 | |
| 688 | bool CCECClient::SendKeyRelease(const cec_logical_address iDestination, bool bWait /* = true */) |
| 689 | { |
| 690 | CCECBusDevice *dest = m_processor->GetDevice(iDestination); |
| 691 | |
| 692 | return dest ? |
| 693 | dest->TransmitKeyRelease(GetPrimaryLogicalAdddress(), bWait) : |
| 694 | false; |
| 695 | } |
| 696 | |
| 697 | bool CCECClient::GetCurrentConfiguration(libcec_configuration &configuration) |
| 698 | { |
| 699 | CLockObject lock(m_mutex); |
| 700 | |
| 701 | // client version 1.5.0 |
| 702 | snprintf(configuration.strDeviceName, 13, "%s", m_configuration.strDeviceName); |
| 703 | configuration.deviceTypes = m_configuration.deviceTypes; |
| 704 | configuration.bAutodetectAddress = m_configuration.bAutodetectAddress; |
| 705 | configuration.iPhysicalAddress = m_configuration.iPhysicalAddress; |
| 706 | configuration.baseDevice = m_configuration.baseDevice; |
| 707 | configuration.iHDMIPort = m_configuration.iHDMIPort; |
| 708 | configuration.clientVersion = m_configuration.clientVersion; |
| 709 | configuration.serverVersion = m_configuration.serverVersion; |
| 710 | configuration.tvVendor = m_configuration.tvVendor; |
| 711 | |
| 712 | configuration.bGetSettingsFromROM = m_configuration.bGetSettingsFromROM; |
| 713 | configuration.bUseTVMenuLanguage = m_configuration.bUseTVMenuLanguage; |
| 714 | configuration.bActivateSource = m_configuration.bActivateSource; |
| 715 | configuration.wakeDevices = m_configuration.wakeDevices; |
| 716 | configuration.powerOffDevices = m_configuration.powerOffDevices; |
| 717 | configuration.bPowerOffScreensaver = m_configuration.bPowerOffScreensaver; |
| 718 | configuration.bPowerOffOnStandby = m_configuration.bPowerOffOnStandby; |
| 719 | |
| 720 | // client version 1.5.1 |
| 721 | if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_1) |
| 722 | configuration.bSendInactiveSource = m_configuration.bSendInactiveSource; |
| 723 | |
| 724 | // client version 1.5.3 |
| 725 | if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_3) |
| 726 | configuration.logicalAddresses = m_configuration.logicalAddresses; |
| 727 | |
| 728 | // client version 1.6.0 |
| 729 | if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_0) |
| 730 | { |
| 731 | configuration.iFirmwareVersion = m_configuration.iFirmwareVersion; |
| 732 | configuration.bPowerOffDevicesOnStandby = m_configuration.bPowerOffDevicesOnStandby; |
| 733 | configuration.bShutdownOnStandby = m_configuration.bShutdownOnStandby; |
| 734 | } |
| 735 | |
| 736 | // client version 1.6.2 |
| 737 | if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_2) |
| 738 | { |
| 739 | memcpy(configuration.strDeviceLanguage, m_configuration.strDeviceLanguage, 3); |
| 740 | configuration.iFirmwareBuildDate = m_configuration.iFirmwareBuildDate; |
| 741 | } |
| 742 | |
| 743 | // client version 1.6.3 |
| 744 | if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_3) |
| 745 | { |
| 746 | configuration.bMonitorOnly = m_configuration.bMonitorOnly; |
| 747 | } |
| 748 | |
| 749 | return true; |
| 750 | } |
| 751 | |
| 752 | bool CCECClient::SetConfiguration(const libcec_configuration &configuration) |
| 753 | { |
| 754 | bool bIsRunning(m_processor && m_processor->CECInitialised()); |
| 755 | CCECBusDevice *primary = bIsRunning ? GetPrimaryDevice() : NULL; |
| 756 | uint16_t iPA = primary ? primary->GetCurrentPhysicalAddress() : CEC_INVALID_PHYSICAL_ADDRESS; |
| 757 | |
| 758 | // update the callbacks |
| 759 | if (configuration.callbacks) |
| 760 | EnableCallbacks(configuration.callbackParam, configuration.callbacks); |
| 761 | |
| 762 | // update the client version |
| 763 | SetClientVersion((cec_client_version)configuration.clientVersion); |
| 764 | |
| 765 | // update the OSD name |
| 766 | CStdString strOSDName(configuration.strDeviceName); |
| 767 | SetOSDName(strOSDName); |
| 768 | |
| 769 | // update the TV vendor override |
| 770 | SetTVVendorOverride((cec_vendor_id)configuration.tvVendor); |
| 771 | |
| 772 | // just copy these |
| 773 | { |
| 774 | CLockObject lock(m_mutex); |
| 775 | m_configuration.bUseTVMenuLanguage = configuration.bUseTVMenuLanguage; |
| 776 | m_configuration.bActivateSource = configuration.bActivateSource; |
| 777 | m_configuration.bGetSettingsFromROM = configuration.bGetSettingsFromROM; |
| 778 | m_configuration.wakeDevices = configuration.wakeDevices; |
| 779 | m_configuration.powerOffDevices = configuration.powerOffDevices; |
| 780 | m_configuration.bPowerOffScreensaver = configuration.bPowerOffScreensaver; |
| 781 | m_configuration.bPowerOffOnStandby = configuration.bPowerOffOnStandby; |
| 782 | |
| 783 | // client version 1.5.1 |
| 784 | if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_1) |
| 785 | m_configuration.bSendInactiveSource = configuration.bSendInactiveSource; |
| 786 | |
| 787 | // client version 1.6.0 |
| 788 | if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_0) |
| 789 | { |
| 790 | m_configuration.bPowerOffDevicesOnStandby = configuration.bPowerOffDevicesOnStandby; |
| 791 | m_configuration.bShutdownOnStandby = configuration.bShutdownOnStandby; |
| 792 | } |
| 793 | |
| 794 | // client version 1.6.2 |
| 795 | if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_2) |
| 796 | { |
| 797 | memcpy(m_configuration.strDeviceLanguage, configuration.strDeviceLanguage, 3); |
| 798 | } |
| 799 | |
| 800 | // client version 1.6.3 |
| 801 | if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_3) |
| 802 | { |
| 803 | m_configuration.bMonitorOnly = configuration.bMonitorOnly; |
| 804 | } |
| 805 | |
| 806 | // ensure that there is at least 1 device type set |
| 807 | if (m_configuration.deviceTypes.IsEmpty()) |
| 808 | m_configuration.deviceTypes.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE); |
| 809 | } |
| 810 | |
| 811 | bool bNeedReinit(false); |
| 812 | |
| 813 | // device types |
| 814 | if (SetDeviceTypes(configuration.deviceTypes)) |
| 815 | { |
| 816 | // the device type changed. just copy the rest, and re-register |
| 817 | { |
| 818 | CLockObject lock(m_mutex); |
| 819 | m_configuration.iPhysicalAddress = configuration.iPhysicalAddress; |
| 820 | m_configuration.baseDevice = configuration.baseDevice; |
| 821 | m_configuration.iHDMIPort = configuration.iHDMIPort; |
| 822 | bNeedReinit = true; |
| 823 | } |
| 824 | } |
| 825 | else |
| 826 | { |
| 827 | // set the physical address |
| 828 | SetPhysicalAddress(configuration); |
| 829 | } |
| 830 | |
| 831 | m_processor->PersistConfiguration(m_configuration); |
| 832 | |
| 833 | if (!primary) |
| 834 | primary = GetPrimaryDevice(); |
| 835 | |
| 836 | if (bNeedReinit || !primary || primary->GetCurrentPhysicalAddress() != iPA) |
| 837 | { |
| 838 | // PA or device type changed |
| 839 | m_processor->RegisterClient(this); |
| 840 | } |
| 841 | else if (primary && configuration.bActivateSource == 1 && bIsRunning && !primary->IsActiveSource()) |
| 842 | { |
| 843 | // activate the source if we're not already the active source |
| 844 | primary->ActivateSource(); |
| 845 | } |
| 846 | |
| 847 | return true; |
| 848 | } |
| 849 | |
| 850 | void CCECClient::AddCommand(const cec_command &command) |
| 851 | { |
| 852 | CLockObject lock(m_mutex); |
| 853 | |
| 854 | LIB_CEC->AddLog(CEC_LOG_NOTICE, ">> %s (%X) -> %s (%X): %s (%2X)", ToString(command.initiator), command.initiator, ToString(command.destination), command.destination, ToString(command.opcode), command.opcode); |
| 855 | |
| 856 | if (m_configuration.callbacks && m_configuration.callbacks->CBCecCommand) |
| 857 | m_configuration.callbacks->CBCecCommand(m_configuration.callbackParam, command); |
| 858 | else if (!m_commandBuffer.Push(command)) |
| 859 | LIB_CEC->AddLog(CEC_LOG_WARNING, "command buffer is full"); |
| 860 | } |
| 861 | |
| 862 | int CCECClient::MenuStateChanged(const cec_menu_state newState) |
| 863 | { |
| 864 | CLockObject lock(m_mutex); |
| 865 | |
| 866 | LIB_CEC->AddLog(CEC_LOG_NOTICE, ">> %s: %s", ToString(CEC_OPCODE_MENU_REQUEST), ToString(newState)); |
| 867 | |
| 868 | if (m_configuration.callbacks && |
| 869 | m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_2 && |
| 870 | m_configuration.callbacks->CBCecMenuStateChanged) |
| 871 | return m_configuration.callbacks->CBCecMenuStateChanged(m_configuration.callbackParam, newState); |
| 872 | |
| 873 | return 0; |
| 874 | } |
| 875 | |
| 876 | void CCECClient::Alert(const libcec_alert type, const libcec_parameter ¶m) |
| 877 | { |
| 878 | CLockObject lock(m_mutex); |
| 879 | |
| 880 | if (m_configuration.callbacks && |
| 881 | m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_0 && |
| 882 | m_configuration.callbacks->CBCecAlert) |
| 883 | m_configuration.callbacks->CBCecAlert(m_configuration.callbackParam, type, param); |
| 884 | } |
| 885 | |
| 886 | void CCECClient::AddLog(const cec_log_message &message) |
| 887 | { |
| 888 | CLockObject lock(m_logMutex); |
| 889 | if (m_configuration.callbacks && m_configuration.callbacks->CBCecLogMessage) |
| 890 | m_configuration.callbacks->CBCecLogMessage(m_configuration.callbackParam, message); |
| 891 | else |
| 892 | m_logBuffer.Push(message); |
| 893 | } |
| 894 | |
| 895 | void CCECClient::AddKey(void) |
| 896 | { |
| 897 | CLockObject lock(m_mutex); |
| 898 | |
| 899 | if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN) |
| 900 | { |
| 901 | cec_keypress key; |
| 902 | |
| 903 | key.duration = (unsigned int) (GetTimeMs() - m_buttontime); |
| 904 | key.keycode = m_iCurrentButton; |
| 905 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "key released: %1x", key.keycode); |
| 906 | |
| 907 | if (m_configuration.callbacks && m_configuration.callbacks->CBCecKeyPress) |
| 908 | m_configuration.callbacks->CBCecKeyPress(m_configuration.callbackParam, key); |
| 909 | else |
| 910 | m_keyBuffer.Push(key); |
| 911 | m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN; |
| 912 | } |
| 913 | |
| 914 | m_buttontime = 0; |
| 915 | } |
| 916 | |
| 917 | void CCECClient::AddKey(const cec_keypress &key) |
| 918 | { |
| 919 | CLockObject lock(m_mutex); |
| 920 | |
| 921 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "key pressed: %1x", key.keycode); |
| 922 | |
| 923 | if (m_configuration.callbacks && m_configuration.callbacks->CBCecKeyPress) |
| 924 | m_configuration.callbacks->CBCecKeyPress(m_configuration.callbackParam, key); |
| 925 | else |
| 926 | m_keyBuffer.Push(key); |
| 927 | |
| 928 | m_iCurrentButton = key.duration > 0 ? CEC_USER_CONTROL_CODE_UNKNOWN : key.keycode; |
| 929 | m_buttontime = key.duration > 0 ? 0 : GetTimeMs(); |
| 930 | } |
| 931 | |
| 932 | void CCECClient::SetCurrentButton(const cec_user_control_code iButtonCode) |
| 933 | { |
| 934 | // push a keypress to the buffer with 0 duration and another with the duration set when released |
| 935 | cec_keypress key; |
| 936 | key.duration = 0; |
| 937 | key.keycode = iButtonCode; |
| 938 | |
| 939 | AddKey(key); |
| 940 | } |
| 941 | |
| 942 | void CCECClient::CheckKeypressTimeout(void) |
| 943 | { |
| 944 | if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN && GetTimeMs() - m_buttontime > CEC_BUTTON_TIMEOUT) |
| 945 | { |
| 946 | AddKey(); |
| 947 | m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN; |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | void CCECClient::ConfigurationChanged(const libcec_configuration &config) |
| 952 | { |
| 953 | CLockObject lock(m_mutex); |
| 954 | |
| 955 | if (m_configuration.callbacks && |
| 956 | m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_0 && |
| 957 | m_configuration.callbacks->CBCecConfigurationChanged && |
| 958 | m_processor->CECInitialised()) |
| 959 | m_configuration.callbacks->CBCecConfigurationChanged(m_configuration.callbackParam, config); |
| 960 | } |
| 961 | |
| 962 | bool CCECClient::EnableCallbacks(void *cbParam, ICECCallbacks *callbacks) |
| 963 | { |
| 964 | CLockObject lock(m_mutex); |
| 965 | m_configuration.callbackParam = cbParam; |
| 966 | m_configuration.callbacks = callbacks; |
| 967 | return true; |
| 968 | } |
| 969 | |
| 970 | bool CCECClient::PingAdapter(void) |
| 971 | { |
| 972 | return m_processor ? m_processor->PingAdapter() : false; |
| 973 | } |
| 974 | |
| 975 | bool CCECClient::GetNextLogMessage(cec_log_message *message) |
| 976 | { |
| 977 | return (m_logBuffer.Pop(*message)); |
| 978 | } |
| 979 | |
| 980 | bool CCECClient::GetNextKeypress(cec_keypress *key) |
| 981 | { |
| 982 | return m_keyBuffer.Pop(*key); |
| 983 | } |
| 984 | |
| 985 | bool CCECClient::GetNextCommand(cec_command *command) |
| 986 | { |
| 987 | return m_commandBuffer.Pop(*command); |
| 988 | } |
| 989 | |
| 990 | CStdString CCECClient::GetConnectionInfo(void) |
| 991 | { |
| 992 | CStdString strLog; |
| 993 | strLog.Format("libCEC version = %s, client version = %s, firmware version = %d", ToString((cec_server_version)m_configuration.serverVersion), ToString((cec_client_version)m_configuration.clientVersion), m_configuration.iFirmwareVersion); |
| 994 | if (m_configuration.iFirmwareBuildDate != CEC_FW_BUILD_UNKNOWN) |
| 995 | { |
| 996 | time_t buildTime = (time_t)m_configuration.iFirmwareBuildDate; |
| 997 | strLog.AppendFormat(", firmware build date: %s", asctime(gmtime(&buildTime))); |
| 998 | strLog = strLog.Left((int)strLog.length() - 1); // strip \n added by asctime |
| 999 | strLog.append(" +0000"); |
| 1000 | } |
| 1001 | |
| 1002 | // log the addresses that are being used |
| 1003 | if (!m_configuration.logicalAddresses.IsEmpty()) |
| 1004 | { |
| 1005 | strLog.append(", logical address(es) = "); |
| 1006 | CECDEVICEVEC devices; |
| 1007 | m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses); |
| 1008 | for (CECDEVICEVEC::iterator it = devices.begin(); it != devices.end(); it++) |
| 1009 | strLog.AppendFormat("%s (%X) ", (*it)->GetLogicalAddressName(), (*it)->GetLogicalAddress()); |
| 1010 | } |
| 1011 | |
| 1012 | if (!CLibCEC::IsValidPhysicalAddress(m_configuration.iPhysicalAddress)) |
| 1013 | strLog.AppendFormat(", base device: %s (%X), HDMI port number: %d", ToString(m_configuration.baseDevice), m_configuration.baseDevice, m_configuration.iHDMIPort); |
| 1014 | else |
| 1015 | strLog.AppendFormat(", physical address: %04x", m_configuration.iPhysicalAddress); |
| 1016 | |
| 1017 | return strLog; |
| 1018 | } |
| 1019 | |
| 1020 | void CCECClient::SetTVVendorOverride(const cec_vendor_id id) |
| 1021 | { |
| 1022 | { |
| 1023 | CLockObject lock(m_mutex); |
| 1024 | m_configuration.tvVendor = id; |
| 1025 | } |
| 1026 | |
| 1027 | if (id != CEC_VENDOR_UNKNOWN) |
| 1028 | { |
| 1029 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - vendor id '%s'", __FUNCTION__, ToString(id)); |
| 1030 | |
| 1031 | CCECBusDevice *tv = m_processor ? m_processor->GetTV() : NULL; |
| 1032 | if (tv) |
| 1033 | tv->SetVendorId((uint64_t)id); |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | cec_vendor_id CCECClient::GetTVVendorOverride(void) |
| 1038 | { |
| 1039 | CLockObject lock(m_mutex); |
| 1040 | return (cec_vendor_id)m_configuration.tvVendor; |
| 1041 | } |
| 1042 | |
| 1043 | void CCECClient::SetOSDName(const CStdString &strDeviceName) |
| 1044 | { |
| 1045 | { |
| 1046 | CLockObject lock(m_mutex); |
| 1047 | snprintf(m_configuration.strDeviceName, 13, "%s", strDeviceName.c_str()); |
| 1048 | } |
| 1049 | |
| 1050 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - using OSD name '%s'", __FUNCTION__, strDeviceName.c_str()); |
| 1051 | |
| 1052 | CCECBusDevice *primary = GetPrimaryDevice(); |
| 1053 | if (primary && !primary->GetCurrentOSDName().Equals(strDeviceName)) |
| 1054 | { |
| 1055 | primary->SetOSDName(strDeviceName); |
| 1056 | if (m_processor && m_processor->CECInitialised()) |
| 1057 | primary->TransmitOSDName(CECDEVICE_TV); |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | CStdString CCECClient::GetOSDName(void) |
| 1062 | { |
| 1063 | CLockObject lock(m_mutex); |
| 1064 | CStdString strOSDName(m_configuration.strDeviceName); |
| 1065 | return strOSDName; |
| 1066 | } |
| 1067 | |
| 1068 | void CCECClient::SetWakeDevices(const cec_logical_addresses &addresses) |
| 1069 | { |
| 1070 | CLockObject lock(m_mutex); |
| 1071 | m_configuration.wakeDevices = addresses; |
| 1072 | } |
| 1073 | |
| 1074 | cec_logical_addresses CCECClient::GetWakeDevices(void) |
| 1075 | { |
| 1076 | CLockObject lock(m_mutex); |
| 1077 | return m_configuration.wakeDevices; |
| 1078 | } |
| 1079 | |
| 1080 | bool CCECClient::AutodetectPhysicalAddress(void) |
| 1081 | { |
| 1082 | bool bPhysicalAutodetected(false); |
| 1083 | uint16_t iPhysicalAddress = m_processor ? m_processor->GetDetectedPhysicalAddress() : CEC_INVALID_PHYSICAL_ADDRESS; |
| 1084 | |
| 1085 | if (CLibCEC::IsValidPhysicalAddress(iPhysicalAddress)) |
| 1086 | { |
| 1087 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - autodetected physical address '%04X'", __FUNCTION__, iPhysicalAddress); |
| 1088 | |
| 1089 | CLockObject lock(m_mutex); |
| 1090 | m_configuration.iPhysicalAddress = iPhysicalAddress; |
| 1091 | m_configuration.iHDMIPort = CEC_HDMI_PORTNUMBER_NONE; |
| 1092 | m_configuration.baseDevice = CECDEVICE_UNKNOWN; |
| 1093 | bPhysicalAutodetected = true; |
| 1094 | } |
| 1095 | |
| 1096 | SetDevicePhysicalAddress(iPhysicalAddress); |
| 1097 | |
| 1098 | return bPhysicalAutodetected; |
| 1099 | } |
| 1100 | |
| 1101 | void CCECClient::SetClientVersion(const cec_client_version version) |
| 1102 | { |
| 1103 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - using client version '%s'", __FUNCTION__, ToString(version)); |
| 1104 | |
| 1105 | CLockObject lock(m_mutex); |
| 1106 | m_configuration.clientVersion = (uint32_t)version; |
| 1107 | } |
| 1108 | |
| 1109 | cec_client_version CCECClient::GetClientVersion(void) |
| 1110 | { |
| 1111 | CLockObject lock(m_mutex); |
| 1112 | return (cec_client_version)m_configuration.clientVersion; |
| 1113 | } |
| 1114 | |
| 1115 | bool CCECClient::SetDeviceTypes(const cec_device_type_list &deviceTypes) |
| 1116 | { |
| 1117 | bool bNeedReinit(false); |
| 1118 | |
| 1119 | { |
| 1120 | CLockObject lock(m_mutex); |
| 1121 | bNeedReinit = m_processor && m_processor->CECInitialised() && |
| 1122 | (m_configuration.deviceTypes != deviceTypes); |
| 1123 | m_configuration.deviceTypes = deviceTypes; |
| 1124 | } |
| 1125 | |
| 1126 | if (bNeedReinit) |
| 1127 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - using primary device type '%s'", __FUNCTION__, ToString(deviceTypes[0])); |
| 1128 | |
| 1129 | return bNeedReinit; |
| 1130 | } |
| 1131 | |
| 1132 | cec_device_type_list CCECClient::GetDeviceTypes(void) |
| 1133 | { |
| 1134 | cec_device_type_list retVal; |
| 1135 | CLockObject lock(m_mutex); |
| 1136 | retVal = m_configuration.deviceTypes; |
| 1137 | return retVal; |
| 1138 | } |
| 1139 | |
| 1140 | bool CCECClient::SetDevicePhysicalAddress(const uint16_t iPhysicalAddress) |
| 1141 | { |
| 1142 | if (!CLibCEC::IsValidPhysicalAddress(iPhysicalAddress)) |
| 1143 | return false; |
| 1144 | |
| 1145 | // reconfigure all devices |
| 1146 | cec_logical_address reactivateSource(CECDEVICE_UNKNOWN); |
| 1147 | CECDEVICEVEC devices; |
| 1148 | m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses); |
| 1149 | for (CECDEVICEVEC::iterator it = devices.begin(); it != devices.end(); it++) |
| 1150 | { |
| 1151 | // if this device was the active source, reactivate it afterwards |
| 1152 | if ((*it)->IsActiveSource()) |
| 1153 | reactivateSource = (*it)->GetLogicalAddress(); |
| 1154 | |
| 1155 | // mark the device as inactive source |
| 1156 | if (IsInitialised()) |
| 1157 | (*it)->MarkAsInactiveSource(); |
| 1158 | |
| 1159 | // set the new physical address |
| 1160 | (*it)->SetPhysicalAddress(iPhysicalAddress); |
| 1161 | |
| 1162 | // and transmit it |
| 1163 | if (IsInitialised()) |
| 1164 | (*it)->TransmitPhysicalAddress(); |
| 1165 | } |
| 1166 | |
| 1167 | // reactivate the previous active source |
| 1168 | if (reactivateSource != CECDEVICE_UNKNOWN && |
| 1169 | m_processor->CECInitialised() && |
| 1170 | IsInitialised()) |
| 1171 | { |
| 1172 | CCECBusDevice *device = m_processor->GetDevice(reactivateSource); |
| 1173 | if (device) |
| 1174 | device->ActivateSource(); |
| 1175 | } |
| 1176 | |
| 1177 | return true; |
| 1178 | } |
| 1179 | |
| 1180 | bool CCECClient::SwitchMonitoring(bool bEnable) |
| 1181 | { |
| 1182 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "== %s monitoring mode ==", bEnable ? "enabling" : "disabling"); |
| 1183 | |
| 1184 | if (m_processor) |
| 1185 | { |
| 1186 | if (bEnable) |
| 1187 | return m_processor->UnregisterClient(this); |
| 1188 | else |
| 1189 | { |
| 1190 | m_configuration.bMonitorOnly = false; |
| 1191 | return m_processor->RegisterClient(this); |
| 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | return false; |
| 1196 | } |
| 1197 | |
| 1198 | bool CCECClient::PollDevice(const cec_logical_address iAddress) |
| 1199 | { |
| 1200 | // try to find the primary device |
| 1201 | CCECBusDevice *primary = GetPrimaryDevice(); |
| 1202 | // poll the destination, with the primary as source |
| 1203 | if (primary) |
| 1204 | return primary->TransmitPoll(iAddress); |
| 1205 | |
| 1206 | return m_processor ? m_processor->PollDevice(iAddress) : false; |
| 1207 | } |
| 1208 | |
| 1209 | cec_logical_addresses CCECClient::GetActiveDevices(void) |
| 1210 | { |
| 1211 | CECDEVICEVEC activeDevices; |
| 1212 | if (m_processor) |
| 1213 | m_processor->GetDevices()->GetActive(activeDevices); |
| 1214 | return CCECDeviceMap::ToLogicalAddresses(activeDevices); |
| 1215 | } |
| 1216 | |
| 1217 | bool CCECClient::IsActiveDevice(const cec_logical_address iAddress) |
| 1218 | { |
| 1219 | cec_logical_addresses activeDevices = GetActiveDevices(); |
| 1220 | return activeDevices.IsSet(iAddress); |
| 1221 | } |
| 1222 | |
| 1223 | bool CCECClient::IsActiveDeviceType(const cec_device_type type) |
| 1224 | { |
| 1225 | CECDEVICEVEC activeDevices; |
| 1226 | if (m_processor) |
| 1227 | m_processor->GetDevices()->GetActive(activeDevices); |
| 1228 | CCECDeviceMap::FilterType(type, activeDevices); |
| 1229 | return !activeDevices.empty(); |
| 1230 | } |
| 1231 | |
| 1232 | cec_logical_address CCECClient::GetActiveSource(void) |
| 1233 | { |
| 1234 | return m_processor ? m_processor->GetActiveSource() : CECDEVICE_UNKNOWN; |
| 1235 | } |
| 1236 | |
| 1237 | bool CCECClient::IsActiveSource(const cec_logical_address iAddress) |
| 1238 | { |
| 1239 | return m_processor ? m_processor->IsActiveSource(iAddress) : false; |
| 1240 | } |
| 1241 | |
| 1242 | bool CCECClient::SetStreamPath(const cec_logical_address iAddress) |
| 1243 | { |
| 1244 | uint16_t iPhysicalAddress = GetDevicePhysicalAddress(iAddress); |
| 1245 | if (iPhysicalAddress != CEC_INVALID_PHYSICAL_ADDRESS) |
| 1246 | return SetStreamPath(iPhysicalAddress); |
| 1247 | return false; |
| 1248 | } |
| 1249 | |
| 1250 | bool CCECClient::SetStreamPath(const uint16_t iPhysicalAddress) |
| 1251 | { |
| 1252 | return m_processor ? m_processor->SetStreamPath(iPhysicalAddress) : false; |
| 1253 | } |
| 1254 | |
| 1255 | cec_logical_addresses CCECClient::GetLogicalAddresses(void) |
| 1256 | { |
| 1257 | cec_logical_addresses addresses; |
| 1258 | CLockObject lock(m_mutex); |
| 1259 | addresses = m_configuration.logicalAddresses; |
| 1260 | return addresses; |
| 1261 | } |
| 1262 | |
| 1263 | bool CCECClient::CanPersistConfiguration(void) |
| 1264 | { |
| 1265 | return m_processor ? m_processor->CanPersistConfiguration() : false; |
| 1266 | } |
| 1267 | |
| 1268 | bool CCECClient::PersistConfiguration(const libcec_configuration &configuration) |
| 1269 | { |
| 1270 | return m_processor ? m_processor->PersistConfiguration(configuration) : false; |
| 1271 | } |
| 1272 | |
| 1273 | void CCECClient::RescanActiveDevices(void) |
| 1274 | { |
| 1275 | if (m_processor) |
| 1276 | m_processor->RescanActiveDevices(); |
| 1277 | } |
| 1278 | |
| 1279 | bool CCECClient::IsLibCECActiveSource(void) |
| 1280 | { |
| 1281 | bool bReturn(false); |
| 1282 | if (m_processor) |
| 1283 | { |
| 1284 | cec_logical_address activeSource = m_processor->GetActiveSource(); |
| 1285 | CCECBusDevice *device = m_processor->GetDevice(activeSource); |
| 1286 | if (device) |
| 1287 | bReturn = device->IsHandledByLibCEC(); |
| 1288 | } |
| 1289 | return bReturn; |
| 1290 | } |