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