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