cec: don't forward the standby opcode more than once every 10 seconds
[deb_libcec.git] / src / lib / CECClient.cpp
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 "CECClient.h"
34 #include "CECProcessor.h"
35 #include "LibCEC.h"
36 #include "CECTypeUtils.h"
37 #include "devices/CECPlaybackDevice.h"
38 #include "devices/CECAudioSystem.h"
39 #include "devices/CECTV.h"
40 #include "implementations/CECCommandHandler.h"
41
42 using namespace CEC;
43 using namespace PLATFORM;
44
45 #define LIB_CEC m_processor->GetLib()
46 #define ToString(x) CCECTypeUtils::ToString(x)
47
48 CCECClient::CCECClient(CCECProcessor *processor, const libcec_configuration &configuration) :
49 m_processor(processor),
50 m_bInitialised(false),
51 m_bRegistered(false),
52 m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN),
53 m_buttontime(0),
54 m_iPreventForwardingPowerOffCommand(0)
55 {
56 // set the initial configuration
57 SetConfiguration(configuration);
58 }
59
60 CCECClient::~CCECClient(void)
61 {
62 // unregister the client
63 if (m_processor && IsRegistered())
64 m_processor->UnregisterClient(this);
65 }
66
67 bool CCECClient::IsInitialised(void)
68 {
69 CLockObject lock(m_mutex);
70 return m_bInitialised && m_processor;
71 }
72
73 void CCECClient::SetInitialised(bool bSetTo)
74 {
75 CLockObject lock(m_mutex);
76 m_bInitialised = bSetTo;
77 }
78
79 bool CCECClient::IsRegistered(void)
80 {
81 CLockObject lock(m_mutex);
82 return m_bRegistered && m_processor;
83 }
84
85 void CCECClient::SetRegistered(bool bSetTo)
86 {
87 CLockObject lock(m_mutex);
88 m_bRegistered = bSetTo;
89 }
90
91 bool CCECClient::OnRegister(void)
92 {
93 // return false if already initialised
94 if (IsInitialised())
95 return true;
96
97 // get all device we control
98 CECDEVICEVEC devices;
99 m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses);
100
101 // return false when no devices were found
102 if (devices.empty())
103 {
104 LIB_CEC->AddLog(CEC_LOG_WARNING, "cannot find the primary device (logical address %x)", GetPrimaryLogicalAdddress());
105 return false;
106 }
107
108 // mark as initialised
109 SetInitialised(true);
110
111 // configure all devices
112 for (CECDEVICEVEC::iterator it = devices.begin(); it != devices.end(); it++)
113 {
114 // only set our OSD name for the primary device
115 if ((*it)->GetLogicalAddress() == GetPrimaryLogicalAdddress())
116 (*it)->SetOSDName(m_configuration.strDeviceName);
117
118 // set the default menu language for devices we control
119 (*it)->SetMenuLanguage(m_configuration.strDeviceLanguage);
120 }
121
122 // set the physical address
123 SetPhysicalAddress(m_configuration);
124
125 // make the primary device the active source if the option is set
126 if (m_configuration.bActivateSource == 1)
127 GetPrimaryDevice()->ActivateSource();
128
129 return true;
130 }
131
132 bool CCECClient::SetHDMIPort(const cec_logical_address iBaseDevice, const uint8_t iPort, bool bForce /* = false */)
133 {
134 bool bReturn(false);
135
136 // limit the HDMI port range to 1-15
137 if (iPort < CEC_MIN_HDMI_PORTNUMBER ||
138 iPort > CEC_MAX_HDMI_PORTNUMBER)
139 return bReturn;
140
141 LIB_CEC->AddLog(CEC_LOG_DEBUG, "setting HDMI port to %d on device %s (%d)", iPort, ToString(iBaseDevice), (int)iBaseDevice);
142
143 // update the configuration
144 {
145 CLockObject lock(m_mutex);
146 m_configuration.baseDevice = iBaseDevice;
147 m_configuration.iHDMIPort = iPort;
148 }
149
150 // don't continue if the connection isn't opened
151 if (!m_processor->CECInitialised() && !bForce)
152 return true;
153
154 // get the PA of the base device
155 uint16_t iPhysicalAddress(CEC_INVALID_PHYSICAL_ADDRESS);
156 CCECBusDevice *baseDevice = m_processor->GetDevice(iBaseDevice);
157 if (baseDevice)
158 iPhysicalAddress = baseDevice->GetPhysicalAddress(GetPrimaryLogicalAdddress());
159
160 // add our port number
161 if (iPhysicalAddress <= CEC_MAX_PHYSICAL_ADDRESS)
162 {
163 if (iPhysicalAddress == 0)
164 iPhysicalAddress += 0x1000 * iPort;
165 else if (iPhysicalAddress % 0x1000 == 0)
166 iPhysicalAddress += 0x100 * iPort;
167 else if (iPhysicalAddress % 0x100 == 0)
168 iPhysicalAddress += 0x10 * iPort;
169 else if (iPhysicalAddress % 0x10 == 0)
170 iPhysicalAddress += iPort;
171
172 bReturn = true;
173 }
174
175 // set the default address when something went wrong
176 if (!bReturn)
177 {
178 LIB_CEC->AddLog(CEC_LOG_WARNING, "failed to set the physical address to %04X, setting it to the default value %04X", iPhysicalAddress, CEC_DEFAULT_PHYSICAL_ADDRESS);
179 iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS;
180 }
181
182 // and set the address
183 SetDevicePhysicalAddress(iPhysicalAddress);
184
185 ConfigurationChanged(m_configuration);
186
187 return bReturn;
188 }
189
190 void CCECClient::ResetPhysicalAddress(void)
191 {
192 SetPhysicalAddress(m_configuration);
193 }
194
195 void CCECClient::SetPhysicalAddress(const libcec_configuration &configuration)
196 {
197 bool bPASet(false);
198
199 // override the physical address from configuration.iPhysicalAddress if it's set
200 if (!bPASet && CLibCEC::IsValidPhysicalAddress(configuration.iPhysicalAddress))
201 bPASet = SetPhysicalAddress(configuration.iPhysicalAddress);
202
203 // try to autodetect the address
204 if (!bPASet && m_processor->CECInitialised())
205 {
206 bPASet = AutodetectPhysicalAddress();
207 m_configuration.bAutodetectAddress = bPASet ? 1 : 0;
208 }
209
210 // use the base device + hdmi port settings
211 if (!bPASet)
212 bPASet = SetHDMIPort(configuration.baseDevice, configuration.iHDMIPort);
213
214 // reset to defaults if something went wrong
215 if (!bPASet)
216 {
217 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - resetting HDMI port and base device to defaults", __FUNCTION__);
218 m_configuration.baseDevice = CECDEVICE_UNKNOWN;
219 m_configuration.iHDMIPort = CEC_HDMI_PORTNUMBER_NONE;
220 }
221 }
222
223 bool CCECClient::SetPhysicalAddress(const uint16_t iPhysicalAddress)
224 {
225 // update the configuration
226 {
227 CLockObject lock(m_mutex);
228 if (m_configuration.iPhysicalAddress == iPhysicalAddress)
229 {
230 LIB_CEC->AddLog(CEC_LOG_DEBUG, "physical address unchanged (%04X)", iPhysicalAddress);
231 return true;
232 }
233 else
234 {
235 m_configuration.iPhysicalAddress = iPhysicalAddress;
236 LIB_CEC->AddLog(CEC_LOG_DEBUG, "setting physical address to '%04X'", iPhysicalAddress);
237 }
238 }
239
240 // persist the new configuration
241 m_processor->PersistConfiguration(m_configuration);
242
243 // set the physical address for each device
244 SetDevicePhysicalAddress(iPhysicalAddress);
245
246 // and send back the updated configuration
247 ConfigurationChanged(m_configuration);
248
249 return true;
250 }
251
252 void CCECClient::SetSupportedDeviceTypes(void)
253 {
254 cec_device_type_list types;
255 types.Clear();
256
257 // get the command handler for the tv
258 CCECCommandHandler *tvHandler = m_processor->GetTV()->GetHandler();
259 if (!tvHandler)
260 return;
261
262 // check all device types
263 for (uint8_t iPtr = 0; iPtr < 5; iPtr++)
264 {
265 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_RESERVED)
266 continue;
267
268 // get the supported device type. the handler will replace types it doesn't support by one it does support
269 cec_device_type type = tvHandler->GetReplacementDeviceType(m_configuration.deviceTypes.types[iPtr]);
270 if (!types.IsSet(type))
271 types.Add(type);
272 }
273 m_processor->GetTV()->MarkHandlerReady();
274
275 // set the new type list
276 m_configuration.deviceTypes = types;
277 }
278
279 bool CCECClient::AllocateLogicalAddresses(void)
280 {
281 // reset all previous LAs that were set
282 m_configuration.logicalAddresses.Clear();
283
284 // get the supported device types from the command handler of the TV
285 SetSupportedDeviceTypes();
286
287 // display an error if no device types are set
288 if (m_configuration.deviceTypes.IsEmpty())
289 {
290 LIB_CEC->AddLog(CEC_LOG_ERROR, "no device types given");
291 return false;
292 }
293
294 // check each entry of the list
295 for (uint8_t iPtr = 0; iPtr < 5; iPtr++)
296 {
297 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_RESERVED)
298 continue;
299
300 // find an LA for this type
301 cec_logical_address address(CECDEVICE_UNKNOWN);
302 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_RECORDING_DEVICE)
303 address = AllocateLogicalAddressRecordingDevice();
304 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_TUNER)
305 address = AllocateLogicalAddressTuner();
306 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE)
307 address = AllocateLogicalAddressPlaybackDevice();
308 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
309 address = AllocateLogicalAddressAudioSystem();
310
311 // display an error if no LA could be allocated
312 if (address == CECDEVICE_UNKNOWN)
313 {
314 LIB_CEC->AddLog(CEC_LOG_ERROR, "%s - failed to allocate device '%d', type '%s'", __FUNCTION__, iPtr, ToString(m_configuration.deviceTypes.types[iPtr]));
315 return false;
316 }
317
318 // display the registered LA
319 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - device '%d', type '%s', LA '%X'", __FUNCTION__, iPtr, ToString(m_configuration.deviceTypes.types[iPtr]), address);
320 m_configuration.logicalAddresses.Set(address);
321 }
322
323 return true;
324 }
325
326 cec_logical_address CCECClient::AllocateLogicalAddressRecordingDevice(void)
327 {
328 cec_logical_address retVal(CECDEVICE_UNKNOWN);
329
330 LIB_CEC->AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'recording device'");
331 if (m_processor->TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1))
332 retVal = CECDEVICE_RECORDINGDEVICE1;
333 else if (m_processor->TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2))
334 retVal = CECDEVICE_RECORDINGDEVICE2;
335 else if (m_processor->TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3))
336 retVal = CECDEVICE_RECORDINGDEVICE3;
337
338 return retVal;
339 }
340
341 cec_logical_address CCECClient::AllocateLogicalAddressTuner(void)
342 {
343 cec_logical_address retVal(CECDEVICE_UNKNOWN);
344
345 LIB_CEC->AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'tuner'");
346 if (m_processor->TryLogicalAddress(CECDEVICE_TUNER1))
347 retVal = CECDEVICE_TUNER1;
348 else if (m_processor->TryLogicalAddress(CECDEVICE_TUNER2))
349 retVal = CECDEVICE_TUNER2;
350 else if (m_processor->TryLogicalAddress(CECDEVICE_TUNER3))
351 retVal = CECDEVICE_TUNER3;
352 else if (m_processor->TryLogicalAddress(CECDEVICE_TUNER4))
353 retVal = CECDEVICE_TUNER4;
354
355 return retVal;
356 }
357
358 cec_logical_address CCECClient::AllocateLogicalAddressPlaybackDevice(void)
359 {
360 cec_logical_address retVal(CECDEVICE_UNKNOWN);
361
362 LIB_CEC->AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'playback device'");
363 if (m_processor->TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1))
364 retVal = CECDEVICE_PLAYBACKDEVICE1;
365 else if (m_processor->TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2))
366 retVal = CECDEVICE_PLAYBACKDEVICE2;
367 else if (m_processor->TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3))
368 retVal = CECDEVICE_PLAYBACKDEVICE3;
369
370 return retVal;
371 }
372
373 cec_logical_address CCECClient::AllocateLogicalAddressAudioSystem(void)
374 {
375 cec_logical_address retVal(CECDEVICE_UNKNOWN);
376
377 LIB_CEC->AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'audiosystem'");
378 if (m_processor->TryLogicalAddress(CECDEVICE_AUDIOSYSTEM))
379 retVal = CECDEVICE_AUDIOSYSTEM;
380
381 return retVal;
382 }
383
384 CCECBusDevice *CCECClient::GetDeviceByType(const cec_device_type type) const
385 {
386 // get all devices that match our logical addresses
387 CECDEVICEVEC devices;
388 m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses);
389
390 // filter the type we need
391 CCECDeviceMap::FilterType(type, devices);
392
393 return devices.empty() ?
394 NULL :
395 *devices.begin();
396 }
397
398 bool CCECClient::ChangeDeviceType(const cec_device_type from, const cec_device_type to)
399 {
400 LIB_CEC->AddLog(CEC_LOG_NOTICE, "changing device type '%s' into '%s'", ToString(from), ToString(to));
401
402 CLockObject lock(m_mutex);
403
404 // get the previous device that was allocated
405 CCECBusDevice *previousDevice = GetDeviceByType(from);
406 if (!previousDevice)
407 return false;
408
409 // change the type in the device type list
410 bool bChanged(false);
411 for (uint8_t iPtr = 0; iPtr < 5; iPtr++)
412 {
413 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_RESERVED)
414 continue;
415
416 if (m_configuration.deviceTypes.types[iPtr] == from)
417 {
418 bChanged = true;
419 m_configuration.deviceTypes.types[iPtr] = to;
420 }
421 else if (m_configuration.deviceTypes.types[iPtr] == to && bChanged)
422 {
423 // ensure that dupes are removed
424 m_configuration.deviceTypes.types[iPtr] = CEC_DEVICE_TYPE_RESERVED;
425 }
426 }
427
428 // re-register the client to set the new ackmask
429 if (!m_processor->RegisterClient(this))
430 return false;
431
432 return true;
433 }
434
435 bool CCECClient::SetLogicalAddress(const cec_logical_address iLogicalAddress)
436 {
437 CLockObject lock(m_mutex);
438 if (GetPrimaryLogicalAdddress() != iLogicalAddress)
439 {
440 {
441 CLockObject lock(m_mutex);
442 LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< setting primary logical address to %1x", iLogicalAddress);
443 m_configuration.logicalAddresses.primary = iLogicalAddress;
444 m_configuration.logicalAddresses.Set(iLogicalAddress);
445 }
446 return m_processor->RegisterClient(this);
447 }
448
449 return true;
450 }
451
452 bool CCECClient::Transmit(const cec_command &data)
453 {
454 return m_processor ? m_processor->Transmit(data) : false;
455 }
456
457 bool CCECClient::SendPowerOnDevices(const cec_logical_address address /* = CECDEVICE_TV */)
458 {
459 // if the broadcast address if set as destination and the client version >=1.5.0, read the wakeDevices setting
460 if (address == CECDEVICE_BROADCAST && m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_0)
461 {
462 CECDEVICEVEC devices;
463 m_processor->GetDevices()->GetWakeDevices(m_configuration, devices);
464 return m_processor->PowerOnDevices(GetPrimaryLogicalAdddress(), devices);
465 }
466
467 return m_processor->PowerOnDevice(GetPrimaryLogicalAdddress(), address);
468 }
469
470 bool CCECClient::SendStandbyDevices(const cec_logical_address address /* = CECDEVICE_BROADCAST */)
471 {
472 // if the broadcast address if set as destination and the client version >=1.5.0, read the standbyDevices setting
473 if (address == CECDEVICE_BROADCAST && m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_0)
474 {
475 CECDEVICEVEC devices;
476 m_processor->GetDevices()->GetPowerOffDevices(m_configuration, devices);
477 return m_processor->StandbyDevices(GetPrimaryLogicalAdddress(), devices);
478 }
479
480 return m_processor->StandbyDevice(GetPrimaryLogicalAdddress(), address);
481 }
482
483 bool CCECClient::SendSetActiveSource(const cec_device_type type /* = CEC_DEVICE_TYPE_RESERVED */)
484 {
485 // get the devices that are controlled by us
486 CECDEVICEVEC devices;
487 m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses);
488
489 // filter out the device that matches the given type
490 if (type != CEC_DEVICE_TYPE_RESERVED)
491 CCECDeviceMap::FilterType(type, devices);
492
493 // no devices left, re-fetch the list of devices that are controlled by us
494 if (devices.empty())
495 m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses);
496
497 if (!devices.empty())
498 {
499 // get the first device from the list
500 CCECBusDevice *device = *devices.begin();
501
502 // and activate it
503 if (!m_processor->CECInitialised())
504 device->MarkAsActiveSource();
505 else if (device->HasValidPhysicalAddress())
506 return device->ActivateSource();
507 }
508
509 return false;
510 }
511
512 CCECPlaybackDevice *CCECClient::GetPlaybackDevice(void)
513 {
514 CCECPlaybackDevice *device(NULL);
515 CECDEVICEVEC devices;
516
517 // get the playback devices
518 m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses);
519 CCECDeviceMap::FilterType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE, devices);
520
521 // no matches, get the recording devices
522 if (devices.empty())
523 {
524 m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses);
525 CCECDeviceMap::FilterType(CEC_DEVICE_TYPE_RECORDING_DEVICE, devices);
526 }
527
528 // get the first device that matches, and cast it to CCECPlaybackDevice
529 if (!devices.empty())
530 device = (*devices.begin())->AsPlaybackDevice();
531
532 return device;
533 }
534
535 cec_logical_address CCECClient::GetPrimaryLogicalAdddress(void)
536 {
537 CLockObject lock(m_mutex);
538 return m_configuration.logicalAddresses.primary;
539 }
540
541 CCECBusDevice *CCECClient::GetPrimaryDevice(void)
542 {
543 return m_processor->GetDevice(GetPrimaryLogicalAdddress());
544 }
545
546 bool CCECClient::SendSetDeckControlMode(const cec_deck_control_mode mode, bool bSendUpdate /* = true */)
547 {
548 // find a playback device that we control
549 CCECPlaybackDevice *device = GetPlaybackDevice();
550 if (device)
551 {
552 // and set the deck control mode if there is a match
553 device->SetDeckControlMode(mode);
554 if (bSendUpdate)
555 return device->TransmitDeckStatus(CECDEVICE_TV);
556 return true;
557 }
558
559 // no match
560 return false;
561 }
562
563 bool CCECClient::SendSetDeckInfo(const cec_deck_info info, bool bSendUpdate /* = true */)
564 {
565 // find a playback device that we control
566 CCECPlaybackDevice *device = GetPlaybackDevice();
567 if (device)
568 {
569 // and set the deck status if there is a match
570 device->SetDeckStatus(info);
571 if (bSendUpdate)
572 return device->AsPlaybackDevice()->TransmitDeckStatus(CECDEVICE_TV);
573 return true;
574 }
575
576 // no match
577 return false;
578 }
579
580 bool CCECClient::SendSetMenuState(const cec_menu_state state, bool bSendUpdate /* = true */)
581 {
582 CECDEVICEVEC devices;
583
584 // set the menu state for all devices that are controlled by us
585 m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses);
586 for (CECDEVICEVEC::iterator it = devices.begin(); it != devices.end(); it++)
587 {
588 (*it)->SetMenuState(state);
589 if (bSendUpdate)
590 (*it)->TransmitMenuState(CECDEVICE_TV);
591 }
592
593 return true;
594 }
595
596 bool CCECClient::SendSetInactiveView(void)
597 {
598 CECDEVICEVEC devices;
599
600 // mark all devices that are controlled by us as inactive source
601 m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses);
602 for (CECDEVICEVEC::iterator it = devices.begin(); it != devices.end(); it++)
603 {
604 if ((*it)->IsActiveSource())
605 {
606 (*it)->MarkAsInactiveSource();
607 return (*it)->TransmitInactiveSource();
608 }
609 }
610
611 return true;
612 }
613
614 bool CCECClient::SendSetOSDString(const cec_logical_address iLogicalAddress, const cec_display_control duration, const char *strMessage)
615 {
616 CCECBusDevice *primary = GetPrimaryDevice();
617 if (primary)
618 return primary->TransmitOSDString(iLogicalAddress, duration, strMessage);
619
620 return false;
621 }
622
623 cec_version CCECClient::GetDeviceCecVersion(const cec_logical_address iAddress)
624 {
625 CCECBusDevice *device = m_processor->GetDevice(iAddress);
626 if (device)
627 return device->GetCecVersion(GetPrimaryLogicalAdddress());
628 return CEC_VERSION_UNKNOWN;
629 }
630
631 bool CCECClient::GetDeviceMenuLanguage(const cec_logical_address iAddress, cec_menu_language &language)
632 {
633 CCECBusDevice *device = m_processor->GetDevice(iAddress);
634 if (device)
635 {
636 language = device->GetMenuLanguage(GetPrimaryLogicalAdddress());
637 return (strcmp(language.language, "???") != 0);
638 }
639 return false;
640 }
641
642 cec_osd_name CCECClient::GetDeviceOSDName(const cec_logical_address iAddress)
643 {
644 cec_osd_name retVal;
645 retVal.device = iAddress;
646 retVal.name[0] = 0;
647
648 CCECBusDevice *device = m_processor->GetDevice(iAddress);
649 if (device)
650 {
651 CStdString strOSDName = device->GetOSDName(GetPrimaryLogicalAdddress());
652 snprintf(retVal.name, sizeof(retVal.name), "%s", strOSDName.c_str());
653 retVal.device = iAddress;
654 }
655
656 return retVal;
657 }
658
659 uint16_t CCECClient::GetDevicePhysicalAddress(const cec_logical_address iAddress)
660 {
661 CCECBusDevice *device = m_processor->GetDevice(iAddress);
662 if (device)
663 return device->GetPhysicalAddress(GetPrimaryLogicalAdddress());
664 return CEC_INVALID_PHYSICAL_ADDRESS;
665 }
666
667 cec_power_status CCECClient::GetDevicePowerStatus(const cec_logical_address iAddress)
668 {
669 CCECBusDevice *device = m_processor->GetDevice(iAddress);
670 if (device)
671 return device->GetPowerStatus(GetPrimaryLogicalAdddress());
672 return CEC_POWER_STATUS_UNKNOWN;
673 }
674
675 uint64_t CCECClient::GetDeviceVendorId(const cec_logical_address iAddress)
676 {
677 CCECBusDevice *device = m_processor->GetDevice(iAddress);
678 if (device)
679 return device->GetVendorId(GetPrimaryLogicalAdddress());
680 return CEC_VENDOR_UNKNOWN;
681 }
682
683 uint8_t CCECClient::SendVolumeUp(bool bSendRelease /* = true */)
684 {
685 CCECBusDevice *device = GetPrimaryDevice();
686 CCECAudioSystem *audio = m_processor->GetAudioSystem();
687
688 return device && audio && audio->IsPresent() ?
689 audio->VolumeUp(device->GetLogicalAddress(), bSendRelease) :
690 (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN;
691 }
692
693 uint8_t CCECClient::SendVolumeDown(bool bSendRelease /* = true */)
694 {
695 CCECBusDevice *device = GetPrimaryDevice();
696 CCECAudioSystem *audio = m_processor->GetAudioSystem();
697
698 return device && audio && audio->IsPresent() ?
699 audio->VolumeDown(device->GetLogicalAddress(), bSendRelease) :
700 (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN;
701 }
702
703 uint8_t CCECClient::SendMuteAudio(void)
704 {
705 CCECBusDevice *device = GetPrimaryDevice();
706 CCECAudioSystem *audio = m_processor->GetAudioSystem();
707
708 return device && audio && audio->IsPresent() ?
709 audio->MuteAudio(device->GetLogicalAddress()) :
710 (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN;
711 }
712
713 bool CCECClient::SendKeypress(const cec_logical_address iDestination, const cec_user_control_code key, bool bWait /* = true */)
714 {
715 CCECBusDevice *dest = m_processor->GetDevice(iDestination);
716
717 return dest ?
718 dest->TransmitKeypress(GetPrimaryLogicalAdddress(), key, bWait) :
719 false;
720 }
721
722 bool CCECClient::SendKeyRelease(const cec_logical_address iDestination, bool bWait /* = true */)
723 {
724 CCECBusDevice *dest = m_processor->GetDevice(iDestination);
725
726 return dest ?
727 dest->TransmitKeyRelease(GetPrimaryLogicalAdddress(), bWait) :
728 false;
729 }
730
731 bool CCECClient::GetCurrentConfiguration(libcec_configuration &configuration)
732 {
733 CLockObject lock(m_mutex);
734
735 // client version 1.5.0
736 snprintf(configuration.strDeviceName, 13, "%s", m_configuration.strDeviceName);
737 configuration.deviceTypes = m_configuration.deviceTypes;
738 configuration.bAutodetectAddress = m_configuration.bAutodetectAddress;
739 configuration.iPhysicalAddress = m_configuration.iPhysicalAddress;
740 configuration.baseDevice = m_configuration.baseDevice;
741 configuration.iHDMIPort = m_configuration.iHDMIPort;
742 configuration.clientVersion = m_configuration.clientVersion;
743 configuration.serverVersion = m_configuration.serverVersion;
744 configuration.tvVendor = m_configuration.tvVendor;
745
746 configuration.bGetSettingsFromROM = m_configuration.bGetSettingsFromROM;
747 configuration.bUseTVMenuLanguage = m_configuration.bUseTVMenuLanguage;
748 configuration.bActivateSource = m_configuration.bActivateSource;
749 configuration.wakeDevices = m_configuration.wakeDevices;
750 configuration.powerOffDevices = m_configuration.powerOffDevices;
751 configuration.bPowerOffScreensaver = m_configuration.bPowerOffScreensaver;
752 configuration.bPowerOffOnStandby = m_configuration.bPowerOffOnStandby;
753
754 // client version 1.5.1
755 if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_1)
756 configuration.bSendInactiveSource = m_configuration.bSendInactiveSource;
757
758 // client version 1.5.3
759 if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_3)
760 configuration.logicalAddresses = m_configuration.logicalAddresses;
761
762 // client version 1.6.0
763 if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_0)
764 {
765 configuration.iFirmwareVersion = m_configuration.iFirmwareVersion;
766 configuration.bPowerOffDevicesOnStandby = m_configuration.bPowerOffDevicesOnStandby;
767 configuration.bShutdownOnStandby = m_configuration.bShutdownOnStandby;
768 }
769
770 // client version 1.6.2
771 if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_2)
772 {
773 memcpy(configuration.strDeviceLanguage, m_configuration.strDeviceLanguage, 3);
774 configuration.iFirmwareBuildDate = m_configuration.iFirmwareBuildDate;
775 }
776
777 // client version 1.6.3
778 if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_3)
779 {
780 configuration.bMonitorOnly = m_configuration.bMonitorOnly;
781 }
782
783 return true;
784 }
785
786 bool CCECClient::SetConfiguration(const libcec_configuration &configuration)
787 {
788 bool bIsRunning(m_processor && m_processor->CECInitialised());
789 CCECBusDevice *primary = bIsRunning ? GetPrimaryDevice() : NULL;
790 uint16_t iPA = primary ? primary->GetCurrentPhysicalAddress() : CEC_INVALID_PHYSICAL_ADDRESS;
791
792 // update the callbacks
793 if (configuration.callbacks)
794 EnableCallbacks(configuration.callbackParam, configuration.callbacks);
795
796 // update the client version
797 SetClientVersion((cec_client_version)configuration.clientVersion);
798
799 // update the OSD name
800 CStdString strOSDName(configuration.strDeviceName);
801 SetOSDName(strOSDName);
802
803 // update the TV vendor override
804 SetTVVendorOverride((cec_vendor_id)configuration.tvVendor);
805
806 // just copy these
807 {
808 CLockObject lock(m_mutex);
809 m_configuration.bUseTVMenuLanguage = configuration.bUseTVMenuLanguage;
810 m_configuration.bActivateSource = configuration.bActivateSource;
811 m_configuration.bGetSettingsFromROM = configuration.bGetSettingsFromROM;
812 m_configuration.wakeDevices = configuration.wakeDevices;
813 m_configuration.powerOffDevices = configuration.powerOffDevices;
814 m_configuration.bPowerOffScreensaver = configuration.bPowerOffScreensaver;
815 m_configuration.bPowerOffOnStandby = configuration.bPowerOffOnStandby;
816
817 // client version 1.5.1
818 if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_1)
819 m_configuration.bSendInactiveSource = configuration.bSendInactiveSource;
820
821 // client version 1.6.0
822 if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_0)
823 {
824 m_configuration.bPowerOffDevicesOnStandby = configuration.bPowerOffDevicesOnStandby;
825 m_configuration.bShutdownOnStandby = configuration.bShutdownOnStandby;
826 }
827
828 // client version 1.6.2
829 if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_2)
830 {
831 memcpy(m_configuration.strDeviceLanguage, configuration.strDeviceLanguage, 3);
832 }
833
834 // client version 1.6.3
835 if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_3)
836 {
837 m_configuration.bMonitorOnly = configuration.bMonitorOnly;
838 }
839
840 // ensure that there is at least 1 device type set
841 if (m_configuration.deviceTypes.IsEmpty())
842 m_configuration.deviceTypes.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
843 }
844
845 bool bNeedReinit(false);
846
847 // device types
848 if (SetDeviceTypes(configuration.deviceTypes))
849 {
850 // the device type changed. just copy the rest, and re-register
851 {
852 CLockObject lock(m_mutex);
853 m_configuration.iPhysicalAddress = configuration.iPhysicalAddress;
854 m_configuration.baseDevice = configuration.baseDevice;
855 m_configuration.iHDMIPort = configuration.iHDMIPort;
856 bNeedReinit = true;
857 }
858 }
859 else
860 {
861 // set the physical address
862 SetPhysicalAddress(configuration);
863 }
864
865 m_processor->PersistConfiguration(m_configuration);
866
867 if (!primary)
868 primary = GetPrimaryDevice();
869
870 if (bNeedReinit || !primary || primary->GetCurrentPhysicalAddress() != iPA)
871 {
872 // PA or device type changed
873 m_processor->RegisterClient(this);
874 }
875 else if (primary && configuration.bActivateSource == 1 && bIsRunning && !primary->IsActiveSource())
876 {
877 // activate the source if we're not already the active source
878 primary->ActivateSource();
879 }
880
881 return true;
882 }
883
884 void CCECClient::AddCommand(const cec_command &command)
885 {
886 // don't forward the standby opcode more than once every 10 seconds
887 if (command.opcode == CEC_OPCODE_STANDBY)
888 {
889 CLockObject lock(m_mutex);
890 if (m_iPreventForwardingPowerOffCommand != 0 &&
891 m_iPreventForwardingPowerOffCommand > GetTimeMs())
892 return;
893 else
894 m_iPreventForwardingPowerOffCommand = GetTimeMs() + CEC_FORWARD_STANDBY_MIN_INTERVAL;
895 }
896
897 if (command.destination == CECDEVICE_BROADCAST || GetLogicalAddresses().IsSet(command.destination))
898 {
899 CLockObject lock(m_mutex);
900
901 LIB_CEC->AddLog(CEC_LOG_NOTICE, ">> %s (%X) -> %s (%X): %s (%2X)", ToString(command.initiator), command.initiator, ToString(command.destination), command.destination, ToString(command.opcode), command.opcode);
902
903 if (m_configuration.callbacks && m_configuration.callbacks->CBCecCommand)
904 m_configuration.callbacks->CBCecCommand(m_configuration.callbackParam, command);
905 else if (!m_commandBuffer.Push(command))
906 LIB_CEC->AddLog(CEC_LOG_WARNING, "command buffer is full");
907 }
908 }
909
910 int CCECClient::MenuStateChanged(const cec_menu_state newState)
911 {
912 CLockObject lock(m_mutex);
913
914 LIB_CEC->AddLog(CEC_LOG_NOTICE, ">> %s: %s", ToString(CEC_OPCODE_MENU_REQUEST), ToString(newState));
915
916 if (m_configuration.callbacks &&
917 m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_2 &&
918 m_configuration.callbacks->CBCecMenuStateChanged)
919 return m_configuration.callbacks->CBCecMenuStateChanged(m_configuration.callbackParam, newState);
920
921 return 0;
922 }
923
924 void CCECClient::SourceActivated(const cec_logical_address logicalAddress)
925 {
926 CLockObject lock(m_mutex);
927
928 LIB_CEC->AddLog(CEC_LOG_NOTICE, ">> source activated: %s (%x)", ToString(logicalAddress), logicalAddress);
929
930 if (m_configuration.callbacks &&
931 m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_7_1 &&
932 m_configuration.callbacks->CBCecSourceActivated)
933 m_configuration.callbacks->CBCecSourceActivated(m_configuration.callbackParam, logicalAddress, 1);
934 }
935
936 void CCECClient::SourceDeactivated(const cec_logical_address logicalAddress)
937 {
938 CLockObject lock(m_mutex);
939
940 LIB_CEC->AddLog(CEC_LOG_NOTICE, ">> source deactivated: %s (%x)", ToString(logicalAddress), logicalAddress);
941
942 if (m_configuration.callbacks &&
943 m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_7_1 &&
944 m_configuration.callbacks->CBCecSourceActivated)
945 m_configuration.callbacks->CBCecSourceActivated(m_configuration.callbackParam, logicalAddress, 0);
946 }
947
948 void CCECClient::Alert(const libcec_alert type, const libcec_parameter &param)
949 {
950 CLockObject lock(m_mutex);
951
952 if (m_configuration.callbacks &&
953 m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_0 &&
954 m_configuration.callbacks->CBCecAlert)
955 m_configuration.callbacks->CBCecAlert(m_configuration.callbackParam, type, param);
956 }
957
958 void CCECClient::AddLog(const cec_log_message &message)
959 {
960 CLockObject lock(m_logMutex);
961 if (m_configuration.callbacks && m_configuration.callbacks->CBCecLogMessage)
962 m_configuration.callbacks->CBCecLogMessage(m_configuration.callbackParam, message);
963 else
964 m_logBuffer.Push(message);
965 }
966
967 void CCECClient::AddKey(void)
968 {
969 CLockObject lock(m_mutex);
970
971 if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN)
972 {
973 cec_keypress key;
974
975 key.duration = (unsigned int) (GetTimeMs() - m_buttontime);
976 key.keycode = m_iCurrentButton;
977 LIB_CEC->AddLog(CEC_LOG_DEBUG, "key released: %1x", key.keycode);
978
979 if (m_configuration.callbacks && m_configuration.callbacks->CBCecKeyPress)
980 m_configuration.callbacks->CBCecKeyPress(m_configuration.callbackParam, key);
981 else
982 m_keyBuffer.Push(key);
983 m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
984 }
985
986 m_buttontime = 0;
987 }
988
989 void CCECClient::AddKey(const cec_keypress &key)
990 {
991 CLockObject lock(m_mutex);
992
993 LIB_CEC->AddLog(CEC_LOG_DEBUG, "key pressed: %1x", key.keycode);
994
995 if (m_configuration.callbacks && m_configuration.callbacks->CBCecKeyPress)
996 m_configuration.callbacks->CBCecKeyPress(m_configuration.callbackParam, key);
997 else
998 m_keyBuffer.Push(key);
999
1000 m_iCurrentButton = key.duration > 0 ? CEC_USER_CONTROL_CODE_UNKNOWN : key.keycode;
1001 m_buttontime = key.duration > 0 ? 0 : GetTimeMs();
1002 }
1003
1004 void CCECClient::SetCurrentButton(const cec_user_control_code iButtonCode)
1005 {
1006 // push a keypress to the buffer with 0 duration and another with the duration set when released
1007 cec_keypress key;
1008 key.duration = 0;
1009 key.keycode = iButtonCode;
1010
1011 AddKey(key);
1012 }
1013
1014 void CCECClient::CheckKeypressTimeout(void)
1015 {
1016 if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN && GetTimeMs() - m_buttontime > CEC_BUTTON_TIMEOUT)
1017 {
1018 AddKey();
1019 m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
1020 }
1021 }
1022
1023 void CCECClient::ConfigurationChanged(const libcec_configuration &config)
1024 {
1025 CLockObject lock(m_mutex);
1026
1027 if (m_configuration.callbacks &&
1028 m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_0 &&
1029 m_configuration.callbacks->CBCecConfigurationChanged &&
1030 m_processor->CECInitialised())
1031 m_configuration.callbacks->CBCecConfigurationChanged(m_configuration.callbackParam, config);
1032 }
1033
1034 bool CCECClient::EnableCallbacks(void *cbParam, ICECCallbacks *callbacks)
1035 {
1036 CLockObject lock(m_mutex);
1037 m_configuration.callbackParam = cbParam;
1038 m_configuration.callbacks = callbacks;
1039 return true;
1040 }
1041
1042 bool CCECClient::PingAdapter(void)
1043 {
1044 return m_processor ? m_processor->PingAdapter() : false;
1045 }
1046
1047 bool CCECClient::GetNextLogMessage(cec_log_message *message)
1048 {
1049 return (m_logBuffer.Pop(*message));
1050 }
1051
1052 bool CCECClient::GetNextKeypress(cec_keypress *key)
1053 {
1054 return m_keyBuffer.Pop(*key);
1055 }
1056
1057 bool CCECClient::GetNextCommand(cec_command *command)
1058 {
1059 return m_commandBuffer.Pop(*command);
1060 }
1061
1062 CStdString CCECClient::GetConnectionInfo(void)
1063 {
1064 CStdString strLog;
1065 strLog.Format("libCEC version = %s, client version = %s, firmware version = %d", ToString((cec_server_version)m_configuration.serverVersion), ToString((cec_client_version)m_configuration.clientVersion), m_configuration.iFirmwareVersion);
1066 if (m_configuration.iFirmwareBuildDate != CEC_FW_BUILD_UNKNOWN)
1067 {
1068 time_t buildTime = (time_t)m_configuration.iFirmwareBuildDate;
1069 strLog.AppendFormat(", firmware build date: %s", asctime(gmtime(&buildTime)));
1070 strLog = strLog.Left((int)strLog.length() - 1); // strip \n added by asctime
1071 strLog.append(" +0000");
1072 }
1073
1074 // log the addresses that are being used
1075 if (!m_configuration.logicalAddresses.IsEmpty())
1076 {
1077 strLog.append(", logical address(es) = ");
1078 CECDEVICEVEC devices;
1079 m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses);
1080 for (CECDEVICEVEC::iterator it = devices.begin(); it != devices.end(); it++)
1081 strLog.AppendFormat("%s (%X) ", (*it)->GetLogicalAddressName(), (*it)->GetLogicalAddress());
1082 }
1083
1084 if (!CLibCEC::IsValidPhysicalAddress(m_configuration.iPhysicalAddress))
1085 strLog.AppendFormat(", base device: %s (%X), HDMI port number: %d", ToString(m_configuration.baseDevice), m_configuration.baseDevice, m_configuration.iHDMIPort);
1086 else
1087 strLog.AppendFormat(", physical address: %04x", m_configuration.iPhysicalAddress);
1088
1089 return strLog;
1090 }
1091
1092 void CCECClient::SetTVVendorOverride(const cec_vendor_id id)
1093 {
1094 {
1095 CLockObject lock(m_mutex);
1096 m_configuration.tvVendor = id;
1097 }
1098
1099 if (id != CEC_VENDOR_UNKNOWN)
1100 {
1101 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - vendor id '%s'", __FUNCTION__, ToString(id));
1102
1103 CCECBusDevice *tv = m_processor ? m_processor->GetTV() : NULL;
1104 if (tv)
1105 tv->SetVendorId((uint64_t)id);
1106 }
1107 }
1108
1109 cec_vendor_id CCECClient::GetTVVendorOverride(void)
1110 {
1111 CLockObject lock(m_mutex);
1112 return (cec_vendor_id)m_configuration.tvVendor;
1113 }
1114
1115 void CCECClient::SetOSDName(const CStdString &strDeviceName)
1116 {
1117 {
1118 CLockObject lock(m_mutex);
1119 snprintf(m_configuration.strDeviceName, 13, "%s", strDeviceName.c_str());
1120 }
1121
1122 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - using OSD name '%s'", __FUNCTION__, strDeviceName.c_str());
1123
1124 CCECBusDevice *primary = GetPrimaryDevice();
1125 if (primary && !primary->GetCurrentOSDName().Equals(strDeviceName))
1126 {
1127 primary->SetOSDName(strDeviceName);
1128 if (m_processor && m_processor->CECInitialised())
1129 primary->TransmitOSDName(CECDEVICE_TV);
1130 }
1131 }
1132
1133 CStdString CCECClient::GetOSDName(void)
1134 {
1135 CLockObject lock(m_mutex);
1136 CStdString strOSDName(m_configuration.strDeviceName);
1137 return strOSDName;
1138 }
1139
1140 void CCECClient::SetWakeDevices(const cec_logical_addresses &addresses)
1141 {
1142 CLockObject lock(m_mutex);
1143 m_configuration.wakeDevices = addresses;
1144 }
1145
1146 cec_logical_addresses CCECClient::GetWakeDevices(void)
1147 {
1148 CLockObject lock(m_mutex);
1149 return m_configuration.wakeDevices;
1150 }
1151
1152 bool CCECClient::AutodetectPhysicalAddress(void)
1153 {
1154 bool bPhysicalAutodetected(false);
1155 uint16_t iPhysicalAddress = m_processor ? m_processor->GetDetectedPhysicalAddress() : CEC_INVALID_PHYSICAL_ADDRESS;
1156
1157 if (CLibCEC::IsValidPhysicalAddress(iPhysicalAddress))
1158 {
1159 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - autodetected physical address '%04X'", __FUNCTION__, iPhysicalAddress);
1160
1161 CLockObject lock(m_mutex);
1162 m_configuration.iPhysicalAddress = iPhysicalAddress;
1163 m_configuration.iHDMIPort = CEC_HDMI_PORTNUMBER_NONE;
1164 m_configuration.baseDevice = CECDEVICE_UNKNOWN;
1165 bPhysicalAutodetected = true;
1166 }
1167
1168 SetDevicePhysicalAddress(iPhysicalAddress);
1169
1170 return bPhysicalAutodetected;
1171 }
1172
1173 void CCECClient::SetClientVersion(const cec_client_version version)
1174 {
1175 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - using client version '%s'", __FUNCTION__, ToString(version));
1176
1177 CLockObject lock(m_mutex);
1178 m_configuration.clientVersion = (uint32_t)version;
1179 }
1180
1181 cec_client_version CCECClient::GetClientVersion(void)
1182 {
1183 CLockObject lock(m_mutex);
1184 return (cec_client_version)m_configuration.clientVersion;
1185 }
1186
1187 bool CCECClient::SetDeviceTypes(const cec_device_type_list &deviceTypes)
1188 {
1189 bool bNeedReinit(false);
1190
1191 {
1192 CLockObject lock(m_mutex);
1193 bNeedReinit = m_processor && m_processor->CECInitialised() &&
1194 (m_configuration.deviceTypes != deviceTypes);
1195 m_configuration.deviceTypes = deviceTypes;
1196 }
1197
1198 if (bNeedReinit)
1199 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - using primary device type '%s'", __FUNCTION__, ToString(deviceTypes[0]));
1200
1201 return bNeedReinit;
1202 }
1203
1204 cec_device_type_list CCECClient::GetDeviceTypes(void)
1205 {
1206 cec_device_type_list retVal;
1207 CLockObject lock(m_mutex);
1208 retVal = m_configuration.deviceTypes;
1209 return retVal;
1210 }
1211
1212 bool CCECClient::SetDevicePhysicalAddress(const uint16_t iPhysicalAddress)
1213 {
1214 if (!CLibCEC::IsValidPhysicalAddress(iPhysicalAddress))
1215 {
1216 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - not setting invalid physical address %04x", __FUNCTION__, iPhysicalAddress);
1217 return false;
1218 }
1219
1220 // reconfigure all devices
1221 cec_logical_address reactivateSource(CECDEVICE_UNKNOWN);
1222 CECDEVICEVEC devices;
1223 m_processor->GetDevices()->GetByLogicalAddresses(devices, m_configuration.logicalAddresses);
1224 for (CECDEVICEVEC::iterator it = devices.begin(); it != devices.end(); it++)
1225 {
1226 // if this device was the active source, reactivate it afterwards
1227 if ((*it)->IsActiveSource())
1228 reactivateSource = (*it)->GetLogicalAddress();
1229
1230 // mark the device as inactive source
1231 if (IsInitialised())
1232 (*it)->MarkAsInactiveSource();
1233
1234 // set the new physical address
1235 (*it)->SetPhysicalAddress(iPhysicalAddress);
1236
1237 // and transmit it
1238 if (IsInitialised())
1239 (*it)->TransmitPhysicalAddress();
1240 }
1241
1242 // reactivate the previous active source
1243 if (reactivateSource != CECDEVICE_UNKNOWN &&
1244 m_processor->CECInitialised() &&
1245 IsInitialised())
1246 {
1247 CCECBusDevice *device = m_processor->GetDevice(reactivateSource);
1248 if (device)
1249 device->ActivateSource();
1250 }
1251
1252 return true;
1253 }
1254
1255 bool CCECClient::SwitchMonitoring(bool bEnable)
1256 {
1257 LIB_CEC->AddLog(CEC_LOG_NOTICE, "== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
1258
1259 if (m_processor)
1260 {
1261 if (bEnable)
1262 return m_processor->UnregisterClient(this);
1263 else
1264 {
1265 m_configuration.bMonitorOnly = false;
1266 return m_processor->RegisterClient(this);
1267 }
1268 }
1269
1270 return false;
1271 }
1272
1273 bool CCECClient::PollDevice(const cec_logical_address iAddress)
1274 {
1275 // try to find the primary device
1276 CCECBusDevice *primary = GetPrimaryDevice();
1277 // poll the destination, with the primary as source
1278 if (primary)
1279 return primary->TransmitPoll(iAddress);
1280
1281 return m_processor ? m_processor->PollDevice(iAddress) : false;
1282 }
1283
1284 cec_logical_addresses CCECClient::GetActiveDevices(void)
1285 {
1286 CECDEVICEVEC activeDevices;
1287 if (m_processor)
1288 m_processor->GetDevices()->GetActive(activeDevices);
1289 return CCECDeviceMap::ToLogicalAddresses(activeDevices);
1290 }
1291
1292 bool CCECClient::IsActiveDevice(const cec_logical_address iAddress)
1293 {
1294 cec_logical_addresses activeDevices = GetActiveDevices();
1295 return activeDevices.IsSet(iAddress);
1296 }
1297
1298 bool CCECClient::IsActiveDeviceType(const cec_device_type type)
1299 {
1300 CECDEVICEVEC activeDevices;
1301 if (m_processor)
1302 m_processor->GetDevices()->GetActive(activeDevices);
1303 CCECDeviceMap::FilterType(type, activeDevices);
1304 return !activeDevices.empty();
1305 }
1306
1307 cec_logical_address CCECClient::GetActiveSource(void)
1308 {
1309 return m_processor ? m_processor->GetActiveSource() : CECDEVICE_UNKNOWN;
1310 }
1311
1312 bool CCECClient::IsActiveSource(const cec_logical_address iAddress)
1313 {
1314 return m_processor ? m_processor->IsActiveSource(iAddress) : false;
1315 }
1316
1317 bool CCECClient::SetStreamPath(const cec_logical_address iAddress)
1318 {
1319 uint16_t iPhysicalAddress = GetDevicePhysicalAddress(iAddress);
1320 if (iPhysicalAddress != CEC_INVALID_PHYSICAL_ADDRESS)
1321 return SetStreamPath(iPhysicalAddress);
1322 return false;
1323 }
1324
1325 bool CCECClient::SetStreamPath(const uint16_t iPhysicalAddress)
1326 {
1327 return m_processor ? m_processor->SetStreamPath(iPhysicalAddress) : false;
1328 }
1329
1330 cec_logical_addresses CCECClient::GetLogicalAddresses(void)
1331 {
1332 cec_logical_addresses addresses;
1333 CLockObject lock(m_mutex);
1334 addresses = m_configuration.logicalAddresses;
1335 return addresses;
1336 }
1337
1338 bool CCECClient::CanPersistConfiguration(void)
1339 {
1340 return m_processor ? m_processor->CanPersistConfiguration() : false;
1341 }
1342
1343 bool CCECClient::PersistConfiguration(const libcec_configuration &configuration)
1344 {
1345 return m_processor ? m_processor->PersistConfiguration(configuration) : false;
1346 }
1347
1348 void CCECClient::RescanActiveDevices(void)
1349 {
1350 if (m_processor)
1351 m_processor->RescanActiveDevices();
1352 }
1353
1354 bool CCECClient::IsLibCECActiveSource(void)
1355 {
1356 bool bReturn(false);
1357 if (m_processor)
1358 {
1359 cec_logical_address activeSource = m_processor->GetActiveSource();
1360 CCECBusDevice *device = m_processor->GetDevice(activeSource);
1361 if (device)
1362 bReturn = device->IsHandledByLibCEC();
1363 }
1364 return bReturn;
1365 }