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