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" | |
11621576 | 38 | #include "../implementations/VLCommandHandler.h" |
eafa9d46 | 39 | #include "../platform/timeutils.h" |
e9de9629 LOK |
40 | |
41 | using namespace CEC; | |
42 | ||
c4098482 LOK |
43 | #define ToString(p) CCECCommandHandler::ToString(p) |
44 | ||
e9de9629 | 45 | CCECBusDevice::CCECBusDevice(CCECProcessor *processor, cec_logical_address iLogicalAddress, uint16_t iPhysicalAddress) : |
c4098482 | 46 | m_type(CEC_DEVICE_TYPE_RESERVED), |
e9de9629 | 47 | m_iPhysicalAddress(iPhysicalAddress), |
9dc04b07 | 48 | m_iStreamPath(0), |
e9de9629 | 49 | m_iLogicalAddress(iLogicalAddress), |
e55f3f70 | 50 | m_powerStatus(CEC_POWER_STATUS_UNKNOWN), |
e9de9629 | 51 | m_processor(processor), |
c4098482 | 52 | m_vendor(CEC_VENDOR_UNKNOWN), |
4abd6768 | 53 | m_menuState(CEC_MENU_STATE_ACTIVATED), |
8747dd4f | 54 | m_bActiveSource(false), |
d54e8570 | 55 | m_iLastCommandSent(0), |
6a1c0009 | 56 | m_iLastActive(0), |
f8ae3295 LOK |
57 | m_cecVersion(CEC_VERSION_UNKNOWN), |
58 | m_deviceStatus(CEC_DEVICE_STATUS_UNKNOWN) | |
e9de9629 LOK |
59 | { |
60 | m_handler = new CCECCommandHandler(this); | |
51b2a094 | 61 | |
a3269a0a LOK |
62 | for (unsigned int iPtr = 0; iPtr < 4; iPtr++) |
63 | m_menuLanguage.language[iPtr] = '?'; | |
64 | m_menuLanguage.language[3] = 0; | |
65 | m_menuLanguage.device = iLogicalAddress; | |
1fcf5a3f | 66 | |
c4098482 | 67 | m_strDeviceName = ToString(m_iLogicalAddress); |
e9de9629 LOK |
68 | } |
69 | ||
70 | CCECBusDevice::~CCECBusDevice(void) | |
71 | { | |
6a1c0009 | 72 | m_condition.Broadcast(); |
e9de9629 LOK |
73 | delete m_handler; |
74 | } | |
75 | ||
93729720 | 76 | void CCECBusDevice::AddLog(cec_log_level level, const CStdString &strMessage) |
e9de9629 | 77 | { |
93729720 | 78 | m_processor->AddLog(level, strMessage); |
e9de9629 LOK |
79 | } |
80 | ||
93729720 | 81 | bool CCECBusDevice::HandleCommand(const cec_command &command) |
f8513317 | 82 | { |
81a1e39d | 83 | CLockObject lock(&m_transmitMutex); |
93729720 LOK |
84 | m_iLastActive = GetTimeMs(); |
85 | m_handler->HandleCommand(command); | |
a24c27d3 LOK |
86 | if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC) |
87 | m_deviceStatus = CEC_DEVICE_STATUS_PRESENT; | |
93729720 LOK |
88 | m_condition.Signal(); |
89 | return true; | |
90 | } | |
91 | ||
92 | void CCECBusDevice::PollVendorId(void) | |
93 | { | |
81a1e39d | 94 | CLockObject lock(&m_transmitMutex); |
93729720 | 95 | if (m_iLastActive > 0 && m_iLogicalAddress != CECDEVICE_BROADCAST && |
c4098482 | 96 | m_vendor == CEC_VENDOR_UNKNOWN && |
d54e8570 | 97 | GetTimeMs() - m_iLastCommandSent > 5000 && |
315ff809 | 98 | !m_processor->IsMonitoring()) |
93729720 | 99 | { |
5e822b09 | 100 | CStdString strLog; |
62f5527d | 101 | strLog.Format("<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); |
5e822b09 | 102 | AddLog(CEC_LOG_NOTICE, strLog); |
d54e8570 | 103 | m_iLastCommandSent = GetTimeMs(); |
93729720 LOK |
104 | |
105 | cec_command command; | |
ab1469a0 | 106 | cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID); |
93729720 | 107 | if (m_processor->Transmit(command)) |
81a1e39d | 108 | m_condition.Wait(&m_transmitMutex, 1000); |
93729720 LOK |
109 | } |
110 | } | |
111 | ||
112 | bool CCECBusDevice::PowerOn(void) | |
113 | { | |
00762a71 LOK |
114 | CStdString strLog; |
115 | strLog.Format("<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); | |
116 | AddLog(CEC_LOG_DEBUG, strLog.c_str()); | |
117 | ||
118 | cec_command command; | |
119 | cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_IMAGE_VIEW_ON); | |
120 | if (m_processor->Transmit(command)) | |
121 | { | |
122 | GetPowerStatus(); | |
123 | return true; | |
f437e4be LOK |
124 | } |
125 | ||
00762a71 | 126 | return false; |
93729720 LOK |
127 | } |
128 | ||
129 | bool CCECBusDevice::Standby(void) | |
130 | { | |
131 | CStdString strLog; | |
62f5527d | 132 | strLog.Format("<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress); |
93729720 LOK |
133 | AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
134 | ||
135 | cec_command command; | |
ab1469a0 | 136 | cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_STANDBY); |
93729720 LOK |
137 | |
138 | return m_processor->Transmit(command); | |
139 | } | |
140 | ||
141 | /** @name Getters */ | |
142 | //@{ | |
d7be392a | 143 | cec_version CCECBusDevice::GetCecVersion(void) |
93729720 | 144 | { |
f294b22f LOK |
145 | CLockObject lock(&m_mutex); |
146 | if (m_cecVersion == CEC_VERSION_UNKNOWN) | |
147 | { | |
148 | lock.Leave(); | |
149 | RequestCecVersion(); | |
150 | lock.Lock(); | |
151 | } | |
152 | ||
153 | return m_cecVersion; | |
154 | } | |
155 | ||
156 | bool CCECBusDevice::RequestCecVersion(void) | |
157 | { | |
158 | bool bReturn(false); | |
81a1e39d | 159 | if (!MyLogicalAddressContains(m_iLogicalAddress)) |
93729720 | 160 | { |
81a1e39d LOK |
161 | CStdString strLog; |
162 | strLog.Format("<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); | |
163 | AddLog(CEC_LOG_NOTICE, strLog); | |
164 | cec_command command; | |
165 | cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GET_CEC_VERSION); | |
166 | CLockObject lock(&m_transmitMutex); | |
167 | if (m_processor->Transmit(command)) | |
f294b22f | 168 | bReturn = m_condition.Wait(&m_transmitMutex, 1000); |
93729720 | 169 | } |
f294b22f | 170 | return bReturn; |
93729720 LOK |
171 | } |
172 | ||
62f5527d LOK |
173 | const char* CCECBusDevice::GetLogicalAddressName(void) const |
174 | { | |
c4098482 | 175 | return ToString(m_iLogicalAddress); |
62f5527d LOK |
176 | } |
177 | ||
d7be392a | 178 | cec_menu_language &CCECBusDevice::GetMenuLanguage(void) |
93729720 | 179 | { |
f294b22f LOK |
180 | CLockObject lock(&m_mutex); |
181 | if (!strcmp(m_menuLanguage.language, "???")) | |
182 | { | |
183 | lock.Leave(); | |
184 | RequestMenuLanguage(); | |
185 | lock.Lock(); | |
186 | } | |
187 | return m_menuLanguage; | |
188 | } | |
189 | ||
190 | bool CCECBusDevice::RequestMenuLanguage(void) | |
191 | { | |
192 | bool bReturn(false); | |
81a1e39d | 193 | if (!MyLogicalAddressContains(m_iLogicalAddress)) |
93729720 | 194 | { |
81a1e39d LOK |
195 | CStdString strLog; |
196 | strLog.Format("<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); | |
197 | AddLog(CEC_LOG_NOTICE, strLog); | |
198 | cec_command command; | |
199 | cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GET_MENU_LANGUAGE); | |
200 | CLockObject lock(&m_transmitMutex); | |
201 | if (m_processor->Transmit(command)) | |
f294b22f | 202 | bReturn = m_condition.Wait(&m_transmitMutex, 1000); |
93729720 | 203 | } |
f294b22f | 204 | return bReturn; |
93729720 LOK |
205 | } |
206 | ||
207 | cec_logical_address CCECBusDevice::GetMyLogicalAddress(void) const | |
208 | { | |
209 | return m_processor->GetLogicalAddress(); | |
f8513317 LOK |
210 | } |
211 | ||
e9de9629 LOK |
212 | uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const |
213 | { | |
214 | return m_processor->GetPhysicalAddress(); | |
215 | } | |
216 | ||
ed21be2a LOK |
217 | CStdString CCECBusDevice::GetOSDName(void) |
218 | { | |
219 | if (GetStatus() == CEC_DEVICE_STATUS_PRESENT) | |
220 | { | |
221 | CLockObject lock(&m_mutex); | |
222 | if (m_strDeviceName.Equals(ToString(m_iLogicalAddress))) | |
223 | { | |
224 | lock.Leave(); | |
225 | RequestOSDName(); | |
226 | lock.Lock(); | |
227 | } | |
228 | } | |
229 | ||
230 | CLockObject lock(&m_mutex); | |
231 | return m_strDeviceName; | |
232 | } | |
233 | ||
234 | bool CCECBusDevice::RequestOSDName(void) | |
235 | { | |
236 | bool bReturn(false); | |
237 | if (!MyLogicalAddressContains(m_iLogicalAddress)) | |
238 | { | |
239 | CStdString strLog; | |
240 | strLog.Format("<< requesting OSD name of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); | |
241 | AddLog(CEC_LOG_NOTICE, strLog); | |
242 | cec_command command; | |
243 | cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_OSD_NAME); | |
244 | CLockObject lock(&m_transmitMutex); | |
245 | if (m_processor->Transmit(command)) | |
246 | bReturn = m_condition.Wait(&m_transmitMutex, 1000); | |
247 | } | |
248 | return bReturn; | |
249 | } | |
250 | ||
16b1e052 LOK |
251 | uint16_t CCECBusDevice::GetPhysicalAddress(bool bRefresh /* = true */) |
252 | { | |
253 | if (GetStatus() == CEC_DEVICE_STATUS_PRESENT) | |
254 | { | |
255 | CLockObject lock(&m_mutex); | |
256 | if (m_iPhysicalAddress == 0xFFFF || bRefresh) | |
257 | { | |
258 | lock.Leave(); | |
259 | RequestPhysicalAddress(); | |
260 | lock.Lock(); | |
261 | } | |
262 | } | |
263 | ||
264 | CLockObject lock(&m_mutex); | |
265 | return m_iPhysicalAddress; | |
266 | } | |
267 | ||
268 | bool CCECBusDevice::RequestPhysicalAddress(void) | |
269 | { | |
270 | bool bReturn(false); | |
271 | if (!MyLogicalAddressContains(m_iLogicalAddress)) | |
272 | { | |
273 | CStdString strLog; | |
274 | strLog.Format("<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); | |
275 | AddLog(CEC_LOG_NOTICE, strLog); | |
276 | cec_command command; | |
277 | cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS); | |
278 | CLockObject lock(&m_transmitMutex); | |
279 | if (m_processor->Transmit(command)) | |
280 | bReturn = m_condition.Wait(&m_transmitMutex, 1000); | |
281 | } | |
282 | return bReturn; | |
283 | } | |
284 | ||
d7be392a | 285 | cec_power_status CCECBusDevice::GetPowerStatus(void) |
e9de9629 | 286 | { |
f294b22f LOK |
287 | CLockObject lock(&m_mutex); |
288 | if (m_powerStatus == CEC_POWER_STATUS_UNKNOWN) | |
289 | { | |
290 | lock.Leave(); | |
291 | RequestPowerStatus(); | |
292 | lock.Lock(); | |
293 | } | |
294 | return m_powerStatus; | |
295 | } | |
296 | ||
297 | bool CCECBusDevice::RequestPowerStatus(void) | |
298 | { | |
299 | bool bReturn(false); | |
81a1e39d | 300 | if (!MyLogicalAddressContains(m_iLogicalAddress)) |
93729720 | 301 | { |
81a1e39d LOK |
302 | CStdString strLog; |
303 | strLog.Format("<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); | |
304 | AddLog(CEC_LOG_NOTICE, strLog); | |
305 | cec_command command; | |
306 | cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS); | |
307 | CLockObject lock(&m_transmitMutex); | |
308 | if (m_processor->Transmit(command)) | |
f294b22f | 309 | bReturn = m_condition.Wait(&m_transmitMutex, 1000); |
93729720 | 310 | } |
f294b22f LOK |
311 | return bReturn; |
312 | } | |
93729720 | 313 | |
f294b22f LOK |
314 | cec_vendor_id CCECBusDevice::GetVendorId(void) |
315 | { | |
81a1e39d | 316 | CLockObject lock(&m_mutex); |
f294b22f LOK |
317 | if (m_vendor == CEC_VENDOR_UNKNOWN) |
318 | { | |
319 | lock.Leave(); | |
320 | RequestVendorId(); | |
321 | lock.Lock(); | |
322 | } | |
323 | return m_vendor; | |
e9de9629 LOK |
324 | } |
325 | ||
f294b22f | 326 | bool CCECBusDevice::RequestVendorId(void) |
a3269a0a | 327 | { |
f294b22f | 328 | bool bReturn(false); |
81a1e39d | 329 | if (!MyLogicalAddressContains(m_iLogicalAddress)) |
a3269a0a | 330 | { |
81a1e39d LOK |
331 | CStdString strLog; |
332 | strLog.Format("<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); | |
333 | AddLog(CEC_LOG_NOTICE, strLog); | |
334 | cec_command command; | |
335 | cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID); | |
81a1e39d | 336 | |
f294b22f | 337 | CLockObject lock(&m_transmitMutex); |
81a1e39d | 338 | if (m_processor->Transmit(command)) |
f294b22f | 339 | bReturn = m_condition.Wait(&m_transmitMutex, 1000); |
a3269a0a | 340 | } |
f294b22f | 341 | return bReturn; |
93729720 LOK |
342 | } |
343 | ||
c4098482 LOK |
344 | const char *CCECBusDevice::GetVendorName(void) |
345 | { | |
f294b22f | 346 | return ToString(GetVendorId()); |
c4098482 LOK |
347 | } |
348 | ||
93729720 LOK |
349 | bool CCECBusDevice::MyLogicalAddressContains(cec_logical_address address) const |
350 | { | |
351 | return m_processor->HasLogicalAddress(address); | |
a3269a0a LOK |
352 | } |
353 | ||
f8ae3295 LOK |
354 | cec_bus_device_status CCECBusDevice::GetStatus(void) |
355 | { | |
356 | CLockObject lock(&m_mutex); | |
357 | if (m_deviceStatus == CEC_DEVICE_STATUS_UNKNOWN) | |
358 | { | |
95a73fa7 LOK |
359 | lock.Leave(); |
360 | bool bPollAcked = m_processor->PollDevice(m_iLogicalAddress); | |
361 | ||
362 | lock.Lock(); | |
363 | m_deviceStatus = bPollAcked ? CEC_DEVICE_STATUS_PRESENT : CEC_DEVICE_STATUS_NOT_PRESENT; | |
f8ae3295 LOK |
364 | } |
365 | ||
366 | return m_deviceStatus; | |
367 | } | |
368 | ||
93729720 LOK |
369 | //@} |
370 | ||
371 | /** @name Setters */ | |
372 | //@{ | |
e55f3f70 | 373 | void CCECBusDevice::SetCecVersion(const cec_version newVersion) |
6a1c0009 | 374 | { |
6a1c0009 LOK |
375 | m_cecVersion = newVersion; |
376 | ||
c686413b | 377 | CStdString strLog; |
c4098482 | 378 | strLog.Format("%s (%X): CEC version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(newVersion)); |
6a1c0009 LOK |
379 | AddLog(CEC_LOG_DEBUG, strLog); |
380 | } | |
381 | ||
93729720 LOK |
382 | void CCECBusDevice::SetMenuLanguage(const cec_menu_language &language) |
383 | { | |
81a1e39d | 384 | CLockObject lock(&m_mutex); |
93729720 LOK |
385 | if (language.device == m_iLogicalAddress) |
386 | { | |
387 | CStdString strLog; | |
62f5527d | 388 | strLog.Format(">> %s (%X): menu language set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, language.language); |
93729720 LOK |
389 | m_processor->AddLog(CEC_LOG_DEBUG, strLog); |
390 | m_menuLanguage = language; | |
391 | } | |
392 | } | |
393 | ||
15d1a84c LOK |
394 | void CCECBusDevice::SetOSDName(CStdString strName) |
395 | { | |
396 | CLockObject lock(&m_mutex); | |
397 | if (m_strDeviceName != strName) | |
398 | { | |
399 | CStdString strLog; | |
400 | strLog.Format(">> %s (%X): osd name set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, strName); | |
401 | m_processor->AddLog(CEC_LOG_DEBUG, strLog); | |
402 | m_strDeviceName = strName; | |
403 | } | |
404 | } | |
405 | ||
28fa6c97 LOK |
406 | void CCECBusDevice::SetMenuState(const cec_menu_state state) |
407 | { | |
81a1e39d | 408 | CLockObject lock(&m_mutex); |
28fa6c97 LOK |
409 | if (m_menuState != state) |
410 | { | |
411 | CStdString strLog; | |
412 | strLog.Format(">> %s (%X): menu state set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_menuState)); | |
413 | m_processor->AddLog(CEC_LOG_DEBUG, strLog); | |
414 | m_menuState = state; | |
415 | } | |
416 | } | |
417 | ||
7856411b LOK |
418 | void CCECBusDevice::SetInactiveDevice(void) |
419 | { | |
420 | CLockObject lock(&m_mutex); | |
421 | m_bActiveSource = false; | |
422 | } | |
423 | ||
424 | void CCECBusDevice::SetActiveDevice(void) | |
425 | { | |
426 | CLockObject lock(&m_mutex); | |
427 | ||
428 | for (int iPtr = 0; iPtr < 16; iPtr++) | |
429 | if (iPtr != m_iLogicalAddress) | |
430 | m_processor->m_busDevices[iPtr]->SetInactiveDevice(); | |
431 | ||
432 | m_bActiveSource = true; | |
433 | m_powerStatus = CEC_POWER_STATUS_ON; | |
434 | } | |
435 | ||
93fff5c1 LOK |
436 | bool CCECBusDevice::TryLogicalAddress(void) |
437 | { | |
438 | CStdString strLog; | |
439 | strLog.Format("trying logical address '%s'", GetLogicalAddressName()); | |
440 | AddLog(CEC_LOG_DEBUG, strLog); | |
441 | ||
442 | m_processor->SetAckMask(0x1 << m_iLogicalAddress); | |
443 | if (!TransmitPoll(m_iLogicalAddress)) | |
444 | { | |
445 | strLog.Format("using logical address '%s'", GetLogicalAddressName()); | |
446 | AddLog(CEC_LOG_NOTICE, strLog); | |
447 | SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC); | |
448 | ||
449 | return true; | |
450 | } | |
451 | ||
452 | strLog.Format("logical address '%s' already taken", GetLogicalAddressName()); | |
453 | AddLog(CEC_LOG_DEBUG, strLog); | |
454 | SetDeviceStatus(CEC_DEVICE_STATUS_PRESENT); | |
455 | return false; | |
456 | } | |
457 | ||
458 | void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus) | |
459 | { | |
460 | CLockObject lock(&m_mutex); | |
461 | switch (newStatus) | |
462 | { | |
463 | case CEC_DEVICE_STATUS_UNKNOWN: | |
464 | m_iStreamPath = 0; | |
465 | m_powerStatus = CEC_POWER_STATUS_UNKNOWN; | |
466 | m_vendor = CEC_VENDOR_UNKNOWN; | |
467 | m_menuState = CEC_MENU_STATE_ACTIVATED; | |
468 | m_bActiveSource = false; | |
469 | m_iLastCommandSent = 0; | |
470 | m_iLastActive = 0; | |
471 | m_cecVersion = CEC_VERSION_UNKNOWN; | |
472 | m_deviceStatus = newStatus; | |
473 | break; | |
474 | case CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC: | |
475 | m_iStreamPath = 0; | |
476 | m_powerStatus = CEC_POWER_STATUS_ON; | |
477 | m_vendor = CEC_VENDOR_UNKNOWN; | |
478 | m_menuState = CEC_MENU_STATE_ACTIVATED; | |
479 | m_bActiveSource = false; | |
480 | m_iLastCommandSent = 0; | |
481 | m_iLastActive = 0; | |
482 | m_cecVersion = CEC_VERSION_1_3A; | |
483 | m_deviceStatus = newStatus; | |
484 | break; | |
485 | case CEC_DEVICE_STATUS_PRESENT: | |
486 | case CEC_DEVICE_STATUS_NOT_PRESENT: | |
487 | m_deviceStatus = newStatus; | |
488 | break; | |
489 | } | |
490 | } | |
491 | ||
9dc04b07 | 492 | void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress) |
93729720 | 493 | { |
81a1e39d | 494 | CLockObject lock(&m_mutex); |
b8176e3c | 495 | if (iNewAddress > 0 && m_iPhysicalAddress != iNewAddress) |
93729720 LOK |
496 | { |
497 | CStdString strLog; | |
62f5527d | 498 | strLog.Format(">> %s (%X): physical address changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress, iNewAddress); |
93729720 LOK |
499 | AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
500 | ||
501 | m_iPhysicalAddress = iNewAddress; | |
502 | } | |
503 | } | |
504 | ||
9dc04b07 LOK |
505 | void CCECBusDevice::SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress /* = 0 */) |
506 | { | |
81a1e39d | 507 | CLockObject lock(&m_mutex); |
9dc04b07 LOK |
508 | if (iNewAddress > 0) |
509 | { | |
510 | CStdString strLog; | |
68a12fbf | 511 | strLog.Format(">> %s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, iOldAddress == 0 ? m_iStreamPath : iOldAddress, iNewAddress); |
9dc04b07 LOK |
512 | AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
513 | ||
514 | m_iStreamPath = iNewAddress; | |
96274140 LOK |
515 | |
516 | if (iNewAddress > 0) | |
81a1e39d LOK |
517 | { |
518 | lock.Leave(); | |
96274140 | 519 | SetPowerStatus(CEC_POWER_STATUS_ON); |
81a1e39d | 520 | } |
9dc04b07 LOK |
521 | } |
522 | } | |
523 | ||
e55f3f70 LOK |
524 | void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus) |
525 | { | |
81a1e39d | 526 | CLockObject lock(&m_mutex); |
b0271d54 LOK |
527 | if (m_powerStatus != powerStatus) |
528 | { | |
529 | CStdString strLog; | |
c4098482 | 530 | strLog.Format(">> %s (%X): power status changed from '%s' to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_powerStatus), ToString(powerStatus)); |
b0271d54 LOK |
531 | m_processor->AddLog(CEC_LOG_DEBUG, strLog); |
532 | m_powerStatus = powerStatus; | |
533 | } | |
e55f3f70 LOK |
534 | } |
535 | ||
c4098482 | 536 | void CCECBusDevice::SetVendorId(uint64_t iVendorId) |
e9de9629 | 537 | { |
e9de9629 | 538 | { |
81a1e39d LOK |
539 | CLockObject lock(&m_mutex); |
540 | m_vendor = (cec_vendor_id)iVendorId; | |
541 | ||
542 | switch (iVendorId) | |
0ab58650 | 543 | { |
81a1e39d LOK |
544 | case CEC_VENDOR_SAMSUNG: |
545 | if (m_handler->GetVendorId() != CEC_VENDOR_SAMSUNG) | |
546 | { | |
547 | delete m_handler; | |
548 | m_handler = new CANCommandHandler(this); | |
549 | } | |
550 | break; | |
551 | case CEC_VENDOR_LG: | |
552 | if (m_handler->GetVendorId() != CEC_VENDOR_LG) | |
553 | { | |
554 | delete m_handler; | |
555 | m_handler = new CSLCommandHandler(this); | |
556 | } | |
557 | break; | |
558 | case CEC_VENDOR_PANASONIC: | |
559 | if (m_handler->GetVendorId() != CEC_VENDOR_PANASONIC) | |
560 | { | |
561 | delete m_handler; | |
562 | m_handler = new CVLCommandHandler(this); | |
563 | } | |
564 | break; | |
565 | default: | |
566 | if (m_handler->GetVendorId() != CEC_VENDOR_UNKNOWN) | |
567 | { | |
568 | delete m_handler; | |
569 | m_handler = new CCECCommandHandler(this); | |
570 | } | |
571 | break; | |
0ab58650 | 572 | } |
e9de9629 LOK |
573 | } |
574 | ||
575 | CStdString strLog; | |
81a1e39d | 576 | strLog.Format("%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress, GetVendorName(), m_vendor); |
e9de9629 LOK |
577 | m_processor->AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
578 | } | |
93729720 | 579 | //@} |
e9de9629 | 580 | |
93729720 LOK |
581 | /** @name Transmit methods */ |
582 | //@{ | |
583 | bool CCECBusDevice::TransmitActiveSource(void) | |
0f23c85c | 584 | { |
81a1e39d | 585 | CLockObject lock(&m_mutex); |
58c1f6f5 LOK |
586 | if (m_powerStatus != CEC_POWER_STATUS_ON) |
587 | { | |
588 | CStdString strLog; | |
589 | strLog.Format("<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress); | |
590 | AddLog(CEC_LOG_DEBUG, strLog); | |
591 | } | |
592 | else if (m_bActiveSource) | |
8747dd4f LOK |
593 | { |
594 | CStdString strLog; | |
62f5527d | 595 | strLog.Format("<< %s (%X) -> broadcast (F): active source (%4x)", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress); |
8747dd4f | 596 | AddLog(CEC_LOG_NOTICE, strLog); |
0f23c85c | 597 | |
8747dd4f | 598 | cec_command command; |
ab1469a0 LOK |
599 | cec_command::Format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE); |
600 | command.parameters.PushBack((uint8_t) ((m_iPhysicalAddress >> 8) & 0xFF)); | |
601 | command.parameters.PushBack((uint8_t) (m_iPhysicalAddress & 0xFF)); | |
0f23c85c | 602 | |
81a1e39d | 603 | lock.Leave(); |
8747dd4f LOK |
604 | return m_processor->Transmit(command); |
605 | } | |
606 | else | |
607 | { | |
608 | CStdString strLog; | |
62f5527d | 609 | strLog.Format("<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress); |
8747dd4f LOK |
610 | AddLog(CEC_LOG_DEBUG, strLog); |
611 | } | |
612 | ||
613 | return false; | |
0f23c85c LOK |
614 | } |
615 | ||
29912296 | 616 | bool CCECBusDevice::TransmitCECVersion(cec_logical_address dest) |
0f23c85c | 617 | { |
81a1e39d | 618 | CLockObject lock(&m_mutex); |
d7be392a | 619 | CStdString strLog; |
c4098482 | 620 | strLog.Format("<< %s (%X) -> %s (%X): cec version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_cecVersion)); |
d7be392a | 621 | AddLog(CEC_LOG_NOTICE, strLog); |
0f23c85c LOK |
622 | |
623 | cec_command command; | |
ab1469a0 LOK |
624 | cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_CEC_VERSION); |
625 | command.parameters.PushBack((uint8_t)m_cecVersion); | |
0f23c85c | 626 | |
81a1e39d | 627 | lock.Leave(); |
0f23c85c LOK |
628 | return m_processor->Transmit(command); |
629 | } | |
630 | ||
93729720 LOK |
631 | bool CCECBusDevice::TransmitInactiveView(void) |
632 | { | |
d7be392a | 633 | CStdString strLog; |
62f5527d | 634 | strLog.Format("<< %s (%X) -> broadcast (F): inactive view", GetLogicalAddressName(), m_iLogicalAddress); |
d7be392a | 635 | AddLog(CEC_LOG_NOTICE, strLog); |
93729720 LOK |
636 | |
637 | cec_command command; | |
ab1469a0 LOK |
638 | cec_command::Format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_INACTIVE_SOURCE); |
639 | command.parameters.PushBack((m_iPhysicalAddress >> 8) & 0xFF); | |
640 | command.parameters.PushBack(m_iPhysicalAddress & 0xFF); | |
93729720 LOK |
641 | |
642 | return m_processor->Transmit(command); | |
643 | } | |
644 | ||
29912296 | 645 | bool CCECBusDevice::TransmitMenuState(cec_logical_address dest) |
0f23c85c | 646 | { |
d7be392a | 647 | CStdString strLog; |
28fa6c97 | 648 | strLog.Format("<< %s (%X) -> %s (%X): menu state '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_menuState)); |
d7be392a | 649 | AddLog(CEC_LOG_NOTICE, strLog); |
0f23c85c LOK |
650 | |
651 | cec_command command; | |
ab1469a0 LOK |
652 | cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_MENU_STATUS); |
653 | command.parameters.PushBack((uint8_t)m_menuState); | |
0f23c85c LOK |
654 | |
655 | return m_processor->Transmit(command); | |
656 | } | |
657 | ||
29912296 | 658 | bool CCECBusDevice::TransmitOSDName(cec_logical_address dest) |
0f23c85c | 659 | { |
81a1e39d | 660 | CLockObject lock(&m_mutex); |
0f23c85c | 661 | CStdString strLog; |
c4098482 | 662 | strLog.Format("<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, m_strDeviceName.c_str()); |
0f23c85c LOK |
663 | AddLog(CEC_LOG_NOTICE, strLog.c_str()); |
664 | ||
665 | cec_command command; | |
ab1469a0 | 666 | cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_SET_OSD_NAME); |
787a3cb8 | 667 | for (unsigned int iPtr = 0; iPtr < m_strDeviceName.length(); iPtr++) |
ab1469a0 | 668 | command.parameters.PushBack(m_strDeviceName.at(iPtr)); |
0f23c85c | 669 | |
81a1e39d | 670 | lock.Leave(); |
0f23c85c LOK |
671 | return m_processor->Transmit(command); |
672 | } | |
673 | ||
38bdb943 LOK |
674 | bool CCECBusDevice::TransmitOSDString(cec_logical_address dest, cec_display_control duration, const char *strMessage) |
675 | { | |
81a1e39d | 676 | CLockObject lock(&m_mutex); |
38bdb943 | 677 | CStdString strLog; |
c4098482 | 678 | strLog.Format("<< %s (%X) -> %s (%X): display OSD message '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, strMessage); |
38bdb943 LOK |
679 | AddLog(CEC_LOG_NOTICE, strLog.c_str()); |
680 | ||
681 | cec_command command; | |
ab1469a0 LOK |
682 | cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_SET_OSD_STRING); |
683 | command.parameters.PushBack((uint8_t)duration); | |
38bdb943 LOK |
684 | |
685 | unsigned int iLen = strlen(strMessage); | |
686 | if (iLen > 13) iLen = 13; | |
687 | ||
688 | for (unsigned int iPtr = 0; iPtr < iLen; iPtr++) | |
ab1469a0 | 689 | command.parameters.PushBack(strMessage[iPtr]); |
38bdb943 | 690 | |
81a1e39d | 691 | lock.Leave(); |
38bdb943 LOK |
692 | return m_processor->Transmit(command); |
693 | } | |
694 | ||
29912296 | 695 | bool CCECBusDevice::TransmitPhysicalAddress(void) |
0f23c85c | 696 | { |
81a1e39d | 697 | CLockObject lock(&m_mutex); |
0f23c85c | 698 | CStdString strLog; |
62f5527d | 699 | strLog.Format("<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress); |
0f23c85c LOK |
700 | AddLog(CEC_LOG_NOTICE, strLog.c_str()); |
701 | ||
702 | cec_command command; | |
ab1469a0 LOK |
703 | cec_command::Format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS); |
704 | command.parameters.PushBack((uint8_t) ((m_iPhysicalAddress >> 8) & 0xFF)); | |
705 | command.parameters.PushBack((uint8_t) (m_iPhysicalAddress & 0xFF)); | |
706 | command.parameters.PushBack((uint8_t) (m_type)); | |
0f23c85c | 707 | |
81a1e39d | 708 | lock.Leave(); |
0f23c85c LOK |
709 | return m_processor->Transmit(command); |
710 | } | |
711 | ||
93729720 | 712 | bool CCECBusDevice::TransmitPoll(cec_logical_address dest) |
57f45e6c LOK |
713 | { |
714 | bool bReturn(false); | |
93729720 LOK |
715 | if (dest == CECDEVICE_UNKNOWN) |
716 | dest = m_iLogicalAddress; | |
f8513317 | 717 | |
57f45e6c | 718 | CStdString strLog; |
c4098482 | 719 | strLog.Format("<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest); |
d7be392a | 720 | AddLog(CEC_LOG_NOTICE, strLog.c_str()); |
57f45e6c LOK |
721 | |
722 | cec_command command; | |
ab1469a0 | 723 | cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_NONE); |
57f45e6c | 724 | |
1674de37 LOK |
725 | { |
726 | CLockObject lock(&m_transmitMutex); | |
727 | bReturn = m_processor->Transmit(command); | |
728 | } | |
729 | ||
57f45e6c | 730 | AddLog(CEC_LOG_DEBUG, bReturn ? ">> POLL sent" : ">> POLL not sent"); |
1674de37 LOK |
731 | |
732 | if (bReturn) | |
733 | { | |
734 | CLockObject lock(&m_mutex); | |
735 | m_iLastActive = GetTimeMs(); | |
736 | } | |
737 | ||
57f45e6c LOK |
738 | return bReturn; |
739 | } | |
93729720 LOK |
740 | |
741 | bool CCECBusDevice::TransmitPowerState(cec_logical_address dest) | |
742 | { | |
81a1e39d | 743 | CLockObject lock(&m_mutex); |
93729720 | 744 | CStdString strLog; |
c4098482 | 745 | strLog.Format("<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_powerStatus)); |
d7be392a LOK |
746 | AddLog(CEC_LOG_NOTICE, strLog.c_str()); |
747 | ||
93729720 | 748 | cec_command command; |
ab1469a0 LOK |
749 | cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_REPORT_POWER_STATUS); |
750 | command.parameters.PushBack((uint8_t) m_powerStatus); | |
93729720 | 751 | |
81a1e39d | 752 | lock.Leave(); |
93729720 LOK |
753 | return m_processor->Transmit(command); |
754 | } | |
755 | ||
756 | bool CCECBusDevice::TransmitVendorID(cec_logical_address dest) | |
757 | { | |
81a1e39d | 758 | CLockObject lock(&m_mutex); |
c4098482 LOK |
759 | if (m_vendor == CEC_VENDOR_UNKNOWN) |
760 | { | |
761 | CStdString strLog; | |
762 | strLog.Format("<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest); | |
763 | AddLog(CEC_LOG_NOTICE, strLog); | |
d7be392a | 764 | |
81a1e39d | 765 | lock.Leave(); |
c4098482 LOK |
766 | m_processor->TransmitAbort(dest, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID); |
767 | return false; | |
768 | } | |
769 | else | |
770 | { | |
771 | CStdString strLog; | |
772 | strLog.Format("<< %s (%X) -> %s (%X): vendor id %s (%x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_vendor), (uint64_t)m_vendor); | |
773 | AddLog(CEC_LOG_NOTICE, strLog); | |
774 | ||
775 | cec_command command; | |
5e9b399e | 776 | cec_command::Format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_DEVICE_VENDOR_ID); |
c4098482 | 777 | |
ab1469a0 LOK |
778 | command.parameters.PushBack((uint8_t) (((uint64_t)m_vendor >> 16) & 0xFF)); |
779 | command.parameters.PushBack((uint8_t) (((uint64_t)m_vendor >> 8) & 0xFF)); | |
780 | command.parameters.PushBack((uint8_t) ((uint64_t)m_vendor & 0xFF)); | |
c4098482 | 781 | |
81a1e39d | 782 | lock.Leave(); |
c4098482 LOK |
783 | return m_processor->Transmit(command); |
784 | } | |
93729720 | 785 | } |
a33794d8 LOK |
786 | |
787 | bool CCECBusDevice::SendKeypress(cec_user_control_code key, bool bWait /* = false */) | |
788 | { | |
789 | { | |
790 | CLockObject lock(&m_transmitMutex); | |
791 | cec_command command; | |
792 | cec_command::Format(command, m_processor->GetLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_USER_CONTROL_PRESSED); | |
988de7b9 | 793 | command.parameters.PushBack((uint8_t)key); |
a33794d8 LOK |
794 | |
795 | if (bWait) | |
796 | { | |
797 | if (m_processor->Transmit(command)) | |
798 | return m_condition.Wait(&m_transmitMutex, 1000); | |
799 | return false; | |
800 | } | |
801 | ||
802 | return m_processor->Transmit(command); | |
803 | } | |
804 | } | |
805 | ||
806 | bool CCECBusDevice::SendKeyRelease(bool bWait /* = false */) | |
807 | { | |
808 | { | |
809 | CLockObject lock(&m_transmitMutex); | |
810 | cec_command command; | |
811 | cec_command::Format(command, m_processor->GetLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_USER_CONTROL_RELEASE); | |
812 | ||
813 | if (bWait) | |
814 | { | |
815 | if (m_processor->Transmit(command)) | |
816 | return m_condition.Wait(&m_transmitMutex, 1000); | |
817 | return false; | |
818 | } | |
819 | else | |
820 | { | |
821 | return m_processor->Transmit(command); | |
822 | } | |
823 | } | |
824 | } | |
93729720 | 825 | //@} |