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