Merge branch 'master' into release
[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
2b44051c 33#include "env.h"
e9de9629 34#include "CECBusDevice.h"
2b44051c
LOK
35
36#include "lib/CECProcessor.h"
37#include "lib/CECClient.h"
38#include "lib/implementations/ANCommandHandler.h"
39#include "lib/implementations/CECCommandHandler.h"
40#include "lib/implementations/SLCommandHandler.h"
41#include "lib/implementations/VLCommandHandler.h"
42#include "lib/LibCEC.h"
43#include "lib/CECTypeUtils.h"
44#include "lib/platform/util/timeutils.h"
45#include "lib/platform/util/util.h"
e9de9629 46
004b8382
LOK
47#include "CECAudioSystem.h"
48#include "CECPlaybackDevice.h"
49#include "CECRecordingDevice.h"
50#include "CECTuner.h"
51#include "CECTV.h"
52
53using namespace std;
e9de9629 54using namespace CEC;
f00ff009 55using namespace PLATFORM;
e9de9629 56
004b8382 57#define LIB_CEC m_processor->GetLib()
0d800fe5 58#define ToString(p) CCECTypeUtils::ToString(p)
c4098482 59
d2d1660c 60CCECBusDevice::CCECBusDevice(CCECProcessor *processor, cec_logical_address iLogicalAddress, uint16_t iPhysicalAddress /* = CEC_INVALID_PHYSICAL_ADDRESS */) :
ecc676ca
LOK
61 m_type (CEC_DEVICE_TYPE_RESERVED),
62 m_iPhysicalAddress (iPhysicalAddress),
b32ffd87 63 m_iStreamPath (CEC_INVALID_PHYSICAL_ADDRESS),
ecc676ca
LOK
64 m_iLogicalAddress (iLogicalAddress),
65 m_powerStatus (CEC_POWER_STATUS_UNKNOWN),
66 m_processor (processor),
67 m_vendor (CEC_VENDOR_UNKNOWN),
68 m_bReplaceHandler (false),
69 m_menuState (CEC_MENU_STATE_ACTIVATED),
70 m_bActiveSource (false),
71 m_iLastActive (0),
72 m_iLastPowerStateUpdate (0),
73 m_cecVersion (CEC_VERSION_UNKNOWN),
74 m_deviceStatus (CEC_DEVICE_STATUS_UNKNOWN),
75 m_iHandlerUseCount (0),
ebb6ddb3 76 m_bAwaitingReceiveFailed(false),
060a7b5e
LOK
77 m_bVendorIdRequested (false),
78 m_waitForResponse (new CWaitForResponse)
e9de9629
LOK
79{
80 m_handler = new CCECCommandHandler(this);
51b2a094 81
a3269a0a
LOK
82 for (unsigned int iPtr = 0; iPtr < 4; iPtr++)
83 m_menuLanguage.language[iPtr] = '?';
84 m_menuLanguage.language[3] = 0;
85 m_menuLanguage.device = iLogicalAddress;
1fcf5a3f 86
c4098482 87 m_strDeviceName = ToString(m_iLogicalAddress);
e9de9629
LOK
88}
89
90CCECBusDevice::~CCECBusDevice(void)
91{
c9d15485 92 DELETE_AND_NULL(m_handler);
060a7b5e 93 DELETE_AND_NULL(m_waitForResponse);
e9de9629
LOK
94}
95
004b8382
LOK
96bool CCECBusDevice::ReplaceHandler(bool bActivateSource /* = true */)
97{
880f082e
LOK
98 if (m_iLogicalAddress == CECDEVICE_BROADCAST)
99 return false;
100
004b8382
LOK
101 bool bInitHandler(false);
102 {
2b44051c 103 CLockObject lock(m_mutex);
004b8382
LOK
104 CLockObject handlerLock(m_handlerMutex);
105 if (m_iHandlerUseCount > 0)
106 return false;
107
108 MarkBusy();
109
110 if (m_vendor != m_handler->GetVendorId())
111 {
112 if (CCECCommandHandler::HasSpecificHandler(m_vendor))
113 {
114 LIB_CEC->AddLog(CEC_LOG_DEBUG, "replacing the command handler for device '%s' (%x)", GetLogicalAddressName(), GetLogicalAddress());
060a7b5e
LOK
115
116 int32_t iTransmitTimeout = m_handler->m_iTransmitTimeout;
117 int32_t iTransmitWait = m_handler->m_iTransmitWait;
118 int8_t iTransmitRetries = m_handler->m_iTransmitRetries;
119 int64_t iActiveSourcePending = m_handler->m_iActiveSourcePending;
120
c9d15485 121 DELETE_AND_NULL(m_handler);
004b8382
LOK
122
123 switch (m_vendor)
124 {
125 case CEC_VENDOR_SAMSUNG:
060a7b5e 126 m_handler = new CANCommandHandler(this, iTransmitTimeout, iTransmitWait, iTransmitRetries, iActiveSourcePending);
004b8382
LOK
127 break;
128 case CEC_VENDOR_LG:
060a7b5e 129 m_handler = new CSLCommandHandler(this, iTransmitTimeout, iTransmitWait, iTransmitRetries, iActiveSourcePending);
004b8382
LOK
130 break;
131 case CEC_VENDOR_PANASONIC:
060a7b5e 132 m_handler = new CVLCommandHandler(this, iTransmitTimeout, iTransmitWait, iTransmitRetries, iActiveSourcePending);
004b8382
LOK
133 break;
134 default:
060a7b5e 135 m_handler = new CCECCommandHandler(this, iTransmitTimeout, iTransmitWait, iTransmitRetries, iActiveSourcePending);
004b8382
LOK
136 break;
137 }
138
139 m_handler->SetVendorId(m_vendor);
140 bInitHandler = true;
141 }
142 }
143 }
144
145 if (bInitHandler)
146 {
42d28d15
LOK
147 CCECBusDevice *primary = GetProcessor()->GetPrimaryDevice();
148 if (primary->GetLogicalAddress() != CECDEVICE_UNREGISTERED)
149 {
150 m_handler->InitHandler();
004b8382 151
42d28d15
LOK
152 if (bActivateSource && IsHandledByLibCEC() && IsActiveSource())
153 m_handler->ActivateSource();
154 }
004b8382
LOK
155 }
156
157 MarkReady();
158
159 return true;
160}
161
91ef4e2d
LOK
162CCECCommandHandler *CCECBusDevice::GetHandler(void)
163{
164 ReplaceHandler(false);
165 MarkBusy();
166 return m_handler;
167}
168
93729720 169bool CCECBusDevice::HandleCommand(const cec_command &command)
f8513317 170{
7f919115
LOK
171 bool bHandled(false);
172
173 /* update "last active" */
8fa35473 174 {
f00ff009 175 CLockObject lock(m_mutex);
8fa35473 176 m_iLastActive = GetTimeMs();
f4b7b1dc 177
ba427965 178 /* don't call GetStatus() here, just read the value with the mutex locked */
d297cbd4 179 if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC && command.opcode_set == 1)
f4b7b1dc 180 m_deviceStatus = CEC_DEVICE_STATUS_PRESENT;
1344fd1a
LOK
181
182 MarkBusy();
8fa35473
LOK
183 }
184
7f919115
LOK
185 /* handle the command */
186 bHandled = m_handler->HandleCommand(command);
8fa35473 187
7f919115 188 /* change status to present */
004b8382 189 if (bHandled && GetLogicalAddress() != CECDEVICE_BROADCAST)
8d915412 190 {
f00ff009 191 CLockObject lock(m_mutex);
8fa35473 192 if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC)
8d915412 193 {
8fa35473 194 if (m_deviceStatus != CEC_DEVICE_STATUS_PRESENT)
004b8382 195 LIB_CEC->AddLog(CEC_LOG_DEBUG, "device %s (%x) status changed to present after command %s", GetLogicalAddressName(), (uint8_t)GetLogicalAddress(), ToString(command.opcode));
8fa35473 196 m_deviceStatus = CEC_DEVICE_STATUS_PRESENT;
8d915412 197 }
8d915412 198 }
7f919115 199
1344fd1a 200 MarkReady();
7f919115 201 return bHandled;
93729720
LOK
202}
203
004b8382 204const char* CCECBusDevice::GetLogicalAddressName(void) const
93729720 205{
004b8382
LOK
206 return ToString(m_iLogicalAddress);
207}
208
209bool CCECBusDevice::IsPresent(void)
210{
211 CLockObject lock(m_mutex);
212 return m_deviceStatus == CEC_DEVICE_STATUS_PRESENT;
213}
214
215bool CCECBusDevice::IsHandledByLibCEC(void)
216{
217 CLockObject lock(m_mutex);
218 return m_deviceStatus == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC;
219}
220
221void CCECBusDevice::SetUnsupportedFeature(cec_opcode opcode)
222{
223 // some commands should never be marked as unsupported
224 if (opcode == CEC_OPCODE_VENDOR_COMMAND ||
225 opcode == CEC_OPCODE_VENDOR_COMMAND_WITH_ID ||
226 opcode == CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN ||
227 opcode == CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP ||
228 opcode == CEC_OPCODE_ABORT ||
229 opcode == CEC_OPCODE_FEATURE_ABORT ||
230 opcode == CEC_OPCODE_NONE)
231 return;
c6d7f0e1 232
5e5637c6 233 {
004b8382
LOK
234 CLockObject lock(m_mutex);
235 if (m_unsupportedFeatures.find(opcode) == m_unsupportedFeatures.end())
8670c970 236 {
004b8382
LOK
237 LIB_CEC->AddLog(CEC_LOG_DEBUG, "marking opcode '%s' as unsupported feature for device '%s'", ToString(opcode), GetLogicalAddressName());
238 m_unsupportedFeatures.insert(opcode);
8670c970
LOK
239 }
240 }
f437e4be 241
004b8382
LOK
242 // signal threads that are waiting for a reponse
243 MarkBusy();
060a7b5e 244 SignalOpcode(cec_command::GetResponseOpcode(opcode));
004b8382
LOK
245 MarkReady();
246}
247
248bool CCECBusDevice::IsUnsupportedFeature(cec_opcode opcode)
249{
250 CLockObject lock(m_mutex);
251 bool bUnsupported = (m_unsupportedFeatures.find(opcode) != m_unsupportedFeatures.end());
252 if (bUnsupported)
253 LIB_CEC->AddLog(CEC_LOG_DEBUG, "'%s' is marked as unsupported feature for device '%s'", ToString(opcode), GetLogicalAddressName());
254 return bUnsupported;
255}
256
257bool CCECBusDevice::TransmitKeypress(const cec_logical_address initiator, cec_user_control_code key, bool bWait /* = true */)
258{
259 MarkBusy();
260 bool bReturn = m_handler->TransmitKeypress(initiator, m_iLogicalAddress, key, bWait);
1344fd1a
LOK
261 MarkReady();
262 return bReturn;
93729720
LOK
263}
264
004b8382 265bool CCECBusDevice::TransmitKeyRelease(const cec_logical_address initiator, bool bWait /* = true */)
93729720 266{
1344fd1a 267 MarkBusy();
004b8382 268 bool bReturn = m_handler->TransmitKeyRelease(initiator, m_iLogicalAddress, bWait);
1344fd1a
LOK
269 MarkReady();
270 return bReturn;
93729720
LOK
271}
272
004b8382 273cec_version CCECBusDevice::GetCecVersion(const cec_logical_address initiator, bool bUpdate /* = false */)
93729720 274{
6bbfc3f7 275 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
97fc4ffb
LOK
276 bool bRequestUpdate(false);
277 {
f00ff009 278 CLockObject lock(m_mutex);
ddb6ac5b 279 bRequestUpdate = bIsPresent &&
6bbfc3f7 280 (bUpdate || m_cecVersion == CEC_VERSION_UNKNOWN);
97fc4ffb
LOK
281 }
282
283 if (bRequestUpdate)
ebb6ddb3 284 {
004b8382
LOK
285 CheckVendorIdRequested(initiator);
286 RequestCecVersion(initiator);
ebb6ddb3 287 }
f294b22f 288
f00ff009 289 CLockObject lock(m_mutex);
f294b22f
LOK
290 return m_cecVersion;
291}
292
004b8382 293void CCECBusDevice::SetCecVersion(const cec_version newVersion)
5734016c 294{
004b8382
LOK
295 CLockObject lock(m_mutex);
296 if (m_cecVersion != newVersion)
297 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): CEC version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(newVersion));
298 m_cecVersion = newVersion;
5734016c
LOK
299}
300
004b8382 301bool CCECBusDevice::RequestCecVersion(const cec_logical_address initiator, bool bWaitForResponse /* = true */)
f294b22f
LOK
302{
303 bool bReturn(false);
b64db02e 304
004b8382 305 if (!IsHandledByLibCEC() &&
66c3ef5a 306 !IsUnsupportedFeature(CEC_OPCODE_GET_CEC_VERSION))
93729720 307 {
1344fd1a 308 MarkBusy();
ff07d530 309 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
004b8382 310 bReturn = m_handler->TransmitRequestCecVersion(initiator, m_iLogicalAddress, bWaitForResponse);
1344fd1a 311 MarkReady();
93729720 312 }
f294b22f 313 return bReturn;
93729720
LOK
314}
315
2b44051c 316bool CCECBusDevice::TransmitCECVersion(const cec_logical_address destination, bool bIsReply)
62f5527d 317{
004b8382
LOK
318 cec_version version;
319 {
320 CLockObject lock(m_mutex);
ff07d530 321 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) -> %s (%X): cec version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination, ToString(m_cecVersion));
004b8382
LOK
322 version = m_cecVersion;
323 }
324
325 MarkBusy();
2b44051c 326 bool bReturn = m_handler->TransmitCECVersion(m_iLogicalAddress, destination, version, bIsReply);
004b8382
LOK
327 MarkReady();
328 return bReturn;
62f5527d
LOK
329}
330
004b8382 331cec_menu_language &CCECBusDevice::GetMenuLanguage(const cec_logical_address initiator, bool bUpdate /* = false */)
93729720 332{
6bbfc3f7 333 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
97fc4ffb
LOK
334 bool bRequestUpdate(false);
335 {
f00ff009 336 CLockObject lock(m_mutex);
ddb6ac5b 337 bRequestUpdate = (bIsPresent &&
97fc4ffb
LOK
338 (bUpdate || !strcmp(m_menuLanguage.language, "???")));
339 }
340
341 if (bRequestUpdate)
ebb6ddb3 342 {
004b8382
LOK
343 CheckVendorIdRequested(initiator);
344 RequestMenuLanguage(initiator);
ebb6ddb3 345 }
5e5637c6 346
f00ff009 347 CLockObject lock(m_mutex);
f294b22f
LOK
348 return m_menuLanguage;
349}
350
004b8382
LOK
351void CCECBusDevice::SetMenuLanguage(const char *strLanguage)
352{
353 if (!strLanguage)
354 return;
355
356 CLockObject lock(m_mutex);
357 if (strcmp(strLanguage, m_menuLanguage.language))
358 {
359 memcpy(m_menuLanguage.language, strLanguage, 3);
60c28d82 360 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): menu language set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, m_menuLanguage.language);
004b8382
LOK
361 }
362}
363
364void CCECBusDevice::SetMenuLanguage(const cec_menu_language &language)
365{
366 if (language.device == m_iLogicalAddress)
367 SetMenuLanguage(language.language);
368}
369
370bool CCECBusDevice::RequestMenuLanguage(const cec_logical_address initiator, bool bWaitForResponse /* = true */)
f294b22f
LOK
371{
372 bool bReturn(false);
b64db02e 373
004b8382 374 if (!IsHandledByLibCEC() &&
4d738fe3 375 !IsUnsupportedFeature(CEC_OPCODE_GET_MENU_LANGUAGE))
93729720 376 {
1344fd1a 377 MarkBusy();
ff07d530 378 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
004b8382 379 bReturn = m_handler->TransmitRequestMenuLanguage(initiator, m_iLogicalAddress, bWaitForResponse);
1344fd1a 380 MarkReady();
93729720 381 }
f294b22f 382 return bReturn;
93729720
LOK
383}
384
2b44051c 385bool CCECBusDevice::TransmitSetMenuLanguage(const cec_logical_address destination, bool bIsReply)
3e61b350 386{
004b8382
LOK
387 bool bReturn(false);
388 cec_menu_language language;
389 {
390 CLockObject lock(m_mutex);
391 language = m_menuLanguage;
392 }
3e61b350 393
004b8382
LOK
394 char lang[3];
395 {
396 CLockObject lock(m_mutex);
397 lang[0] = language.language[0];
398 lang[1] = language.language[1];
399 lang[2] = language.language[2];
400 }
401
402 MarkBusy();
403 if (lang[0] == '?' && lang[1] == '?' && lang[2] == '?')
404 {
ff07d530 405 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) -> %s (%X): menu language feature abort", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination);
004b8382
LOK
406 m_processor->TransmitAbort(m_iLogicalAddress, destination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
407 bReturn = true;
408 }
409 else
410 {
ff07d530 411 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) -> broadcast (F): menu language '%s'", GetLogicalAddressName(), m_iLogicalAddress, lang);
2b44051c 412 bReturn = m_handler->TransmitSetMenuLanguage(m_iLogicalAddress, lang, bIsReply);
004b8382
LOK
413 }
414 MarkReady();
415 return bReturn;
f8513317
LOK
416}
417
2b44051c 418bool CCECBusDevice::TransmitOSDString(const cec_logical_address destination, cec_display_control duration, const char *strMessage, bool bIsReply)
e9de9629 419{
004b8382
LOK
420 bool bReturn(false);
421 if (!m_processor->GetDevice(destination)->IsUnsupportedFeature(CEC_OPCODE_SET_OSD_STRING))
422 {
ff07d530 423 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) -> %s (%X): display OSD message '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination, strMessage);
004b8382 424 MarkBusy();
2b44051c 425 bReturn = m_handler->TransmitOSDString(m_iLogicalAddress, destination, duration, strMessage, bIsReply);
004b8382
LOK
426 MarkReady();
427 }
428 return bReturn;
e9de9629
LOK
429}
430
c0152c09
LOK
431CStdString CCECBusDevice::GetCurrentOSDName(void)
432{
433 CLockObject lock(m_mutex);
434 return m_strDeviceName;
435}
436
004b8382 437CStdString CCECBusDevice::GetOSDName(const cec_logical_address initiator, bool bUpdate /* = false */)
ed21be2a 438{
6bbfc3f7 439 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
97fc4ffb
LOK
440 bool bRequestUpdate(false);
441 {
f00ff009 442 CLockObject lock(m_mutex);
ddb6ac5b 443 bRequestUpdate = bIsPresent &&
97fc4ffb 444 (bUpdate || m_strDeviceName.Equals(ToString(m_iLogicalAddress))) &&
ddb6ac5b 445 m_type != CEC_DEVICE_TYPE_TV;
97fc4ffb
LOK
446 }
447
448 if (bRequestUpdate)
ebb6ddb3 449 {
004b8382
LOK
450 CheckVendorIdRequested(initiator);
451 RequestOSDName(initiator);
ebb6ddb3 452 }
5e5637c6 453
f00ff009 454 CLockObject lock(m_mutex);
ed21be2a
LOK
455 return m_strDeviceName;
456}
457
004b8382
LOK
458void CCECBusDevice::SetOSDName(CStdString strName)
459{
460 CLockObject lock(m_mutex);
461 if (m_strDeviceName != strName)
462 {
60c28d82 463 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): osd name set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, strName.c_str());
004b8382
LOK
464 m_strDeviceName = strName;
465 }
466}
467
468bool CCECBusDevice::RequestOSDName(const cec_logical_address initiator, bool bWaitForResponse /* = true */)
ed21be2a
LOK
469{
470 bool bReturn(false);
b64db02e 471
004b8382 472 if (!IsHandledByLibCEC() &&
4d738fe3 473 !IsUnsupportedFeature(CEC_OPCODE_GIVE_OSD_NAME))
ed21be2a 474 {
1344fd1a 475 MarkBusy();
ff07d530 476 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< requesting OSD name of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
004b8382 477 bReturn = m_handler->TransmitRequestOSDName(initiator, m_iLogicalAddress, bWaitForResponse);
1344fd1a 478 MarkReady();
ed21be2a
LOK
479 }
480 return bReturn;
481}
482
2b44051c 483bool CCECBusDevice::TransmitOSDName(const cec_logical_address destination, bool bIsReply)
004b8382
LOK
484{
485 CStdString strDeviceName;
486 {
487 CLockObject lock(m_mutex);
ff07d530 488 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination, m_strDeviceName.c_str());
004b8382
LOK
489 strDeviceName = m_strDeviceName;
490 }
491
492 MarkBusy();
2b44051c 493 bool bReturn = m_handler->TransmitOSDName(m_iLogicalAddress, destination, strDeviceName, bIsReply);
004b8382
LOK
494 MarkReady();
495 return bReturn;
496}
497
498bool CCECBusDevice::HasValidPhysicalAddress(void)
499{
500 CLockObject lock(m_mutex);
501 return CLibCEC::IsValidPhysicalAddress(m_iPhysicalAddress);
502}
503
504uint16_t CCECBusDevice::GetCurrentPhysicalAddress(void)
505{
506 CLockObject lock(m_mutex);
507 return m_iPhysicalAddress;
508}
509
510uint16_t CCECBusDevice::GetPhysicalAddress(const cec_logical_address initiator, bool bSuppressUpdate /* = false */)
16b1e052 511{
0680dab3 512 if (!bSuppressUpdate)
16b1e052 513 {
0680dab3
LOK
514 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
515 bool bRequestUpdate(false);
516 {
517 CLockObject lock(m_mutex);
b32ffd87 518 bRequestUpdate = bIsPresent && m_iPhysicalAddress == CEC_INVALID_PHYSICAL_ADDRESS;
0680dab3 519 }
16b1e052 520
0680dab3
LOK
521 if (bRequestUpdate)
522 {
004b8382
LOK
523 CheckVendorIdRequested(initiator);
524 if (!RequestPhysicalAddress(initiator))
525 LIB_CEC->AddLog(CEC_LOG_ERROR, "failed to request the physical address");
0680dab3 526 }
ebb6ddb3 527 }
97fc4ffb 528
f00ff009 529 CLockObject lock(m_mutex);
16b1e052
LOK
530 return m_iPhysicalAddress;
531}
532
004b8382
LOK
533bool CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress)
534{
535 CLockObject lock(m_mutex);
536 if (iNewAddress > 0 && m_iPhysicalAddress != iNewAddress)
537 {
60c28d82 538 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): physical address changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress, iNewAddress);
004b8382
LOK
539 m_iPhysicalAddress = iNewAddress;
540 }
541 return true;
542}
543
544bool CCECBusDevice::RequestPhysicalAddress(const cec_logical_address initiator, bool bWaitForResponse /* = true */)
16b1e052
LOK
545{
546 bool bReturn(false);
b64db02e 547
004b8382 548 if (!IsHandledByLibCEC())
16b1e052 549 {
1344fd1a 550 MarkBusy();
ff07d530 551 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
004b8382 552 bReturn = m_handler->TransmitRequestPhysicalAddress(initiator, m_iLogicalAddress, bWaitForResponse);
1344fd1a 553 MarkReady();
16b1e052
LOK
554 }
555 return bReturn;
556}
557
2b44051c 558bool CCECBusDevice::TransmitPhysicalAddress(bool bIsReply)
004b8382
LOK
559{
560 uint16_t iPhysicalAddress;
561 cec_device_type type;
562 {
563 CLockObject lock(m_mutex);
564 if (m_iPhysicalAddress == CEC_INVALID_PHYSICAL_ADDRESS)
565 return false;
566
ff07d530 567 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress);
004b8382
LOK
568 iPhysicalAddress = m_iPhysicalAddress;
569 type = m_type;
570 }
571
572 MarkBusy();
2b44051c 573 bool bReturn = m_handler->TransmitPhysicalAddress(m_iLogicalAddress, iPhysicalAddress, type, bIsReply);
004b8382
LOK
574 MarkReady();
575 return bReturn;
576}
577
578cec_power_status CCECBusDevice::GetCurrentPowerStatus(void)
579{
580 CLockObject lock(m_mutex);
581 return m_powerStatus;
582}
583
584cec_power_status CCECBusDevice::GetPowerStatus(const cec_logical_address initiator, bool bUpdate /* = false */)
e9de9629 585{
6bbfc3f7 586 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
97fc4ffb
LOK
587 bool bRequestUpdate(false);
588 {
f00ff009 589 CLockObject lock(m_mutex);
ddb6ac5b 590 bRequestUpdate = (bIsPresent &&
c8f0eef0 591 (bUpdate || m_powerStatus == CEC_POWER_STATUS_UNKNOWN ||
2efa39b7
LOK
592 m_powerStatus == CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON ||
593 m_powerStatus == CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY ||
594 GetTimeMs() - m_iLastPowerStateUpdate >= CEC_POWER_STATE_REFRESH_TIME));
97fc4ffb
LOK
595 }
596
597 if (bRequestUpdate)
ebb6ddb3 598 {
004b8382
LOK
599 CheckVendorIdRequested(initiator);
600 RequestPowerStatus(initiator);
ebb6ddb3 601 }
5e5637c6 602
f00ff009 603 CLockObject lock(m_mutex);
f294b22f
LOK
604 return m_powerStatus;
605}
606
004b8382
LOK
607void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus)
608{
609 CLockObject lock(m_mutex);
610 if (m_powerStatus != powerStatus)
611 {
612 m_iLastPowerStateUpdate = GetTimeMs();
60c28d82 613 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): power status changed from '%s' to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_powerStatus), ToString(powerStatus));
004b8382
LOK
614 m_powerStatus = powerStatus;
615 }
616}
617
618bool CCECBusDevice::RequestPowerStatus(const cec_logical_address initiator, bool bWaitForResponse /* = true */)
f294b22f
LOK
619{
620 bool bReturn(false);
b64db02e 621
004b8382 622 if (!IsHandledByLibCEC() &&
4d738fe3 623 !IsUnsupportedFeature(CEC_OPCODE_GIVE_DEVICE_POWER_STATUS))
93729720 624 {
1344fd1a 625 MarkBusy();
ff07d530 626 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
004b8382 627 bReturn = m_handler->TransmitRequestPowerStatus(initiator, m_iLogicalAddress, bWaitForResponse);
1344fd1a 628 MarkReady();
93729720 629 }
f294b22f
LOK
630 return bReturn;
631}
93729720 632
2b44051c 633bool CCECBusDevice::TransmitPowerState(const cec_logical_address destination, bool bIsReply)
f294b22f 634{
004b8382 635 cec_power_status state;
97fc4ffb 636 {
f00ff009 637 CLockObject lock(m_mutex);
ff07d530 638 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination, ToString(m_powerStatus));
004b8382 639 state = m_powerStatus;
97fc4ffb
LOK
640 }
641
004b8382 642 MarkBusy();
2b44051c 643 bool bReturn = m_handler->TransmitPowerState(m_iLogicalAddress, destination, state, bIsReply);
004b8382
LOK
644 MarkReady();
645 return bReturn;
646}
647
648cec_vendor_id CCECBusDevice::GetCurrentVendorId(void)
649{
650 CLockObject lock(m_mutex);
f294b22f 651 return m_vendor;
e9de9629
LOK
652}
653
004b8382
LOK
654cec_vendor_id CCECBusDevice::GetVendorId(const cec_logical_address initiator, bool bUpdate /* = false */)
655{
656 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
657 bool bRequestUpdate(false);
658 {
659 CLockObject lock(m_mutex);
660 bRequestUpdate = (bIsPresent &&
661 (bUpdate || m_vendor == CEC_VENDOR_UNKNOWN));
662 }
663
664 if (bRequestUpdate)
665 RequestVendorId(initiator);
666
667 CLockObject lock(m_mutex);
668 return m_vendor;
669}
670
671const char *CCECBusDevice::GetVendorName(const cec_logical_address initiator, bool bUpdate /* = false */)
672{
673 return ToString(GetVendorId(initiator, bUpdate));
674}
675
676bool CCECBusDevice::SetVendorId(uint64_t iVendorId)
677{
678 bool bVendorChanged(false);
679
680 {
681 CLockObject lock(m_mutex);
682 bVendorChanged = (m_vendor != (cec_vendor_id)iVendorId);
683 m_vendor = (cec_vendor_id)iVendorId;
684 }
685
686 if (bVendorChanged)
687 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_vendor), m_vendor);
688
689 return bVendorChanged;
690}
691
692bool CCECBusDevice::RequestVendorId(const cec_logical_address initiator, bool bWaitForResponse /* = true */)
a3269a0a 693{
f294b22f 694 bool bReturn(false);
b64db02e 695
004b8382 696 if (!IsHandledByLibCEC() && initiator != CECDEVICE_UNKNOWN)
a3269a0a 697 {
1344fd1a 698 MarkBusy();
ff07d530 699 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
004b8382 700 bReturn = m_handler->TransmitRequestVendorId(initiator, m_iLogicalAddress, bWaitForResponse);
1344fd1a 701 MarkReady();
3e61b350 702
a75e3a5a
LOK
703 if (bWaitForResponse)
704 ReplaceHandler(true);
a3269a0a 705 }
f294b22f 706 return bReturn;
93729720
LOK
707}
708
2b44051c 709bool CCECBusDevice::TransmitVendorID(const cec_logical_address destination, bool bSendAbort, bool bIsReply)
93729720 710{
004b8382
LOK
711 bool bReturn(false);
712 uint64_t iVendorId;
713 {
714 CLockObject lock(m_mutex);
715 iVendorId = (uint64_t)m_vendor;
716 }
a3269a0a 717
004b8382
LOK
718 MarkBusy();
719 if (iVendorId == CEC_VENDOR_UNKNOWN)
9fd73dd4 720 {
004b8382 721 if (bSendAbort)
c6d7f0e1 722 {
ff07d530 723 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination);
004b8382
LOK
724 m_processor->TransmitAbort(m_iLogicalAddress, destination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
725 bReturn = true;
c6d7f0e1 726 }
9fd73dd4 727 }
004b8382
LOK
728 else
729 {
ff07d530 730 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) -> %s (%X): vendor id %s (%x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination, ToString((cec_vendor_id)iVendorId), iVendorId);
2b44051c 731 bReturn = m_handler->TransmitVendorID(m_iLogicalAddress, iVendorId, bIsReply);
004b8382
LOK
732 }
733 MarkReady();
734 return bReturn;
9fd73dd4
LOK
735}
736
a75e3a5a 737cec_bus_device_status CCECBusDevice::GetStatus(bool bForcePoll /* = false */, bool bSuppressPoll /* = false */)
f8ae3295 738{
a2facc35
LOK
739 if (m_iLogicalAddress == CECDEVICE_BROADCAST)
740 return CEC_DEVICE_STATUS_NOT_PRESENT;
741
ba427965
LOK
742 cec_bus_device_status status(CEC_DEVICE_STATUS_UNKNOWN);
743 bool bNeedsPoll(false);
744
745 {
746 CLockObject lock(m_mutex);
747 status = m_deviceStatus;
a75e3a5a
LOK
748 bNeedsPoll = !bSuppressPoll &&
749 (bForcePoll || m_deviceStatus == CEC_DEVICE_STATUS_UNKNOWN);
ba427965
LOK
750 }
751
752 if (bNeedsPoll)
f8ae3295 753 {
9fd73dd4 754 bool bPollAcked(false);
7b62b76e 755 if (bNeedsPoll && NeedsPoll())
9fd73dd4 756 bPollAcked = m_processor->PollDevice(m_iLogicalAddress);
95a73fa7 757
ba427965
LOK
758 status = bPollAcked ? CEC_DEVICE_STATUS_PRESENT : CEC_DEVICE_STATUS_NOT_PRESENT;
759 SetDeviceStatus(status);
f8ae3295
LOK
760 }
761
ba427965 762 return status;
f8ae3295
LOK
763}
764
2b44051c 765void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus, cec_version libCECSpecVersion /* = CEC_VERSION_1_4 */)
93fff5c1 766{
93fff5c1 767 {
ee17ad58
LOK
768 CLockObject lock(m_mutex);
769 switch (newStatus)
770 {
ee17ad58
LOK
771 case CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC:
772 if (m_deviceStatus != newStatus)
f5aa7e4c 773 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): device status changed into 'handled by libCEC'", GetLogicalAddressName(), m_iLogicalAddress);
a20d808d
LOK
774 SetPowerStatus (CEC_POWER_STATUS_ON);
775 SetVendorId (CEC_VENDOR_UNKNOWN);
776 SetMenuState (CEC_MENU_STATE_ACTIVATED);
2b44051c 777 SetCecVersion (libCECSpecVersion);
b32ffd87 778 SetStreamPath (CEC_INVALID_PHYSICAL_ADDRESS);
004b8382 779 MarkAsInactiveSource();
a20d808d
LOK
780 m_iLastActive = 0;
781 m_deviceStatus = newStatus;
ee17ad58
LOK
782 break;
783 case CEC_DEVICE_STATUS_PRESENT:
784 if (m_deviceStatus != newStatus)
f5aa7e4c 785 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): device status changed into 'present'", GetLogicalAddressName(), m_iLogicalAddress);
ee17ad58
LOK
786 m_deviceStatus = newStatus;
787 break;
788 case CEC_DEVICE_STATUS_NOT_PRESENT:
789 if (m_deviceStatus != newStatus)
a20d808d 790 {
f5aa7e4c 791 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): device status changed into 'not present'", GetLogicalAddressName(), m_iLogicalAddress);
a20d808d
LOK
792 ResetDeviceStatus();
793 m_deviceStatus = newStatus;
794 }
ee17ad58 795 break;
f5aa7e4c
LOK
796 default:
797 ResetDeviceStatus();
798 break;
ee17ad58 799 }
93fff5c1
LOK
800 }
801}
802
004b8382 803void CCECBusDevice::ResetDeviceStatus(void)
93729720 804{
f00ff009 805 CLockObject lock(m_mutex);
004b8382
LOK
806 SetPowerStatus (CEC_POWER_STATUS_UNKNOWN);
807 SetVendorId (CEC_VENDOR_UNKNOWN);
808 SetMenuState (CEC_MENU_STATE_ACTIVATED);
809 SetCecVersion (CEC_VERSION_UNKNOWN);
810 SetStreamPath (CEC_INVALID_PHYSICAL_ADDRESS);
811 SetOSDName (ToString(m_iLogicalAddress));
812 MarkAsInactiveSource();
f5aa7e4c 813
004b8382
LOK
814 m_iLastActive = 0;
815 m_bVendorIdRequested = false;
816 m_unsupportedFeatures.clear();
060a7b5e 817 m_waitForResponse->Clear();
f5aa7e4c
LOK
818
819 if (m_deviceStatus != CEC_DEVICE_STATUS_UNKNOWN)
820 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): device status changed into 'unknown'", GetLogicalAddressName(), m_iLogicalAddress);
821 m_deviceStatus = CEC_DEVICE_STATUS_UNKNOWN;
93729720
LOK
822}
823
2b44051c 824bool CCECBusDevice::TransmitPoll(const cec_logical_address dest, bool bIsReply)
9dc04b07 825{
004b8382
LOK
826 bool bReturn(false);
827 cec_logical_address destination(dest);
828 if (destination == CECDEVICE_UNKNOWN)
829 destination = m_iLogicalAddress;
96274140 830
004b8382
LOK
831 CCECBusDevice *destDevice = m_processor->GetDevice(destination);
832 if (destDevice->m_deviceStatus == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC)
833 return bReturn;
834
835 MarkBusy();
ff07d530 836 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest);
2b44051c 837 bReturn = m_handler->TransmitPoll(m_iLogicalAddress, destination, bIsReply);
004b8382
LOK
838 LIB_CEC->AddLog(CEC_LOG_DEBUG, bReturn ? ">> POLL sent" : ">> POLL not sent");
839
840 CLockObject lock(m_mutex);
841 if (bReturn)
5f2068fe 842 {
004b8382
LOK
843 m_iLastActive = GetTimeMs();
844 destDevice->m_deviceStatus = CEC_DEVICE_STATUS_PRESENT;
5f2068fe
LOK
845 }
846 else
004b8382
LOK
847 destDevice->m_deviceStatus = CEC_DEVICE_STATUS_NOT_PRESENT;
848
849 MarkReady();
850 return bReturn;
851}
852
853void CCECBusDevice::HandlePoll(const cec_logical_address destination)
854{
855 if (destination >= 0 && destination < CECDEVICE_BROADCAST)
5f2068fe 856 {
004b8382 857 CCECBusDevice *device = m_processor->GetDevice(destination);
0cb55c43 858 if (device)
004b8382 859 device->HandlePollFrom(m_iLogicalAddress);
9dc04b07
LOK
860 }
861}
862
004b8382 863void CCECBusDevice::HandlePollFrom(const cec_logical_address initiator)
e55f3f70 864{
004b8382
LOK
865 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< POLL: %s (%x) -> %s (%x)", ToString(initiator), initiator, ToString(m_iLogicalAddress), m_iLogicalAddress);
866 m_bAwaitingReceiveFailed = true;
e55f3f70
LOK
867}
868
004b8382 869bool CCECBusDevice::HandleReceiveFailed(void)
1344fd1a 870{
004b8382
LOK
871 bool bReturn = m_bAwaitingReceiveFailed;
872 m_bAwaitingReceiveFailed = false;
873 return bReturn;
1344fd1a
LOK
874}
875
004b8382 876cec_menu_state CCECBusDevice::GetMenuState(const cec_logical_address UNUSED(initiator))
1344fd1a 877{
004b8382
LOK
878 CLockObject lock(m_mutex);
879 return m_menuState;
1344fd1a
LOK
880}
881
004b8382 882void CCECBusDevice::SetMenuState(const cec_menu_state state)
e9de9629 883{
004b8382
LOK
884 CLockObject lock(m_mutex);
885 if (m_menuState != state)
94e9a2af 886 {
60c28d82 887 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): menu state set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_menuState));
004b8382
LOK
888 m_menuState = state;
889 }
890}
d211708b 891
2b44051c 892bool CCECBusDevice::TransmitMenuState(const cec_logical_address dest, bool bIsReply)
004b8382
LOK
893{
894 cec_menu_state menuState;
895 {
896 CLockObject lock(m_mutex);
ff07d530 897 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) -> %s (%X): menu state '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_menuState));
004b8382
LOK
898 menuState = m_menuState;
899 }
104125dc 900
004b8382 901 MarkBusy();
2b44051c 902 bool bReturn = m_handler->TransmitMenuState(m_iLogicalAddress, dest, menuState, bIsReply);
004b8382
LOK
903 MarkReady();
904 return bReturn;
905}
94e9a2af 906
060a7b5e 907bool CCECBusDevice::ActivateSource(uint64_t iDelay /* = 0 */)
004b8382
LOK
908{
909 MarkAsActiveSource();
004b8382 910 MarkBusy();
060a7b5e
LOK
911 bool bReturn(true);
912 if (iDelay == 0)
913 {
914 LIB_CEC->AddLog(CEC_LOG_DEBUG, "sending active source message for '%s'", ToString(m_iLogicalAddress));
915 bReturn = m_handler->ActivateSource();
916 }
917 else
918 {
919 LIB_CEC->AddLog(CEC_LOG_DEBUG, "scheduling active source message for '%s'", ToString(m_iLogicalAddress));
920 m_handler->ScheduleActivateSource(iDelay);
921 }
004b8382
LOK
922 MarkReady();
923 return bReturn;
924}
94e9a2af 925
004b8382
LOK
926bool CCECBusDevice::RequestActiveSource(bool bWaitForResponse /* = true */)
927{
928 bool bReturn(false);
e9de9629 929
004b8382 930 if (IsHandledByLibCEC())
104125dc 931 {
004b8382 932 MarkBusy();
ff07d530 933 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< requesting active source");
104125dc 934
004b8382
LOK
935 bReturn = m_handler->TransmitRequestActiveSource(m_iLogicalAddress, bWaitForResponse);
936 MarkReady();
104125dc 937 }
004b8382
LOK
938 return bReturn;
939}
104125dc 940
004b8382
LOK
941void CCECBusDevice::MarkAsActiveSource(void)
942{
29d5198c
LOK
943 bool bWasActivated(false);
944
945 // set the power status to powered on
946 SetPowerStatus(CEC_POWER_STATUS_ON);
947
948 // mark this device as active source
949 {
950 CLockObject lock(m_mutex);
951 if (!m_bActiveSource)
952 {
953 LIB_CEC->AddLog(CEC_LOG_DEBUG, "making %s (%x) the active source", GetLogicalAddressName(), m_iLogicalAddress);
954 bWasActivated = true;
955 }
956 else
957 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%x) was already marked as active source", GetLogicalAddressName(), m_iLogicalAddress);
104125dc 958
29d5198c
LOK
959 m_bActiveSource = true;
960 }
961
962 // mark other devices as inactive sources
004b8382
LOK
963 CECDEVICEVEC devices;
964 m_processor->GetDevices()->Get(devices);
965 for (CECDEVICEVEC::iterator it = devices.begin(); it != devices.end(); it++)
966 if ((*it)->GetLogicalAddress() != m_iLogicalAddress)
967 (*it)->MarkAsInactiveSource();
968
29d5198c
LOK
969 if (bWasActivated)
970 {
971 CCECClient *client = GetClient();
972 if (client)
973 client->SourceActivated(m_iLogicalAddress);
974 }
b64db02e
LOK
975}
976
004b8382 977void CCECBusDevice::MarkAsInactiveSource(void)
b64db02e 978{
29d5198c 979 bool bWasDeactivated(false);
b64db02e 980 {
f00ff009 981 CLockObject lock(m_mutex);
004b8382 982 if (m_bActiveSource)
29d5198c 983 {
004b8382 984 LIB_CEC->AddLog(CEC_LOG_DEBUG, "marking %s (%X) as inactive source", GetLogicalAddressName(), m_iLogicalAddress);
29d5198c
LOK
985 bWasDeactivated = true;
986 }
004b8382 987 m_bActiveSource = false;
b64db02e 988 }
29d5198c
LOK
989
990 if (bWasDeactivated)
991 {
992 CCECClient *client = GetClient();
993 if (client)
994 client->SourceDeactivated(m_iLogicalAddress);
995 }
e9de9629
LOK
996}
997
2b44051c 998bool CCECBusDevice::TransmitActiveSource(bool bIsReply)
0f23c85c 999{
8fa35473 1000 bool bSendActiveSource(false);
3a48aeae 1001 uint16_t iPhysicalAddress(CEC_INVALID_PHYSICAL_ADDRESS);
0f23c85c 1002
8747dd4f 1003 {
f00ff009 1004 CLockObject lock(m_mutex);
3a48aeae
LOK
1005 if (!HasValidPhysicalAddress())
1006 {
b1d821c6 1007 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X) has an invalid physical address (%04x), not sending active source commands", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress);
3a48aeae
LOK
1008 return false;
1009 }
1010
1011 iPhysicalAddress = m_iPhysicalAddress;
1012
49c8f2e4 1013 if (m_powerStatus != CEC_POWER_STATUS_ON && m_powerStatus != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON)
004b8382 1014 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress);
8fa35473
LOK
1015 else if (m_bActiveSource)
1016 {
004b8382 1017 LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): active source (%4x)", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress);
8fa35473
LOK
1018 bSendActiveSource = true;
1019 }
1020 else
004b8382 1021 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress);
8747dd4f
LOK
1022 }
1023
dbad810a 1024 bool bActiveSourceSent(false);
b64db02e
LOK
1025 if (bSendActiveSource)
1026 {
1344fd1a 1027 MarkBusy();
2b44051c 1028 bActiveSourceSent = m_handler->TransmitActiveSource(m_iLogicalAddress, iPhysicalAddress, bIsReply);
1344fd1a 1029 MarkReady();
b64db02e
LOK
1030 }
1031
dbad810a 1032 return bActiveSourceSent;
0f23c85c
LOK
1033}
1034
49c8f2e4
LOK
1035bool CCECBusDevice::TransmitImageViewOn(void)
1036{
49c8f2e4 1037 {
9a2f12df
LOK
1038 CLockObject lock(m_mutex);
1039 if (m_powerStatus != CEC_POWER_STATUS_ON && m_powerStatus != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON)
1040 {
004b8382 1041 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress);
9a2f12df
LOK
1042 return false;
1043 }
49c8f2e4 1044 }
9a2f12df 1045
dbad810a 1046 bool bImageViewOnSent(false);
9a2f12df 1047 MarkBusy();
dbad810a 1048 bImageViewOnSent = m_handler->TransmitImageViewOn(m_iLogicalAddress, CECDEVICE_TV);
9a2f12df 1049 MarkReady();
dbad810a 1050 return bImageViewOnSent;
49c8f2e4
LOK
1051}
1052
ab27363d 1053bool CCECBusDevice::TransmitInactiveSource(void)
93729720 1054{
8fa35473
LOK
1055 uint16_t iPhysicalAddress;
1056 {
f00ff009 1057 CLockObject lock(m_mutex);
004b8382 1058 LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): inactive source", GetLogicalAddressName(), m_iLogicalAddress);
8fa35473
LOK
1059 iPhysicalAddress = m_iPhysicalAddress;
1060 }
93729720 1061
1344fd1a
LOK
1062 MarkBusy();
1063 bool bReturn = m_handler->TransmitInactiveSource(m_iLogicalAddress, iPhysicalAddress);
1064 MarkReady();
1065 return bReturn;
93729720
LOK
1066}
1067
004b8382 1068bool CCECBusDevice::TransmitPendingActiveSourceCommands(void)
0f23c85c 1069{
1344fd1a 1070 MarkBusy();
aa4c0d34 1071 bool bReturn = m_handler->ActivateSource(true);
1344fd1a
LOK
1072 MarkReady();
1073 return bReturn;
0f23c85c
LOK
1074}
1075
004b8382 1076void CCECBusDevice::SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress /* = CEC_INVALID_PHYSICAL_ADDRESS */)
0f23c85c 1077{
004b8382
LOK
1078 CLockObject lock(m_mutex);
1079 if (iNewAddress != m_iStreamPath)
8fa35473 1080 {
60c28d82 1081 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, iOldAddress == 0 ? m_iStreamPath : iOldAddress, iNewAddress);
004b8382 1082 m_iStreamPath = iNewAddress;
8fa35473 1083 }
0f23c85c 1084
99aeafb9
LOK
1085 if (!LIB_CEC->IsValidPhysicalAddress(iNewAddress))
1086 return;
1087
004b8382
LOK
1088 CCECBusDevice *device = m_processor->GetDeviceByPhysicalAddress(iNewAddress);
1089 if (device)
4d738fe3 1090 {
004b8382
LOK
1091 // if a device is found with the new physical address, mark it as active, which will automatically mark all other devices as inactive
1092 device->MarkAsActiveSource();
e681f8a5
LOK
1093
1094 // respond with an active source message if this device is handled by libCEC
1095 if (device->IsHandledByLibCEC())
1096 device->TransmitActiveSource(true);
4d738fe3 1097 }
004b8382 1098 else
8fa35473 1099 {
004b8382
LOK
1100 // try to find the device with the old address, and mark it as inactive when found
1101 device = m_processor->GetDeviceByPhysicalAddress(iOldAddress);
1102 if (device)
1103 device->MarkAsInactiveSource();
8fa35473 1104 }
0f23c85c
LOK
1105}
1106
004b8382 1107bool CCECBusDevice::PowerOn(const cec_logical_address initiator)
fbdea54c
MK
1108{
1109 bool bReturn(false);
004b8382 1110 GetVendorId(initiator); // ensure that we got the vendor id, because the implementations vary per vendor
fbdea54c
MK
1111
1112 MarkBusy();
f81fdbfa
LOK
1113 cec_power_status currentStatus;
1114 if (m_iLogicalAddress == CECDEVICE_TV ||
1115 ((currentStatus = GetPowerStatus(initiator, false)) != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON &&
1116 currentStatus != CEC_POWER_STATUS_ON))
fbdea54c 1117 {
004b8382 1118 LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
f81fdbfa 1119 bReturn = m_handler->PowerOn(initiator, m_iLogicalAddress);
fbdea54c
MK
1120 }
1121 else
1122 {
ff07d530 1123 LIB_CEC->AddLog(CEC_LOG_DEBUG, "'%s' (%X) is already '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(currentStatus));
fbdea54c 1124 }
004b8382 1125
fbdea54c
MK
1126 MarkReady();
1127 return bReturn;
1128}
1129
004b8382 1130bool CCECBusDevice::Standby(const cec_logical_address initiator)
57f45e6c 1131{
004b8382 1132 GetVendorId(initiator); // ensure that we got the vendor id, because the implementations vary per vendor
b750a5c3 1133
ff07d530 1134 LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress);
1344fd1a 1135 MarkBusy();
004b8382 1136 bool bReturn = m_handler->TransmitStandby(initiator, m_iLogicalAddress);
1344fd1a 1137 MarkReady();
57f45e6c
LOK
1138 return bReturn;
1139}
93729720 1140
004b8382 1141bool CCECBusDevice::NeedsPoll(void)
93729720 1142{
004b8382
LOK
1143 bool bSendPoll(false);
1144 cec_logical_address pollAddress(CECDEVICE_UNKNOWN);
1145 switch (m_iLogicalAddress)
8fa35473 1146 {
004b8382
LOK
1147 case CECDEVICE_PLAYBACKDEVICE3:
1148 pollAddress = CECDEVICE_PLAYBACKDEVICE2;
1149 break;
1150 case CECDEVICE_PLAYBACKDEVICE2:
1151 pollAddress = CECDEVICE_PLAYBACKDEVICE1;
1152 break;
1153 case CECDEVICE_RECORDINGDEVICE3:
1154 pollAddress = CECDEVICE_RECORDINGDEVICE2;
1155 break;
1156 case CECDEVICE_RECORDINGDEVICE2:
1157 pollAddress = CECDEVICE_RECORDINGDEVICE1;
1158 break;
1159 case CECDEVICE_TUNER4:
1160 pollAddress = CECDEVICE_TUNER3;
1161 break;
1162 case CECDEVICE_TUNER3:
1163 pollAddress = CECDEVICE_TUNER2;
1164 break;
1165 case CECDEVICE_TUNER2:
1166 pollAddress = CECDEVICE_TUNER1;
1167 break;
1168 case CECDEVICE_AUDIOSYSTEM:
1169 case CECDEVICE_PLAYBACKDEVICE1:
1170 case CECDEVICE_RECORDINGDEVICE1:
1171 case CECDEVICE_TUNER1:
1172 case CECDEVICE_TV:
1173 bSendPoll = true;
1174 break;
1175 default:
1176 break;
8fa35473 1177 }
93729720 1178
004b8382
LOK
1179 if (!bSendPoll && pollAddress != CECDEVICE_UNKNOWN)
1180 {
1181 CCECBusDevice *device = m_processor->GetDevice(pollAddress);
1182 if (device)
1183 {
1184 cec_bus_device_status status = device->GetStatus();
1185 bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
1186 }
1187 else
1188 {
1189 bSendPoll = true;
1190 }
1191 }
1192
1193 return bSendPoll;
93729720
LOK
1194}
1195
004b8382 1196void CCECBusDevice::CheckVendorIdRequested(const cec_logical_address initiator)
93729720 1197{
004b8382 1198 bool bRequestVendorId(false);
8fa35473 1199 {
f00ff009 1200 CLockObject lock(m_mutex);
004b8382
LOK
1201 bRequestVendorId = !m_bVendorIdRequested;
1202 m_bVendorIdRequested = true;
8fa35473
LOK
1203 }
1204
004b8382 1205 if (bRequestVendorId)
c4098482 1206 {
004b8382
LOK
1207 ReplaceHandler(false);
1208 GetVendorId(initiator);
c4098482 1209 }
93729720 1210}
004b8382 1211//@}
a33794d8 1212
004b8382 1213CCECAudioSystem *CCECBusDevice::AsAudioSystem(void)
a33794d8 1214{
004b8382 1215 return AsAudioSystem(this);
a33794d8
LOK
1216}
1217
004b8382 1218CCECPlaybackDevice *CCECBusDevice::AsPlaybackDevice(void)
a33794d8 1219{
004b8382 1220 return AsPlaybackDevice(this);
a33794d8 1221}
4d738fe3 1222
004b8382 1223CCECRecordingDevice *CCECBusDevice::AsRecordingDevice(void)
4d738fe3 1224{
004b8382 1225 return AsRecordingDevice(this);
4d738fe3
LOK
1226}
1227
004b8382 1228CCECTuner *CCECBusDevice::AsTuner(void)
4d738fe3 1229{
004b8382
LOK
1230 return AsTuner(this);
1231}
ad7e0696 1232
004b8382
LOK
1233CCECTV *CCECBusDevice::AsTV(void)
1234{
1235 return AsTV(this);
1236}
b499cf16 1237
004b8382
LOK
1238CCECAudioSystem *CCECBusDevice::AsAudioSystem(CCECBusDevice *device)
1239{
1240 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
1241 return static_cast<CCECAudioSystem *>(device);
1242 return NULL;
4d738fe3 1243}
b64db02e 1244
004b8382 1245CCECPlaybackDevice *CCECBusDevice::AsPlaybackDevice(CCECBusDevice *device)
b64db02e 1246{
004b8382
LOK
1247 if (device &&
1248 (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE ||
1249 device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
1250 return static_cast<CCECPlaybackDevice *>(device);
1251 return NULL;
b64db02e
LOK
1252}
1253
004b8382 1254CCECRecordingDevice *CCECBusDevice::AsRecordingDevice(CCECBusDevice *device)
a75e3a5a 1255{
004b8382
LOK
1256 if (device && device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE)
1257 return static_cast<CCECRecordingDevice *>(device);
1258 return NULL;
a75e3a5a
LOK
1259}
1260
004b8382 1261CCECTuner *CCECBusDevice::AsTuner(CCECBusDevice *device)
0cfdeb5a 1262{
004b8382
LOK
1263 if (device && device->GetType() == CEC_DEVICE_TYPE_TUNER)
1264 return static_cast<CCECTuner *>(device);
1265 return NULL;
0cfdeb5a
LOK
1266}
1267
004b8382 1268CCECTV *CCECBusDevice::AsTV(CCECBusDevice *device)
0cfdeb5a 1269{
004b8382
LOK
1270 if (device && device->GetType() == CEC_DEVICE_TYPE_TV)
1271 return static_cast<CCECTV *>(device);
1272 return NULL;
0cfdeb5a
LOK
1273}
1274
004b8382 1275void CCECBusDevice::MarkBusy(void)
ebb6ddb3 1276{
004b8382
LOK
1277 CLockObject handlerLock(m_handlerMutex);
1278 ++m_iHandlerUseCount;
1279}
ebb6ddb3 1280
004b8382
LOK
1281void CCECBusDevice::MarkReady(void)
1282{
1283 CLockObject handlerLock(m_handlerMutex);
1284 if (m_iHandlerUseCount > 0)
1285 --m_iHandlerUseCount;
1286}
1287
2b44051c 1288bool CCECBusDevice::TryLogicalAddress(cec_version libCECSpecVersion /* = CEC_VERSION_1_4 */)
004b8382
LOK
1289{
1290 LIB_CEC->AddLog(CEC_LOG_DEBUG, "trying logical address '%s'", GetLogicalAddressName());
1291
2b44051c 1292 if (!TransmitPoll(m_iLogicalAddress, false))
ebb6ddb3 1293 {
ff07d530 1294 LIB_CEC->AddLog(CEC_LOG_DEBUG, "using logical address '%s'", GetLogicalAddressName());
2b44051c 1295 SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC, libCECSpecVersion);
004b8382
LOK
1296
1297 return true;
ebb6ddb3 1298 }
004b8382
LOK
1299
1300 LIB_CEC->AddLog(CEC_LOG_DEBUG, "logical address '%s' already taken", GetLogicalAddressName());
1301 SetDeviceStatus(CEC_DEVICE_STATUS_PRESENT);
1302 return false;
ebb6ddb3
LOK
1303}
1304
004b8382 1305CCECClient *CCECBusDevice::GetClient(void)
b78b4e33 1306{
004b8382 1307 return m_processor->GetClient(m_iLogicalAddress);
b78b4e33 1308}
060a7b5e
LOK
1309
1310void CCECBusDevice::SignalOpcode(cec_opcode opcode)
1311{
1312 m_waitForResponse->Received(opcode);
1313}
1314
1315bool CCECBusDevice::WaitForOpcode(cec_opcode opcode)
1316{
1317 return m_waitForResponse->Wait(opcode);
1318}