| 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 "CECCommandHandler.h" |
| 34 | #include "../devices/CECBusDevice.h" |
| 35 | #include "../devices/CECAudioSystem.h" |
| 36 | #include "../devices/CECPlaybackDevice.h" |
| 37 | #include "../CECProcessor.h" |
| 38 | |
| 39 | using namespace CEC; |
| 40 | using namespace std; |
| 41 | |
| 42 | CCECCommandHandler::CCECCommandHandler(CCECBusDevice *busDevice) : |
| 43 | m_busDevice(busDevice), |
| 44 | m_processor(m_busDevice->GetProcessor()), |
| 45 | m_iTransmitTimeout(1000) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | CCECCommandHandler::~CCECCommandHandler(void) |
| 50 | { |
| 51 | m_condition.Broadcast(); |
| 52 | } |
| 53 | |
| 54 | bool CCECCommandHandler::HandleCommand(const cec_command &command) |
| 55 | { |
| 56 | bool bHandled(true), bHandlerChanged(false); |
| 57 | |
| 58 | CStdString strLog; |
| 59 | strLog.Format(">> %s (%X) -> %s (%X): %s (%2X)", m_processor->ToString(command.initiator), command.initiator, m_processor->ToString(command.destination), command.destination, m_processor->ToString(command.opcode), command.opcode); |
| 60 | m_busDevice->AddLog(CEC_LOG_NOTICE, strLog); |
| 61 | |
| 62 | m_processor->AddCommand(command); |
| 63 | |
| 64 | switch(command.opcode) |
| 65 | { |
| 66 | case CEC_OPCODE_REPORT_POWER_STATUS: |
| 67 | HandleReportPowerStatus(command); |
| 68 | break; |
| 69 | case CEC_OPCODE_CEC_VERSION: |
| 70 | HandleDeviceCecVersion(command); |
| 71 | break; |
| 72 | case CEC_OPCODE_SET_MENU_LANGUAGE: |
| 73 | HandleSetMenuLanguage(command); |
| 74 | break; |
| 75 | case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS: |
| 76 | HandleGivePhysicalAddress(command); |
| 77 | break; |
| 78 | case CEC_OPCODE_GIVE_OSD_NAME: |
| 79 | HandleGiveOSDName(command); |
| 80 | break; |
| 81 | case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID: |
| 82 | HandleGiveDeviceVendorId(command); |
| 83 | break; |
| 84 | case CEC_OPCODE_DEVICE_VENDOR_ID: |
| 85 | bHandlerChanged = HandleDeviceVendorId(command); |
| 86 | break; |
| 87 | case CEC_OPCODE_VENDOR_COMMAND_WITH_ID: |
| 88 | HandleDeviceVendorCommandWithId(command); |
| 89 | break; |
| 90 | case CEC_OPCODE_GIVE_DECK_STATUS: |
| 91 | HandleGiveDeckStatus(command); |
| 92 | break; |
| 93 | case CEC_OPCODE_DECK_CONTROL: |
| 94 | HandleDeckControl(command); |
| 95 | break; |
| 96 | case CEC_OPCODE_MENU_REQUEST: |
| 97 | HandleMenuRequest(command); |
| 98 | break; |
| 99 | case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS: |
| 100 | HandleGiveDevicePowerStatus(command); |
| 101 | break; |
| 102 | case CEC_OPCODE_GET_CEC_VERSION: |
| 103 | HandleGetCecVersion(command); |
| 104 | break; |
| 105 | case CEC_OPCODE_USER_CONTROL_PRESSED: |
| 106 | HandleUserControlPressed(command); |
| 107 | break; |
| 108 | case CEC_OPCODE_USER_CONTROL_RELEASE: |
| 109 | HandleUserControlRelease(command); |
| 110 | break; |
| 111 | case CEC_OPCODE_GIVE_AUDIO_STATUS: |
| 112 | HandleGiveAudioStatus(command); |
| 113 | break; |
| 114 | case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS: |
| 115 | HandleGiveSystemAudioModeStatus(command); |
| 116 | break; |
| 117 | case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST: |
| 118 | HandleSystemAudioModeRequest(command); |
| 119 | break; |
| 120 | case CEC_OPCODE_REPORT_AUDIO_STATUS: |
| 121 | HandleReportAudioStatus(command); |
| 122 | break; |
| 123 | case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS: |
| 124 | HandleSystemAudioModeStatus(command); |
| 125 | break; |
| 126 | case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE: |
| 127 | HandleSetSystemAudioMode(command); |
| 128 | break; |
| 129 | case CEC_OPCODE_REQUEST_ACTIVE_SOURCE: |
| 130 | HandleRequestActiveSource(command); |
| 131 | break; |
| 132 | case CEC_OPCODE_SET_STREAM_PATH: |
| 133 | HandleSetStreamPath(command); |
| 134 | break; |
| 135 | case CEC_OPCODE_ROUTING_CHANGE: |
| 136 | HandleRoutingChange(command); |
| 137 | break; |
| 138 | case CEC_OPCODE_ROUTING_INFORMATION: |
| 139 | HandleRoutingInformation(command); |
| 140 | break; |
| 141 | case CEC_OPCODE_STANDBY: |
| 142 | HandleStandby(command); |
| 143 | break; |
| 144 | case CEC_OPCODE_ACTIVE_SOURCE: |
| 145 | HandleActiveSource(command); |
| 146 | break; |
| 147 | case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS: |
| 148 | HandleReportPhysicalAddress(command); |
| 149 | break; |
| 150 | case CEC_OPCODE_SET_OSD_NAME: |
| 151 | HandleSetOSDName(command); |
| 152 | break; |
| 153 | case CEC_OPCODE_IMAGE_VIEW_ON: |
| 154 | HandleImageViewOn(command); |
| 155 | break; |
| 156 | case CEC_OPCODE_TEXT_VIEW_ON: |
| 157 | HandleTextViewOn(command); |
| 158 | break; |
| 159 | default: |
| 160 | UnhandledCommand(command); |
| 161 | bHandled = false; |
| 162 | break; |
| 163 | } |
| 164 | |
| 165 | if (bHandled && !bHandlerChanged) |
| 166 | { |
| 167 | CLockObject lock(&m_processor->m_transmitMutex); |
| 168 | m_condition.Signal(); |
| 169 | } |
| 170 | |
| 171 | return bHandled; |
| 172 | } |
| 173 | |
| 174 | bool CCECCommandHandler::HandleActiveSource(const cec_command &command) |
| 175 | { |
| 176 | if (command.parameters.size == 2) |
| 177 | { |
| 178 | uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]); |
| 179 | return m_processor->SetStreamPath(iAddress); |
| 180 | } |
| 181 | |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | bool CCECCommandHandler::HandleDeckControl(const cec_command &command) |
| 186 | { |
| 187 | CCECBusDevice *device = GetDevice(command.destination); |
| 188 | if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE) && command.parameters.size > 0) |
| 189 | { |
| 190 | ((CCECPlaybackDevice *) device)->SetDeckControlMode((cec_deck_control_mode) command.parameters[0]); |
| 191 | return true; |
| 192 | } |
| 193 | |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command &command) |
| 198 | { |
| 199 | if (command.parameters.size == 1) |
| 200 | { |
| 201 | CCECBusDevice *device = GetDevice(command.initiator); |
| 202 | if (device) |
| 203 | device->SetCecVersion((cec_version) command.parameters[0]); |
| 204 | } |
| 205 | |
| 206 | return true; |
| 207 | } |
| 208 | |
| 209 | bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command) |
| 210 | { |
| 211 | if (m_busDevice->MyLogicalAddressContains(command.destination)) |
| 212 | m_processor->TransmitAbort(command.initiator, command.opcode, CEC_ABORT_REASON_REFUSED); |
| 213 | |
| 214 | return true; |
| 215 | } |
| 216 | |
| 217 | bool CCECCommandHandler::HandleDeviceVendorId(const cec_command &command) |
| 218 | { |
| 219 | return SetVendorId(command); |
| 220 | } |
| 221 | |
| 222 | bool CCECCommandHandler::HandleGetCecVersion(const cec_command &command) |
| 223 | { |
| 224 | if (m_busDevice->MyLogicalAddressContains(command.destination)) |
| 225 | { |
| 226 | CCECBusDevice *device = GetDevice(command.destination); |
| 227 | if (device) |
| 228 | return device->TransmitCECVersion(command.initiator); |
| 229 | } |
| 230 | |
| 231 | return false; |
| 232 | } |
| 233 | |
| 234 | bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command &command) |
| 235 | { |
| 236 | if (m_busDevice->MyLogicalAddressContains(command.destination)) |
| 237 | { |
| 238 | CCECBusDevice *device = GetDevice(command.destination); |
| 239 | if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM) |
| 240 | return ((CCECAudioSystem *) device)->TransmitAudioStatus(command.initiator); |
| 241 | } |
| 242 | |
| 243 | return false; |
| 244 | } |
| 245 | |
| 246 | bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command &command) |
| 247 | { |
| 248 | if (m_busDevice->MyLogicalAddressContains(command.destination)) |
| 249 | { |
| 250 | CCECBusDevice *device = GetDevice(command.destination); |
| 251 | if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE)) |
| 252 | return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator); |
| 253 | } |
| 254 | |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command) |
| 259 | { |
| 260 | if (m_busDevice->MyLogicalAddressContains(command.destination)) |
| 261 | { |
| 262 | CCECBusDevice *device = GetDevice(command.destination); |
| 263 | if (device) |
| 264 | return device->TransmitPowerState(command.initiator); |
| 265 | } |
| 266 | |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command &command) |
| 271 | { |
| 272 | if (m_busDevice->MyLogicalAddressContains(command.destination)) |
| 273 | { |
| 274 | CCECBusDevice *device = GetDevice(command.destination); |
| 275 | if (device) |
| 276 | return device->TransmitVendorID(command.initiator); |
| 277 | } |
| 278 | |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | bool CCECCommandHandler::HandleGiveOSDName(const cec_command &command) |
| 283 | { |
| 284 | if (m_busDevice->MyLogicalAddressContains(command.destination)) |
| 285 | { |
| 286 | CCECBusDevice *device = GetDevice(command.destination); |
| 287 | if (device) |
| 288 | return device->TransmitOSDName(command.initiator); |
| 289 | } |
| 290 | |
| 291 | return false; |
| 292 | } |
| 293 | |
| 294 | bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command &command) |
| 295 | { |
| 296 | if (m_busDevice->MyLogicalAddressContains(command.destination)) |
| 297 | { |
| 298 | CCECBusDevice *device = GetDevice(command.destination); |
| 299 | if (device) |
| 300 | return device->TransmitPhysicalAddress(); |
| 301 | } |
| 302 | |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command &command) |
| 307 | { |
| 308 | if (m_busDevice->MyLogicalAddressContains(command.destination)) |
| 309 | { |
| 310 | CCECBusDevice *device = GetDevice(command.destination); |
| 311 | if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM) |
| 312 | return ((CCECAudioSystem *) device)->TransmitSystemAudioModeStatus(command.initiator); |
| 313 | } |
| 314 | |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | bool CCECCommandHandler::HandleImageViewOn(const cec_command &command) |
| 319 | { |
| 320 | m_processor->SetActiveSource(command.initiator); |
| 321 | return true; |
| 322 | } |
| 323 | |
| 324 | bool CCECCommandHandler::HandleMenuRequest(const cec_command &command) |
| 325 | { |
| 326 | if (m_busDevice->MyLogicalAddressContains(command.destination)) |
| 327 | { |
| 328 | if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_QUERY) |
| 329 | { |
| 330 | CCECBusDevice *device = GetDevice(command.destination); |
| 331 | if (device) |
| 332 | return device->TransmitMenuState(command.initiator); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | return false; |
| 337 | } |
| 338 | |
| 339 | bool CCECCommandHandler::HandleReportAudioStatus(const cec_command &command) |
| 340 | { |
| 341 | if (command.parameters.size == 1) |
| 342 | { |
| 343 | CCECBusDevice *device = GetDevice(command.initiator); |
| 344 | if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM) |
| 345 | { |
| 346 | ((CCECAudioSystem *)device)->SetAudioStatus(command.parameters[0]); |
| 347 | return true; |
| 348 | } |
| 349 | } |
| 350 | return false; |
| 351 | } |
| 352 | |
| 353 | bool CCECCommandHandler::HandleReportPhysicalAddress(const cec_command &command) |
| 354 | { |
| 355 | if (command.parameters.size == 3) |
| 356 | { |
| 357 | uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]); |
| 358 | SetPhysicalAddress(command.initiator, iNewAddress); |
| 359 | } |
| 360 | return true; |
| 361 | } |
| 362 | |
| 363 | bool CCECCommandHandler::HandleReportPowerStatus(const cec_command &command) |
| 364 | { |
| 365 | if (command.parameters.size == 1) |
| 366 | { |
| 367 | CCECBusDevice *device = GetDevice(command.initiator); |
| 368 | if (device) |
| 369 | device->SetPowerStatus((cec_power_status) command.parameters[0]); |
| 370 | } |
| 371 | return true; |
| 372 | } |
| 373 | |
| 374 | bool CCECCommandHandler::HandleRequestActiveSource(const cec_command &command) |
| 375 | { |
| 376 | CStdString strLog; |
| 377 | strLog.Format(">> %i requests active source", (uint8_t) command.initiator); |
| 378 | m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
| 379 | |
| 380 | vector<CCECBusDevice *> devices; |
| 381 | for (int iDevicePtr = (int)GetMyDevices(devices)-1; iDevicePtr >=0; iDevicePtr--) |
| 382 | devices[iDevicePtr]->TransmitActiveSource(); |
| 383 | |
| 384 | return true; |
| 385 | } |
| 386 | |
| 387 | bool CCECCommandHandler::HandleRoutingChange(const cec_command &command) |
| 388 | { |
| 389 | if (command.parameters.size == 4) |
| 390 | { |
| 391 | uint16_t iOldAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]); |
| 392 | uint16_t iNewAddress = ((uint16_t)command.parameters[2] << 8) | ((uint16_t)command.parameters[3]); |
| 393 | |
| 394 | CCECBusDevice *device = GetDevice(command.initiator); |
| 395 | if (device) |
| 396 | device->SetStreamPath(iNewAddress, iOldAddress); |
| 397 | } |
| 398 | return true; |
| 399 | } |
| 400 | |
| 401 | bool CCECCommandHandler::HandleRoutingInformation(const cec_command &command) |
| 402 | { |
| 403 | if (command.parameters.size == 2) |
| 404 | { |
| 405 | uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]); |
| 406 | m_processor->SetStreamPath(iNewAddress); |
| 407 | } |
| 408 | |
| 409 | return false; |
| 410 | } |
| 411 | |
| 412 | bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command &command) |
| 413 | { |
| 414 | if (command.parameters.size == 3) |
| 415 | { |
| 416 | CCECBusDevice *device = GetDevice(command.initiator); |
| 417 | if (device) |
| 418 | { |
| 419 | cec_menu_language language; |
| 420 | language.device = command.initiator; |
| 421 | for (uint8_t iPtr = 0; iPtr < 4; iPtr++) |
| 422 | language.language[iPtr] = command.parameters[iPtr]; |
| 423 | language.language[3] = 0; |
| 424 | device->SetMenuLanguage(language); |
| 425 | return true; |
| 426 | } |
| 427 | } |
| 428 | return false; |
| 429 | } |
| 430 | |
| 431 | bool CCECCommandHandler::HandleSetOSDName(const cec_command &command) |
| 432 | { |
| 433 | if (command.parameters.size > 0) |
| 434 | { |
| 435 | CCECBusDevice *device = GetDevice(command.initiator); |
| 436 | if (device) |
| 437 | { |
| 438 | char buf[1024]; |
| 439 | for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++) |
| 440 | buf[iPtr] = (char)command.parameters[iPtr]; |
| 441 | buf[command.parameters.size] = 0; |
| 442 | |
| 443 | CStdString strName(buf); |
| 444 | device->SetOSDName(strName); |
| 445 | |
| 446 | return true; |
| 447 | } |
| 448 | } |
| 449 | return false; |
| 450 | } |
| 451 | |
| 452 | bool CCECCommandHandler::HandleSetStreamPath(const cec_command &command) |
| 453 | { |
| 454 | if (command.parameters.size >= 2) |
| 455 | { |
| 456 | uint16_t iStreamAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]); |
| 457 | CStdString strLog; |
| 458 | strLog.Format(">> %i sets stream path to physical address %04x", command.initiator, iStreamAddress); |
| 459 | m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
| 460 | |
| 461 | if (m_processor->SetStreamPath(iStreamAddress)) |
| 462 | { |
| 463 | CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamAddress); |
| 464 | if (device) |
| 465 | { |
| 466 | return device->TransmitActiveSource() && |
| 467 | device->TransmitMenuState(command.initiator); |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | return false; |
| 472 | } |
| 473 | |
| 474 | bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command &command) |
| 475 | { |
| 476 | if (m_busDevice->MyLogicalAddressContains(command.destination)) |
| 477 | { |
| 478 | CCECBusDevice *device = GetDevice(command.destination); |
| 479 | if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM) |
| 480 | { |
| 481 | if (command.parameters.size >= 2) |
| 482 | { |
| 483 | device->SetPowerStatus(CEC_POWER_STATUS_ON); |
| 484 | ((CCECAudioSystem *) device)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON); |
| 485 | uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]); |
| 486 | CCECBusDevice *newActiveDevice = GetDeviceByPhysicalAddress(iNewAddress); |
| 487 | if (newActiveDevice) |
| 488 | m_processor->SetActiveSource(newActiveDevice->GetLogicalAddress()); |
| 489 | return ((CCECAudioSystem *) device)->TransmitSetSystemAudioMode(command.initiator); |
| 490 | } |
| 491 | else |
| 492 | { |
| 493 | ((CCECAudioSystem *) device)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF); |
| 494 | return ((CCECAudioSystem *) device)->TransmitSetSystemAudioMode(command.initiator); |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | return false; |
| 499 | } |
| 500 | |
| 501 | bool CCECCommandHandler::HandleStandby(const cec_command &command) |
| 502 | { |
| 503 | CCECBusDevice *device = GetDevice(command.initiator); |
| 504 | if (device) |
| 505 | device->SetPowerStatus(CEC_POWER_STATUS_STANDBY); |
| 506 | |
| 507 | return true; |
| 508 | } |
| 509 | |
| 510 | bool CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command &command) |
| 511 | { |
| 512 | if (command.parameters.size == 1) |
| 513 | { |
| 514 | CCECBusDevice *device = GetDevice(command.initiator); |
| 515 | if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM) |
| 516 | { |
| 517 | ((CCECAudioSystem *)device)->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]); |
| 518 | return true; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | return false; |
| 523 | } |
| 524 | |
| 525 | bool CCECCommandHandler::HandleSetSystemAudioMode(const cec_command &command) |
| 526 | { |
| 527 | if (command.parameters.size == 1) |
| 528 | { |
| 529 | CCECBusDevice *device = GetDevice(command.initiator); |
| 530 | if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM) |
| 531 | { |
| 532 | ((CCECAudioSystem *)device)->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]); |
| 533 | return true; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | return false; |
| 538 | } |
| 539 | |
| 540 | bool CCECCommandHandler::HandleTextViewOn(const cec_command &command) |
| 541 | { |
| 542 | m_processor->SetActiveSource(command.initiator); |
| 543 | return true; |
| 544 | } |
| 545 | |
| 546 | bool CCECCommandHandler::HandleUserControlPressed(const cec_command &command) |
| 547 | { |
| 548 | if (m_busDevice->MyLogicalAddressContains(command.destination) && command.parameters.size > 0) |
| 549 | { |
| 550 | m_processor->AddKey(); |
| 551 | |
| 552 | if (command.parameters[0] <= CEC_USER_CONTROL_CODE_MAX) |
| 553 | { |
| 554 | CStdString strLog; |
| 555 | strLog.Format("key pressed: %x", command.parameters[0]); |
| 556 | m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
| 557 | |
| 558 | if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER || |
| 559 | command.parameters[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION) |
| 560 | { |
| 561 | CCECBusDevice *device = GetDevice(command.destination); |
| 562 | if (device) |
| 563 | device->SetPowerStatus(CEC_POWER_STATUS_ON); |
| 564 | } |
| 565 | |
| 566 | m_processor->SetCurrentButton((cec_user_control_code) command.parameters[0]); |
| 567 | return true; |
| 568 | } |
| 569 | } |
| 570 | return false; |
| 571 | } |
| 572 | |
| 573 | bool CCECCommandHandler::HandleUserControlRelease(const cec_command &command) |
| 574 | { |
| 575 | if (m_busDevice->MyLogicalAddressContains(command.destination)) |
| 576 | m_processor->AddKey(); |
| 577 | |
| 578 | return true; |
| 579 | } |
| 580 | |
| 581 | void CCECCommandHandler::UnhandledCommand(const cec_command &command) |
| 582 | { |
| 583 | CStdString strLog; |
| 584 | strLog.Format("unhandled command with opcode %02x from address %d", command.opcode, command.initiator); |
| 585 | m_busDevice->AddLog(CEC_LOG_DEBUG, strLog); |
| 586 | } |
| 587 | |
| 588 | unsigned int CCECCommandHandler::GetMyDevices(vector<CCECBusDevice *> &devices) const |
| 589 | { |
| 590 | unsigned int iReturn(0); |
| 591 | |
| 592 | cec_logical_addresses addresses = m_processor->GetLogicalAddresses(); |
| 593 | for (uint8_t iPtr = 0; iPtr < 16; iPtr++) |
| 594 | { |
| 595 | if (addresses[iPtr]) |
| 596 | { |
| 597 | devices.push_back(GetDevice((cec_logical_address) iPtr)); |
| 598 | ++iReturn; |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | return iReturn; |
| 603 | } |
| 604 | |
| 605 | CCECBusDevice *CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress) const |
| 606 | { |
| 607 | CCECBusDevice *device = NULL; |
| 608 | |
| 609 | if (iLogicalAddress >= CECDEVICE_TV && iLogicalAddress <= CECDEVICE_BROADCAST) |
| 610 | device = m_processor->m_busDevices[iLogicalAddress]; |
| 611 | |
| 612 | return device; |
| 613 | } |
| 614 | |
| 615 | CCECBusDevice *CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress) const |
| 616 | { |
| 617 | return m_processor->GetDeviceByPhysicalAddress(iPhysicalAddress); |
| 618 | } |
| 619 | |
| 620 | CCECBusDevice *CCECCommandHandler::GetDeviceByType(cec_device_type type) const |
| 621 | { |
| 622 | return m_processor->GetDeviceByType(type); |
| 623 | } |
| 624 | |
| 625 | bool CCECCommandHandler::SetVendorId(const cec_command &command) |
| 626 | { |
| 627 | bool bChanged(false); |
| 628 | if (command.parameters.size < 3) |
| 629 | { |
| 630 | m_busDevice->AddLog(CEC_LOG_WARNING, "invalid vendor ID received"); |
| 631 | return bChanged; |
| 632 | } |
| 633 | |
| 634 | uint64_t iVendorId = ((uint64_t)command.parameters[0] << 16) + |
| 635 | ((uint64_t)command.parameters[1] << 8) + |
| 636 | (uint64_t)command.parameters[2]; |
| 637 | |
| 638 | CCECBusDevice *device = GetDevice((cec_logical_address) command.initiator); |
| 639 | if (device) |
| 640 | bChanged = device->SetVendorId(iVendorId); |
| 641 | return bChanged; |
| 642 | } |
| 643 | |
| 644 | void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress, uint16_t iNewAddress) |
| 645 | { |
| 646 | if (!m_busDevice->MyLogicalAddressContains(iAddress)) |
| 647 | { |
| 648 | bool bOurAddress(m_processor->GetPhysicalAddress() == iNewAddress); |
| 649 | GetDevice(iAddress)->SetPhysicalAddress(iNewAddress); |
| 650 | if (bOurAddress) |
| 651 | { |
| 652 | /* another device reported the same physical address as ours |
| 653 | * since we don't have physical address detection yet, we'll just use the |
| 654 | * given address, increased by 0x100 for now */ |
| 655 | m_processor->SetPhysicalAddress(iNewAddress + 0x100); |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | void CCECCommandHandler::HandlePoll(const cec_logical_address iInitiator, const cec_logical_address iDestination) |
| 661 | { |
| 662 | CStdString strLog; |
| 663 | strLog.Format("<< POLL: %s (%x) -> %s (%x)", m_processor->ToString(iInitiator), iInitiator, m_processor->ToString(iDestination), iDestination); |
| 664 | m_processor->AddLog(CEC_LOG_DEBUG, strLog); |
| 665 | } |
| 666 | |
| 667 | bool CCECCommandHandler::HandleReceiveFailed(void) |
| 668 | { |
| 669 | /* default = error */ |
| 670 | return true; |
| 671 | } |
| 672 | |
| 673 | bool CCECCommandHandler::TransmitPowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination) |
| 674 | { |
| 675 | cec_command command; |
| 676 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_IMAGE_VIEW_ON, m_iTransmitTimeout); |
| 677 | |
| 678 | return Transmit(command); |
| 679 | } |
| 680 | |
| 681 | bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator, const cec_logical_address iDestination) |
| 682 | { |
| 683 | cec_command command; |
| 684 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_STANDBY, m_iTransmitTimeout); |
| 685 | |
| 686 | return Transmit(command); |
| 687 | } |
| 688 | |
| 689 | bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination) |
| 690 | { |
| 691 | cec_command command; |
| 692 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_CEC_VERSION, m_iTransmitTimeout); |
| 693 | |
| 694 | return Transmit(command); |
| 695 | } |
| 696 | |
| 697 | bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator, const cec_logical_address iDestination) |
| 698 | { |
| 699 | cec_command command; |
| 700 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_MENU_LANGUAGE, m_iTransmitTimeout); |
| 701 | |
| 702 | return Transmit(command); |
| 703 | } |
| 704 | |
| 705 | bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination) |
| 706 | { |
| 707 | cec_command command; |
| 708 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_OSD_NAME, m_iTransmitTimeout); |
| 709 | |
| 710 | return Transmit(command); |
| 711 | } |
| 712 | |
| 713 | bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator, const cec_logical_address iDestination) |
| 714 | { |
| 715 | cec_command command; |
| 716 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS, m_iTransmitTimeout); |
| 717 | |
| 718 | return Transmit(command); |
| 719 | } |
| 720 | |
| 721 | bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination) |
| 722 | { |
| 723 | cec_command command; |
| 724 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS, m_iTransmitTimeout); |
| 725 | |
| 726 | return Transmit(command); |
| 727 | } |
| 728 | |
| 729 | bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator, const cec_logical_address iDestination) |
| 730 | { |
| 731 | cec_command command; |
| 732 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID, m_iTransmitTimeout); |
| 733 | |
| 734 | return Transmit(command); |
| 735 | } |
| 736 | |
| 737 | bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress) |
| 738 | { |
| 739 | cec_command command; |
| 740 | cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE, m_iTransmitTimeout); |
| 741 | command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF)); |
| 742 | command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF)); |
| 743 | |
| 744 | return Transmit(command); |
| 745 | } |
| 746 | |
| 747 | bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_version cecVersion) |
| 748 | { |
| 749 | cec_command command; |
| 750 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_CEC_VERSION, m_iTransmitTimeout); |
| 751 | command.parameters.PushBack((uint8_t)cecVersion); |
| 752 | |
| 753 | return Transmit(command); |
| 754 | } |
| 755 | |
| 756 | bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress) |
| 757 | { |
| 758 | cec_command command; |
| 759 | cec_command::Format(command, iInitiator, CECDEVICE_TV, CEC_OPCODE_INACTIVE_SOURCE, m_iTransmitTimeout); |
| 760 | command.parameters.PushBack((iPhysicalAddress >> 8) & 0xFF); |
| 761 | command.parameters.PushBack(iPhysicalAddress & 0xFF); |
| 762 | |
| 763 | return Transmit(command); |
| 764 | } |
| 765 | |
| 766 | bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_menu_state menuState) |
| 767 | { |
| 768 | cec_command command; |
| 769 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_MENU_STATUS, m_iTransmitTimeout); |
| 770 | command.parameters.PushBack((uint8_t)menuState); |
| 771 | |
| 772 | return Transmit(command); |
| 773 | } |
| 774 | |
| 775 | bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, CStdString strDeviceName) |
| 776 | { |
| 777 | cec_command command; |
| 778 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_NAME, m_iTransmitTimeout); |
| 779 | for (unsigned int iPtr = 0; iPtr < strDeviceName.length(); iPtr++) |
| 780 | command.parameters.PushBack(strDeviceName.at(iPtr)); |
| 781 | |
| 782 | return Transmit(command); |
| 783 | } |
| 784 | |
| 785 | bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_display_control duration, const char *strMessage) |
| 786 | { |
| 787 | cec_command command; |
| 788 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_STRING, m_iTransmitTimeout); |
| 789 | command.parameters.PushBack((uint8_t)duration); |
| 790 | |
| 791 | unsigned int iLen = strlen(strMessage); |
| 792 | if (iLen > 13) iLen = 13; |
| 793 | |
| 794 | for (unsigned int iPtr = 0; iPtr < iLen; iPtr++) |
| 795 | command.parameters.PushBack(strMessage[iPtr]); |
| 796 | |
| 797 | return Transmit(command); |
| 798 | } |
| 799 | |
| 800 | bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator, uint16_t iPhysicalAddress, cec_device_type type) |
| 801 | { |
| 802 | cec_command command; |
| 803 | cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS, m_iTransmitTimeout); |
| 804 | command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF)); |
| 805 | command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF)); |
| 806 | command.parameters.PushBack((uint8_t) (type)); |
| 807 | |
| 808 | return Transmit(command); |
| 809 | } |
| 810 | |
| 811 | bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator, const cec_logical_address iDestination) |
| 812 | { |
| 813 | cec_command command; |
| 814 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_NONE, m_iTransmitTimeout); |
| 815 | command.retries = 0; |
| 816 | |
| 817 | return Transmit(command); |
| 818 | } |
| 819 | |
| 820 | bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_power_status state) |
| 821 | { |
| 822 | cec_command command; |
| 823 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_POWER_STATUS, m_iTransmitTimeout); |
| 824 | command.parameters.PushBack((uint8_t) state); |
| 825 | |
| 826 | return Transmit(command); |
| 827 | } |
| 828 | |
| 829 | bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator, uint64_t iVendorId) |
| 830 | { |
| 831 | cec_command command; |
| 832 | cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_DEVICE_VENDOR_ID, m_iTransmitTimeout); |
| 833 | |
| 834 | command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 16) & 0xFF)); |
| 835 | command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 8) & 0xFF)); |
| 836 | command.parameters.PushBack((uint8_t) ((uint64_t)iVendorId & 0xFF)); |
| 837 | |
| 838 | return Transmit(command); |
| 839 | } |
| 840 | |
| 841 | bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, uint8_t state) |
| 842 | { |
| 843 | cec_command command; |
| 844 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_AUDIO_STATUS, m_iTransmitTimeout); |
| 845 | command.parameters.PushBack(state); |
| 846 | |
| 847 | return Transmit(command); |
| 848 | } |
| 849 | |
| 850 | bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state) |
| 851 | { |
| 852 | cec_command command; |
| 853 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE, m_iTransmitTimeout); |
| 854 | command.parameters.PushBack((uint8_t)state); |
| 855 | |
| 856 | return Transmit(command); |
| 857 | } |
| 858 | |
| 859 | bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state) |
| 860 | { |
| 861 | cec_command command; |
| 862 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS, m_iTransmitTimeout); |
| 863 | command.parameters.PushBack((uint8_t)state); |
| 864 | |
| 865 | return Transmit(command); |
| 866 | } |
| 867 | |
| 868 | bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_deck_info state) |
| 869 | { |
| 870 | cec_command command; |
| 871 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_DECK_STATUS, m_iTransmitTimeout); |
| 872 | command.PushBack((uint8_t)state); |
| 873 | |
| 874 | return Transmit(command); |
| 875 | } |
| 876 | |
| 877 | bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_user_control_code key) |
| 878 | { |
| 879 | cec_command command; |
| 880 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_PRESSED, m_iTransmitTimeout); |
| 881 | command.parameters.PushBack((uint8_t)key); |
| 882 | |
| 883 | return Transmit(command); |
| 884 | } |
| 885 | |
| 886 | bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator, const cec_logical_address iDestination) |
| 887 | { |
| 888 | cec_command command; |
| 889 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_RELEASE, m_iTransmitTimeout); |
| 890 | |
| 891 | return Transmit(command); |
| 892 | } |
| 893 | |
| 894 | bool CCECCommandHandler::Transmit(cec_command &command) |
| 895 | { |
| 896 | CLockObject writeLock(&m_processor->m_transmitMutex); |
| 897 | if (m_processor->Transmit(command)) |
| 898 | return m_condition.Wait(&m_processor->m_transmitMutex, m_iTransmitTimeout); |
| 899 | |
| 900 | return false; |
| 901 | } |