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