| 1 | /* |
| 2 | * This file is part of the libCEC(R) library. |
| 3 | * |
| 4 | * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved. |
| 5 | * libCEC(R) is an original work, containing original code. |
| 6 | * |
| 7 | * libCEC(R) is a trademark of Pulse-Eight Limited. |
| 8 | * |
| 9 | * This program is dual-licensed; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 22 | * |
| 23 | * |
| 24 | * Alternatively, you can license this library under a commercial license, |
| 25 | * please contact Pulse-Eight Licensing for more information. |
| 26 | * |
| 27 | * For more information contact: |
| 28 | * Pulse-Eight Licensing <license@pulse-eight.com> |
| 29 | * http://www.pulse-eight.com/ |
| 30 | * http://www.pulse-eight.net/ |
| 31 | */ |
| 32 | |
| 33 | #include "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 | return COMMAND_HANDLED; |
| 261 | } |
| 262 | |
| 263 | int CCECCommandHandler::HandleFeatureAbort(const cec_command &command) |
| 264 | { |
| 265 | if (command.parameters.size == 2 && |
| 266 | (command.parameters[1] == CEC_ABORT_REASON_UNRECOGNIZED_OPCODE || |
| 267 | command.parameters[1] == CEC_ABORT_REASON_REFUSED)) |
| 268 | m_processor->GetDevice(command.initiator)->SetUnsupportedFeature((cec_opcode)command.parameters[0]); |
| 269 | return COMMAND_HANDLED; |
| 270 | } |
| 271 | |
| 272 | int CCECCommandHandler::HandleGetCecVersion(const cec_command &command) |
| 273 | { |
| 274 | if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination)) |
| 275 | { |
| 276 | CCECBusDevice *device = GetDevice(command.destination); |
| 277 | if (device && device->TransmitCECVersion(command.initiator, true)) |
| 278 | return COMMAND_HANDLED; |
| 279 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 280 | } |
| 281 | |
| 282 | return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND; |
| 283 | } |
| 284 | |
| 285 | int CCECCommandHandler::HandleGiveAudioStatus(const cec_command &command) |
| 286 | { |
| 287 | if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination)) |
| 288 | { |
| 289 | CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination)); |
| 290 | if (device && device->TransmitAudioStatus(command.initiator, true)) |
| 291 | return COMMAND_HANDLED; |
| 292 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 293 | } |
| 294 | |
| 295 | return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND; |
| 296 | } |
| 297 | |
| 298 | int CCECCommandHandler::HandleGiveDeckStatus(const cec_command &command) |
| 299 | { |
| 300 | if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination)) |
| 301 | { |
| 302 | CCECPlaybackDevice *device = CCECBusDevice::AsPlaybackDevice(GetDevice(command.destination)); |
| 303 | if (device && device->TransmitDeckStatus(command.initiator, true)) |
| 304 | return COMMAND_HANDLED; |
| 305 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 306 | } |
| 307 | |
| 308 | return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND; |
| 309 | } |
| 310 | |
| 311 | int CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command) |
| 312 | { |
| 313 | if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination)) |
| 314 | { |
| 315 | CCECBusDevice *device = GetDevice(command.destination); |
| 316 | if (device && device->TransmitPowerState(command.initiator, true)) |
| 317 | return COMMAND_HANDLED; |
| 318 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 319 | } |
| 320 | |
| 321 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 322 | } |
| 323 | |
| 324 | int CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command &command) |
| 325 | { |
| 326 | if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination)) |
| 327 | { |
| 328 | CCECBusDevice *device = GetDevice(command.destination); |
| 329 | if (device && device->TransmitVendorID(command.initiator, true, true)) |
| 330 | return COMMAND_HANDLED; |
| 331 | } |
| 332 | |
| 333 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 334 | } |
| 335 | |
| 336 | int CCECCommandHandler::HandleGiveOSDName(const cec_command &command) |
| 337 | { |
| 338 | if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination)) |
| 339 | { |
| 340 | CCECBusDevice *device = GetDevice(command.destination); |
| 341 | if (device && device->TransmitOSDName(command.initiator, true)) |
| 342 | return COMMAND_HANDLED; |
| 343 | } |
| 344 | |
| 345 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 346 | } |
| 347 | |
| 348 | int CCECCommandHandler::HandleGivePhysicalAddress(const cec_command &command) |
| 349 | { |
| 350 | if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination)) |
| 351 | { |
| 352 | CCECBusDevice *device = GetDevice(command.destination); |
| 353 | if (device && device->TransmitPhysicalAddress(true)) |
| 354 | return COMMAND_HANDLED; |
| 355 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 356 | } |
| 357 | |
| 358 | return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND; |
| 359 | } |
| 360 | |
| 361 | int CCECCommandHandler::HandleGiveMenuLanguage(const cec_command &command) |
| 362 | { |
| 363 | if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination)) |
| 364 | { |
| 365 | CCECBusDevice *device = GetDevice(command.destination); |
| 366 | if (device && device->TransmitSetMenuLanguage(command.initiator, true)) |
| 367 | return COMMAND_HANDLED; |
| 368 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 369 | } |
| 370 | |
| 371 | return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND; |
| 372 | } |
| 373 | |
| 374 | int CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command &command) |
| 375 | { |
| 376 | if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination)) |
| 377 | { |
| 378 | CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination)); |
| 379 | if (device && device->TransmitSystemAudioModeStatus(command.initiator, true)) |
| 380 | return COMMAND_HANDLED; |
| 381 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 382 | } |
| 383 | |
| 384 | return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND; |
| 385 | } |
| 386 | |
| 387 | int CCECCommandHandler::HandleImageViewOn(const cec_command &command) |
| 388 | { |
| 389 | CCECBusDevice *device = GetDevice(command.destination); |
| 390 | if (device && device->GetCurrentStatus() == CEC_DEVICE_STATUS_PRESENT) |
| 391 | { |
| 392 | if (device->GetCurrentPowerStatus() == CEC_POWER_STATUS_STANDBY || |
| 393 | device->GetCurrentPowerStatus() == CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY) |
| 394 | device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); |
| 395 | CCECBusDevice* tv = GetDevice(CECDEVICE_TV); |
| 396 | if (tv) |
| 397 | tv->OnImageViewOnSent(false); |
| 398 | } |
| 399 | return COMMAND_HANDLED; |
| 400 | } |
| 401 | |
| 402 | int CCECCommandHandler::HandleMenuRequest(const cec_command &command) |
| 403 | { |
| 404 | if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination)) |
| 405 | { |
| 406 | CCECBusDevice *device = GetDevice(command.destination); |
| 407 | if (device) |
| 408 | { |
| 409 | CCECClient *client = device->GetClient(); |
| 410 | if (client) |
| 411 | { |
| 412 | if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_ACTIVATE) |
| 413 | { |
| 414 | if (client->MenuStateChanged(CEC_MENU_STATE_ACTIVATED) == 1) |
| 415 | device->SetMenuState(CEC_MENU_STATE_ACTIVATED); |
| 416 | } |
| 417 | else if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_DEACTIVATE) |
| 418 | { |
| 419 | if (client->MenuStateChanged(CEC_MENU_STATE_DEACTIVATED) == 1) |
| 420 | device->SetMenuState(CEC_MENU_STATE_DEACTIVATED); |
| 421 | } |
| 422 | } |
| 423 | if (device->TransmitMenuState(command.initiator, true)) |
| 424 | return COMMAND_HANDLED; |
| 425 | } |
| 426 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 427 | } |
| 428 | |
| 429 | return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND; |
| 430 | } |
| 431 | |
| 432 | bool CCECCommandHandler::HandlePoll(const cec_command &command) |
| 433 | { |
| 434 | m_busDevice->HandlePoll(command.destination); |
| 435 | return true; |
| 436 | } |
| 437 | |
| 438 | int CCECCommandHandler::HandleReportAudioStatus(const cec_command &command) |
| 439 | { |
| 440 | if (command.parameters.size == 1) |
| 441 | { |
| 442 | CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator)); |
| 443 | if (device) |
| 444 | { |
| 445 | device->SetAudioStatus(command.parameters[0]); |
| 446 | return COMMAND_HANDLED; |
| 447 | } |
| 448 | } |
| 449 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 450 | } |
| 451 | |
| 452 | int CCECCommandHandler::HandleReportPhysicalAddress(const cec_command &command) |
| 453 | { |
| 454 | if (command.parameters.size == 3) |
| 455 | { |
| 456 | uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]); |
| 457 | SetPhysicalAddress(command.initiator, iNewAddress); |
| 458 | return COMMAND_HANDLED; |
| 459 | } |
| 460 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 461 | } |
| 462 | |
| 463 | int CCECCommandHandler::HandleReportPowerStatus(const cec_command &command) |
| 464 | { |
| 465 | if (command.parameters.size == 1) |
| 466 | { |
| 467 | CCECBusDevice *device = GetDevice(command.initiator); |
| 468 | if (device) |
| 469 | { |
| 470 | device->SetPowerStatus((cec_power_status) command.parameters[0]); |
| 471 | return COMMAND_HANDLED; |
| 472 | } |
| 473 | } |
| 474 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 475 | } |
| 476 | |
| 477 | int CCECCommandHandler::HandleRequestActiveSource(const cec_command &command) |
| 478 | { |
| 479 | if (m_processor->CECInitialised()) |
| 480 | { |
| 481 | LIB_CEC->AddLog(CEC_LOG_DEBUG, ">> %i requests active source", (uint8_t) command.initiator); |
| 482 | m_processor->GetDevice(command.initiator)->SetPowerStatus(CEC_POWER_STATUS_ON); |
| 483 | |
| 484 | vector<CCECBusDevice *> devices; |
| 485 | for (size_t iDevicePtr = 0; iDevicePtr < GetMyDevices(devices); iDevicePtr++) |
| 486 | devices[iDevicePtr]->TransmitActiveSource(true); |
| 487 | } |
| 488 | |
| 489 | return COMMAND_HANDLED; |
| 490 | } |
| 491 | |
| 492 | int CCECCommandHandler::HandleRoutingChange(const cec_command &command) |
| 493 | { |
| 494 | if (command.parameters.size == 4) |
| 495 | { |
| 496 | CCECBusDevice *device = GetDevice(command.initiator); |
| 497 | if (device) |
| 498 | { |
| 499 | uint16_t iNewAddress = ((uint16_t)command.parameters[2] << 8) | ((uint16_t)command.parameters[3]); |
| 500 | device->SetActiveRoute(iNewAddress); |
| 501 | return COMMAND_HANDLED; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 506 | } |
| 507 | |
| 508 | int CCECCommandHandler::HandleRoutingInformation(const cec_command &command) |
| 509 | { |
| 510 | if (command.parameters.size == 2) |
| 511 | { |
| 512 | CCECBusDevice *device = GetDevice(command.initiator); |
| 513 | if (device) |
| 514 | { |
| 515 | uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]); |
| 516 | device->SetActiveRoute(iNewAddress); |
| 517 | return COMMAND_HANDLED; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 522 | } |
| 523 | |
| 524 | int CCECCommandHandler::HandleSetMenuLanguage(const cec_command &command) |
| 525 | { |
| 526 | if (command.parameters.size == 3) |
| 527 | { |
| 528 | CCECBusDevice *device = GetDevice(command.initiator); |
| 529 | if (device) |
| 530 | { |
| 531 | cec_menu_language language; |
| 532 | language.device = command.initiator; |
| 533 | for (uint8_t iPtr = 0; iPtr < 4; iPtr++) |
| 534 | language.language[iPtr] = command.parameters[iPtr]; |
| 535 | language.language[3] = 0; |
| 536 | device->SetMenuLanguage(language); |
| 537 | return COMMAND_HANDLED; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 542 | } |
| 543 | |
| 544 | int CCECCommandHandler::HandleSetOSDName(const cec_command &command) |
| 545 | { |
| 546 | if (command.parameters.size > 0) |
| 547 | { |
| 548 | CCECBusDevice *device = GetDevice(command.initiator); |
| 549 | if (device) |
| 550 | { |
| 551 | char buf[1024]; |
| 552 | for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++) |
| 553 | buf[iPtr] = (char)command.parameters[iPtr]; |
| 554 | buf[command.parameters.size] = 0; |
| 555 | |
| 556 | CStdString strName(buf); |
| 557 | device->SetOSDName(strName); |
| 558 | |
| 559 | return COMMAND_HANDLED; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 564 | } |
| 565 | |
| 566 | int CCECCommandHandler::HandleSetStreamPath(const cec_command &command) |
| 567 | { |
| 568 | if (!m_processor->CECInitialised()) |
| 569 | return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND; |
| 570 | |
| 571 | if (command.parameters.size >= 2) |
| 572 | { |
| 573 | uint16_t iStreamAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]); |
| 574 | LIB_CEC->AddLog(CEC_LOG_DEBUG, ">> %s (%x) sets stream path to physical address %04x", ToString(command.initiator), command.initiator, iStreamAddress); |
| 575 | |
| 576 | /* one of the device handled by libCEC has been made active */ |
| 577 | CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamAddress); |
| 578 | if (device) |
| 579 | { |
| 580 | if (device->IsHandledByLibCEC()) |
| 581 | device->ActivateSource(); |
| 582 | else |
| 583 | device->MarkAsActiveSource(); |
| 584 | return COMMAND_HANDLED; |
| 585 | } |
| 586 | else |
| 587 | { |
| 588 | cec_logical_address previousSource = m_processor->GetActiveSource(false); |
| 589 | CCECBusDevice* device = m_processor->GetDevice(previousSource); |
| 590 | if (device && device->GetCurrentPhysicalAddress() != iStreamAddress) |
| 591 | device->MarkAsInactiveSource(); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 596 | } |
| 597 | |
| 598 | int CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command &command) |
| 599 | { |
| 600 | if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination)) |
| 601 | { |
| 602 | CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination)); |
| 603 | if (device) |
| 604 | { |
| 605 | if (command.parameters.size >= 2) |
| 606 | { |
| 607 | device->SetPowerStatus(CEC_POWER_STATUS_ON); |
| 608 | device->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON); |
| 609 | uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]); |
| 610 | CCECBusDevice *newActiveDevice = GetDeviceByPhysicalAddress(iNewAddress); |
| 611 | if (newActiveDevice) |
| 612 | newActiveDevice->MarkAsActiveSource(); |
| 613 | if (device->TransmitSetSystemAudioMode(command.initiator, true)) |
| 614 | return COMMAND_HANDLED; |
| 615 | } |
| 616 | else |
| 617 | { |
| 618 | device->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF); |
| 619 | if (device->TransmitSetSystemAudioMode(command.initiator, true)) |
| 620 | return COMMAND_HANDLED; |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND; |
| 626 | } |
| 627 | |
| 628 | int CCECCommandHandler::HandleStandby(const cec_command &command) |
| 629 | { |
| 630 | CCECBusDevice *device = GetDevice(command.initiator); |
| 631 | if (device) |
| 632 | device->SetPowerStatus(CEC_POWER_STATUS_STANDBY); |
| 633 | |
| 634 | return COMMAND_HANDLED; |
| 635 | } |
| 636 | |
| 637 | int CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command &command) |
| 638 | { |
| 639 | if (command.parameters.size == 1) |
| 640 | { |
| 641 | CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator)); |
| 642 | if (device) |
| 643 | { |
| 644 | device->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]); |
| 645 | return COMMAND_HANDLED; |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 650 | } |
| 651 | |
| 652 | int CCECCommandHandler::HandleSetSystemAudioMode(const cec_command &command) |
| 653 | { |
| 654 | if (command.parameters.size == 1) |
| 655 | { |
| 656 | CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator)); |
| 657 | if (device) |
| 658 | { |
| 659 | device->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]); |
| 660 | return COMMAND_HANDLED; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 665 | } |
| 666 | |
| 667 | int CCECCommandHandler::HandleTextViewOn(const cec_command &command) |
| 668 | { |
| 669 | m_processor->GetDevice(command.initiator)->MarkAsActiveSource(); |
| 670 | return COMMAND_HANDLED; |
| 671 | } |
| 672 | |
| 673 | int CCECCommandHandler::HandleUserControlPressed(const cec_command &command) |
| 674 | { |
| 675 | if (!m_processor->CECInitialised() || |
| 676 | !m_processor->IsHandledByLibCEC(command.destination)) |
| 677 | return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND; |
| 678 | |
| 679 | if (command.parameters.size == 0) |
| 680 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 681 | |
| 682 | CCECBusDevice *device = GetDevice(command.destination); |
| 683 | if (!device) |
| 684 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 685 | |
| 686 | CCECClient *client = device->GetClient(); |
| 687 | if (client) |
| 688 | client->SetCurrentButton((cec_user_control_code) command.parameters[0]); |
| 689 | |
| 690 | if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER || |
| 691 | command.parameters[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION|| |
| 692 | command.parameters[0] == CEC_USER_CONTROL_CODE_POWER_TOGGLE_FUNCTION) |
| 693 | { |
| 694 | bool bPowerOn(true); |
| 695 | |
| 696 | // CEC_USER_CONTROL_CODE_POWER and CEC_USER_CONTROL_CODE_POWER_TOGGLE_FUNCTION operate as a toggle |
| 697 | // assume CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION does not |
| 698 | if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER || |
| 699 | command.parameters[0] == CEC_USER_CONTROL_CODE_POWER_TOGGLE_FUNCTION) |
| 700 | { |
| 701 | cec_power_status status = device->GetCurrentPowerStatus(); |
| 702 | bPowerOn = !(status == CEC_POWER_STATUS_ON || status == CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); |
| 703 | } |
| 704 | |
| 705 | if (bPowerOn) |
| 706 | { |
| 707 | device->ActivateSource(); |
| 708 | } |
| 709 | else |
| 710 | { |
| 711 | device->MarkAsInactiveSource(); |
| 712 | device->TransmitInactiveSource(); |
| 713 | device->SetMenuState(CEC_MENU_STATE_DEACTIVATED); |
| 714 | } |
| 715 | } |
| 716 | else if (command.parameters[0] != CEC_USER_CONTROL_CODE_POWER_OFF_FUNCTION) |
| 717 | { |
| 718 | // we're not marked as active source, but the tv sends keypresses to us, so assume it forgot to activate us |
| 719 | if (!device->IsActiveSource() && command.initiator == CECDEVICE_TV) |
| 720 | device->MarkAsActiveSource(); |
| 721 | } |
| 722 | |
| 723 | return COMMAND_HANDLED; |
| 724 | } |
| 725 | |
| 726 | int CCECCommandHandler::HandleUserControlRelease(const cec_command &command) |
| 727 | { |
| 728 | if (!m_processor->CECInitialised() || |
| 729 | !m_processor->IsHandledByLibCEC(command.destination)) |
| 730 | return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND; |
| 731 | |
| 732 | CCECClient *client = m_processor->GetClient(command.destination); |
| 733 | if (client) |
| 734 | client->AddKey(); |
| 735 | |
| 736 | return COMMAND_HANDLED; |
| 737 | } |
| 738 | |
| 739 | int CCECCommandHandler::HandleVendorCommand(const cec_command & UNUSED(command)) |
| 740 | { |
| 741 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 742 | } |
| 743 | |
| 744 | int CCECCommandHandler::HandleVendorRemoteButtonDown(const cec_command& command) |
| 745 | { |
| 746 | if (command.parameters.size == 0) |
| 747 | return CEC_ABORT_REASON_INVALID_OPERAND; |
| 748 | |
| 749 | LIB_CEC->AddLog(CEC_LOG_NOTICE, "unhandled vendor remote button received with keycode %x", command.parameters[0]); |
| 750 | return COMMAND_HANDLED; |
| 751 | } |
| 752 | |
| 753 | void CCECCommandHandler::UnhandledCommand(const cec_command &command, const cec_abort_reason reason) |
| 754 | { |
| 755 | if (m_processor->IsHandledByLibCEC(command.destination)) |
| 756 | { |
| 757 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "sending abort with opcode %02x and reason '%s' to %s", command.opcode, ToString(reason), ToString(command.initiator)); |
| 758 | m_processor->TransmitAbort(command.destination, command.initiator, command.opcode, reason); |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | size_t CCECCommandHandler::GetMyDevices(vector<CCECBusDevice *> &devices) const |
| 763 | { |
| 764 | size_t iReturn(0); |
| 765 | |
| 766 | cec_logical_addresses addresses = m_processor->GetLogicalAddresses(); |
| 767 | for (uint8_t iPtr = CECDEVICE_TV; iPtr < CECDEVICE_BROADCAST; iPtr++) |
| 768 | { |
| 769 | if (addresses[iPtr]) |
| 770 | { |
| 771 | devices.push_back(GetDevice((cec_logical_address) iPtr)); |
| 772 | ++iReturn; |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | return iReturn; |
| 777 | } |
| 778 | |
| 779 | CCECBusDevice *CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress) const |
| 780 | { |
| 781 | return m_processor->GetDevice(iLogicalAddress); |
| 782 | } |
| 783 | |
| 784 | CCECBusDevice *CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress) const |
| 785 | { |
| 786 | return m_processor->GetDeviceByPhysicalAddress(iPhysicalAddress); |
| 787 | } |
| 788 | |
| 789 | bool CCECCommandHandler::SetVendorId(const cec_command &command) |
| 790 | { |
| 791 | bool bChanged(false); |
| 792 | if (command.parameters.size < 3) |
| 793 | { |
| 794 | LIB_CEC->AddLog(CEC_LOG_WARNING, "invalid vendor ID received"); |
| 795 | return bChanged; |
| 796 | } |
| 797 | |
| 798 | uint64_t iVendorId = ((uint64_t)command.parameters[0] << 16) + |
| 799 | ((uint64_t)command.parameters[1] << 8) + |
| 800 | (uint64_t)command.parameters[2]; |
| 801 | |
| 802 | CCECBusDevice *device = GetDevice((cec_logical_address) command.initiator); |
| 803 | if (device) |
| 804 | bChanged = device->SetVendorId(iVendorId); |
| 805 | return bChanged; |
| 806 | } |
| 807 | |
| 808 | void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress, uint16_t iNewAddress) |
| 809 | { |
| 810 | if (!m_processor->IsHandledByLibCEC(iAddress)) |
| 811 | { |
| 812 | CCECBusDevice *otherDevice = m_processor->GetDeviceByPhysicalAddress(iNewAddress); |
| 813 | CCECClient *client = otherDevice ? otherDevice->GetClient() : NULL; |
| 814 | |
| 815 | CCECBusDevice *device = m_processor->GetDevice(iAddress); |
| 816 | if (device) |
| 817 | device->SetPhysicalAddress(iNewAddress); |
| 818 | else |
| 819 | { |
| 820 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "device with logical address %X not found", iAddress); |
| 821 | } |
| 822 | |
| 823 | /* another device reported the same physical address as ours */ |
| 824 | if (client) |
| 825 | client->ResetPhysicalAddress(); |
| 826 | } |
| 827 | else |
| 828 | { |
| 829 | 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); |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | bool CCECCommandHandler::PowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination) |
| 834 | { |
| 835 | if (iDestination == CECDEVICE_TV) |
| 836 | return TransmitImageViewOn(iInitiator, iDestination); |
| 837 | |
| 838 | return TransmitKeypress(iInitiator, iDestination, CEC_USER_CONTROL_CODE_POWER) && |
| 839 | TransmitKeyRelease(iInitiator, iDestination); |
| 840 | } |
| 841 | |
| 842 | bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator, const cec_logical_address iDestination) |
| 843 | { |
| 844 | cec_command command; |
| 845 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_IMAGE_VIEW_ON); |
| 846 | |
| 847 | if (Transmit(command, false, false)) |
| 848 | { |
| 849 | CCECBusDevice* dest = m_processor->GetDevice(iDestination); |
| 850 | if (dest && dest->GetCurrentPowerStatus() != CEC_POWER_STATUS_ON) |
| 851 | dest->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON); |
| 852 | return true; |
| 853 | } |
| 854 | return false; |
| 855 | } |
| 856 | |
| 857 | bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator, const cec_logical_address iDestination) |
| 858 | { |
| 859 | cec_command command; |
| 860 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_STANDBY); |
| 861 | |
| 862 | return Transmit(command, false, false); |
| 863 | } |
| 864 | |
| 865 | bool CCECCommandHandler::TransmitRequestActiveSource(const cec_logical_address iInitiator, bool bWaitForResponse /* = true */) |
| 866 | { |
| 867 | cec_command command; |
| 868 | cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REQUEST_ACTIVE_SOURCE); |
| 869 | |
| 870 | return Transmit(command, !bWaitForResponse, false); |
| 871 | } |
| 872 | |
| 873 | bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */) |
| 874 | { |
| 875 | cec_command command; |
| 876 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_CEC_VERSION); |
| 877 | |
| 878 | return Transmit(command, !bWaitForResponse, false); |
| 879 | } |
| 880 | |
| 881 | bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */) |
| 882 | { |
| 883 | cec_command command; |
| 884 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_MENU_LANGUAGE); |
| 885 | |
| 886 | return Transmit(command, !bWaitForResponse, false); |
| 887 | } |
| 888 | |
| 889 | bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */) |
| 890 | { |
| 891 | cec_command command; |
| 892 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_OSD_NAME); |
| 893 | |
| 894 | return Transmit(command, !bWaitForResponse, false); |
| 895 | } |
| 896 | |
| 897 | bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */) |
| 898 | { |
| 899 | cec_command command; |
| 900 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS); |
| 901 | |
| 902 | return Transmit(command, !bWaitForResponse, false); |
| 903 | } |
| 904 | |
| 905 | bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */) |
| 906 | { |
| 907 | if (iDestination == CECDEVICE_TV) |
| 908 | { |
| 909 | int64_t now(GetTimeMs()); |
| 910 | if (now - m_iPowerStatusRequested < REQUEST_POWER_STATUS_TIMEOUT) |
| 911 | return true; |
| 912 | m_iPowerStatusRequested = now; |
| 913 | } |
| 914 | |
| 915 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< requesting power status of '%s' (%X)", m_busDevice->GetLogicalAddressName(), iDestination); |
| 916 | |
| 917 | cec_command command; |
| 918 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS); |
| 919 | |
| 920 | return Transmit(command, !bWaitForResponse, false); |
| 921 | } |
| 922 | |
| 923 | bool CCECCommandHandler::TransmitRequestVendorId(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_DEVICE_VENDOR_ID); |
| 927 | |
| 928 | return Transmit(command, !bWaitForResponse, false); |
| 929 | } |
| 930 | |
| 931 | bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress, bool bIsReply) |
| 932 | { |
| 933 | cec_command command; |
| 934 | cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE); |
| 935 | command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF)); |
| 936 | command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF)); |
| 937 | |
| 938 | return Transmit(command, false, bIsReply); |
| 939 | } |
| 940 | |
| 941 | bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_version cecVersion, bool bIsReply) |
| 942 | { |
| 943 | cec_command command; |
| 944 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_CEC_VERSION); |
| 945 | command.parameters.PushBack((uint8_t)cecVersion); |
| 946 | |
| 947 | return Transmit(command, false, bIsReply); |
| 948 | } |
| 949 | |
| 950 | bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress) |
| 951 | { |
| 952 | cec_command command; |
| 953 | cec_command::Format(command, iInitiator, CECDEVICE_TV, CEC_OPCODE_INACTIVE_SOURCE); |
| 954 | command.parameters.PushBack((iPhysicalAddress >> 8) & 0xFF); |
| 955 | command.parameters.PushBack(iPhysicalAddress & 0xFF); |
| 956 | |
| 957 | return Transmit(command, false, false); |
| 958 | } |
| 959 | |
| 960 | bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_menu_state menuState, bool bIsReply) |
| 961 | { |
| 962 | cec_command command; |
| 963 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_MENU_STATUS); |
| 964 | command.parameters.PushBack((uint8_t)menuState); |
| 965 | |
| 966 | return Transmit(command, false, bIsReply); |
| 967 | } |
| 968 | |
| 969 | bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, std::string strDeviceName, bool bIsReply) |
| 970 | { |
| 971 | cec_command command; |
| 972 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_NAME); |
| 973 | for (size_t iPtr = 0; iPtr < strDeviceName.length(); iPtr++) |
| 974 | command.parameters.PushBack(strDeviceName.at(iPtr)); |
| 975 | |
| 976 | return Transmit(command, false, bIsReply); |
| 977 | } |
| 978 | |
| 979 | bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_display_control duration, const char *strMessage, bool bIsReply) |
| 980 | { |
| 981 | cec_command command; |
| 982 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_STRING); |
| 983 | command.parameters.PushBack((uint8_t)duration); |
| 984 | |
| 985 | size_t iLen = strlen(strMessage); |
| 986 | if (iLen > 13) iLen = 13; |
| 987 | |
| 988 | for (size_t iPtr = 0; iPtr < iLen; iPtr++) |
| 989 | command.parameters.PushBack(strMessage[iPtr]); |
| 990 | |
| 991 | return Transmit(command, false, bIsReply); |
| 992 | } |
| 993 | |
| 994 | bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator, uint16_t iPhysicalAddress, cec_device_type type, bool bIsReply) |
| 995 | { |
| 996 | cec_command command; |
| 997 | cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS); |
| 998 | command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF)); |
| 999 | command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF)); |
| 1000 | command.parameters.PushBack((uint8_t) (type)); |
| 1001 | |
| 1002 | return Transmit(command, false, bIsReply); |
| 1003 | } |
| 1004 | |
| 1005 | bool CCECCommandHandler::TransmitSetMenuLanguage(const cec_logical_address iInitiator, const char lang[3], bool bIsReply) |
| 1006 | { |
| 1007 | cec_command command; |
| 1008 | command.Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_SET_MENU_LANGUAGE); |
| 1009 | command.parameters.PushBack((uint8_t) lang[0]); |
| 1010 | command.parameters.PushBack((uint8_t) lang[1]); |
| 1011 | command.parameters.PushBack((uint8_t) lang[2]); |
| 1012 | |
| 1013 | return Transmit(command, false, bIsReply); |
| 1014 | } |
| 1015 | |
| 1016 | bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bIsReply) |
| 1017 | { |
| 1018 | cec_command command; |
| 1019 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_NONE); |
| 1020 | |
| 1021 | return Transmit(command, false, bIsReply); |
| 1022 | } |
| 1023 | |
| 1024 | bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_power_status state, bool bIsReply) |
| 1025 | { |
| 1026 | cec_command command; |
| 1027 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_POWER_STATUS); |
| 1028 | command.parameters.PushBack((uint8_t) state); |
| 1029 | |
| 1030 | return Transmit(command, false, bIsReply); |
| 1031 | } |
| 1032 | |
| 1033 | bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator, uint64_t iVendorId, bool bIsReply) |
| 1034 | { |
| 1035 | cec_command command; |
| 1036 | cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_DEVICE_VENDOR_ID); |
| 1037 | |
| 1038 | command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 16) & 0xFF)); |
| 1039 | command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 8) & 0xFF)); |
| 1040 | command.parameters.PushBack((uint8_t) ((uint64_t)iVendorId & 0xFF)); |
| 1041 | |
| 1042 | return Transmit(command, false, bIsReply); |
| 1043 | } |
| 1044 | |
| 1045 | bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, uint8_t state, bool bIsReply) |
| 1046 | { |
| 1047 | cec_command command; |
| 1048 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_AUDIO_STATUS); |
| 1049 | command.parameters.PushBack(state); |
| 1050 | |
| 1051 | return Transmit(command, false, bIsReply); |
| 1052 | } |
| 1053 | |
| 1054 | bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state, bool bIsReply) |
| 1055 | { |
| 1056 | cec_command command; |
| 1057 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE); |
| 1058 | command.parameters.PushBack((uint8_t)state); |
| 1059 | |
| 1060 | return Transmit(command, false, bIsReply); |
| 1061 | } |
| 1062 | |
| 1063 | bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath, bool bIsReply) |
| 1064 | { |
| 1065 | if (m_busDevice->GetLogicalAddress() != CECDEVICE_TV) |
| 1066 | { |
| 1067 | LIB_CEC->AddLog(CEC_LOG_ERROR, "only the TV is allowed to send CEC_OPCODE_SET_STREAM_PATH"); |
| 1068 | return false; |
| 1069 | } |
| 1070 | cec_command command; |
| 1071 | cec_command::Format(command, m_busDevice->GetLogicalAddress(), CECDEVICE_BROADCAST, CEC_OPCODE_SET_STREAM_PATH); |
| 1072 | command.parameters.PushBack((uint8_t) ((iStreamPath >> 8) & 0xFF)); |
| 1073 | command.parameters.PushBack((uint8_t) (iStreamPath & 0xFF)); |
| 1074 | |
| 1075 | return Transmit(command, false, bIsReply); |
| 1076 | } |
| 1077 | |
| 1078 | bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state, bool bIsReply) |
| 1079 | { |
| 1080 | cec_command command; |
| 1081 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS); |
| 1082 | command.parameters.PushBack((uint8_t)state); |
| 1083 | |
| 1084 | return Transmit(command, false, bIsReply); |
| 1085 | } |
| 1086 | |
| 1087 | bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_deck_info state, bool bIsReply) |
| 1088 | { |
| 1089 | cec_command command; |
| 1090 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_DECK_STATUS); |
| 1091 | command.PushBack((uint8_t)state); |
| 1092 | |
| 1093 | return Transmit(command, false, bIsReply); |
| 1094 | } |
| 1095 | |
| 1096 | bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */) |
| 1097 | { |
| 1098 | cec_command command; |
| 1099 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_PRESSED); |
| 1100 | command.parameters.PushBack((uint8_t)key); |
| 1101 | |
| 1102 | return Transmit(command, !bWait, false); |
| 1103 | } |
| 1104 | |
| 1105 | bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWait /* = true */) |
| 1106 | { |
| 1107 | cec_command command; |
| 1108 | cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_RELEASE); |
| 1109 | |
| 1110 | return Transmit(command, !bWait, false); |
| 1111 | } |
| 1112 | |
| 1113 | bool CCECCommandHandler::Transmit(cec_command &command, bool bSuppressWait, bool bIsReply) |
| 1114 | { |
| 1115 | bool bReturn(false); |
| 1116 | cec_opcode expectedResponse(cec_command::GetResponseOpcode(command.opcode)); |
| 1117 | bool bExpectResponse(expectedResponse != CEC_OPCODE_NONE && !bSuppressWait); |
| 1118 | command.transmit_timeout = m_iTransmitTimeout; |
| 1119 | |
| 1120 | if (command.initiator == CECDEVICE_UNKNOWN) |
| 1121 | { |
| 1122 | LIB_CEC->AddLog(CEC_LOG_ERROR, "not transmitting a command without a valid initiator"); |
| 1123 | return bReturn; |
| 1124 | } |
| 1125 | |
| 1126 | // check whether the destination is not marked as not present or handled by libCEC |
| 1127 | if (command.destination != CECDEVICE_BROADCAST && command.opcode_set) |
| 1128 | { |
| 1129 | CCECBusDevice* destinationDevice = m_processor->GetDevice(command.destination); |
| 1130 | cec_bus_device_status status = destinationDevice ? destinationDevice->GetStatus() : CEC_DEVICE_STATUS_NOT_PRESENT; |
| 1131 | if (status == CEC_DEVICE_STATUS_NOT_PRESENT) |
| 1132 | { |
| 1133 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "not sending command '%s': destination device '%s' marked as not present", ToString(command.opcode),ToString(command.destination)); |
| 1134 | return bReturn; |
| 1135 | } |
| 1136 | else if (status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC) |
| 1137 | { |
| 1138 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "not sending command '%s': destination device '%s' marked as handled by libCEC", ToString(command.opcode),ToString(command.destination)); |
| 1139 | return bReturn; |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | { |
| 1144 | uint8_t iTries(0), iMaxTries(!command.opcode_set ? 1 : m_iTransmitRetries + 1); |
| 1145 | while (!bReturn && ++iTries <= iMaxTries && !m_busDevice->IsUnsupportedFeature(command.opcode)) |
| 1146 | { |
| 1147 | if ((bReturn = m_processor->Transmit(command, bIsReply)) == true) |
| 1148 | { |
| 1149 | #ifdef CEC_DEBUGGING |
| 1150 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "command transmitted"); |
| 1151 | #endif |
| 1152 | if (bExpectResponse) |
| 1153 | { |
| 1154 | bReturn = m_busDevice->WaitForOpcode(expectedResponse); |
| 1155 | LIB_CEC->AddLog(CEC_LOG_DEBUG, bReturn ? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse, ToString(expectedResponse)); |
| 1156 | } |
| 1157 | } |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | return bReturn; |
| 1162 | } |
| 1163 | |
| 1164 | bool CCECCommandHandler::ActivateSource(bool bTransmitDelayedCommandsOnly /* = false */) |
| 1165 | { |
| 1166 | if (m_busDevice->IsActiveSource() && |
| 1167 | m_busDevice->IsHandledByLibCEC()) |
| 1168 | { |
| 1169 | { |
| 1170 | CLockObject lock(m_mutex); |
| 1171 | // check if we need to send a delayed source switch |
| 1172 | if (bTransmitDelayedCommandsOnly) |
| 1173 | { |
| 1174 | if (m_iActiveSourcePending == 0 || GetTimeMs() < m_iActiveSourcePending) |
| 1175 | return false; |
| 1176 | |
| 1177 | #ifdef CEC_DEBUGGING |
| 1178 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "transmitting delayed activate source command"); |
| 1179 | #endif |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | // update the power state and menu state |
| 1184 | if (!bTransmitDelayedCommandsOnly) |
| 1185 | { |
| 1186 | m_busDevice->SetPowerStatus(CEC_POWER_STATUS_ON); |
| 1187 | m_busDevice->SetMenuState(CEC_MENU_STATE_ACTIVATED); |
| 1188 | } |
| 1189 | |
| 1190 | // vendor specific hook |
| 1191 | VendorPreActivateSourceHook(); |
| 1192 | |
| 1193 | // power on the TV |
| 1194 | CCECBusDevice* tv = m_processor->GetDevice(CECDEVICE_TV); |
| 1195 | bool bTvPresent = (tv && tv->GetStatus() == CEC_DEVICE_STATUS_PRESENT); |
| 1196 | bool bActiveSourceFailed(false); |
| 1197 | if (bTvPresent) |
| 1198 | bActiveSourceFailed = !m_busDevice->TransmitImageViewOn(); |
| 1199 | else |
| 1200 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "TV not present, not sending 'image view on'"); |
| 1201 | |
| 1202 | // check if we're allowed to switch sources |
| 1203 | bool bSourceSwitchAllowed = SourceSwitchAllowed(); |
| 1204 | if (!bSourceSwitchAllowed) |
| 1205 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "source switch is currently not allowed by command handler"); |
| 1206 | |
| 1207 | // switch sources (if allowed) |
| 1208 | if (!bActiveSourceFailed && bSourceSwitchAllowed) |
| 1209 | { |
| 1210 | bActiveSourceFailed = !m_busDevice->TransmitActiveSource(false); |
| 1211 | if (bTvPresent && !bActiveSourceFailed) |
| 1212 | bActiveSourceFailed = !m_busDevice->TransmitMenuState(CECDEVICE_TV, false); |
| 1213 | |
| 1214 | // update the deck status for playback devices |
| 1215 | if (bTvPresent && !bActiveSourceFailed) |
| 1216 | { |
| 1217 | CCECPlaybackDevice *playbackDevice = m_busDevice->AsPlaybackDevice(); |
| 1218 | if (playbackDevice && SendDeckStatusUpdateOnActiveSource()) |
| 1219 | bActiveSourceFailed = !playbackDevice->TransmitDeckStatus(CECDEVICE_TV, false); |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | // retry later |
| 1224 | if (bActiveSourceFailed || !bSourceSwitchAllowed) |
| 1225 | { |
| 1226 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "failed to make '%s' the active source. will retry later", m_busDevice->GetLogicalAddressName()); |
| 1227 | int64_t now(GetTimeMs()); |
| 1228 | CLockObject lock(m_mutex); |
| 1229 | if (m_iActiveSourcePending == 0 || m_iActiveSourcePending < now) |
| 1230 | m_iActiveSourcePending = now + (int64_t)CEC_ACTIVE_SOURCE_SWITCH_RETRY_TIME_MS; |
| 1231 | return false; |
| 1232 | } |
| 1233 | else |
| 1234 | { |
| 1235 | CLockObject lock(m_mutex); |
| 1236 | // clear previous pending active source command |
| 1237 | m_iActiveSourcePending = 0; |
| 1238 | } |
| 1239 | |
| 1240 | // mark the handler as initialised |
| 1241 | CLockObject lock(m_mutex); |
| 1242 | m_bHandlerInited = true; |
| 1243 | } |
| 1244 | return true; |
| 1245 | } |
| 1246 | |
| 1247 | void CCECCommandHandler::ScheduleActivateSource(uint64_t iDelay) |
| 1248 | { |
| 1249 | CLockObject lock(m_mutex); |
| 1250 | m_iActiveSourcePending = GetTimeMs() + iDelay; |
| 1251 | } |