cec-client: don't activate the source when starting
[deb_libcec.git] / src / lib / devices / CECBusDevice.cpp
CommitLineData
e9de9629
LOK
1/*
2 * This file is part of the libCEC(R) library.
3 *
b492c10e 4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
e9de9629
LOK
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"
eafa9d46
LOK
34#include "../CECProcessor.h"
35#include "../implementations/ANCommandHandler.h"
36#include "../implementations/CECCommandHandler.h"
37#include "../implementations/SLCommandHandler.h"
11621576 38#include "../implementations/VLCommandHandler.h"
5477a250 39#include "../LibCEC.h"
ba65909d 40#include "../platform/util/timeutils.h"
e9de9629
LOK
41
42using namespace CEC;
f00ff009 43using namespace PLATFORM;
e9de9629 44
03ae897d 45#define ToString(p) m_processor->ToString(p)
c4098482 46
e9de9629 47CCECBusDevice::CCECBusDevice(CCECProcessor *processor, cec_logical_address iLogicalAddress, uint16_t iPhysicalAddress) :
ecc676ca
LOK
48 m_type (CEC_DEVICE_TYPE_RESERVED),
49 m_iPhysicalAddress (iPhysicalAddress),
50 m_iStreamPath (0),
51 m_iLogicalAddress (iLogicalAddress),
52 m_powerStatus (CEC_POWER_STATUS_UNKNOWN),
53 m_processor (processor),
54 m_vendor (CEC_VENDOR_UNKNOWN),
55 m_bReplaceHandler (false),
56 m_menuState (CEC_MENU_STATE_ACTIVATED),
57 m_bActiveSource (false),
58 m_iLastActive (0),
59 m_iLastPowerStateUpdate (0),
60 m_cecVersion (CEC_VERSION_UNKNOWN),
61 m_deviceStatus (CEC_DEVICE_STATUS_UNKNOWN),
62 m_iHandlerUseCount (0),
ebb6ddb3 63 m_bAwaitingReceiveFailed(false),
ecc676ca 64 m_bVendorIdRequested (false)
e9de9629
LOK
65{
66 m_handler = new CCECCommandHandler(this);
51b2a094 67
a3269a0a
LOK
68 for (unsigned int iPtr = 0; iPtr < 4; iPtr++)
69 m_menuLanguage.language[iPtr] = '?';
70 m_menuLanguage.language[3] = 0;
71 m_menuLanguage.device = iLogicalAddress;
1fcf5a3f 72
c4098482 73 m_strDeviceName = ToString(m_iLogicalAddress);
e9de9629
LOK
74}
75
76CCECBusDevice::~CCECBusDevice(void)
77{
78 delete m_handler;
79}
80
93729720 81bool CCECBusDevice::HandleCommand(const cec_command &command)
f8513317 82{
7f919115
LOK
83 bool bHandled(false);
84
85 /* update "last active" */
8fa35473 86 {
f00ff009 87 CLockObject lock(m_mutex);
8fa35473 88 m_iLastActive = GetTimeMs();
f4b7b1dc 89
ba427965 90 /* don't call GetStatus() here, just read the value with the mutex locked */
d297cbd4 91 if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC && command.opcode_set == 1)
f4b7b1dc 92 m_deviceStatus = CEC_DEVICE_STATUS_PRESENT;
1344fd1a
LOK
93
94 MarkBusy();
8fa35473
LOK
95 }
96
7f919115
LOK
97 /* handle the command */
98 bHandled = m_handler->HandleCommand(command);
8fa35473 99
7f919115
LOK
100 /* change status to present */
101 if (bHandled)
8d915412 102 {
f00ff009 103 CLockObject lock(m_mutex);
8fa35473 104 if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC)
8d915412 105 {
8fa35473 106 if (m_deviceStatus != CEC_DEVICE_STATUS_PRESENT)
5477a250 107 CLibCEC::AddLog(CEC_LOG_DEBUG, "device %s (%x) status changed to present after command %s", GetLogicalAddressName(), (uint8_t)GetLogicalAddress(), ToString(command.opcode));
8fa35473 108 m_deviceStatus = CEC_DEVICE_STATUS_PRESENT;
8d915412 109 }
8d915412 110 }
7f919115 111
1344fd1a 112 MarkReady();
7f919115 113 return bHandled;
93729720
LOK
114}
115
93729720
LOK
116bool CCECBusDevice::PowerOn(void)
117{
1344fd1a 118 bool bReturn(false);
f4698390 119 GetVendorId(); // ensure that we got the vendor id, because the implementations vary per vendor
c6d7f0e1 120
1344fd1a 121 MarkBusy();
8670c970
LOK
122 cec_power_status currentStatus = GetPowerStatus(false);
123 if (currentStatus != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON &&
124 currentStatus != CEC_POWER_STATUS_ON)
5e5637c6 125 {
8670c970
LOK
126 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
127 if (m_handler->PowerOn(GetMyLogicalAddress(), m_iLogicalAddress))
128 {
129 SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
130 bReturn = true;
131 }
132 }
133 else
134 {
135 CLibCEC::AddLog(CEC_LOG_NOTICE, "'%s' (%X) is already '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(currentStatus));
f437e4be
LOK
136 }
137
1344fd1a
LOK
138 MarkReady();
139 return bReturn;
93729720
LOK
140}
141
142bool CCECBusDevice::Standby(void)
143{
5477a250 144 CLibCEC::AddLog(CEC_LOG_DEBUG, "<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress);
1344fd1a
LOK
145 MarkBusy();
146 bool bReturn = m_handler->TransmitStandby(GetMyLogicalAddress(), m_iLogicalAddress);
147 MarkReady();
148 return bReturn;
93729720
LOK
149}
150
151/** @name Getters */
152//@{
7f919115 153cec_version CCECBusDevice::GetCecVersion(bool bUpdate /* = false */)
93729720 154{
6bbfc3f7 155 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
97fc4ffb
LOK
156 bool bRequestUpdate(false);
157 {
f00ff009 158 CLockObject lock(m_mutex);
ddb6ac5b 159 bRequestUpdate = bIsPresent &&
6bbfc3f7 160 (bUpdate || m_cecVersion == CEC_VERSION_UNKNOWN);
97fc4ffb
LOK
161 }
162
163 if (bRequestUpdate)
ebb6ddb3
LOK
164 {
165 CheckVendorIdRequested();
f294b22f 166 RequestCecVersion();
ebb6ddb3 167 }
f294b22f 168
f00ff009 169 CLockObject lock(m_mutex);
f294b22f
LOK
170 return m_cecVersion;
171}
172
a75e3a5a 173bool CCECBusDevice::RequestCecVersion(bool bWaitForResponse /* = true */)
f294b22f
LOK
174{
175 bool bReturn(false);
b64db02e 176
66c3ef5a
LOK
177 if (!MyLogicalAddressContains(m_iLogicalAddress) &&
178 !IsUnsupportedFeature(CEC_OPCODE_GET_CEC_VERSION))
93729720 179 {
1344fd1a 180 MarkBusy();
5477a250 181 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
8fa35473 182
a75e3a5a 183 bReturn = m_handler->TransmitRequestCecVersion(GetMyLogicalAddress(), m_iLogicalAddress, bWaitForResponse);
1344fd1a 184 MarkReady();
93729720 185 }
f294b22f 186 return bReturn;
93729720
LOK
187}
188
62f5527d
LOK
189const char* CCECBusDevice::GetLogicalAddressName(void) const
190{
c4098482 191 return ToString(m_iLogicalAddress);
62f5527d
LOK
192}
193
7f919115 194cec_menu_language &CCECBusDevice::GetMenuLanguage(bool bUpdate /* = false */)
93729720 195{
6bbfc3f7 196 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
97fc4ffb
LOK
197 bool bRequestUpdate(false);
198 {
f00ff009 199 CLockObject lock(m_mutex);
ddb6ac5b 200 bRequestUpdate = (bIsPresent &&
97fc4ffb
LOK
201 (bUpdate || !strcmp(m_menuLanguage.language, "???")));
202 }
203
204 if (bRequestUpdate)
ebb6ddb3
LOK
205 {
206 CheckVendorIdRequested();
f294b22f 207 RequestMenuLanguage();
ebb6ddb3 208 }
5e5637c6 209
f00ff009 210 CLockObject lock(m_mutex);
f294b22f
LOK
211 return m_menuLanguage;
212}
213
a75e3a5a 214bool CCECBusDevice::RequestMenuLanguage(bool bWaitForResponse /* = true */)
f294b22f
LOK
215{
216 bool bReturn(false);
b64db02e 217
4d738fe3
LOK
218 if (!MyLogicalAddressContains(m_iLogicalAddress) &&
219 !IsUnsupportedFeature(CEC_OPCODE_GET_MENU_LANGUAGE))
93729720 220 {
1344fd1a 221 MarkBusy();
5477a250 222 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
a75e3a5a 223 bReturn = m_handler->TransmitRequestMenuLanguage(GetMyLogicalAddress(), m_iLogicalAddress, bWaitForResponse);
1344fd1a 224 MarkReady();
93729720 225 }
f294b22f 226 return bReturn;
93729720
LOK
227}
228
3e61b350
LOK
229cec_menu_state CCECBusDevice::GetMenuState(void)
230{
f00ff009 231 CLockObject lock(m_mutex);
3e61b350
LOK
232 return m_menuState;
233}
234
93729720
LOK
235cec_logical_address CCECBusDevice::GetMyLogicalAddress(void) const
236{
237 return m_processor->GetLogicalAddress();
f8513317
LOK
238}
239
e9de9629
LOK
240uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const
241{
242 return m_processor->GetPhysicalAddress();
243}
244
7f919115 245CStdString CCECBusDevice::GetOSDName(bool bUpdate /* = false */)
ed21be2a 246{
6bbfc3f7 247 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
97fc4ffb
LOK
248 bool bRequestUpdate(false);
249 {
f00ff009 250 CLockObject lock(m_mutex);
ddb6ac5b 251 bRequestUpdate = bIsPresent &&
97fc4ffb 252 (bUpdate || m_strDeviceName.Equals(ToString(m_iLogicalAddress))) &&
ddb6ac5b 253 m_type != CEC_DEVICE_TYPE_TV;
97fc4ffb
LOK
254 }
255
256 if (bRequestUpdate)
ebb6ddb3
LOK
257 {
258 CheckVendorIdRequested();
5e5637c6 259 RequestOSDName();
ebb6ddb3 260 }
5e5637c6 261
f00ff009 262 CLockObject lock(m_mutex);
ed21be2a
LOK
263 return m_strDeviceName;
264}
265
a75e3a5a 266bool CCECBusDevice::RequestOSDName(bool bWaitForResponse /* = true */)
ed21be2a
LOK
267{
268 bool bReturn(false);
b64db02e 269
4d738fe3
LOK
270 if (!MyLogicalAddressContains(m_iLogicalAddress) &&
271 !IsUnsupportedFeature(CEC_OPCODE_GIVE_OSD_NAME))
ed21be2a 272 {
1344fd1a 273 MarkBusy();
5477a250 274 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting OSD name of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
a75e3a5a 275 bReturn = m_handler->TransmitRequestOSDName(GetMyLogicalAddress(), m_iLogicalAddress, bWaitForResponse);
1344fd1a 276 MarkReady();
ed21be2a
LOK
277 }
278 return bReturn;
279}
280
0680dab3 281uint16_t CCECBusDevice::GetPhysicalAddress(bool bSuppressUpdate /* = true */)
16b1e052 282{
0680dab3 283 if (!bSuppressUpdate)
16b1e052 284 {
0680dab3
LOK
285 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
286 bool bRequestUpdate(false);
287 {
288 CLockObject lock(m_mutex);
289 bRequestUpdate = bIsPresent && m_iPhysicalAddress == 0xFFFF;
290 }
16b1e052 291
0680dab3
LOK
292 if (bRequestUpdate)
293 {
294 CheckVendorIdRequested();
295 if (!RequestPhysicalAddress())
296 CLibCEC::AddLog(CEC_LOG_ERROR, "failed to request the physical address");
297 }
ebb6ddb3 298 }
97fc4ffb 299
f00ff009 300 CLockObject lock(m_mutex);
16b1e052
LOK
301 return m_iPhysicalAddress;
302}
303
a75e3a5a 304bool CCECBusDevice::RequestPhysicalAddress(bool bWaitForResponse /* = true */)
16b1e052
LOK
305{
306 bool bReturn(false);
b64db02e 307
16b1e052
LOK
308 if (!MyLogicalAddressContains(m_iLogicalAddress))
309 {
1344fd1a 310 MarkBusy();
5477a250 311 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
a75e3a5a 312 bReturn = m_handler->TransmitRequestPhysicalAddress(GetMyLogicalAddress(), m_iLogicalAddress, bWaitForResponse);
1344fd1a 313 MarkReady();
16b1e052
LOK
314 }
315 return bReturn;
316}
317
7f919115 318cec_power_status CCECBusDevice::GetPowerStatus(bool bUpdate /* = false */)
e9de9629 319{
6bbfc3f7 320 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
97fc4ffb
LOK
321 bool bRequestUpdate(false);
322 {
f00ff009 323 CLockObject lock(m_mutex);
ddb6ac5b 324 bRequestUpdate = (bIsPresent &&
c8f0eef0 325 (bUpdate || m_powerStatus == CEC_POWER_STATUS_UNKNOWN ||
2efa39b7
LOK
326 m_powerStatus == CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON ||
327 m_powerStatus == CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY ||
328 GetTimeMs() - m_iLastPowerStateUpdate >= CEC_POWER_STATE_REFRESH_TIME));
97fc4ffb
LOK
329 }
330
331 if (bRequestUpdate)
ebb6ddb3
LOK
332 {
333 CheckVendorIdRequested();
f294b22f 334 RequestPowerStatus();
ebb6ddb3 335 }
5e5637c6 336
f00ff009 337 CLockObject lock(m_mutex);
f294b22f
LOK
338 return m_powerStatus;
339}
340
a75e3a5a 341bool CCECBusDevice::RequestPowerStatus(bool bWaitForResponse /* = true */)
f294b22f
LOK
342{
343 bool bReturn(false);
b64db02e 344
4d738fe3
LOK
345 if (!MyLogicalAddressContains(m_iLogicalAddress) &&
346 !IsUnsupportedFeature(CEC_OPCODE_GIVE_DEVICE_POWER_STATUS))
93729720 347 {
1344fd1a 348 MarkBusy();
5477a250 349 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
a75e3a5a 350 bReturn = m_handler->TransmitRequestPowerStatus(GetMyLogicalAddress(), m_iLogicalAddress, bWaitForResponse);
1344fd1a 351 MarkReady();
93729720 352 }
f294b22f
LOK
353 return bReturn;
354}
93729720 355
7f919115 356cec_vendor_id CCECBusDevice::GetVendorId(bool bUpdate /* = false */)
f294b22f 357{
6bbfc3f7 358 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
97fc4ffb
LOK
359 bool bRequestUpdate(false);
360 {
f00ff009 361 CLockObject lock(m_mutex);
ddb6ac5b 362 bRequestUpdate = (bIsPresent &&
97fc4ffb
LOK
363 (bUpdate || m_vendor == CEC_VENDOR_UNKNOWN));
364 }
365
366 if (bRequestUpdate)
f294b22f 367 RequestVendorId();
5e5637c6 368
f00ff009 369 CLockObject lock(m_mutex);
f294b22f 370 return m_vendor;
e9de9629
LOK
371}
372
a75e3a5a 373bool CCECBusDevice::RequestVendorId(bool bWaitForResponse /* = true */)
a3269a0a 374{
f294b22f 375 bool bReturn(false);
b64db02e 376
63b56678 377 if (!MyLogicalAddressContains(m_iLogicalAddress) && GetMyLogicalAddress() != CECDEVICE_UNKNOWN)
a3269a0a 378 {
1344fd1a 379 MarkBusy();
5477a250 380 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
a75e3a5a 381 bReturn = m_handler->TransmitRequestVendorId(GetMyLogicalAddress(), m_iLogicalAddress, bWaitForResponse);
1344fd1a 382 MarkReady();
3e61b350 383
a75e3a5a
LOK
384 if (bWaitForResponse)
385 ReplaceHandler(true);
a3269a0a 386 }
f294b22f 387 return bReturn;
93729720
LOK
388}
389
7f919115 390const char *CCECBusDevice::GetVendorName(bool bUpdate /* = false */)
c4098482 391{
7f919115 392 return ToString(GetVendorId(bUpdate));
c4098482
LOK
393}
394
93729720
LOK
395bool CCECBusDevice::MyLogicalAddressContains(cec_logical_address address) const
396{
397 return m_processor->HasLogicalAddress(address);
a3269a0a
LOK
398}
399
9fd73dd4
LOK
400bool CCECBusDevice::NeedsPoll(void)
401{
402 bool bSendPoll(false);
403 switch (m_iLogicalAddress)
404 {
405 case CECDEVICE_PLAYBACKDEVICE3:
c6d7f0e1
LOK
406 {
407 cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_PLAYBACKDEVICE2]->GetStatus();
408 bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
409 }
9fd73dd4
LOK
410 break;
411 case CECDEVICE_PLAYBACKDEVICE2:
c6d7f0e1
LOK
412 {
413 cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_PLAYBACKDEVICE1]->GetStatus();
414 bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
415 }
9fd73dd4
LOK
416 break;
417 case CECDEVICE_RECORDINGDEVICE3:
c6d7f0e1
LOK
418 {
419 cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_RECORDINGDEVICE2]->GetStatus();
420 bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
421 }
9fd73dd4
LOK
422 break;
423 case CECDEVICE_RECORDINGDEVICE2:
c6d7f0e1
LOK
424 {
425 cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_RECORDINGDEVICE1]->GetStatus();
426 bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
427 }
9fd73dd4
LOK
428 break;
429 case CECDEVICE_TUNER4:
c6d7f0e1
LOK
430 {
431 cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_TUNER3]->GetStatus();
432 bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
433 }
9fd73dd4
LOK
434 break;
435 case CECDEVICE_TUNER3:
c6d7f0e1
LOK
436 {
437 cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_TUNER2]->GetStatus();
438 bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
439 }
9fd73dd4
LOK
440 break;
441 case CECDEVICE_TUNER2:
c6d7f0e1
LOK
442 {
443 cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_TUNER1]->GetStatus();
444 bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
445 }
9fd73dd4
LOK
446 break;
447 case CECDEVICE_AUDIOSYSTEM:
448 case CECDEVICE_PLAYBACKDEVICE1:
449 case CECDEVICE_RECORDINGDEVICE1:
450 case CECDEVICE_TUNER1:
451 case CECDEVICE_TV:
452 bSendPoll = true;
453 break;
454 default:
455 break;
456 }
457
458 return bSendPoll;
459}
460
a75e3a5a 461cec_bus_device_status CCECBusDevice::GetStatus(bool bForcePoll /* = false */, bool bSuppressPoll /* = false */)
f8ae3295 462{
ba427965
LOK
463 cec_bus_device_status status(CEC_DEVICE_STATUS_UNKNOWN);
464 bool bNeedsPoll(false);
465
466 {
467 CLockObject lock(m_mutex);
468 status = m_deviceStatus;
a75e3a5a
LOK
469 bNeedsPoll = !bSuppressPoll &&
470 (bForcePoll || m_deviceStatus == CEC_DEVICE_STATUS_UNKNOWN);
ba427965
LOK
471 }
472
473 if (bNeedsPoll)
f8ae3295 474 {
9fd73dd4 475 bool bPollAcked(false);
7b62b76e 476 if (bNeedsPoll && NeedsPoll())
9fd73dd4 477 bPollAcked = m_processor->PollDevice(m_iLogicalAddress);
95a73fa7 478
ba427965
LOK
479 status = bPollAcked ? CEC_DEVICE_STATUS_PRESENT : CEC_DEVICE_STATUS_NOT_PRESENT;
480 SetDeviceStatus(status);
f8ae3295
LOK
481 }
482
ba427965 483 return status;
f8ae3295
LOK
484}
485
93729720
LOK
486//@}
487
488/** @name Setters */
489//@{
e55f3f70 490void CCECBusDevice::SetCecVersion(const cec_version newVersion)
6a1c0009 491{
a20d808d
LOK
492 if (m_cecVersion != newVersion)
493 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s (%X): CEC version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(newVersion));
6a1c0009 494 m_cecVersion = newVersion;
6a1c0009
LOK
495}
496
93729720
LOK
497void CCECBusDevice::SetMenuLanguage(const cec_menu_language &language)
498{
f00ff009 499 CLockObject lock(m_mutex);
a20d808d
LOK
500 if (language.device == m_iLogicalAddress &&
501 strcmp(language.language, m_menuLanguage.language))
93729720 502 {
5477a250 503 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): menu language set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, language.language);
93729720
LOK
504 m_menuLanguage = language;
505 }
506}
507
15d1a84c
LOK
508void CCECBusDevice::SetOSDName(CStdString strName)
509{
f00ff009 510 CLockObject lock(m_mutex);
15d1a84c
LOK
511 if (m_strDeviceName != strName)
512 {
5477a250 513 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): osd name set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, strName.c_str());
15d1a84c
LOK
514 m_strDeviceName = strName;
515 }
516}
517
28fa6c97
LOK
518void CCECBusDevice::SetMenuState(const cec_menu_state state)
519{
f00ff009 520 CLockObject lock(m_mutex);
28fa6c97
LOK
521 if (m_menuState != state)
522 {
5477a250 523 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): menu state set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_menuState));
28fa6c97
LOK
524 m_menuState = state;
525 }
526}
527
3fd2a2b8 528void CCECBusDevice::SetInactiveSource(void)
7856411b 529{
3fd2a2b8 530 {
f00ff009 531 CLockObject lock(m_mutex);
24dd566c
LOK
532 if (m_bActiveSource)
533 CLibCEC::AddLog(CEC_LOG_DEBUG, "marking %s (%X) as inactive source", GetLogicalAddressName(), m_iLogicalAddress);
3fd2a2b8
LOK
534 m_bActiveSource = false;
535 }
7856411b
LOK
536}
537
37b0c572 538void CCECBusDevice::SetActiveSource(void)
7856411b 539{
f00ff009 540 CLockObject lock(m_mutex);
ff684fef 541 if (!m_bActiveSource)
5477a250 542 CLibCEC::AddLog(CEC_LOG_DEBUG, "making %s (%x) the active source", GetLogicalAddressName(), m_iLogicalAddress);
f818ec6c
LOK
543 else
544 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s (%x) was already marked as active source", GetLogicalAddressName(), m_iLogicalAddress);
7856411b
LOK
545
546 for (int iPtr = 0; iPtr < 16; iPtr++)
547 if (iPtr != m_iLogicalAddress)
3fd2a2b8 548 m_processor->m_busDevices[iPtr]->SetInactiveSource();
7856411b
LOK
549
550 m_bActiveSource = true;
9a2f12df 551 SetPowerStatus(CEC_POWER_STATUS_ON);
7856411b
LOK
552}
553
93fff5c1
LOK
554bool CCECBusDevice::TryLogicalAddress(void)
555{
5477a250 556 CLibCEC::AddLog(CEC_LOG_DEBUG, "trying logical address '%s'", GetLogicalAddressName());
93fff5c1 557
b81f75db 558 m_processor->SetAckMask(0);
93fff5c1
LOK
559 if (!TransmitPoll(m_iLogicalAddress))
560 {
5477a250 561 CLibCEC::AddLog(CEC_LOG_NOTICE, "using logical address '%s'", GetLogicalAddressName());
93fff5c1
LOK
562 SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
563
564 return true;
565 }
566
5477a250 567 CLibCEC::AddLog(CEC_LOG_DEBUG, "logical address '%s' already taken", GetLogicalAddressName());
93fff5c1
LOK
568 SetDeviceStatus(CEC_DEVICE_STATUS_PRESENT);
569 return false;
570}
571
a20d808d
LOK
572void CCECBusDevice::ResetDeviceStatus(void)
573{
574 CLockObject lock(m_mutex);
575 SetPowerStatus (CEC_POWER_STATUS_UNKNOWN);
576 SetVendorId (CEC_VENDOR_UNKNOWN);
577 SetMenuState (CEC_MENU_STATE_ACTIVATED);
578 SetCecVersion (CEC_VERSION_UNKNOWN);
f818ec6c 579 SetStreamPath (0xFFFF);
a20d808d
LOK
580 SetOSDName (ToString(m_iLogicalAddress));
581 SetInactiveSource();
582 m_iLastActive = 0;
c04eaabc 583 m_unsupportedFeatures.clear();
a20d808d
LOK
584}
585
93fff5c1
LOK
586void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus)
587{
93fff5c1 588 {
ee17ad58
LOK
589 CLockObject lock(m_mutex);
590 switch (newStatus)
591 {
592 case CEC_DEVICE_STATUS_UNKNOWN:
593 if (m_deviceStatus != newStatus)
594 CLibCEC::AddLog(CEC_LOG_DEBUG, "device status of %s changed into 'unknown'", ToString(m_iLogicalAddress));
a20d808d
LOK
595 ResetDeviceStatus();
596 m_deviceStatus = newStatus;
ee17ad58
LOK
597 break;
598 case CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC:
599 if (m_deviceStatus != newStatus)
600 CLibCEC::AddLog(CEC_LOG_DEBUG, "device status of %s changed into 'handled by libCEC'", ToString(m_iLogicalAddress));
a20d808d
LOK
601 SetPowerStatus (CEC_POWER_STATUS_ON);
602 SetVendorId (CEC_VENDOR_UNKNOWN);
603 SetMenuState (CEC_MENU_STATE_ACTIVATED);
604 SetCecVersion (CEC_VERSION_1_3A);
f818ec6c 605 SetStreamPath (0xFFFF);
a20d808d
LOK
606 SetInactiveSource();
607 m_iLastActive = 0;
608 m_deviceStatus = newStatus;
ee17ad58
LOK
609 break;
610 case CEC_DEVICE_STATUS_PRESENT:
611 if (m_deviceStatus != newStatus)
612 CLibCEC::AddLog(CEC_LOG_DEBUG, "device status of %s changed into 'present'", ToString(m_iLogicalAddress));
613 m_deviceStatus = newStatus;
614 break;
615 case CEC_DEVICE_STATUS_NOT_PRESENT:
616 if (m_deviceStatus != newStatus)
a20d808d 617 {
ee17ad58 618 CLibCEC::AddLog(CEC_LOG_DEBUG, "device status of %s changed into 'not present'", ToString(m_iLogicalAddress));
a20d808d
LOK
619 ResetDeviceStatus();
620 m_deviceStatus = newStatus;
621 }
ee17ad58
LOK
622 break;
623 }
93fff5c1 624 }
ee17ad58
LOK
625
626 if (newStatus == CEC_DEVICE_STATUS_PRESENT)
a75e3a5a 627 RequestVendorId(false);
93fff5c1
LOK
628}
629
9dc04b07 630void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress)
93729720 631{
f00ff009 632 CLockObject lock(m_mutex);
b8176e3c 633 if (iNewAddress > 0 && m_iPhysicalAddress != iNewAddress)
93729720 634 {
5477a250 635 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): physical address changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress, iNewAddress);
93729720
LOK
636 m_iPhysicalAddress = iNewAddress;
637 }
638}
639
9dc04b07
LOK
640void CCECBusDevice::SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress /* = 0 */)
641{
f00ff009 642 CLockObject lock(m_mutex);
5f2068fe 643 if (iNewAddress != m_iStreamPath)
9dc04b07 644 {
5477a250 645 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, iOldAddress == 0 ? m_iStreamPath : iOldAddress, iNewAddress);
9dc04b07 646 m_iStreamPath = iNewAddress;
5f2068fe 647 }
f818ec6c
LOK
648 else
649 {
650 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s (%X): stream path unchanged (%04x)", GetLogicalAddressName(), m_iLogicalAddress, m_iStreamPath);
651 }
96274140 652
5f2068fe
LOK
653 CCECBusDevice *device = m_processor->GetDeviceByPhysicalAddress(iNewAddress);
654 if (device)
655 {
656 // if a device is found with the new physical address, mark it as active, which will automatically mark all other devices as inactive
657 device->SetActiveSource();
658 }
659 else
660 {
661 // try to find the device with the old address, and mark it as inactive when found
662 device = m_processor->GetDeviceByPhysicalAddress(iOldAddress);
0cb55c43 663 if (device)
5f2068fe 664 device->SetInactiveSource();
9dc04b07 665 }
5f2068fe
LOK
666
667 SetPowerStatus(CEC_POWER_STATUS_ON);
9dc04b07
LOK
668}
669
e55f3f70
LOK
670void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus)
671{
f00ff009 672 CLockObject lock(m_mutex);
b0271d54
LOK
673 if (m_powerStatus != powerStatus)
674 {
2efa39b7 675 m_iLastPowerStateUpdate = GetTimeMs();
5477a250 676 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): power status changed from '%s' to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_powerStatus), ToString(powerStatus));
b0271d54
LOK
677 m_powerStatus = powerStatus;
678 }
e55f3f70
LOK
679}
680
1344fd1a
LOK
681void CCECBusDevice::MarkBusy(void)
682{
683 CLockObject handlerLock(m_handlerMutex);
684 ++m_iHandlerUseCount;
685}
686
687void CCECBusDevice::MarkReady(void)
688{
689 CLockObject handlerLock(m_handlerMutex);
690 if (m_iHandlerUseCount > 0)
691 --m_iHandlerUseCount;
692}
693
3e61b350 694bool CCECBusDevice::ReplaceHandler(bool bActivateSource /* = true */)
e9de9629 695{
94e9a2af
LOK
696 bool bInitHandler(false);
697 {
698 CTryLockObject lock(m_mutex);
699 if (!lock.IsLocked())
700 return false;
2563e7eb 701
94e9a2af
LOK
702 CLockObject handlerLock(m_handlerMutex);
703 if (m_iHandlerUseCount > 0)
704 return false;
d211708b 705
94e9a2af 706 MarkBusy();
104125dc 707
94e9a2af 708 if (m_vendor != m_handler->GetVendorId())
3e61b350 709 {
94e9a2af 710 if (CCECCommandHandler::HasSpecificHandler(m_vendor))
2e49a6e4 711 {
94e9a2af
LOK
712 CLibCEC::AddLog(CEC_LOG_DEBUG, "replacing the command handler for device '%s' (%x)", GetLogicalAddressName(), GetLogicalAddress());
713 delete m_handler;
714
715 switch (m_vendor)
716 {
717 case CEC_VENDOR_SAMSUNG:
718 m_handler = new CANCommandHandler(this);
719 break;
720 case CEC_VENDOR_LG:
721 m_handler = new CSLCommandHandler(this);
722 break;
723 case CEC_VENDOR_PANASONIC:
724 m_handler = new CVLCommandHandler(this);
725 break;
726 default:
727 m_handler = new CCECCommandHandler(this);
728 break;
729 }
730
731 m_handler->SetVendorId(m_vendor);
732 bInitHandler = true;
2e49a6e4 733 }
2e49a6e4 734 }
e9de9629
LOK
735 }
736
104125dc
LOK
737 if (bInitHandler)
738 {
739 m_handler->InitHandler();
740
741 if (bActivateSource && m_processor->GetLogicalAddresses().IsSet(m_iLogicalAddress) && m_processor->IsInitialised() && IsActiveSource())
742 m_handler->ActivateSource();
743 }
744
745 MarkReady();
746
b64db02e
LOK
747 return true;
748}
749
3e61b350 750bool CCECBusDevice::SetVendorId(uint64_t iVendorId)
b64db02e
LOK
751{
752 bool bVendorChanged(false);
753
754 {
f00ff009 755 CLockObject lock(m_mutex);
b64db02e
LOK
756 bVendorChanged = (m_vendor != (cec_vendor_id)iVendorId);
757 m_vendor = (cec_vendor_id)iVendorId;
b64db02e 758 }
8d915412 759
a20d808d
LOK
760 if (bVendorChanged)
761 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_vendor), m_vendor);
8fa35473
LOK
762
763 return bVendorChanged;
e9de9629 764}
93729720 765//@}
e9de9629 766
93729720
LOK
767/** @name Transmit methods */
768//@{
769bool CCECBusDevice::TransmitActiveSource(void)
0f23c85c 770{
8fa35473 771 bool bSendActiveSource(false);
0f23c85c 772
8747dd4f 773 {
f00ff009 774 CLockObject lock(m_mutex);
49c8f2e4 775 if (m_powerStatus != CEC_POWER_STATUS_ON && m_powerStatus != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON)
5477a250 776 CLibCEC::AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress);
8fa35473
LOK
777 else if (m_bActiveSource)
778 {
5477a250 779 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): active source (%4x)", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress);
8fa35473
LOK
780 bSendActiveSource = true;
781 }
782 else
5477a250 783 CLibCEC::AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress);
8747dd4f
LOK
784 }
785
b64db02e
LOK
786 if (bSendActiveSource)
787 {
1344fd1a 788 MarkBusy();
468a1414 789 m_handler->TransmitActiveSource(m_iLogicalAddress, m_iPhysicalAddress);
1344fd1a 790 MarkReady();
b64db02e
LOK
791 return true;
792 }
793
794 return false;
0f23c85c
LOK
795}
796
29912296 797bool CCECBusDevice::TransmitCECVersion(cec_logical_address dest)
0f23c85c 798{
8fa35473
LOK
799 cec_version version;
800 {
f00ff009 801 CLockObject lock(m_mutex);
5477a250 802 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): cec version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_cecVersion));
8fa35473
LOK
803 version = m_cecVersion;
804 }
0f23c85c 805
1344fd1a
LOK
806 MarkBusy();
807 bool bReturn = m_handler->TransmitCECVersion(m_iLogicalAddress, dest, version);
808 MarkReady();
809 return bReturn;
0f23c85c
LOK
810}
811
49c8f2e4
LOK
812bool CCECBusDevice::TransmitImageViewOn(void)
813{
49c8f2e4 814 {
9a2f12df
LOK
815 CLockObject lock(m_mutex);
816 if (m_powerStatus != CEC_POWER_STATUS_ON && m_powerStatus != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON)
817 {
818 CLibCEC::AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress);
819 return false;
820 }
49c8f2e4 821 }
9a2f12df
LOK
822
823 MarkBusy();
824 m_handler->TransmitImageViewOn(m_iLogicalAddress, CECDEVICE_TV);
825 MarkReady();
826 return true;
49c8f2e4
LOK
827}
828
ab27363d 829bool CCECBusDevice::TransmitInactiveSource(void)
93729720 830{
8fa35473
LOK
831 uint16_t iPhysicalAddress;
832 {
f00ff009 833 CLockObject lock(m_mutex);
5477a250 834 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): inactive source", GetLogicalAddressName(), m_iLogicalAddress);
8fa35473
LOK
835 iPhysicalAddress = m_iPhysicalAddress;
836 }
93729720 837
1344fd1a
LOK
838 MarkBusy();
839 bool bReturn = m_handler->TransmitInactiveSource(m_iLogicalAddress, iPhysicalAddress);
840 MarkReady();
841 return bReturn;
93729720
LOK
842}
843
29912296 844bool CCECBusDevice::TransmitMenuState(cec_logical_address dest)
0f23c85c 845{
8fa35473
LOK
846 cec_menu_state menuState;
847 {
f00ff009 848 CLockObject lock(m_mutex);
5477a250 849 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): menu state '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_menuState));
8fa35473
LOK
850 menuState = m_menuState;
851 }
0f23c85c 852
1344fd1a
LOK
853 MarkBusy();
854 bool bReturn = m_handler->TransmitMenuState(m_iLogicalAddress, dest, menuState);
855 MarkReady();
856 return bReturn;
0f23c85c
LOK
857}
858
29912296 859bool CCECBusDevice::TransmitOSDName(cec_logical_address dest)
0f23c85c 860{
8fa35473
LOK
861 CStdString strDeviceName;
862 {
f00ff009 863 CLockObject lock(m_mutex);
5477a250 864 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, m_strDeviceName.c_str());
8fa35473
LOK
865 strDeviceName = m_strDeviceName;
866 }
0f23c85c 867
1344fd1a
LOK
868 MarkBusy();
869 bool bReturn = m_handler->TransmitOSDName(m_iLogicalAddress, dest, strDeviceName);
870 MarkReady();
871 return bReturn;
0f23c85c
LOK
872}
873
38bdb943
LOK
874bool CCECBusDevice::TransmitOSDString(cec_logical_address dest, cec_display_control duration, const char *strMessage)
875{
1344fd1a 876 bool bReturn(false);
66c3ef5a 877 if (!m_processor->m_busDevices[dest]->IsUnsupportedFeature(CEC_OPCODE_SET_OSD_STRING))
4d738fe3 878 {
5477a250 879 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): display OSD message '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, strMessage);
1344fd1a
LOK
880 MarkBusy();
881 bReturn = m_handler->TransmitOSDString(m_iLogicalAddress, dest, duration, strMessage);
882 MarkReady();
4d738fe3 883 }
1344fd1a 884 return bReturn;
38bdb943
LOK
885}
886
29912296 887bool CCECBusDevice::TransmitPhysicalAddress(void)
0f23c85c 888{
8fa35473
LOK
889 uint16_t iPhysicalAddress;
890 cec_device_type type;
891 {
f00ff009 892 CLockObject lock(m_mutex);
8fa35473
LOK
893 if (m_iPhysicalAddress == 0xffff)
894 return false;
2bdfdeef 895
5477a250 896 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress);
8fa35473
LOK
897 iPhysicalAddress = m_iPhysicalAddress;
898 type = m_type;
899 }
0f23c85c 900
1344fd1a
LOK
901 MarkBusy();
902 bool bReturn = m_handler->TransmitPhysicalAddress(m_iLogicalAddress, iPhysicalAddress, type);
903 MarkReady();
904 return bReturn;
0f23c85c
LOK
905}
906
fbdea54c
MK
907bool CCECBusDevice::TransmitSetMenuLanguage(cec_logical_address dest)
908{
909 bool bReturn(false);
910 cec_menu_language language = GetMenuLanguage();
911
912 char lang[3];
913 {
914 CLockObject lock(m_mutex);
915 lang[0] = language.language[0];
916 lang[1] = language.language[1];
917 lang[2] = language.language[2];
918 }
919
920 MarkBusy();
921 if (lang[0] == '?' && lang[1] == '?' && lang[2] == '?')
922 {
923 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): Menu language feature abort", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest);
924 m_processor->TransmitAbort(dest, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
925 bReturn = true;
926 }
927 else
928 {
929 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): Menu language '%s'", GetLogicalAddressName(), m_iLogicalAddress, lang);
930 bReturn = m_handler->TransmitSetMenuLanguage(m_iLogicalAddress, lang);
931 }
932 MarkReady();
933 return bReturn;
934}
935
93729720 936bool CCECBusDevice::TransmitPoll(cec_logical_address dest)
57f45e6c
LOK
937{
938 bool bReturn(false);
93729720
LOK
939 if (dest == CECDEVICE_UNKNOWN)
940 dest = m_iLogicalAddress;
f8513317 941
b750a5c3
LOK
942 CCECBusDevice *destDevice = m_processor->m_busDevices[dest];
943 if (destDevice->m_deviceStatus == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC)
944 return bReturn;
945
1344fd1a 946 MarkBusy();
5477a250 947 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest);
8fa35473 948 bReturn = m_handler->TransmitPoll(m_iLogicalAddress, dest);
5477a250 949 CLibCEC::AddLog(CEC_LOG_DEBUG, bReturn ? ">> POLL sent" : ">> POLL not sent");
8fa35473 950
f00ff009 951 CLockObject lock(m_mutex);
1674de37 952 if (bReturn)
8fa35473 953 {
1674de37 954 m_iLastActive = GetTimeMs();
b750a5c3 955 destDevice->m_deviceStatus = CEC_DEVICE_STATUS_PRESENT;
8fa35473 956 }
b750a5c3
LOK
957 else
958 destDevice->m_deviceStatus = CEC_DEVICE_STATUS_NOT_PRESENT;
1674de37 959
1344fd1a 960 MarkReady();
57f45e6c
LOK
961 return bReturn;
962}
93729720
LOK
963
964bool CCECBusDevice::TransmitPowerState(cec_logical_address dest)
965{
8fa35473
LOK
966 cec_power_status state;
967 {
f00ff009 968 CLockObject lock(m_mutex);
bbc71623 969 if (!IsActiveSource())
24dd566c
LOK
970 {
971 CLibCEC::AddLog(CEC_LOG_NOTICE, "power state requested of %s (%X), but we are not the active source. setting power state to standby", GetLogicalAddressName(), m_iLogicalAddress);
bbc71623 972 SetPowerStatus(CEC_POWER_STATUS_STANDBY);
24dd566c 973 }
bbc71623 974
5477a250 975 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_powerStatus));
8fa35473
LOK
976 state = m_powerStatus;
977 }
93729720 978
1344fd1a
LOK
979 MarkBusy();
980 bool bReturn = m_handler->TransmitPowerState(m_iLogicalAddress, dest, state);
981 MarkReady();
982 return bReturn;
93729720
LOK
983}
984
fe6f8e37 985bool CCECBusDevice::TransmitVendorID(cec_logical_address dest, bool bSendAbort /* = true */)
93729720 986{
1344fd1a 987 bool bReturn(false);
8fa35473
LOK
988 uint64_t iVendorId;
989 {
f00ff009 990 CLockObject lock(m_mutex);
8fa35473
LOK
991 iVendorId = (uint64_t)m_vendor;
992 }
993
1344fd1a 994 MarkBusy();
8fa35473 995 if (iVendorId == CEC_VENDOR_UNKNOWN)
c4098482 996 {
fe6f8e37
LOK
997 if (bSendAbort)
998 {
5477a250 999 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest);
fe6f8e37 1000 m_processor->TransmitAbort(dest, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
1344fd1a 1001 bReturn = true;
fe6f8e37 1002 }
c4098482
LOK
1003 }
1004 else
1005 {
5477a250 1006 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): vendor id %s (%x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString((cec_vendor_id)iVendorId), iVendorId);
1344fd1a 1007 bReturn = m_handler->TransmitVendorID(m_iLogicalAddress, iVendorId);
c4098482 1008 }
1344fd1a
LOK
1009 MarkReady();
1010 return bReturn;
93729720 1011}
a33794d8 1012
4bec9d79 1013bool CCECBusDevice::TransmitKeypress(cec_user_control_code key, bool bWait /* = true */)
a33794d8 1014{
1344fd1a
LOK
1015 MarkBusy();
1016 bool bReturn = m_handler->TransmitKeypress(m_processor->GetLogicalAddress(), m_iLogicalAddress, key, bWait);
1017 MarkReady();
1018 return bReturn;
a33794d8
LOK
1019}
1020
4bec9d79 1021bool CCECBusDevice::TransmitKeyRelease(bool bWait /* = true */)
a33794d8 1022{
1344fd1a
LOK
1023 MarkBusy();
1024 bool bReturn = m_handler->TransmitKeyRelease(m_processor->GetLogicalAddress(), m_iLogicalAddress, bWait);
1025 MarkReady();
1026 return bReturn;
a33794d8 1027}
4d738fe3 1028
33dd87a9 1029bool CCECBusDevice::IsUnsupportedFeature(cec_opcode opcode)
4d738fe3 1030{
33dd87a9 1031 CLockObject lock(m_mutex);
66c3ef5a
LOK
1032 bool bUnsupported = (m_unsupportedFeatures.find(opcode) != m_unsupportedFeatures.end());
1033 if (bUnsupported)
2f2cfc2d 1034 CLibCEC::AddLog(CEC_LOG_DEBUG, "'%s' is marked as unsupported feature for device '%s'", ToString(opcode), GetLogicalAddressName());
66c3ef5a 1035 return bUnsupported;
4d738fe3
LOK
1036}
1037
1038void CCECBusDevice::SetUnsupportedFeature(cec_opcode opcode)
1039{
ad7e0696
LOK
1040 // some commands should never be marked as unsupported
1041 if (opcode == CEC_OPCODE_VENDOR_COMMAND ||
1042 opcode == CEC_OPCODE_VENDOR_COMMAND_WITH_ID ||
1043 opcode == CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN ||
1044 opcode == CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP ||
1045 opcode == CEC_OPCODE_ABORT ||
1046 opcode == CEC_OPCODE_FEATURE_ABORT ||
1047 opcode == CEC_OPCODE_NONE)
1048 return;
1049
b499cf16
LOK
1050 {
1051 CLockObject lock(m_mutex);
ad7e0696
LOK
1052 if (m_unsupportedFeatures.find(opcode) == m_unsupportedFeatures.end())
1053 {
1054 CLibCEC::AddLog(CEC_LOG_DEBUG, "marking opcode '%s' as unsupported feature for device '%s'", ToString(opcode), GetLogicalAddressName());
1055 m_unsupportedFeatures.insert(opcode);
1056 }
b499cf16
LOK
1057 }
1058
1059 // signal threads that are waiting for a reponse
1060 MarkBusy();
1061 m_handler->SignalOpcode(cec_command::GetResponseOpcode(opcode));
1062 MarkReady();
4d738fe3 1063}
b64db02e 1064
3e61b350 1065bool CCECBusDevice::ActivateSource(void)
b64db02e 1066{
e2c644d2 1067 CLibCEC::AddLog(CEC_LOG_DEBUG, "activating source '%s'", ToString(m_iLogicalAddress));
1344fd1a
LOK
1068 MarkBusy();
1069 bool bReturn = m_handler->ActivateSource();
1070 MarkReady();
1071 return bReturn;
b64db02e
LOK
1072}
1073
a75e3a5a
LOK
1074void CCECBusDevice::HandlePoll(cec_logical_address destination)
1075{
1076 if (destination >= 0 && destination < CECDEVICE_BROADCAST)
1077 {
1078 CCECBusDevice *device = m_processor->m_busDevices[destination];
1079 if (device)
1080 device->HandlePollFrom(m_iLogicalAddress);
1081 }
1082}
1083
1084void CCECBusDevice::HandlePollFrom(cec_logical_address initiator)
0cfdeb5a 1085{
a75e3a5a 1086 CLibCEC::AddLog(CEC_LOG_DEBUG, "<< POLL: %s (%x) -> %s (%x)", ToString(initiator), initiator, ToString(m_iLogicalAddress), m_iLogicalAddress);
ef583662 1087 m_bAwaitingReceiveFailed = true;
0cfdeb5a
LOK
1088}
1089
1090bool CCECBusDevice::HandleReceiveFailed(void)
1091{
ef583662
LOK
1092 bool bReturn = m_bAwaitingReceiveFailed;
1093 m_bAwaitingReceiveFailed = false;
1094 return bReturn;
0cfdeb5a
LOK
1095}
1096
ebb6ddb3
LOK
1097void CCECBusDevice::CheckVendorIdRequested(void)
1098{
1099 bool bRequestVendorId(false);
1100 {
1101 CLockObject lock(m_mutex);
1102 bRequestVendorId = !m_bVendorIdRequested;
1103 m_bVendorIdRequested = true;
1104 }
1105
1106 if (bRequestVendorId)
1107 {
1108 ReplaceHandler(false);
1109 GetVendorId();
1110 }
1111}
1112
b78b4e33
LOK
1113bool CCECBusDevice::TransmitPendingActiveSourceCommands(void)
1114{
1115 MarkBusy();
1116 bool bReturn = m_handler->TransmitPendingActiveSourceCommands();
1117 MarkReady();
1118 return bReturn;
1119}
1120
93729720 1121//@}