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