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