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