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