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