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