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