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