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