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