break break break
[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;
1fcf5a3f
LOK
57
58 switch(iLogicalAddress)
59 {
60 case CECDEVICE_AUDIOSYSTEM:
61 m_type = CEC_DEVICE_TYPE_AUDIO_SYSTEM;
62 m_strDeviceName = "Audio";
2e8207ce 63 break;
1fcf5a3f
LOK
64 case CECDEVICE_PLAYBACKDEVICE1:
65 case CECDEVICE_PLAYBACKDEVICE2:
66 case CECDEVICE_PLAYBACKDEVICE3:
67 m_type = CEC_DEVICE_TYPE_PLAYBACK_DEVICE;
68 m_strDeviceName = "Player";
2e8207ce 69 break;
1fcf5a3f
LOK
70 case CECDEVICE_RECORDINGDEVICE1:
71 case CECDEVICE_RECORDINGDEVICE2:
72 case CECDEVICE_RECORDINGDEVICE3:
73 m_type = CEC_DEVICE_TYPE_RECORDING_DEVICE;
74 m_strDeviceName = "Recorder";
2e8207ce 75 break;
1fcf5a3f
LOK
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";
2e8207ce 82 break;
1fcf5a3f
LOK
83 case CECDEVICE_TV:
84 m_type = CEC_DEVICE_TYPE_TV;
85 m_strDeviceName = "TV";
2e8207ce 86 break;
1fcf5a3f
LOK
87 default:
88 m_type = CEC_DEVICE_TYPE_RESERVED;
89 m_strDeviceName = "Unknown";
2e8207ce 90 break;
1fcf5a3f 91 }
e9de9629
LOK
92}
93
94CCECBusDevice::~CCECBusDevice(void)
95{
6a1c0009 96 m_condition.Broadcast();
e9de9629
LOK
97 delete m_handler;
98}
99
100cec_logical_address CCECBusDevice::GetMyLogicalAddress(void) const
101{
102 return m_processor->GetLogicalAddress();
103}
104
f8513317
LOK
105bool CCECBusDevice::MyLogicalAddressContains(cec_logical_address address) const
106{
107 return m_processor->HasLogicalAddress(address);
108}
109
e9de9629
LOK
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
a3269a0a
LOK
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
e55f3f70 131void CCECBusDevice::SetCecVersion(const cec_version newVersion)
6a1c0009
LOK
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
e55f3f70
LOK
158void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus)
159{
b0271d54
LOK
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 }
e55f3f70
LOK
167}
168
0f23c85c
LOK
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
2cd8b5d0 184void CCECBusDevice::SetVendorId(uint64_t iVendorId, uint8_t iVendorClass /* = 0 */)
e9de9629 185{
0500da96 186 m_vendor.vendor = (cec_vendor_id)iVendorId;
e9de9629
LOK
187 m_iVendorClass = iVendorClass;
188
189 switch (iVendorId)
190 {
191 case CEC_VENDOR_SAMSUNG:
0ab58650
LOK
192 if (m_handler->GetVendorId() != CEC_VENDOR_SAMSUNG)
193 {
194 delete m_handler;
195 m_handler = new CANCommandHandler(this);
196 }
e9de9629
LOK
197 break;
198 case CEC_VENDOR_LG:
0ab58650
LOK
199 if (m_handler->GetVendorId() != CEC_VENDOR_LG)
200 {
201 delete m_handler;
202 m_handler = new CSLCommandHandler(this);
203 }
e9de9629
LOK
204 break;
205 default:
0ab58650
LOK
206 if (m_handler->GetVendorId() != CEC_VENDOR_UNKNOWN)
207 {
208 delete m_handler;
209 m_handler = new CCECCommandHandler(this);
210 }
e9de9629
LOK
211 break;
212 }
213
214 CStdString strLog;
0f23c85c 215 strLog.Format("device %d: vendor = %s (%06x) class = %2x", m_iLogicalAddress, GetVendorName(), GetVendorId(), GetVendorClass());
e9de9629
LOK
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);
0ab58650 222 m_iLastActive = GetTimeMs();
e9de9629 223 m_handler->HandleCommand(command);
6a1c0009 224 m_condition.Signal();
e9de9629
LOK
225 return true;
226}
227
0500da96 228const cec_vendor &CCECBusDevice::GetVendor(void)
44c74256 229{
0500da96 230 if (m_vendor.vendor == CEC_VENDOR_UNKNOWN)
44c74256
LOK
231 {
232 AddLog(CEC_LOG_NOTICE, "<< requesting vendor ID");
233 cec_command command;
f8513317 234 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
44c74256
LOK
235 CLockObject lock(&m_mutex);
236
237 if (m_processor->Transmit(command))
238 m_condition.Wait(&m_mutex, 1000);
239 }
240
0500da96 241 return m_vendor;
44c74256
LOK
242}
243
0ab58650
LOK
244void CCECBusDevice::PollVendorId(void)
245{
246 CLockObject lock(&m_mutex);
0f23c85c 247 if (m_iLastActive > 0 && m_iLogicalAddress != CECDEVICE_BROADCAST &&
0500da96 248 m_vendor.vendor == CEC_VENDOR_UNKNOWN &&
0ab58650
LOK
249 GetTimeMs() - m_iLastActive > 5000)
250 {
251 m_iLastActive = GetTimeMs();
252
253 cec_command command;
f8513317 254 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
a9e03a86
LOK
255 if (m_processor->Transmit(command))
256 m_condition.Wait(&m_mutex, 1000);
0ab58650
LOK
257 }
258}
259
4e4be5cf
LOK
260void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress, uint16_t iOldAddress /* = 0 */)
261{
5eb9e3af
LOK
262 if (iNewAddress > 0)
263 {
264 CStdString strLog;
f8513317 265 strLog.Format(">> %i changed physical address from %04x to %04x", m_iLogicalAddress, m_iPhysicalAddress, iNewAddress);
5eb9e3af 266 AddLog(CEC_LOG_DEBUG, strLog.c_str());
4e4be5cf 267
5eb9e3af
LOK
268 m_iPhysicalAddress = iNewAddress;
269 }
4e4be5cf
LOK
270}
271
0f23c85c
LOK
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
f8513317 312bool CCECBusDevice::ReportCECVersion(cec_logical_address dest)
0f23c85c
LOK
313{
314 AddLog(CEC_LOG_NOTICE, "<< reporting CEC version as 1.3a");
315
316 cec_command command;
f8513317 317 cec_command::format(command, m_iLogicalAddress, dest, CEC_OPCODE_CEC_VERSION);
0f23c85c
LOK
318 command.parameters.push_back(CEC_VERSION_1_3A);
319
320 return m_processor->Transmit(command);
321}
322
f8513317 323bool CCECBusDevice::ReportDeckStatus(cec_logical_address dest)
0f23c85c
LOK
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");
f8513317 327 m_processor->TransmitAbort(dest, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
0f23c85c
LOK
328 return false;
329}
330
f8513317 331bool CCECBusDevice::ReportMenuState(cec_logical_address dest, bool bActive /* = true */)
0f23c85c
LOK
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;
f8513317 339 cec_command::format(command, m_iLogicalAddress, dest, CEC_OPCODE_MENU_STATUS);
0f23c85c
LOK
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
f8513317 345bool CCECBusDevice::ReportOSDName(cec_logical_address dest)
0f23c85c 346{
0f23c85c 347 CStdString strLog;
787a3cb8 348 strLog.Format("<< reporting OSD name as %s", m_strDeviceName.c_str());
0f23c85c
LOK
349 AddLog(CEC_LOG_NOTICE, strLog.c_str());
350
351 cec_command command;
f8513317 352 cec_command::format(command, m_iLogicalAddress, dest, CEC_OPCODE_SET_OSD_NAME);
787a3cb8
LOK
353 for (unsigned int iPtr = 0; iPtr < m_strDeviceName.length(); iPtr++)
354 command.parameters.push_back(m_strDeviceName.at(iPtr));
0f23c85c
LOK
355
356 return m_processor->Transmit(command);
357}
358
f8513317 359bool CCECBusDevice::ReportPowerState(cec_logical_address dest)
0f23c85c 360{
f8513317 361 AddLog(CEC_LOG_NOTICE, "<< reporting \"On\" power status");
0f23c85c
LOK
362
363 cec_command command;
f8513317
LOK
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);
0f23c85c
LOK
367
368 return m_processor->Transmit(command);
369}
370
f8513317 371bool CCECBusDevice::ReportVendorID(cec_logical_address dest)
0f23c85c
LOK
372{
373 AddLog(CEC_LOG_NOTICE, "<< vendor ID requested, feature abort");
f8513317 374 m_processor->TransmitAbort(dest, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
0f23c85c
LOK
375 return false;
376}
377
378bool CCECBusDevice::BroadcastActiveView(void)
379{
380 AddLog(CEC_LOG_DEBUG, "<< setting active view");
381
382 cec_command command;
f8513317 383 cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
0f23c85c
LOK
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;
f8513317 395 cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_INACTIVE_SOURCE);
0f23c85c
LOK
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;
f8513317 409 cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
0f23c85c
LOK
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;
f8513317 422 cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
0f23c85c
LOK
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
8acf4aac 429cec_version CCECBusDevice::GetCecVersion(bool bRefresh /* = true */)
6a1c0009 430{
8acf4aac 431 if (bRefresh || m_cecVersion == CEC_VERSION_UNKNOWN)
6a1c0009
LOK
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
8acf4aac 444cec_menu_language &CCECBusDevice::GetMenuLanguage(bool bRefresh /* = true */)
a3269a0a 445{
8acf4aac 446 if (bRefresh || !strcmp(m_menuLanguage.language, "???"))
a3269a0a
LOK
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
8acf4aac 459cec_power_status CCECBusDevice::GetPowerStatus(bool bRefresh /* = true */)
e55f3f70 460{
8acf4aac 461 if (bRefresh || m_powerStatus == CEC_POWER_STATUS_UNKNOWN)
e55f3f70
LOK
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}
57f45e6c 473
f8513317 474bool CCECBusDevice::PollDevice(cec_logical_address source /* = CECDEVICE_UNKNOWN */)
57f45e6c
LOK
475{
476 bool bReturn(false);
477
f8513317
LOK
478 if (source == CECDEVICE_UNKNOWN)
479 source = GetMyLogicalAddress();
480
57f45e6c 481 CStdString strLog;
f8513317 482 strLog.Format("<< sending POLL from device %1x to device %1x", (int8_t)source, m_iLogicalAddress);
57f45e6c
LOK
483 AddLog(CEC_LOG_DEBUG, strLog);
484
485 cec_command command;
f8513317 486 cec_command::format(command, source, m_iLogicalAddress, CEC_OPCODE_NONE);
57f45e6c
LOK
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}