break break break
[deb_libcec.git] / src / lib / devices / CECBusDevice.cpp
... / ...
CommitLineData
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 "../platform/timeutils.h"
39
40using namespace CEC;
41
42CCECBusDevice::CCECBusDevice(CCECProcessor *processor, cec_logical_address iLogicalAddress, uint16_t iPhysicalAddress) :
43 m_iPhysicalAddress(iPhysicalAddress),
44 m_iLogicalAddress(iLogicalAddress),
45 m_powerStatus(CEC_POWER_STATUS_UNKNOWN),
46 m_processor(processor),
47 m_iVendorClass(CEC_VENDOR_UNKNOWN),
48 m_iLastActive(0),
49 m_cecVersion(CEC_VERSION_UNKNOWN)
50{
51 m_handler = new CCECCommandHandler(this);
52 for (unsigned int iPtr = 0; iPtr < 4; iPtr++)
53 m_menuLanguage.language[iPtr] = '?';
54 m_menuLanguage.language[3] = 0;
55 m_menuLanguage.device = iLogicalAddress;
56 m_vendor.vendor = CEC_VENDOR_UNKNOWN;
57
58 switch(iLogicalAddress)
59 {
60 case CECDEVICE_AUDIOSYSTEM:
61 m_type = CEC_DEVICE_TYPE_AUDIO_SYSTEM;
62 m_strDeviceName = "Audio";
63 break;
64 case CECDEVICE_PLAYBACKDEVICE1:
65 case CECDEVICE_PLAYBACKDEVICE2:
66 case CECDEVICE_PLAYBACKDEVICE3:
67 m_type = CEC_DEVICE_TYPE_PLAYBACK_DEVICE;
68 m_strDeviceName = "Player";
69 break;
70 case CECDEVICE_RECORDINGDEVICE1:
71 case CECDEVICE_RECORDINGDEVICE2:
72 case CECDEVICE_RECORDINGDEVICE3:
73 m_type = CEC_DEVICE_TYPE_RECORDING_DEVICE;
74 m_strDeviceName = "Recorder";
75 break;
76 case CECDEVICE_TUNER1:
77 case CECDEVICE_TUNER2:
78 case CECDEVICE_TUNER3:
79 case CECDEVICE_TUNER4:
80 m_type = CEC_DEVICE_TYPE_TUNER;
81 m_strDeviceName = "Tuner";
82 break;
83 case CECDEVICE_TV:
84 m_type = CEC_DEVICE_TYPE_TV;
85 m_strDeviceName = "TV";
86 break;
87 default:
88 m_type = CEC_DEVICE_TYPE_RESERVED;
89 m_strDeviceName = "Unknown";
90 break;
91 }
92}
93
94CCECBusDevice::~CCECBusDevice(void)
95{
96 m_condition.Broadcast();
97 delete m_handler;
98}
99
100cec_logical_address CCECBusDevice::GetMyLogicalAddress(void) const
101{
102 return m_processor->GetLogicalAddress();
103}
104
105bool CCECBusDevice::MyLogicalAddressContains(cec_logical_address address) const
106{
107 return m_processor->HasLogicalAddress(address);
108}
109
110uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const
111{
112 return m_processor->GetPhysicalAddress();
113}
114
115void CCECBusDevice::AddLog(cec_log_level level, const CStdString &strMessage)
116{
117 m_processor->AddLog(level, strMessage);
118}
119
120void CCECBusDevice::SetMenuLanguage(const cec_menu_language &language)
121{
122 if (language.device == m_iLogicalAddress)
123 {
124 CStdString strLog;
125 strLog.Format("device %d menu language set to '%s'", m_iLogicalAddress, language.language);
126 m_processor->AddLog(CEC_LOG_DEBUG, strLog);
127 m_menuLanguage = language;
128 }
129}
130
131void CCECBusDevice::SetCecVersion(const cec_version newVersion)
132{
133 CStdString strLog;
134 m_cecVersion = newVersion;
135
136 switch (newVersion)
137 {
138 case CEC_VERSION_1_2:
139 strLog.Format("device %d reports CEC version 1.2", m_iLogicalAddress);
140 break;
141 case CEC_VERSION_1_2A:
142 strLog.Format("device %d reports CEC version 1.2a", m_iLogicalAddress);
143 break;
144 case CEC_VERSION_1_3:
145 strLog.Format("device %d reports CEC version 1.3", m_iLogicalAddress);
146 break;
147 case CEC_VERSION_1_3A:
148 strLog.Format("device %d reports CEC version 1.3a", m_iLogicalAddress);
149 break;
150 default:
151 strLog.Format("device %d reports an unknown CEC version", m_iLogicalAddress);
152 m_cecVersion = CEC_VERSION_UNKNOWN;
153 break;
154 }
155 AddLog(CEC_LOG_DEBUG, strLog);
156}
157
158void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus)
159{
160 if (m_powerStatus != powerStatus)
161 {
162 CStdString strLog;
163 strLog.Format("device %d power status changed from %2x to %2x", m_iLogicalAddress, m_powerStatus, powerStatus);
164 m_processor->AddLog(CEC_LOG_DEBUG, strLog);
165 m_powerStatus = powerStatus;
166 }
167}
168
169void CCECBusDevice::SetVendorId(const cec_datapacket &data)
170{
171 if (data.size < 3)
172 {
173 AddLog(CEC_LOG_WARNING, "invalid vendor ID received");
174 return;
175 }
176
177 uint64_t iVendorId = ((uint64_t)data[0] << 3) +
178 ((uint64_t)data[1] << 2) +
179 (uint64_t)data[2];
180
181 SetVendorId(iVendorId, data.size >= 4 ? data[3] : 0);
182}
183
184void CCECBusDevice::SetVendorId(uint64_t iVendorId, uint8_t iVendorClass /* = 0 */)
185{
186 m_vendor.vendor = (cec_vendor_id)iVendorId;
187 m_iVendorClass = iVendorClass;
188
189 switch (iVendorId)
190 {
191 case CEC_VENDOR_SAMSUNG:
192 if (m_handler->GetVendorId() != CEC_VENDOR_SAMSUNG)
193 {
194 delete m_handler;
195 m_handler = new CANCommandHandler(this);
196 }
197 break;
198 case CEC_VENDOR_LG:
199 if (m_handler->GetVendorId() != CEC_VENDOR_LG)
200 {
201 delete m_handler;
202 m_handler = new CSLCommandHandler(this);
203 }
204 break;
205 default:
206 if (m_handler->GetVendorId() != CEC_VENDOR_UNKNOWN)
207 {
208 delete m_handler;
209 m_handler = new CCECCommandHandler(this);
210 }
211 break;
212 }
213
214 CStdString strLog;
215 strLog.Format("device %d: vendor = %s (%06x) class = %2x", m_iLogicalAddress, GetVendorName(), GetVendorId(), GetVendorClass());
216 m_processor->AddLog(CEC_LOG_DEBUG, strLog.c_str());
217}
218
219bool CCECBusDevice::HandleCommand(const cec_command &command)
220{
221 CLockObject lock(&m_mutex);
222 m_iLastActive = GetTimeMs();
223 m_handler->HandleCommand(command);
224 m_condition.Signal();
225 return true;
226}
227
228const cec_vendor &CCECBusDevice::GetVendor(void)
229{
230 if (m_vendor.vendor == CEC_VENDOR_UNKNOWN)
231 {
232 AddLog(CEC_LOG_NOTICE, "<< requesting vendor ID");
233 cec_command command;
234 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
235 CLockObject lock(&m_mutex);
236
237 if (m_processor->Transmit(command))
238 m_condition.Wait(&m_mutex, 1000);
239 }
240
241 return m_vendor;
242}
243
244void CCECBusDevice::PollVendorId(void)
245{
246 CLockObject lock(&m_mutex);
247 if (m_iLastActive > 0 && m_iLogicalAddress != CECDEVICE_BROADCAST &&
248 m_vendor.vendor == CEC_VENDOR_UNKNOWN &&
249 GetTimeMs() - m_iLastActive > 5000)
250 {
251 m_iLastActive = GetTimeMs();
252
253 cec_command command;
254 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
255 if (m_processor->Transmit(command))
256 m_condition.Wait(&m_mutex, 1000);
257 }
258}
259
260void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress, uint16_t iOldAddress /* = 0 */)
261{
262 if (iNewAddress > 0)
263 {
264 CStdString strLog;
265 strLog.Format(">> %i changed physical address from %04x to %04x", m_iLogicalAddress, m_iPhysicalAddress, iNewAddress);
266 AddLog(CEC_LOG_DEBUG, strLog.c_str());
267
268 m_iPhysicalAddress = iNewAddress;
269 }
270}
271
272bool CCECBusDevice::PowerOn(void)
273{
274 CStdString strLog;
275 strLog.Format("<< powering on device with logical address %d", (int8_t)m_iLogicalAddress);
276 AddLog(CEC_LOG_DEBUG, strLog.c_str());
277
278 cec_command command;
279 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_IMAGE_VIEW_ON);
280
281 return m_processor->Transmit(command);
282}
283
284bool CCECBusDevice::Standby(void)
285{
286 CStdString strLog;
287 strLog.Format("<< putting device with logical address %d in standby mode", (int8_t)m_iLogicalAddress);
288 AddLog(CEC_LOG_DEBUG, strLog.c_str());
289
290 cec_command command;
291 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_STANDBY);
292
293 return m_processor->Transmit(command);
294}
295
296bool CCECBusDevice::SetOSDString(cec_display_control duration, const char *strMessage)
297{
298 CStdString strLog;
299 strLog.Format("<< display message '%s'", strMessage);
300 AddLog(CEC_LOG_NOTICE, strLog.c_str());
301
302 cec_command command;
303 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_SET_OSD_STRING);
304 command.parameters.push_back((uint8_t)duration);
305
306 for (unsigned int iPtr = 0; iPtr < strlen(strMessage); iPtr++)
307 command.parameters.push_back(strMessage[iPtr]);
308
309 return m_processor->Transmit(command);
310}
311
312bool CCECBusDevice::ReportCECVersion(cec_logical_address dest)
313{
314 AddLog(CEC_LOG_NOTICE, "<< reporting CEC version as 1.3a");
315
316 cec_command command;
317 cec_command::format(command, m_iLogicalAddress, dest, CEC_OPCODE_CEC_VERSION);
318 command.parameters.push_back(CEC_VERSION_1_3A);
319
320 return m_processor->Transmit(command);
321}
322
323bool CCECBusDevice::ReportDeckStatus(cec_logical_address dest)
324{
325 // need to support opcodes play and deck control before doing anything with this
326 AddLog(CEC_LOG_NOTICE, "<< deck status requested, feature abort");
327 m_processor->TransmitAbort(dest, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
328 return false;
329}
330
331bool CCECBusDevice::ReportMenuState(cec_logical_address dest, bool bActive /* = true */)
332{
333 if (bActive)
334 AddLog(CEC_LOG_NOTICE, "<< reporting menu state as active");
335 else
336 AddLog(CEC_LOG_NOTICE, "<< reporting menu state as inactive");
337
338 cec_command command;
339 cec_command::format(command, m_iLogicalAddress, dest, CEC_OPCODE_MENU_STATUS);
340 command.parameters.push_back(bActive ? (uint8_t) CEC_MENU_STATE_ACTIVATED : (uint8_t) CEC_MENU_STATE_DEACTIVATED);
341
342 return m_processor->Transmit(command);
343}
344
345bool CCECBusDevice::ReportOSDName(cec_logical_address dest)
346{
347 CStdString strLog;
348 strLog.Format("<< reporting OSD name as %s", m_strDeviceName.c_str());
349 AddLog(CEC_LOG_NOTICE, strLog.c_str());
350
351 cec_command command;
352 cec_command::format(command, m_iLogicalAddress, dest, CEC_OPCODE_SET_OSD_NAME);
353 for (unsigned int iPtr = 0; iPtr < m_strDeviceName.length(); iPtr++)
354 command.parameters.push_back(m_strDeviceName.at(iPtr));
355
356 return m_processor->Transmit(command);
357}
358
359bool CCECBusDevice::ReportPowerState(cec_logical_address dest)
360{
361 AddLog(CEC_LOG_NOTICE, "<< reporting \"On\" power status");
362
363 cec_command command;
364 cec_command::format(command, m_iLogicalAddress, dest, CEC_OPCODE_REPORT_POWER_STATUS);
365// command.parameters.push_back(bOn ? (uint8_t) CEC_POWER_STATUS_ON : (uint8_t) CEC_POWER_STATUS_STANDBY);
366 command.parameters.push_back((uint8_t) CEC_POWER_STATUS_ON);
367
368 return m_processor->Transmit(command);
369}
370
371bool CCECBusDevice::ReportVendorID(cec_logical_address dest)
372{
373 AddLog(CEC_LOG_NOTICE, "<< vendor ID requested, feature abort");
374 m_processor->TransmitAbort(dest, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
375 return false;
376}
377
378bool CCECBusDevice::BroadcastActiveView(void)
379{
380 AddLog(CEC_LOG_DEBUG, "<< setting active view");
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
390bool CCECBusDevice::BroadcastInactiveView(void)
391{
392 AddLog(CEC_LOG_DEBUG, "<< setting inactive view");
393
394 cec_command command;
395 cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_INACTIVE_SOURCE);
396 command.parameters.push_back((m_iPhysicalAddress >> 8) & 0xFF);
397 command.parameters.push_back(m_iPhysicalAddress & 0xFF);
398
399 return m_processor->Transmit(command);
400}
401
402bool CCECBusDevice::BroadcastPhysicalAddress(void)
403{
404 CStdString strLog;
405 strLog.Format("<< reporting physical address as %04x", m_iPhysicalAddress);
406 AddLog(CEC_LOG_NOTICE, strLog.c_str());
407
408 cec_command command;
409 cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
410 command.parameters.push_back((uint8_t) ((m_iPhysicalAddress >> 8) & 0xFF));
411 command.parameters.push_back((uint8_t) (m_iPhysicalAddress & 0xFF));
412 command.parameters.push_back((uint8_t) (CEC_DEVICE_TYPE_PLAYBACK_DEVICE));
413
414 return m_processor->Transmit(command);
415}
416
417bool CCECBusDevice::BroadcastActiveSource(void)
418{
419 AddLog(CEC_LOG_NOTICE, "<< broadcasting active source");
420
421 cec_command command;
422 cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
423 command.parameters.push_back((uint8_t) ((m_iPhysicalAddress >> 8) & 0xFF));
424 command.parameters.push_back((uint8_t) (m_iPhysicalAddress & 0xFF));
425
426 return m_processor->Transmit(command);
427}
428
429cec_version CCECBusDevice::GetCecVersion(bool bRefresh /* = true */)
430{
431 if (bRefresh || m_cecVersion == CEC_VERSION_UNKNOWN)
432 {
433 AddLog(CEC_LOG_NOTICE, "<< requesting CEC version");
434 cec_command command;
435 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GET_CEC_VERSION);
436 CLockObject lock(&m_mutex);
437 if (m_processor->Transmit(command))
438 m_condition.Wait(&m_mutex, 1000);
439 }
440
441 return m_cecVersion;
442}
443
444cec_menu_language &CCECBusDevice::GetMenuLanguage(bool bRefresh /* = true */)
445{
446 if (bRefresh || !strcmp(m_menuLanguage.language, "???"))
447 {
448 AddLog(CEC_LOG_NOTICE, "<< requesting menu language");
449 cec_command command;
450 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GET_MENU_LANGUAGE);
451 CLockObject lock(&m_mutex);
452 if (m_processor->Transmit(command))
453 m_condition.Wait(&m_mutex, 1000);
454 }
455
456 return m_menuLanguage;
457}
458
459cec_power_status CCECBusDevice::GetPowerStatus(bool bRefresh /* = true */)
460{
461 if (bRefresh || m_powerStatus == CEC_POWER_STATUS_UNKNOWN)
462 {
463 AddLog(CEC_LOG_NOTICE, "<< requesting power status");
464 cec_command command;
465 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS);
466 CLockObject lock(&m_mutex);
467 if (m_processor->Transmit(command))
468 m_condition.Wait(&m_mutex, 1000);
469 }
470
471 return m_powerStatus;
472}
473
474bool CCECBusDevice::PollDevice(cec_logical_address source /* = CECDEVICE_UNKNOWN */)
475{
476 bool bReturn(false);
477
478 if (source == CECDEVICE_UNKNOWN)
479 source = GetMyLogicalAddress();
480
481 CStdString strLog;
482 strLog.Format("<< sending POLL from device %1x to device %1x", (int8_t)source, m_iLogicalAddress);
483 AddLog(CEC_LOG_DEBUG, strLog);
484
485 cec_command command;
486 cec_command::format(command, source, m_iLogicalAddress, CEC_OPCODE_NONE);
487 CLockObject lock(&m_mutex);
488
489 bReturn = m_processor->Transmit(command);
490 AddLog(CEC_LOG_DEBUG, bReturn ? ">> POLL sent" : ">> POLL not sent");
491 return bReturn;
492}