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