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