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