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