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