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