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