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