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