Commit | Line | Data |
---|---|---|
abbca718 LOK |
1 | /* |
2 | * This file is part of the libCEC(R) library. | |
3 | * | |
4 | * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved. | |
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 | |
2abe74eb | 35 | #include "AdapterCommunication.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" |
abbca718 | 44 | #include "util/StdString.h" |
b9187cc6 | 45 | #include "platform/timeutils.h" |
abbca718 LOK |
46 | |
47 | using namespace CEC; | |
48 | using namespace std; | |
49 | ||
1113cb7d | 50 | CCECProcessor::CCECProcessor(CLibCEC *controller, const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) : |
f8513317 | 51 | m_bStarted(false), |
16b1e052 | 52 | m_iHDMIPort(CEC_DEFAULT_HDMI_PORT), |
d2f1c157 | 53 | m_iBaseDevice((cec_logical_address)CEC_DEFAULT_BASE_DEVICE), |
855a3a98 | 54 | m_lastInitiator(CECDEVICE_UNKNOWN), |
2abe74eb | 55 | m_strDeviceName(strDeviceName), |
8b7e5ff6 | 56 | m_controller(controller), |
7c63a480 | 57 | m_bMonitor(false), |
dcd240b2 LOK |
58 | m_busScan(NULL), |
59 | m_iStandardLineTimeout(3), | |
5f316715 LOK |
60 | m_iRetryLineTimeout(3), |
61 | m_iLastTransmission(0) | |
abbca718 | 62 | { |
1113cb7d | 63 | m_communication = new CAdapterCommunication(this); |
ab1469a0 LOK |
64 | m_logicalAddresses.Clear(); |
65 | m_logicalAddresses.Set(iLogicalAddress); | |
20b8870a | 66 | m_types.clear(); |
7871d66e | 67 | for (int iPtr = 0; iPtr <= 16; iPtr++) |
09c10b66 | 68 | m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, iPtr == iLogicalAddress ? iPhysicalAddress : 0); |
abbca718 LOK |
69 | } |
70 | ||
1113cb7d | 71 | CCECProcessor::CCECProcessor(CLibCEC *controller, const char *strDeviceName, const cec_device_type_list &types) : |
f8513317 | 72 | m_bStarted(false), |
16b1e052 | 73 | m_iHDMIPort(CEC_DEFAULT_HDMI_PORT), |
d2f1c157 | 74 | m_iBaseDevice((cec_logical_address)CEC_DEFAULT_BASE_DEVICE), |
f8513317 LOK |
75 | m_strDeviceName(strDeviceName), |
76 | m_types(types), | |
f8513317 | 77 | m_controller(controller), |
dcd240b2 LOK |
78 | m_bMonitor(false), |
79 | m_iStandardLineTimeout(3), | |
5f316715 LOK |
80 | m_iRetryLineTimeout(3), |
81 | m_iLastTransmission(0) | |
f8513317 | 82 | { |
1113cb7d | 83 | m_communication = new CAdapterCommunication(this); |
ab1469a0 | 84 | m_logicalAddresses.Clear(); |
f8513317 | 85 | for (int iPtr = 0; iPtr < 16; iPtr++) |
51b2a094 LOK |
86 | { |
87 | switch(iPtr) | |
88 | { | |
89 | case CECDEVICE_AUDIOSYSTEM: | |
5d5a2dc2 | 90 | m_busDevices[iPtr] = new CCECAudioSystem(this, (cec_logical_address) iPtr, 0xFFFF); |
51b2a094 LOK |
91 | break; |
92 | case CECDEVICE_PLAYBACKDEVICE1: | |
93 | case CECDEVICE_PLAYBACKDEVICE2: | |
94 | case CECDEVICE_PLAYBACKDEVICE3: | |
5d5a2dc2 | 95 | m_busDevices[iPtr] = new CCECPlaybackDevice(this, (cec_logical_address) iPtr, 0xFFFF); |
51b2a094 LOK |
96 | break; |
97 | case CECDEVICE_RECORDINGDEVICE1: | |
98 | case CECDEVICE_RECORDINGDEVICE2: | |
99 | case CECDEVICE_RECORDINGDEVICE3: | |
5d5a2dc2 | 100 | m_busDevices[iPtr] = new CCECRecordingDevice(this, (cec_logical_address) iPtr, 0xFFFF); |
51b2a094 LOK |
101 | break; |
102 | case CECDEVICE_TUNER1: | |
103 | case CECDEVICE_TUNER2: | |
104 | case CECDEVICE_TUNER3: | |
105 | case CECDEVICE_TUNER4: | |
5d5a2dc2 | 106 | m_busDevices[iPtr] = new CCECTuner(this, (cec_logical_address) iPtr, 0xFFFF); |
51b2a094 LOK |
107 | break; |
108 | case CECDEVICE_TV: | |
109 | m_busDevices[iPtr] = new CCECTV(this, (cec_logical_address) iPtr, 0); | |
110 | break; | |
111 | default: | |
5d5a2dc2 | 112 | m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, 0xFFFF); |
51b2a094 LOK |
113 | break; |
114 | } | |
115 | } | |
f8513317 LOK |
116 | } |
117 | ||
2abe74eb | 118 | CCECProcessor::~CCECProcessor(void) |
abbca718 | 119 | { |
5f316715 LOK |
120 | m_bStarted = false; |
121 | StopThread(false); | |
122 | ||
7c63a480 LOK |
123 | if (m_busScan) |
124 | { | |
125 | m_busScan->StopThread(); | |
126 | delete m_busScan; | |
127 | } | |
128 | ||
8b0ee2cc | 129 | m_startCondition.Broadcast(); |
2abe74eb | 130 | StopThread(); |
7c63a480 | 131 | |
1113cb7d | 132 | delete m_communication; |
2abe74eb LOK |
133 | m_communication = NULL; |
134 | m_controller = NULL; | |
d1998c7b LOK |
135 | for (unsigned int iPtr = 0; iPtr < 16; iPtr++) |
136 | delete m_busDevices[iPtr]; | |
abbca718 LOK |
137 | } |
138 | ||
1113cb7d | 139 | bool CCECProcessor::Start(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */) |
abbca718 | 140 | { |
8b0ee2cc | 141 | CLockObject lock(&m_mutex); |
1113cb7d | 142 | if (!m_communication || m_communication->IsOpen()) |
13fd6a66 | 143 | { |
1113cb7d LOK |
144 | m_controller->AddLog(CEC_LOG_ERROR, "connection already opened"); |
145 | return false; | |
146 | } | |
147 | ||
148 | if (!m_communication->Open(strPort, iBaudRate, iTimeoutMs)) | |
149 | { | |
150 | m_controller->AddLog(CEC_LOG_ERROR, "could not open a connection"); | |
a8f0bd18 | 151 | return false; |
13fd6a66 | 152 | } |
abbca718 | 153 | |
60fa4578 | 154 | if (CreateThread()) |
8b0ee2cc | 155 | { |
f8513317 | 156 | if (!m_startCondition.Wait(&m_mutex) || !m_bStarted) |
8b0ee2cc LOK |
157 | { |
158 | m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread"); | |
159 | return false; | |
160 | } | |
9fd73dd4 LOK |
161 | |
162 | lock.Leave(); | |
163 | if (SetAckMask(m_logicalAddresses.AckMask()) && | |
d2f1c157 | 164 | SetHDMIPort(m_iBaseDevice, m_iHDMIPort, true)) |
9fd73dd4 | 165 | { |
b750a5c3 | 166 | m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started"); |
7c63a480 LOK |
167 | m_busScan = new CCECBusScan(this); |
168 | m_busScan->CreateThread(true); | |
9fd73dd4 LOK |
169 | return true; |
170 | } | |
45b97de7 LOK |
171 | else |
172 | { | |
173 | m_controller->AddLog(CEC_LOG_ERROR, "failed to initialise the processor"); | |
174 | } | |
8b0ee2cc | 175 | } |
abbca718 | 176 | |
45b97de7 | 177 | m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread"); |
a8f0bd18 | 178 | return false; |
abbca718 LOK |
179 | } |
180 | ||
b58d9277 | 181 | bool CCECProcessor::TryLogicalAddress(cec_logical_address address) |
f8513317 | 182 | { |
93fff5c1 | 183 | if (m_busDevices[address]->TryLogicalAddress()) |
f8513317 | 184 | { |
8747dd4f | 185 | /* only set our OSD name and active source for the primary device */ |
ab1469a0 | 186 | if (m_logicalAddresses.IsEmpty()) |
8747dd4f | 187 | { |
787a3cb8 | 188 | m_busDevices[address]->m_strDeviceName = m_strDeviceName; |
8747dd4f LOK |
189 | m_busDevices[address]->m_bActiveSource = true; |
190 | } | |
ab1469a0 | 191 | m_logicalAddresses.Set(address); |
f8513317 LOK |
192 | return true; |
193 | } | |
194 | ||
f8513317 LOK |
195 | return false; |
196 | } | |
197 | ||
b58d9277 | 198 | bool CCECProcessor::FindLogicalAddressRecordingDevice(void) |
f8513317 LOK |
199 | { |
200 | AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'recording device'"); | |
b58d9277 LOK |
201 | return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1) || |
202 | TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2) || | |
203 | TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3); | |
f8513317 LOK |
204 | } |
205 | ||
b58d9277 | 206 | bool CCECProcessor::FindLogicalAddressTuner(void) |
f8513317 LOK |
207 | { |
208 | AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'tuner'"); | |
b58d9277 LOK |
209 | return TryLogicalAddress(CECDEVICE_TUNER1) || |
210 | TryLogicalAddress(CECDEVICE_TUNER2) || | |
211 | TryLogicalAddress(CECDEVICE_TUNER3) || | |
212 | TryLogicalAddress(CECDEVICE_TUNER4); | |
f8513317 LOK |
213 | } |
214 | ||
b58d9277 | 215 | bool CCECProcessor::FindLogicalAddressPlaybackDevice(void) |
f8513317 LOK |
216 | { |
217 | AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'playback device'"); | |
b58d9277 LOK |
218 | return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1) || |
219 | TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2) || | |
220 | TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3); | |
f8513317 LOK |
221 | } |
222 | ||
b58d9277 | 223 | bool CCECProcessor::FindLogicalAddressAudioSystem(void) |
f8513317 LOK |
224 | { |
225 | AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'audio'"); | |
b58d9277 | 226 | return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM); |
f8513317 LOK |
227 | } |
228 | ||
229 | bool CCECProcessor::FindLogicalAddresses(void) | |
230 | { | |
231 | bool bReturn(true); | |
ab1469a0 | 232 | m_logicalAddresses.Clear(); |
f8513317 LOK |
233 | CStdString strLog; |
234 | ||
235 | for (unsigned int iPtr = 0; iPtr < 5; iPtr++) | |
236 | { | |
237 | if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RESERVED) | |
238 | continue; | |
239 | ||
240 | strLog.Format("%s - device %d: type %d", __FUNCTION__, iPtr, m_types.types[iPtr]); | |
241 | AddLog(CEC_LOG_DEBUG, strLog); | |
242 | ||
243 | if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RECORDING_DEVICE) | |
b58d9277 | 244 | bReturn &= FindLogicalAddressRecordingDevice(); |
f8513317 | 245 | if (m_types.types[iPtr] == CEC_DEVICE_TYPE_TUNER) |
b58d9277 | 246 | bReturn &= FindLogicalAddressTuner(); |
f8513317 | 247 | if (m_types.types[iPtr] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE) |
b58d9277 | 248 | bReturn &= FindLogicalAddressPlaybackDevice(); |
f8513317 | 249 | if (m_types.types[iPtr] == CEC_DEVICE_TYPE_AUDIO_SYSTEM) |
b58d9277 | 250 | bReturn &= FindLogicalAddressAudioSystem(); |
f8513317 LOK |
251 | } |
252 | ||
253 | return bReturn; | |
254 | } | |
255 | ||
2abe74eb | 256 | void *CCECProcessor::Process(void) |
f99bc831 | 257 | { |
8747dd4f | 258 | bool bParseFrame(false); |
e0407d3d LOK |
259 | cec_command command; |
260 | CCECAdapterMessage msg; | |
261 | ||
9fd73dd4 | 262 | if (m_logicalAddresses.IsEmpty() && !FindLogicalAddresses()) |
8b0ee2cc | 263 | { |
9fd73dd4 LOK |
264 | CLockObject lock(&m_mutex); |
265 | m_controller->AddLog(CEC_LOG_ERROR, "could not detect our logical addresses"); | |
266 | m_startCondition.Signal(); | |
267 | return NULL; | |
268 | } | |
269 | else | |
270 | { | |
fe6f8e37 LOK |
271 | m_busDevices[CECDEVICE_TV]->GetVendorId(); |
272 | m_busDevices[m_logicalAddresses.primary]->TransmitVendorID(CECDEVICE_TV, false); | |
273 | ||
9fd73dd4 LOK |
274 | CLockObject lock(&m_mutex); |
275 | m_bStarted = true; | |
276 | m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started"); | |
277 | m_startCondition.Signal(); | |
8b0ee2cc | 278 | } |
abbca718 | 279 | |
13fd6a66 | 280 | while (!IsStopped()) |
abbca718 | 281 | { |
ab1469a0 | 282 | command.Clear(); |
76321de4 LOK |
283 | msg.clear(); |
284 | ||
60fa4578 LOK |
285 | { |
286 | CLockObject lock(&m_mutex); | |
8747dd4f LOK |
287 | if (m_commandBuffer.Pop(command)) |
288 | { | |
289 | bParseFrame = true; | |
290 | } | |
291 | else if (m_communication->IsOpen() && m_communication->Read(msg, 50)) | |
b5f34cf9 | 292 | { |
f2198ab5 | 293 | if ((bParseFrame = (ParseMessage(msg) && !IsStopped())) == true) |
8747dd4f | 294 | command = m_currentframe; |
b5f34cf9 | 295 | } |
60fa4578 LOK |
296 | } |
297 | ||
13fd6a66 | 298 | if (bParseFrame) |
9dee1670 | 299 | ParseCommand(command); |
8747dd4f | 300 | bParseFrame = false; |
abbca718 | 301 | |
b5f34cf9 LOK |
302 | Sleep(5); |
303 | ||
13fd6a66 | 304 | m_controller->CheckKeypressTimeout(); |
abbca718 LOK |
305 | } |
306 | ||
1113cb7d LOK |
307 | if (m_communication) |
308 | m_communication->Close(); | |
309 | ||
60fa4578 | 310 | return NULL; |
abbca718 LOK |
311 | } |
312 | ||
18203d17 | 313 | bool CCECProcessor::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RESERVED */) |
abbca718 | 314 | { |
8ac9c610 LOK |
315 | bool bReturn(false); |
316 | ||
60fa4578 | 317 | if (!IsRunning()) |
8ac9c610 | 318 | return bReturn; |
f99bc831 | 319 | |
18203d17 | 320 | cec_logical_address addr = m_logicalAddresses.primary; |
04437dcf | 321 | |
18203d17 LOK |
322 | if (type != CEC_DEVICE_TYPE_RESERVED) |
323 | { | |
90ea9066 | 324 | for (uint8_t iPtr = 0; iPtr <= 11; iPtr++) |
18203d17 LOK |
325 | { |
326 | if (m_logicalAddresses[iPtr] && m_busDevices[iPtr]->m_type == type) | |
327 | { | |
328 | addr = (cec_logical_address) iPtr; | |
329 | break; | |
330 | } | |
331 | } | |
332 | } | |
333 | ||
9a0d7b9f | 334 | bReturn = m_busDevices[addr]->TransmitActiveSource() && |
8d915412 LOK |
335 | SetStreamPath(m_busDevices[addr]->GetPhysicalAddress(false)); |
336 | ||
337 | if (bReturn && (m_busDevices[addr]->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || | |
338 | m_busDevices[addr]->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE)) | |
339 | { | |
340 | bReturn = ((CCECPlaybackDevice *)m_busDevices[addr])->TransmitDeckStatus(CECDEVICE_TV); | |
341 | } | |
342 | ||
343 | return bReturn; | |
18203d17 LOK |
344 | } |
345 | ||
dcd240b2 LOK |
346 | void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout) |
347 | { | |
348 | CLockObject lock(&m_mutex); | |
349 | m_iStandardLineTimeout = iTimeout; | |
350 | } | |
351 | ||
352 | void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout) | |
353 | { | |
354 | CLockObject lock(&m_mutex); | |
355 | m_iRetryLineTimeout = iTimeout; | |
356 | } | |
357 | ||
18203d17 LOK |
358 | bool CCECProcessor::SetActiveView(void) |
359 | { | |
4a870dc8 | 360 | return SetActiveSource(m_types.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED : m_types[0]); |
8ac9c610 LOK |
361 | } |
362 | ||
28fa6c97 | 363 | bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode, bool bSendUpdate /* = true */) |
a9232a79 LOK |
364 | { |
365 | bool bReturn(false); | |
366 | ||
367 | CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE); | |
368 | if (device) | |
369 | { | |
370 | ((CCECPlaybackDevice *) device)->SetDeckControlMode(mode); | |
28fa6c97 LOK |
371 | if (bSendUpdate) |
372 | ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV); | |
a9232a79 LOK |
373 | bReturn = true; |
374 | } | |
375 | ||
376 | return bReturn; | |
377 | } | |
378 | ||
28fa6c97 | 379 | bool CCECProcessor::SetDeckInfo(cec_deck_info info, bool bSendUpdate /* = true */) |
a9232a79 LOK |
380 | { |
381 | bool bReturn(false); | |
382 | ||
383 | CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE); | |
384 | if (device) | |
385 | { | |
386 | ((CCECPlaybackDevice *) device)->SetDeckStatus(info); | |
28fa6c97 LOK |
387 | if (bSendUpdate) |
388 | ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV); | |
a9232a79 LOK |
389 | bReturn = true; |
390 | } | |
391 | ||
392 | return bReturn; | |
393 | } | |
394 | ||
d2f1c157 | 395 | bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice, uint8_t iPort, bool bForce /* = false */) |
16b1e052 LOK |
396 | { |
397 | bool bReturn(false); | |
398 | ||
399 | CStdString strLog; | |
d2f1c157 | 400 | strLog.Format("setting HDMI port to %d on device %s (%d)", iPort, ToString(iBaseDevice), (int)iBaseDevice); |
16b1e052 LOK |
401 | AddLog(CEC_LOG_DEBUG, strLog); |
402 | ||
d2f1c157 | 403 | m_iBaseDevice = iBaseDevice; |
16b1e052 | 404 | m_iHDMIPort = iPort; |
9fd73dd4 | 405 | if (!m_bStarted && !bForce) |
16b1e052 LOK |
406 | return true; |
407 | ||
408 | uint16_t iPhysicalAddress(0); | |
d2f1c157 LOK |
409 | iPhysicalAddress = m_busDevices[iBaseDevice]->GetPhysicalAddress(); |
410 | uint16_t iPos = 0; | |
411 | if (iPhysicalAddress == 0) | |
412 | iPos = 0x1000; | |
413 | else if (iPhysicalAddress % 0x1000 == 0) | |
414 | iPos = 0x100; | |
415 | else if (iPhysicalAddress % 0x100 == 0) | |
416 | iPos = 0x10; | |
417 | else if (iPhysicalAddress % 0x10 == 0) | |
418 | iPos = 0x1; | |
419 | ||
420 | while(!bReturn && iPos > 0) | |
16b1e052 | 421 | { |
d2f1c157 | 422 | iPhysicalAddress += (uint16_t)(iPort * iPos); |
16b1e052 LOK |
423 | strLog.Format("checking physical address %4x", iPhysicalAddress); |
424 | AddLog(CEC_LOG_DEBUG, strLog); | |
45b97de7 | 425 | if (PhysicalAddressInUse(iPhysicalAddress)) |
16b1e052 LOK |
426 | { |
427 | strLog.Format("physical address %4x is in use", iPhysicalAddress); | |
428 | AddLog(CEC_LOG_DEBUG, strLog); | |
d2f1c157 | 429 | iPos = (iPos == 1) ? 0 : iPos / 0x10; |
16b1e052 LOK |
430 | } |
431 | else | |
432 | { | |
45b97de7 LOK |
433 | strLog.Format("physical address %4x is free", iPhysicalAddress); |
434 | AddLog(CEC_LOG_DEBUG, strLog); | |
16b1e052 LOK |
435 | SetPhysicalAddress(iPhysicalAddress); |
436 | bReturn = true; | |
437 | } | |
438 | } | |
439 | ||
440 | return bReturn; | |
441 | } | |
442 | ||
45b97de7 | 443 | bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress) |
16b1e052 | 444 | { |
9fd73dd4 | 445 | for (unsigned int iPtr = 0; iPtr < 15; iPtr++) |
16b1e052 LOK |
446 | { |
447 | if (m_busDevices[iPtr]->GetPhysicalAddress(false) == iPhysicalAddress) | |
448 | return true; | |
449 | } | |
450 | return false; | |
451 | } | |
452 | ||
8ac9c610 LOK |
453 | bool CCECProcessor::SetStreamPath(uint16_t iStreamPath) |
454 | { | |
04437dcf LOK |
455 | bool bReturn(false); |
456 | ||
8ac9c610 LOK |
457 | CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamPath); |
458 | if (device) | |
459 | { | |
7856411b | 460 | device->SetActiveDevice(); |
91bea174 | 461 | bReturn = true; |
8ac9c610 LOK |
462 | } |
463 | ||
04437dcf | 464 | return bReturn; |
abbca718 LOK |
465 | } |
466 | ||
ab27363d | 467 | bool CCECProcessor::TransmitInactiveSource(void) |
abbca718 | 468 | { |
60fa4578 | 469 | if (!IsRunning()) |
f99bc831 LOK |
470 | return false; |
471 | ||
ab1469a0 | 472 | if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary]) |
ab27363d | 473 | return m_busDevices[m_logicalAddresses.primary]->TransmitInactiveSource(); |
cc60ab1c | 474 | return false; |
abbca718 LOK |
475 | } |
476 | ||
9dee1670 | 477 | void CCECProcessor::LogOutput(const cec_command &data) |
abbca718 | 478 | { |
1d3ca3de | 479 | CStdString strTx; |
57f45e6c LOK |
480 | strTx.Format("<< %02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination); |
481 | if (data.opcode_set) | |
482 | strTx.AppendFormat(":%02x", (uint8_t)data.opcode); | |
9dee1670 | 483 | |
06a1f7ce | 484 | for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++) |
1d3ca3de LOK |
485 | strTx.AppendFormat(":%02x", data.parameters[iPtr]); |
486 | m_controller->AddLog(CEC_LOG_TRAFFIC, strTx.c_str()); | |
9dee1670 | 487 | } |
2abe74eb | 488 | |
06bfd4d7 | 489 | bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress) |
abbca718 | 490 | { |
f8513317 | 491 | if (m_logicalAddresses.primary != iLogicalAddress) |
06775107 LOK |
492 | { |
493 | CStdString strLog; | |
f8513317 | 494 | strLog.Format("<< setting primary logical address to %1x", iLogicalAddress); |
06775107 | 495 | m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str()); |
f8513317 | 496 | m_logicalAddresses.primary = iLogicalAddress; |
ab1469a0 LOK |
497 | m_logicalAddresses.Set(iLogicalAddress); |
498 | return SetAckMask(m_logicalAddresses.AckMask()); | |
06775107 | 499 | } |
2abe74eb | 500 | |
06bfd4d7 | 501 | return true; |
abbca718 | 502 | } |
825ddb96 | 503 | |
28fa6c97 LOK |
504 | bool CCECProcessor::SetMenuState(cec_menu_state state, bool bSendUpdate /* = true */) |
505 | { | |
506 | for (uint8_t iPtr = 0; iPtr < 16; iPtr++) | |
507 | { | |
508 | if (m_logicalAddresses[iPtr]) | |
509 | m_busDevices[iPtr]->SetMenuState(state); | |
510 | } | |
511 | ||
512 | if (bSendUpdate) | |
513 | m_busDevices[m_logicalAddresses.primary]->TransmitMenuState(CECDEVICE_TV); | |
514 | ||
515 | return true; | |
516 | } | |
517 | ||
2492216a LOK |
518 | bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress) |
519 | { | |
16b1e052 | 520 | if (!m_logicalAddresses.IsEmpty()) |
cc60ab1c | 521 | { |
16b1e052 LOK |
522 | for (uint8_t iPtr = 0; iPtr < 15; iPtr++) |
523 | if (m_logicalAddresses[iPtr]) | |
fe6f8e37 | 524 | { |
16b1e052 | 525 | m_busDevices[iPtr]->SetPhysicalAddress(iPhysicalAddress); |
fe6f8e37 LOK |
526 | m_busDevices[iPtr]->TransmitPhysicalAddress(); |
527 | } | |
8ac9c610 | 528 | return SetActiveView(); |
cc60ab1c LOK |
529 | } |
530 | return false; | |
1969b140 LOK |
531 | } |
532 | ||
8b7e5ff6 LOK |
533 | bool CCECProcessor::SwitchMonitoring(bool bEnable) |
534 | { | |
535 | CStdString strLog; | |
536 | strLog.Format("== %s monitoring mode ==", bEnable ? "enabling" : "disabling"); | |
537 | m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str()); | |
538 | ||
855a3a98 LOK |
539 | { |
540 | CLockObject lock(&m_mutex); | |
541 | m_bMonitor = bEnable; | |
542 | ||
fe6f8e37 | 543 | if (!bEnable) |
855a3a98 LOK |
544 | { |
545 | if (!m_busScan) | |
546 | { | |
547 | m_busScan = new CCECBusScan(this); | |
548 | m_busScan->CreateThread(true); | |
549 | } | |
550 | } | |
551 | else | |
552 | { | |
553 | if (m_busScan) | |
554 | { | |
555 | m_busScan->StopThread(); | |
556 | delete m_busScan; | |
557 | m_busScan = NULL; | |
558 | } | |
559 | } | |
560 | } | |
561 | ||
8b7e5ff6 | 562 | if (bEnable) |
06bfd4d7 | 563 | return SetAckMask(0); |
8b7e5ff6 | 564 | else |
ab1469a0 | 565 | return SetAckMask(m_logicalAddresses.AckMask()); |
8b7e5ff6 LOK |
566 | } |
567 | ||
57f45e6c LOK |
568 | bool CCECProcessor::PollDevice(cec_logical_address iAddress) |
569 | { | |
570 | if (iAddress != CECDEVICE_UNKNOWN && m_busDevices[iAddress]) | |
95a73fa7 LOK |
571 | { |
572 | return m_logicalAddresses.primary == CECDEVICE_UNKNOWN ? | |
573 | m_busDevices[iAddress]->TransmitPoll(iAddress) : | |
574 | m_busDevices[m_logicalAddresses.primary]->TransmitPoll(iAddress); | |
575 | } | |
57f45e6c LOK |
576 | return false; |
577 | } | |
578 | ||
8fa35473 | 579 | uint8_t CCECProcessor::VolumeUp(void) |
04e637f9 LOK |
580 | { |
581 | uint8_t status = 0; | |
582 | if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM)) | |
8fa35473 | 583 | status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeUp(); |
04e637f9 LOK |
584 | |
585 | return status; | |
586 | } | |
587 | ||
8fa35473 | 588 | uint8_t CCECProcessor::VolumeDown(void) |
04e637f9 LOK |
589 | { |
590 | uint8_t status = 0; | |
591 | if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM)) | |
8fa35473 | 592 | status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeDown(); |
04e637f9 LOK |
593 | |
594 | return status; | |
595 | } | |
596 | ||
8fa35473 | 597 | uint8_t CCECProcessor::MuteAudio(void) |
04e637f9 LOK |
598 | { |
599 | uint8_t status = 0; | |
600 | if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM)) | |
8fa35473 | 601 | status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->MuteAudio(); |
04e637f9 LOK |
602 | |
603 | return status; | |
604 | } | |
605 | ||
16b1e052 | 606 | CCECBusDevice *CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bRefresh /* = false */) const |
8ac9c610 | 607 | { |
16b1e052 | 608 | if (m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false) == iPhysicalAddress) |
b58d9277 | 609 | return m_busDevices[m_logicalAddresses.primary]; |
8ac9c610 | 610 | |
b58d9277 | 611 | CCECBusDevice *device = NULL; |
8ac9c610 LOK |
612 | for (unsigned int iPtr = 0; iPtr < 16; iPtr++) |
613 | { | |
16b1e052 | 614 | if (m_busDevices[iPtr]->GetPhysicalAddress(bRefresh) == iPhysicalAddress) |
8ac9c610 LOK |
615 | { |
616 | device = m_busDevices[iPtr]; | |
617 | break; | |
618 | } | |
619 | } | |
620 | ||
621 | return device; | |
622 | } | |
623 | ||
a9232a79 LOK |
624 | CCECBusDevice *CCECProcessor::GetDeviceByType(cec_device_type type) const |
625 | { | |
626 | CCECBusDevice *device = NULL; | |
627 | ||
628 | for (unsigned int iPtr = 0; iPtr < 16; iPtr++) | |
629 | { | |
630 | if (m_busDevices[iPtr]->m_type == type) | |
631 | { | |
632 | device = m_busDevices[iPtr]; | |
633 | break; | |
634 | } | |
635 | } | |
636 | ||
637 | return device; | |
638 | } | |
639 | ||
6a1c0009 LOK |
640 | cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress) |
641 | { | |
642 | return m_busDevices[iAddress]->GetCecVersion(); | |
643 | } | |
644 | ||
ed21be2a LOK |
645 | cec_osd_name CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress) |
646 | { | |
647 | CStdString strOSDName = m_busDevices[iAddress]->GetOSDName(); | |
648 | cec_osd_name retVal; | |
649 | ||
650 | snprintf(retVal.name, sizeof(retVal.name), "%s", strOSDName.c_str()); | |
651 | retVal.device = iAddress; | |
652 | ||
653 | return retVal; | |
654 | } | |
655 | ||
a3269a0a LOK |
656 | bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language) |
657 | { | |
cc60ab1c LOK |
658 | if (m_busDevices[iAddress]) |
659 | { | |
660 | *language = m_busDevices[iAddress]->GetMenuLanguage(); | |
5744fbba | 661 | return (strcmp(language->language, "???") != 0); |
cc60ab1c LOK |
662 | } |
663 | return false; | |
a3269a0a LOK |
664 | } |
665 | ||
44c74256 LOK |
666 | uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress) |
667 | { | |
cc60ab1c LOK |
668 | if (m_busDevices[iAddress]) |
669 | return m_busDevices[iAddress]->GetVendorId(); | |
670 | return false; | |
44c74256 LOK |
671 | } |
672 | ||
e55f3f70 LOK |
673 | cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress) |
674 | { | |
cc60ab1c LOK |
675 | if (m_busDevices[iAddress]) |
676 | return m_busDevices[iAddress]->GetPowerStatus(); | |
677 | return CEC_POWER_STATUS_UNKNOWN; | |
e55f3f70 LOK |
678 | } |
679 | ||
8d84e2c0 | 680 | bool CCECProcessor::Transmit(const cec_command &data) |
825ddb96 | 681 | { |
28352a04 | 682 | bool bReturn(false); |
13929cff LOK |
683 | LogOutput(data); |
684 | ||
28352a04 LOK |
685 | CCECAdapterMessage *output = new CCECAdapterMessage(data); |
686 | bReturn = Transmit(output); | |
28352a04 | 687 | |
90ea9066 LOK |
688 | /* set to "not present" on failed ack */ |
689 | if (output->is_error() && output->reply == MSGCODE_TRANSMIT_FAILED_ACK && | |
690 | output->destination() != CECDEVICE_BROADCAST) | |
691 | m_busDevices[output->destination()]->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT); | |
692 | ||
693 | delete output; | |
28352a04 | 694 | return bReturn; |
06bfd4d7 | 695 | } |
13929cff | 696 | |
28352a04 | 697 | bool CCECProcessor::Transmit(CCECAdapterMessage *output) |
06bfd4d7 LOK |
698 | { |
699 | bool bReturn(false); | |
2abe74eb | 700 | CLockObject lock(&m_mutex); |
eb35e4ca | 701 | { |
5f316715 | 702 | m_iLastTransmission = GetTimeMs(); |
dcd240b2 | 703 | m_communication->SetLineTimeout(m_iStandardLineTimeout); |
ae693aaa | 704 | output->tries = 1; |
9902f4e8 | 705 | |
fd5e333a | 706 | do |
0e31a62c | 707 | { |
9902f4e8 | 708 | if (output->tries > 0) |
dcd240b2 | 709 | m_communication->SetLineTimeout(m_iRetryLineTimeout); |
9902f4e8 | 710 | |
82d1a9a1 LOK |
711 | CLockObject msgLock(&output->mutex); |
712 | if (!m_communication || !m_communication->Write(output)) | |
0e31a62c | 713 | return bReturn; |
82d1a9a1 LOK |
714 | else |
715 | { | |
716 | output->condition.Wait(&output->mutex); | |
717 | if (output->state != ADAPTER_MESSAGE_STATE_SENT) | |
718 | { | |
719 | m_controller->AddLog(CEC_LOG_ERROR, "command was not sent"); | |
720 | return bReturn; | |
721 | } | |
0e31a62c | 722 | } |
2abe74eb | 723 | |
82d1a9a1 LOK |
724 | if (output->transmit_timeout > 0) |
725 | { | |
726 | if ((bReturn = WaitForTransmitSucceeded(output)) == false) | |
727 | m_controller->AddLog(CEC_LOG_DEBUG, "did not receive ack"); | |
728 | } | |
729 | else | |
730 | bReturn = true; | |
ae693aaa | 731 | }while (output->transmit_timeout > 0 && output->needs_retry() && ++output->tries < output->maxTries); |
2abe74eb LOK |
732 | } |
733 | ||
dcd240b2 | 734 | m_communication->SetLineTimeout(m_iStandardLineTimeout); |
9902f4e8 | 735 | |
7ea0d558 | 736 | return bReturn; |
825ddb96 | 737 | } |
abbca718 | 738 | |
22b4e74a | 739 | void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, cec_abort_reason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */) |
abbca718 | 740 | { |
9dee1670 LOK |
741 | m_controller->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message"); |
742 | ||
06a1f7ce | 743 | cec_command command; |
f8513317 | 744 | // TODO |
ab1469a0 LOK |
745 | cec_command::Format(command, m_logicalAddresses.primary, address, CEC_OPCODE_FEATURE_ABORT); |
746 | command.parameters.PushBack((uint8_t)opcode); | |
747 | command.parameters.PushBack((uint8_t)reason); | |
9dee1670 LOK |
748 | |
749 | Transmit(command); | |
abbca718 LOK |
750 | } |
751 | ||
82d1a9a1 | 752 | bool CCECProcessor::WaitForTransmitSucceeded(CCECAdapterMessage *message) |
abbca718 | 753 | { |
1f0e9e0f LOK |
754 | bool bError(false); |
755 | bool bTransmitSucceeded(false); | |
82d1a9a1 | 756 | uint8_t iPacketsLeft(message->size() / 4); |
abbca718 LOK |
757 | |
758 | int64_t iNow = GetTimeMs(); | |
82d1a9a1 | 759 | int64_t iTargetTime = iNow + message->transmit_timeout; |
abbca718 | 760 | |
82d1a9a1 | 761 | while (!bTransmitSucceeded && !bError && (message->transmit_timeout == 0 || iNow < iTargetTime)) |
abbca718 | 762 | { |
220537f2 | 763 | CCECAdapterMessage msg; |
25701fa6 | 764 | |
82d1a9a1 | 765 | if (!m_communication->Read(msg, message->transmit_timeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000)) |
abbca718 | 766 | { |
abbca718 | 767 | iNow = GetTimeMs(); |
5d38b0b6 LOK |
768 | continue; |
769 | } | |
770 | ||
855a3a98 LOK |
771 | if (msg.message() == MSGCODE_FRAME_START && msg.ack()) |
772 | { | |
773 | m_busDevices[msg.initiator()]->GetHandler()->HandlePoll(msg.initiator(), msg.destination()); | |
774 | m_lastInitiator = msg.initiator(); | |
775 | iNow = GetTimeMs(); | |
776 | continue; | |
777 | } | |
778 | ||
779 | bError = msg.is_error(); | |
780 | if (msg.message() == MSGCODE_RECEIVE_FAILED && | |
781 | m_lastInitiator != CECDEVICE_UNKNOWN && | |
782 | !m_busDevices[m_lastInitiator]->GetHandler()->HandleReceiveFailed()) | |
783 | { | |
784 | iNow = GetTimeMs(); | |
785 | continue; | |
786 | } | |
787 | ||
82d1a9a1 | 788 | if (bError) |
fd5e333a | 789 | { |
82d1a9a1 | 790 | message->reply = msg.message(); |
fd5e333a LOK |
791 | m_controller->AddLog(CEC_LOG_DEBUG, msg.ToString()); |
792 | } | |
82d1a9a1 | 793 | else |
7ea0d558 | 794 | { |
edab6137 LOK |
795 | switch(msg.message()) |
796 | { | |
797 | case MSGCODE_COMMAND_ACCEPTED: | |
fd5e333a | 798 | m_controller->AddLog(CEC_LOG_DEBUG, msg.ToString()); |
edab6137 LOK |
799 | if (iPacketsLeft > 0) |
800 | iPacketsLeft--; | |
801 | break; | |
802 | case MSGCODE_TRANSMIT_SUCCEEDED: | |
fd5e333a | 803 | m_controller->AddLog(CEC_LOG_DEBUG, msg.ToString()); |
edab6137 LOK |
804 | bTransmitSucceeded = (iPacketsLeft == 0); |
805 | bError = !bTransmitSucceeded; | |
82d1a9a1 | 806 | message->reply = MSGCODE_TRANSMIT_SUCCEEDED; |
edab6137 LOK |
807 | break; |
808 | default: | |
26128836 LOK |
809 | // ignore other data while waiting |
810 | break; | |
edab6137 | 811 | } |
7ea0d558 | 812 | |
edab6137 LOK |
813 | iNow = GetTimeMs(); |
814 | } | |
abbca718 LOK |
815 | } |
816 | ||
1f0e9e0f | 817 | return bTransmitSucceeded && !bError; |
abbca718 LOK |
818 | } |
819 | ||
0e7b6c54 | 820 | bool CCECProcessor::ParseMessage(const CCECAdapterMessage &msg) |
abbca718 | 821 | { |
855a3a98 LOK |
822 | bool bEom(false); |
823 | bool bIsError(msg.is_error()); | |
60fa4578 | 824 | |
0e7b6c54 | 825 | if (msg.empty()) |
7ea0d558 | 826 | return bEom; |
abbca718 | 827 | |
0e7b6c54 | 828 | switch(msg.message()) |
abbca718 | 829 | { |
abbca718 LOK |
830 | case MSGCODE_FRAME_START: |
831 | { | |
ab1469a0 | 832 | m_currentframe.Clear(); |
0e7b6c54 | 833 | if (msg.size() >= 2) |
abbca718 | 834 | { |
0e7b6c54 LOK |
835 | m_currentframe.initiator = msg.initiator(); |
836 | m_currentframe.destination = msg.destination(); | |
837 | m_currentframe.ack = msg.ack(); | |
838 | m_currentframe.eom = msg.eom(); | |
abbca718 | 839 | } |
8b86fb7a | 840 | if (m_currentframe.ack == 0x1) |
855a3a98 LOK |
841 | { |
842 | m_lastInitiator = m_currentframe.initiator; | |
843 | m_busDevices[m_lastInitiator]->GetHandler()->HandlePoll(m_currentframe.initiator, m_currentframe.destination); | |
844 | } | |
845 | } | |
846 | break; | |
847 | case MSGCODE_RECEIVE_FAILED: | |
848 | { | |
849 | if (m_lastInitiator != CECDEVICE_UNKNOWN) | |
850 | bIsError = m_busDevices[m_lastInitiator]->GetHandler()->HandleReceiveFailed(); | |
abbca718 LOK |
851 | } |
852 | break; | |
853 | case MSGCODE_FRAME_DATA: | |
854 | { | |
0e7b6c54 | 855 | if (msg.size() >= 2) |
78e8010a | 856 | { |
ab1469a0 | 857 | m_currentframe.PushBack(msg[1]); |
0e7b6c54 | 858 | m_currentframe.eom = msg.eom(); |
abbca718 | 859 | } |
0e7b6c54 | 860 | bEom = msg.eom(); |
abbca718 | 861 | } |
78e8010a | 862 | break; |
abbca718 LOK |
863 | default: |
864 | break; | |
865 | } | |
7ea0d558 | 866 | |
855a3a98 | 867 | m_controller->AddLog(bIsError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString()); |
7ea0d558 | 868 | return bEom; |
abbca718 LOK |
869 | } |
870 | ||
e9de9629 | 871 | void CCECProcessor::ParseCommand(cec_command &command) |
abbca718 | 872 | { |
e9de9629 | 873 | CStdString dataStr; |
1d3ca3de | 874 | dataStr.Format(">> %1x%1x:%02x", command.initiator, command.destination, command.opcode); |
e9de9629 LOK |
875 | for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++) |
876 | dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]); | |
1d3ca3de | 877 | m_controller->AddLog(CEC_LOG_TRAFFIC, dataStr.c_str()); |
dc113aca | 878 | |
7871d66e | 879 | if (!m_bMonitor && command.initiator >= CECDEVICE_TV && command.initiator <= CECDEVICE_BROADCAST) |
e9de9629 | 880 | m_busDevices[(uint8_t)command.initiator]->HandleCommand(command); |
dc113aca LOK |
881 | } |
882 | ||
6d858ba4 LOK |
883 | cec_logical_addresses CCECProcessor::GetActiveDevices(void) |
884 | { | |
885 | cec_logical_addresses addresses; | |
988de7b9 | 886 | addresses.Clear(); |
6d858ba4 LOK |
887 | for (unsigned int iPtr = 0; iPtr < 15; iPtr++) |
888 | { | |
889 | if (m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) | |
890 | addresses.Set((cec_logical_address) iPtr); | |
891 | } | |
892 | return addresses; | |
893 | } | |
894 | ||
895 | bool CCECProcessor::IsActiveDevice(cec_logical_address address) | |
896 | { | |
897 | return m_busDevices[address]->GetStatus() == CEC_DEVICE_STATUS_PRESENT; | |
898 | } | |
899 | ||
900 | bool CCECProcessor::IsActiveDeviceType(cec_device_type type) | |
901 | { | |
6d858ba4 | 902 | for (unsigned int iPtr = 0; iPtr < 15; iPtr++) |
2813bd07 LOK |
903 | { |
904 | if (m_busDevices[iPtr]->GetType() == type && m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) | |
6d858ba4 | 905 | return true; |
2813bd07 LOK |
906 | } |
907 | ||
6d858ba4 LOK |
908 | return false; |
909 | } | |
910 | ||
0f23c85c LOK |
911 | uint16_t CCECProcessor::GetPhysicalAddress(void) const |
912 | { | |
ab1469a0 | 913 | if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary]) |
16b1e052 | 914 | return m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false); |
cc60ab1c | 915 | return false; |
0f23c85c LOK |
916 | } |
917 | ||
e9de9629 | 918 | void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode) |
dc113aca | 919 | { |
e9de9629 | 920 | m_controller->SetCurrentButton(iButtonCode); |
dc113aca LOK |
921 | } |
922 | ||
e9de9629 | 923 | void CCECProcessor::AddCommand(const cec_command &command) |
dc113aca | 924 | { |
0544ff56 | 925 | m_controller->AddCommand(command); |
dc113aca LOK |
926 | } |
927 | ||
95ba7a09 LOK |
928 | void CCECProcessor::AddKey(cec_keypress &key) |
929 | { | |
930 | m_controller->AddKey(key); | |
931 | } | |
932 | ||
e9de9629 | 933 | void CCECProcessor::AddKey(void) |
dc113aca | 934 | { |
e9de9629 | 935 | m_controller->AddKey(); |
abbca718 | 936 | } |
acec5f48 | 937 | |
e9de9629 | 938 | void CCECProcessor::AddLog(cec_log_level level, const CStdString &strMessage) |
acec5f48 | 939 | { |
e9de9629 | 940 | m_controller->AddLog(level, strMessage); |
acec5f48 | 941 | } |
06bfd4d7 LOK |
942 | |
943 | bool CCECProcessor::SetAckMask(uint16_t iMask) | |
944 | { | |
28352a04 | 945 | bool bReturn(false); |
06bfd4d7 LOK |
946 | CStdString strLog; |
947 | strLog.Format("setting ackmask to %2x", iMask); | |
948 | m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str()); | |
949 | ||
28352a04 | 950 | CCECAdapterMessage *output = new CCECAdapterMessage; |
06bfd4d7 LOK |
951 | |
952 | output->push_back(MSGSTART); | |
953 | output->push_escaped(MSGCODE_SET_ACK_MASK); | |
954 | output->push_escaped(iMask >> 8); | |
955 | output->push_escaped((uint8_t)iMask); | |
956 | output->push_back(MSGEND); | |
957 | ||
28352a04 | 958 | if ((bReturn = Transmit(output)) == false) |
06bfd4d7 | 959 | m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask"); |
06bfd4d7 | 960 | |
28352a04 LOK |
961 | delete output; |
962 | ||
963 | return bReturn; | |
06bfd4d7 | 964 | } |
a33794d8 | 965 | |
7f919115 | 966 | bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination, cec_user_control_code key) |
a33794d8 | 967 | { |
7f919115 | 968 | return m_busDevices[iDestination]->TransmitKeypress(key); |
a33794d8 LOK |
969 | } |
970 | ||
7f919115 | 971 | bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination) |
a33794d8 | 972 | { |
7f919115 | 973 | return m_busDevices[iDestination]->TransmitKeyRelease(); |
a33794d8 | 974 | } |
7c63a480 | 975 | |
03ae897d LOK |
976 | const char *CCECProcessor::ToString(const cec_menu_state state) |
977 | { | |
978 | switch (state) | |
979 | { | |
980 | case CEC_MENU_STATE_ACTIVATED: | |
981 | return "activated"; | |
982 | case CEC_MENU_STATE_DEACTIVATED: | |
983 | return "deactivated"; | |
984 | default: | |
985 | return "unknown"; | |
986 | } | |
987 | } | |
988 | ||
989 | const char *CCECProcessor::ToString(const cec_version version) | |
990 | { | |
991 | switch (version) | |
992 | { | |
993 | case CEC_VERSION_1_2: | |
994 | return "1.2"; | |
995 | case CEC_VERSION_1_2A: | |
996 | return "1.2a"; | |
997 | case CEC_VERSION_1_3: | |
998 | return "1.3"; | |
999 | case CEC_VERSION_1_3A: | |
1000 | return "1.3a"; | |
1001 | case CEC_VERSION_1_4: | |
1002 | return "1.4"; | |
1003 | default: | |
1004 | return "unknown"; | |
1005 | } | |
1006 | } | |
1007 | ||
1008 | const char *CCECProcessor::ToString(const cec_power_status status) | |
1009 | { | |
1010 | switch (status) | |
1011 | { | |
1012 | case CEC_POWER_STATUS_ON: | |
1013 | return "on"; | |
1014 | case CEC_POWER_STATUS_STANDBY: | |
1015 | return "standby"; | |
1016 | case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY: | |
1017 | return "in transition from on to standby"; | |
1018 | case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON: | |
1019 | return "in transition from standby to on"; | |
1020 | default: | |
1021 | return "unknown"; | |
1022 | } | |
1023 | } | |
1024 | ||
1025 | const char *CCECProcessor::ToString(const cec_logical_address address) | |
1026 | { | |
1027 | switch(address) | |
1028 | { | |
1029 | case CECDEVICE_AUDIOSYSTEM: | |
1030 | return "Audio"; | |
1031 | case CECDEVICE_BROADCAST: | |
1032 | return "Broadcast"; | |
1033 | case CECDEVICE_FREEUSE: | |
1034 | return "Free use"; | |
1035 | case CECDEVICE_PLAYBACKDEVICE1: | |
1036 | return "Playback 1"; | |
1037 | case CECDEVICE_PLAYBACKDEVICE2: | |
1038 | return "Playback 2"; | |
1039 | case CECDEVICE_PLAYBACKDEVICE3: | |
1040 | return "Playback 3"; | |
1041 | case CECDEVICE_RECORDINGDEVICE1: | |
1042 | return "Recorder 1"; | |
1043 | case CECDEVICE_RECORDINGDEVICE2: | |
1044 | return "Recorder 2"; | |
1045 | case CECDEVICE_RECORDINGDEVICE3: | |
1046 | return "Recorder 3"; | |
1047 | case CECDEVICE_RESERVED1: | |
1048 | return "Reserved 1"; | |
1049 | case CECDEVICE_RESERVED2: | |
1050 | return "Reserved 2"; | |
1051 | case CECDEVICE_TUNER1: | |
1052 | return "Tuner 1"; | |
1053 | case CECDEVICE_TUNER2: | |
1054 | return "Tuner 2"; | |
1055 | case CECDEVICE_TUNER3: | |
1056 | return "Tuner 3"; | |
1057 | case CECDEVICE_TUNER4: | |
1058 | return "Tuner 4"; | |
1059 | case CECDEVICE_TV: | |
1060 | return "TV"; | |
1061 | default: | |
1062 | return "unknown"; | |
1063 | } | |
1064 | } | |
1065 | ||
1066 | const char *CCECProcessor::ToString(const cec_deck_control_mode mode) | |
1067 | { | |
1068 | switch (mode) | |
1069 | { | |
1070 | case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND: | |
1071 | return "skip forward wind"; | |
1072 | case CEC_DECK_CONTROL_MODE_EJECT: | |
1073 | return "eject"; | |
1074 | case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND: | |
1075 | return "reverse rewind"; | |
1076 | case CEC_DECK_CONTROL_MODE_STOP: | |
1077 | return "stop"; | |
1078 | default: | |
1079 | return "unknown"; | |
1080 | } | |
1081 | } | |
1082 | ||
1083 | const char *CCECProcessor::ToString(const cec_deck_info status) | |
1084 | { | |
1085 | switch (status) | |
1086 | { | |
1087 | case CEC_DECK_INFO_PLAY: | |
1088 | return "play"; | |
1089 | case CEC_DECK_INFO_RECORD: | |
1090 | return "record"; | |
1091 | case CEC_DECK_INFO_PLAY_REVERSE: | |
1092 | return "play reverse"; | |
1093 | case CEC_DECK_INFO_STILL: | |
1094 | return "still"; | |
1095 | case CEC_DECK_INFO_SLOW: | |
1096 | return "slow"; | |
1097 | case CEC_DECK_INFO_SLOW_REVERSE: | |
1098 | return "slow reverse"; | |
1099 | case CEC_DECK_INFO_FAST_FORWARD: | |
1100 | return "fast forward"; | |
1101 | case CEC_DECK_INFO_FAST_REVERSE: | |
1102 | return "fast reverse"; | |
1103 | case CEC_DECK_INFO_NO_MEDIA: | |
1104 | return "no media"; | |
1105 | case CEC_DECK_INFO_STOP: | |
1106 | return "stop"; | |
1107 | case CEC_DECK_INFO_SKIP_FORWARD_WIND: | |
1108 | return "info skip forward wind"; | |
1109 | case CEC_DECK_INFO_SKIP_REVERSE_REWIND: | |
1110 | return "info skip reverse rewind"; | |
1111 | case CEC_DECK_INFO_INDEX_SEARCH_FORWARD: | |
1112 | return "info index search forward"; | |
1113 | case CEC_DECK_INFO_INDEX_SEARCH_REVERSE: | |
1114 | return "info index search reverse"; | |
1115 | case CEC_DECK_INFO_OTHER_STATUS: | |
1116 | return "other"; | |
1117 | default: | |
1118 | return "unknown"; | |
1119 | } | |
1120 | } | |
1121 | ||
1122 | const char *CCECProcessor::ToString(const cec_opcode opcode) | |
1123 | { | |
1124 | switch (opcode) | |
1125 | { | |
1126 | case CEC_OPCODE_ACTIVE_SOURCE: | |
1127 | return "active source"; | |
1128 | case CEC_OPCODE_IMAGE_VIEW_ON: | |
1129 | return "image view on"; | |
1130 | case CEC_OPCODE_TEXT_VIEW_ON: | |
1131 | return "text view on"; | |
1132 | case CEC_OPCODE_INACTIVE_SOURCE: | |
1133 | return "inactive source"; | |
1134 | case CEC_OPCODE_REQUEST_ACTIVE_SOURCE: | |
1135 | return "request active source"; | |
1136 | case CEC_OPCODE_ROUTING_CHANGE: | |
1137 | return "routing change"; | |
1138 | case CEC_OPCODE_ROUTING_INFORMATION: | |
1139 | return "routing information"; | |
1140 | case CEC_OPCODE_SET_STREAM_PATH: | |
1141 | return "set stream path"; | |
1142 | case CEC_OPCODE_STANDBY: | |
1143 | return "standby"; | |
1144 | case CEC_OPCODE_RECORD_OFF: | |
1145 | return "record off"; | |
1146 | case CEC_OPCODE_RECORD_ON: | |
1147 | return "record on"; | |
1148 | case CEC_OPCODE_RECORD_STATUS: | |
1149 | return "record status"; | |
1150 | case CEC_OPCODE_RECORD_TV_SCREEN: | |
1151 | return "record tv screen"; | |
1152 | case CEC_OPCODE_CLEAR_ANALOGUE_TIMER: | |
1153 | return "clear analogue timer"; | |
1154 | case CEC_OPCODE_CLEAR_DIGITAL_TIMER: | |
1155 | return "clear digital timer"; | |
1156 | case CEC_OPCODE_CLEAR_EXTERNAL_TIMER: | |
1157 | return "clear external timer"; | |
1158 | case CEC_OPCODE_SET_ANALOGUE_TIMER: | |
1159 | return "set analogue timer"; | |
1160 | case CEC_OPCODE_SET_DIGITAL_TIMER: | |
1161 | return "set digital timer"; | |
1162 | case CEC_OPCODE_SET_EXTERNAL_TIMER: | |
1163 | return "set external timer"; | |
1164 | case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE: | |
1165 | return "set timer program title"; | |
1166 | case CEC_OPCODE_TIMER_CLEARED_STATUS: | |
1167 | return "timer cleared status"; | |
1168 | case CEC_OPCODE_TIMER_STATUS: | |
1169 | return "timer status"; | |
1170 | case CEC_OPCODE_CEC_VERSION: | |
1171 | return "cec version"; | |
1172 | case CEC_OPCODE_GET_CEC_VERSION: | |
1173 | return "get cec version"; | |
1174 | case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS: | |
1175 | return "give physical address"; | |
1176 | case CEC_OPCODE_GET_MENU_LANGUAGE: | |
1177 | return "get menu language"; | |
1178 | case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS: | |
1179 | return "report physical address"; | |
1180 | case CEC_OPCODE_SET_MENU_LANGUAGE: | |
1181 | return "set menu language"; | |
1182 | case CEC_OPCODE_DECK_CONTROL: | |
1183 | return "deck control"; | |
1184 | case CEC_OPCODE_DECK_STATUS: | |
1185 | return "deck status"; | |
1186 | case CEC_OPCODE_GIVE_DECK_STATUS: | |
1187 | return "give deck status"; | |
1188 | case CEC_OPCODE_PLAY: | |
1189 | return "play"; | |
1190 | case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS: | |
1191 | return "give tuner status"; | |
1192 | case CEC_OPCODE_SELECT_ANALOGUE_SERVICE: | |
1193 | return "select analogue service"; | |
1194 | case CEC_OPCODE_SELECT_DIGITAL_SERVICE: | |
1195 | return "set digital service"; | |
1196 | case CEC_OPCODE_TUNER_DEVICE_STATUS: | |
1197 | return "tuner device status"; | |
1198 | case CEC_OPCODE_TUNER_STEP_DECREMENT: | |
1199 | return "tuner step decrement"; | |
1200 | case CEC_OPCODE_TUNER_STEP_INCREMENT: | |
1201 | return "tuner step increment"; | |
1202 | case CEC_OPCODE_DEVICE_VENDOR_ID: | |
1203 | return "device vendor id"; | |
1204 | case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID: | |
1205 | return "give device vendor id"; | |
1206 | case CEC_OPCODE_VENDOR_COMMAND: | |
1207 | return "vendor command"; | |
1208 | case CEC_OPCODE_VENDOR_COMMAND_WITH_ID: | |
1209 | return "vendor command with id"; | |
1210 | case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN: | |
1211 | return "vendor remote button down"; | |
1212 | case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP: | |
1213 | return "vendor remote button up"; | |
1214 | case CEC_OPCODE_SET_OSD_STRING: | |
1215 | return "set osd string"; | |
1216 | case CEC_OPCODE_GIVE_OSD_NAME: | |
1217 | return "give osd name"; | |
1218 | case CEC_OPCODE_SET_OSD_NAME: | |
1219 | return "set osd name"; | |
1220 | case CEC_OPCODE_MENU_REQUEST: | |
1221 | return "menu request"; | |
1222 | case CEC_OPCODE_MENU_STATUS: | |
1223 | return "menu status"; | |
1224 | case CEC_OPCODE_USER_CONTROL_PRESSED: | |
1225 | return "user control pressed"; | |
1226 | case CEC_OPCODE_USER_CONTROL_RELEASE: | |
1227 | return "user control release"; | |
1228 | case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS: | |
1229 | return "give device power status"; | |
1230 | case CEC_OPCODE_REPORT_POWER_STATUS: | |
1231 | return "report power status"; | |
1232 | case CEC_OPCODE_FEATURE_ABORT: | |
1233 | return "feature abort"; | |
1234 | case CEC_OPCODE_ABORT: | |
1235 | return "abort"; | |
1236 | case CEC_OPCODE_GIVE_AUDIO_STATUS: | |
1237 | return "give audio status"; | |
1238 | case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS: | |
1239 | return "give audio mode status"; | |
1240 | case CEC_OPCODE_REPORT_AUDIO_STATUS: | |
1241 | return "report audio status"; | |
1242 | case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE: | |
1243 | return "set system audio mode"; | |
1244 | case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST: | |
1245 | return "system audio mode request"; | |
1246 | case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS: | |
1247 | return "system audio mode status"; | |
1248 | case CEC_OPCODE_SET_AUDIO_RATE: | |
1249 | return "set audio rate"; | |
1250 | default: | |
1251 | return "UNKNOWN"; | |
1252 | } | |
1253 | } | |
1254 | ||
1255 | const char *CCECProcessor::ToString(const cec_system_audio_status mode) | |
1256 | { | |
1257 | switch(mode) | |
1258 | { | |
1259 | case CEC_SYSTEM_AUDIO_STATUS_ON: | |
1260 | return "on"; | |
1261 | case CEC_SYSTEM_AUDIO_STATUS_OFF: | |
1262 | return "off"; | |
1263 | default: | |
1264 | return "unknown"; | |
1265 | } | |
1266 | } | |
1267 | ||
1268 | const char *CCECProcessor::ToString(const cec_audio_status status) | |
1269 | { | |
1270 | // TODO this is a mask | |
1271 | return "TODO"; | |
1272 | } | |
1273 | ||
1274 | const char *CCECProcessor::ToString(const cec_vendor_id vendor) | |
1275 | { | |
1276 | switch (vendor) | |
1277 | { | |
1278 | case CEC_VENDOR_SAMSUNG: | |
1279 | return "Samsung"; | |
1280 | case CEC_VENDOR_LG: | |
1281 | return "LG"; | |
1282 | case CEC_VENDOR_PANASONIC: | |
1283 | return "Panasonic"; | |
1284 | case CEC_VENDOR_PIONEER: | |
1285 | return "Pioneer"; | |
1286 | case CEC_VENDOR_ONKYO: | |
1287 | return "Onkyo"; | |
1288 | case CEC_VENDOR_YAMAHA: | |
1289 | return "Yamaha"; | |
ec0deac1 LOK |
1290 | case CEC_VENDOR_PHILIPS: |
1291 | return "Philips"; | |
03ae897d LOK |
1292 | default: |
1293 | return "Unknown"; | |
1294 | } | |
1295 | } | |
1296 | ||
7c63a480 LOK |
1297 | void *CCECBusScan::Process(void) |
1298 | { | |
1299 | CCECBusDevice *device(NULL); | |
fe6f8e37 | 1300 | uint8_t iCounter(0); |
5f316715 | 1301 | |
5e5637c6 | 1302 | while (!IsStopped()) |
7c63a480 | 1303 | { |
fe6f8e37 LOK |
1304 | if (++iCounter < 30) |
1305 | { | |
1306 | Sleep(1000); | |
1307 | continue; | |
1308 | } | |
5f316715 | 1309 | for (unsigned int iPtr = 0; iPtr <= 11 && !IsStopped(); iPtr++) |
7c63a480 | 1310 | { |
5f316715 LOK |
1311 | device = m_processor->m_busDevices[iPtr]; |
1312 | WaitUntilIdle(); | |
1313 | if (device && device->GetStatus(true) == CEC_DEVICE_STATUS_PRESENT) | |
5e5637c6 | 1314 | { |
5f316715 LOK |
1315 | WaitUntilIdle(); |
1316 | if (!IsStopped()) | |
1317 | device->GetVendorId(); | |
1318 | ||
1319 | WaitUntilIdle(); | |
1320 | if (!IsStopped()) | |
1321 | device->GetPowerStatus(true); | |
5e5637c6 | 1322 | } |
7c63a480 LOK |
1323 | } |
1324 | } | |
5f316715 | 1325 | |
7c63a480 LOK |
1326 | return NULL; |
1327 | } | |
1113cb7d | 1328 | |
5f316715 LOK |
1329 | void CCECBusScan::WaitUntilIdle(void) |
1330 | { | |
1331 | if (IsStopped()) | |
1332 | return; | |
1333 | ||
1334 | int64_t iWaitTime = 3000 - (GetTimeMs() - m_processor->GetLastTransmission()); | |
1335 | while (iWaitTime > 0) | |
1336 | { | |
1337 | Sleep(iWaitTime); | |
1338 | iWaitTime = 3000 - (GetTimeMs() - m_processor->GetLastTransmission()); | |
1339 | } | |
1340 | } | |
1341 | ||
1113cb7d LOK |
1342 | bool CCECProcessor::StartBootloader(void) |
1343 | { | |
1344 | return m_communication->StartBootloader(); | |
1345 | } | |
1346 | ||
1347 | bool CCECProcessor::PingAdapter(void) | |
1348 | { | |
1349 | return m_communication->PingAdapter(); | |
1350 | } |