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