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