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