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