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