cec: cosmetics
[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 "../platform/timeutils.h"
39
40 using namespace CEC;
41
42 CCECBusDevice::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_bMenuActive(true),
48 m_iVendorClass(CEC_VENDOR_UNKNOWN),
49 m_iLastActive(0),
50 m_cecVersion(CEC_VERSION_1_3A)
51 {
52 m_handler = new CCECCommandHandler(this);
53
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;
58
59 m_vendor.vendor = CEC_VENDOR_UNKNOWN;
60 m_type = CEC_DEVICE_TYPE_RESERVED;
61 m_strDeviceName = "Unknown";
62 }
63
64 CCECBusDevice::~CCECBusDevice(void)
65 {
66 m_condition.Broadcast();
67 delete m_handler;
68 }
69
70 void CCECBusDevice::AddLog(cec_log_level level, const CStdString &strMessage)
71 {
72 m_processor->AddLog(level, strMessage);
73 }
74
75 bool CCECBusDevice::HandleCommand(const cec_command &command)
76 {
77 CLockObject lock(&m_mutex);
78 m_iLastActive = GetTimeMs();
79 m_handler->HandleCommand(command);
80 m_condition.Signal();
81 return true;
82 }
83
84 void 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
100 bool 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
112 bool 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 //@{
126 cec_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
141 cec_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
156 cec_logical_address CCECBusDevice::GetMyLogicalAddress(void) const
157 {
158 return m_processor->GetLogicalAddress();
159 }
160
161 uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const
162 {
163 return m_processor->GetPhysicalAddress();
164 }
165
166 cec_power_status CCECBusDevice::GetPowerStatus(bool bRefresh /* = true */)
167 {
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;
179 }
180
181 const cec_vendor &CCECBusDevice::GetVendor(void)
182 {
183 if (m_vendor.vendor == CEC_VENDOR_UNKNOWN)
184 {
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);
192 }
193
194 return m_vendor;
195 }
196
197 bool CCECBusDevice::MyLogicalAddressContains(cec_logical_address address) const
198 {
199 return m_processor->HasLogicalAddress(address);
200 }
201
202 //@}
203
204 /** @name Setters */
205 //@{
206 void CCECBusDevice::SetCecVersion(const cec_version newVersion)
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
233 void 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
244 bool 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
263 void 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
275 void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus)
276 {
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 }
284 }
285
286 void 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
301 void CCECBusDevice::SetVendorId(uint64_t iVendorId, uint8_t iVendorClass /* = 0 */)
302 {
303 m_vendor.vendor = (cec_vendor_id)iVendorId;
304 m_iVendorClass = iVendorClass;
305
306 switch (iVendorId)
307 {
308 case CEC_VENDOR_SAMSUNG:
309 if (m_handler->GetVendorId() != CEC_VENDOR_SAMSUNG)
310 {
311 delete m_handler;
312 m_handler = new CANCommandHandler(this);
313 }
314 break;
315 case CEC_VENDOR_LG:
316 if (m_handler->GetVendorId() != CEC_VENDOR_LG)
317 {
318 delete m_handler;
319 m_handler = new CSLCommandHandler(this);
320 }
321 break;
322 default:
323 if (m_handler->GetVendorId() != CEC_VENDOR_UNKNOWN)
324 {
325 delete m_handler;
326 m_handler = new CCECCommandHandler(this);
327 }
328 break;
329 }
330
331 CStdString strLog;
332 strLog.Format("device %d: vendor = %s (%06x) class = %2x", m_iLogicalAddress, GetVendorName(), GetVendorId(), GetVendorClass());
333 m_processor->AddLog(CEC_LOG_DEBUG, strLog.c_str());
334 }
335 //@}
336
337 /** @name Transmit methods */
338 //@{
339 bool CCECBusDevice::TransmitActiveSource(void)
340 {
341 AddLog(CEC_LOG_NOTICE, "<< broadcasting active source");
342
343 cec_command command;
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));
347
348 return m_processor->Transmit(command);
349 }
350
351 bool CCECBusDevice::TransmitActiveView(void)
352 {
353 AddLog(CEC_LOG_DEBUG, "<< setting active view");
354
355 cec_command command;
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);
359
360 return m_processor->Transmit(command);
361 }
362
363 bool CCECBusDevice::TransmitCECVersion(cec_logical_address dest)
364 {
365 AddLog(CEC_LOG_NOTICE, "<< reporting CEC version as 1.3a");
366
367 cec_command command;
368 cec_command::format(command, m_iLogicalAddress, dest, CEC_OPCODE_CEC_VERSION);
369 command.parameters.push_back(m_cecVersion);
370
371 return m_processor->Transmit(command);
372 }
373
374 bool CCECBusDevice::TransmitDeckStatus(cec_logical_address dest)
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");
378 m_processor->TransmitAbort(dest, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
379 return false;
380 }
381
382 bool 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
394 bool CCECBusDevice::TransmitMenuState(cec_logical_address dest)
395 {
396 if (m_bMenuActive)
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;
402 cec_command::format(command, m_iLogicalAddress, dest, CEC_OPCODE_MENU_STATUS);
403 command.parameters.push_back(m_bMenuActive ? (uint8_t) CEC_MENU_STATE_ACTIVATED : (uint8_t) CEC_MENU_STATE_DEACTIVATED);
404
405 return m_processor->Transmit(command);
406 }
407
408 bool CCECBusDevice::TransmitOSDName(cec_logical_address dest)
409 {
410 CStdString strLog;
411 strLog.Format("<< reporting OSD name as %s", m_strDeviceName.c_str());
412 AddLog(CEC_LOG_NOTICE, strLog.c_str());
413
414 cec_command command;
415 cec_command::format(command, m_iLogicalAddress, dest, CEC_OPCODE_SET_OSD_NAME);
416 for (unsigned int iPtr = 0; iPtr < m_strDeviceName.length(); iPtr++)
417 command.parameters.push_back(m_strDeviceName.at(iPtr));
418
419 return m_processor->Transmit(command);
420 }
421
422 bool CCECBusDevice::TransmitPhysicalAddress(void)
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;
429 cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
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
437 bool CCECBusDevice::TransmitPoll(cec_logical_address dest)
438 {
439 bool bReturn(false);
440
441 if (dest == CECDEVICE_UNKNOWN)
442 dest = m_iLogicalAddress;
443
444 CStdString strLog;
445 strLog.Format("<< sending POLL from device %1x to device %1x", m_iLogicalAddress, (int8_t)dest);
446 AddLog(CEC_LOG_DEBUG, strLog);
447
448 cec_command command;
449 cec_command::format(command, m_iLogicalAddress, dest, CEC_OPCODE_NONE);
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 }
456
457 bool 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
470 bool 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 //@}