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