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