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