Commit | Line | Data |
---|---|---|
abbca718 LOK |
1 | /* |
2 | * This file is part of the libCEC(R) library. | |
3 | * | |
b492c10e | 4 | * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved. |
abbca718 LOK |
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 | ||
2abe74eb | 33 | #include "CECProcessor.h" |
abbca718 | 34 | |
7bb4ed43 | 35 | #include "adapter/USBCECAdapterCommunication.h" |
eafa9d46 | 36 | #include "devices/CECBusDevice.h" |
51b2a094 LOK |
37 | #include "devices/CECAudioSystem.h" |
38 | #include "devices/CECPlaybackDevice.h" | |
39 | #include "devices/CECRecordingDevice.h" | |
40 | #include "devices/CECTuner.h" | |
41 | #include "devices/CECTV.h" | |
62f5527d | 42 | #include "implementations/CECCommandHandler.h" |
2abe74eb | 43 | #include "LibCEC.h" |
004b8382 | 44 | #include "CECClient.h" |
ba65909d | 45 | #include "platform/util/timeutils.h" |
abbca718 LOK |
46 | |
47 | using namespace CEC; | |
48 | using namespace std; | |
f00ff009 | 49 | using namespace PLATFORM; |
abbca718 | 50 | |
b32ffd87 LOK |
51 | #define CEC_PROCESSOR_SIGNAL_WAIT_TIME 1000 |
52 | ||
004b8382 LOK |
53 | #define ToString(x) m_libcec->ToString(x) |
54 | ||
55 | CCECProcessor::CCECProcessor(CLibCEC *libcec) : | |
caca2d81 | 56 | m_bInitialised(false), |
caca2d81 | 57 | m_communication(NULL), |
004b8382 | 58 | m_libcec(libcec), |
caca2d81 | 59 | m_bMonitor(false), |
004b8382 | 60 | m_iPreviousAckMask(0), |
caca2d81 LOK |
61 | m_iStandardLineTimeout(3), |
62 | m_iRetryLineTimeout(3), | |
30b4aac0 | 63 | m_iLastTransmission(0) |
caca2d81 | 64 | { |
004b8382 | 65 | m_busDevices = new CCECDeviceMap(this); |
caca2d81 LOK |
66 | } |
67 | ||
004b8382 | 68 | CCECProcessor::~CCECProcessor(void) |
f8513317 | 69 | { |
004b8382 LOK |
70 | Close(); |
71 | delete m_busDevices; | |
caca2d81 LOK |
72 | } |
73 | ||
004b8382 | 74 | bool CCECProcessor::Start(const char *strPort, uint16_t iBaudRate /* = CEC_SERIAL_DEFAULT_BAUDRATE */, uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */) |
caca2d81 | 75 | { |
004b8382 LOK |
76 | CLockObject lock(m_mutex); |
77 | if (!OpenConnection(strPort, iBaudRate, iTimeoutMs)) | |
78 | return false; | |
79 | ||
80 | /* create the processor thread */ | |
81 | if (!IsRunning()) | |
51b2a094 | 82 | { |
004b8382 LOK |
83 | if (CreateThread()) |
84 | m_libcec->AddLog(CEC_LOG_DEBUG, "processor thread started"); | |
85 | else | |
51b2a094 | 86 | { |
004b8382 LOK |
87 | m_libcec->AddLog(CEC_LOG_ERROR, "could not create a processor thread"); |
88 | return false; | |
51b2a094 LOK |
89 | } |
90 | } | |
f8513317 | 91 | |
004b8382 | 92 | SetCECInitialised(true); |
7c63a480 | 93 | |
004b8382 | 94 | return true; |
abbca718 LOK |
95 | } |
96 | ||
eca71746 LOK |
97 | void CCECProcessor::Close(void) |
98 | { | |
004b8382 | 99 | SetCECInitialised(false); |
eca71746 LOK |
100 | StopThread(); |
101 | ||
004b8382 | 102 | if (m_communication) |
eca71746 | 103 | { |
eca71746 LOK |
104 | delete m_communication; |
105 | m_communication = NULL; | |
106 | } | |
99aeafb9 LOK |
107 | |
108 | m_bMonitor = false; | |
109 | m_iPreviousAckMask = 0; | |
110 | m_iStandardLineTimeout = 3; | |
111 | m_iRetryLineTimeout = 3; | |
112 | m_iLastTransmission = 0; | |
113 | m_busDevices->ResetDeviceStatus(); | |
eca71746 LOK |
114 | } |
115 | ||
f80cd208 | 116 | bool CCECProcessor::OpenConnection(const char *strPort, uint16_t iBaudRate, uint32_t iTimeoutMs, bool bStartListening /* = true */) |
abbca718 | 117 | { |
a674fd68 | 118 | bool bReturn(false); |
80726797 LOK |
119 | Close(); |
120 | ||
578c3905 | 121 | { |
80726797 | 122 | CLockObject lock(m_mutex); |
004b8382 | 123 | if (m_communication && m_communication->IsOpen()) |
80726797 | 124 | { |
004b8382 LOK |
125 | m_libcec->AddLog(CEC_LOG_ERROR, "connection already opened"); |
126 | return true; | |
80726797 | 127 | } |
004b8382 LOK |
128 | else if (!m_communication) |
129 | m_communication = new CUSBCECAdapterCommunication(this, strPort, iBaudRate); | |
578c3905 | 130 | } |
1113cb7d | 131 | |
3b5e433a | 132 | CTimeout timeout(iTimeoutMs > 0 ? iTimeoutMs : CEC_DEFAULT_TRANSMIT_WAIT); |
c520af4f | 133 | |
578c3905 | 134 | /* open a new connection */ |
efed01e1 | 135 | unsigned iConnectTry(0); |
a75e3a5a | 136 | while (timeout.TimeLeft() > 0 && (bReturn = m_communication->Open((timeout.TimeLeft() / CEC_CONNECT_TRIES), false, bStartListening)) == false) |
3d78df91 | 137 | { |
004b8382 | 138 | m_libcec->AddLog(CEC_LOG_ERROR, "could not open a connection (try %d)", ++iConnectTry); |
d55f263f | 139 | m_communication->Close(); |
b32ffd87 | 140 | CEvent::Sleep(CEC_DEFAULT_CONNECT_RETRY_WAIT); |
3d78df91 | 141 | } |
56035c86 | 142 | |
004b8382 | 143 | m_libcec->AddLog(CEC_LOG_NOTICE, "connection opened"); |
12a36be9 | 144 | |
578c3905 LOK |
145 | return bReturn; |
146 | } | |
147 | ||
004b8382 | 148 | bool CCECProcessor::CECInitialised(void) |
0cfdeb5a | 149 | { |
0b714871 | 150 | CLockObject lock(m_threadMutex); |
0cfdeb5a LOK |
151 | return m_bInitialised; |
152 | } | |
153 | ||
004b8382 | 154 | void CCECProcessor::SetCECInitialised(bool bSetTo /* = true */) |
578c3905 | 155 | { |
578c3905 | 156 | CLockObject lock(m_mutex); |
2db8981f | 157 | m_bInitialised = bSetTo; |
004b8382 LOK |
158 | if (!bSetTo) |
159 | UnregisterClients(); | |
abbca718 LOK |
160 | } |
161 | ||
b58d9277 | 162 | bool CCECProcessor::TryLogicalAddress(cec_logical_address address) |
f8513317 | 163 | { |
004b8382 LOK |
164 | CCECBusDevice *device = m_busDevices->At(address); |
165 | if (device) | |
c39611ec | 166 | { |
004b8382 LOK |
167 | if (device->IsPresent() || device->IsHandledByLibCEC()) |
168 | return false; | |
c39611ec | 169 | |
004b8382 LOK |
170 | SetAckMask(0); |
171 | return device->TryLogicalAddress(); | |
f8513317 LOK |
172 | } |
173 | ||
004b8382 | 174 | return false; |
f8513317 LOK |
175 | } |
176 | ||
a3ffc068 LOK |
177 | void CCECProcessor::ReplaceHandlers(void) |
178 | { | |
004b8382 | 179 | if (!CECInitialised()) |
0cfdeb5a | 180 | return; |
004b8382 LOK |
181 | |
182 | for (CECDEVICEMAP::iterator it = m_busDevices->Begin(); it != m_busDevices->End(); it++) | |
183 | it->second->ReplaceHandler(true); | |
a3ffc068 LOK |
184 | } |
185 | ||
b1f94db1 LOK |
186 | bool CCECProcessor::OnCommandReceived(const cec_command &command) |
187 | { | |
a4b9f561 | 188 | return m_inBuffer.Push(command); |
b1f94db1 LOK |
189 | } |
190 | ||
2abe74eb | 191 | void *CCECProcessor::Process(void) |
f99bc831 | 192 | { |
004b8382 | 193 | m_libcec->AddLog(CEC_LOG_DEBUG, "processor thread started"); |
abbca718 | 194 | |
a4b9f561 LOK |
195 | cec_command command; |
196 | command.Clear(); | |
197 | ||
b1f94db1 | 198 | while (!IsStopped() && m_communication->IsOpen()) |
abbca718 | 199 | { |
b32ffd87 | 200 | if (m_inBuffer.Pop(command, CEC_PROCESSOR_SIGNAL_WAIT_TIME)) |
a4b9f561 LOK |
201 | ParseCommand(command); |
202 | ||
004b8382 LOK |
203 | if (CECInitialised()) |
204 | { | |
205 | ReplaceHandlers(); | |
8ac9c610 | 206 | |
004b8382 LOK |
207 | m_libcec->CheckKeypressTimeout(); |
208 | } | |
8ac9c610 LOK |
209 | } |
210 | ||
004b8382 | 211 | return NULL; |
8ac9c610 LOK |
212 | } |
213 | ||
004b8382 | 214 | bool CCECProcessor::SetActiveSource(uint16_t iStreamPath) |
a9232a79 | 215 | { |
004b8382 | 216 | bool bReturn(false); |
a9232a79 | 217 | |
004b8382 LOK |
218 | CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamPath); |
219 | if (device) | |
220 | bReturn = device->ActivateSource(); | |
221 | else | |
222 | m_libcec->AddLog(CEC_LOG_DEBUG, "device with PA '%04x' not found", iStreamPath); | |
a9232a79 | 223 | |
004b8382 | 224 | return bReturn; |
a9232a79 LOK |
225 | } |
226 | ||
004b8382 | 227 | void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout) |
842262d8 | 228 | { |
004b8382 LOK |
229 | CLockObject lock(m_mutex); |
230 | m_iStandardLineTimeout = iTimeout; | |
842262d8 LOK |
231 | } |
232 | ||
004b8382 | 233 | void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout) |
6a1c0009 | 234 | { |
004b8382 LOK |
235 | CLockObject lock(m_mutex); |
236 | m_iRetryLineTimeout = iTimeout; | |
6a1c0009 LOK |
237 | } |
238 | ||
004b8382 | 239 | bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress) |
ed21be2a | 240 | { |
004b8382 LOK |
241 | CCECBusDevice *device = GetDeviceByPhysicalAddress(iPhysicalAddress); |
242 | return device != NULL; | |
243 | } | |
ed21be2a | 244 | |
004b8382 LOK |
245 | void CCECProcessor::LogOutput(const cec_command &data) |
246 | { | |
247 | CStdString strTx; | |
248 | strTx.Format("<< %02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination); | |
249 | if (data.opcode_set) | |
250 | strTx.AppendFormat(":%02x", (uint8_t)data.opcode); | |
ed21be2a | 251 | |
004b8382 LOK |
252 | for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++) |
253 | strTx.AppendFormat(":%02x", data.parameters[iPtr]); | |
254 | m_libcec->AddLog(CEC_LOG_TRAFFIC, strTx.c_str()); | |
ed21be2a LOK |
255 | } |
256 | ||
004b8382 | 257 | bool CCECProcessor::SwitchMonitoring(bool bEnable) |
a3269a0a | 258 | { |
004b8382 LOK |
259 | m_libcec->AddLog(CEC_LOG_NOTICE, "== %s monitoring mode ==", bEnable ? "enabling" : "disabling"); |
260 | ||
cc60ab1c | 261 | { |
004b8382 LOK |
262 | CLockObject lock(m_mutex); |
263 | m_bMonitor = bEnable; | |
264 | m_iPreviousAckMask = m_communication->GetAckMask(); | |
cc60ab1c | 265 | } |
a3269a0a | 266 | |
004b8382 LOK |
267 | if (bEnable) |
268 | return SetAckMask(0); | |
269 | else | |
270 | return SetAckMask(m_iPreviousAckMask); | |
e186a843 LOK |
271 | } |
272 | ||
004b8382 | 273 | bool CCECProcessor::PollDevice(cec_logical_address iAddress) |
44c74256 | 274 | { |
004b8382 LOK |
275 | CCECBusDevice *device = m_busDevices->At(iAddress); |
276 | CCECBusDevice *primary = GetPrimaryDevice(); | |
277 | if (device) | |
278 | { | |
279 | return primary ? | |
280 | primary->TransmitPoll(iAddress) : | |
281 | device->TransmitPoll(iAddress); | |
282 | } | |
cc60ab1c | 283 | return false; |
44c74256 LOK |
284 | } |
285 | ||
004b8382 | 286 | CCECBusDevice *CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bSuppressUpdate /* = true */) |
eab72c40 | 287 | { |
004b8382 LOK |
288 | return m_busDevices ? |
289 | m_busDevices->GetDeviceByPhysicalAddress(iPhysicalAddress, bSuppressUpdate) : | |
290 | NULL; | |
eab72c40 LOK |
291 | } |
292 | ||
004b8382 | 293 | CCECBusDevice *CCECProcessor::GetDevice(cec_logical_address address) const |
e55f3f70 | 294 | { |
004b8382 LOK |
295 | return m_busDevices ? |
296 | m_busDevices->At(address) : | |
297 | NULL; | |
e55f3f70 LOK |
298 | } |
299 | ||
5734016c | 300 | cec_logical_address CCECProcessor::GetActiveSource(bool bRequestActiveSource /* = true */) |
b4b1b49b | 301 | { |
004b8382 LOK |
302 | // get the device that is marked as active source from the device map |
303 | CCECBusDevice *activeSource = m_busDevices->GetActiveSource(); | |
304 | if (activeSource) | |
305 | return activeSource->GetLogicalAddress(); | |
b4b1b49b | 306 | |
004b8382 | 307 | if (bRequestActiveSource) |
5734016c | 308 | { |
004b8382 LOK |
309 | // request the active source from the bus |
310 | CCECBusDevice *primary = GetPrimaryDevice(); | |
5734016c | 311 | if (primary) |
004b8382 | 312 | { |
5734016c | 313 | primary->RequestActiveSource(); |
004b8382 LOK |
314 | return GetActiveSource(false); |
315 | } | |
5734016c LOK |
316 | } |
317 | ||
004b8382 | 318 | // unknown or none |
b4b1b49b LOK |
319 | return CECDEVICE_UNKNOWN; |
320 | } | |
321 | ||
322 | bool CCECProcessor::IsActiveSource(cec_logical_address iAddress) | |
323 | { | |
004b8382 LOK |
324 | CCECBusDevice *device = m_busDevices->At(iAddress); |
325 | return device && device->IsActiveSource(); | |
b4b1b49b LOK |
326 | } |
327 | ||
8d84e2c0 | 328 | bool CCECProcessor::Transmit(const cec_command &data) |
825ddb96 | 329 | { |
004b8382 LOK |
330 | CCECBusDevice *initiator = m_busDevices->At(data.initiator); |
331 | if (!initiator) | |
c4287bcd | 332 | { |
004b8382 | 333 | m_libcec->AddLog(CEC_LOG_WARNING, "invalid initiator"); |
c4287bcd LOK |
334 | return false; |
335 | } | |
336 | ||
004b8382 LOK |
337 | if (data.destination != CECDEVICE_BROADCAST) |
338 | { | |
339 | CCECBusDevice *destination = m_busDevices->At(data.destination); | |
340 | if (destination && destination->IsHandledByLibCEC()) | |
341 | { | |
342 | m_libcec->AddLog(CEC_LOG_WARNING, "not sending data to myself!"); | |
343 | return false; | |
344 | } | |
345 | } | |
346 | ||
7cf8ba15 | 347 | uint8_t iMaxTries(0); |
eb35e4ca | 348 | { |
7bb4ed43 | 349 | CLockObject lock(m_mutex); |
44a1d92a LOK |
350 | if (IsStopped()) |
351 | return false; | |
7bb4ed43 | 352 | LogOutput(data); |
5f316715 | 353 | m_iLastTransmission = GetTimeMs(); |
44195125 | 354 | if (!m_communication || !m_communication->IsOpen()) |
f9e01dac | 355 | { |
004b8382 | 356 | m_libcec->AddLog(CEC_LOG_ERROR, "cannot transmit command: connection closed"); |
7bb4ed43 | 357 | return false; |
f9e01dac | 358 | } |
004b8382 | 359 | iMaxTries = initiator->GetHandler()->GetTransmitRetries() + 1; |
2abe74eb LOK |
360 | } |
361 | ||
33dd87a9 MK |
362 | bool bRetry(true); |
363 | uint8_t iTries(0); | |
364 | uint8_t iLineTimeout = m_iStandardLineTimeout; | |
365 | cec_adapter_message_state adapterState = ADAPTER_MESSAGE_STATE_UNKNOWN; | |
366 | ||
367 | while (bRetry && ++iTries < iMaxTries) | |
368 | { | |
004b8382 | 369 | if (initiator->IsUnsupportedFeature(data.opcode)) |
33dd87a9 MK |
370 | return false; |
371 | ||
372 | adapterState = m_communication->Write(data, bRetry, iLineTimeout); | |
373 | iLineTimeout = m_iRetryLineTimeout; | |
374 | } | |
375 | ||
376 | return adapterState == ADAPTER_MESSAGE_STATE_SENT_ACKED; | |
825ddb96 | 377 | } |
abbca718 | 378 | |
004b8382 | 379 | void CCECProcessor::TransmitAbort(cec_logical_address source, cec_logical_address destination, cec_opcode opcode, cec_abort_reason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */) |
abbca718 | 380 | { |
004b8382 | 381 | m_libcec->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message"); |
9dee1670 | 382 | |
06a1f7ce | 383 | cec_command command; |
004b8382 | 384 | cec_command::Format(command, source, destination, CEC_OPCODE_FEATURE_ABORT); |
ab1469a0 LOK |
385 | command.parameters.PushBack((uint8_t)opcode); |
386 | command.parameters.PushBack((uint8_t)reason); | |
9dee1670 LOK |
387 | |
388 | Transmit(command); | |
abbca718 LOK |
389 | } |
390 | ||
b1f94db1 | 391 | void CCECProcessor::ParseCommand(const cec_command &command) |
abbca718 | 392 | { |
e9de9629 | 393 | CStdString dataStr; |
d297cbd4 LOK |
394 | dataStr.Format(">> %1x%1x", command.initiator, command.destination); |
395 | if (command.opcode_set == 1) | |
396 | dataStr.AppendFormat(":%02x", command.opcode); | |
e9de9629 LOK |
397 | for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++) |
398 | dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]); | |
004b8382 | 399 | m_libcec->AddLog(CEC_LOG_TRAFFIC, dataStr.c_str()); |
dc113aca | 400 | |
004b8382 | 401 | if (!m_bMonitor) |
6d858ba4 | 402 | { |
004b8382 LOK |
403 | CCECBusDevice *device = m_busDevices->At(command.initiator); |
404 | if (device) | |
405 | device->HandleCommand(command); | |
6d858ba4 | 406 | } |
6d858ba4 LOK |
407 | } |
408 | ||
37b0c572 | 409 | bool CCECProcessor::IsPresentDevice(cec_logical_address address) |
6d858ba4 | 410 | { |
004b8382 LOK |
411 | CCECBusDevice *device = m_busDevices->At(address); |
412 | return device && device->GetStatus() == CEC_DEVICE_STATUS_PRESENT; | |
6d858ba4 LOK |
413 | } |
414 | ||
37b0c572 | 415 | bool CCECProcessor::IsPresentDeviceType(cec_device_type type) |
6d858ba4 | 416 | { |
004b8382 LOK |
417 | CECDEVICEVEC devices; |
418 | m_busDevices->GetByType(type, devices); | |
419 | CCECDeviceMap::FilterActive(devices); | |
420 | return !devices.empty(); | |
6d858ba4 LOK |
421 | } |
422 | ||
004b8382 | 423 | uint16_t CCECProcessor::GetDetectedPhysicalAddress(void) const |
0f23c85c | 424 | { |
004b8382 | 425 | return m_communication ? m_communication->GetPhysicalAddress() : CEC_INVALID_PHYSICAL_ADDRESS; |
0f23c85c LOK |
426 | } |
427 | ||
06bfd4d7 LOK |
428 | bool CCECProcessor::SetAckMask(uint16_t iMask) |
429 | { | |
0b714871 | 430 | return m_communication ? m_communication->SetAckMask(iMask) : false; |
06bfd4d7 | 431 | } |
a33794d8 | 432 | |
004b8382 | 433 | bool CCECProcessor::StandbyDevices(const cec_logical_address initiator, const CECDEVICEVEC &devices) |
a33794d8 | 434 | { |
004b8382 LOK |
435 | bool bReturn(true); |
436 | for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++) | |
437 | bReturn &= (*it)->Standby(initiator); | |
438 | return bReturn; | |
a33794d8 LOK |
439 | } |
440 | ||
004b8382 | 441 | bool CCECProcessor::StandbyDevice(const cec_logical_address initiator, cec_logical_address address) |
a33794d8 | 442 | { |
004b8382 LOK |
443 | CCECBusDevice *device = m_busDevices->At(address); |
444 | return device ? device->Standby(initiator) : false; | |
a33794d8 | 445 | } |
7c63a480 | 446 | |
004b8382 | 447 | bool CCECProcessor::PowerOnDevices(const cec_logical_address initiator, const CECDEVICEVEC &devices) |
8670c970 | 448 | { |
004b8382 LOK |
449 | bool bReturn(true); |
450 | for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++) | |
451 | bReturn &= (*it)->PowerOn(initiator); | |
452 | return bReturn; | |
8670c970 LOK |
453 | } |
454 | ||
004b8382 | 455 | bool CCECProcessor::PowerOnDevice(const cec_logical_address initiator, cec_logical_address address) |
ca27e6cf | 456 | { |
004b8382 LOK |
457 | CCECBusDevice *device = m_busDevices->At(address); |
458 | return device ? device->PowerOn(initiator) : false; | |
ca27e6cf LOK |
459 | } |
460 | ||
004b8382 | 461 | bool CCECProcessor::StartBootloader(const char *strPort /* = NULL */) |
ca27e6cf | 462 | { |
004b8382 LOK |
463 | bool bReturn(false); |
464 | if (!m_communication && strPort) | |
ca27e6cf | 465 | { |
004b8382 LOK |
466 | IAdapterCommunication *comm = new CUSBCECAdapterCommunication(this, strPort); |
467 | CTimeout timeout(CEC_DEFAULT_CONNECT_TIMEOUT); | |
468 | int iConnectTry(0); | |
469 | while (timeout.TimeLeft() > 0 && (bReturn = comm->Open(timeout.TimeLeft() / CEC_CONNECT_TRIES, true)) == false) | |
470 | { | |
471 | m_libcec->AddLog(CEC_LOG_ERROR, "could not open a connection (try %d)", ++iConnectTry); | |
472 | comm->Close(); | |
473 | Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT); | |
474 | } | |
475 | if (comm->IsOpen()) | |
ca27e6cf | 476 | { |
004b8382 LOK |
477 | bReturn = comm->StartBootloader(); |
478 | delete comm; | |
ca27e6cf LOK |
479 | } |
480 | return bReturn; | |
481 | } | |
004b8382 | 482 | else |
3e61b350 | 483 | { |
004b8382 LOK |
484 | m_communication->StartBootloader(); |
485 | Close(); | |
486 | bReturn = true; | |
3e61b350 | 487 | } |
3e61b350 | 488 | |
004b8382 | 489 | return bReturn; |
03ae897d LOK |
490 | } |
491 | ||
004b8382 | 492 | bool CCECProcessor::PingAdapter(void) |
03ae897d | 493 | { |
004b8382 | 494 | return m_communication->PingAdapter(); |
03ae897d LOK |
495 | } |
496 | ||
004b8382 | 497 | void CCECProcessor::HandlePoll(cec_logical_address initiator, cec_logical_address destination) |
03ae897d | 498 | { |
004b8382 LOK |
499 | CCECBusDevice *device = m_busDevices->At(destination); |
500 | if (device) | |
501 | device->HandlePollFrom(initiator); | |
03ae897d LOK |
502 | } |
503 | ||
004b8382 | 504 | bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator) |
03ae897d | 505 | { |
004b8382 LOK |
506 | CCECBusDevice *device = m_busDevices->At(initiator); |
507 | return !device || !device->HandleReceiveFailed(); | |
03ae897d LOK |
508 | } |
509 | ||
004b8382 | 510 | bool CCECProcessor::SetStreamPath(uint16_t iPhysicalAddress) |
03ae897d | 511 | { |
004b8382 LOK |
512 | // stream path changes are sent by the TV |
513 | return GetTV()->GetHandler()->TransmitSetStreamPath(iPhysicalAddress); | |
03ae897d LOK |
514 | } |
515 | ||
004b8382 | 516 | bool CCECProcessor::CanPersistConfiguration(void) |
03ae897d | 517 | { |
004b8382 | 518 | return m_communication ? m_communication->GetFirmwareVersion() >= 2 : false; |
03ae897d LOK |
519 | } |
520 | ||
004b8382 | 521 | bool CCECProcessor::PersistConfiguration(libcec_configuration *configuration) |
03ae897d | 522 | { |
004b8382 | 523 | return m_communication ? m_communication->PersistConfiguration(configuration) : false; |
03ae897d LOK |
524 | } |
525 | ||
004b8382 | 526 | void CCECProcessor::RescanActiveDevices(void) |
03ae897d | 527 | { |
004b8382 LOK |
528 | for (CECDEVICEMAP::iterator it = m_busDevices->Begin(); it != m_busDevices->End(); it++) |
529 | it->second->GetStatus(true); | |
03ae897d LOK |
530 | } |
531 | ||
004b8382 | 532 | bool CCECProcessor::GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */) |
03ae897d | 533 | { |
004b8382 LOK |
534 | if (!OpenConnection(strPort, CEC_SERIAL_DEFAULT_BAUDRATE, iTimeoutMs, false)) |
535 | return false; | |
03ae897d | 536 | |
004b8382 LOK |
537 | config->iFirmwareVersion = m_communication->GetFirmwareVersion(); |
538 | config->iPhysicalAddress = m_communication->GetPhysicalAddress(); | |
539 | config->iFirmwareBuildDate = m_communication->GetFirmwareBuildDate(); | |
03ae897d | 540 | |
004b8382 | 541 | return true; |
caca2d81 LOK |
542 | } |
543 | ||
004b8382 | 544 | bool CCECProcessor::TransmitPendingActiveSourceCommands(void) |
3efda01a | 545 | { |
004b8382 LOK |
546 | bool bReturn(true); |
547 | for (CECDEVICEMAP::iterator it = m_busDevices->Begin(); it != m_busDevices->End(); it++) | |
548 | bReturn &= it->second->TransmitPendingActiveSourceCommands(); | |
549 | return bReturn; | |
3efda01a LOK |
550 | } |
551 | ||
004b8382 | 552 | CCECTV *CCECProcessor::GetTV(void) const |
1113cb7d | 553 | { |
004b8382 | 554 | return CCECBusDevice::AsTV(m_busDevices->At(CECDEVICE_TV)); |
1113cb7d LOK |
555 | } |
556 | ||
004b8382 | 557 | CCECAudioSystem *CCECProcessor::GetAudioSystem(void) const |
1113cb7d | 558 | { |
004b8382 | 559 | return CCECBusDevice::AsAudioSystem(m_busDevices->At(CECDEVICE_AUDIOSYSTEM)); |
1113cb7d | 560 | } |
6729ac71 | 561 | |
004b8382 | 562 | CCECPlaybackDevice *CCECProcessor::GetPlaybackDevice(cec_logical_address address) const |
6729ac71 | 563 | { |
004b8382 | 564 | return CCECBusDevice::AsPlaybackDevice(m_busDevices->At(address)); |
6729ac71 LOK |
565 | } |
566 | ||
004b8382 | 567 | CCECRecordingDevice *CCECProcessor::GetRecordingDevice(cec_logical_address address) const |
6729ac71 | 568 | { |
004b8382 | 569 | return CCECBusDevice::AsRecordingDevice(m_busDevices->At(address)); |
6729ac71 | 570 | } |
f42d3e0f | 571 | |
004b8382 | 572 | CCECTuner *CCECProcessor::GetTuner(cec_logical_address address) const |
f42d3e0f | 573 | { |
004b8382 | 574 | return CCECBusDevice::AsTuner(m_busDevices->At(address)); |
f42d3e0f | 575 | } |
d40928b5 | 576 | |
004b8382 | 577 | bool CCECProcessor::RegisterClient(CCECClient *client) |
30b4aac0 | 578 | { |
004b8382 LOK |
579 | if (!client) |
580 | return false; | |
30b4aac0 | 581 | |
004b8382 LOK |
582 | libcec_configuration &configuration = *client->GetConfiguration(); |
583 | m_libcec->AddLog(CEC_LOG_NOTICE, "registering new CEC client - v%s", ToString((cec_client_version)configuration.clientVersion)); | |
30b4aac0 | 584 | |
004b8382 LOK |
585 | client->SetRegistered(false); |
586 | client->SetInitialised(false); | |
30b4aac0 | 587 | |
004b8382 | 588 | uint16_t iPreviousMask(m_communication->GetAckMask()); |
30a09f32 | 589 | |
004b8382 LOK |
590 | // find logical addresses for this client |
591 | if (!client->FindLogicalAddresses()) | |
8670c970 | 592 | { |
004b8382 LOK |
593 | SetAckMask(iPreviousMask); |
594 | return false; | |
8670c970 LOK |
595 | } |
596 | ||
004b8382 LOK |
597 | // register this client on the new addresses |
598 | CECDEVICEVEC devices; | |
599 | m_busDevices->GetByLogicalAddresses(devices, configuration.logicalAddresses); | |
600 | for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++) | |
8670c970 | 601 | { |
004b8382 LOK |
602 | CLockObject lock(m_mutex); |
603 | m_clients.erase((*it)->GetLogicalAddress()); | |
604 | m_clients.insert(make_pair<cec_logical_address, CCECClient *>((*it)->GetLogicalAddress(), client)); | |
605 | client->SetRegistered(true); | |
8670c970 | 606 | } |
30b4aac0 | 607 | |
004b8382 LOK |
608 | // get the settings from the rom |
609 | if (configuration.bGetSettingsFromROM == 1) | |
8670c970 | 610 | { |
004b8382 LOK |
611 | libcec_configuration config; |
612 | m_communication->GetConfiguration(&config); | |
f0154449 | 613 | |
004b8382 LOK |
614 | CLockObject lock(m_mutex); |
615 | if (!config.deviceTypes.IsEmpty()) | |
616 | configuration.deviceTypes = config.deviceTypes; | |
617 | if (CLibCEC::IsValidPhysicalAddress(config.iPhysicalAddress)) | |
618 | configuration.iPhysicalAddress = config.iPhysicalAddress; | |
619 | snprintf(configuration.strDeviceName, 13, "%s", config.strDeviceName); | |
30b4aac0 LOK |
620 | } |
621 | ||
004b8382 LOK |
622 | // set the new ack mask |
623 | bool bReturn = SetAckMask(GetLogicalAddresses().AckMask()) && | |
624 | client->Initialise(); | |
30b4aac0 | 625 | |
004b8382 | 626 | // set the firmware version and build date |
99aeafb9 LOK |
627 | configuration.serverVersion = LIBCEC_VERSION_CURRENT; |
628 | configuration.iFirmwareVersion = m_communication->GetFirmwareVersion(); | |
004b8382 LOK |
629 | configuration.iFirmwareBuildDate = m_communication->GetFirmwareBuildDate(); |
630 | ||
631 | CStdString strLog; | |
632 | if (bReturn) | |
633 | strLog = "CEC client registered."; | |
634 | else | |
635 | strLog = "failed to register the CEC client."; | |
636 | strLog.AppendFormat(" libCEC version = %s, client version = %s, firmware version = %d", ToString((cec_server_version)configuration.serverVersion), ToString((cec_client_version)configuration.clientVersion), configuration.iFirmwareVersion); | |
637 | if (configuration.iFirmwareBuildDate != CEC_FW_BUILD_UNKNOWN) | |
30b4aac0 | 638 | { |
004b8382 LOK |
639 | time_t buildTime = (time_t)configuration.iFirmwareBuildDate; |
640 | strLog.AppendFormat(", firmware build date: %s", asctime(gmtime(&buildTime))); | |
641 | strLog = strLog.Left((int)strLog.length() - 1); // strip \n added by asctime | |
642 | strLog.append(" +0000"); | |
30b4aac0 LOK |
643 | } |
644 | ||
004b8382 | 645 | m_libcec->AddLog(bReturn ? CEC_LOG_NOTICE : CEC_LOG_ERROR, strLog); |
41e3372a | 646 | |
004b8382 | 647 | if (bReturn) |
fa4b80df | 648 | { |
004b8382 LOK |
649 | strLog = "Using logical address(es): "; |
650 | CECDEVICEVEC devices; | |
651 | m_busDevices->GetByLogicalAddresses(devices, configuration.logicalAddresses); | |
652 | for (CECDEVICEVEC::iterator it = devices.begin(); it != devices.end(); it++) | |
653 | strLog.AppendFormat("%s (%X) ", (*it)->GetLogicalAddressName(), (*it)->GetLogicalAddress()); | |
654 | m_libcec->AddLog(CEC_LOG_NOTICE, strLog); | |
fa4b80df | 655 | } |
b98fc43d | 656 | |
004b8382 LOK |
657 | // display a warning if the firmware can be upgraded |
658 | if (!m_communication->IsRunningLatestFirmware()) | |
3ef17606 | 659 | { |
004b8382 LOK |
660 | const char *strUpgradeMessage = "The firmware of this adapter can be upgraded. Please visit http://blog.pulse-eight.com/ for more information."; |
661 | m_libcec->AddLog(CEC_LOG_WARNING, strUpgradeMessage); | |
c30acafa LOK |
662 | libcec_parameter param; |
663 | param.paramData = (void*)strUpgradeMessage; param.paramType = CEC_PARAMETER_TYPE_STRING; | |
664 | client->Alert(CEC_ALERT_SERVICE_DEVICE, param); | |
3ef17606 | 665 | } |
004b8382 | 666 | else |
30b4aac0 | 667 | { |
004b8382 | 668 | m_libcec->AddLog(CEC_LOG_DEBUG, "the adapter is using the latest (known) firmware version"); |
30b4aac0 | 669 | } |
004b8382 LOK |
670 | |
671 | if (bReturn) | |
c3c13157 | 672 | { |
004b8382 LOK |
673 | /* get the vendor id from the TV, so we are using the correct handler */ |
674 | GetTV()->GetVendorId(configuration.logicalAddresses.primary); | |
c3c13157 | 675 | } |
30b4aac0 | 676 | |
43b2dfdd | 677 | return bReturn; |
30b4aac0 LOK |
678 | } |
679 | ||
004b8382 | 680 | void CCECProcessor::UnregisterClient(CCECClient *client) |
d40928b5 | 681 | { |
004b8382 | 682 | CLockObject lock(m_mutex); |
004b8382 | 683 | CECDEVICEVEC devices; |
99aeafb9 | 684 | m_busDevices->GetByLogicalAddresses(devices, client->GetConfiguration()->logicalAddresses); |
004b8382 | 685 | for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++) |
7f274e72 | 686 | { |
004b8382 LOK |
687 | map<cec_logical_address, CCECClient *>::iterator entry = m_clients.find((*it)->GetLogicalAddress()); |
688 | if (entry != m_clients.end()) | |
689 | m_clients.erase(entry); | |
7f274e72 | 690 | } |
d40928b5 | 691 | } |
224ea877 | 692 | |
004b8382 | 693 | void CCECProcessor::UnregisterClients(void) |
224ea877 | 694 | { |
004b8382 LOK |
695 | CLockObject lock(m_mutex); |
696 | for (map<cec_logical_address, CCECClient *>::iterator client = m_clients.begin(); client != m_clients.end(); client++) | |
697 | client->second->OnUnregister(); | |
698 | m_clients.clear(); | |
224ea877 LOK |
699 | } |
700 | ||
004b8382 | 701 | CCECClient *CCECProcessor::GetClient(const cec_logical_address address) |
224ea877 | 702 | { |
004b8382 LOK |
703 | CLockObject lock(m_mutex); |
704 | map<cec_logical_address, CCECClient *>::const_iterator client = m_clients.find(address); | |
705 | if (client != m_clients.end()) | |
706 | return client->second; | |
707 | return NULL; | |
224ea877 | 708 | } |
3efda01a | 709 | |
004b8382 | 710 | CCECClient *CCECProcessor::GetPrimaryClient(void) |
3efda01a | 711 | { |
004b8382 LOK |
712 | CLockObject lock(m_mutex); |
713 | map<cec_logical_address, CCECClient *>::const_iterator client = m_clients.begin(); | |
714 | if (client != m_clients.end()) | |
715 | return client->second; | |
716 | return NULL; | |
3efda01a | 717 | } |
f80cd208 | 718 | |
004b8382 | 719 | CCECBusDevice *CCECProcessor::GetPrimaryDevice(void) const |
f80cd208 | 720 | { |
004b8382 LOK |
721 | return m_busDevices->At(GetLogicalAddress()); |
722 | } | |
f80cd208 | 723 | |
004b8382 LOK |
724 | cec_logical_address CCECProcessor::GetLogicalAddress(void) const |
725 | { | |
726 | cec_logical_addresses addresses = GetLogicalAddresses(); | |
727 | return addresses.primary; | |
f80cd208 | 728 | } |
b78b4e33 | 729 | |
004b8382 | 730 | cec_logical_addresses CCECProcessor::GetLogicalAddresses(void) const |
b78b4e33 | 731 | { |
004b8382 | 732 | cec_logical_addresses addresses; |
c30acafa | 733 | addresses.Clear(); |
004b8382 LOK |
734 | for (map<cec_logical_address, CCECClient *>::const_iterator client = m_clients.begin(); client != m_clients.end(); client++) |
735 | addresses.Set(client->first); | |
736 | ||
737 | return addresses; | |
b78b4e33 | 738 | } |
d2d1660c | 739 | |
004b8382 | 740 | bool CCECProcessor::IsHandledByLibCEC(const cec_logical_address address) const |
d2d1660c | 741 | { |
004b8382 LOK |
742 | CCECBusDevice *device = GetDevice(address); |
743 | return device && device->IsHandledByLibCEC(); | |
d2d1660c | 744 | } |