| 1 | /* |
| 2 | * This file is part of the libCEC(R) library. |
| 3 | * |
| 4 | * libCEC(R) is Copyright (C) 2011-2013 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 "env.h" |
| 34 | #include "CECCommandHandler.h" |
| 35 | |
| 36 | #include "lib/devices/CECBusDevice.h" |
| 37 | #include "lib/devices/CECAudioSystem.h" |
| 38 | #include "lib/devices/CECPlaybackDevice.h" |
| 39 | #include "lib/CECClient.h" |
| 40 | #include "lib/CECProcessor.h" |
| 41 | #include "lib/LibCEC.h" |
| 42 | #include "lib/CECTypeUtils.h" |
| 43 | #include "lib/platform/util/util.h" |
| 44 | |
| 45 | using namespace CEC; |
| 46 | using namespace std; |
| 47 | using namespace PLATFORM; |
| 48 | |
| 49 | #define LIB_CEC m_busDevice->GetProcessor()->GetLib() |
| 50 | #define ToString(p) CCECTypeUtils::ToString(p) |
| 51 | #define REQUEST_POWER_STATUS_TIMEOUT 5000 |
| 52 | |
| 53 | CCECCommandHandler::CCECCommandHandler(CCECBusDevice *busDevice, |
| 54 | int32_t iTransmitTimeout /* = CEC_DEFAULT_TRANSMIT_TIMEOUT */, |
| 55 | int32_t iTransmitWait /* = CEC_DEFAULT_TRANSMIT_WAIT */, |
| 56 | int8_t iTransmitRetries /* = CEC_DEFAULT_TRANSMIT_RETRIES */, |
| 57 | int64_t iActiveSourcePending /* = 0 */) : |
| 58 | m_busDevice(busDevice), |
| 59 | m_processor(m_busDevice->GetProcessor()), |
| 60 | m_iTransmitTimeout(iTransmitTimeout), |
| 61 | m_iTransmitWait(iTransmitWait), |
| 62 | m_iTransmitRetries(iTransmitRetries), |
| 63 | m_bHandlerInited(false), |
| 64 | m_bOPTSendDeckStatusUpdateOnActiveSource(false), |
| 65 | m_vendorId(CEC_VENDOR_UNKNOWN), |
| 66 | m_iActiveSourcePending(iActiveSourcePending), |
| 67 | m_iPowerStatusRequested(0) |
| 68 | { |
| 69 | } |
| 70 | |
| 71 | bool CCECCommandHandler::HandleCommand(const cec_command &command) |
| 72 | { |
| 73 | if (command.opcode_set == 0) |
| 74 | return HandlePoll(command); |
| 75 | |
| 76 | int iHandled(CEC_ABORT_REASON_UNRECOGNIZED_OPCODE); |
| 77 | |
| 78 | LIB_CEC->AddCommand(command); |
| 79 | |
| 80 | switch(command.opcode) |
| 81 | { |
| 82 | case CEC_OPCODE_REPORT_POWER_STATUS: |
| 83 | iHandled = HandleReportPowerStatus(command); |
| 84 | break; |
| 85 | case CEC_OPCODE_CEC_VERSION: |
| 86 | iHandled = HandleDeviceCecVersion(command); |
| 87 | break; |
| 88 | case CEC_OPCODE_SET_MENU_LANGUAGE: |
| 89 | iHandled = HandleSetMenuLanguage(command); |
| 90 | break; |
| 91 | case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS: |
| 92 | iHandled = HandleGivePhysicalAddress(command); |
| 93 | break; |
| 94 | case CEC_OPCODE_GET_MENU_LANGUAGE: |
| 95 | iHandled = HandleGiveMenuLanguage(command); |
| 96 | break; |
| 97 | case CEC_OPCODE_GIVE_OSD_NAME: |
| 98 | iHandled = HandleGiveOSDName(command); |
| 99 | break; |
| 100 | case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID: |
| 101 | iHandled = HandleGiveDeviceVendorId(command); |
| 102 | break; |
| 103 | case CEC_OPCODE_DEVICE_VENDOR_ID: |
| 104 | iHandled = HandleDeviceVendorId(command); |
| 105 | break; |
| 106 | case CEC_OPCODE_VENDOR_COMMAND_WITH_ID: |
| 107 | iHandled = HandleDeviceVendorCommandWithId(command); |
| 108 | break; |
| 109 | case CEC_OPCODE_GIVE_DECK_STATUS: |
| 110 | iHandled = HandleGiveDeckStatus(command); |
| 111 | break; |
| 112 | case CEC_OPCODE_DECK_CONTROL: |
| 113 | iHandled = HandleDeckControl(command); |
| 114 | break; |
| 115 | case CEC_OPCODE_MENU_REQUEST: |
| 116 | iHandled = HandleMenuRequest(command); |
| 117 | break; |
| 118 | case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS: |
| 119 | iHandled = HandleGiveDevicePowerStatus(command); |
| 120 | break; |
| 121 | case CEC_OPCODE_GET_CEC_VERSION: |
| 122 | iHandled = HandleGetCecVersion(command); |
| 123 | break; |
| 124 | case CEC_OPCODE_USER_CONTROL_PRESSED: |
| 125 | iHandled = HandleUserControlPressed(command); |
| 126 | break; |
| 127 | case CEC_OPCODE_USER_CONTROL_RELEASE: |
| 128 | iHandled = HandleUserControlRelease(command); |
| 129 | break; |
| 130 | case CEC_OPCODE_GIVE_AUDIO_STATUS: |
| 131 | iHandled = HandleGiveAudioStatus(command); |
| 132 | break; |
| 133 | case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS: |
| 134 | iHandled = HandleGiveSystemAudioModeStatus(command); |
| 135 | break; |
| 136 | case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST: |
| 137 | iHandled = HandleSystemAudioModeRequest(command); |
| 138 | break; |
| 139 | case CEC_OPCODE_REPORT_AUDIO_STATUS: |
| 140 | iHandled = HandleReportAudioStatus(command); |
| 141 | break; |
| 142 | case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS: |
| 143 | iHandled = HandleSystemAudioModeStatus(command); |
| 144 | break; |
| 145 | case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE: |
| 146 | iHandled = HandleSetSystemAudioMode(command); |
| 147 | break; |
| 148 | case CEC_OPCODE_REQUEST_ACTIVE_SOURCE: |
| 149 | iHandled = HandleRequestActiveSource(command); |
| 150 | break; |
| 151 | case CEC_OPCODE_SET_STREAM_PATH: |
| 152 | iHandled = HandleSetStreamPath(command); |
| 153 | break; |
| 154 | case CEC_OPCODE_ROUTING_CHANGE: |
| 155 | iHandled = HandleRoutingChange(command); |
| 156 | break; |
| 157 | case CEC_OPCODE_ROUTING_INFORMATION: |
| 158 | iHandled = HandleRoutingInformation(command); |
| 159 | break; |
| 160 | case CEC_OPCODE_STANDBY: |
| 161 | iHandled = HandleStandby(command); |
| 162 | break; |
| 163 | case CEC_OPCODE_ACTIVE_SOURCE: |
| 164 | iHandled = HandleActiveSource(command); |
| 165 | break; |
| 166 | case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS: |
| 167 | iHandled = HandleReportPhysicalAddress(command); |
| 168 | break; |
| 169 | case CEC_OPCODE_SET_OSD_NAME: |
| 170 | iHandled = HandleSetOSDName(command); |
| 171 | break; |
| 172 | case CEC_OPCODE_IMAGE_VIEW_ON: |
| 173 | iHandled = HandleImageViewOn(command); |
| 174 | break; |
| 175 | case CEC_OPCODE_TEXT_VIEW_ON: |
| 176 | iHandled = HandleTextViewOn(command); |
| 177 | break; |
| 178 | case CEC_OPCODE_FEATURE_ABORT: |
| 179 | iHandled = HandleFeatureAbort(command); |
| 180 | break; |
| 181 | case CEC_OPCODE_VENDOR_COMMAND: |
| 182 | iHandled = HandleVendorCommand(command); |
| 183 | break; |
| 184 | case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN: |
| 185 | iHandled = HandleVendorRemoteButtonDown(command); |
| 186 | break; |
| 187 | case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP: |
| 188 | iHandled = HandleVendorRemoteButtonUp(command); |
| 189 | break; |
| 190 | case CEC_OPCODE_PLAY: |
| 191 | // libCEC (currently) doesn't need to do anything with this, since player applications handle it |
| 192 | // but it should not respond with a feature abort |
| 193 | iHandled = COMMAND_HANDLED; |
| 194 | break; |
| 195 | default: |
| 196 | break; |
| 197 | } |
| 198 | |
| 199 | if (iHandled == COMMAND_HANDLED) |
| 200 | m_busDevice->SignalOpcode((command.opcode == CEC_OPCODE_FEATURE_ABORT && command.parameters.size > 0) ? (cec_opcode)command.parameters[0] : command.opcode); |
| 201 | else |
| 202 | UnhandledCommand(command, (cec_abort_reason)iHandled); |
| 203 | |
| 204 | return iHandled == COMMAND_HANDLED; |
| 205 | } |
| 206 | |
| 207 | int CCECCommandHandler::HandleActiveSource(const cec_command &command) |
| 208 | { |
| 209 | if (command.parameters.size == 2) |
| 210 | { |
| 211 | uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]); |
| 212 | CCECBusDevice *device = m_processor->GetDevice(command.initiator); |
| 213 | if (device) |
| 214 | { |
| 215 | device->SetPhysicalAddress(iAddress); |
| 216 | device->MarkAsActiveSource(); |
| 217 | } |
| 218 | |
| 219 | m_processor->GetDevices()->SignalAll(command.opcode); |
| 220 | return COMMAND_HANDLED; |
| 221 | } |
| 222 | |
| 223 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 224 | } |
| 225 | |
| 226 | int CCECCommandHandler::HandleDeckControl(const cec_command &command) |
| 227 | { |
| 228 | CCECPlaybackDevice *device = CCECBusDevice::AsPlaybackDevice(GetDevice(command.destination)); |
| 229 | if (device && command.parameters.size > 0) |
| 230 | { |
| 231 | device->SetDeckControlMode((cec_deck_control_mode) command.parameters[0]); |
| 232 | return COMMAND_HANDLED; |
| 233 | } |
| 234 | |
| 235 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 236 | } |
| 237 | |
| 238 | int CCECCommandHandler::HandleDeviceCecVersion(const cec_command &command) |
| 239 | { |
| 240 | if (command.parameters.size == 1) |
| 241 | { |
| 242 | CCECBusDevice *device = GetDevice(command.initiator); |
| 243 | if (device) |
| 244 | device->SetCecVersion((cec_version) command.parameters[0]); |
| 245 | |
| 246 | return COMMAND_HANDLED; |
| 247 | } |
| 248 | |
| 249 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 250 | } |
| 251 | |
| 252 | int CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command & UNUSED(command)) |
| 253 | { |
| 254 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 255 | } |
| 256 | |
| 257 | int CCECCommandHandler::HandleDeviceVendorId(const cec_command &command) |
| 258 | { |
| 259 | SetVendorId(command); |
| 260 | |
| 261 | if (command.initiator == CECDEVICE_TV) |
| 262 | { |
| 263 | CCECBusDevice* primary = m_processor->GetPrimaryDevice(); |
| 264 | if (primary) |
| 265 | primary->TransmitVendorID(CECDEVICE_BROADCAST, false, false); |
| 266 | } |
| 267 | |
| 268 | return COMMAND_HANDLED; |
| 269 | } |
| 270 | |
| 271 | int CCECCommandHandler::HandleFeatureAbort(const cec_command &command) |
| 272 | { |
| 273 | if (command.parameters.size == 2 && |
| 274 | (command.parameters[1] == CEC_ABORT_REASON_UNRECOGNIZED_OPCODE || |
| 275 | command.parameters[1] == CEC_ABORT_REASON_REFUSED)) |
| 276 | m_processor->GetDevice(command.initiator)->SetUnsupportedFeature((cec_opcode)command.parameters[0]); |
| 277 | return COMMAND_HANDLED; |
| 278 | } |
| 279 | |
| 280 | int CCECCommandHandler::HandleGetCecVersion(const cec_command &command) |
| 281 | { |
| 282 | if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination)) |
| 283 | { |
| 284 | CCECBusDevice *device = GetDevice(command.destination); |
| 285 | if (device && device->TransmitCECVersion(command.initiator, true)) |
| 286 | return COMMAND_HANDLED; |
| 287 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 288 | } |
| 289 | |
| 290 | return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND; |
| 291 | } |
| 292 | |
| 293 | int CCECCommandHandler::HandleGiveAudioStatus(const cec_command &command) |
| 294 | { |
| 295 | if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination)) |
| 296 | { |
| 297 | CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination)); |
| 298 | if (device && device->TransmitAudioStatus(command.initiator, true)) |
| 299 | return COMMAND_HANDLED; |
| 300 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 301 | } |
| 302 | |
| 303 | return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND; |
| 304 | } |
| 305 | |
| 306 | int CCECCommandHandler::HandleGiveDeckStatus(const cec_command &command) |
| 307 | { |
| 308 | if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination)) |
| 309 | { |
| 310 | CCECPlaybackDevice *device = CCECBusDevice::AsPlaybackDevice(GetDevice(command.destination)); |
| 311 | if (device && device->TransmitDeckStatus(command.initiator, true)) |
| 312 | return COMMAND_HANDLED; |
| 313 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 314 | } |
| 315 | |
| 316 | return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND; |
| 317 | } |
| 318 | |
| 319 | int CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command) |
| 320 | { |
| 321 | if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination)) |
| 322 | { |
| 323 | CCECBusDevice *device = GetDevice(command.destination); |
| 324 | if (device && device->TransmitPowerState(command.initiator, true)) |
| 325 | return COMMAND_HANDLED; |
| 326 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 327 | } |
| 328 | |
| 329 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 330 | } |
| 331 | |
| 332 | int CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command &command) |
| 333 | { |
| 334 | if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination)) |
| 335 | { |
| 336 | CCECBusDevice *device = GetDevice(command.destination); |
| 337 | if (device && device->TransmitVendorID(command.initiator, true, true)) |
| 338 | return COMMAND_HANDLED; |
| 339 | } |
| 340 | |
| 341 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 342 | } |
| 343 | |
| 344 | int CCECCommandHandler::HandleGiveOSDName(const cec_command &command) |
| 345 | { |
| 346 | if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination)) |
| 347 | { |
| 348 | CCECBusDevice *device = GetDevice(command.destination); |
| 349 | if (device && device->TransmitOSDName(command.initiator, true)) |
| 350 | return COMMAND_HANDLED; |
| 351 | } |
| 352 | |
| 353 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 354 | } |
| 355 | |
| 356 | int CCECCommandHandler::HandleGivePhysicalAddress(const cec_command &command) |
| 357 | { |
| 358 | if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination)) |
| 359 | { |
| 360 | CCECBusDevice *device = GetDevice(command.destination); |
| 361 | if (device && device->TransmitPhysicalAddress(true)) |
| 362 | return COMMAND_HANDLED; |
| 363 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 364 | } |
| 365 | |
| 366 | return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND; |
| 367 | } |
| 368 | |
| 369 | int CCECCommandHandler::HandleGiveMenuLanguage(const cec_command &command) |
| 370 | { |
| 371 | if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination)) |
| 372 | { |
| 373 | CCECBusDevice *device = GetDevice(command.destination); |
| 374 | if (device && device->TransmitSetMenuLanguage(command.initiator, true)) |
| 375 | return COMMAND_HANDLED; |
| 376 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 377 | } |
| 378 | |
| 379 | return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND; |
| 380 | } |
| 381 | |
| 382 | int CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command &command) |
| 383 | { |
| 384 | if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination)) |
| 385 | { |
| 386 | CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination)); |
| 387 | if (device && device->TransmitSystemAudioModeStatus(command.initiator, true)) |
| 388 | return COMMAND_HANDLED; |
| 389 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 390 | } |
| 391 | |
| 392 | return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND; |
| 393 | } |
| 394 | |
| 395 | int CCECCommandHandler::HandleImageViewOn(const cec_command &command) |
| 396 | { |
| 397 | CCECBusDevice *device = GetDevice(command.destination); |
| 398 | if (device && device->GetCurrentStatus() == CEC_DEVICE_STATUS_PRESENT) |
| 399 | { |
| 400 | if (device->GetCurrentPowerStatus() == CEC_POWER_STATUS_STANDBY || |
| 401 | device->GetCurrentPowerStatus() == CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY) |
| 402 | device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); |
| 403 | CCECBusDevice* tv = GetDevice(CECDEVICE_TV); |
| 404 | if (tv) |
| 405 | tv->OnImageViewOnSent(false); |
| 406 | } |
| 407 | return COMMAND_HANDLED; |
| 408 | } |
| 409 | |
| 410 | int CCECCommandHandler::HandleMenuRequest(const cec_command &command) |
| 411 | { |
| 412 | if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination)) |
| 413 | { |
| 414 | CCECBusDevice *device = GetDevice(command.destination); |
| 415 | if (device) |
| 416 | { |
| 417 | CCECClient *client = device->GetClient(); |
| 418 | if (client) |
| 419 | { |
| 420 | if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_ACTIVATE) |
| 421 | { |
| 422 | if (client->MenuStateChanged(CEC_MENU_STATE_ACTIVATED) == 1) |
| 423 | device->SetMenuState(CEC_MENU_STATE_ACTIVATED); |
| 424 | } |
| 425 | else if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_DEACTIVATE) |
| 426 | { |
| 427 | if (client->MenuStateChanged(CEC_MENU_STATE_DEACTIVATED) == 1) |
| 428 | device->SetMenuState(CEC_MENU_STATE_DEACTIVATED); |
| 429 | } |
| 430 | } |
| 431 | if (device->TransmitMenuState(command.initiator, true)) |
| 432 | return COMMAND_HANDLED; |
| 433 | } |
| 434 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 435 | } |
| 436 | |
| 437 | return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND; |
| 438 | } |
| 439 | |
| 440 | bool CCECCommandHandler::HandlePoll(const cec_command &command) |
| 441 | { |
| 442 | m_busDevice->HandlePoll(command.destination); |
| 443 | return true; |
| 444 | } |
| 445 | |
| 446 | int CCECCommandHandler::HandleReportAudioStatus(const cec_command &command) |
| 447 | { |
| 448 | if (command.parameters.size == 1) |
| 449 | { |
| 450 | CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator)); |
| 451 | if (device) |
| 452 | { |
| 453 | device->SetAudioStatus(command.parameters[0]); |
| 454 | return COMMAND_HANDLED; |
| 455 | } |
| 456 | } |
| 457 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 458 | } |
| 459 | |
| 460 | int CCECCommandHandler::HandleReportPhysicalAddress(const cec_command &command) |
| 461 | { |
| 462 | if (command.parameters.size == 3) |
| 463 | { |
| 464 | uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]); |
| 465 | SetPhysicalAddress(command.initiator, iNewAddress); |
| 466 | |
| 467 | if (command.initiator == CECDEVICE_TV) |
| 468 | { |
| 469 | CCECBusDevice* primary = m_processor->GetPrimaryDevice(); |
| 470 | if (primary) |
| 471 | primary->TransmitPhysicalAddress(false); |
| 472 | } |
| 473 | return COMMAND_HANDLED; |
| 474 | } |
| 475 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 476 | } |
| 477 | |
| 478 | int CCECCommandHandler::HandleReportPowerStatus(const cec_command &command) |
| 479 | { |
| 480 | if (command.parameters.size == 1) |
| 481 | { |
| 482 | CCECBusDevice *device = GetDevice(command.initiator); |
| 483 | if (device) |
| 484 | { |
| 485 | device->SetPowerStatus((cec_power_status) command.parameters[0]); |
| 486 | return COMMAND_HANDLED; |
| 487 | } |
| 488 | } |
| 489 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 490 | } |
| 491 | |
| 492 | int CCECCommandHandler::HandleRequestActiveSource(const cec_command &command) |
| 493 | { |
| 494 | if (m_processor->CECInitialised()) |
| 495 | { |
| 496 | LIB_CEC->AddLog(CEC_LOG_DEBUG, ">> %i requests active source", (uint8_t) command.initiator); |
| 497 | m_processor->GetDevice(command.initiator)->SetPowerStatus(CEC_POWER_STATUS_ON); |
| 498 | |
| 499 | vector<CCECBusDevice *> devices; |
| 500 | for (size_t iDevicePtr = 0; iDevicePtr < GetMyDevices(devices); iDevicePtr++) |
| 501 | devices[iDevicePtr]->TransmitActiveSource(true); |
| 502 | } |
| 503 | |
| 504 | return COMMAND_HANDLED; |
| 505 | } |
| 506 | |
| 507 | int CCECCommandHandler::HandleRoutingChange(const cec_command &command) |
| 508 | { |
| 509 | if (command.parameters.size == 4) |
| 510 | { |
| 511 | CCECBusDevice *device = GetDevice(command.initiator); |
| 512 | if (device) |
| 513 | { |
| 514 | uint16_t iNewAddress = ((uint16_t)command.parameters[2] << 8) | ((uint16_t)command.parameters[3]); |
| 515 | device->SetActiveRoute(iNewAddress); |
| 516 | return COMMAND_HANDLED; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 521 | } |
| 522 | |
| 523 | int CCECCommandHandler::HandleRoutingInformation(const cec_command &command) |
| 524 | { |
| 525 | if (command.parameters.size == 2) |
| 526 | { |
| 527 | CCECBusDevice *device = GetDevice(command.initiator); |
| 528 | if (device) |
| 529 | { |
| 530 | uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]); |
| 531 | device->SetActiveRoute(iNewAddress); |
| 532 | return COMMAND_HANDLED; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 537 | } |
| 538 | |
| 539 | int CCECCommandHandler::HandleSetMenuLanguage(const cec_command &command) |
| 540 | { |
| 541 | if (command.parameters.size == 3) |
| 542 | { |
| 543 | CCECBusDevice *device = GetDevice(command.initiator); |
| 544 | if (device) |
| 545 | { |
| 546 | cec_menu_language language; |
| 547 | language.device = command.initiator; |
| 548 | for (uint8_t iPtr = 0; iPtr < 4; iPtr++) |
| 549 | language.language[iPtr] = command.parameters[iPtr]; |
| 550 | language.language[3] = 0; |
| 551 | device->SetMenuLanguage(language); |
| 552 | return COMMAND_HANDLED; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 557 | } |
| 558 | |
| 559 | int CCECCommandHandler::HandleSetOSDName(const cec_command &command) |
| 560 | { |
| 561 | if (command.parameters.size > 0) |
| 562 | { |
| 563 | CCECBusDevice *device = GetDevice(command.initiator); |
| 564 | if (device) |
| 565 | { |
| 566 | char buf[1024]; |
| 567 | for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++) |
| 568 | buf[iPtr] = (char)command.parameters[iPtr]; |
| 569 | buf[command.parameters.size] = 0; |
| 570 | |
| 571 | CStdString strName(buf); |
| 572 | device->SetOSDName(strName); |
| 573 | |
| 574 | return COMMAND_HANDLED; |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 579 | } |
| 580 | |
| 581 | int CCECCommandHandler::HandleSetStreamPath(const cec_command &command) |
| 582 | { |
| 583 | if (!m_processor->CECInitialised()) |
| 584 | return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND; |
| 585 | |
| 586 | if (command.parameters.size >= 2) |
| 587 | { |
| 588 | uint16_t iStreamAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]); |
| 589 | LIB_CEC->AddLog(CEC_LOG_DEBUG, ">> %s (%x) sets stream path to physical address %04x", ToString(command.initiator), command.initiator, iStreamAddress); |
| 590 | |
| 591 | /* one of the device handled by libCEC has been made active */ |
| 592 | CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamAddress); |
| 593 | if (device) |
| 594 | { |
| 595 | if (device->IsHandledByLibCEC() && !device->IsActiveSource()) |
| 596 | device->ActivateSource(); |
| 597 | else |
| 598 | { |
| 599 | device->MarkAsActiveSource(); |
| 600 | device->TransmitActiveSource(true); |
| 601 | } |
| 602 | return COMMAND_HANDLED; |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 607 | } |
| 608 | |
| 609 | int CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command &command) |
| 610 | { |
| 611 | if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination)) |
| 612 | { |
| 613 | CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination)); |
| 614 | if (device) |
| 615 | { |
| 616 | if (command.parameters.size >= 2) |
| 617 | { |
| 618 | device->SetPowerStatus(CEC_POWER_STATUS_ON); |
| 619 | device->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON); |
| 620 | uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]); |
| 621 | CCECBusDevice *newActiveDevice = GetDeviceByPhysicalAddress(iNewAddress); |
| 622 | if (newActiveDevice) |
| 623 | newActiveDevice->MarkAsActiveSource(); |
| 624 | if (device->TransmitSetSystemAudioMode(command.initiator, true)) |
| 625 | return COMMAND_HANDLED; |
| 626 | } |
| 627 | else |
| 628 | { |
| 629 | device->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF); |
| 630 | if (device->TransmitSetSystemAudioMode(command.initiator, true)) |
| 631 | return COMMAND_HANDLED; |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND; |
| 637 | } |
| 638 | |
| 639 | int CCECCommandHandler::HandleStandby(const cec_command &command) |
| 640 | { |
| 641 | CCECBusDevice *device = GetDevice(command.initiator); |
| 642 | if (device) |
| 643 | device->SetPowerStatus(CEC_POWER_STATUS_STANDBY); |
| 644 | |
| 645 | return COMMAND_HANDLED; |
| 646 | } |
| 647 | |
| 648 | int CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command &command) |
| 649 | { |
| 650 | if (command.parameters.size == 1) |
| 651 | { |
| 652 | CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator)); |
| 653 | if (device) |
| 654 | { |
| 655 | device->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]); |
| 656 | return COMMAND_HANDLED; |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 661 | } |
| 662 | |
| 663 | int CCECCommandHandler::HandleSetSystemAudioMode(const cec_command &command) |
| 664 | { |
| 665 | if (command.parameters.size == 1) |
| 666 | { |
| 667 | CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator)); |
| 668 | if (device) |
| 669 | { |
| 670 | device->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]); |
| 671 | return COMMAND_HANDLED; |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 676 | } |
| 677 | |
| 678 | int CCECCommandHandler::HandleTextViewOn(const cec_command &command) |
| 679 | { |
| 680 | m_processor->GetDevice(command.initiator)->MarkAsActiveSource(); |
| 681 | return COMMAND_HANDLED; |
| 682 | } |
| 683 | |
| 684 | int CCECCommandHandler::HandleUserControlPressed(const cec_command &command) |
| 685 | { |
| 686 | if (!m_processor->CECInitialised() || |
| 687 | !m_processor->IsHandledByLibCEC(command.destination)) |
| 688 | return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND; |
| 689 | |
| 690 | if (command.parameters.size == 0) |
| 691 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 692 | |
| 693 | CCECBusDevice *device = GetDevice(command.destination); |
| 694 | if (!device) |
| 695 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 696 | |
| 697 | CCECClient *client = device->GetClient(); |
| 698 | if (client) |
| 699 | client->SetCurrentButton((cec_user_control_code) command.parameters[0]); |
| 700 | |
| 701 | if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER || |
| 702 | command.parameters[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION|| |
| 703 | command.parameters[0] == CEC_USER_CONTROL_CODE_POWER_TOGGLE_FUNCTION) |
| 704 | { |
| 705 | bool bPowerOn(true); |
| 706 | |
| 707 | // CEC_USER_CONTROL_CODE_POWER and CEC_USER_CONTROL_CODE_POWER_TOGGLE_FUNCTION operate as a toggle |
| 708 | // assume CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION does not |
| 709 | if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER || |
| 710 | command.parameters[0] == CEC_USER_CONTROL_CODE_POWER_TOGGLE_FUNCTION) |
| 711 | { |
| 712 | cec_power_status status = device->GetCurrentPowerStatus(); |
| 713 | bPowerOn = !(status == CEC_POWER_STATUS_ON || status == CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); |
| 714 | } |
| 715 | |
| 716 | if (bPowerOn) |
| 717 | { |
| 718 | device->ActivateSource(); |
| 719 | } |
| 720 | else |
| 721 | { |
| 722 | device->MarkAsInactiveSource(); |
| 723 | device->TransmitInactiveSource(); |
| 724 | device->SetMenuState(CEC_MENU_STATE_DEACTIVATED); |
| 725 | } |
| 726 | } |
| 727 | else if (command.parameters[0] != CEC_USER_CONTROL_CODE_POWER_OFF_FUNCTION) |
| 728 | { |
| 729 | // we're not marked as active source, but the tv sends keypresses to us, so assume it forgot to activate us |
| 730 | if (!device->IsActiveSource() && command.initiator == CECDEVICE_TV) |
| 731 | device->MarkAsActiveSource(); |
| 732 | } |
| 733 | |
| 734 | return COMMAND_HANDLED; |
| 735 | } |
| 736 | |
| 737 | int CCECCommandHandler::HandleUserControlRelease(const cec_command &command) |
| 738 | { |
| 739 | if (!m_processor->CECInitialised() || |
| 740 | !m_processor->IsHandledByLibCEC(command.destination)) |
| 741 | return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND; |
| 742 | |
| 743 | CCECClient *client = m_processor->GetClient(command.destination); |
| 744 | if (client) |
| 745 | client->AddKey(); |
| 746 | |
| 747 | return COMMAND_HANDLED; |
| 748 | } |
| 749 | |
| 750 | int CCECCommandHandler::HandleVendorCommand(const cec_command & UNUSED(command)) |
| 751 | { |
| 752 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 753 | } |
| 754 | |
| 755 | int CCECCommandHandler::HandleVendorRemoteButtonDown(const cec_command& command) |
| 756 | { |
| 757 | if (command.parameters.size == 0) |
| 758 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 759 | |
| 760 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "unhandled vendor remote button received with keycode %x", command.parameters[0]); |
| 761 | return COMMAND_HANDLED; |
| 762 | } |
| 763 | |
| 764 | void CCECCommandHandler::UnhandledCommand(const cec_command &command, const cec_abort_reason reason) |
| 765 | { |
| 766 | if (m_processor->IsHandledByLibCEC(command.destination)) |
| 767 | { |
| 768 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "sending abort with opcode %02x and reason '%s' to %s", command.opcode, ToString(reason), ToString(command.initiator)); |
| 769 | m_processor->TransmitAbort(command.destination, command.initiator, command.opcode, reason); |
| 770 | |
| 771 | if (reason == CEC_ABORT_REASON_INVALID_OPERAND) |
| 772 | RequestEmailFromCustomer(command); |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | size_t CCECCommandHandler::GetMyDevices(vector<CCECBusDevice *> &devices) const |
| 777 | { |
| 778 | size_t iReturn(0); |
| 779 | |
| 780 | cec_logical_addresses addresses = m_processor->GetLogicalAddresses(); |
| 781 | for (uint8_t iPtr = CECDEVICE_TV; iPtr < CECDEVICE_BROADCAST; iPtr++) |
| 782 | { |
| 783 | if (addresses[iPtr]) |
| 784 | { |
| 785 | devices.push_back(GetDevice((cec_logical_address) iPtr)); |
| 786 | ++iReturn; |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | return iReturn; |
| 791 | } |
| 792 | |
| 793 | CCECBusDevice *CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress) const |
| 794 | { |
| 795 | return m_processor->GetDevice(iLogicalAddress); |
| 796 | } |
| 797 | |
| 798 | CCECBusDevice *CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress) const |
| 799 | { |
| 800 | return m_processor->GetDeviceByPhysicalAddress(iPhysicalAddress); |
| 801 | } |
| 802 | |
| 803 | bool CCECCommandHandler::SetVendorId(const cec_command &command) |
| 804 | { |
| 805 | bool bChanged(false); |
| 806 | if (command.parameters.size < 3) |
| 807 | { |
| 808 | LIB_CEC->AddLog(CEC_LOG_WARNING, "invalid vendor ID received"); |
| 809 | return bChanged; |
| 810 | } |
| 811 | |
| 812 | uint64_t iVendorId = ((uint64_t)command.parameters[0] << 16) + |
| 813 | ((uint64_t)command.parameters[1] << 8) + |
| 814 | (uint64_t)command.parameters[2]; |
| 815 | |
| 816 | CCECBusDevice *device = GetDevice((cec_logical_address) command.initiator); |
| 817 | if (device) |
| 818 | bChanged = device->SetVendorId(iVendorId); |
| 819 | return bChanged; |
| 820 | } |
| 821 | |
| 822 | void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress, uint16_t iNewAddress) |
| 823 | { |
| 824 | if (!m_processor->IsHandledByLibCEC(iAddress)) |
| 825 | { |
| 826 | CCECBusDevice *otherDevice = m_processor->GetDeviceByPhysicalAddress(iNewAddress); |
| 827 | CCECClient *client = otherDevice ? otherDevice->GetClient() : NULL; |
| 828 | |
| 829 | CCECBusDevice *device = m_processor->GetDevice(iAddress); |
| 830 | if (device) |
| 831 | device->SetPhysicalAddress(iNewAddress); |
| 832 | else |
| 833 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "device with logical address %X not found", iAddress); |
| 834 | |
| 835 | /* another device reported the same physical address as ours */ |
| 836 | if (client) |
| 837 | { |
| 838 | libcec_parameter param; |
| 839 | param.paramType = CEC_PARAMETER_TYPE_STRING; |
| 840 | param.paramData = (void*)"Physical address in use by another device. Please verify your settings"; |
| 841 | client->Alert(CEC_ALERT_PHYSICAL_ADDRESS_ERROR, param); |
| 842 | client->ResetPhysicalAddress(); |
| 843 | } |
| 844 | } |
| 845 | else |
| 846 | { |
| 847 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "ignore physical address report for device %s (%X) because it's marked as handled by libCEC", ToString(iAddress), iAddress); |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | bool CCECCommandHandler::PowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination) |
| 852 | { |
| 853 | if (iDestination == CECDEVICE_TV) |
| 854 | return TransmitImageViewOn(iInitiator, iDestination); |
| 855 | |
| 856 | return TransmitKeypress(iInitiator, iDestination, CEC_USER_CONTROL_CODE_POWER) && |
| 857 | TransmitKeyRelease(iInitiator, iDestination); |
| 858 | } |
| 859 | |
| 860 | bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator, const cec_logical_address iDestination) |
| 861 | { |
| 862 | cec_command command; |
| 863 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_IMAGE_VIEW_ON); |
| 864 | |
| 865 | if (Transmit(command, false, false)) |
| 866 | { |
| 867 | CCECBusDevice* dest = m_processor->GetDevice(iDestination); |
| 868 | if (dest && dest->GetCurrentPowerStatus() != CEC_POWER_STATUS_ON) |
| 869 | dest->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); |
| 870 | return true; |
| 871 | } |
| 872 | return false; |
| 873 | } |
| 874 | |
| 875 | bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator, const cec_logical_address iDestination) |
| 876 | { |
| 877 | cec_command command; |
| 878 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_STANDBY); |
| 879 | |
| 880 | return Transmit(command, false, false); |
| 881 | } |
| 882 | |
| 883 | bool CCECCommandHandler::TransmitRequestActiveSource(const cec_logical_address iInitiator, bool bWaitForResponse /* = true */) |
| 884 | { |
| 885 | cec_command command; |
| 886 | cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REQUEST_ACTIVE_SOURCE); |
| 887 | |
| 888 | return Transmit(command, !bWaitForResponse, false); |
| 889 | } |
| 890 | |
| 891 | bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */) |
| 892 | { |
| 893 | cec_command command; |
| 894 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_CEC_VERSION); |
| 895 | |
| 896 | return Transmit(command, !bWaitForResponse, false); |
| 897 | } |
| 898 | |
| 899 | bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */) |
| 900 | { |
| 901 | cec_command command; |
| 902 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_MENU_LANGUAGE); |
| 903 | |
| 904 | return Transmit(command, !bWaitForResponse, false); |
| 905 | } |
| 906 | |
| 907 | bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */) |
| 908 | { |
| 909 | cec_command command; |
| 910 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_OSD_NAME); |
| 911 | |
| 912 | return Transmit(command, !bWaitForResponse, false); |
| 913 | } |
| 914 | |
| 915 | bool CCECCommandHandler::TransmitRequestAudioStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */) |
| 916 | { |
| 917 | cec_command command; |
| 918 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_AUDIO_STATUS); |
| 919 | |
| 920 | return Transmit(command, !bWaitForResponse, false); |
| 921 | } |
| 922 | |
| 923 | bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */) |
| 924 | { |
| 925 | cec_command command; |
| 926 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS); |
| 927 | |
| 928 | return Transmit(command, !bWaitForResponse, false); |
| 929 | } |
| 930 | |
| 931 | bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bUpdate, bool bWaitForResponse /* = true */) |
| 932 | { |
| 933 | if (iDestination == CECDEVICE_TV) |
| 934 | { |
| 935 | int64_t now(GetTimeMs()); |
| 936 | if (!bUpdate && now - m_iPowerStatusRequested < REQUEST_POWER_STATUS_TIMEOUT) |
| 937 | return true; |
| 938 | m_iPowerStatusRequested = now; |
| 939 | } |
| 940 | |
| 941 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< requesting power status of '%s' (%X)", m_busDevice->GetLogicalAddressName(), iDestination); |
| 942 | |
| 943 | cec_command command; |
| 944 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS); |
| 945 | |
| 946 | return Transmit(command, !bWaitForResponse, false); |
| 947 | } |
| 948 | |
| 949 | bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */) |
| 950 | { |
| 951 | cec_command command; |
| 952 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID); |
| 953 | |
| 954 | return Transmit(command, !bWaitForResponse, false); |
| 955 | } |
| 956 | |
| 957 | bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress, bool bIsReply) |
| 958 | { |
| 959 | cec_command command; |
| 960 | cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE); |
| 961 | command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF)); |
| 962 | command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF)); |
| 963 | |
| 964 | return Transmit(command, false, bIsReply); |
| 965 | } |
| 966 | |
| 967 | bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_version cecVersion, bool bIsReply) |
| 968 | { |
| 969 | cec_command command; |
| 970 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_CEC_VERSION); |
| 971 | command.parameters.PushBack((uint8_t)cecVersion); |
| 972 | |
| 973 | return Transmit(command, false, bIsReply); |
| 974 | } |
| 975 | |
| 976 | bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress) |
| 977 | { |
| 978 | cec_command command; |
| 979 | cec_command::Format(command, iInitiator, CECDEVICE_TV, CEC_OPCODE_INACTIVE_SOURCE); |
| 980 | command.parameters.PushBack((iPhysicalAddress >> 8) & 0xFF); |
| 981 | command.parameters.PushBack(iPhysicalAddress & 0xFF); |
| 982 | |
| 983 | return Transmit(command, false, false); |
| 984 | } |
| 985 | |
| 986 | bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_menu_state menuState, bool bIsReply) |
| 987 | { |
| 988 | cec_command command; |
| 989 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_MENU_STATUS); |
| 990 | command.parameters.PushBack((uint8_t)menuState); |
| 991 | |
| 992 | return Transmit(command, false, bIsReply); |
| 993 | } |
| 994 | |
| 995 | bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, std::string strDeviceName, bool bIsReply) |
| 996 | { |
| 997 | cec_command command; |
| 998 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_NAME); |
| 999 | for (size_t iPtr = 0; iPtr < strDeviceName.length(); iPtr++) |
| 1000 | command.parameters.PushBack(strDeviceName.at(iPtr)); |
| 1001 | |
| 1002 | return Transmit(command, false, bIsReply); |
| 1003 | } |
| 1004 | |
| 1005 | bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_display_control duration, const char *strMessage, bool bIsReply) |
| 1006 | { |
| 1007 | cec_command command; |
| 1008 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_STRING); |
| 1009 | command.parameters.PushBack((uint8_t)duration); |
| 1010 | |
| 1011 | size_t iLen = strlen(strMessage); |
| 1012 | if (iLen > 13) iLen = 13; |
| 1013 | |
| 1014 | for (size_t iPtr = 0; iPtr < iLen; iPtr++) |
| 1015 | command.parameters.PushBack(strMessage[iPtr]); |
| 1016 | |
| 1017 | return Transmit(command, false, bIsReply); |
| 1018 | } |
| 1019 | |
| 1020 | bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator, uint16_t iPhysicalAddress, cec_device_type type, bool bIsReply) |
| 1021 | { |
| 1022 | cec_command command; |
| 1023 | cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS); |
| 1024 | command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF)); |
| 1025 | command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF)); |
| 1026 | command.parameters.PushBack((uint8_t) (type)); |
| 1027 | |
| 1028 | return Transmit(command, false, bIsReply); |
| 1029 | } |
| 1030 | |
| 1031 | bool CCECCommandHandler::TransmitSetMenuLanguage(const cec_logical_address iInitiator, const char lang[4], bool bIsReply) |
| 1032 | { |
| 1033 | cec_command command; |
| 1034 | command.Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_SET_MENU_LANGUAGE); |
| 1035 | command.parameters.PushBack((uint8_t) lang[0]); |
| 1036 | command.parameters.PushBack((uint8_t) lang[1]); |
| 1037 | command.parameters.PushBack((uint8_t) lang[2]); |
| 1038 | |
| 1039 | return Transmit(command, false, bIsReply); |
| 1040 | } |
| 1041 | |
| 1042 | bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bIsReply) |
| 1043 | { |
| 1044 | cec_command command; |
| 1045 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_NONE); |
| 1046 | |
| 1047 | return Transmit(command, false, bIsReply); |
| 1048 | } |
| 1049 | |
| 1050 | bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_power_status state, bool bIsReply) |
| 1051 | { |
| 1052 | cec_command command; |
| 1053 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_POWER_STATUS); |
| 1054 | command.parameters.PushBack((uint8_t) state); |
| 1055 | |
| 1056 | return Transmit(command, false, bIsReply); |
| 1057 | } |
| 1058 | |
| 1059 | bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator, const cec_logical_address UNUSED(iDestination), uint64_t iVendorId, bool bIsReply) |
| 1060 | { |
| 1061 | cec_command command; |
| 1062 | cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_DEVICE_VENDOR_ID); |
| 1063 | |
| 1064 | command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 16) & 0xFF)); |
| 1065 | command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 8) & 0xFF)); |
| 1066 | command.parameters.PushBack((uint8_t) ((uint64_t)iVendorId & 0xFF)); |
| 1067 | |
| 1068 | return Transmit(command, false, bIsReply); |
| 1069 | } |
| 1070 | |
| 1071 | bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, uint8_t state, bool bIsReply) |
| 1072 | { |
| 1073 | cec_command command; |
| 1074 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_AUDIO_STATUS); |
| 1075 | command.parameters.PushBack(state); |
| 1076 | |
| 1077 | return Transmit(command, false, bIsReply); |
| 1078 | } |
| 1079 | |
| 1080 | bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state, bool bIsReply) |
| 1081 | { |
| 1082 | cec_command command; |
| 1083 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE); |
| 1084 | command.parameters.PushBack((uint8_t)state); |
| 1085 | |
| 1086 | return Transmit(command, false, bIsReply); |
| 1087 | } |
| 1088 | |
| 1089 | bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath, bool bIsReply) |
| 1090 | { |
| 1091 | if (m_busDevice->GetLogicalAddress() != CECDEVICE_TV) |
| 1092 | { |
| 1093 | LIB_CEC->AddLog(CEC_LOG_ERROR, "only the TV is allowed to send CEC_OPCODE_SET_STREAM_PATH"); |
| 1094 | return false; |
| 1095 | } |
| 1096 | cec_command command; |
| 1097 | cec_command::Format(command, m_busDevice->GetLogicalAddress(), CECDEVICE_BROADCAST, CEC_OPCODE_SET_STREAM_PATH); |
| 1098 | command.parameters.PushBack((uint8_t) ((iStreamPath >> 8) & 0xFF)); |
| 1099 | command.parameters.PushBack((uint8_t) (iStreamPath & 0xFF)); |
| 1100 | |
| 1101 | return Transmit(command, false, bIsReply); |
| 1102 | } |
| 1103 | |
| 1104 | bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state, bool bIsReply) |
| 1105 | { |
| 1106 | cec_command command; |
| 1107 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS); |
| 1108 | command.parameters.PushBack((uint8_t)state); |
| 1109 | |
| 1110 | return Transmit(command, false, bIsReply); |
| 1111 | } |
| 1112 | |
| 1113 | bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_deck_info state, bool bIsReply) |
| 1114 | { |
| 1115 | cec_command command; |
| 1116 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_DECK_STATUS); |
| 1117 | command.PushBack((uint8_t)state); |
| 1118 | |
| 1119 | return Transmit(command, false, bIsReply); |
| 1120 | } |
| 1121 | |
| 1122 | bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */) |
| 1123 | { |
| 1124 | cec_command command; |
| 1125 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_PRESSED); |
| 1126 | command.parameters.PushBack((uint8_t)key); |
| 1127 | |
| 1128 | return Transmit(command, !bWait, false); |
| 1129 | } |
| 1130 | |
| 1131 | bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWait /* = true */) |
| 1132 | { |
| 1133 | cec_command command; |
| 1134 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_RELEASE); |
| 1135 | |
| 1136 | return Transmit(command, !bWait, false); |
| 1137 | } |
| 1138 | |
| 1139 | bool CCECCommandHandler::Transmit(cec_command &command, bool bSuppressWait, bool bIsReply) |
| 1140 | { |
| 1141 | bool bReturn(false); |
| 1142 | cec_opcode expectedResponse(cec_command::GetResponseOpcode(command.opcode)); |
| 1143 | bool bExpectResponse(expectedResponse != CEC_OPCODE_NONE && !bSuppressWait); |
| 1144 | command.transmit_timeout = m_iTransmitTimeout; |
| 1145 | |
| 1146 | if (command.initiator == CECDEVICE_UNKNOWN) |
| 1147 | { |
| 1148 | LIB_CEC->AddLog(CEC_LOG_ERROR, "not transmitting a command without a valid initiator"); |
| 1149 | return bReturn; |
| 1150 | } |
| 1151 | |
| 1152 | // check whether the destination is not marked as not present or handled by libCEC |
| 1153 | if (command.destination != CECDEVICE_BROADCAST && command.opcode_set) |
| 1154 | { |
| 1155 | CCECBusDevice* destinationDevice = m_processor->GetDevice(command.destination); |
| 1156 | cec_bus_device_status status = destinationDevice ? destinationDevice->GetStatus() : CEC_DEVICE_STATUS_NOT_PRESENT; |
| 1157 | if (status == CEC_DEVICE_STATUS_NOT_PRESENT) |
| 1158 | { |
| 1159 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "not sending command '%s': destination device '%s' marked as not present", ToString(command.opcode),ToString(command.destination)); |
| 1160 | return bReturn; |
| 1161 | } |
| 1162 | else if (status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC) |
| 1163 | { |
| 1164 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "not sending command '%s': destination device '%s' marked as handled by libCEC", ToString(command.opcode),ToString(command.destination)); |
| 1165 | return bReturn; |
| 1166 | } |
| 1167 | else if (destinationDevice->IsUnsupportedFeature(command.opcode)) |
| 1168 | { |
| 1169 | return true; |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | { |
| 1174 | uint8_t iTries(0), iMaxTries(m_iTransmitRetries + 1); |
| 1175 | while (!bReturn && ++iTries <= iMaxTries) |
| 1176 | { |
| 1177 | if ((bReturn = m_processor->Transmit(command, bIsReply)) == true) |
| 1178 | { |
| 1179 | #ifdef CEC_DEBUGGING |
| 1180 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "command transmitted"); |
| 1181 | #endif |
| 1182 | if (bExpectResponse) |
| 1183 | { |
| 1184 | bReturn = m_busDevice->WaitForOpcode(expectedResponse); |
| 1185 | LIB_CEC->AddLog(CEC_LOG_DEBUG, bReturn ? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse, ToString(expectedResponse)); |
| 1186 | } |
| 1187 | } |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | return bReturn; |
| 1192 | } |
| 1193 | |
| 1194 | bool CCECCommandHandler::ActivateSource(bool bTransmitDelayedCommandsOnly /* = false */) |
| 1195 | { |
| 1196 | if (m_busDevice->IsActiveSource() && |
| 1197 | m_busDevice->IsHandledByLibCEC()) |
| 1198 | { |
| 1199 | { |
| 1200 | CLockObject lock(m_mutex); |
| 1201 | // check if we need to send a delayed source switch |
| 1202 | if (bTransmitDelayedCommandsOnly) |
| 1203 | { |
| 1204 | if (m_iActiveSourcePending == 0 || GetTimeMs() < m_iActiveSourcePending) |
| 1205 | return false; |
| 1206 | |
| 1207 | #ifdef CEC_DEBUGGING |
| 1208 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "transmitting delayed activate source command"); |
| 1209 | #endif |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | // update the power state and menu state |
| 1214 | if (!bTransmitDelayedCommandsOnly) |
| 1215 | { |
| 1216 | m_busDevice->SetPowerStatus(CEC_POWER_STATUS_ON); |
| 1217 | m_busDevice->SetMenuState(CEC_MENU_STATE_ACTIVATED); |
| 1218 | } |
| 1219 | |
| 1220 | // vendor specific hook |
| 1221 | VendorPreActivateSourceHook(); |
| 1222 | |
| 1223 | // power on the TV |
| 1224 | CCECBusDevice* tv = m_processor->GetDevice(CECDEVICE_TV); |
| 1225 | bool bTvPresent = (tv && tv->GetStatus() == CEC_DEVICE_STATUS_PRESENT); |
| 1226 | bool bActiveSourceFailed(false); |
| 1227 | if (bTvPresent) |
| 1228 | bActiveSourceFailed = !tv->PowerOn(m_busDevice->GetLogicalAddress()); |
| 1229 | else |
| 1230 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "TV not present, not sending 'image view on'"); |
| 1231 | |
| 1232 | // check if we're allowed to switch sources |
| 1233 | bool bSourceSwitchAllowed = SourceSwitchAllowed(); |
| 1234 | if (!bSourceSwitchAllowed) |
| 1235 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "source switch is currently not allowed by command handler"); |
| 1236 | |
| 1237 | // switch sources (if allowed) |
| 1238 | if (!bActiveSourceFailed && bSourceSwitchAllowed) |
| 1239 | { |
| 1240 | bActiveSourceFailed = !m_busDevice->TransmitActiveSource(false); |
| 1241 | if (bTvPresent && !bActiveSourceFailed) |
| 1242 | bActiveSourceFailed = !m_busDevice->TransmitMenuState(CECDEVICE_TV, false); |
| 1243 | |
| 1244 | // update the deck status for playback devices |
| 1245 | if (bTvPresent && !bActiveSourceFailed) |
| 1246 | { |
| 1247 | CCECPlaybackDevice *playbackDevice = m_busDevice->AsPlaybackDevice(); |
| 1248 | if (playbackDevice && SendDeckStatusUpdateOnActiveSource()) |
| 1249 | bActiveSourceFailed = !playbackDevice->TransmitDeckStatus(CECDEVICE_TV, false); |
| 1250 | } |
| 1251 | |
| 1252 | // update system audio mode for audiosystem devices |
| 1253 | if (bTvPresent && !bActiveSourceFailed) |
| 1254 | { |
| 1255 | CCECAudioSystem* audioDevice = m_busDevice->AsAudioSystem(); |
| 1256 | if (audioDevice) |
| 1257 | bActiveSourceFailed = !audioDevice->TransmitSetSystemAudioMode(CECDEVICE_TV, false); |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | // retry later |
| 1262 | if (bActiveSourceFailed || !bSourceSwitchAllowed) |
| 1263 | { |
| 1264 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "failed to make '%s' the active source. will retry later", m_busDevice->GetLogicalAddressName()); |
| 1265 | int64_t now(GetTimeMs()); |
| 1266 | CLockObject lock(m_mutex); |
| 1267 | if (m_iActiveSourcePending == 0 || m_iActiveSourcePending < now) |
| 1268 | m_iActiveSourcePending = now + (int64_t)CEC_ACTIVE_SOURCE_SWITCH_RETRY_TIME_MS; |
| 1269 | return false; |
| 1270 | } |
| 1271 | else |
| 1272 | { |
| 1273 | CLockObject lock(m_mutex); |
| 1274 | // clear previous pending active source command |
| 1275 | m_iActiveSourcePending = 0; |
| 1276 | } |
| 1277 | |
| 1278 | // mark the handler as initialised |
| 1279 | CLockObject lock(m_mutex); |
| 1280 | m_bHandlerInited = true; |
| 1281 | } |
| 1282 | return true; |
| 1283 | } |
| 1284 | |
| 1285 | void CCECCommandHandler::ScheduleActivateSource(uint64_t iDelay) |
| 1286 | { |
| 1287 | CLockObject lock(m_mutex); |
| 1288 | m_iActiveSourcePending = GetTimeMs() + iDelay; |
| 1289 | } |
| 1290 | |
| 1291 | void CCECCommandHandler::RequestEmailFromCustomer(const cec_command& command) |
| 1292 | { |
| 1293 | bool bInserted(false); |
| 1294 | map<cec_opcode, vector<cec_command> >::iterator it = m_logsRequested.find(command.opcode); |
| 1295 | if (it != m_logsRequested.end()) |
| 1296 | { |
| 1297 | for (vector<cec_command>::const_iterator it2 = it->second.begin(); it2 != it->second.end(); it2++) |
| 1298 | { |
| 1299 | // we already logged this one |
| 1300 | if ((*it2).parameters == command.parameters) |
| 1301 | return; |
| 1302 | } |
| 1303 | |
| 1304 | it->second.push_back(command); |
| 1305 | bInserted = true; |
| 1306 | } |
| 1307 | |
| 1308 | if (!bInserted) |
| 1309 | { |
| 1310 | vector<cec_command> commands; |
| 1311 | commands.push_back(command); |
| 1312 | m_logsRequested.insert(make_pair(command.opcode, commands)); |
| 1313 | } |
| 1314 | |
| 1315 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "Unmapped code detected. Please send an email to support@pulse-eight.com with the following details, and if you pressed a key, tell us which one you pressed, and we'll add support for this it.\nCEC command: %s\nVendor ID: %s (%06x)", ToString(command).c_str(), ToString(m_vendorId), m_vendorId); |
| 1316 | } |
| 1317 | |