cec: always wait for thread exit in CThread's desctructor
[deb_libcec.git] / src / lib / CECProcessor.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011-2012 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
33#include "CECProcessor.h"
34
35#include "adapter/USBCECAdapterCommunication.h"
36#include "devices/CECBusDevice.h"
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"
42#include "implementations/CECCommandHandler.h"
43#include "LibCEC.h"
44#include "platform/util/timeutils.h"
45
46using namespace CEC;
47using namespace std;
48using namespace PLATFORM;
49
50CCECProcessor::CCECProcessor(CLibCEC *controller, libcec_configuration *configuration) :
51 m_bConnectionOpened(false),
52 m_bInitialised(false),
53 m_communication(NULL),
54 m_controller(controller),
55 m_bMonitor(false),
56 m_iStandardLineTimeout(3),
57 m_iRetryLineTimeout(3),
58 m_iLastTransmission(0)
59{
60 CreateBusDevices();
61 m_configuration.Clear();
62 m_configuration.serverVersion = configuration->serverVersion;
63 SetConfiguration(configuration);
64
65 if (m_configuration.tvVendor != CEC_VENDOR_UNKNOWN)
66 m_busDevices[CECDEVICE_TV]->ReplaceHandler(false);
67}
68
69CCECProcessor::CCECProcessor(CLibCEC *controller, const char *strDeviceName, const cec_device_type_list &types, uint16_t iPhysicalAddress) :
70 m_bConnectionOpened(false),
71 m_bInitialised(false),
72 m_communication(NULL),
73 m_controller(controller),
74 m_bMonitor(false),
75 m_iStandardLineTimeout(3),
76 m_iRetryLineTimeout(3),
77 m_iLastTransmission(0)
78{
79 m_configuration.Clear();
80 m_configuration.serverVersion = CEC_SERVER_VERSION_1_5_2;
81
82 // client version < 1.5.0
83 m_configuration.clientVersion = (uint32_t)CEC_CLIENT_VERSION_PRE_1_5;
84 snprintf(m_configuration.strDeviceName, 13, "%s", strDeviceName);
85 m_configuration.deviceTypes = types;
86 m_configuration.iPhysicalAddress = iPhysicalAddress;
87 m_configuration.baseDevice = (cec_logical_address)CEC_DEFAULT_BASE_DEVICE;
88 m_configuration.iHDMIPort = CEC_DEFAULT_HDMI_PORT;
89
90 if (m_configuration.deviceTypes.IsEmpty())
91 m_configuration.deviceTypes.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
92 CreateBusDevices();
93}
94
95void CCECProcessor::CreateBusDevices(void)
96{
97 for (int iPtr = 0; iPtr < 16; iPtr++)
98 {
99 switch(iPtr)
100 {
101 case CECDEVICE_AUDIOSYSTEM:
102 m_busDevices[iPtr] = new CCECAudioSystem(this, (cec_logical_address) iPtr, 0xFFFF);
103 break;
104 case CECDEVICE_PLAYBACKDEVICE1:
105 case CECDEVICE_PLAYBACKDEVICE2:
106 case CECDEVICE_PLAYBACKDEVICE3:
107 m_busDevices[iPtr] = new CCECPlaybackDevice(this, (cec_logical_address) iPtr, 0xFFFF);
108 break;
109 case CECDEVICE_RECORDINGDEVICE1:
110 case CECDEVICE_RECORDINGDEVICE2:
111 case CECDEVICE_RECORDINGDEVICE3:
112 m_busDevices[iPtr] = new CCECRecordingDevice(this, (cec_logical_address) iPtr, 0xFFFF);
113 break;
114 case CECDEVICE_TUNER1:
115 case CECDEVICE_TUNER2:
116 case CECDEVICE_TUNER3:
117 case CECDEVICE_TUNER4:
118 m_busDevices[iPtr] = new CCECTuner(this, (cec_logical_address) iPtr, 0xFFFF);
119 break;
120 case CECDEVICE_TV:
121 m_busDevices[iPtr] = new CCECTV(this, (cec_logical_address) iPtr, 0);
122 break;
123 default:
124 m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, 0xFFFF);
125 break;
126 }
127 }
128}
129
130CCECProcessor::~CCECProcessor(void)
131{
132 Close();
133
134 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
135 delete m_busDevices[iPtr];
136}
137
138void CCECProcessor::Close(void)
139{
140 StopThread(false);
141 SetInitialised(false);
142 StopThread();
143
144 bool bClose(false);
145 {
146 CLockObject lock(m_mutex);
147 bClose = m_bConnectionOpened;
148 m_bConnectionOpened = false;
149 }
150
151 if (bClose && m_communication)
152 {
153 m_communication->PersistConfiguration(&m_configuration);
154 m_communication->Close();
155 delete m_communication;
156 m_communication = NULL;
157 }
158}
159
160bool CCECProcessor::OpenConnection(const char *strPort, uint16_t iBaudRate, uint32_t iTimeoutMs)
161{
162 bool bReturn(false);
163 Close();
164
165 {
166 CLockObject lock(m_mutex);
167 if (m_bConnectionOpened)
168 {
169 CLibCEC::AddLog(CEC_LOG_ERROR, "connection already opened");
170 return false;
171 }
172 m_communication = new CUSBCECAdapterCommunication(this, strPort, iBaudRate);
173 m_bConnectionOpened = (m_communication != NULL);
174 }
175
176 /* check for an already opened connection */
177 if (m_communication->IsOpen())
178 {
179 CLibCEC::AddLog(CEC_LOG_ERROR, "connection already opened");
180 return bReturn;
181 }
182
183 CTimeout timeout(iTimeoutMs > 0 ? iTimeoutMs : CEC_DEFAULT_TRANSMIT_WAIT);
184
185 /* open a new connection */
186 unsigned iConnectTry(0);
187 while (timeout.TimeLeft() > 0 && (bReturn = m_communication->Open(this, (timeout.TimeLeft() / CEC_CONNECT_TRIES))) == false)
188 {
189 CLibCEC::AddLog(CEC_LOG_ERROR, "could not open a connection (try %d)", ++iConnectTry);
190 m_communication->Close();
191 CEvent::Sleep(1000);
192 }
193
194 if (bReturn)
195 CLibCEC::AddLog(CEC_LOG_NOTICE, "connected to the CEC adapter. firmware version = %d, client version = %s", m_communication->GetFirmwareVersion(), ToString((cec_client_version)m_configuration.clientVersion));
196
197 if (m_configuration.bGetSettingsFromROM == 1)
198 m_communication->GetConfiguration(&m_configuration);
199
200 return bReturn;
201}
202
203bool CCECProcessor::IsInitialised(void)
204{
205 CLockObject lock(m_mutex);
206 return m_bInitialised;
207}
208
209void CCECProcessor::SetInitialised(bool bSetTo /* = true */)
210{
211 CLockObject lock(m_mutex);
212 m_bInitialised = bSetTo;
213}
214
215bool CCECProcessor::Initialise(void)
216{
217 bool bReturn(false);
218 {
219 CLockObject lock(m_mutex);
220 if (!m_configuration.logicalAddresses.IsEmpty())
221 m_configuration.logicalAddresses.Clear();
222
223 if (!FindLogicalAddresses())
224 {
225 CLibCEC::AddLog(CEC_LOG_ERROR, "could not detect our logical addresses");
226 return bReturn;
227 }
228
229 /* only set our OSD name for the primary device */
230 m_busDevices[m_configuration.logicalAddresses.primary]->m_strDeviceName = m_configuration.strDeviceName;
231
232 /* make the primary device the active source if the option is set */
233 if (m_configuration.bActivateSource == 1)
234 m_busDevices[m_configuration.logicalAddresses.primary]->m_bActiveSource = true;
235 }
236
237 /* get the vendor id from the TV, so we are using the correct handler */
238 m_busDevices[CECDEVICE_TV]->GetVendorId();
239
240 if (m_configuration.iPhysicalAddress != 0)
241 {
242 CLibCEC::AddLog(CEC_LOG_NOTICE, "setting the physical address to %4x", m_configuration.iPhysicalAddress);
243 m_busDevices[m_configuration.logicalAddresses.primary]->m_iPhysicalAddress = m_configuration.iPhysicalAddress;
244 if ((bReturn = m_busDevices[m_configuration.logicalAddresses.primary]->TransmitPhysicalAddress()) == false)
245 CLibCEC::AddLog(CEC_LOG_ERROR, "unable to set the physical address to %4x", m_configuration.iPhysicalAddress);
246 }
247 else if (m_configuration.iPhysicalAddress == 0 && (bReturn = SetHDMIPort(m_configuration.baseDevice, m_configuration.iHDMIPort, true)) == false)
248 CLibCEC::AddLog(CEC_LOG_ERROR, "unable to set HDMI port %d on %s (%x)", m_configuration.iHDMIPort, ToString(m_configuration.baseDevice), (uint8_t)m_configuration.baseDevice);
249
250 if (m_configuration.bActivateSource == 1)
251 m_busDevices[m_configuration.logicalAddresses.primary]->ActivateSource();
252
253 SetInitialised(bReturn);
254 if (bReturn)
255 CLibCEC::ConfigurationChanged(m_configuration);
256
257 return bReturn;
258}
259
260bool CCECProcessor::Start(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */)
261{
262 bool bReturn(false);
263
264 {
265 CLockObject lock(m_mutex);
266 if (!OpenConnection(strPort, iBaudRate, iTimeoutMs))
267 return bReturn;
268
269 /* create the processor thread */
270 if (!CreateThread())
271 {
272 CLibCEC::AddLog(CEC_LOG_ERROR, "could not create a processor thread");
273 return bReturn;
274 }
275 }
276
277 if ((bReturn = Initialise()) == false)
278 {
279 CLibCEC::AddLog(CEC_LOG_ERROR, "could not create a processor thread");
280 StopThread(true);
281 }
282 else
283 {
284 CLibCEC::AddLog(CEC_LOG_DEBUG, "processor thread started");
285 }
286
287 return bReturn;
288}
289
290bool CCECProcessor::TryLogicalAddress(cec_logical_address address)
291{
292 if (m_busDevices[address]->TryLogicalAddress())
293 {
294 m_configuration.logicalAddresses.Set(address);
295 return true;
296 }
297
298 return false;
299}
300
301bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
302{
303 CLibCEC::AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'recording device'");
304 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1) ||
305 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2) ||
306 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3);
307}
308
309bool CCECProcessor::FindLogicalAddressTuner(void)
310{
311 CLibCEC::AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'tuner'");
312 return TryLogicalAddress(CECDEVICE_TUNER1) ||
313 TryLogicalAddress(CECDEVICE_TUNER2) ||
314 TryLogicalAddress(CECDEVICE_TUNER3) ||
315 TryLogicalAddress(CECDEVICE_TUNER4);
316}
317
318bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
319{
320 CLibCEC::AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'playback device'");
321 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1) ||
322 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2) ||
323 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3);
324}
325
326bool CCECProcessor::FindLogicalAddressAudioSystem(void)
327{
328 CLibCEC::AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'audio'");
329 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM);
330}
331
332bool CCECProcessor::ChangeDeviceType(cec_device_type from, cec_device_type to)
333{
334 bool bChanged(false);
335
336 CLibCEC::AddLog(CEC_LOG_NOTICE, "changing device type '%s' into '%s'", ToString(from), ToString(to));
337
338 CLockObject lock(m_mutex);
339 CCECBusDevice *previousDevice = GetDeviceByType(from);
340 m_configuration.logicalAddresses.primary = CECDEVICE_UNKNOWN;
341
342 for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
343 {
344 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_RESERVED)
345 continue;
346
347 if (m_configuration.deviceTypes.types[iPtr] == from)
348 {
349 bChanged = true;
350 m_configuration.deviceTypes.types[iPtr] = to;
351 }
352 else if (m_configuration.deviceTypes.types[iPtr] == to && bChanged)
353 {
354 m_configuration.deviceTypes.types[iPtr] = CEC_DEVICE_TYPE_RESERVED;
355 }
356 }
357
358 if (bChanged)
359 {
360 FindLogicalAddresses();
361
362 CCECBusDevice *newDevice = GetDeviceByType(to);
363 if (previousDevice && newDevice)
364 {
365 newDevice->SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
366 previousDevice->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT);
367
368 newDevice->SetCecVersion(previousDevice->GetCecVersion(false));
369 previousDevice->SetCecVersion(CEC_VERSION_UNKNOWN);
370
371 newDevice->SetMenuLanguage(previousDevice->GetMenuLanguage(false));
372 cec_menu_language lang;
373 lang.device = previousDevice->GetLogicalAddress();
374 for (unsigned int iPtr = 0; iPtr < 4; iPtr++)
375 lang.language[iPtr] = '?';
376 lang.language[3] = 0;
377 previousDevice->SetMenuLanguage(lang);
378
379 newDevice->SetMenuState(previousDevice->GetMenuState());
380 previousDevice->SetMenuState(CEC_MENU_STATE_DEACTIVATED);
381
382 newDevice->SetOSDName(previousDevice->GetOSDName(false));
383 previousDevice->SetOSDName(ToString(previousDevice->GetLogicalAddress()));
384
385 newDevice->SetPhysicalAddress(previousDevice->GetPhysicalAddress(false));
386 previousDevice->SetPhysicalAddress(0xFFFF);
387
388 newDevice->SetPowerStatus(previousDevice->GetPowerStatus(false));
389 previousDevice->SetPowerStatus(CEC_POWER_STATUS_UNKNOWN);
390
391 newDevice->SetVendorId(previousDevice->GetVendorId(false));
392 previousDevice->SetVendorId(CEC_VENDOR_UNKNOWN);
393
394 if ((from == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || from == CEC_DEVICE_TYPE_RECORDING_DEVICE) &&
395 (to == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || to == CEC_DEVICE_TYPE_RECORDING_DEVICE))
396 {
397 ((CCECPlaybackDevice *) newDevice)->SetDeckControlMode(((CCECPlaybackDevice *) previousDevice)->GetDeckControlMode());
398 ((CCECPlaybackDevice *) previousDevice)->SetDeckControlMode(CEC_DECK_CONTROL_MODE_STOP);
399
400 ((CCECPlaybackDevice *) newDevice)->SetDeckStatus(((CCECPlaybackDevice *) previousDevice)->GetDeckStatus());
401 ((CCECPlaybackDevice *) previousDevice)->SetDeckStatus(CEC_DECK_INFO_STOP);
402 }
403 }
404 }
405
406 return true;
407}
408
409bool CCECProcessor::FindLogicalAddresses(void)
410{
411 bool bReturn(true);
412 m_configuration.logicalAddresses.Clear();
413
414 if (m_configuration.deviceTypes.IsEmpty())
415 {
416 CLibCEC::AddLog(CEC_LOG_ERROR, "no device types set");
417 return false;
418 }
419
420 for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
421 {
422 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_RESERVED)
423 continue;
424
425 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - device %d: type %d", __FUNCTION__, iPtr, m_configuration.deviceTypes.types[iPtr]);
426
427 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_RECORDING_DEVICE)
428 bReturn &= FindLogicalAddressRecordingDevice();
429 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_TUNER)
430 bReturn &= FindLogicalAddressTuner();
431 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE)
432 bReturn &= FindLogicalAddressPlaybackDevice();
433 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
434 bReturn &= FindLogicalAddressAudioSystem();
435 }
436
437 if (bReturn)
438 SetAckMask(m_configuration.logicalAddresses.AckMask());
439
440 return bReturn;
441}
442
443void CCECProcessor::ReplaceHandlers(void)
444{
445 if (!IsInitialised())
446 return;
447 for (uint8_t iPtr = 0; iPtr <= CECDEVICE_PLAYBACKDEVICE3; iPtr++)
448 m_busDevices[iPtr]->ReplaceHandler(m_bInitialised);
449}
450
451bool CCECProcessor::OnCommandReceived(const cec_command &command)
452{
453 ParseCommand(command);
454 return true;
455}
456
457void *CCECProcessor::Process(void)
458{
459 CLibCEC::AddLog(CEC_LOG_DEBUG, "processor thread started");
460
461 while (!IsStopped() && m_communication->IsOpen())
462 {
463 if (IsInitialised())
464 {
465 ReplaceHandlers();
466
467 m_controller->CheckKeypressTimeout();
468 }
469 Sleep(5);
470 }
471
472 return NULL;
473}
474
475bool CCECProcessor::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RESERVED */)
476{
477 bool bReturn(false);
478
479 if (!IsRunning())
480 return bReturn;
481
482 cec_logical_address addr = m_configuration.logicalAddresses.primary;
483
484 if (type != CEC_DEVICE_TYPE_RESERVED)
485 {
486 for (uint8_t iPtr = 0; iPtr <= 11; iPtr++)
487 {
488 if (m_configuration.logicalAddresses[iPtr] && m_busDevices[iPtr]->m_type == type)
489 {
490 addr = (cec_logical_address) iPtr;
491 break;
492 }
493 }
494 }
495
496 m_busDevices[addr]->SetActiveSource();
497 if (m_busDevices[addr]->GetPhysicalAddress(false) != 0xFFFF)
498 bReturn = m_busDevices[addr]->ActivateSource();
499
500 return bReturn;
501}
502
503bool CCECProcessor::SetActiveSource(uint16_t iStreamPath)
504{
505 bool bReturn(false);
506
507 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamPath);
508 if (device)
509 {
510 device->SetActiveSource();
511 bReturn = true;
512 }
513
514 return bReturn;
515}
516
517void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout)
518{
519 CLockObject lock(m_mutex);
520 m_iStandardLineTimeout = iTimeout;
521}
522
523void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout)
524{
525 CLockObject lock(m_mutex);
526 m_iRetryLineTimeout = iTimeout;
527}
528
529bool CCECProcessor::SetActiveView(void)
530{
531 CLibCEC::AddLog(CEC_LOG_WARNING, "deprecated method %s called", __FUNCTION__);
532 return SetActiveSource(m_configuration.deviceTypes.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED : m_configuration.deviceTypes[0]);
533}
534
535bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode, bool bSendUpdate /* = true */)
536{
537 bool bReturn(false);
538
539 CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
540 if (device)
541 {
542 ((CCECPlaybackDevice *) device)->SetDeckControlMode(mode);
543 if (bSendUpdate)
544 ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV);
545 bReturn = true;
546 }
547
548 return bReturn;
549}
550
551bool CCECProcessor::SetDeckInfo(cec_deck_info info, bool bSendUpdate /* = true */)
552{
553 bool bReturn(false);
554
555 CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
556 if (device)
557 {
558 ((CCECPlaybackDevice *) device)->SetDeckStatus(info);
559 if (bSendUpdate)
560 ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV);
561 bReturn = true;
562 }
563
564 return bReturn;
565}
566
567bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice, uint8_t iPort, bool bForce /* = false */)
568{
569 bool bReturn(false);
570
571 {
572 CLockObject lock(m_mutex);
573 m_configuration.baseDevice = iBaseDevice;
574 m_configuration.iHDMIPort = iPort;
575 }
576
577 if (!IsRunning() && !bForce)
578 return true;
579
580 CLibCEC::AddLog(CEC_LOG_DEBUG, "setting HDMI port to %d on device %s (%d)", iPort, ToString(iBaseDevice), (int)iBaseDevice);
581
582 uint16_t iPhysicalAddress(0);
583 if (iBaseDevice > CECDEVICE_TV)
584 {
585 iPhysicalAddress = m_busDevices[iBaseDevice]->GetPhysicalAddress();
586 }
587
588 if (iPhysicalAddress < 0xffff)
589 {
590 if (iPhysicalAddress == 0)
591 iPhysicalAddress += 0x1000 * iPort;
592 else if (iPhysicalAddress % 0x1000 == 0)
593 iPhysicalAddress += 0x100 * iPort;
594 else if (iPhysicalAddress % 0x100 == 0)
595 iPhysicalAddress += 0x10 * iPort;
596 else if (iPhysicalAddress % 0x10 == 0)
597 iPhysicalAddress += iPort;
598
599 bReturn = true;
600 }
601
602 if (!bReturn)
603 CLibCEC::AddLog(CEC_LOG_ERROR, "failed to set the physical address");
604 else
605 SetPhysicalAddress(iPhysicalAddress);
606
607 return bReturn;
608}
609
610bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress)
611{
612 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
613 {
614 if (m_busDevices[iPtr]->GetPhysicalAddress(false) == iPhysicalAddress)
615 return true;
616 }
617 return false;
618}
619
620bool CCECProcessor::TransmitInactiveSource(void)
621{
622 if (!IsRunning())
623 return false;
624
625 if (!m_configuration.logicalAddresses.IsEmpty() && m_busDevices[m_configuration.logicalAddresses.primary])
626 return m_busDevices[m_configuration.logicalAddresses.primary]->TransmitInactiveSource();
627 return false;
628}
629
630void CCECProcessor::LogOutput(const cec_command &data)
631{
632 CStdString strTx;
633 strTx.Format("<< %02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination);
634 if (data.opcode_set)
635 strTx.AppendFormat(":%02x", (uint8_t)data.opcode);
636
637 for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++)
638 strTx.AppendFormat(":%02x", data.parameters[iPtr]);
639 CLibCEC::AddLog(CEC_LOG_TRAFFIC, strTx.c_str());
640}
641
642bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress)
643{
644 CLockObject lock(m_mutex);
645 if (m_configuration.logicalAddresses.primary != iLogicalAddress)
646 {
647 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< setting primary logical address to %1x", iLogicalAddress);
648 m_configuration.logicalAddresses.primary = iLogicalAddress;
649 m_configuration.logicalAddresses.Set(iLogicalAddress);
650 return SetAckMask(m_configuration.logicalAddresses.AckMask());
651 }
652
653 return true;
654}
655
656bool CCECProcessor::SetMenuState(cec_menu_state state, bool bSendUpdate /* = true */)
657{
658 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
659 {
660 if (m_configuration.logicalAddresses[iPtr])
661 m_busDevices[iPtr]->SetMenuState(state);
662 }
663
664 if (bSendUpdate)
665 m_busDevices[m_configuration.logicalAddresses.primary]->TransmitMenuState(CECDEVICE_TV);
666
667 return true;
668}
669
670bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress, bool bSendUpdate /* = true */)
671{
672 bool bSendActiveView(false);
673 bool bReturn(false);
674 cec_logical_addresses sendUpdatesTo;
675 sendUpdatesTo.Clear();
676
677 {
678 CLockObject lock(m_mutex);
679 m_configuration.iPhysicalAddress = iPhysicalAddress;
680 CLibCEC::AddLog(CEC_LOG_DEBUG, "setting physical address to '%4x'", iPhysicalAddress);
681
682 if (!m_configuration.logicalAddresses.IsEmpty())
683 {
684 bool bWasActiveSource(false);
685 for (uint8_t iPtr = 0; iPtr < 15; iPtr++)
686 if (m_configuration.logicalAddresses[iPtr])
687 {
688 bWasActiveSource |= m_busDevices[iPtr]->IsActiveSource();
689 m_busDevices[iPtr]->SetInactiveSource();
690 m_busDevices[iPtr]->SetPhysicalAddress(iPhysicalAddress);
691 if (bSendUpdate)
692 sendUpdatesTo.Set((cec_logical_address)iPtr);
693 }
694
695 bSendActiveView = bWasActiveSource && bSendUpdate;
696 bReturn = true;
697 }
698 }
699
700 for (uint8_t iPtr = 0; iPtr < 15; iPtr++)
701 if (sendUpdatesTo[iPtr])
702 m_busDevices[iPtr]->TransmitPhysicalAddress();
703
704 if (bSendActiveView)
705 SetActiveView();
706
707 if (bReturn)
708 CLibCEC::ConfigurationChanged(m_configuration);
709
710 return bReturn;
711}
712
713bool CCECProcessor::SwitchMonitoring(bool bEnable)
714{
715 CLibCEC::AddLog(CEC_LOG_NOTICE, "== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
716
717 {
718 CLockObject lock(m_mutex);
719 m_bMonitor = bEnable;
720 }
721
722 if (bEnable)
723 return SetAckMask(0);
724 else
725 return SetAckMask(m_configuration.logicalAddresses.AckMask());
726}
727
728bool CCECProcessor::PollDevice(cec_logical_address iAddress)
729{
730 if (iAddress != CECDEVICE_UNKNOWN && m_busDevices[iAddress])
731 {
732 return m_configuration.logicalAddresses.primary == CECDEVICE_UNKNOWN ?
733 m_busDevices[iAddress]->TransmitPoll(iAddress) :
734 m_busDevices[m_configuration.logicalAddresses.primary]->TransmitPoll(iAddress);
735 }
736 return false;
737}
738
739uint8_t CCECProcessor::VolumeUp(bool bSendRelease /* = true */)
740{
741 uint8_t status = 0;
742 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM))
743 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeUp(bSendRelease);
744
745 return status;
746}
747
748uint8_t CCECProcessor::VolumeDown(bool bSendRelease /* = true */)
749{
750 uint8_t status = 0;
751 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM))
752 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeDown(bSendRelease);
753
754 return status;
755}
756
757uint8_t CCECProcessor::MuteAudio(bool bSendRelease /* = true */)
758{
759 uint8_t status = 0;
760 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM))
761 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->MuteAudio(bSendRelease);
762
763 return status;
764}
765
766CCECBusDevice *CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bRefresh /* = false */) const
767{
768 if (m_busDevices[m_configuration.logicalAddresses.primary]->GetPhysicalAddress(false) == iPhysicalAddress)
769 return m_busDevices[m_configuration.logicalAddresses.primary];
770
771 CCECBusDevice *device = NULL;
772 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
773 {
774 if (m_busDevices[iPtr]->GetPhysicalAddress(bRefresh) == iPhysicalAddress)
775 {
776 device = m_busDevices[iPtr];
777 break;
778 }
779 }
780
781 return device;
782}
783
784CCECBusDevice *CCECProcessor::GetDeviceByType(cec_device_type type) const
785{
786 CCECBusDevice *device = NULL;
787
788 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
789 {
790 if (m_busDevices[iPtr]->m_type == type && m_configuration.logicalAddresses[iPtr])
791 {
792 device = m_busDevices[iPtr];
793 break;
794 }
795 }
796
797 return device;
798}
799
800CCECBusDevice *CCECProcessor::GetPrimaryDevice(void) const
801{
802 CCECBusDevice *device(NULL);
803 cec_logical_address primary = m_configuration.logicalAddresses.primary;
804 if (primary != CECDEVICE_UNKNOWN)
805 device = m_busDevices[primary];
806 return device;
807}
808
809cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress)
810{
811 return m_busDevices[iAddress]->GetCecVersion();
812}
813
814cec_osd_name CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress)
815{
816 CStdString strOSDName = m_busDevices[iAddress]->GetOSDName();
817 cec_osd_name retVal;
818
819 snprintf(retVal.name, sizeof(retVal.name), "%s", strOSDName.c_str());
820 retVal.device = iAddress;
821
822 return retVal;
823}
824
825bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
826{
827 if (m_busDevices[iAddress])
828 {
829 *language = m_busDevices[iAddress]->GetMenuLanguage();
830 return (strcmp(language->language, "???") != 0);
831 }
832 return false;
833}
834
835uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress)
836{
837 if (m_busDevices[iAddress])
838 return m_busDevices[iAddress]->GetVendorId();
839 return false;
840}
841
842uint16_t CCECProcessor::GetDevicePhysicalAddress(cec_logical_address iAddress)
843{
844 if (m_busDevices[iAddress])
845 return m_busDevices[iAddress]->GetPhysicalAddress(false);
846 return false;
847}
848
849cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress)
850{
851 if (m_busDevices[iAddress])
852 return m_busDevices[iAddress]->GetPowerStatus();
853 return CEC_POWER_STATUS_UNKNOWN;
854}
855
856cec_logical_address CCECProcessor::GetActiveSource(void)
857{
858 for (uint8_t iPtr = 0; iPtr <= 11; iPtr++)
859 {
860 if (m_busDevices[iPtr]->IsActiveSource())
861 return (cec_logical_address)iPtr;
862 }
863
864 return CECDEVICE_UNKNOWN;
865}
866
867bool CCECProcessor::IsActiveSource(cec_logical_address iAddress)
868{
869 return m_busDevices[iAddress]->IsActiveSource();
870}
871
872bool CCECProcessor::Transmit(const cec_command &data)
873{
874 if (m_configuration.logicalAddresses[(uint8_t)data.destination])
875 {
876 CLibCEC::AddLog(CEC_LOG_WARNING, "not sending data to myself!");
877 return false;
878 }
879
880 uint8_t iMaxTries(0);
881 {
882 CLockObject lock(m_mutex);
883 if (IsStopped())
884 return false;
885 LogOutput(data);
886 m_iLastTransmission = GetTimeMs();
887 if (!m_communication || !m_communication->IsOpen())
888 {
889 CLibCEC::AddLog(CEC_LOG_ERROR, "cannot transmit command: connection closed");
890 return false;
891 }
892 iMaxTries = m_busDevices[data.initiator]->GetHandler()->GetTransmitRetries() + 1;
893 }
894
895 return m_communication->Write(data, iMaxTries, m_iStandardLineTimeout, m_iRetryLineTimeout)
896 == ADAPTER_MESSAGE_STATE_SENT_ACKED;
897}
898
899void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, cec_abort_reason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
900{
901 CLibCEC::AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
902
903 cec_command command;
904 // TODO
905 cec_command::Format(command, m_configuration.logicalAddresses.primary, address, CEC_OPCODE_FEATURE_ABORT);
906 command.parameters.PushBack((uint8_t)opcode);
907 command.parameters.PushBack((uint8_t)reason);
908
909 Transmit(command);
910}
911
912void CCECProcessor::ParseCommand(const cec_command &command)
913{
914 CStdString dataStr;
915 dataStr.Format(">> %1x%1x", command.initiator, command.destination);
916 if (command.opcode_set == 1)
917 dataStr.AppendFormat(":%02x", command.opcode);
918 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
919 dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
920 CLibCEC::AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
921
922 if (!m_bMonitor && command.initiator >= CECDEVICE_TV && command.initiator <= CECDEVICE_BROADCAST)
923 m_busDevices[(uint8_t)command.initiator]->HandleCommand(command);
924}
925
926cec_logical_addresses CCECProcessor::GetActiveDevices(void)
927{
928 cec_logical_addresses addresses;
929 addresses.Clear();
930 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
931 {
932 if (m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
933 addresses.Set((cec_logical_address) iPtr);
934 }
935 return addresses;
936}
937
938bool CCECProcessor::IsPresentDevice(cec_logical_address address)
939{
940 return m_busDevices[address]->GetStatus() == CEC_DEVICE_STATUS_PRESENT;
941}
942
943bool CCECProcessor::IsPresentDeviceType(cec_device_type type)
944{
945 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
946 {
947 if (m_busDevices[iPtr]->GetType() == type && m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
948 return true;
949 }
950
951 return false;
952}
953
954uint16_t CCECProcessor::GetPhysicalAddress(void) const
955{
956 if (!m_configuration.logicalAddresses.IsEmpty() && m_busDevices[m_configuration.logicalAddresses.primary])
957 return m_busDevices[m_configuration.logicalAddresses.primary]->GetPhysicalAddress(false);
958 return false;
959}
960
961bool CCECProcessor::SetAckMask(uint16_t iMask)
962{
963 return m_communication->SetAckMask(iMask);
964}
965
966bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
967{
968 return m_busDevices[iDestination]->TransmitKeypress(key, bWait);
969}
970
971bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination, bool bWait /* = true */)
972{
973 return m_busDevices[iDestination]->TransmitKeyRelease(bWait);
974}
975
976bool CCECProcessor::EnablePhysicalAddressDetection(void)
977{
978 CLibCEC::AddLog(CEC_LOG_WARNING, "deprecated method %s called", __FUNCTION__);
979 uint16_t iPhysicalAddress = m_communication->GetPhysicalAddress();
980 if (iPhysicalAddress != 0)
981 {
982 m_configuration.bAutodetectAddress = 1;
983 m_configuration.iPhysicalAddress = iPhysicalAddress;
984 m_configuration.baseDevice = CECDEVICE_UNKNOWN;
985 m_configuration.iHDMIPort = 0;
986 return SetPhysicalAddress(iPhysicalAddress);
987 }
988 return false;
989}
990
991bool CCECProcessor::StandbyDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
992{
993 if (address == CECDEVICE_BROADCAST && m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_0)
994 {
995 bool bReturn(true);
996 for (uint8_t iPtr = 0; iPtr <= 0xF; iPtr++)
997 {
998 if (m_configuration.powerOffDevices[iPtr])
999 bReturn &= m_busDevices[iPtr]->Standby();
1000 }
1001 return bReturn;
1002 }
1003
1004 return m_busDevices[address]->Standby();
1005}
1006
1007bool CCECProcessor::PowerOnDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
1008{
1009 if (address == CECDEVICE_BROADCAST && m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_0)
1010 {
1011 bool bReturn(true);
1012 for (uint8_t iPtr = 0; iPtr <= 0xF; iPtr++)
1013 {
1014 if (m_configuration.powerOffDevices[iPtr])
1015 bReturn &= m_busDevices[iPtr]->PowerOn();
1016 }
1017 return bReturn;
1018 }
1019
1020 return m_busDevices[address]->PowerOn();
1021}
1022
1023const char *CCECProcessor::ToString(const cec_device_type type)
1024{
1025 switch (type)
1026 {
1027 case CEC_DEVICE_TYPE_AUDIO_SYSTEM:
1028 return "audio system";
1029 case CEC_DEVICE_TYPE_PLAYBACK_DEVICE:
1030 return "playback device";
1031 case CEC_DEVICE_TYPE_RECORDING_DEVICE:
1032 return "recording device";
1033 case CEC_DEVICE_TYPE_RESERVED:
1034 return "reserved";
1035 case CEC_DEVICE_TYPE_TUNER:
1036 return "tuner";
1037 case CEC_DEVICE_TYPE_TV:
1038 return "TV";
1039 default:
1040 return "unknown";
1041 }
1042}
1043
1044const char *CCECProcessor::ToString(const cec_menu_state state)
1045{
1046 switch (state)
1047 {
1048 case CEC_MENU_STATE_ACTIVATED:
1049 return "activated";
1050 case CEC_MENU_STATE_DEACTIVATED:
1051 return "deactivated";
1052 default:
1053 return "unknown";
1054 }
1055}
1056
1057const char *CCECProcessor::ToString(const cec_version version)
1058{
1059 switch (version)
1060 {
1061 case CEC_VERSION_1_2:
1062 return "1.2";
1063 case CEC_VERSION_1_2A:
1064 return "1.2a";
1065 case CEC_VERSION_1_3:
1066 return "1.3";
1067 case CEC_VERSION_1_3A:
1068 return "1.3a";
1069 case CEC_VERSION_1_4:
1070 return "1.4";
1071 default:
1072 return "unknown";
1073 }
1074}
1075
1076const char *CCECProcessor::ToString(const cec_power_status status)
1077{
1078 switch (status)
1079 {
1080 case CEC_POWER_STATUS_ON:
1081 return "on";
1082 case CEC_POWER_STATUS_STANDBY:
1083 return "standby";
1084 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY:
1085 return "in transition from on to standby";
1086 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON:
1087 return "in transition from standby to on";
1088 default:
1089 return "unknown";
1090 }
1091}
1092
1093const char *CCECProcessor::ToString(const cec_logical_address address)
1094{
1095 switch(address)
1096 {
1097 case CECDEVICE_AUDIOSYSTEM:
1098 return "Audio";
1099 case CECDEVICE_BROADCAST:
1100 return "Broadcast";
1101 case CECDEVICE_FREEUSE:
1102 return "Free use";
1103 case CECDEVICE_PLAYBACKDEVICE1:
1104 return "Playback 1";
1105 case CECDEVICE_PLAYBACKDEVICE2:
1106 return "Playback 2";
1107 case CECDEVICE_PLAYBACKDEVICE3:
1108 return "Playback 3";
1109 case CECDEVICE_RECORDINGDEVICE1:
1110 return "Recorder 1";
1111 case CECDEVICE_RECORDINGDEVICE2:
1112 return "Recorder 2";
1113 case CECDEVICE_RECORDINGDEVICE3:
1114 return "Recorder 3";
1115 case CECDEVICE_RESERVED1:
1116 return "Reserved 1";
1117 case CECDEVICE_RESERVED2:
1118 return "Reserved 2";
1119 case CECDEVICE_TUNER1:
1120 return "Tuner 1";
1121 case CECDEVICE_TUNER2:
1122 return "Tuner 2";
1123 case CECDEVICE_TUNER3:
1124 return "Tuner 3";
1125 case CECDEVICE_TUNER4:
1126 return "Tuner 4";
1127 case CECDEVICE_TV:
1128 return "TV";
1129 default:
1130 return "unknown";
1131 }
1132}
1133
1134const char *CCECProcessor::ToString(const cec_deck_control_mode mode)
1135{
1136 switch (mode)
1137 {
1138 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND:
1139 return "skip forward wind";
1140 case CEC_DECK_CONTROL_MODE_EJECT:
1141 return "eject";
1142 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND:
1143 return "reverse rewind";
1144 case CEC_DECK_CONTROL_MODE_STOP:
1145 return "stop";
1146 default:
1147 return "unknown";
1148 }
1149}
1150
1151const char *CCECProcessor::ToString(const cec_deck_info status)
1152{
1153 switch (status)
1154 {
1155 case CEC_DECK_INFO_PLAY:
1156 return "play";
1157 case CEC_DECK_INFO_RECORD:
1158 return "record";
1159 case CEC_DECK_INFO_PLAY_REVERSE:
1160 return "play reverse";
1161 case CEC_DECK_INFO_STILL:
1162 return "still";
1163 case CEC_DECK_INFO_SLOW:
1164 return "slow";
1165 case CEC_DECK_INFO_SLOW_REVERSE:
1166 return "slow reverse";
1167 case CEC_DECK_INFO_FAST_FORWARD:
1168 return "fast forward";
1169 case CEC_DECK_INFO_FAST_REVERSE:
1170 return "fast reverse";
1171 case CEC_DECK_INFO_NO_MEDIA:
1172 return "no media";
1173 case CEC_DECK_INFO_STOP:
1174 return "stop";
1175 case CEC_DECK_INFO_SKIP_FORWARD_WIND:
1176 return "info skip forward wind";
1177 case CEC_DECK_INFO_SKIP_REVERSE_REWIND:
1178 return "info skip reverse rewind";
1179 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD:
1180 return "info index search forward";
1181 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE:
1182 return "info index search reverse";
1183 case CEC_DECK_INFO_OTHER_STATUS:
1184 return "other";
1185 default:
1186 return "unknown";
1187 }
1188}
1189
1190const char *CCECProcessor::ToString(const cec_opcode opcode)
1191{
1192 switch (opcode)
1193 {
1194 case CEC_OPCODE_ACTIVE_SOURCE:
1195 return "active source";
1196 case CEC_OPCODE_IMAGE_VIEW_ON:
1197 return "image view on";
1198 case CEC_OPCODE_TEXT_VIEW_ON:
1199 return "text view on";
1200 case CEC_OPCODE_INACTIVE_SOURCE:
1201 return "inactive source";
1202 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
1203 return "request active source";
1204 case CEC_OPCODE_ROUTING_CHANGE:
1205 return "routing change";
1206 case CEC_OPCODE_ROUTING_INFORMATION:
1207 return "routing information";
1208 case CEC_OPCODE_SET_STREAM_PATH:
1209 return "set stream path";
1210 case CEC_OPCODE_STANDBY:
1211 return "standby";
1212 case CEC_OPCODE_RECORD_OFF:
1213 return "record off";
1214 case CEC_OPCODE_RECORD_ON:
1215 return "record on";
1216 case CEC_OPCODE_RECORD_STATUS:
1217 return "record status";
1218 case CEC_OPCODE_RECORD_TV_SCREEN:
1219 return "record tv screen";
1220 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER:
1221 return "clear analogue timer";
1222 case CEC_OPCODE_CLEAR_DIGITAL_TIMER:
1223 return "clear digital timer";
1224 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER:
1225 return "clear external timer";
1226 case CEC_OPCODE_SET_ANALOGUE_TIMER:
1227 return "set analogue timer";
1228 case CEC_OPCODE_SET_DIGITAL_TIMER:
1229 return "set digital timer";
1230 case CEC_OPCODE_SET_EXTERNAL_TIMER:
1231 return "set external timer";
1232 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE:
1233 return "set timer program title";
1234 case CEC_OPCODE_TIMER_CLEARED_STATUS:
1235 return "timer cleared status";
1236 case CEC_OPCODE_TIMER_STATUS:
1237 return "timer status";
1238 case CEC_OPCODE_CEC_VERSION:
1239 return "cec version";
1240 case CEC_OPCODE_GET_CEC_VERSION:
1241 return "get cec version";
1242 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
1243 return "give physical address";
1244 case CEC_OPCODE_GET_MENU_LANGUAGE:
1245 return "get menu language";
1246 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS:
1247 return "report physical address";
1248 case CEC_OPCODE_SET_MENU_LANGUAGE:
1249 return "set menu language";
1250 case CEC_OPCODE_DECK_CONTROL:
1251 return "deck control";
1252 case CEC_OPCODE_DECK_STATUS:
1253 return "deck status";
1254 case CEC_OPCODE_GIVE_DECK_STATUS:
1255 return "give deck status";
1256 case CEC_OPCODE_PLAY:
1257 return "play";
1258 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS:
1259 return "give tuner status";
1260 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE:
1261 return "select analogue service";
1262 case CEC_OPCODE_SELECT_DIGITAL_SERVICE:
1263 return "set digital service";
1264 case CEC_OPCODE_TUNER_DEVICE_STATUS:
1265 return "tuner device status";
1266 case CEC_OPCODE_TUNER_STEP_DECREMENT:
1267 return "tuner step decrement";
1268 case CEC_OPCODE_TUNER_STEP_INCREMENT:
1269 return "tuner step increment";
1270 case CEC_OPCODE_DEVICE_VENDOR_ID:
1271 return "device vendor id";
1272 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
1273 return "give device vendor id";
1274 case CEC_OPCODE_VENDOR_COMMAND:
1275 return "vendor command";
1276 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
1277 return "vendor command with id";
1278 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN:
1279 return "vendor remote button down";
1280 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP:
1281 return "vendor remote button up";
1282 case CEC_OPCODE_SET_OSD_STRING:
1283 return "set osd string";
1284 case CEC_OPCODE_GIVE_OSD_NAME:
1285 return "give osd name";
1286 case CEC_OPCODE_SET_OSD_NAME:
1287 return "set osd name";
1288 case CEC_OPCODE_MENU_REQUEST:
1289 return "menu request";
1290 case CEC_OPCODE_MENU_STATUS:
1291 return "menu status";
1292 case CEC_OPCODE_USER_CONTROL_PRESSED:
1293 return "user control pressed";
1294 case CEC_OPCODE_USER_CONTROL_RELEASE:
1295 return "user control release";
1296 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
1297 return "give device power status";
1298 case CEC_OPCODE_REPORT_POWER_STATUS:
1299 return "report power status";
1300 case CEC_OPCODE_FEATURE_ABORT:
1301 return "feature abort";
1302 case CEC_OPCODE_ABORT:
1303 return "abort";
1304 case CEC_OPCODE_GIVE_AUDIO_STATUS:
1305 return "give audio status";
1306 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS:
1307 return "give audio mode status";
1308 case CEC_OPCODE_REPORT_AUDIO_STATUS:
1309 return "report audio status";
1310 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE:
1311 return "set system audio mode";
1312 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST:
1313 return "system audio mode request";
1314 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS:
1315 return "system audio mode status";
1316 case CEC_OPCODE_SET_AUDIO_RATE:
1317 return "set audio rate";
1318 case CEC_OPCODE_NONE:
1319 return "poll";
1320 default:
1321 return "UNKNOWN";
1322 }
1323}
1324
1325const char *CCECProcessor::ToString(const cec_system_audio_status mode)
1326{
1327 switch(mode)
1328 {
1329 case CEC_SYSTEM_AUDIO_STATUS_ON:
1330 return "on";
1331 case CEC_SYSTEM_AUDIO_STATUS_OFF:
1332 return "off";
1333 default:
1334 return "unknown";
1335 }
1336}
1337
1338const char *CCECProcessor::ToString(const cec_audio_status UNUSED(status))
1339{
1340 // TODO this is a mask
1341 return "TODO";
1342}
1343
1344const char *CCECProcessor::ToString(const cec_vendor_id vendor)
1345{
1346 switch (vendor)
1347 {
1348 case CEC_VENDOR_SAMSUNG:
1349 return "Samsung";
1350 case CEC_VENDOR_LG:
1351 return "LG";
1352 case CEC_VENDOR_PANASONIC:
1353 return "Panasonic";
1354 case CEC_VENDOR_PIONEER:
1355 return "Pioneer";
1356 case CEC_VENDOR_ONKYO:
1357 return "Onkyo";
1358 case CEC_VENDOR_YAMAHA:
1359 return "Yamaha";
1360 case CEC_VENDOR_PHILIPS:
1361 return "Philips";
1362 case CEC_VENDOR_SONY:
1363 return "Sony";
1364 case CEC_VENDOR_TOSHIBA:
1365 return "Toshiba";
1366 default:
1367 return "Unknown";
1368 }
1369}
1370
1371const char *CCECProcessor::ToString(const cec_client_version version)
1372{
1373 switch (version)
1374 {
1375 case CEC_CLIENT_VERSION_PRE_1_5:
1376 return "pre-1.5";
1377 case CEC_CLIENT_VERSION_1_5_0:
1378 return "1.5.0";
1379 case CEC_CLIENT_VERSION_1_5_1:
1380 return "1.5.1";
1381 case CEC_CLIENT_VERSION_1_5_2:
1382 return "1.5.2";
1383 default:
1384 return "Unknown";
1385 }
1386}
1387
1388const char *CCECProcessor::ToString(const cec_server_version version)
1389{
1390 switch (version)
1391 {
1392 case CEC_SERVER_VERSION_PRE_1_5:
1393 return "pre-1.5";
1394 case CEC_SERVER_VERSION_1_5_0:
1395 return "1.5.0";
1396 case CEC_SERVER_VERSION_1_5_1:
1397 return "1.5.1";
1398 case CEC_SERVER_VERSION_1_5_2:
1399 return "1.5.2";
1400 default:
1401 return "Unknown";
1402 }
1403}
1404
1405void *CCECBusScan::Process(void)
1406{
1407 CCECBusDevice *device(NULL);
1408 uint8_t iCounter(0);
1409
1410 while (!IsStopped())
1411 {
1412 if (++iCounter < 10)
1413 {
1414 Sleep(1000);
1415 continue;
1416 }
1417 for (unsigned int iPtr = 0; iPtr <= 11 && !IsStopped(); iPtr++)
1418 {
1419 device = m_processor->m_busDevices[iPtr];
1420 WaitUntilIdle();
1421 if (device && device->GetStatus(true) == CEC_DEVICE_STATUS_PRESENT)
1422 {
1423 WaitUntilIdle();
1424 if (!IsStopped())
1425 device->GetVendorId();
1426
1427 WaitUntilIdle();
1428 if (!IsStopped())
1429 device->GetPowerStatus(true);
1430 }
1431 }
1432 }
1433
1434 return NULL;
1435}
1436
1437void CCECBusScan::WaitUntilIdle(void)
1438{
1439 if (IsStopped())
1440 return;
1441
1442 int32_t iWaitTime = 3000 - (int32_t)(GetTimeMs() - m_processor->GetLastTransmission());
1443 while (iWaitTime > 0)
1444 {
1445 Sleep(iWaitTime);
1446 iWaitTime = 3000 - (int32_t)(GetTimeMs() - m_processor->GetLastTransmission());
1447 }
1448}
1449
1450bool CCECProcessor::StartBootloader(const char *strPort /* = NULL */)
1451{
1452 if (!m_communication && strPort)
1453 {
1454 bool bReturn(false);
1455 IAdapterCommunication *comm = new CUSBCECAdapterCommunication(this, strPort);
1456 CTimeout timeout(10000);
1457 int iConnectTry(0);
1458 while (timeout.TimeLeft() > 0 && (bReturn = comm->Open(NULL, (timeout.TimeLeft() / CEC_CONNECT_TRIES)), true) == false)
1459 {
1460 CLibCEC::AddLog(CEC_LOG_ERROR, "could not open a connection (try %d)", ++iConnectTry);
1461 comm->Close();
1462 Sleep(500);
1463 }
1464 if (comm->IsOpen())
1465 {
1466 bReturn = comm->StartBootloader();
1467 delete comm;
1468 }
1469 return bReturn;
1470 }
1471 else
1472 {
1473 return m_communication->StartBootloader();
1474 }
1475}
1476
1477bool CCECProcessor::PingAdapter(void)
1478{
1479 return m_communication->PingAdapter();
1480}
1481
1482void CCECProcessor::HandlePoll(cec_logical_address initiator, cec_logical_address destination)
1483{
1484 m_busDevices[destination]->HandlePoll(initiator);
1485}
1486
1487bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator)
1488{
1489 return !m_busDevices[initiator]->HandleReceiveFailed();
1490}
1491
1492bool CCECProcessor::SetStreamPath(uint16_t iPhysicalAddress)
1493{
1494 // stream path changes are sent by the TV
1495 return m_busDevices[CECDEVICE_TV]->GetHandler()->TransmitSetStreamPath(iPhysicalAddress);
1496}
1497
1498bool CCECProcessor::SetConfiguration(const libcec_configuration *configuration)
1499{
1500 bool bReinit(false);
1501 CCECBusDevice *primary = IsRunning() ? GetPrimaryDevice() : NULL;
1502 cec_device_type oldPrimaryType = primary ? primary->GetType() : CEC_DEVICE_TYPE_RECORDING_DEVICE;
1503 m_configuration.clientVersion = configuration->clientVersion;
1504
1505 // client version 1.5.0
1506
1507 // device types
1508 bool bDeviceTypeChanged = IsRunning () && m_configuration.deviceTypes != configuration->deviceTypes;
1509 m_configuration.deviceTypes = configuration->deviceTypes;
1510
1511 bool bPhysicalAddressChanged(false);
1512
1513 // autodetect address
1514 bool bPhysicalAutodetected(false);
1515 if (IsRunning() && configuration->bAutodetectAddress == 1)
1516 {
1517 uint16_t iPhysicalAddress = m_communication->GetPhysicalAddress();
1518 if (iPhysicalAddress != 0)
1519 {
1520 if (IsRunning())
1521 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - autodetected physical address '%4x'", __FUNCTION__, iPhysicalAddress);
1522 bPhysicalAddressChanged = (m_configuration.iPhysicalAddress != iPhysicalAddress);
1523 m_configuration.iPhysicalAddress = iPhysicalAddress;
1524 m_configuration.iHDMIPort = 0;
1525 m_configuration.baseDevice = CECDEVICE_UNKNOWN;
1526 bPhysicalAutodetected = true;
1527 }
1528 }
1529
1530 // physical address
1531 if (!bPhysicalAutodetected)
1532 {
1533 if (configuration->iPhysicalAddress != 0)
1534 bPhysicalAddressChanged = IsRunning() && m_configuration.iPhysicalAddress != configuration->iPhysicalAddress;
1535 if (IsRunning())
1536 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - using physical address '%4x'", __FUNCTION__, configuration->iPhysicalAddress);
1537 m_configuration.iPhysicalAddress = configuration->iPhysicalAddress;
1538 }
1539
1540 bool bHdmiPortChanged(false);
1541 if (!bPhysicalAutodetected && !bPhysicalAddressChanged)
1542 {
1543 // base device
1544 bHdmiPortChanged = IsRunning() && m_configuration.baseDevice != configuration->baseDevice;
1545 if (IsRunning())
1546 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - using base device '%x'", __FUNCTION__, (int)configuration->baseDevice);
1547 m_configuration.baseDevice = configuration->baseDevice;
1548
1549 // hdmi port
1550 bHdmiPortChanged |= IsRunning() && m_configuration.iHDMIPort != configuration->iHDMIPort;
1551 if (IsRunning())
1552 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - using HDMI port '%d'", __FUNCTION__, configuration->iHDMIPort);
1553 m_configuration.iHDMIPort = configuration->iHDMIPort;
1554 }
1555 else
1556 {
1557 if (IsRunning())
1558 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - resetting HDMI port and base device to defaults", __FUNCTION__);
1559 m_configuration.baseDevice = CECDEVICE_UNKNOWN;
1560 m_configuration.iHDMIPort = 0;
1561 }
1562
1563 bReinit = bPhysicalAddressChanged || bHdmiPortChanged || bDeviceTypeChanged;
1564
1565 // device name
1566 snprintf(m_configuration.strDeviceName, 13, "%s", configuration->strDeviceName);
1567 if (primary && !primary->GetOSDName().Equals(m_configuration.strDeviceName))
1568 {
1569 primary->SetOSDName(m_configuration.strDeviceName);
1570 if (!bReinit && IsRunning())
1571 primary->TransmitOSDName(CECDEVICE_TV);
1572 }
1573
1574 // tv vendor id override
1575 if (m_configuration.tvVendor != configuration->tvVendor)
1576 {
1577 m_configuration.tvVendor= configuration->tvVendor;
1578 m_busDevices[CECDEVICE_TV]->SetVendorId((uint64_t)m_configuration.tvVendor);
1579 }
1580
1581 // wake CEC devices
1582 if (m_configuration.wakeDevices != configuration->wakeDevices)
1583 {
1584 m_configuration.wakeDevices = configuration->wakeDevices;
1585 if (!bReinit && IsRunning())
1586 PowerOnDevices();
1587 }
1588
1589 // just copy these
1590 m_configuration.clientVersion = configuration->clientVersion;
1591 m_configuration.bActivateSource = configuration->bActivateSource;
1592 m_configuration.bGetSettingsFromROM = configuration->bGetSettingsFromROM;
1593 m_configuration.powerOffDevices = configuration->powerOffDevices;
1594 m_configuration.bPowerOffScreensaver = configuration->bPowerOffScreensaver;
1595 m_configuration.bPowerOffOnStandby = configuration->bPowerOffOnStandby;
1596
1597 // client version 1.5.1
1598 if (configuration->clientVersion >= CEC_CLIENT_VERSION_1_5_1)
1599 m_configuration.bSendInactiveSource = configuration->bSendInactiveSource;
1600
1601 // ensure that there is at least 1 device type set
1602 if (m_configuration.deviceTypes.IsEmpty())
1603 m_configuration.deviceTypes.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
1604
1605 if (bReinit)
1606 {
1607 if (bDeviceTypeChanged)
1608 return ChangeDeviceType(oldPrimaryType, m_configuration.deviceTypes[0]);
1609 else if (bPhysicalAddressChanged)
1610 return SetPhysicalAddress(m_configuration.iPhysicalAddress);
1611 else
1612 return SetHDMIPort(m_configuration.baseDevice, m_configuration.iHDMIPort);
1613 }
1614 else if (m_configuration.bActivateSource == 1 && IsRunning() && !IsActiveSource(m_configuration.logicalAddresses.primary))
1615 {
1616 // activate the source if we're not already the active source
1617 SetActiveSource(m_configuration.deviceTypes.types[0]);
1618 }
1619
1620 return true;
1621}
1622
1623bool CCECProcessor::GetCurrentConfiguration(libcec_configuration *configuration)
1624{
1625 // client version 1.5.0
1626 snprintf(configuration->strDeviceName, 13, "%s", m_configuration.strDeviceName);
1627 configuration->deviceTypes = m_configuration.deviceTypes;
1628 configuration->bAutodetectAddress = m_configuration.bAutodetectAddress;
1629 configuration->iPhysicalAddress = m_configuration.iPhysicalAddress;
1630 configuration->baseDevice = m_configuration.baseDevice;
1631 configuration->iHDMIPort = m_configuration.iHDMIPort;
1632 configuration->clientVersion = m_configuration.clientVersion;
1633 configuration->serverVersion = m_configuration.serverVersion;
1634 configuration->tvVendor = m_configuration.tvVendor;
1635
1636 configuration->bGetSettingsFromROM = m_configuration.bGetSettingsFromROM;
1637 configuration->bUseTVMenuLanguage = m_configuration.bUseTVMenuLanguage;
1638 configuration->bActivateSource = m_configuration.bActivateSource;
1639 configuration->wakeDevices = m_configuration.wakeDevices;
1640 configuration->powerOffDevices = m_configuration.powerOffDevices;
1641 configuration->bPowerOffScreensaver = m_configuration.bPowerOffScreensaver;
1642 configuration->bPowerOffOnStandby = m_configuration.bPowerOffOnStandby;
1643
1644 // client version 1.5.1
1645 if (configuration->clientVersion >= CEC_CLIENT_VERSION_1_5_1)
1646 configuration->bSendInactiveSource = m_configuration.bSendInactiveSource;
1647
1648 // client version 1.5.3
1649 if (configuration->clientVersion >= CEC_CLIENT_VERSION_1_5_3)
1650 configuration->logicalAddresses = m_configuration.logicalAddresses;
1651
1652 return true;
1653}
1654
1655bool CCECProcessor::CanPersistConfiguration(void)
1656{
1657 return m_communication->GetFirmwareVersion() >= 2;
1658}
1659
1660bool CCECProcessor::PersistConfiguration(libcec_configuration *configuration)
1661{
1662 return m_communication->PersistConfiguration(configuration);
1663}
1664
1665void CCECProcessor::RescanActiveDevices(void)
1666{
1667 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
1668 m_busDevices[iPtr]->GetStatus(true);
1669}