cec: don't send deck status updates when sending an active source message for samsung
[deb_libcec.git] / src / lib / devices / CECBusDevice.cpp
CommitLineData
e9de9629
LOK
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"
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"
eafa9d46 39#include "../platform/timeutils.h"
e9de9629
LOK
40
41using namespace CEC;
42
03ae897d 43#define ToString(p) m_processor->ToString(p)
c4098482 44
e9de9629 45CCECBusDevice::CCECBusDevice(CCECProcessor *processor, cec_logical_address iLogicalAddress, uint16_t iPhysicalAddress) :
c4098482 46 m_type(CEC_DEVICE_TYPE_RESERVED),
e9de9629 47 m_iPhysicalAddress(iPhysicalAddress),
9dc04b07 48 m_iStreamPath(0),
e9de9629 49 m_iLogicalAddress(iLogicalAddress),
e55f3f70 50 m_powerStatus(CEC_POWER_STATUS_UNKNOWN),
e9de9629 51 m_processor(processor),
c4098482 52 m_vendor(CEC_VENDOR_UNKNOWN),
b64db02e 53 m_bReplaceHandler(false),
4abd6768 54 m_menuState(CEC_MENU_STATE_ACTIVATED),
8747dd4f 55 m_bActiveSource(false),
6a1c0009 56 m_iLastActive(0),
f8ae3295 57 m_cecVersion(CEC_VERSION_UNKNOWN),
b64db02e
LOK
58 m_deviceStatus(CEC_DEVICE_STATUS_UNKNOWN),
59 m_handlerMutex(false)
e9de9629
LOK
60{
61 m_handler = new CCECCommandHandler(this);
51b2a094 62
a3269a0a
LOK
63 for (unsigned int iPtr = 0; iPtr < 4; iPtr++)
64 m_menuLanguage.language[iPtr] = '?';
65 m_menuLanguage.language[3] = 0;
66 m_menuLanguage.device = iLogicalAddress;
1fcf5a3f 67
c4098482 68 m_strDeviceName = ToString(m_iLogicalAddress);
e9de9629
LOK
69}
70
71CCECBusDevice::~CCECBusDevice(void)
72{
73 delete m_handler;
74}
75
93729720 76void CCECBusDevice::AddLog(cec_log_level level, const CStdString &strMessage)
e9de9629 77{
93729720 78 m_processor->AddLog(level, strMessage);
e9de9629
LOK
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 {
6c3b0111 87 CLockObject lock(&m_mutex);
8fa35473 88 m_iLastActive = GetTimeMs();
f4b7b1dc
LOK
89
90 if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC)
91 m_deviceStatus = CEC_DEVICE_STATUS_PRESENT;
8fa35473
LOK
92 }
93
7f919115
LOK
94 /* handle the command */
95 bHandled = m_handler->HandleCommand(command);
8fa35473 96
7f919115
LOK
97 /* change status to present */
98 if (bHandled)
8d915412 99 {
6c3b0111 100 CLockObject lock(&m_mutex);
8fa35473 101 if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC)
8d915412 102 {
8fa35473
LOK
103 if (m_deviceStatus != CEC_DEVICE_STATUS_PRESENT)
104 {
105 CStdString strLog;
106 strLog.Format("device %s (%x) status changed to present after command %s", GetLogicalAddressName(), (uint8_t)GetLogicalAddress(), ToString(command.opcode));
107 AddLog(CEC_LOG_DEBUG, strLog);
108 }
109 m_deviceStatus = CEC_DEVICE_STATUS_PRESENT;
8d915412 110 }
8d915412 111 }
7f919115
LOK
112
113 return bHandled;
93729720
LOK
114}
115
93729720
LOK
116bool CCECBusDevice::PowerOn(void)
117{
5e5637c6
LOK
118 CStdString strLog;
119 strLog.Format("<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
120 AddLog(CEC_LOG_DEBUG, strLog.c_str());
00762a71 121
b64db02e 122 if (m_handler->TransmitImageViewOn(GetMyLogicalAddress(), m_iLogicalAddress))
5e5637c6 123 {
9bc8934c
LOK
124 {
125 CLockObject lock(&m_mutex);
fe6f8e37
LOK
126// m_powerStatus = CEC_POWER_STATUS_UNKNOWN;
127 m_powerStatus = CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON;
9bc8934c 128 }
fe6f8e37
LOK
129// cec_power_status status = GetPowerStatus();
130// if (status == CEC_POWER_STATUS_STANDBY || status == CEC_POWER_STATUS_UNKNOWN)
131// {
132// /* sending the normal power on command appears to have failed */
133// CStdString strLog;
134// strLog.Format("<< sending power on keypress to '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
135// AddLog(CEC_LOG_DEBUG, strLog.c_str());
136//
137// TransmitKeypress(CEC_USER_CONTROL_CODE_POWER);
138// return TransmitKeyRelease();
139// }
5e5637c6 140 return true;
f437e4be
LOK
141 }
142
00762a71 143 return false;
93729720
LOK
144}
145
146bool CCECBusDevice::Standby(void)
147{
148 CStdString strLog;
62f5527d 149 strLog.Format("<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress);
93729720
LOK
150 AddLog(CEC_LOG_DEBUG, strLog.c_str());
151
8fa35473 152 return m_handler->TransmitStandby(GetMyLogicalAddress(), m_iLogicalAddress);
93729720
LOK
153}
154
155/** @name Getters */
156//@{
7f919115 157cec_version CCECBusDevice::GetCecVersion(bool bUpdate /* = false */)
93729720 158{
97fc4ffb
LOK
159 bool bRequestUpdate(false);
160 {
161 CLockObject lock(&m_mutex);
162 bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT &&
163 (bUpdate || m_cecVersion == CEC_VERSION_UNKNOWN));
164 }
165
166 if (bRequestUpdate)
f294b22f 167 RequestCecVersion();
f294b22f 168
97fc4ffb 169 CLockObject lock(&m_mutex);
f294b22f
LOK
170 return m_cecVersion;
171}
172
173bool CCECBusDevice::RequestCecVersion(void)
174{
175 bool bReturn(false);
b64db02e 176
81a1e39d 177 if (!MyLogicalAddressContains(m_iLogicalAddress))
93729720 178 {
3e61b350 179 m_handler->MarkBusy();
81a1e39d
LOK
180 CStdString strLog;
181 strLog.Format("<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
182 AddLog(CEC_LOG_NOTICE, strLog);
8fa35473
LOK
183
184 bReturn = m_handler->TransmitRequestCecVersion(GetMyLogicalAddress(), m_iLogicalAddress);
3e61b350 185 m_handler->MarkReady();
93729720 186 }
f294b22f 187 return bReturn;
93729720
LOK
188}
189
62f5527d
LOK
190const char* CCECBusDevice::GetLogicalAddressName(void) const
191{
c4098482 192 return ToString(m_iLogicalAddress);
62f5527d
LOK
193}
194
7f919115 195cec_menu_language &CCECBusDevice::GetMenuLanguage(bool bUpdate /* = false */)
93729720 196{
97fc4ffb
LOK
197 bool bRequestUpdate(false);
198 {
199 CLockObject lock(&m_mutex);
200 bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT &&
201 (bUpdate || !strcmp(m_menuLanguage.language, "???")));
202 }
203
204 if (bRequestUpdate)
f294b22f 205 RequestMenuLanguage();
5e5637c6 206
97fc4ffb 207 CLockObject lock(&m_mutex);
f294b22f
LOK
208 return m_menuLanguage;
209}
210
211bool CCECBusDevice::RequestMenuLanguage(void)
212{
213 bool bReturn(false);
b64db02e 214
4d738fe3
LOK
215 if (!MyLogicalAddressContains(m_iLogicalAddress) &&
216 !IsUnsupportedFeature(CEC_OPCODE_GET_MENU_LANGUAGE))
93729720 217 {
3e61b350 218 m_handler->MarkBusy();
81a1e39d
LOK
219 CStdString strLog;
220 strLog.Format("<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
221 AddLog(CEC_LOG_NOTICE, strLog);
8fa35473 222 bReturn = m_handler->TransmitRequestMenuLanguage(GetMyLogicalAddress(), m_iLogicalAddress);
3e61b350 223 m_handler->MarkReady();
93729720 224 }
f294b22f 225 return bReturn;
93729720
LOK
226}
227
3e61b350
LOK
228cec_menu_state CCECBusDevice::GetMenuState(void)
229{
230 CLockObject lock(&m_mutex);
231 return m_menuState;
232}
233
93729720
LOK
234cec_logical_address CCECBusDevice::GetMyLogicalAddress(void) const
235{
236 return m_processor->GetLogicalAddress();
f8513317
LOK
237}
238
e9de9629
LOK
239uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const
240{
241 return m_processor->GetPhysicalAddress();
242}
243
7f919115 244CStdString CCECBusDevice::GetOSDName(bool bUpdate /* = false */)
ed21be2a 245{
97fc4ffb
LOK
246 bool bRequestUpdate(false);
247 {
248 CLockObject lock(&m_mutex);
249 bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT &&
250 (bUpdate || m_strDeviceName.Equals(ToString(m_iLogicalAddress))) &&
251 m_type != CEC_DEVICE_TYPE_TV);
252 }
253
254 if (bRequestUpdate)
5e5637c6
LOK
255 RequestOSDName();
256
97fc4ffb 257 CLockObject lock(&m_mutex);
ed21be2a
LOK
258 return m_strDeviceName;
259}
260
261bool CCECBusDevice::RequestOSDName(void)
262{
263 bool bReturn(false);
b64db02e 264
4d738fe3
LOK
265 if (!MyLogicalAddressContains(m_iLogicalAddress) &&
266 !IsUnsupportedFeature(CEC_OPCODE_GIVE_OSD_NAME))
ed21be2a 267 {
3e61b350 268 m_handler->MarkBusy();
ed21be2a
LOK
269 CStdString strLog;
270 strLog.Format("<< requesting OSD name of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
271 AddLog(CEC_LOG_NOTICE, strLog);
8fa35473 272 bReturn = m_handler->TransmitRequestOSDName(GetMyLogicalAddress(), m_iLogicalAddress);
3e61b350 273 m_handler->MarkReady();
ed21be2a
LOK
274 }
275 return bReturn;
276}
277
7f919115 278uint16_t CCECBusDevice::GetPhysicalAddress(bool bUpdate /* = false */)
16b1e052 279{
97fc4ffb 280 bool bRequestUpdate(false);
16b1e052 281 {
97fc4ffb
LOK
282 CLockObject lock(&m_mutex);
283 bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT &&
284 (m_iPhysicalAddress == 0xFFFF || bUpdate));
16b1e052
LOK
285 }
286
97fc4ffb
LOK
287 if (bRequestUpdate && !RequestPhysicalAddress())
288 AddLog(CEC_LOG_ERROR, "failed to request the physical address (1)");
289
290 CLockObject lock(&m_mutex);
16b1e052
LOK
291 return m_iPhysicalAddress;
292}
293
294bool CCECBusDevice::RequestPhysicalAddress(void)
295{
296 bool bReturn(false);
b64db02e 297
16b1e052
LOK
298 if (!MyLogicalAddressContains(m_iLogicalAddress))
299 {
3e61b350 300 m_handler->MarkBusy();
16b1e052
LOK
301 CStdString strLog;
302 strLog.Format("<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
303 AddLog(CEC_LOG_NOTICE, strLog);
8fa35473 304 bReturn = m_handler->TransmitRequestPhysicalAddress(GetMyLogicalAddress(), m_iLogicalAddress);
3e61b350 305 m_handler->MarkReady();
16b1e052
LOK
306 }
307 return bReturn;
308}
309
7f919115 310cec_power_status CCECBusDevice::GetPowerStatus(bool bUpdate /* = false */)
e9de9629 311{
97fc4ffb
LOK
312 bool bRequestUpdate(false);
313 {
314 CLockObject lock(&m_mutex);
315 bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT &&
c8f0eef0
LOK
316 (bUpdate || m_powerStatus == CEC_POWER_STATUS_UNKNOWN ||
317 m_powerStatus == CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON));
97fc4ffb
LOK
318 }
319
320 if (bRequestUpdate)
f294b22f 321 RequestPowerStatus();
5e5637c6 322
97fc4ffb 323 CLockObject lock(&m_mutex);
f294b22f
LOK
324 return m_powerStatus;
325}
326
327bool CCECBusDevice::RequestPowerStatus(void)
328{
329 bool bReturn(false);
b64db02e 330
4d738fe3
LOK
331 if (!MyLogicalAddressContains(m_iLogicalAddress) &&
332 !IsUnsupportedFeature(CEC_OPCODE_GIVE_DEVICE_POWER_STATUS))
93729720 333 {
3e61b350 334 m_handler->MarkBusy();
81a1e39d
LOK
335 CStdString strLog;
336 strLog.Format("<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
337 AddLog(CEC_LOG_NOTICE, strLog);
8fa35473 338 bReturn = m_handler->TransmitRequestPowerStatus(GetMyLogicalAddress(), m_iLogicalAddress);
3e61b350 339 m_handler->MarkReady();
93729720 340 }
f294b22f
LOK
341 return bReturn;
342}
93729720 343
7f919115 344cec_vendor_id CCECBusDevice::GetVendorId(bool bUpdate /* = false */)
f294b22f 345{
97fc4ffb
LOK
346 bool bRequestUpdate(false);
347 {
348 CLockObject lock(&m_mutex);
349 bRequestUpdate = (GetStatus() == CEC_DEVICE_STATUS_PRESENT &&
350 (bUpdate || m_vendor == CEC_VENDOR_UNKNOWN));
351 }
352
353 if (bRequestUpdate)
f294b22f 354 RequestVendorId();
5e5637c6 355
97fc4ffb 356 CLockObject lock(&m_mutex);
f294b22f 357 return m_vendor;
e9de9629
LOK
358}
359
f294b22f 360bool CCECBusDevice::RequestVendorId(void)
a3269a0a 361{
f294b22f 362 bool bReturn(false);
b64db02e 363
81a1e39d 364 if (!MyLogicalAddressContains(m_iLogicalAddress))
a3269a0a 365 {
3e61b350 366 m_handler->MarkBusy();
81a1e39d
LOK
367 CStdString strLog;
368 strLog.Format("<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
369 AddLog(CEC_LOG_NOTICE, strLog);
8fa35473 370 bReturn = m_handler->TransmitRequestVendorId(GetMyLogicalAddress(), m_iLogicalAddress);
3e61b350
LOK
371 m_handler->MarkReady();
372
373 ReplaceHandler(true);
a3269a0a 374 }
f294b22f 375 return bReturn;
93729720
LOK
376}
377
7f919115 378const char *CCECBusDevice::GetVendorName(bool bUpdate /* = false */)
c4098482 379{
7f919115 380 return ToString(GetVendorId(bUpdate));
c4098482
LOK
381}
382
93729720
LOK
383bool CCECBusDevice::MyLogicalAddressContains(cec_logical_address address) const
384{
385 return m_processor->HasLogicalAddress(address);
a3269a0a
LOK
386}
387
9fd73dd4
LOK
388bool CCECBusDevice::NeedsPoll(void)
389{
390 bool bSendPoll(false);
391 switch (m_iLogicalAddress)
392 {
393 case CECDEVICE_PLAYBACKDEVICE3:
394 if (m_processor->m_busDevices[CECDEVICE_PLAYBACKDEVICE2]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
395 bSendPoll = true;
396 break;
397 case CECDEVICE_PLAYBACKDEVICE2:
398 if (m_processor->m_busDevices[CECDEVICE_PLAYBACKDEVICE1]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
399 bSendPoll = true;
400 break;
401 case CECDEVICE_RECORDINGDEVICE3:
402 if (m_processor->m_busDevices[CECDEVICE_RECORDINGDEVICE2]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
403 bSendPoll = true;
404 break;
405 case CECDEVICE_RECORDINGDEVICE2:
406 if (m_processor->m_busDevices[CECDEVICE_RECORDINGDEVICE1]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
407 bSendPoll = true;
408 break;
409 case CECDEVICE_TUNER4:
410 if (m_processor->m_busDevices[CECDEVICE_TUNER3]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
411 bSendPoll = true;
412 break;
413 case CECDEVICE_TUNER3:
414 if (m_processor->m_busDevices[CECDEVICE_TUNER2]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
415 bSendPoll = true;
416 break;
417 case CECDEVICE_TUNER2:
418 if (m_processor->m_busDevices[CECDEVICE_TUNER1]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
419 bSendPoll = true;
420 break;
421 case CECDEVICE_AUDIOSYSTEM:
422 case CECDEVICE_PLAYBACKDEVICE1:
423 case CECDEVICE_RECORDINGDEVICE1:
424 case CECDEVICE_TUNER1:
425 case CECDEVICE_TV:
426 bSendPoll = true;
427 break;
428 default:
429 break;
430 }
431
432 return bSendPoll;
433}
434
435cec_bus_device_status CCECBusDevice::GetStatus(bool bForcePoll /* = false */)
f8ae3295 436{
6c3b0111 437 CLockObject lock(&m_mutex);
b750a5c3
LOK
438 if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC &&
439 (m_deviceStatus == CEC_DEVICE_STATUS_UNKNOWN || bForcePoll))
f8ae3295 440 {
95a73fa7 441 lock.Leave();
9fd73dd4
LOK
442 bool bPollAcked(false);
443 if (bForcePoll || NeedsPoll())
444 bPollAcked = m_processor->PollDevice(m_iLogicalAddress);
95a73fa7
LOK
445
446 lock.Lock();
447 m_deviceStatus = bPollAcked ? CEC_DEVICE_STATUS_PRESENT : CEC_DEVICE_STATUS_NOT_PRESENT;
f8ae3295
LOK
448 }
449
450 return m_deviceStatus;
451}
452
93729720
LOK
453//@}
454
455/** @name Setters */
456//@{
e55f3f70 457void CCECBusDevice::SetCecVersion(const cec_version newVersion)
6a1c0009 458{
6a1c0009
LOK
459 m_cecVersion = newVersion;
460
c686413b 461 CStdString strLog;
c4098482 462 strLog.Format("%s (%X): CEC version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(newVersion));
6a1c0009
LOK
463 AddLog(CEC_LOG_DEBUG, strLog);
464}
465
93729720
LOK
466void CCECBusDevice::SetMenuLanguage(const cec_menu_language &language)
467{
6c3b0111 468 CLockObject lock(&m_mutex);
93729720
LOK
469 if (language.device == m_iLogicalAddress)
470 {
471 CStdString strLog;
62f5527d 472 strLog.Format(">> %s (%X): menu language set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, language.language);
93729720
LOK
473 m_processor->AddLog(CEC_LOG_DEBUG, strLog);
474 m_menuLanguage = language;
475 }
476}
477
15d1a84c
LOK
478void CCECBusDevice::SetOSDName(CStdString strName)
479{
6c3b0111 480 CLockObject lock(&m_mutex);
15d1a84c
LOK
481 if (m_strDeviceName != strName)
482 {
483 CStdString strLog;
484 strLog.Format(">> %s (%X): osd name set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, strName);
485 m_processor->AddLog(CEC_LOG_DEBUG, strLog);
486 m_strDeviceName = strName;
487 }
488}
489
28fa6c97
LOK
490void CCECBusDevice::SetMenuState(const cec_menu_state state)
491{
6c3b0111 492 CLockObject lock(&m_mutex);
28fa6c97
LOK
493 if (m_menuState != state)
494 {
495 CStdString strLog;
496 strLog.Format(">> %s (%X): menu state set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_menuState));
497 m_processor->AddLog(CEC_LOG_DEBUG, strLog);
498 m_menuState = state;
499 }
500}
501
3fd2a2b8 502void CCECBusDevice::SetInactiveSource(void)
7856411b 503{
3fd2a2b8 504 {
6c3b0111 505 CLockObject lock(&m_mutex);
3fd2a2b8
LOK
506 m_bActiveSource = false;
507 }
508
509 if (MyLogicalAddressContains(m_iLogicalAddress))
510 SetPowerStatus(CEC_POWER_STATUS_STANDBY);
7856411b
LOK
511}
512
37b0c572 513void CCECBusDevice::SetActiveSource(void)
7856411b 514{
6c3b0111 515 CLockObject lock(&m_mutex);
ff684fef
LOK
516 if (!m_bActiveSource)
517 {
518 CStdString strLog;
519 strLog.Format("making %s (%x) the active source", GetLogicalAddressName(), m_iLogicalAddress);
520 AddLog(CEC_LOG_DEBUG, strLog);
521 }
7856411b
LOK
522
523 for (int iPtr = 0; iPtr < 16; iPtr++)
524 if (iPtr != m_iLogicalAddress)
3fd2a2b8 525 m_processor->m_busDevices[iPtr]->SetInactiveSource();
7856411b
LOK
526
527 m_bActiveSource = true;
528 m_powerStatus = CEC_POWER_STATUS_ON;
529}
530
93fff5c1
LOK
531bool CCECBusDevice::TryLogicalAddress(void)
532{
533 CStdString strLog;
534 strLog.Format("trying logical address '%s'", GetLogicalAddressName());
535 AddLog(CEC_LOG_DEBUG, strLog);
536
537 m_processor->SetAckMask(0x1 << m_iLogicalAddress);
538 if (!TransmitPoll(m_iLogicalAddress))
539 {
540 strLog.Format("using logical address '%s'", GetLogicalAddressName());
541 AddLog(CEC_LOG_NOTICE, strLog);
542 SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
543
544 return true;
545 }
546
547 strLog.Format("logical address '%s' already taken", GetLogicalAddressName());
548 AddLog(CEC_LOG_DEBUG, strLog);
549 SetDeviceStatus(CEC_DEVICE_STATUS_PRESENT);
550 return false;
551}
552
553void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus)
554{
6c3b0111 555 CLockObject lock(&m_mutex);
93fff5c1
LOK
556 switch (newStatus)
557 {
558 case CEC_DEVICE_STATUS_UNKNOWN:
559 m_iStreamPath = 0;
560 m_powerStatus = CEC_POWER_STATUS_UNKNOWN;
561 m_vendor = CEC_VENDOR_UNKNOWN;
562 m_menuState = CEC_MENU_STATE_ACTIVATED;
563 m_bActiveSource = false;
93fff5c1
LOK
564 m_iLastActive = 0;
565 m_cecVersion = CEC_VERSION_UNKNOWN;
566 m_deviceStatus = newStatus;
567 break;
568 case CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC:
569 m_iStreamPath = 0;
570 m_powerStatus = CEC_POWER_STATUS_ON;
571 m_vendor = CEC_VENDOR_UNKNOWN;
572 m_menuState = CEC_MENU_STATE_ACTIVATED;
573 m_bActiveSource = false;
93fff5c1
LOK
574 m_iLastActive = 0;
575 m_cecVersion = CEC_VERSION_1_3A;
576 m_deviceStatus = newStatus;
577 break;
578 case CEC_DEVICE_STATUS_PRESENT:
579 case CEC_DEVICE_STATUS_NOT_PRESENT:
580 m_deviceStatus = newStatus;
581 break;
582 }
583}
584
9dc04b07 585void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress)
93729720 586{
6c3b0111 587 CLockObject lock(&m_mutex);
b8176e3c 588 if (iNewAddress > 0 && m_iPhysicalAddress != iNewAddress)
93729720
LOK
589 {
590 CStdString strLog;
62f5527d 591 strLog.Format(">> %s (%X): physical address changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress, iNewAddress);
93729720
LOK
592 AddLog(CEC_LOG_DEBUG, strLog.c_str());
593
594 m_iPhysicalAddress = iNewAddress;
595 }
596}
597
9dc04b07
LOK
598void CCECBusDevice::SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress /* = 0 */)
599{
6c3b0111 600 CLockObject lock(&m_mutex);
9dc04b07
LOK
601 if (iNewAddress > 0)
602 {
603 CStdString strLog;
68a12fbf 604 strLog.Format(">> %s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, iOldAddress == 0 ? m_iStreamPath : iOldAddress, iNewAddress);
9dc04b07
LOK
605 AddLog(CEC_LOG_DEBUG, strLog.c_str());
606
607 m_iStreamPath = iNewAddress;
96274140
LOK
608
609 if (iNewAddress > 0)
81a1e39d
LOK
610 {
611 lock.Leave();
96274140 612 SetPowerStatus(CEC_POWER_STATUS_ON);
81a1e39d 613 }
9dc04b07
LOK
614 }
615}
616
e55f3f70
LOK
617void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus)
618{
6c3b0111 619 CLockObject lock(&m_mutex);
b0271d54
LOK
620 if (m_powerStatus != powerStatus)
621 {
622 CStdString strLog;
c4098482 623 strLog.Format(">> %s (%X): power status changed from '%s' to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_powerStatus), ToString(powerStatus));
b0271d54
LOK
624 m_processor->AddLog(CEC_LOG_DEBUG, strLog);
625 m_powerStatus = powerStatus;
626 }
e55f3f70
LOK
627}
628
3e61b350 629bool CCECBusDevice::ReplaceHandler(bool bActivateSource /* = true */)
e9de9629 630{
b64db02e
LOK
631 CLockObject lock(&m_mutex);
632 CLockObject handlerLock(&m_handlerMutex);
d211708b 633
b64db02e 634 if (m_vendor != m_handler->GetVendorId())
e9de9629 635 {
2e49a6e4 636 if (CCECCommandHandler::HasSpecificHandler(m_vendor))
3e61b350 637 {
2e49a6e4
LOK
638 CStdString strLog;
639 if (m_handler->InUse())
640 {
641 strLog.Format("handler for device '%s' (%x) is being used. not replacing the command handler", GetLogicalAddressName(), GetLogicalAddress());
642 m_processor->AddLog(CEC_LOG_DEBUG, strLog);
643 return false;
644 }
81a1e39d 645
2e49a6e4
LOK
646 strLog.Format("replacing the command handler for device '%s' (%x)", GetLogicalAddressName(), GetLogicalAddress());
647 m_processor->AddLog(CEC_LOG_DEBUG, strLog);
648 delete m_handler;
d211708b 649
2e49a6e4
LOK
650 switch (m_vendor)
651 {
652 case CEC_VENDOR_SAMSUNG:
653 m_handler = new CANCommandHandler(this);
654 break;
655 case CEC_VENDOR_LG:
656 m_handler = new CSLCommandHandler(this);
657 break;
658 case CEC_VENDOR_PANASONIC:
659 m_handler = new CVLCommandHandler(this);
660 break;
661 default:
662 m_handler = new CCECCommandHandler(this);
663 break;
664 }
b64db02e 665
2e49a6e4
LOK
666 m_handler->SetVendorId(m_vendor);
667 m_handler->InitHandler();
3e61b350 668
ff684fef 669 if (bActivateSource && m_processor->GetLogicalAddresses().IsSet(m_iLogicalAddress) && m_processor->IsInitialised() && IsActiveSource())
2e49a6e4
LOK
670 m_handler->ActivateSource();
671 }
e9de9629
LOK
672 }
673
b64db02e
LOK
674 return true;
675}
676
3e61b350 677bool CCECBusDevice::SetVendorId(uint64_t iVendorId)
b64db02e
LOK
678{
679 bool bVendorChanged(false);
680
681 {
682 CLockObject lock(&m_mutex);
683 bVendorChanged = (m_vendor != (cec_vendor_id)iVendorId);
684 m_vendor = (cec_vendor_id)iVendorId;
b64db02e 685 }
8d915412 686
e9de9629 687 CStdString strLog;
5e5637c6 688 strLog.Format("%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_vendor), m_vendor);
e9de9629 689 m_processor->AddLog(CEC_LOG_DEBUG, strLog.c_str());
8fa35473
LOK
690
691 return bVendorChanged;
e9de9629 692}
93729720 693//@}
e9de9629 694
93729720
LOK
695/** @name Transmit methods */
696//@{
697bool CCECBusDevice::TransmitActiveSource(void)
0f23c85c 698{
8fa35473 699 bool bSendActiveSource(false);
0f23c85c 700
8747dd4f 701 {
6c3b0111 702 CLockObject lock(&m_mutex);
8fa35473
LOK
703 if (m_powerStatus != CEC_POWER_STATUS_ON)
704 {
705 CStdString strLog;
706 strLog.Format("<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress);
707 AddLog(CEC_LOG_DEBUG, strLog);
708 }
709 else if (m_bActiveSource)
710 {
711 CStdString strLog;
712 strLog.Format("<< %s (%X) -> broadcast (F): active source (%4x)", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress);
713 AddLog(CEC_LOG_NOTICE, strLog);
714 bSendActiveSource = true;
715 }
716 else
717 {
718 CStdString strLog;
719 strLog.Format("<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress);
720 AddLog(CEC_LOG_DEBUG, strLog);
721 }
8747dd4f
LOK
722 }
723
b64db02e
LOK
724 if (bSendActiveSource)
725 {
b64db02e 726 m_handler->TransmitImageViewOn(m_iLogicalAddress, CECDEVICE_TV);
468a1414 727 m_handler->TransmitActiveSource(m_iLogicalAddress, m_iPhysicalAddress);
b64db02e
LOK
728 return true;
729 }
730
731 return false;
0f23c85c
LOK
732}
733
29912296 734bool CCECBusDevice::TransmitCECVersion(cec_logical_address dest)
0f23c85c 735{
8fa35473
LOK
736 cec_version version;
737 {
6c3b0111 738 CLockObject lock(&m_mutex);
8fa35473
LOK
739 CStdString strLog;
740 strLog.Format("<< %s (%X) -> %s (%X): cec version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_cecVersion));
741 AddLog(CEC_LOG_NOTICE, strLog);
742 version = m_cecVersion;
743 }
0f23c85c 744
8fa35473 745 return m_handler->TransmitCECVersion(m_iLogicalAddress, dest, version);
0f23c85c
LOK
746}
747
ab27363d 748bool CCECBusDevice::TransmitInactiveSource(void)
93729720 749{
8fa35473
LOK
750 uint16_t iPhysicalAddress;
751 {
6c3b0111 752 CLockObject lock(&m_mutex);
8fa35473
LOK
753 CStdString strLog;
754 strLog.Format("<< %s (%X) -> broadcast (F): inactive source", GetLogicalAddressName(), m_iLogicalAddress);
755 AddLog(CEC_LOG_NOTICE, strLog);
756 iPhysicalAddress = m_iPhysicalAddress;
757 }
93729720 758
8fa35473 759 return m_handler->TransmitInactiveSource(m_iLogicalAddress, iPhysicalAddress);
93729720
LOK
760}
761
29912296 762bool CCECBusDevice::TransmitMenuState(cec_logical_address dest)
0f23c85c 763{
8fa35473
LOK
764 cec_menu_state menuState;
765 {
6c3b0111 766 CLockObject lock(&m_mutex);
8fa35473
LOK
767 CStdString strLog;
768 strLog.Format("<< %s (%X) -> %s (%X): menu state '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_menuState));
769 AddLog(CEC_LOG_NOTICE, strLog);
770 menuState = m_menuState;
771 }
0f23c85c 772
8fa35473 773 return m_handler->TransmitMenuState(m_iLogicalAddress, dest, menuState);
0f23c85c
LOK
774}
775
29912296 776bool CCECBusDevice::TransmitOSDName(cec_logical_address dest)
0f23c85c 777{
8fa35473
LOK
778 CStdString strDeviceName;
779 {
6c3b0111 780 CLockObject lock(&m_mutex);
8fa35473
LOK
781 CStdString strLog;
782 strLog.Format("<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, m_strDeviceName.c_str());
783 AddLog(CEC_LOG_NOTICE, strLog.c_str());
784 strDeviceName = m_strDeviceName;
785 }
0f23c85c 786
8fa35473 787 return m_handler->TransmitOSDName(m_iLogicalAddress, dest, strDeviceName);
0f23c85c
LOK
788}
789
38bdb943
LOK
790bool CCECBusDevice::TransmitOSDString(cec_logical_address dest, cec_display_control duration, const char *strMessage)
791{
4d738fe3
LOK
792 if (!IsUnsupportedFeature(CEC_OPCODE_SET_OSD_STRING))
793 {
794 CStdString strLog;
795 strLog.Format("<< %s (%X) -> %s (%X): display OSD message '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, strMessage);
796 AddLog(CEC_LOG_NOTICE, strLog.c_str());
38bdb943 797
4d738fe3
LOK
798 return m_handler->TransmitOSDString(m_iLogicalAddress, dest, duration, strMessage);
799 }
800 return false;
38bdb943
LOK
801}
802
29912296 803bool CCECBusDevice::TransmitPhysicalAddress(void)
0f23c85c 804{
8fa35473
LOK
805 uint16_t iPhysicalAddress;
806 cec_device_type type;
807 {
6c3b0111 808 CLockObject lock(&m_mutex);
8fa35473
LOK
809 if (m_iPhysicalAddress == 0xffff)
810 return false;
2bdfdeef 811
8fa35473
LOK
812 CStdString strLog;
813 strLog.Format("<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress);
814 AddLog(CEC_LOG_NOTICE, strLog.c_str());
0f23c85c 815
8fa35473
LOK
816 iPhysicalAddress = m_iPhysicalAddress;
817 type = m_type;
818 }
0f23c85c 819
8fa35473 820 return m_handler->TransmitPhysicalAddress(m_iLogicalAddress, iPhysicalAddress, type);
0f23c85c
LOK
821}
822
93729720 823bool CCECBusDevice::TransmitPoll(cec_logical_address dest)
57f45e6c
LOK
824{
825 bool bReturn(false);
93729720
LOK
826 if (dest == CECDEVICE_UNKNOWN)
827 dest = m_iLogicalAddress;
f8513317 828
b750a5c3
LOK
829 CCECBusDevice *destDevice = m_processor->m_busDevices[dest];
830 if (destDevice->m_deviceStatus == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC)
831 return bReturn;
832
57f45e6c 833 CStdString strLog;
c4098482 834 strLog.Format("<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest);
d7be392a 835 AddLog(CEC_LOG_NOTICE, strLog.c_str());
8fa35473 836 bReturn = m_handler->TransmitPoll(m_iLogicalAddress, dest);
57f45e6c 837 AddLog(CEC_LOG_DEBUG, bReturn ? ">> POLL sent" : ">> POLL not sent");
8fa35473 838
6c3b0111 839 CLockObject lock(&m_mutex);
1674de37 840 if (bReturn)
8fa35473 841 {
1674de37 842 m_iLastActive = GetTimeMs();
b750a5c3 843 destDevice->m_deviceStatus = CEC_DEVICE_STATUS_PRESENT;
8fa35473 844 }
b750a5c3
LOK
845 else
846 destDevice->m_deviceStatus = CEC_DEVICE_STATUS_NOT_PRESENT;
1674de37 847
57f45e6c
LOK
848 return bReturn;
849}
93729720
LOK
850
851bool CCECBusDevice::TransmitPowerState(cec_logical_address dest)
852{
8fa35473
LOK
853 cec_power_status state;
854 {
6c3b0111 855 CLockObject lock(&m_mutex);
8fa35473
LOK
856 CStdString strLog;
857 strLog.Format("<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_powerStatus));
858 AddLog(CEC_LOG_NOTICE, strLog.c_str());
859 state = m_powerStatus;
860 }
93729720 861
8fa35473 862 return m_handler->TransmitPowerState(m_iLogicalAddress, dest, state);
93729720
LOK
863}
864
fe6f8e37 865bool CCECBusDevice::TransmitVendorID(cec_logical_address dest, bool bSendAbort /* = true */)
93729720 866{
8fa35473
LOK
867 uint64_t iVendorId;
868 {
6c3b0111 869 CLockObject lock(&m_mutex);
8fa35473
LOK
870 iVendorId = (uint64_t)m_vendor;
871 }
872
873 if (iVendorId == CEC_VENDOR_UNKNOWN)
c4098482 874 {
fe6f8e37
LOK
875 if (bSendAbort)
876 {
877 CStdString strLog;
878 strLog.Format("<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest);
879 AddLog(CEC_LOG_NOTICE, strLog);
d7be392a 880
fe6f8e37
LOK
881 m_processor->TransmitAbort(dest, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
882 }
c4098482
LOK
883 return false;
884 }
885 else
886 {
887 CStdString strLog;
8fa35473 888 strLog.Format("<< %s (%X) -> %s (%X): vendor id %s (%x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString((cec_vendor_id)iVendorId), iVendorId);
c4098482
LOK
889 AddLog(CEC_LOG_NOTICE, strLog);
890
8fa35473 891 return m_handler->TransmitVendorID(m_iLogicalAddress, iVendorId);
c4098482 892 }
93729720 893}
a33794d8 894
4bec9d79 895bool CCECBusDevice::TransmitKeypress(cec_user_control_code key, bool bWait /* = true */)
a33794d8 896{
4bec9d79 897 return m_handler->TransmitKeypress(m_processor->GetLogicalAddress(), m_iLogicalAddress, key, bWait);
a33794d8
LOK
898}
899
4bec9d79 900bool CCECBusDevice::TransmitKeyRelease(bool bWait /* = true */)
a33794d8 901{
4bec9d79 902 return m_handler->TransmitKeyRelease(m_processor->GetLogicalAddress(), m_iLogicalAddress, bWait);
a33794d8 903}
4d738fe3
LOK
904
905bool CCECBusDevice::IsUnsupportedFeature(cec_opcode opcode) const
906{
907 return m_unsupportedFeatures.find(opcode) != m_unsupportedFeatures.end();
908}
909
910void CCECBusDevice::SetUnsupportedFeature(cec_opcode opcode)
911{
912 m_unsupportedFeatures.insert(opcode);
913}
b64db02e 914
3e61b350 915bool CCECBusDevice::ActivateSource(void)
b64db02e
LOK
916{
917 CLockObject lock(&m_mutex);
3e61b350 918 return m_handler->ActivateSource();
b64db02e
LOK
919}
920
93729720 921//@}