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