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