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