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(); | |
9fd73dd4 LOK |
259 | if (!RequestPhysicalAddress()) |
260 | AddLog(CEC_LOG_ERROR, "failed to request the physical address"); | |
16b1e052 LOK |
261 | lock.Lock(); |
262 | } | |
263 | } | |
264 | ||
265 | CLockObject lock(&m_mutex); | |
266 | return m_iPhysicalAddress; | |
267 | } | |
268 | ||
269 | bool CCECBusDevice::RequestPhysicalAddress(void) | |
270 | { | |
271 | bool bReturn(false); | |
272 | if (!MyLogicalAddressContains(m_iLogicalAddress)) | |
273 | { | |
274 | CStdString strLog; | |
275 | strLog.Format("<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); | |
276 | AddLog(CEC_LOG_NOTICE, strLog); | |
277 | cec_command command; | |
278 | cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS); | |
279 | CLockObject lock(&m_transmitMutex); | |
280 | if (m_processor->Transmit(command)) | |
281 | bReturn = m_condition.Wait(&m_transmitMutex, 1000); | |
282 | } | |
283 | return bReturn; | |
284 | } | |
285 | ||
d7be392a | 286 | cec_power_status CCECBusDevice::GetPowerStatus(void) |
e9de9629 | 287 | { |
f294b22f LOK |
288 | CLockObject lock(&m_mutex); |
289 | if (m_powerStatus == CEC_POWER_STATUS_UNKNOWN) | |
290 | { | |
291 | lock.Leave(); | |
292 | RequestPowerStatus(); | |
293 | lock.Lock(); | |
294 | } | |
295 | return m_powerStatus; | |
296 | } | |
297 | ||
298 | bool CCECBusDevice::RequestPowerStatus(void) | |
299 | { | |
300 | bool bReturn(false); | |
81a1e39d | 301 | if (!MyLogicalAddressContains(m_iLogicalAddress)) |
93729720 | 302 | { |
81a1e39d LOK |
303 | CStdString strLog; |
304 | strLog.Format("<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); | |
305 | AddLog(CEC_LOG_NOTICE, strLog); | |
306 | cec_command command; | |
307 | cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS); | |
308 | CLockObject lock(&m_transmitMutex); | |
309 | if (m_processor->Transmit(command)) | |
f294b22f | 310 | bReturn = m_condition.Wait(&m_transmitMutex, 1000); |
93729720 | 311 | } |
f294b22f LOK |
312 | return bReturn; |
313 | } | |
93729720 | 314 | |
f294b22f LOK |
315 | cec_vendor_id CCECBusDevice::GetVendorId(void) |
316 | { | |
81a1e39d | 317 | CLockObject lock(&m_mutex); |
f294b22f LOK |
318 | if (m_vendor == CEC_VENDOR_UNKNOWN) |
319 | { | |
320 | lock.Leave(); | |
321 | RequestVendorId(); | |
322 | lock.Lock(); | |
323 | } | |
324 | return m_vendor; | |
e9de9629 LOK |
325 | } |
326 | ||
f294b22f | 327 | bool CCECBusDevice::RequestVendorId(void) |
a3269a0a | 328 | { |
f294b22f | 329 | bool bReturn(false); |
81a1e39d | 330 | if (!MyLogicalAddressContains(m_iLogicalAddress)) |
a3269a0a | 331 | { |
81a1e39d LOK |
332 | CStdString strLog; |
333 | strLog.Format("<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress); | |
334 | AddLog(CEC_LOG_NOTICE, strLog); | |
335 | cec_command command; | |
336 | cec_command::Format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID); | |
81a1e39d | 337 | |
f294b22f | 338 | CLockObject lock(&m_transmitMutex); |
81a1e39d | 339 | if (m_processor->Transmit(command)) |
f294b22f | 340 | bReturn = m_condition.Wait(&m_transmitMutex, 1000); |
a3269a0a | 341 | } |
f294b22f | 342 | return bReturn; |
93729720 LOK |
343 | } |
344 | ||
c4098482 LOK |
345 | const char *CCECBusDevice::GetVendorName(void) |
346 | { | |
f294b22f | 347 | return ToString(GetVendorId()); |
c4098482 LOK |
348 | } |
349 | ||
93729720 LOK |
350 | bool CCECBusDevice::MyLogicalAddressContains(cec_logical_address address) const |
351 | { | |
352 | return m_processor->HasLogicalAddress(address); | |
a3269a0a LOK |
353 | } |
354 | ||
9fd73dd4 LOK |
355 | bool CCECBusDevice::NeedsPoll(void) |
356 | { | |
357 | bool bSendPoll(false); | |
358 | switch (m_iLogicalAddress) | |
359 | { | |
360 | case CECDEVICE_PLAYBACKDEVICE3: | |
361 | if (m_processor->m_busDevices[CECDEVICE_PLAYBACKDEVICE2]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) | |
362 | bSendPoll = true; | |
363 | break; | |
364 | case CECDEVICE_PLAYBACKDEVICE2: | |
365 | if (m_processor->m_busDevices[CECDEVICE_PLAYBACKDEVICE1]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) | |
366 | bSendPoll = true; | |
367 | break; | |
368 | case CECDEVICE_RECORDINGDEVICE3: | |
369 | if (m_processor->m_busDevices[CECDEVICE_RECORDINGDEVICE2]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) | |
370 | bSendPoll = true; | |
371 | break; | |
372 | case CECDEVICE_RECORDINGDEVICE2: | |
373 | if (m_processor->m_busDevices[CECDEVICE_RECORDINGDEVICE1]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) | |
374 | bSendPoll = true; | |
375 | break; | |
376 | case CECDEVICE_TUNER4: | |
377 | if (m_processor->m_busDevices[CECDEVICE_TUNER3]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) | |
378 | bSendPoll = true; | |
379 | break; | |
380 | case CECDEVICE_TUNER3: | |
381 | if (m_processor->m_busDevices[CECDEVICE_TUNER2]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) | |
382 | bSendPoll = true; | |
383 | break; | |
384 | case CECDEVICE_TUNER2: | |
385 | if (m_processor->m_busDevices[CECDEVICE_TUNER1]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) | |
386 | bSendPoll = true; | |
387 | break; | |
388 | case CECDEVICE_AUDIOSYSTEM: | |
389 | case CECDEVICE_PLAYBACKDEVICE1: | |
390 | case CECDEVICE_RECORDINGDEVICE1: | |
391 | case CECDEVICE_TUNER1: | |
392 | case CECDEVICE_TV: | |
393 | bSendPoll = true; | |
394 | break; | |
395 | default: | |
396 | break; | |
397 | } | |
398 | ||
399 | return bSendPoll; | |
400 | } | |
401 | ||
402 | cec_bus_device_status CCECBusDevice::GetStatus(bool bForcePoll /* = false */) | |
f8ae3295 LOK |
403 | { |
404 | CLockObject lock(&m_mutex); | |
9fd73dd4 | 405 | if (m_deviceStatus == CEC_DEVICE_STATUS_UNKNOWN || bForcePoll) |
f8ae3295 | 406 | { |
95a73fa7 | 407 | lock.Leave(); |
9fd73dd4 LOK |
408 | bool bPollAcked(false); |
409 | if (bForcePoll || NeedsPoll()) | |
410 | bPollAcked = m_processor->PollDevice(m_iLogicalAddress); | |
95a73fa7 LOK |
411 | |
412 | lock.Lock(); | |
413 | m_deviceStatus = bPollAcked ? CEC_DEVICE_STATUS_PRESENT : CEC_DEVICE_STATUS_NOT_PRESENT; | |
f8ae3295 LOK |
414 | } |
415 | ||
416 | return m_deviceStatus; | |
417 | } | |
418 | ||
93729720 LOK |
419 | //@} |
420 | ||
421 | /** @name Setters */ | |
422 | //@{ | |
e55f3f70 | 423 | void CCECBusDevice::SetCecVersion(const cec_version newVersion) |
6a1c0009 | 424 | { |
6a1c0009 LOK |
425 | m_cecVersion = newVersion; |
426 | ||
c686413b | 427 | CStdString strLog; |
c4098482 | 428 | strLog.Format("%s (%X): CEC version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(newVersion)); |
6a1c0009 LOK |
429 | AddLog(CEC_LOG_DEBUG, strLog); |
430 | } | |
431 | ||
93729720 LOK |
432 | void CCECBusDevice::SetMenuLanguage(const cec_menu_language &language) |
433 | { | |
81a1e39d | 434 | CLockObject lock(&m_mutex); |
93729720 LOK |
435 | if (language.device == m_iLogicalAddress) |
436 | { | |
437 | CStdString strLog; | |
62f5527d | 438 | strLog.Format(">> %s (%X): menu language set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, language.language); |
93729720 LOK |
439 | m_processor->AddLog(CEC_LOG_DEBUG, strLog); |
440 | m_menuLanguage = language; | |
441 | } | |
442 | } | |
443 | ||
15d1a84c LOK |
444 | void CCECBusDevice::SetOSDName(CStdString strName) |
445 | { | |
446 | CLockObject lock(&m_mutex); | |
447 | if (m_strDeviceName != strName) | |
448 | { | |
449 | CStdString strLog; | |
450 | strLog.Format(">> %s (%X): osd name set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, strName); | |
451 | m_processor->AddLog(CEC_LOG_DEBUG, strLog); | |
452 | m_strDeviceName = strName; | |
453 | } | |
454 | } | |
455 | ||
28fa6c97 LOK |
456 | void CCECBusDevice::SetMenuState(const cec_menu_state state) |
457 | { | |
81a1e39d | 458 | CLockObject lock(&m_mutex); |
28fa6c97 LOK |
459 | if (m_menuState != state) |
460 | { | |
461 | CStdString strLog; | |
462 | strLog.Format(">> %s (%X): menu state set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_menuState)); | |
463 | m_processor->AddLog(CEC_LOG_DEBUG, strLog); | |
464 | m_menuState = state; | |
465 | } | |
466 | } | |
467 | ||
7856411b LOK |
468 | void CCECBusDevice::SetInactiveDevice(void) |
469 | { | |
470 | CLockObject lock(&m_mutex); | |
471 | m_bActiveSource = false; | |
472 | } | |
473 | ||
474 | void CCECBusDevice::SetActiveDevice(void) | |
475 | { | |
476 | CLockObject lock(&m_mutex); | |
477 | ||
478 | for (int iPtr = 0; iPtr < 16; iPtr++) | |
479 | if (iPtr != m_iLogicalAddress) | |
480 | m_processor->m_busDevices[iPtr]->SetInactiveDevice(); | |
481 | ||
482 | m_bActiveSource = true; | |
483 | m_powerStatus = CEC_POWER_STATUS_ON; | |
484 | } | |
485 | ||
93fff5c1 LOK |
486 | bool CCECBusDevice::TryLogicalAddress(void) |
487 | { | |
488 | CStdString strLog; | |
489 | strLog.Format("trying logical address '%s'", GetLogicalAddressName()); | |
490 | AddLog(CEC_LOG_DEBUG, strLog); | |
491 | ||
492 | m_processor->SetAckMask(0x1 << m_iLogicalAddress); | |
493 | if (!TransmitPoll(m_iLogicalAddress)) | |
494 | { | |
495 | strLog.Format("using logical address '%s'", GetLogicalAddressName()); | |
496 | AddLog(CEC_LOG_NOTICE, strLog); | |
497 | SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC); | |
498 | ||
499 | return true; | |
500 | } | |
501 | ||
502 | strLog.Format("logical address '%s' already taken", GetLogicalAddressName()); | |
503 | AddLog(CEC_LOG_DEBUG, strLog); | |
504 | SetDeviceStatus(CEC_DEVICE_STATUS_PRESENT); | |
505 | return false; | |
506 | } | |
507 | ||
508 | void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus) | |
509 | { | |
510 | CLockObject lock(&m_mutex); | |
511 | switch (newStatus) | |
512 | { | |
513 | case CEC_DEVICE_STATUS_UNKNOWN: | |
514 | m_iStreamPath = 0; | |
515 | m_powerStatus = CEC_POWER_STATUS_UNKNOWN; | |
516 | m_vendor = CEC_VENDOR_UNKNOWN; | |
517 | m_menuState = CEC_MENU_STATE_ACTIVATED; | |
518 | m_bActiveSource = false; | |
519 | m_iLastCommandSent = 0; | |
520 | m_iLastActive = 0; | |
521 | m_cecVersion = CEC_VERSION_UNKNOWN; | |
522 | m_deviceStatus = newStatus; | |
523 | break; | |
524 | case CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC: | |
525 | m_iStreamPath = 0; | |
526 | m_powerStatus = CEC_POWER_STATUS_ON; | |
527 | m_vendor = CEC_VENDOR_UNKNOWN; | |
528 | m_menuState = CEC_MENU_STATE_ACTIVATED; | |
529 | m_bActiveSource = false; | |
530 | m_iLastCommandSent = 0; | |
531 | m_iLastActive = 0; | |
532 | m_cecVersion = CEC_VERSION_1_3A; | |
533 | m_deviceStatus = newStatus; | |
534 | break; | |
535 | case CEC_DEVICE_STATUS_PRESENT: | |
536 | case CEC_DEVICE_STATUS_NOT_PRESENT: | |
537 | m_deviceStatus = newStatus; | |
538 | break; | |
539 | } | |
540 | } | |
541 | ||
9dc04b07 | 542 | void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress) |
93729720 | 543 | { |
81a1e39d | 544 | CLockObject lock(&m_mutex); |
b8176e3c | 545 | if (iNewAddress > 0 && m_iPhysicalAddress != iNewAddress) |
93729720 LOK |
546 | { |
547 | CStdString strLog; | |
62f5527d | 548 | strLog.Format(">> %s (%X): physical address changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress, iNewAddress); |
93729720 LOK |
549 | AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
550 | ||
551 | m_iPhysicalAddress = iNewAddress; | |
552 | } | |
553 | } | |
554 | ||
9dc04b07 LOK |
555 | void CCECBusDevice::SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress /* = 0 */) |
556 | { | |
81a1e39d | 557 | CLockObject lock(&m_mutex); |
9dc04b07 LOK |
558 | if (iNewAddress > 0) |
559 | { | |
560 | CStdString strLog; | |
68a12fbf | 561 | strLog.Format(">> %s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, iOldAddress == 0 ? m_iStreamPath : iOldAddress, iNewAddress); |
9dc04b07 LOK |
562 | AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
563 | ||
564 | m_iStreamPath = iNewAddress; | |
96274140 LOK |
565 | |
566 | if (iNewAddress > 0) | |
81a1e39d LOK |
567 | { |
568 | lock.Leave(); | |
96274140 | 569 | SetPowerStatus(CEC_POWER_STATUS_ON); |
81a1e39d | 570 | } |
9dc04b07 LOK |
571 | } |
572 | } | |
573 | ||
e55f3f70 LOK |
574 | void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus) |
575 | { | |
81a1e39d | 576 | CLockObject lock(&m_mutex); |
b0271d54 LOK |
577 | if (m_powerStatus != powerStatus) |
578 | { | |
579 | CStdString strLog; | |
c4098482 | 580 | strLog.Format(">> %s (%X): power status changed from '%s' to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_powerStatus), ToString(powerStatus)); |
b0271d54 LOK |
581 | m_processor->AddLog(CEC_LOG_DEBUG, strLog); |
582 | m_powerStatus = powerStatus; | |
583 | } | |
e55f3f70 LOK |
584 | } |
585 | ||
c4098482 | 586 | void CCECBusDevice::SetVendorId(uint64_t iVendorId) |
e9de9629 | 587 | { |
e9de9629 | 588 | { |
81a1e39d LOK |
589 | CLockObject lock(&m_mutex); |
590 | m_vendor = (cec_vendor_id)iVendorId; | |
591 | ||
592 | switch (iVendorId) | |
0ab58650 | 593 | { |
81a1e39d LOK |
594 | case CEC_VENDOR_SAMSUNG: |
595 | if (m_handler->GetVendorId() != CEC_VENDOR_SAMSUNG) | |
596 | { | |
597 | delete m_handler; | |
598 | m_handler = new CANCommandHandler(this); | |
599 | } | |
600 | break; | |
601 | case CEC_VENDOR_LG: | |
602 | if (m_handler->GetVendorId() != CEC_VENDOR_LG) | |
603 | { | |
604 | delete m_handler; | |
605 | m_handler = new CSLCommandHandler(this); | |
606 | } | |
607 | break; | |
608 | case CEC_VENDOR_PANASONIC: | |
609 | if (m_handler->GetVendorId() != CEC_VENDOR_PANASONIC) | |
610 | { | |
611 | delete m_handler; | |
612 | m_handler = new CVLCommandHandler(this); | |
613 | } | |
614 | break; | |
615 | default: | |
616 | if (m_handler->GetVendorId() != CEC_VENDOR_UNKNOWN) | |
617 | { | |
618 | delete m_handler; | |
619 | m_handler = new CCECCommandHandler(this); | |
620 | } | |
621 | break; | |
0ab58650 | 622 | } |
e9de9629 LOK |
623 | } |
624 | ||
625 | CStdString strLog; | |
81a1e39d | 626 | strLog.Format("%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress, GetVendorName(), m_vendor); |
e9de9629 LOK |
627 | m_processor->AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
628 | } | |
93729720 | 629 | //@} |
e9de9629 | 630 | |
93729720 LOK |
631 | /** @name Transmit methods */ |
632 | //@{ | |
633 | bool CCECBusDevice::TransmitActiveSource(void) | |
0f23c85c | 634 | { |
81a1e39d | 635 | CLockObject lock(&m_mutex); |
58c1f6f5 LOK |
636 | if (m_powerStatus != CEC_POWER_STATUS_ON) |
637 | { | |
638 | CStdString strLog; | |
639 | strLog.Format("<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress); | |
640 | AddLog(CEC_LOG_DEBUG, strLog); | |
641 | } | |
642 | else if (m_bActiveSource) | |
8747dd4f LOK |
643 | { |
644 | CStdString strLog; | |
62f5527d | 645 | strLog.Format("<< %s (%X) -> broadcast (F): active source (%4x)", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress); |
8747dd4f | 646 | AddLog(CEC_LOG_NOTICE, strLog); |
0f23c85c | 647 | |
8747dd4f | 648 | cec_command command; |
ab1469a0 LOK |
649 | cec_command::Format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE); |
650 | command.parameters.PushBack((uint8_t) ((m_iPhysicalAddress >> 8) & 0xFF)); | |
651 | command.parameters.PushBack((uint8_t) (m_iPhysicalAddress & 0xFF)); | |
0f23c85c | 652 | |
81a1e39d | 653 | lock.Leave(); |
8747dd4f LOK |
654 | return m_processor->Transmit(command); |
655 | } | |
656 | else | |
657 | { | |
658 | CStdString strLog; | |
62f5527d | 659 | strLog.Format("<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress); |
8747dd4f LOK |
660 | AddLog(CEC_LOG_DEBUG, strLog); |
661 | } | |
662 | ||
663 | return false; | |
0f23c85c LOK |
664 | } |
665 | ||
29912296 | 666 | bool CCECBusDevice::TransmitCECVersion(cec_logical_address dest) |
0f23c85c | 667 | { |
81a1e39d | 668 | CLockObject lock(&m_mutex); |
d7be392a | 669 | CStdString strLog; |
c4098482 | 670 | strLog.Format("<< %s (%X) -> %s (%X): cec version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_cecVersion)); |
d7be392a | 671 | AddLog(CEC_LOG_NOTICE, strLog); |
0f23c85c LOK |
672 | |
673 | cec_command command; | |
ab1469a0 LOK |
674 | cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_CEC_VERSION); |
675 | command.parameters.PushBack((uint8_t)m_cecVersion); | |
0f23c85c | 676 | |
81a1e39d | 677 | lock.Leave(); |
0f23c85c LOK |
678 | return m_processor->Transmit(command); |
679 | } | |
680 | ||
ab27363d | 681 | bool CCECBusDevice::TransmitInactiveSource(void) |
93729720 | 682 | { |
d7be392a | 683 | CStdString strLog; |
ab27363d | 684 | strLog.Format("<< %s (%X) -> broadcast (F): inactive source", GetLogicalAddressName(), m_iLogicalAddress); |
d7be392a | 685 | AddLog(CEC_LOG_NOTICE, strLog); |
93729720 LOK |
686 | |
687 | cec_command command; | |
ab27363d | 688 | cec_command::Format(command, m_iLogicalAddress, CECDEVICE_TV, CEC_OPCODE_INACTIVE_SOURCE); |
ab1469a0 LOK |
689 | command.parameters.PushBack((m_iPhysicalAddress >> 8) & 0xFF); |
690 | command.parameters.PushBack(m_iPhysicalAddress & 0xFF); | |
93729720 LOK |
691 | |
692 | return m_processor->Transmit(command); | |
693 | } | |
694 | ||
29912296 | 695 | bool CCECBusDevice::TransmitMenuState(cec_logical_address dest) |
0f23c85c | 696 | { |
d7be392a | 697 | CStdString strLog; |
28fa6c97 | 698 | strLog.Format("<< %s (%X) -> %s (%X): menu state '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_menuState)); |
d7be392a | 699 | AddLog(CEC_LOG_NOTICE, strLog); |
0f23c85c LOK |
700 | |
701 | cec_command command; | |
ab1469a0 LOK |
702 | cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_MENU_STATUS); |
703 | command.parameters.PushBack((uint8_t)m_menuState); | |
0f23c85c LOK |
704 | |
705 | return m_processor->Transmit(command); | |
706 | } | |
707 | ||
29912296 | 708 | bool CCECBusDevice::TransmitOSDName(cec_logical_address dest) |
0f23c85c | 709 | { |
81a1e39d | 710 | CLockObject lock(&m_mutex); |
0f23c85c | 711 | CStdString strLog; |
c4098482 | 712 | strLog.Format("<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, m_strDeviceName.c_str()); |
0f23c85c LOK |
713 | AddLog(CEC_LOG_NOTICE, strLog.c_str()); |
714 | ||
715 | cec_command command; | |
ab1469a0 | 716 | cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_SET_OSD_NAME); |
787a3cb8 | 717 | for (unsigned int iPtr = 0; iPtr < m_strDeviceName.length(); iPtr++) |
ab1469a0 | 718 | command.parameters.PushBack(m_strDeviceName.at(iPtr)); |
0f23c85c | 719 | |
81a1e39d | 720 | lock.Leave(); |
0f23c85c LOK |
721 | return m_processor->Transmit(command); |
722 | } | |
723 | ||
38bdb943 LOK |
724 | bool CCECBusDevice::TransmitOSDString(cec_logical_address dest, cec_display_control duration, const char *strMessage) |
725 | { | |
81a1e39d | 726 | CLockObject lock(&m_mutex); |
38bdb943 | 727 | CStdString strLog; |
c4098482 | 728 | strLog.Format("<< %s (%X) -> %s (%X): display OSD message '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, strMessage); |
38bdb943 LOK |
729 | AddLog(CEC_LOG_NOTICE, strLog.c_str()); |
730 | ||
731 | cec_command command; | |
ab1469a0 LOK |
732 | cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_SET_OSD_STRING); |
733 | command.parameters.PushBack((uint8_t)duration); | |
38bdb943 LOK |
734 | |
735 | unsigned int iLen = strlen(strMessage); | |
736 | if (iLen > 13) iLen = 13; | |
737 | ||
738 | for (unsigned int iPtr = 0; iPtr < iLen; iPtr++) | |
ab1469a0 | 739 | command.parameters.PushBack(strMessage[iPtr]); |
38bdb943 | 740 | |
81a1e39d | 741 | lock.Leave(); |
38bdb943 LOK |
742 | return m_processor->Transmit(command); |
743 | } | |
744 | ||
29912296 | 745 | bool CCECBusDevice::TransmitPhysicalAddress(void) |
0f23c85c | 746 | { |
81a1e39d | 747 | CLockObject lock(&m_mutex); |
0f23c85c | 748 | CStdString strLog; |
62f5527d | 749 | strLog.Format("<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress); |
0f23c85c LOK |
750 | AddLog(CEC_LOG_NOTICE, strLog.c_str()); |
751 | ||
752 | cec_command command; | |
ab1469a0 LOK |
753 | cec_command::Format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS); |
754 | command.parameters.PushBack((uint8_t) ((m_iPhysicalAddress >> 8) & 0xFF)); | |
755 | command.parameters.PushBack((uint8_t) (m_iPhysicalAddress & 0xFF)); | |
756 | command.parameters.PushBack((uint8_t) (m_type)); | |
0f23c85c | 757 | |
81a1e39d | 758 | lock.Leave(); |
0f23c85c LOK |
759 | return m_processor->Transmit(command); |
760 | } | |
761 | ||
93729720 | 762 | bool CCECBusDevice::TransmitPoll(cec_logical_address dest) |
57f45e6c LOK |
763 | { |
764 | bool bReturn(false); | |
93729720 LOK |
765 | if (dest == CECDEVICE_UNKNOWN) |
766 | dest = m_iLogicalAddress; | |
f8513317 | 767 | |
57f45e6c | 768 | CStdString strLog; |
c4098482 | 769 | strLog.Format("<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest); |
d7be392a | 770 | AddLog(CEC_LOG_NOTICE, strLog.c_str()); |
57f45e6c LOK |
771 | |
772 | cec_command command; | |
ab1469a0 | 773 | cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_NONE); |
57f45e6c | 774 | |
1674de37 LOK |
775 | { |
776 | CLockObject lock(&m_transmitMutex); | |
777 | bReturn = m_processor->Transmit(command); | |
778 | } | |
779 | ||
57f45e6c | 780 | AddLog(CEC_LOG_DEBUG, bReturn ? ">> POLL sent" : ">> POLL not sent"); |
1674de37 LOK |
781 | |
782 | if (bReturn) | |
783 | { | |
784 | CLockObject lock(&m_mutex); | |
785 | m_iLastActive = GetTimeMs(); | |
786 | } | |
787 | ||
57f45e6c LOK |
788 | return bReturn; |
789 | } | |
93729720 LOK |
790 | |
791 | bool CCECBusDevice::TransmitPowerState(cec_logical_address dest) | |
792 | { | |
81a1e39d | 793 | CLockObject lock(&m_mutex); |
93729720 | 794 | CStdString strLog; |
c4098482 | 795 | strLog.Format("<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_powerStatus)); |
d7be392a LOK |
796 | AddLog(CEC_LOG_NOTICE, strLog.c_str()); |
797 | ||
93729720 | 798 | cec_command command; |
ab1469a0 LOK |
799 | cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_REPORT_POWER_STATUS); |
800 | command.parameters.PushBack((uint8_t) m_powerStatus); | |
93729720 | 801 | |
81a1e39d | 802 | lock.Leave(); |
93729720 LOK |
803 | return m_processor->Transmit(command); |
804 | } | |
805 | ||
806 | bool CCECBusDevice::TransmitVendorID(cec_logical_address dest) | |
807 | { | |
81a1e39d | 808 | CLockObject lock(&m_mutex); |
c4098482 LOK |
809 | if (m_vendor == CEC_VENDOR_UNKNOWN) |
810 | { | |
811 | CStdString strLog; | |
812 | strLog.Format("<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest); | |
813 | AddLog(CEC_LOG_NOTICE, strLog); | |
d7be392a | 814 | |
81a1e39d | 815 | lock.Leave(); |
c4098482 LOK |
816 | m_processor->TransmitAbort(dest, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID); |
817 | return false; | |
818 | } | |
819 | else | |
820 | { | |
821 | CStdString strLog; | |
822 | strLog.Format("<< %s (%X) -> %s (%X): vendor id %s (%x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_vendor), (uint64_t)m_vendor); | |
823 | AddLog(CEC_LOG_NOTICE, strLog); | |
824 | ||
825 | cec_command command; | |
5e9b399e | 826 | cec_command::Format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_DEVICE_VENDOR_ID); |
c4098482 | 827 | |
ab1469a0 LOK |
828 | command.parameters.PushBack((uint8_t) (((uint64_t)m_vendor >> 16) & 0xFF)); |
829 | command.parameters.PushBack((uint8_t) (((uint64_t)m_vendor >> 8) & 0xFF)); | |
830 | command.parameters.PushBack((uint8_t) ((uint64_t)m_vendor & 0xFF)); | |
c4098482 | 831 | |
81a1e39d | 832 | lock.Leave(); |
c4098482 LOK |
833 | return m_processor->Transmit(command); |
834 | } | |
93729720 | 835 | } |
a33794d8 LOK |
836 | |
837 | bool CCECBusDevice::SendKeypress(cec_user_control_code key, bool bWait /* = false */) | |
838 | { | |
839 | { | |
840 | CLockObject lock(&m_transmitMutex); | |
841 | cec_command command; | |
842 | cec_command::Format(command, m_processor->GetLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_USER_CONTROL_PRESSED); | |
988de7b9 | 843 | command.parameters.PushBack((uint8_t)key); |
a33794d8 LOK |
844 | |
845 | if (bWait) | |
846 | { | |
847 | if (m_processor->Transmit(command)) | |
848 | return m_condition.Wait(&m_transmitMutex, 1000); | |
849 | return false; | |
850 | } | |
851 | ||
852 | return m_processor->Transmit(command); | |
853 | } | |
854 | } | |
855 | ||
856 | bool CCECBusDevice::SendKeyRelease(bool bWait /* = false */) | |
857 | { | |
858 | { | |
859 | CLockObject lock(&m_transmitMutex); | |
860 | cec_command command; | |
861 | cec_command::Format(command, m_processor->GetLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_USER_CONTROL_RELEASE); | |
862 | ||
863 | if (bWait) | |
864 | { | |
865 | if (m_processor->Transmit(command)) | |
866 | return m_condition.Wait(&m_transmitMutex, 1000); | |
867 | return false; | |
868 | } | |
869 | else | |
870 | { | |
871 | return m_processor->Transmit(command); | |
872 | } | |
873 | } | |
874 | } | |
93729720 | 875 | //@} |