| 1 | /* |
| 2 | * This file is part of the libCEC(R) library. |
| 3 | * |
| 4 | * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved. |
| 5 | * libCEC(R) is an original work, containing original code. |
| 6 | * |
| 7 | * libCEC(R) is a trademark of Pulse-Eight Limited. |
| 8 | * |
| 9 | * This program is dual-licensed; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 22 | * |
| 23 | * |
| 24 | * Alternatively, you can license this library under a commercial license, |
| 25 | * please contact Pulse-Eight Licensing for more information. |
| 26 | * |
| 27 | * For more information contact: |
| 28 | * Pulse-Eight Licensing <license@pulse-eight.com> |
| 29 | * http://www.pulse-eight.com/ |
| 30 | * http://www.pulse-eight.net/ |
| 31 | */ |
| 32 | |
| 33 | #include "CECCommandHandler.h" |
| 34 | #include "../devices/CECBusDevice.h" |
| 35 | #include "../CECProcessor.h" |
| 36 | |
| 37 | using namespace CEC; |
| 38 | |
| 39 | CCECCommandHandler::CCECCommandHandler(CCECBusDevice *busDevice) |
| 40 | { |
| 41 | m_busDevice = busDevice; |
| 42 | } |
| 43 | |
| 44 | bool CCECCommandHandler::HandleCommand(const cec_command &command) |
| 45 | { |
| 46 | bool bHandled(true); |
| 47 | |
| 48 | if (command.destination == m_busDevice->GetMyLogicalAddress()) |
| 49 | { |
| 50 | switch(command.opcode) |
| 51 | { |
| 52 | case CEC_OPCODE_CEC_VERSION: |
| 53 | HandleDeviceCecVersion(command); |
| 54 | break; |
| 55 | case CEC_OPCODE_SET_MENU_LANGUAGE: |
| 56 | HandleSetMenuLanguage(command); |
| 57 | break; |
| 58 | case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS: |
| 59 | HandleGivePhysicalAddress(command); |
| 60 | break; |
| 61 | case CEC_OPCODE_GIVE_OSD_NAME: |
| 62 | HandleGiveOSDName(command); |
| 63 | break; |
| 64 | case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID: |
| 65 | HandleGiveDeviceVendorId(command); |
| 66 | break; |
| 67 | case CEC_OPCODE_DEVICE_VENDOR_ID: |
| 68 | HandleDeviceVendorId(command); |
| 69 | break; |
| 70 | case CEC_OPCODE_VENDOR_COMMAND_WITH_ID: |
| 71 | HandleDeviceVendorCommandWithId(command); |
| 72 | break; |
| 73 | case CEC_OPCODE_GIVE_DECK_STATUS: |
| 74 | HandleGiveDeckStatus(command); |
| 75 | break; |
| 76 | case CEC_OPCODE_MENU_REQUEST: |
| 77 | HandleMenuRequest(command); |
| 78 | break; |
| 79 | case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS: |
| 80 | HandleGiveDevicePowerStatus(command); |
| 81 | break; |
| 82 | case CEC_OPCODE_GET_CEC_VERSION: |
| 83 | HandleGetCecVersion(command); |
| 84 | break; |
| 85 | case CEC_OPCODE_USER_CONTROL_PRESSED: |
| 86 | HandleUserControlPressed(command); |
| 87 | break; |
| 88 | case CEC_OPCODE_USER_CONTROL_RELEASE: |
| 89 | HandleUserControlRelease(command); |
| 90 | break; |
| 91 | default: |
| 92 | UnhandledCommand(command); |
| 93 | bHandled = false; |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | m_busDevice->GetProcessor()->AddCommand(command); |
| 98 | } |
| 99 | else if (command.destination == CECDEVICE_BROADCAST) |
| 100 | { |
| 101 | CStdString strLog; |
| 102 | switch (command.opcode) |
| 103 | { |
| 104 | case CEC_OPCODE_SET_MENU_LANGUAGE: |
| 105 | HandleSetMenuLanguage(command); |
| 106 | break; |
| 107 | case CEC_OPCODE_REQUEST_ACTIVE_SOURCE: |
| 108 | HandleRequestActiveSource(command); |
| 109 | break; |
| 110 | case CEC_OPCODE_SET_STREAM_PATH: |
| 111 | HandleSetStreamPath(command); |
| 112 | break; |
| 113 | case CEC_OPCODE_ROUTING_CHANGE: |
| 114 | HandleRoutingChange(command); |
| 115 | break; |
| 116 | case CEC_OPCODE_DEVICE_VENDOR_ID: |
| 117 | HandleDeviceVendorId(command); |
| 118 | break; |
| 119 | case CEC_OPCODE_VENDOR_COMMAND_WITH_ID: |
| 120 | HandleDeviceVendorCommandWithId(command); |
| 121 | break; |
| 122 | default: |
| 123 | UnhandledCommand(command); |
| 124 | bHandled = false; |
| 125 | break; |
| 126 | } |
| 127 | |
| 128 | m_busDevice->GetProcessor()->AddCommand(command); |
| 129 | } |
| 130 | else |
| 131 | { |
| 132 | CStdString strLog; |
| 133 | strLog.Format("ignoring frame: destination: %u != %u", command.destination, (uint8_t)m_busDevice->GetMyLogicalAddress()); |
| 134 | m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
| 135 | bHandled = false; |
| 136 | } |
| 137 | |
| 138 | return bHandled; |
| 139 | } |
| 140 | |
| 141 | bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command &command) |
| 142 | { |
| 143 | if (command.parameters.size == 1) |
| 144 | { |
| 145 | CCECBusDevice *device = GetDevice(command.initiator); |
| 146 | if (device) |
| 147 | device->SetCecVersion((cec_version) command.parameters[0]); |
| 148 | } |
| 149 | |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command) |
| 154 | { |
| 155 | CCECBusDevice *device = GetDevice(command.initiator); |
| 156 | if (device) |
| 157 | device->SetVendorId(command.parameters); |
| 158 | |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | bool CCECCommandHandler::HandleDeviceVendorId(const cec_command &command) |
| 163 | { |
| 164 | CCECBusDevice *device = GetDevice(command.initiator); |
| 165 | if (device) |
| 166 | device->SetVendorId(command.parameters); |
| 167 | |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | bool CCECCommandHandler::HandleGetCecVersion(const cec_command &command) |
| 172 | { |
| 173 | CCECBusDevice *device = GetDevice(command.initiator); |
| 174 | if (device) |
| 175 | return device->ReportCECVersion(); |
| 176 | |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command &command) |
| 181 | { |
| 182 | CCECBusDevice *device = GetDevice(command.initiator); |
| 183 | if (device) |
| 184 | return device->ReportDeckStatus(); |
| 185 | |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command) |
| 190 | { |
| 191 | CCECBusDevice *device = GetDevice(command.initiator); |
| 192 | if (device) |
| 193 | return device->ReportPowerState(); |
| 194 | |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command &command) |
| 199 | { |
| 200 | CCECBusDevice *device = GetDevice(command.initiator); |
| 201 | if (device) |
| 202 | return device->ReportVendorID(); |
| 203 | |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | bool CCECCommandHandler::HandleGiveOSDName(const cec_command &command) |
| 208 | { |
| 209 | CCECBusDevice *device = GetDevice(command.initiator); |
| 210 | if (device) |
| 211 | return device->ReportOSDName(); |
| 212 | |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command &command) |
| 217 | { |
| 218 | CCECBusDevice *device = GetThisDevice(); |
| 219 | if (device) |
| 220 | return device->BroadcastPhysicalAddress(); |
| 221 | |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | bool CCECCommandHandler::HandleMenuRequest(const cec_command &command) |
| 226 | { |
| 227 | if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_QUERY) |
| 228 | { |
| 229 | CCECBusDevice *device = GetDevice(command.initiator); |
| 230 | if (device) |
| 231 | return device->ReportMenuState(); |
| 232 | } |
| 233 | return false; |
| 234 | } |
| 235 | |
| 236 | bool CCECCommandHandler::HandleRequestActiveSource(const cec_command &command) |
| 237 | { |
| 238 | CStdString strLog; |
| 239 | strLog.Format(">> %i requests active source", (uint8_t) command.initiator); |
| 240 | m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
| 241 | CCECBusDevice *device = GetThisDevice(); |
| 242 | if (device) |
| 243 | return device->BroadcastActiveSource(); |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | bool CCECCommandHandler::HandleRoutingChange(const cec_command &command) |
| 248 | { |
| 249 | if (command.parameters.size == 4) |
| 250 | { |
| 251 | uint16_t iOldAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]); |
| 252 | uint16_t iNewAddress = ((uint16_t)command.parameters[2] << 8) | ((uint16_t)command.parameters[3]); |
| 253 | |
| 254 | CCECBusDevice *device = GetDevice(command.initiator); |
| 255 | if (device) |
| 256 | device->SetPhysicalAddress(iNewAddress, iOldAddress); |
| 257 | } |
| 258 | return true; |
| 259 | } |
| 260 | |
| 261 | bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command &command) |
| 262 | { |
| 263 | if (command.parameters.size == 3) |
| 264 | { |
| 265 | CCECBusDevice *device = GetDevice(command.initiator); |
| 266 | if (device) |
| 267 | { |
| 268 | cec_menu_language language; |
| 269 | language.device = command.initiator; |
| 270 | for (unsigned int iPtr = 0; iPtr < 4; iPtr++) |
| 271 | language.language[iPtr] = command.parameters[iPtr]; |
| 272 | language.language[3] = 0; |
| 273 | device->SetMenuLanguage(language); |
| 274 | } |
| 275 | } |
| 276 | return true; |
| 277 | } |
| 278 | |
| 279 | |
| 280 | bool CCECCommandHandler::HandleSetStreamPath(const cec_command &command) |
| 281 | { |
| 282 | if (command.parameters.size >= 2) |
| 283 | { |
| 284 | int streamaddr = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]); |
| 285 | CStdString strLog; |
| 286 | strLog.Format(">> %i requests stream path from physical address %04x", command.initiator, streamaddr); |
| 287 | m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
| 288 | if (streamaddr == m_busDevice->GetMyPhysicalAddress()) |
| 289 | { |
| 290 | CCECBusDevice *device = GetThisDevice(); |
| 291 | if (device) |
| 292 | return device->BroadcastActiveSource(); |
| 293 | return false; |
| 294 | } |
| 295 | } |
| 296 | return true; |
| 297 | } |
| 298 | |
| 299 | bool CCECCommandHandler::HandleUserControlPressed(const cec_command &command) |
| 300 | { |
| 301 | if (command.parameters.size > 0) |
| 302 | { |
| 303 | m_busDevice->GetProcessor()->AddKey(); |
| 304 | |
| 305 | if (command.parameters[0] <= CEC_USER_CONTROL_CODE_MAX) |
| 306 | { |
| 307 | CStdString strLog; |
| 308 | strLog.Format("key pressed: %1x", command.parameters[0]); |
| 309 | m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
| 310 | |
| 311 | m_busDevice->GetProcessor()->SetCurrentButton((cec_user_control_code) command.parameters[0]); |
| 312 | } |
| 313 | } |
| 314 | return true; |
| 315 | } |
| 316 | |
| 317 | bool CCECCommandHandler::HandleUserControlRelease(const cec_command &command) |
| 318 | { |
| 319 | m_busDevice->GetProcessor()->AddKey(); |
| 320 | return true; |
| 321 | } |
| 322 | |
| 323 | void CCECCommandHandler::UnhandledCommand(const cec_command &command) |
| 324 | { |
| 325 | CStdString strLog; |
| 326 | strLog.Format("unhandled command with opcode %02x from address %d", command.opcode, command.initiator); |
| 327 | m_busDevice->AddLog(CEC_LOG_DEBUG, strLog); |
| 328 | } |
| 329 | |
| 330 | CCECBusDevice *CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress) const |
| 331 | { |
| 332 | CCECBusDevice *device = NULL; |
| 333 | |
| 334 | if (iLogicalAddress >= CECDEVICE_TV && iLogicalAddress <= CECDEVICE_BROADCAST) |
| 335 | device = m_busDevice->GetProcessor()->m_busDevices[iLogicalAddress]; |
| 336 | |
| 337 | return device; |
| 338 | } |
| 339 | |
| 340 | CCECBusDevice *CCECCommandHandler::GetThisDevice(void) const |
| 341 | { |
| 342 | return m_busDevice->GetProcessor()->m_busDevices[m_busDevice->GetMyLogicalAddress()]; |
| 343 | } |