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