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