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