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 | ||
2b4d8297 | 50 | CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm, const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) : |
f8513317 | 51 | m_bStarted(false), |
16b1e052 | 52 | m_iHDMIPort(CEC_DEFAULT_HDMI_PORT), |
2abe74eb LOK |
53 | m_strDeviceName(strDeviceName), |
54 | m_communication(serComm), | |
8b7e5ff6 | 55 | m_controller(controller), |
06bfd4d7 | 56 | m_bMonitor(false) |
abbca718 | 57 | { |
ab1469a0 LOK |
58 | m_logicalAddresses.Clear(); |
59 | m_logicalAddresses.Set(iLogicalAddress); | |
20b8870a | 60 | m_types.clear(); |
7871d66e | 61 | for (int iPtr = 0; iPtr <= 16; iPtr++) |
09c10b66 | 62 | m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, iPtr == iLogicalAddress ? iPhysicalAddress : 0); |
abbca718 LOK |
63 | } |
64 | ||
f8513317 LOK |
65 | CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm, const char *strDeviceName, const cec_device_type_list &types) : |
66 | m_bStarted(false), | |
16b1e052 | 67 | m_iHDMIPort(CEC_DEFAULT_HDMI_PORT), |
f8513317 LOK |
68 | m_strDeviceName(strDeviceName), |
69 | m_types(types), | |
70 | m_communication(serComm), | |
71 | m_controller(controller), | |
72 | m_bMonitor(false) | |
73 | { | |
ab1469a0 | 74 | m_logicalAddresses.Clear(); |
f8513317 | 75 | for (int iPtr = 0; iPtr < 16; iPtr++) |
51b2a094 LOK |
76 | { |
77 | switch(iPtr) | |
78 | { | |
79 | case CECDEVICE_AUDIOSYSTEM: | |
5d5a2dc2 | 80 | m_busDevices[iPtr] = new CCECAudioSystem(this, (cec_logical_address) iPtr, 0xFFFF); |
51b2a094 LOK |
81 | break; |
82 | case CECDEVICE_PLAYBACKDEVICE1: | |
83 | case CECDEVICE_PLAYBACKDEVICE2: | |
84 | case CECDEVICE_PLAYBACKDEVICE3: | |
5d5a2dc2 | 85 | m_busDevices[iPtr] = new CCECPlaybackDevice(this, (cec_logical_address) iPtr, 0xFFFF); |
51b2a094 LOK |
86 | break; |
87 | case CECDEVICE_RECORDINGDEVICE1: | |
88 | case CECDEVICE_RECORDINGDEVICE2: | |
89 | case CECDEVICE_RECORDINGDEVICE3: | |
5d5a2dc2 | 90 | m_busDevices[iPtr] = new CCECRecordingDevice(this, (cec_logical_address) iPtr, 0xFFFF); |
51b2a094 LOK |
91 | break; |
92 | case CECDEVICE_TUNER1: | |
93 | case CECDEVICE_TUNER2: | |
94 | case CECDEVICE_TUNER3: | |
95 | case CECDEVICE_TUNER4: | |
5d5a2dc2 | 96 | m_busDevices[iPtr] = new CCECTuner(this, (cec_logical_address) iPtr, 0xFFFF); |
51b2a094 LOK |
97 | break; |
98 | case CECDEVICE_TV: | |
99 | m_busDevices[iPtr] = new CCECTV(this, (cec_logical_address) iPtr, 0); | |
100 | break; | |
101 | default: | |
5d5a2dc2 | 102 | m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, 0xFFFF); |
51b2a094 LOK |
103 | break; |
104 | } | |
105 | } | |
f8513317 LOK |
106 | } |
107 | ||
2abe74eb | 108 | CCECProcessor::~CCECProcessor(void) |
abbca718 | 109 | { |
8b0ee2cc | 110 | m_startCondition.Broadcast(); |
2abe74eb LOK |
111 | StopThread(); |
112 | m_communication = NULL; | |
113 | m_controller = NULL; | |
d1998c7b LOK |
114 | for (unsigned int iPtr = 0; iPtr < 16; iPtr++) |
115 | delete m_busDevices[iPtr]; | |
abbca718 LOK |
116 | } |
117 | ||
2abe74eb | 118 | bool CCECProcessor::Start(void) |
abbca718 | 119 | { |
8b0ee2cc | 120 | CLockObject lock(&m_mutex); |
2abe74eb | 121 | if (!m_communication || !m_communication->IsOpen()) |
13fd6a66 LOK |
122 | { |
123 | m_controller->AddLog(CEC_LOG_ERROR, "connection is closed"); | |
a8f0bd18 | 124 | return false; |
13fd6a66 | 125 | } |
abbca718 | 126 | |
60fa4578 | 127 | if (CreateThread()) |
8b0ee2cc | 128 | { |
f8513317 | 129 | if (!m_startCondition.Wait(&m_mutex) || !m_bStarted) |
8b0ee2cc LOK |
130 | { |
131 | m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread"); | |
132 | return false; | |
133 | } | |
a8f0bd18 | 134 | return true; |
8b0ee2cc | 135 | } |
a8f0bd18 | 136 | else |
2abe74eb | 137 | m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread"); |
abbca718 | 138 | |
a8f0bd18 | 139 | return false; |
abbca718 LOK |
140 | } |
141 | ||
b58d9277 | 142 | bool CCECProcessor::TryLogicalAddress(cec_logical_address address) |
f8513317 | 143 | { |
93fff5c1 | 144 | if (m_busDevices[address]->TryLogicalAddress()) |
f8513317 | 145 | { |
8747dd4f | 146 | /* only set our OSD name and active source for the primary device */ |
ab1469a0 | 147 | if (m_logicalAddresses.IsEmpty()) |
8747dd4f | 148 | { |
787a3cb8 | 149 | m_busDevices[address]->m_strDeviceName = m_strDeviceName; |
8747dd4f LOK |
150 | m_busDevices[address]->m_bActiveSource = true; |
151 | } | |
ab1469a0 | 152 | m_logicalAddresses.Set(address); |
f8513317 LOK |
153 | return true; |
154 | } | |
155 | ||
f8513317 LOK |
156 | return false; |
157 | } | |
158 | ||
b58d9277 | 159 | bool CCECProcessor::FindLogicalAddressRecordingDevice(void) |
f8513317 LOK |
160 | { |
161 | AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'recording device'"); | |
b58d9277 LOK |
162 | return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1) || |
163 | TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2) || | |
164 | TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3); | |
f8513317 LOK |
165 | } |
166 | ||
b58d9277 | 167 | bool CCECProcessor::FindLogicalAddressTuner(void) |
f8513317 LOK |
168 | { |
169 | AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'tuner'"); | |
b58d9277 LOK |
170 | return TryLogicalAddress(CECDEVICE_TUNER1) || |
171 | TryLogicalAddress(CECDEVICE_TUNER2) || | |
172 | TryLogicalAddress(CECDEVICE_TUNER3) || | |
173 | TryLogicalAddress(CECDEVICE_TUNER4); | |
f8513317 LOK |
174 | } |
175 | ||
b58d9277 | 176 | bool CCECProcessor::FindLogicalAddressPlaybackDevice(void) |
f8513317 LOK |
177 | { |
178 | AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'playback device'"); | |
b58d9277 LOK |
179 | return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1) || |
180 | TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2) || | |
181 | TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3); | |
f8513317 LOK |
182 | } |
183 | ||
b58d9277 | 184 | bool CCECProcessor::FindLogicalAddressAudioSystem(void) |
f8513317 LOK |
185 | { |
186 | AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'audio'"); | |
b58d9277 | 187 | return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM); |
f8513317 LOK |
188 | } |
189 | ||
190 | bool CCECProcessor::FindLogicalAddresses(void) | |
191 | { | |
192 | bool bReturn(true); | |
ab1469a0 | 193 | m_logicalAddresses.Clear(); |
f8513317 LOK |
194 | CStdString strLog; |
195 | ||
196 | for (unsigned int iPtr = 0; iPtr < 5; iPtr++) | |
197 | { | |
198 | if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RESERVED) | |
199 | continue; | |
200 | ||
201 | strLog.Format("%s - device %d: type %d", __FUNCTION__, iPtr, m_types.types[iPtr]); | |
202 | AddLog(CEC_LOG_DEBUG, strLog); | |
203 | ||
204 | if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RECORDING_DEVICE) | |
b58d9277 | 205 | bReturn &= FindLogicalAddressRecordingDevice(); |
f8513317 | 206 | if (m_types.types[iPtr] == CEC_DEVICE_TYPE_TUNER) |
b58d9277 | 207 | bReturn &= FindLogicalAddressTuner(); |
f8513317 | 208 | if (m_types.types[iPtr] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE) |
b58d9277 | 209 | bReturn &= FindLogicalAddressPlaybackDevice(); |
f8513317 | 210 | if (m_types.types[iPtr] == CEC_DEVICE_TYPE_AUDIO_SYSTEM) |
b58d9277 | 211 | bReturn &= FindLogicalAddressAudioSystem(); |
f8513317 LOK |
212 | } |
213 | ||
214 | return bReturn; | |
215 | } | |
216 | ||
2abe74eb | 217 | void *CCECProcessor::Process(void) |
f99bc831 | 218 | { |
8747dd4f | 219 | bool bParseFrame(false); |
e0407d3d LOK |
220 | cec_command command; |
221 | CCECAdapterMessage msg; | |
222 | ||
8b0ee2cc | 223 | { |
ab1469a0 | 224 | if (m_logicalAddresses.IsEmpty() && !FindLogicalAddresses()) |
f8513317 LOK |
225 | { |
226 | CLockObject lock(&m_mutex); | |
16b1e052 | 227 | m_controller->AddLog(CEC_LOG_ERROR, "could not detect our logical addresses"); |
f8513317 LOK |
228 | m_startCondition.Signal(); |
229 | return NULL; | |
230 | } | |
231 | ||
ab1469a0 | 232 | SetAckMask(m_logicalAddresses.AckMask()); |
f8513317 LOK |
233 | |
234 | { | |
235 | CLockObject lock(&m_mutex); | |
f8513317 | 236 | m_bStarted = true; |
16b1e052 LOK |
237 | lock.Leave(); |
238 | ||
239 | SetHDMIPort(m_iHDMIPort); | |
240 | ||
241 | lock.Lock(); | |
242 | m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started"); | |
f8513317 LOK |
243 | m_startCondition.Signal(); |
244 | } | |
8b0ee2cc | 245 | } |
abbca718 | 246 | |
13fd6a66 | 247 | while (!IsStopped()) |
abbca718 | 248 | { |
ab1469a0 | 249 | command.Clear(); |
76321de4 LOK |
250 | msg.clear(); |
251 | ||
60fa4578 LOK |
252 | { |
253 | CLockObject lock(&m_mutex); | |
8747dd4f LOK |
254 | if (m_commandBuffer.Pop(command)) |
255 | { | |
256 | bParseFrame = true; | |
257 | } | |
258 | else if (m_communication->IsOpen() && m_communication->Read(msg, 50)) | |
b5f34cf9 LOK |
259 | { |
260 | m_controller->AddLog(msg.is_error() ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString()); | |
f2198ab5 | 261 | if ((bParseFrame = (ParseMessage(msg) && !IsStopped())) == true) |
8747dd4f | 262 | command = m_currentframe; |
b5f34cf9 | 263 | } |
60fa4578 LOK |
264 | } |
265 | ||
13fd6a66 | 266 | if (bParseFrame) |
9dee1670 | 267 | ParseCommand(command); |
8747dd4f | 268 | bParseFrame = false; |
abbca718 | 269 | |
b5f34cf9 LOK |
270 | Sleep(5); |
271 | ||
13fd6a66 LOK |
272 | m_controller->CheckKeypressTimeout(); |
273 | ||
0ab58650 | 274 | for (unsigned int iDevicePtr = 0; iDevicePtr < 16; iDevicePtr++) |
678e227c LOK |
275 | { |
276 | if (!m_logicalAddresses[iDevicePtr]) | |
277 | m_busDevices[iDevicePtr]->PollVendorId(); | |
278 | } | |
0ab58650 | 279 | |
b5f34cf9 | 280 | Sleep(5); |
abbca718 LOK |
281 | } |
282 | ||
60fa4578 | 283 | return NULL; |
abbca718 LOK |
284 | } |
285 | ||
18203d17 | 286 | bool CCECProcessor::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RESERVED */) |
abbca718 | 287 | { |
8ac9c610 LOK |
288 | bool bReturn(false); |
289 | ||
60fa4578 | 290 | if (!IsRunning()) |
8ac9c610 | 291 | return bReturn; |
f99bc831 | 292 | |
18203d17 | 293 | cec_logical_address addr = m_logicalAddresses.primary; |
04437dcf | 294 | |
18203d17 LOK |
295 | if (type != CEC_DEVICE_TYPE_RESERVED) |
296 | { | |
f2198ab5 | 297 | for (uint8_t iPtr = 0; iPtr < 16; iPtr++) |
18203d17 LOK |
298 | { |
299 | if (m_logicalAddresses[iPtr] && m_busDevices[iPtr]->m_type == type) | |
300 | { | |
301 | addr = (cec_logical_address) iPtr; | |
302 | break; | |
303 | } | |
304 | } | |
305 | } | |
306 | ||
16b1e052 | 307 | return SetStreamPath(m_busDevices[addr]->GetPhysicalAddress(false)) && |
4a870dc8 | 308 | m_busDevices[addr]->TransmitActiveSource(); |
18203d17 LOK |
309 | } |
310 | ||
1a6669b8 LOK |
311 | bool CCECProcessor::SetActiveSource(cec_logical_address iAddress) |
312 | { | |
16b1e052 | 313 | return SetStreamPath(m_busDevices[iAddress]->GetPhysicalAddress(false)); |
1a6669b8 LOK |
314 | } |
315 | ||
18203d17 LOK |
316 | bool CCECProcessor::SetActiveView(void) |
317 | { | |
4a870dc8 | 318 | return SetActiveSource(m_types.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED : m_types[0]); |
8ac9c610 LOK |
319 | } |
320 | ||
28fa6c97 | 321 | bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode, bool bSendUpdate /* = true */) |
a9232a79 LOK |
322 | { |
323 | bool bReturn(false); | |
324 | ||
325 | CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE); | |
326 | if (device) | |
327 | { | |
328 | ((CCECPlaybackDevice *) device)->SetDeckControlMode(mode); | |
28fa6c97 LOK |
329 | if (bSendUpdate) |
330 | ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV); | |
a9232a79 LOK |
331 | bReturn = true; |
332 | } | |
333 | ||
334 | return bReturn; | |
335 | } | |
336 | ||
28fa6c97 | 337 | bool CCECProcessor::SetDeckInfo(cec_deck_info info, bool bSendUpdate /* = true */) |
a9232a79 LOK |
338 | { |
339 | bool bReturn(false); | |
340 | ||
341 | CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE); | |
342 | if (device) | |
343 | { | |
344 | ((CCECPlaybackDevice *) device)->SetDeckStatus(info); | |
28fa6c97 LOK |
345 | if (bSendUpdate) |
346 | ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV); | |
a9232a79 LOK |
347 | bReturn = true; |
348 | } | |
349 | ||
350 | return bReturn; | |
351 | } | |
352 | ||
16b1e052 LOK |
353 | bool CCECProcessor::SetHDMIPort(uint8_t iPort) |
354 | { | |
355 | bool bReturn(false); | |
356 | ||
357 | CStdString strLog; | |
358 | strLog.Format("setting HDMI port to %d", iPort); | |
359 | AddLog(CEC_LOG_DEBUG, strLog); | |
360 | ||
361 | m_iHDMIPort = iPort; | |
362 | if (!m_bStarted) | |
363 | return true; | |
364 | ||
365 | uint16_t iPhysicalAddress(0); | |
366 | int iPos = 3; | |
367 | while(!bReturn && iPos >= 0) | |
368 | { | |
369 | iPhysicalAddress += ((uint16_t)iPort * (0x1 << iPos*4)); | |
370 | strLog.Format("checking physical address %4x", iPhysicalAddress); | |
371 | AddLog(CEC_LOG_DEBUG, strLog); | |
372 | if (CheckPhysicalAddress(iPhysicalAddress)) | |
373 | { | |
374 | strLog.Format("physical address %4x is in use", iPhysicalAddress); | |
375 | AddLog(CEC_LOG_DEBUG, strLog); | |
376 | iPos--; | |
377 | } | |
378 | else | |
379 | { | |
380 | SetPhysicalAddress(iPhysicalAddress); | |
381 | bReturn = true; | |
382 | } | |
383 | } | |
384 | ||
385 | return bReturn; | |
386 | } | |
387 | ||
388 | bool CCECProcessor::CheckPhysicalAddress(uint16_t iPhysicalAddress) | |
389 | { | |
390 | for (unsigned int iPtr = 0; iPtr < 16; iPtr++) | |
391 | { | |
392 | if (m_busDevices[iPtr]->GetPhysicalAddress(false) == iPhysicalAddress) | |
393 | return true; | |
394 | } | |
395 | return false; | |
396 | } | |
397 | ||
8ac9c610 LOK |
398 | bool CCECProcessor::SetStreamPath(uint16_t iStreamPath) |
399 | { | |
04437dcf LOK |
400 | bool bReturn(false); |
401 | ||
8ac9c610 LOK |
402 | CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamPath); |
403 | if (device) | |
404 | { | |
7856411b | 405 | device->SetActiveDevice(); |
91bea174 | 406 | bReturn = true; |
8ac9c610 LOK |
407 | } |
408 | ||
04437dcf | 409 | return bReturn; |
abbca718 LOK |
410 | } |
411 | ||
2abe74eb | 412 | bool CCECProcessor::SetInactiveView(void) |
abbca718 | 413 | { |
60fa4578 | 414 | if (!IsRunning()) |
f99bc831 LOK |
415 | return false; |
416 | ||
ab1469a0 | 417 | if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary]) |
29912296 | 418 | return m_busDevices[m_logicalAddresses.primary]->TransmitInactiveView(); |
cc60ab1c | 419 | return false; |
abbca718 LOK |
420 | } |
421 | ||
9dee1670 | 422 | void CCECProcessor::LogOutput(const cec_command &data) |
abbca718 | 423 | { |
1d3ca3de | 424 | CStdString strTx; |
57f45e6c LOK |
425 | strTx.Format("<< %02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination); |
426 | if (data.opcode_set) | |
427 | strTx.AppendFormat(":%02x", (uint8_t)data.opcode); | |
9dee1670 | 428 | |
06a1f7ce | 429 | for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++) |
1d3ca3de LOK |
430 | strTx.AppendFormat(":%02x", data.parameters[iPtr]); |
431 | m_controller->AddLog(CEC_LOG_TRAFFIC, strTx.c_str()); | |
9dee1670 | 432 | } |
2abe74eb | 433 | |
06bfd4d7 | 434 | bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress) |
abbca718 | 435 | { |
f8513317 | 436 | if (m_logicalAddresses.primary != iLogicalAddress) |
06775107 LOK |
437 | { |
438 | CStdString strLog; | |
f8513317 | 439 | strLog.Format("<< setting primary logical address to %1x", iLogicalAddress); |
06775107 | 440 | m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str()); |
f8513317 | 441 | m_logicalAddresses.primary = iLogicalAddress; |
ab1469a0 LOK |
442 | m_logicalAddresses.Set(iLogicalAddress); |
443 | return SetAckMask(m_logicalAddresses.AckMask()); | |
06775107 | 444 | } |
2abe74eb | 445 | |
06bfd4d7 | 446 | return true; |
abbca718 | 447 | } |
825ddb96 | 448 | |
28fa6c97 LOK |
449 | bool CCECProcessor::SetMenuState(cec_menu_state state, bool bSendUpdate /* = true */) |
450 | { | |
451 | for (uint8_t iPtr = 0; iPtr < 16; iPtr++) | |
452 | { | |
453 | if (m_logicalAddresses[iPtr]) | |
454 | m_busDevices[iPtr]->SetMenuState(state); | |
455 | } | |
456 | ||
457 | if (bSendUpdate) | |
458 | m_busDevices[m_logicalAddresses.primary]->TransmitMenuState(CECDEVICE_TV); | |
459 | ||
460 | return true; | |
461 | } | |
462 | ||
2492216a LOK |
463 | bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress) |
464 | { | |
16b1e052 | 465 | if (!m_logicalAddresses.IsEmpty()) |
cc60ab1c | 466 | { |
16b1e052 LOK |
467 | for (uint8_t iPtr = 0; iPtr < 15; iPtr++) |
468 | if (m_logicalAddresses[iPtr]) | |
469 | m_busDevices[iPtr]->SetPhysicalAddress(iPhysicalAddress); | |
8ac9c610 | 470 | return SetActiveView(); |
cc60ab1c LOK |
471 | } |
472 | return false; | |
1969b140 LOK |
473 | } |
474 | ||
8b7e5ff6 LOK |
475 | bool CCECProcessor::SwitchMonitoring(bool bEnable) |
476 | { | |
477 | CStdString strLog; | |
478 | strLog.Format("== %s monitoring mode ==", bEnable ? "enabling" : "disabling"); | |
479 | m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str()); | |
480 | ||
481 | m_bMonitor = bEnable; | |
482 | if (bEnable) | |
06bfd4d7 | 483 | return SetAckMask(0); |
8b7e5ff6 | 484 | else |
ab1469a0 | 485 | return SetAckMask(m_logicalAddresses.AckMask()); |
8b7e5ff6 LOK |
486 | } |
487 | ||
57f45e6c LOK |
488 | bool CCECProcessor::PollDevice(cec_logical_address iAddress) |
489 | { | |
490 | if (iAddress != CECDEVICE_UNKNOWN && m_busDevices[iAddress]) | |
93729720 | 491 | return m_busDevices[m_logicalAddresses.primary]->TransmitPoll(iAddress); |
57f45e6c LOK |
492 | return false; |
493 | } | |
494 | ||
9f332fe2 | 495 | uint8_t CCECProcessor::VolumeUp(bool bWait /* = true */) |
04e637f9 LOK |
496 | { |
497 | uint8_t status = 0; | |
498 | if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM)) | |
9f332fe2 | 499 | status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeUp(bWait); |
04e637f9 LOK |
500 | |
501 | return status; | |
502 | } | |
503 | ||
9f332fe2 | 504 | uint8_t CCECProcessor::VolumeDown(bool bWait /* = true */) |
04e637f9 LOK |
505 | { |
506 | uint8_t status = 0; | |
507 | if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM)) | |
9f332fe2 | 508 | status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeDown(bWait); |
04e637f9 LOK |
509 | |
510 | return status; | |
511 | } | |
512 | ||
9f332fe2 | 513 | uint8_t CCECProcessor::MuteAudio(bool bWait /* = true */) |
04e637f9 LOK |
514 | { |
515 | uint8_t status = 0; | |
516 | if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM)) | |
9f332fe2 | 517 | status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->MuteAudio(bWait); |
04e637f9 LOK |
518 | |
519 | return status; | |
520 | } | |
521 | ||
16b1e052 | 522 | CCECBusDevice *CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bRefresh /* = false */) const |
8ac9c610 | 523 | { |
16b1e052 | 524 | if (m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false) == iPhysicalAddress) |
b58d9277 | 525 | return m_busDevices[m_logicalAddresses.primary]; |
8ac9c610 | 526 | |
b58d9277 | 527 | CCECBusDevice *device = NULL; |
8ac9c610 LOK |
528 | for (unsigned int iPtr = 0; iPtr < 16; iPtr++) |
529 | { | |
16b1e052 | 530 | if (m_busDevices[iPtr]->GetPhysicalAddress(bRefresh) == iPhysicalAddress) |
8ac9c610 LOK |
531 | { |
532 | device = m_busDevices[iPtr]; | |
533 | break; | |
534 | } | |
535 | } | |
536 | ||
537 | return device; | |
538 | } | |
539 | ||
a9232a79 LOK |
540 | CCECBusDevice *CCECProcessor::GetDeviceByType(cec_device_type type) const |
541 | { | |
542 | CCECBusDevice *device = NULL; | |
543 | ||
544 | for (unsigned int iPtr = 0; iPtr < 16; iPtr++) | |
545 | { | |
546 | if (m_busDevices[iPtr]->m_type == type) | |
547 | { | |
548 | device = m_busDevices[iPtr]; | |
549 | break; | |
550 | } | |
551 | } | |
552 | ||
553 | return device; | |
554 | } | |
555 | ||
6a1c0009 LOK |
556 | cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress) |
557 | { | |
558 | return m_busDevices[iAddress]->GetCecVersion(); | |
559 | } | |
560 | ||
a3269a0a LOK |
561 | bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language) |
562 | { | |
cc60ab1c LOK |
563 | if (m_busDevices[iAddress]) |
564 | { | |
565 | *language = m_busDevices[iAddress]->GetMenuLanguage(); | |
5744fbba | 566 | return (strcmp(language->language, "???") != 0); |
cc60ab1c LOK |
567 | } |
568 | return false; | |
a3269a0a LOK |
569 | } |
570 | ||
44c74256 LOK |
571 | uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress) |
572 | { | |
cc60ab1c LOK |
573 | if (m_busDevices[iAddress]) |
574 | return m_busDevices[iAddress]->GetVendorId(); | |
575 | return false; | |
44c74256 LOK |
576 | } |
577 | ||
e55f3f70 LOK |
578 | cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress) |
579 | { | |
cc60ab1c LOK |
580 | if (m_busDevices[iAddress]) |
581 | return m_busDevices[iAddress]->GetPowerStatus(); | |
582 | return CEC_POWER_STATUS_UNKNOWN; | |
e55f3f70 LOK |
583 | } |
584 | ||
8d84e2c0 | 585 | bool CCECProcessor::Transmit(const cec_command &data) |
825ddb96 | 586 | { |
28352a04 | 587 | bool bReturn(false); |
13929cff LOK |
588 | LogOutput(data); |
589 | ||
28352a04 LOK |
590 | CCECAdapterMessage *output = new CCECAdapterMessage(data); |
591 | bReturn = Transmit(output); | |
592 | delete output; | |
593 | ||
594 | return bReturn; | |
06bfd4d7 | 595 | } |
13929cff | 596 | |
28352a04 | 597 | bool CCECProcessor::Transmit(CCECAdapterMessage *output) |
06bfd4d7 LOK |
598 | { |
599 | bool bReturn(false); | |
2abe74eb | 600 | CLockObject lock(&m_mutex); |
eb35e4ca LOK |
601 | { |
602 | CLockObject msgLock(&output->mutex); | |
603 | if (!m_communication || !m_communication->Write(output)) | |
604 | return bReturn; | |
605 | else | |
0e31a62c | 606 | { |
3a0ca6e1 | 607 | output->condition.Wait(&output->mutex); |
0e31a62c LOK |
608 | if (output->state != ADAPTER_MESSAGE_STATE_SENT) |
609 | { | |
610 | m_controller->AddLog(CEC_LOG_ERROR, "command was not sent"); | |
611 | return bReturn; | |
612 | } | |
613 | } | |
2abe74eb | 614 | |
06bfd4d7 | 615 | if (output->transmit_timeout > 0) |
cd505625 | 616 | { |
06bfd4d7 | 617 | if ((bReturn = WaitForTransmitSucceeded(output->size(), output->transmit_timeout)) == false) |
0d8f286e | 618 | m_controller->AddLog(CEC_LOG_DEBUG, "did not receive ack"); |
cd505625 LOK |
619 | } |
620 | else | |
cd505625 | 621 | bReturn = true; |
2abe74eb LOK |
622 | } |
623 | ||
7ea0d558 | 624 | return bReturn; |
825ddb96 | 625 | } |
abbca718 | 626 | |
22b4e74a | 627 | void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, cec_abort_reason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */) |
abbca718 | 628 | { |
9dee1670 LOK |
629 | m_controller->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message"); |
630 | ||
06a1f7ce | 631 | cec_command command; |
f8513317 | 632 | // TODO |
ab1469a0 LOK |
633 | cec_command::Format(command, m_logicalAddresses.primary, address, CEC_OPCODE_FEATURE_ABORT); |
634 | command.parameters.PushBack((uint8_t)opcode); | |
635 | command.parameters.PushBack((uint8_t)reason); | |
9dee1670 LOK |
636 | |
637 | Transmit(command); | |
abbca718 LOK |
638 | } |
639 | ||
1f0e9e0f | 640 | bool CCECProcessor::WaitForTransmitSucceeded(uint8_t iLength, uint32_t iTimeout /* = 1000 */) |
abbca718 | 641 | { |
1f0e9e0f LOK |
642 | bool bError(false); |
643 | bool bTransmitSucceeded(false); | |
7ea0d558 | 644 | uint8_t iPacketsLeft(iLength / 4); |
abbca718 LOK |
645 | |
646 | int64_t iNow = GetTimeMs(); | |
25701fa6 | 647 | int64_t iTargetTime = iNow + (uint64_t) iTimeout; |
abbca718 | 648 | |
1f0e9e0f | 649 | while (!bTransmitSucceeded && !bError && (iTimeout == 0 || iNow < iTargetTime)) |
abbca718 | 650 | { |
220537f2 | 651 | CCECAdapterMessage msg; |
25701fa6 | 652 | |
06a1f7ce | 653 | if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000)) |
abbca718 | 654 | { |
abbca718 | 655 | iNow = GetTimeMs(); |
5d38b0b6 LOK |
656 | continue; |
657 | } | |
658 | ||
edab6137 | 659 | if ((bError = msg.is_error()) == false) |
7ea0d558 | 660 | { |
0544ff56 LOK |
661 | m_controller->AddLog(bError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString()); |
662 | ||
edab6137 LOK |
663 | switch(msg.message()) |
664 | { | |
665 | case MSGCODE_COMMAND_ACCEPTED: | |
666 | if (iPacketsLeft > 0) | |
667 | iPacketsLeft--; | |
668 | break; | |
669 | case MSGCODE_TRANSMIT_SUCCEEDED: | |
670 | bTransmitSucceeded = (iPacketsLeft == 0); | |
671 | bError = !bTransmitSucceeded; | |
672 | break; | |
673 | default: | |
8747dd4f LOK |
674 | if (ParseMessage(msg)) |
675 | m_commandBuffer.Push(m_currentframe); | |
edab6137 | 676 | } |
7ea0d558 | 677 | |
edab6137 LOK |
678 | iNow = GetTimeMs(); |
679 | } | |
abbca718 LOK |
680 | } |
681 | ||
1f0e9e0f | 682 | return bTransmitSucceeded && !bError; |
abbca718 LOK |
683 | } |
684 | ||
0e7b6c54 | 685 | bool CCECProcessor::ParseMessage(const CCECAdapterMessage &msg) |
abbca718 | 686 | { |
7ea0d558 | 687 | bool bEom = false; |
60fa4578 | 688 | |
0e7b6c54 | 689 | if (msg.empty()) |
7ea0d558 | 690 | return bEom; |
abbca718 | 691 | |
0e7b6c54 | 692 | switch(msg.message()) |
abbca718 | 693 | { |
abbca718 LOK |
694 | case MSGCODE_FRAME_START: |
695 | { | |
ab1469a0 | 696 | m_currentframe.Clear(); |
0e7b6c54 | 697 | if (msg.size() >= 2) |
abbca718 | 698 | { |
0e7b6c54 LOK |
699 | m_currentframe.initiator = msg.initiator(); |
700 | m_currentframe.destination = msg.destination(); | |
701 | m_currentframe.ack = msg.ack(); | |
702 | m_currentframe.eom = msg.eom(); | |
abbca718 | 703 | } |
abbca718 LOK |
704 | } |
705 | break; | |
706 | case MSGCODE_FRAME_DATA: | |
707 | { | |
0e7b6c54 | 708 | if (msg.size() >= 2) |
78e8010a | 709 | { |
ab1469a0 | 710 | m_currentframe.PushBack(msg[1]); |
0e7b6c54 | 711 | m_currentframe.eom = msg.eom(); |
abbca718 | 712 | } |
0e7b6c54 | 713 | bEom = msg.eom(); |
abbca718 | 714 | } |
78e8010a | 715 | break; |
abbca718 LOK |
716 | default: |
717 | break; | |
718 | } | |
7ea0d558 LOK |
719 | |
720 | return bEom; | |
abbca718 LOK |
721 | } |
722 | ||
e9de9629 | 723 | void CCECProcessor::ParseCommand(cec_command &command) |
abbca718 | 724 | { |
e9de9629 | 725 | CStdString dataStr; |
1d3ca3de | 726 | dataStr.Format(">> %1x%1x:%02x", command.initiator, command.destination, command.opcode); |
e9de9629 LOK |
727 | for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++) |
728 | dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]); | |
1d3ca3de | 729 | m_controller->AddLog(CEC_LOG_TRAFFIC, dataStr.c_str()); |
dc113aca | 730 | |
7871d66e | 731 | if (!m_bMonitor && command.initiator >= CECDEVICE_TV && command.initiator <= CECDEVICE_BROADCAST) |
e9de9629 | 732 | m_busDevices[(uint8_t)command.initiator]->HandleCommand(command); |
dc113aca LOK |
733 | } |
734 | ||
6d858ba4 LOK |
735 | cec_logical_addresses CCECProcessor::GetActiveDevices(void) |
736 | { | |
737 | cec_logical_addresses addresses; | |
738 | for (unsigned int iPtr = 0; iPtr < 15; iPtr++) | |
739 | { | |
740 | if (m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) | |
741 | addresses.Set((cec_logical_address) iPtr); | |
742 | } | |
743 | return addresses; | |
744 | } | |
745 | ||
746 | bool CCECProcessor::IsActiveDevice(cec_logical_address address) | |
747 | { | |
748 | return m_busDevices[address]->GetStatus() == CEC_DEVICE_STATUS_PRESENT; | |
749 | } | |
750 | ||
751 | bool CCECProcessor::IsActiveDeviceType(cec_device_type type) | |
752 | { | |
6d858ba4 | 753 | for (unsigned int iPtr = 0; iPtr < 15; iPtr++) |
2813bd07 LOK |
754 | { |
755 | if (m_busDevices[iPtr]->GetType() == type && m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT) | |
6d858ba4 | 756 | return true; |
2813bd07 LOK |
757 | } |
758 | ||
6d858ba4 LOK |
759 | return false; |
760 | } | |
761 | ||
0f23c85c LOK |
762 | uint16_t CCECProcessor::GetPhysicalAddress(void) const |
763 | { | |
ab1469a0 | 764 | if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary]) |
16b1e052 | 765 | return m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false); |
cc60ab1c | 766 | return false; |
0f23c85c LOK |
767 | } |
768 | ||
e9de9629 | 769 | void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode) |
dc113aca | 770 | { |
e9de9629 | 771 | m_controller->SetCurrentButton(iButtonCode); |
dc113aca LOK |
772 | } |
773 | ||
e9de9629 | 774 | void CCECProcessor::AddCommand(const cec_command &command) |
dc113aca | 775 | { |
0544ff56 | 776 | m_controller->AddCommand(command); |
dc113aca LOK |
777 | } |
778 | ||
95ba7a09 LOK |
779 | void CCECProcessor::AddKey(cec_keypress &key) |
780 | { | |
781 | m_controller->AddKey(key); | |
782 | } | |
783 | ||
e9de9629 | 784 | void CCECProcessor::AddKey(void) |
dc113aca | 785 | { |
e9de9629 | 786 | m_controller->AddKey(); |
abbca718 | 787 | } |
acec5f48 | 788 | |
e9de9629 | 789 | void CCECProcessor::AddLog(cec_log_level level, const CStdString &strMessage) |
acec5f48 | 790 | { |
e9de9629 | 791 | m_controller->AddLog(level, strMessage); |
acec5f48 | 792 | } |
06bfd4d7 LOK |
793 | |
794 | bool CCECProcessor::SetAckMask(uint16_t iMask) | |
795 | { | |
28352a04 | 796 | bool bReturn(false); |
06bfd4d7 LOK |
797 | CStdString strLog; |
798 | strLog.Format("setting ackmask to %2x", iMask); | |
799 | m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str()); | |
800 | ||
28352a04 | 801 | CCECAdapterMessage *output = new CCECAdapterMessage; |
06bfd4d7 LOK |
802 | |
803 | output->push_back(MSGSTART); | |
804 | output->push_escaped(MSGCODE_SET_ACK_MASK); | |
805 | output->push_escaped(iMask >> 8); | |
806 | output->push_escaped((uint8_t)iMask); | |
807 | output->push_back(MSGEND); | |
808 | ||
28352a04 | 809 | if ((bReturn = Transmit(output)) == false) |
06bfd4d7 | 810 | m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask"); |
06bfd4d7 | 811 | |
28352a04 LOK |
812 | delete output; |
813 | ||
814 | return bReturn; | |
06bfd4d7 | 815 | } |