| 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 "CECBusDevice.h" |
| 34 | #include "../CECProcessor.h" |
| 35 | #include "../implementations/ANCommandHandler.h" |
| 36 | #include "../implementations/CECCommandHandler.h" |
| 37 | #include "../implementations/SLCommandHandler.h" |
| 38 | #include "../platform/timeutils.h" |
| 39 | |
| 40 | using namespace CEC; |
| 41 | |
| 42 | CCECBusDevice::CCECBusDevice(CCECProcessor *processor, cec_logical_address iLogicalAddress, uint16_t iPhysicalAddress) : |
| 43 | m_iPhysicalAddress(iPhysicalAddress), |
| 44 | m_iLogicalAddress(iLogicalAddress), |
| 45 | m_powerStatus(CEC_POWER_STATUS_UNKNOWN), |
| 46 | m_processor(processor), |
| 47 | m_iVendorId(0), |
| 48 | m_iVendorClass(CEC_VENDOR_UNKNOWN), |
| 49 | m_iLastActive(0), |
| 50 | m_cecVersion(CEC_VERSION_UNKNOWN) |
| 51 | { |
| 52 | m_handler = new CCECCommandHandler(this); |
| 53 | for (unsigned int iPtr = 0; iPtr < 4; iPtr++) |
| 54 | m_menuLanguage.language[iPtr] = '?'; |
| 55 | m_menuLanguage.language[3] = 0; |
| 56 | m_menuLanguage.device = iLogicalAddress; |
| 57 | } |
| 58 | |
| 59 | CCECBusDevice::~CCECBusDevice(void) |
| 60 | { |
| 61 | m_condition.Broadcast(); |
| 62 | delete m_handler; |
| 63 | } |
| 64 | |
| 65 | cec_logical_address CCECBusDevice::GetMyLogicalAddress(void) const |
| 66 | { |
| 67 | return m_processor->GetLogicalAddress(); |
| 68 | } |
| 69 | |
| 70 | uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const |
| 71 | { |
| 72 | return m_processor->GetPhysicalAddress(); |
| 73 | } |
| 74 | |
| 75 | void CCECBusDevice::AddLog(cec_log_level level, const CStdString &strMessage) |
| 76 | { |
| 77 | m_processor->AddLog(level, strMessage); |
| 78 | } |
| 79 | |
| 80 | void CCECBusDevice::SetMenuLanguage(const cec_menu_language &language) |
| 81 | { |
| 82 | if (language.device == m_iLogicalAddress) |
| 83 | { |
| 84 | CStdString strLog; |
| 85 | strLog.Format("device %d menu language set to '%s'", m_iLogicalAddress, language.language); |
| 86 | m_processor->AddLog(CEC_LOG_DEBUG, strLog); |
| 87 | m_menuLanguage = language; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void CCECBusDevice::SetCecVersion(const cec_version newVersion) |
| 92 | { |
| 93 | CStdString strLog; |
| 94 | m_cecVersion = newVersion; |
| 95 | |
| 96 | switch (newVersion) |
| 97 | { |
| 98 | case CEC_VERSION_1_2: |
| 99 | strLog.Format("device %d reports CEC version 1.2", m_iLogicalAddress); |
| 100 | break; |
| 101 | case CEC_VERSION_1_2A: |
| 102 | strLog.Format("device %d reports CEC version 1.2a", m_iLogicalAddress); |
| 103 | break; |
| 104 | case CEC_VERSION_1_3: |
| 105 | strLog.Format("device %d reports CEC version 1.3", m_iLogicalAddress); |
| 106 | break; |
| 107 | case CEC_VERSION_1_3A: |
| 108 | strLog.Format("device %d reports CEC version 1.3a", m_iLogicalAddress); |
| 109 | break; |
| 110 | default: |
| 111 | strLog.Format("device %d reports an unknown CEC version", m_iLogicalAddress); |
| 112 | m_cecVersion = CEC_VERSION_UNKNOWN; |
| 113 | break; |
| 114 | } |
| 115 | AddLog(CEC_LOG_DEBUG, strLog); |
| 116 | } |
| 117 | |
| 118 | void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus) |
| 119 | { |
| 120 | CStdString strLog; |
| 121 | strLog.Format("device %d power status changed from %2x to %2x", m_iLogicalAddress, m_powerStatus, powerStatus); |
| 122 | m_processor->AddLog(CEC_LOG_DEBUG, strLog); |
| 123 | m_powerStatus = powerStatus; |
| 124 | } |
| 125 | |
| 126 | void CCECBusDevice::SetVendorId(const cec_datapacket &data) |
| 127 | { |
| 128 | if (data.size < 3) |
| 129 | { |
| 130 | AddLog(CEC_LOG_WARNING, "invalid vendor ID received"); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | uint64_t iVendorId = ((uint64_t)data[0] << 3) + |
| 135 | ((uint64_t)data[1] << 2) + |
| 136 | (uint64_t)data[2]; |
| 137 | |
| 138 | SetVendorId(iVendorId, data.size >= 4 ? data[3] : 0); |
| 139 | } |
| 140 | |
| 141 | void CCECBusDevice::SetVendorId(uint64_t iVendorId, uint8_t iVendorClass /* = 0 */) |
| 142 | { |
| 143 | m_iVendorId = iVendorId; |
| 144 | m_iVendorClass = iVendorClass; |
| 145 | |
| 146 | switch (iVendorId) |
| 147 | { |
| 148 | case CEC_VENDOR_SAMSUNG: |
| 149 | if (m_handler->GetVendorId() != CEC_VENDOR_SAMSUNG) |
| 150 | { |
| 151 | delete m_handler; |
| 152 | m_handler = new CANCommandHandler(this); |
| 153 | } |
| 154 | break; |
| 155 | case CEC_VENDOR_LG: |
| 156 | if (m_handler->GetVendorId() != CEC_VENDOR_LG) |
| 157 | { |
| 158 | delete m_handler; |
| 159 | m_handler = new CSLCommandHandler(this); |
| 160 | } |
| 161 | break; |
| 162 | default: |
| 163 | if (m_handler->GetVendorId() != CEC_VENDOR_UNKNOWN) |
| 164 | { |
| 165 | delete m_handler; |
| 166 | m_handler = new CCECCommandHandler(this); |
| 167 | } |
| 168 | break; |
| 169 | } |
| 170 | |
| 171 | CStdString strLog; |
| 172 | strLog.Format("device %d: vendor = %s (%06x) class = %2x", m_iLogicalAddress, GetVendorName(), GetVendorId(), GetVendorClass()); |
| 173 | m_processor->AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
| 174 | } |
| 175 | |
| 176 | bool CCECBusDevice::HandleCommand(const cec_command &command) |
| 177 | { |
| 178 | CLockObject lock(&m_mutex); |
| 179 | m_iLastActive = GetTimeMs(); |
| 180 | m_handler->HandleCommand(command); |
| 181 | m_condition.Signal(); |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | uint64_t CCECBusDevice::GetVendorId(void) |
| 186 | { |
| 187 | if (m_iVendorId == CEC_VENDOR_UNKNOWN) |
| 188 | { |
| 189 | AddLog(CEC_LOG_NOTICE, "<< requesting vendor ID"); |
| 190 | cec_command command; |
| 191 | cec_command::format(command, GetMyLogicalAddress(), GetLogicalAddress(), CEC_OPCODE_GIVE_DEVICE_VENDOR_ID); |
| 192 | CLockObject lock(&m_mutex); |
| 193 | |
| 194 | if (m_processor->Transmit(command)) |
| 195 | m_condition.Wait(&m_mutex, 1000); |
| 196 | } |
| 197 | |
| 198 | return m_iVendorId; |
| 199 | } |
| 200 | |
| 201 | void CCECBusDevice::PollVendorId(void) |
| 202 | { |
| 203 | CLockObject lock(&m_mutex); |
| 204 | if (m_iLastActive > 0 && m_iLogicalAddress != CECDEVICE_BROADCAST && |
| 205 | m_iVendorId == CEC_VENDOR_UNKNOWN && |
| 206 | GetTimeMs() - m_iLastActive > 5000) |
| 207 | { |
| 208 | m_iLastActive = GetTimeMs(); |
| 209 | |
| 210 | cec_command command; |
| 211 | cec_command::format(command, GetMyLogicalAddress(), GetLogicalAddress(), CEC_OPCODE_GIVE_DEVICE_VENDOR_ID); |
| 212 | command.ack_timeout = 0; |
| 213 | m_processor->Transmit(command); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress, uint16_t iOldAddress /* = 0 */) |
| 218 | { |
| 219 | CStdString strLog; |
| 220 | strLog.Format(">> %i changed physical address from %04x to %04x", GetLogicalAddress(), m_iPhysicalAddress, iNewAddress); |
| 221 | AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
| 222 | |
| 223 | m_iPhysicalAddress = iNewAddress; |
| 224 | } |
| 225 | |
| 226 | bool CCECBusDevice::PowerOn(void) |
| 227 | { |
| 228 | CStdString strLog; |
| 229 | strLog.Format("<< powering on device with logical address %d", (int8_t)m_iLogicalAddress); |
| 230 | AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
| 231 | |
| 232 | cec_command command; |
| 233 | cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_IMAGE_VIEW_ON); |
| 234 | |
| 235 | return m_processor->Transmit(command); |
| 236 | } |
| 237 | |
| 238 | bool CCECBusDevice::Standby(void) |
| 239 | { |
| 240 | CStdString strLog; |
| 241 | strLog.Format("<< putting device with logical address %d in standby mode", (int8_t)m_iLogicalAddress); |
| 242 | AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
| 243 | |
| 244 | cec_command command; |
| 245 | cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_STANDBY); |
| 246 | |
| 247 | return m_processor->Transmit(command); |
| 248 | } |
| 249 | |
| 250 | bool CCECBusDevice::SetOSDString(cec_display_control duration, const char *strMessage) |
| 251 | { |
| 252 | CStdString strLog; |
| 253 | strLog.Format("<< display message '%s'", strMessage); |
| 254 | AddLog(CEC_LOG_NOTICE, strLog.c_str()); |
| 255 | |
| 256 | cec_command command; |
| 257 | cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_SET_OSD_STRING); |
| 258 | command.parameters.push_back((uint8_t)duration); |
| 259 | |
| 260 | for (unsigned int iPtr = 0; iPtr < strlen(strMessage); iPtr++) |
| 261 | command.parameters.push_back(strMessage[iPtr]); |
| 262 | |
| 263 | return m_processor->Transmit(command); |
| 264 | } |
| 265 | |
| 266 | bool CCECBusDevice::ReportCECVersion(void) |
| 267 | { |
| 268 | AddLog(CEC_LOG_NOTICE, "<< reporting CEC version as 1.3a"); |
| 269 | |
| 270 | cec_command command; |
| 271 | cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_CEC_VERSION); |
| 272 | command.parameters.push_back(CEC_VERSION_1_3A); |
| 273 | |
| 274 | return m_processor->Transmit(command); |
| 275 | } |
| 276 | |
| 277 | bool CCECBusDevice::ReportDeckStatus(void) |
| 278 | { |
| 279 | // need to support opcodes play and deck control before doing anything with this |
| 280 | AddLog(CEC_LOG_NOTICE, "<< deck status requested, feature abort"); |
| 281 | m_processor->TransmitAbort(m_iLogicalAddress, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID); |
| 282 | return false; |
| 283 | } |
| 284 | |
| 285 | bool CCECBusDevice::ReportMenuState(bool bActive /* = true */) |
| 286 | { |
| 287 | if (bActive) |
| 288 | AddLog(CEC_LOG_NOTICE, "<< reporting menu state as active"); |
| 289 | else |
| 290 | AddLog(CEC_LOG_NOTICE, "<< reporting menu state as inactive"); |
| 291 | |
| 292 | cec_command command; |
| 293 | cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_MENU_STATUS); |
| 294 | command.parameters.push_back(bActive ? (uint8_t) CEC_MENU_STATE_ACTIVATED : (uint8_t) CEC_MENU_STATE_DEACTIVATED); |
| 295 | |
| 296 | return m_processor->Transmit(command); |
| 297 | } |
| 298 | |
| 299 | bool CCECBusDevice::ReportOSDName(void) |
| 300 | { |
| 301 | const char *osdname = m_processor->GetDeviceName().c_str(); |
| 302 | CStdString strLog; |
| 303 | strLog.Format("<< reporting OSD name as %s", osdname); |
| 304 | AddLog(CEC_LOG_NOTICE, strLog.c_str()); |
| 305 | |
| 306 | cec_command command; |
| 307 | cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_SET_OSD_NAME); |
| 308 | for (unsigned int iPtr = 0; iPtr < strlen(osdname); iPtr++) |
| 309 | command.parameters.push_back(osdname[iPtr]); |
| 310 | |
| 311 | return m_processor->Transmit(command); |
| 312 | } |
| 313 | |
| 314 | bool CCECBusDevice::ReportPowerState(bool bOn /* = true */) |
| 315 | { |
| 316 | if (bOn) |
| 317 | AddLog(CEC_LOG_NOTICE, "<< reporting \"On\" power status"); |
| 318 | else |
| 319 | AddLog(CEC_LOG_NOTICE, "<< reporting \"Off\" power status"); |
| 320 | |
| 321 | cec_command command; |
| 322 | cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_REPORT_POWER_STATUS); |
| 323 | command.parameters.push_back(bOn ? (uint8_t) CEC_POWER_STATUS_ON : (uint8_t) CEC_POWER_STATUS_STANDBY); |
| 324 | |
| 325 | return m_processor->Transmit(command); |
| 326 | } |
| 327 | |
| 328 | bool CCECBusDevice::ReportVendorID(void) |
| 329 | { |
| 330 | AddLog(CEC_LOG_NOTICE, "<< vendor ID requested, feature abort"); |
| 331 | m_processor->TransmitAbort(m_iLogicalAddress, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID); |
| 332 | return false; |
| 333 | } |
| 334 | |
| 335 | bool CCECBusDevice::BroadcastActiveView(void) |
| 336 | { |
| 337 | AddLog(CEC_LOG_DEBUG, "<< setting active view"); |
| 338 | |
| 339 | cec_command command; |
| 340 | cec_command::format(command, GetMyLogicalAddress(), CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE); |
| 341 | command.parameters.push_back((m_iPhysicalAddress >> 8) & 0xFF); |
| 342 | command.parameters.push_back(m_iPhysicalAddress & 0xFF); |
| 343 | |
| 344 | return m_processor->Transmit(command); |
| 345 | } |
| 346 | |
| 347 | bool CCECBusDevice::BroadcastInactiveView(void) |
| 348 | { |
| 349 | AddLog(CEC_LOG_DEBUG, "<< setting inactive view"); |
| 350 | |
| 351 | cec_command command; |
| 352 | cec_command::format(command, GetMyLogicalAddress(), CECDEVICE_BROADCAST, CEC_OPCODE_INACTIVE_SOURCE); |
| 353 | command.parameters.push_back((m_iPhysicalAddress >> 8) & 0xFF); |
| 354 | command.parameters.push_back(m_iPhysicalAddress & 0xFF); |
| 355 | |
| 356 | return m_processor->Transmit(command); |
| 357 | } |
| 358 | |
| 359 | bool CCECBusDevice::BroadcastPhysicalAddress(void) |
| 360 | { |
| 361 | CStdString strLog; |
| 362 | strLog.Format("<< reporting physical address as %04x", m_iPhysicalAddress); |
| 363 | AddLog(CEC_LOG_NOTICE, strLog.c_str()); |
| 364 | |
| 365 | cec_command command; |
| 366 | cec_command::format(command, GetMyLogicalAddress(), CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS); |
| 367 | command.parameters.push_back((uint8_t) ((m_iPhysicalAddress >> 8) & 0xFF)); |
| 368 | command.parameters.push_back((uint8_t) (m_iPhysicalAddress & 0xFF)); |
| 369 | command.parameters.push_back((uint8_t) (CEC_DEVICE_TYPE_PLAYBACK_DEVICE)); |
| 370 | |
| 371 | return m_processor->Transmit(command); |
| 372 | } |
| 373 | |
| 374 | bool CCECBusDevice::BroadcastActiveSource(void) |
| 375 | { |
| 376 | AddLog(CEC_LOG_NOTICE, "<< broadcasting active source"); |
| 377 | |
| 378 | cec_command command; |
| 379 | cec_command::format(command, GetMyLogicalAddress(), CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE); |
| 380 | command.parameters.push_back((uint8_t) ((m_iPhysicalAddress >> 8) & 0xFF)); |
| 381 | command.parameters.push_back((uint8_t) (m_iPhysicalAddress & 0xFF)); |
| 382 | |
| 383 | return m_processor->Transmit(command); |
| 384 | } |
| 385 | |
| 386 | cec_version CCECBusDevice::GetCecVersion(void) |
| 387 | { |
| 388 | if (m_cecVersion == CEC_VERSION_UNKNOWN) |
| 389 | { |
| 390 | AddLog(CEC_LOG_NOTICE, "<< requesting CEC version"); |
| 391 | cec_command command; |
| 392 | cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GET_CEC_VERSION); |
| 393 | CLockObject lock(&m_mutex); |
| 394 | if (m_processor->Transmit(command)) |
| 395 | m_condition.Wait(&m_mutex, 1000); |
| 396 | } |
| 397 | |
| 398 | return m_cecVersion; |
| 399 | } |
| 400 | |
| 401 | cec_menu_language &CCECBusDevice::GetMenuLanguage(void) |
| 402 | { |
| 403 | if (!strcmp(m_menuLanguage.language, "???")) |
| 404 | { |
| 405 | AddLog(CEC_LOG_NOTICE, "<< requesting menu language"); |
| 406 | cec_command command; |
| 407 | cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GET_MENU_LANGUAGE); |
| 408 | CLockObject lock(&m_mutex); |
| 409 | if (m_processor->Transmit(command)) |
| 410 | m_condition.Wait(&m_mutex, 1000); |
| 411 | } |
| 412 | |
| 413 | return m_menuLanguage; |
| 414 | } |
| 415 | |
| 416 | cec_power_status CCECBusDevice::GetPowerStatus(void) |
| 417 | { |
| 418 | if (m_powerStatus == CEC_POWER_STATUS_UNKNOWN) |
| 419 | { |
| 420 | AddLog(CEC_LOG_NOTICE, "<< requesting power status"); |
| 421 | cec_command command; |
| 422 | cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS); |
| 423 | CLockObject lock(&m_mutex); |
| 424 | if (m_processor->Transmit(command)) |
| 425 | m_condition.Wait(&m_mutex, 1000); |
| 426 | } |
| 427 | |
| 428 | return m_powerStatus; |
| 429 | } |
| 430 | |
| 431 | const char *CCECBusDevice::CECVendorIdToString(const uint64_t iVendorId) |
| 432 | { |
| 433 | switch (iVendorId) |
| 434 | { |
| 435 | case CEC_VENDOR_SAMSUNG: |
| 436 | return "Samsung"; |
| 437 | case CEC_VENDOR_LG: |
| 438 | return "LG"; |
| 439 | default: |
| 440 | return "Unknown"; |
| 441 | } |
| 442 | } |