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