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