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