cec: separate 'last command sent' and 'last active' in CCECBusDevice
[deb_libcec.git] / src / lib / devices / CECBusDevice.cpp
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"
34 #include "../CECProcessor.h"
35 #include "../implementations/ANCommandHandler.h"
36 #include "../implementations/CECCommandHandler.h"
37 #include "../implementations/SLCommandHandler.h"
38 #include "../implementations/VLCommandHandler.h"
39 #include "../platform/timeutils.h"
40
41 using namespace CEC;
42
43 CCECBusDevice::CCECBusDevice(CCECProcessor *processor, cec_logical_address iLogicalAddress, uint16_t iPhysicalAddress) :
44 m_iPhysicalAddress(iPhysicalAddress),
45 m_iStreamPath(0),
46 m_iLogicalAddress(iLogicalAddress),
47 m_powerStatus(CEC_POWER_STATUS_UNKNOWN),
48 m_processor(processor),
49 m_bMenuActive(true),
50 m_bActiveSource(false),
51 m_iVendorClass(CEC_VENDOR_UNKNOWN),
52 m_iLastCommandSent(0),
53 m_iLastActive(0),
54 m_cecVersion(CEC_VERSION_UNKNOWN)
55 {
56 m_handler = new CCECCommandHandler(this);
57
58 for (unsigned int iPtr = 0; iPtr < 4; iPtr++)
59 m_menuLanguage.language[iPtr] = '?';
60 m_menuLanguage.language[3] = 0;
61 m_menuLanguage.device = iLogicalAddress;
62
63 m_vendor.vendor = CEC_VENDOR_UNKNOWN;
64 m_type = CEC_DEVICE_TYPE_RESERVED;
65 m_strDeviceName = CCECCommandHandler::ToString(m_iLogicalAddress);
66 }
67
68 CCECBusDevice::~CCECBusDevice(void)
69 {
70 m_condition.Broadcast();
71 delete m_handler;
72 }
73
74 void CCECBusDevice::AddLog(cec_log_level level, const CStdString &strMessage)
75 {
76 m_processor->AddLog(level, strMessage);
77 }
78
79 bool CCECBusDevice::HandleCommand(const cec_command &command)
80 {
81 CLockObject lock(&m_mutex);
82 m_iLastActive = GetTimeMs();
83 m_handler->HandleCommand(command);
84 m_condition.Signal();
85 return true;
86 }
87
88 void CCECBusDevice::PollVendorId(void)
89 {
90 CLockObject lock(&m_mutex);
91 if (m_iLastActive > 0 && m_iLogicalAddress != CECDEVICE_BROADCAST &&
92 m_vendor.vendor == CEC_VENDOR_UNKNOWN &&
93 GetTimeMs() - m_iLastCommandSent > 5000 &&
94 !m_processor->IsMonitoring())
95 {
96 CStdString strLog;
97 strLog.Format("<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
98 AddLog(CEC_LOG_NOTICE, strLog);
99 m_iLastCommandSent = GetTimeMs();
100
101 cec_command command;
102 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
103 if (m_processor->Transmit(command))
104 m_condition.Wait(&m_mutex, 1000);
105 }
106 }
107
108 bool CCECBusDevice::PowerOn(void)
109 {
110 CStdString strLog;
111 strLog.Format("<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
112 AddLog(CEC_LOG_DEBUG, strLog.c_str());
113
114 cec_command command;
115 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_IMAGE_VIEW_ON);
116
117 return m_processor->Transmit(command);
118 }
119
120 bool CCECBusDevice::Standby(void)
121 {
122 CStdString strLog;
123 strLog.Format("<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress);
124 AddLog(CEC_LOG_DEBUG, strLog.c_str());
125
126 cec_command command;
127 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_STANDBY);
128
129 return m_processor->Transmit(command);
130 }
131
132 /** @name Getters */
133 //@{
134 cec_version CCECBusDevice::GetCecVersion(void)
135 {
136 if (m_cecVersion == CEC_VERSION_UNKNOWN)
137 {
138 if (!MyLogicalAddressContains(m_iLogicalAddress))
139 {
140 CStdString strLog;
141 strLog.Format("<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
142 AddLog(CEC_LOG_NOTICE, strLog);
143 cec_command command;
144 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GET_CEC_VERSION);
145 CLockObject lock(&m_mutex);
146 if (m_processor->Transmit(command))
147 m_condition.Wait(&m_mutex, 1000);
148 }
149 }
150
151 return m_cecVersion;
152 }
153
154 const char* CCECBusDevice::GetLogicalAddressName(void) const
155 {
156 return CCECCommandHandler::ToString(m_iLogicalAddress);
157 }
158
159 cec_menu_language &CCECBusDevice::GetMenuLanguage(void)
160 {
161 if (!strcmp(m_menuLanguage.language, "???"))
162 {
163 if (!MyLogicalAddressContains(m_iLogicalAddress))
164 {
165 CStdString strLog;
166 strLog.Format("<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
167 AddLog(CEC_LOG_NOTICE, strLog);
168 cec_command command;
169 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GET_MENU_LANGUAGE);
170 CLockObject lock(&m_mutex);
171 if (m_processor->Transmit(command))
172 m_condition.Wait(&m_mutex, 1000);
173 }
174 }
175
176 return m_menuLanguage;
177 }
178
179 cec_logical_address CCECBusDevice::GetMyLogicalAddress(void) const
180 {
181 return m_processor->GetLogicalAddress();
182 }
183
184 uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const
185 {
186 return m_processor->GetPhysicalAddress();
187 }
188
189 cec_power_status CCECBusDevice::GetPowerStatus(void)
190 {
191 if (m_powerStatus == CEC_POWER_STATUS_UNKNOWN)
192 {
193 if (!MyLogicalAddressContains(m_iLogicalAddress))
194 {
195 CStdString strLog;
196 strLog.Format("<< requesting power status 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_GIVE_DEVICE_POWER_STATUS);
200 CLockObject lock(&m_mutex);
201 if (m_processor->Transmit(command))
202 m_condition.Wait(&m_mutex, 1000);
203 }
204 }
205
206 return m_powerStatus;
207 }
208
209 const cec_vendor &CCECBusDevice::GetVendor(void)
210 {
211 if (m_vendor.vendor == CEC_VENDOR_UNKNOWN)
212 {
213 if (!MyLogicalAddressContains(m_iLogicalAddress))
214 {
215 CStdString strLog;
216 strLog.Format("<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
217 AddLog(CEC_LOG_NOTICE, strLog);
218 cec_command command;
219 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
220 CLockObject lock(&m_mutex);
221
222 if (m_processor->Transmit(command))
223 m_condition.Wait(&m_mutex, 1000);
224 }
225 }
226
227 return m_vendor;
228 }
229
230 bool CCECBusDevice::MyLogicalAddressContains(cec_logical_address address) const
231 {
232 return m_processor->HasLogicalAddress(address);
233 }
234
235 //@}
236
237 /** @name Setters */
238 //@{
239 void CCECBusDevice::SetCecVersion(const cec_version newVersion)
240 {
241 CStdString strLog;
242 m_cecVersion = newVersion;
243
244 switch (newVersion)
245 {
246 case CEC_VERSION_1_2:
247 strLog.Format("%s (%X): CEC version 1.2", GetLogicalAddressName(), m_iLogicalAddress);
248 break;
249 case CEC_VERSION_1_2A:
250 strLog.Format("%s (%X): CEC version 1.2a", GetLogicalAddressName(), m_iLogicalAddress);
251 break;
252 case CEC_VERSION_1_3:
253 strLog.Format("%s (%X): CEC version 1.3", GetLogicalAddressName(), m_iLogicalAddress);
254 break;
255 case CEC_VERSION_1_3A:
256 strLog.Format("%s (%X): CEC version 1.3a", GetLogicalAddressName(), m_iLogicalAddress);
257 break;
258 default:
259 strLog.Format("%s (%X): unknown CEC version", GetLogicalAddressName(), m_iLogicalAddress);
260 m_cecVersion = CEC_VERSION_UNKNOWN;
261 break;
262 }
263 AddLog(CEC_LOG_DEBUG, strLog);
264 }
265
266 void CCECBusDevice::SetMenuLanguage(const cec_menu_language &language)
267 {
268 if (language.device == m_iLogicalAddress)
269 {
270 CStdString strLog;
271 strLog.Format(">> %s (%X): menu language set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, language.language);
272 m_processor->AddLog(CEC_LOG_DEBUG, strLog);
273 m_menuLanguage = language;
274 }
275 }
276
277 void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress)
278 {
279 if (iNewAddress > 0)
280 {
281 CStdString strLog;
282 strLog.Format(">> %s (%X): physical address changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress, iNewAddress);
283 AddLog(CEC_LOG_DEBUG, strLog.c_str());
284
285 m_iPhysicalAddress = iNewAddress;
286 }
287 }
288
289 void CCECBusDevice::SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress /* = 0 */)
290 {
291 if (iNewAddress > 0)
292 {
293 CStdString strLog;
294 strLog.Format(">> %s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, iOldAddress, iNewAddress);
295 AddLog(CEC_LOG_DEBUG, strLog.c_str());
296
297 m_iStreamPath = iNewAddress;
298 }
299 }
300
301 void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus)
302 {
303 if (m_powerStatus != powerStatus)
304 {
305 CStdString strLog;
306 strLog.Format(">> %s (%X): power status changed from %2x to %2x", GetLogicalAddressName(), m_iLogicalAddress, m_powerStatus, powerStatus);
307 m_processor->AddLog(CEC_LOG_DEBUG, strLog);
308 m_powerStatus = powerStatus;
309 }
310 }
311
312 void CCECBusDevice::SetVendorId(uint64_t iVendorId, uint8_t iVendorClass /* = 0 */)
313 {
314 m_vendor.vendor = (cec_vendor_id)iVendorId;
315 m_iVendorClass = iVendorClass;
316
317 switch (iVendorId)
318 {
319 case CEC_VENDOR_SAMSUNG:
320 if (m_handler->GetVendorId() != CEC_VENDOR_SAMSUNG)
321 {
322 delete m_handler;
323 m_handler = new CANCommandHandler(this);
324 }
325 break;
326 case CEC_VENDOR_LG:
327 if (m_handler->GetVendorId() != CEC_VENDOR_LG)
328 {
329 delete m_handler;
330 m_handler = new CSLCommandHandler(this);
331 }
332 break;
333 case CEC_VENDOR_PANASONIC:
334 if (m_handler->GetVendorId() != CEC_VENDOR_PANASONIC)
335 {
336 delete m_handler;
337 m_handler = new CVLCommandHandler(this);
338 }
339 break;
340 default:
341 if (m_handler->GetVendorId() != CEC_VENDOR_UNKNOWN)
342 {
343 delete m_handler;
344 m_handler = new CCECCommandHandler(this);
345 }
346 break;
347 }
348
349 CStdString strLog;
350 strLog.Format("%s (%X): vendor = %s (%06x) class = %2x", GetLogicalAddressName(), m_iLogicalAddress, GetVendorName(), GetVendorId(), GetVendorClass());
351 m_processor->AddLog(CEC_LOG_DEBUG, strLog.c_str());
352 }
353 //@}
354
355 /** @name Transmit methods */
356 //@{
357 bool CCECBusDevice::TransmitActiveSource(void)
358 {
359 if (m_bActiveSource)
360 {
361 CStdString strLog;
362 strLog.Format("<< %s (%X) -> broadcast (F): active source (%4x)", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress);
363 AddLog(CEC_LOG_NOTICE, strLog);
364
365 cec_command command;
366 cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
367 command.parameters.push_back((uint8_t) ((m_iPhysicalAddress >> 8) & 0xFF));
368 command.parameters.push_back((uint8_t) (m_iPhysicalAddress & 0xFF));
369
370 return m_processor->Transmit(command);
371 }
372 else
373 {
374 CStdString strLog;
375 strLog.Format("<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress);
376 AddLog(CEC_LOG_DEBUG, strLog);
377 }
378
379 return false;
380 }
381
382 bool CCECBusDevice::TransmitActiveView(void)
383 {
384 CStdString strLog;
385 strLog.Format("<< %s (%X) -> broadcast (F): active view (%4x)", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress);
386 AddLog(CEC_LOG_NOTICE, strLog);
387
388 cec_command command;
389 cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
390 command.parameters.push_back((m_iPhysicalAddress >> 8) & 0xFF);
391 command.parameters.push_back(m_iPhysicalAddress & 0xFF);
392
393 return m_processor->Transmit(command);
394 }
395
396 bool CCECBusDevice::TransmitCECVersion(cec_logical_address dest)
397 {
398 CStdString strLog;
399 strLog.Format("<< %s (%X) -> %s (%X): cec version ", GetLogicalAddressName(), m_iLogicalAddress, CCECCommandHandler::ToString(dest), dest);
400 switch (m_cecVersion)
401 {
402 case CEC_VERSION_1_2:
403 strLog.append("1.2");
404 break;
405 case CEC_VERSION_1_2A:
406 strLog.append("1.2a");
407 break;
408 case CEC_VERSION_1_3:
409 strLog.append("1.3");
410 break;
411 case CEC_VERSION_1_3A:
412 strLog.append("1.3a");
413 break;
414 default:
415 strLog.append("unknown");
416 break;
417 }
418 AddLog(CEC_LOG_NOTICE, strLog);
419
420 cec_command command;
421 cec_command::format(command, m_iLogicalAddress, dest, CEC_OPCODE_CEC_VERSION);
422 command.parameters.push_back(m_cecVersion);
423
424 return m_processor->Transmit(command);
425 }
426
427 bool CCECBusDevice::TransmitDeckStatus(cec_logical_address dest)
428 {
429 // need to support opcodes play and deck control before doing anything with this
430 CStdString strLog;
431 strLog.Format("<< %s (%X) -> %s (%X): deck status feature abort", GetLogicalAddressName(), m_iLogicalAddress, CCECCommandHandler::ToString(dest), dest);
432 AddLog(CEC_LOG_NOTICE, strLog);
433
434 m_processor->TransmitAbort(dest, CEC_OPCODE_GIVE_DECK_STATUS);
435 return false;
436 }
437
438 bool CCECBusDevice::TransmitInactiveView(void)
439 {
440 CStdString strLog;
441 strLog.Format("<< %s (%X) -> broadcast (F): inactive view", GetLogicalAddressName(), m_iLogicalAddress);
442 AddLog(CEC_LOG_NOTICE, strLog);
443
444 cec_command command;
445 cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_INACTIVE_SOURCE);
446 command.parameters.push_back((m_iPhysicalAddress >> 8) & 0xFF);
447 command.parameters.push_back(m_iPhysicalAddress & 0xFF);
448
449 return m_processor->Transmit(command);
450 }
451
452 bool CCECBusDevice::TransmitMenuState(cec_logical_address dest)
453 {
454 CStdString strLog;
455 strLog.Format("<< %s (%X) -> %s (%X): ", GetLogicalAddressName(), m_iLogicalAddress, CCECCommandHandler::ToString(dest), dest);
456 if (m_bMenuActive)
457 strLog.append("menu active");
458 else
459 strLog.append("menu inactive");
460 AddLog(CEC_LOG_NOTICE, strLog);
461
462 cec_command command;
463 cec_command::format(command, m_iLogicalAddress, dest, CEC_OPCODE_MENU_STATUS);
464 command.parameters.push_back(m_bMenuActive ? (uint8_t) CEC_MENU_STATE_ACTIVATED : (uint8_t) CEC_MENU_STATE_DEACTIVATED);
465
466 return m_processor->Transmit(command);
467 }
468
469 bool CCECBusDevice::TransmitOSDName(cec_logical_address dest)
470 {
471 CStdString strLog;
472 strLog.Format("<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress, CCECCommandHandler::ToString(dest), dest, m_strDeviceName.c_str());
473 AddLog(CEC_LOG_NOTICE, strLog.c_str());
474
475 cec_command command;
476 cec_command::format(command, m_iLogicalAddress, dest, CEC_OPCODE_SET_OSD_NAME);
477 for (unsigned int iPtr = 0; iPtr < m_strDeviceName.length(); iPtr++)
478 command.parameters.push_back(m_strDeviceName.at(iPtr));
479
480 return m_processor->Transmit(command);
481 }
482
483 bool CCECBusDevice::TransmitOSDString(cec_logical_address dest, cec_display_control duration, const char *strMessage)
484 {
485 CStdString strLog;
486 strLog.Format("<< %s (%X) -> %s (%X): display OSD message '%s'", GetLogicalAddressName(), m_iLogicalAddress, CCECCommandHandler::ToString(dest), dest, strMessage);
487 AddLog(CEC_LOG_NOTICE, strLog.c_str());
488
489 cec_command command;
490 cec_command::format(command, m_iLogicalAddress, dest, CEC_OPCODE_SET_OSD_STRING);
491 command.parameters.push_back((uint8_t)duration);
492
493 unsigned int iLen = strlen(strMessage);
494 if (iLen > 13) iLen = 13;
495
496 for (unsigned int iPtr = 0; iPtr < iLen; iPtr++)
497 command.parameters.push_back(strMessage[iPtr]);
498
499 return m_processor->Transmit(command);
500 }
501
502 bool CCECBusDevice::TransmitPhysicalAddress(void)
503 {
504 CStdString strLog;
505 strLog.Format("<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress);
506 AddLog(CEC_LOG_NOTICE, strLog.c_str());
507
508 cec_command command;
509 cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
510 command.parameters.push_back((uint8_t) ((m_iPhysicalAddress >> 8) & 0xFF));
511 command.parameters.push_back((uint8_t) (m_iPhysicalAddress & 0xFF));
512 command.parameters.push_back((uint8_t) (m_type));
513
514 return m_processor->Transmit(command);
515 }
516
517 bool CCECBusDevice::TransmitPoll(cec_logical_address dest)
518 {
519 bool bReturn(false);
520
521 if (dest == CECDEVICE_UNKNOWN)
522 dest = m_iLogicalAddress;
523
524 CStdString strLog;
525 strLog.Format("<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress, CCECCommandHandler::ToString(dest), dest);
526 AddLog(CEC_LOG_NOTICE, strLog.c_str());
527
528 cec_command command;
529 cec_command::format(command, m_iLogicalAddress, dest, CEC_OPCODE_NONE);
530 CLockObject lock(&m_mutex);
531
532 bReturn = m_processor->Transmit(command);
533 AddLog(CEC_LOG_DEBUG, bReturn ? ">> POLL sent" : ">> POLL not sent");
534 return bReturn;
535 }
536
537 bool CCECBusDevice::TransmitPowerState(cec_logical_address dest)
538 {
539 CStdString strLog;
540 strLog.Format("<< %s (%X) -> %s (%X): ", GetLogicalAddressName(), m_iLogicalAddress, CCECCommandHandler::ToString(dest), dest);
541
542 switch (m_powerStatus)
543 {
544 case CEC_POWER_STATUS_ON:
545 strLog.append("powered on");
546 break;
547 case CEC_POWER_STATUS_STANDBY:
548 strLog.append("in standby mode");
549 break;
550 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY:
551 strLog.append("in transition from on to standby");
552 break;
553 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON:
554 strLog.append("in transition from standby to on");
555 break;
556 default:
557 strLog.append("power state unknown");
558 break;
559 }
560 AddLog(CEC_LOG_NOTICE, strLog.c_str());
561
562 strLog.Format("<< reporting power status '%d'", m_powerStatus);
563 AddLog(CEC_LOG_NOTICE, strLog);
564
565 cec_command command;
566 cec_command::format(command, m_iLogicalAddress, dest, CEC_OPCODE_REPORT_POWER_STATUS);
567 command.parameters.push_back((uint8_t) m_powerStatus);
568
569 return m_processor->Transmit(command);
570 }
571
572 bool CCECBusDevice::TransmitVendorID(cec_logical_address dest)
573 {
574 CStdString strLog;
575 strLog.Format("<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress, CCECCommandHandler::ToString(dest), dest);
576 AddLog(CEC_LOG_NOTICE, strLog);
577
578 m_processor->TransmitAbort(dest, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
579 return false;
580 }
581 //@}