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