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