cec: add GetDeviceCecVersion()/cec_get_device_cec_version() to the interface. mostly...
[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),
45 m_processor(processor),
46 m_iVendorId(0),
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);
52}
53
54CCECBusDevice::~CCECBusDevice(void)
55{
6a1c0009 56 m_condition.Broadcast();
e9de9629
LOK
57 delete m_handler;
58}
59
60cec_logical_address CCECBusDevice::GetMyLogicalAddress(void) const
61{
62 return m_processor->GetLogicalAddress();
63}
64
65uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const
66{
67 return m_processor->GetPhysicalAddress();
68}
69
70void CCECBusDevice::AddLog(cec_log_level level, const CStdString &strMessage)
71{
72 m_processor->AddLog(level, strMessage);
73}
74
6a1c0009
LOK
75void CCECBusDevice::SetCecVersion(cec_version newVersion)
76{
77 CStdString strLog;
78 m_cecVersion = newVersion;
79
80 switch (newVersion)
81 {
82 case CEC_VERSION_1_2:
83 strLog.Format("device %d reports CEC version 1.2", m_iLogicalAddress);
84 break;
85 case CEC_VERSION_1_2A:
86 strLog.Format("device %d reports CEC version 1.2a", m_iLogicalAddress);
87 break;
88 case CEC_VERSION_1_3:
89 strLog.Format("device %d reports CEC version 1.3", m_iLogicalAddress);
90 break;
91 case CEC_VERSION_1_3A:
92 strLog.Format("device %d reports CEC version 1.3a", m_iLogicalAddress);
93 break;
94 default:
95 strLog.Format("device %d reports an unknown CEC version", m_iLogicalAddress);
96 m_cecVersion = CEC_VERSION_UNKNOWN;
97 break;
98 }
99 AddLog(CEC_LOG_DEBUG, strLog);
100}
101
0f23c85c
LOK
102void CCECBusDevice::SetVendorId(const cec_datapacket &data)
103{
104 if (data.size < 3)
105 {
106 AddLog(CEC_LOG_WARNING, "invalid vendor ID received");
107 return;
108 }
109
110 uint64_t iVendorId = ((uint64_t)data[0] << 3) +
111 ((uint64_t)data[1] << 2) +
112 (uint64_t)data[2];
113
114 SetVendorId(iVendorId, data.size >= 4 ? data[3] : 0);
115}
116
2cd8b5d0 117void CCECBusDevice::SetVendorId(uint64_t iVendorId, uint8_t iVendorClass /* = 0 */)
e9de9629 118{
e9de9629
LOK
119 m_iVendorId = iVendorId;
120 m_iVendorClass = iVendorClass;
121
122 switch (iVendorId)
123 {
124 case CEC_VENDOR_SAMSUNG:
0ab58650
LOK
125 if (m_handler->GetVendorId() != CEC_VENDOR_SAMSUNG)
126 {
127 delete m_handler;
128 m_handler = new CANCommandHandler(this);
129 }
e9de9629
LOK
130 break;
131 case CEC_VENDOR_LG:
0ab58650
LOK
132 if (m_handler->GetVendorId() != CEC_VENDOR_LG)
133 {
134 delete m_handler;
135 m_handler = new CSLCommandHandler(this);
136 }
e9de9629
LOK
137 break;
138 default:
0ab58650
LOK
139 if (m_handler->GetVendorId() != CEC_VENDOR_UNKNOWN)
140 {
141 delete m_handler;
142 m_handler = new CCECCommandHandler(this);
143 }
e9de9629
LOK
144 break;
145 }
146
147 CStdString strLog;
0f23c85c 148 strLog.Format("device %d: vendor = %s (%06x) class = %2x", m_iLogicalAddress, GetVendorName(), GetVendorId(), GetVendorClass());
e9de9629
LOK
149 m_processor->AddLog(CEC_LOG_DEBUG, strLog.c_str());
150}
151
152bool CCECBusDevice::HandleCommand(const cec_command &command)
153{
154 CLockObject lock(&m_mutex);
0ab58650 155 m_iLastActive = GetTimeMs();
e9de9629 156 m_handler->HandleCommand(command);
6a1c0009 157 m_condition.Signal();
e9de9629
LOK
158 return true;
159}
160
0ab58650
LOK
161void CCECBusDevice::PollVendorId(void)
162{
163 CLockObject lock(&m_mutex);
0f23c85c
LOK
164 if (m_iLastActive > 0 && m_iLogicalAddress != CECDEVICE_BROADCAST &&
165 m_iVendorId == CEC_VENDOR_UNKNOWN &&
0ab58650
LOK
166 GetTimeMs() - m_iLastActive > 5000)
167 {
168 m_iLastActive = GetTimeMs();
169
170 cec_command command;
171 cec_command::format(command, GetMyLogicalAddress(), GetLogicalAddress(), CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
8d84e2c0
LOK
172 command.ack_timeout = 0;
173 m_processor->Transmit(command);
0ab58650
LOK
174 }
175}
176
4e4be5cf
LOK
177void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress, uint16_t iOldAddress /* = 0 */)
178{
179 CStdString strLog;
180 strLog.Format(">> %i changed physical address from %04x to %04x", GetLogicalAddress(), m_iPhysicalAddress, iNewAddress);
181 AddLog(CEC_LOG_DEBUG, strLog.c_str());
182
183 m_iPhysicalAddress = iNewAddress;
184}
185
0f23c85c
LOK
186bool CCECBusDevice::PowerOn(void)
187{
188 CStdString strLog;
189 strLog.Format("<< powering on device with logical address %d", (int8_t)m_iLogicalAddress);
190 AddLog(CEC_LOG_DEBUG, strLog.c_str());
191
192 cec_command command;
193 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_IMAGE_VIEW_ON);
194
195 return m_processor->Transmit(command);
196}
197
198bool CCECBusDevice::Standby(void)
199{
200 CStdString strLog;
201 strLog.Format("<< putting device with logical address %d in standby mode", (int8_t)m_iLogicalAddress);
202 AddLog(CEC_LOG_DEBUG, strLog.c_str());
203
204 cec_command command;
205 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_STANDBY);
206
207 return m_processor->Transmit(command);
208}
209
210bool CCECBusDevice::SetOSDString(cec_display_control duration, const char *strMessage)
211{
212 CStdString strLog;
213 strLog.Format("<< display message '%s'", strMessage);
214 AddLog(CEC_LOG_NOTICE, strLog.c_str());
215
216 cec_command command;
217 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_SET_OSD_STRING);
218 command.parameters.push_back((uint8_t)duration);
219
220 for (unsigned int iPtr = 0; iPtr < strlen(strMessage); iPtr++)
221 command.parameters.push_back(strMessage[iPtr]);
222
223 return m_processor->Transmit(command);
224}
225
226bool CCECBusDevice::ReportCECVersion(void)
227{
228 AddLog(CEC_LOG_NOTICE, "<< reporting CEC version as 1.3a");
229
230 cec_command command;
231 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_CEC_VERSION);
232 command.parameters.push_back(CEC_VERSION_1_3A);
233
234 return m_processor->Transmit(command);
235}
236
237bool CCECBusDevice::ReportDeckStatus(void)
238{
239 // need to support opcodes play and deck control before doing anything with this
240 AddLog(CEC_LOG_NOTICE, "<< deck status requested, feature abort");
241 m_processor->TransmitAbort(m_iLogicalAddress, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
242 return false;
243}
244
245bool CCECBusDevice::ReportMenuState(bool bActive /* = true */)
246{
247 if (bActive)
248 AddLog(CEC_LOG_NOTICE, "<< reporting menu state as active");
249 else
250 AddLog(CEC_LOG_NOTICE, "<< reporting menu state as inactive");
251
252 cec_command command;
253 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_MENU_STATUS);
254 command.parameters.push_back(bActive ? (uint8_t) CEC_MENU_STATE_ACTIVATED : (uint8_t) CEC_MENU_STATE_DEACTIVATED);
255
256 return m_processor->Transmit(command);
257}
258
259bool CCECBusDevice::ReportOSDName(void)
260{
261 const char *osdname = m_processor->GetDeviceName().c_str();
262 CStdString strLog;
263 strLog.Format("<< reporting OSD name as %s", osdname);
264 AddLog(CEC_LOG_NOTICE, strLog.c_str());
265
266 cec_command command;
267 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_SET_OSD_NAME);
268 for (unsigned int iPtr = 0; iPtr < strlen(osdname); iPtr++)
269 command.parameters.push_back(osdname[iPtr]);
270
271 return m_processor->Transmit(command);
272}
273
274bool CCECBusDevice::ReportPowerState(bool bOn /* = true */)
275{
276 if (bOn)
277 AddLog(CEC_LOG_NOTICE, "<< reporting \"On\" power status");
278 else
279 AddLog(CEC_LOG_NOTICE, "<< reporting \"Off\" power status");
280
281 cec_command command;
282 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_REPORT_POWER_STATUS);
283 command.parameters.push_back(bOn ? (uint8_t) CEC_POWER_STATUS_ON : (uint8_t) CEC_POWER_STATUS_STANDBY);
284
285 return m_processor->Transmit(command);
286}
287
288bool CCECBusDevice::ReportVendorID(void)
289{
290 AddLog(CEC_LOG_NOTICE, "<< vendor ID requested, feature abort");
291 m_processor->TransmitAbort(m_iLogicalAddress, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
292 return false;
293}
294
295bool CCECBusDevice::BroadcastActiveView(void)
296{
297 AddLog(CEC_LOG_DEBUG, "<< setting active view");
298
299 cec_command command;
82b6ff53 300 cec_command::format(command, GetMyLogicalAddress(), CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
0f23c85c
LOK
301 command.parameters.push_back((m_iPhysicalAddress >> 8) & 0xFF);
302 command.parameters.push_back(m_iPhysicalAddress & 0xFF);
303
304 return m_processor->Transmit(command);
305}
306
307bool CCECBusDevice::BroadcastInactiveView(void)
308{
309 AddLog(CEC_LOG_DEBUG, "<< setting inactive view");
310
311 cec_command command;
82b6ff53 312 cec_command::format(command, GetMyLogicalAddress(), CECDEVICE_BROADCAST, CEC_OPCODE_INACTIVE_SOURCE);
0f23c85c
LOK
313 command.parameters.push_back((m_iPhysicalAddress >> 8) & 0xFF);
314 command.parameters.push_back(m_iPhysicalAddress & 0xFF);
315
316 return m_processor->Transmit(command);
317}
318
319bool CCECBusDevice::BroadcastPhysicalAddress(void)
320{
321 CStdString strLog;
322 strLog.Format("<< reporting physical address as %04x", m_iPhysicalAddress);
323 AddLog(CEC_LOG_NOTICE, strLog.c_str());
324
325 cec_command command;
82b6ff53 326 cec_command::format(command, GetMyLogicalAddress(), CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
0f23c85c
LOK
327 command.parameters.push_back((uint8_t) ((m_iPhysicalAddress >> 8) & 0xFF));
328 command.parameters.push_back((uint8_t) (m_iPhysicalAddress & 0xFF));
329 command.parameters.push_back((uint8_t) (CEC_DEVICE_TYPE_PLAYBACK_DEVICE));
330
331 return m_processor->Transmit(command);
332}
333
334bool CCECBusDevice::BroadcastActiveSource(void)
335{
336 AddLog(CEC_LOG_NOTICE, "<< broadcasting active source");
337
338 cec_command command;
82b6ff53 339 cec_command::format(command, GetMyLogicalAddress(), CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
0f23c85c
LOK
340 command.parameters.push_back((uint8_t) ((m_iPhysicalAddress >> 8) & 0xFF));
341 command.parameters.push_back((uint8_t) (m_iPhysicalAddress & 0xFF));
342
343 return m_processor->Transmit(command);
344}
345
6a1c0009
LOK
346cec_version CCECBusDevice::GetCecVersion(void)
347{
348 if (m_cecVersion == CEC_VERSION_UNKNOWN)
349 {
350 AddLog(CEC_LOG_NOTICE, "<< requesting CEC version");
351 cec_command command;
352 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GET_CEC_VERSION);
353 CLockObject lock(&m_mutex);
354 if (m_processor->Transmit(command))
355 m_condition.Wait(&m_mutex, 1000);
356 }
357
358 return m_cecVersion;
359}
360
e9de9629
LOK
361const char *CCECBusDevice::CECVendorIdToString(const uint64_t iVendorId)
362{
363 switch (iVendorId)
364 {
365 case CEC_VENDOR_SAMSUNG:
366 return "Samsung";
367 case CEC_VENDOR_LG:
368 return "LG";
369 default:
370 return "Unknown";
371 }
372}