cec: give priority to messages from the TV. removed the global lock in CCECProcessor...
[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(this, (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 (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 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamPath);
518 if (device)
519 {
520 device->SetActiveSource();
521 bReturn = true;
522 }
523
524 return bReturn;
525 }
526
527 void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout)
528 {
529 CLockObject lock(m_mutex);
530 m_iStandardLineTimeout = iTimeout;
531 }
532
533 void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout)
534 {
535 CLockObject lock(m_mutex);
536 m_iRetryLineTimeout = iTimeout;
537 }
538
539 bool CCECProcessor::SetActiveView(void)
540 {
541 CLibCEC::AddLog(CEC_LOG_WARNING, "deprecated method %s called", __FUNCTION__);
542 return SetActiveSource(m_configuration.deviceTypes.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED : m_configuration.deviceTypes[0]);
543 }
544
545 bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode, bool bSendUpdate /* = true */)
546 {
547 bool bReturn(false);
548
549 CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
550 if (device)
551 {
552 ((CCECPlaybackDevice *) device)->SetDeckControlMode(mode);
553 if (bSendUpdate)
554 ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV);
555 bReturn = true;
556 }
557
558 return bReturn;
559 }
560
561 bool CCECProcessor::SetDeckInfo(cec_deck_info info, bool bSendUpdate /* = true */)
562 {
563 bool bReturn(false);
564
565 CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
566 if (device)
567 {
568 ((CCECPlaybackDevice *) device)->SetDeckStatus(info);
569 if (bSendUpdate)
570 ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV);
571 bReturn = true;
572 }
573
574 return bReturn;
575 }
576
577 bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice, uint8_t iPort, bool bForce /* = false */)
578 {
579 bool bReturn(false);
580
581 {
582 CLockObject lock(m_mutex);
583 m_configuration.baseDevice = iBaseDevice;
584 m_configuration.iHDMIPort = iPort;
585 }
586
587 if (!IsRunning() && !bForce)
588 return true;
589
590 CLibCEC::AddLog(CEC_LOG_DEBUG, "setting HDMI port to %d on device %s (%d)", iPort, ToString(iBaseDevice), (int)iBaseDevice);
591
592 uint16_t iPhysicalAddress(0);
593 if (iBaseDevice > CECDEVICE_TV)
594 {
595 iPhysicalAddress = m_busDevices[iBaseDevice]->GetPhysicalAddress();
596 }
597
598 if (iPhysicalAddress < 0xffff)
599 {
600 if (iPhysicalAddress == 0)
601 iPhysicalAddress += 0x1000 * iPort;
602 else if (iPhysicalAddress % 0x1000 == 0)
603 iPhysicalAddress += 0x100 * iPort;
604 else if (iPhysicalAddress % 0x100 == 0)
605 iPhysicalAddress += 0x10 * iPort;
606 else if (iPhysicalAddress % 0x10 == 0)
607 iPhysicalAddress += iPort;
608
609 bReturn = true;
610 }
611
612 if (!bReturn)
613 CLibCEC::AddLog(CEC_LOG_ERROR, "failed to set the physical address");
614 else
615 SetPhysicalAddress(iPhysicalAddress);
616
617 return bReturn;
618 }
619
620 bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress)
621 {
622 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
623 {
624 if (m_busDevices[iPtr]->GetPhysicalAddress(false) == iPhysicalAddress)
625 return true;
626 }
627 return false;
628 }
629
630 bool CCECProcessor::TransmitInactiveSource(void)
631 {
632 if (!IsRunning())
633 return false;
634
635 if (!m_configuration.logicalAddresses.IsEmpty() && m_busDevices[m_configuration.logicalAddresses.primary])
636 return m_busDevices[m_configuration.logicalAddresses.primary]->TransmitInactiveSource();
637 return false;
638 }
639
640 void CCECProcessor::LogOutput(const cec_command &data)
641 {
642 CStdString strTx;
643 strTx.Format("<< %02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination);
644 if (data.opcode_set)
645 strTx.AppendFormat(":%02x", (uint8_t)data.opcode);
646
647 for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++)
648 strTx.AppendFormat(":%02x", data.parameters[iPtr]);
649 CLibCEC::AddLog(CEC_LOG_TRAFFIC, strTx.c_str());
650 }
651
652 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress)
653 {
654 CLockObject lock(m_mutex);
655 if (m_configuration.logicalAddresses.primary != iLogicalAddress)
656 {
657 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< setting primary logical address to %1x", iLogicalAddress);
658 m_configuration.logicalAddresses.primary = iLogicalAddress;
659 m_configuration.logicalAddresses.Set(iLogicalAddress);
660 return SetAckMask(m_configuration.logicalAddresses.AckMask());
661 }
662
663 return true;
664 }
665
666 bool CCECProcessor::SetMenuState(cec_menu_state state, bool bSendUpdate /* = true */)
667 {
668 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
669 {
670 if (m_configuration.logicalAddresses[iPtr])
671 m_busDevices[iPtr]->SetMenuState(state);
672 }
673
674 if (bSendUpdate)
675 m_busDevices[m_configuration.logicalAddresses.primary]->TransmitMenuState(CECDEVICE_TV);
676
677 return true;
678 }
679
680 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress, bool bSendUpdate /* = true */)
681 {
682 bool bSendActiveView(false);
683 bool bReturn(false);
684 cec_logical_addresses sendUpdatesTo;
685 sendUpdatesTo.Clear();
686
687 {
688 CLockObject lock(m_mutex);
689 m_configuration.iPhysicalAddress = iPhysicalAddress;
690 CLibCEC::AddLog(CEC_LOG_DEBUG, "setting physical address to '%4x'", iPhysicalAddress);
691
692 if (!m_configuration.logicalAddresses.IsEmpty())
693 {
694 bool bWasActiveSource(false);
695 for (uint8_t iPtr = 0; iPtr < 15; iPtr++)
696 if (m_configuration.logicalAddresses[iPtr])
697 {
698 bWasActiveSource |= m_busDevices[iPtr]->IsActiveSource();
699 m_busDevices[iPtr]->SetInactiveSource();
700 m_busDevices[iPtr]->SetPhysicalAddress(iPhysicalAddress);
701 if (bSendUpdate)
702 sendUpdatesTo.Set((cec_logical_address)iPtr);
703 }
704
705 bSendActiveView = bWasActiveSource && bSendUpdate;
706 bReturn = true;
707 }
708 }
709
710 for (uint8_t iPtr = 0; iPtr < 15; iPtr++)
711 if (sendUpdatesTo[iPtr])
712 m_busDevices[iPtr]->TransmitPhysicalAddress();
713
714 if (bSendActiveView)
715 SetActiveView();
716
717 if (bReturn)
718 CLibCEC::ConfigurationChanged(m_configuration);
719
720 return bReturn;
721 }
722
723 bool CCECProcessor::SwitchMonitoring(bool bEnable)
724 {
725 CLibCEC::AddLog(CEC_LOG_NOTICE, "== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
726
727 {
728 CLockObject lock(m_mutex);
729 m_bMonitor = bEnable;
730 }
731
732 if (bEnable)
733 return SetAckMask(0);
734 else
735 return SetAckMask(m_configuration.logicalAddresses.AckMask());
736 }
737
738 bool CCECProcessor::PollDevice(cec_logical_address iAddress)
739 {
740 if (iAddress != CECDEVICE_UNKNOWN && m_busDevices[iAddress])
741 {
742 return m_configuration.logicalAddresses.primary == CECDEVICE_UNKNOWN ?
743 m_busDevices[iAddress]->TransmitPoll(iAddress) :
744 m_busDevices[m_configuration.logicalAddresses.primary]->TransmitPoll(iAddress);
745 }
746 return false;
747 }
748
749 uint8_t CCECProcessor::VolumeUp(bool bSendRelease /* = true */)
750 {
751 uint8_t status = 0;
752 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM))
753 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeUp(bSendRelease);
754
755 return status;
756 }
757
758 uint8_t CCECProcessor::VolumeDown(bool bSendRelease /* = true */)
759 {
760 uint8_t status = 0;
761 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM))
762 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeDown(bSendRelease);
763
764 return status;
765 }
766
767 uint8_t CCECProcessor::MuteAudio(bool bSendRelease /* = true */)
768 {
769 uint8_t status = 0;
770 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM))
771 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->MuteAudio(bSendRelease);
772
773 return status;
774 }
775
776 CCECBusDevice *CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bRefresh /* = false */) const
777 {
778 if (m_busDevices[m_configuration.logicalAddresses.primary]->GetPhysicalAddress(false) == iPhysicalAddress)
779 return m_busDevices[m_configuration.logicalAddresses.primary];
780
781 CCECBusDevice *device = NULL;
782 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
783 {
784 if (m_busDevices[iPtr]->GetPhysicalAddress(bRefresh) == iPhysicalAddress)
785 {
786 device = m_busDevices[iPtr];
787 break;
788 }
789 }
790
791 return device;
792 }
793
794 CCECBusDevice *CCECProcessor::GetDeviceByType(cec_device_type type) const
795 {
796 CCECBusDevice *device = NULL;
797
798 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
799 {
800 if (m_busDevices[iPtr]->m_type == type && m_configuration.logicalAddresses[iPtr])
801 {
802 device = m_busDevices[iPtr];
803 break;
804 }
805 }
806
807 return device;
808 }
809
810 CCECBusDevice *CCECProcessor::GetPrimaryDevice(void) const
811 {
812 CCECBusDevice *device(NULL);
813 cec_logical_address primary = m_configuration.logicalAddresses.primary;
814 if (primary != CECDEVICE_UNKNOWN)
815 device = m_busDevices[primary];
816 return device;
817 }
818
819 cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress)
820 {
821 return m_busDevices[iAddress]->GetCecVersion();
822 }
823
824 cec_osd_name CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress)
825 {
826 CStdString strOSDName = m_busDevices[iAddress]->GetOSDName();
827 cec_osd_name retVal;
828
829 snprintf(retVal.name, sizeof(retVal.name), "%s", strOSDName.c_str());
830 retVal.device = iAddress;
831
832 return retVal;
833 }
834
835 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
836 {
837 if (m_busDevices[iAddress])
838 {
839 *language = m_busDevices[iAddress]->GetMenuLanguage();
840 return (strcmp(language->language, "???") != 0);
841 }
842 return false;
843 }
844
845 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress)
846 {
847 if (m_busDevices[iAddress])
848 return m_busDevices[iAddress]->GetVendorId();
849 return false;
850 }
851
852 uint16_t CCECProcessor::GetDevicePhysicalAddress(cec_logical_address iAddress)
853 {
854 if (m_busDevices[iAddress])
855 return m_busDevices[iAddress]->GetPhysicalAddress(false);
856 return false;
857 }
858
859 cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress)
860 {
861 if (m_busDevices[iAddress])
862 return m_busDevices[iAddress]->GetPowerStatus();
863 return CEC_POWER_STATUS_UNKNOWN;
864 }
865
866 cec_logical_address CCECProcessor::GetActiveSource(void)
867 {
868 for (uint8_t iPtr = 0; iPtr <= 11; iPtr++)
869 {
870 if (m_busDevices[iPtr]->IsActiveSource())
871 return (cec_logical_address)iPtr;
872 }
873
874 return CECDEVICE_UNKNOWN;
875 }
876
877 bool CCECProcessor::IsActiveSource(cec_logical_address iAddress)
878 {
879 return m_busDevices[iAddress]->IsActiveSource();
880 }
881
882 bool CCECProcessor::Transmit(const cec_command &data)
883 {
884 if (m_configuration.logicalAddresses[(uint8_t)data.destination])
885 {
886 CLibCEC::AddLog(CEC_LOG_WARNING, "not sending data to myself!");
887 return false;
888 }
889
890 uint8_t iMaxTries(0);
891 {
892 CLockObject lock(m_mutex);
893 if (IsStopped())
894 return false;
895 LogOutput(data);
896 m_iLastTransmission = GetTimeMs();
897 if (!m_communication || !m_communication->IsOpen())
898 {
899 CLibCEC::AddLog(CEC_LOG_ERROR, "cannot transmit command: connection closed");
900 return false;
901 }
902 iMaxTries = m_busDevices[data.initiator]->GetHandler()->GetTransmitRetries() + 1;
903 }
904
905 return m_communication->Write(data, iMaxTries, m_iStandardLineTimeout, m_iRetryLineTimeout)
906 == ADAPTER_MESSAGE_STATE_SENT_ACKED;
907 }
908
909 void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, cec_abort_reason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
910 {
911 CLibCEC::AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
912
913 cec_command command;
914 // TODO
915 cec_command::Format(command, m_configuration.logicalAddresses.primary, address, CEC_OPCODE_FEATURE_ABORT);
916 command.parameters.PushBack((uint8_t)opcode);
917 command.parameters.PushBack((uint8_t)reason);
918
919 Transmit(command);
920 }
921
922 void CCECProcessor::ParseCommand(const cec_command &command)
923 {
924 CStdString dataStr;
925 dataStr.Format(">> %1x%1x", command.initiator, command.destination);
926 if (command.opcode_set == 1)
927 dataStr.AppendFormat(":%02x", command.opcode);
928 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
929 dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
930 CLibCEC::AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
931
932 if (!m_bMonitor && command.initiator >= CECDEVICE_TV && command.initiator <= CECDEVICE_BROADCAST)
933 m_busDevices[(uint8_t)command.initiator]->HandleCommand(command);
934 }
935
936 cec_logical_addresses CCECProcessor::GetActiveDevices(void)
937 {
938 cec_logical_addresses addresses;
939 addresses.Clear();
940 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
941 {
942 if (m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
943 addresses.Set((cec_logical_address) iPtr);
944 }
945 return addresses;
946 }
947
948 bool CCECProcessor::IsPresentDevice(cec_logical_address address)
949 {
950 return m_busDevices[address]->GetStatus() == CEC_DEVICE_STATUS_PRESENT;
951 }
952
953 bool CCECProcessor::IsPresentDeviceType(cec_device_type type)
954 {
955 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
956 {
957 if (m_busDevices[iPtr]->GetType() == type && m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
958 return true;
959 }
960
961 return false;
962 }
963
964 uint16_t CCECProcessor::GetPhysicalAddress(void) const
965 {
966 if (!m_configuration.logicalAddresses.IsEmpty() && m_busDevices[m_configuration.logicalAddresses.primary])
967 return m_busDevices[m_configuration.logicalAddresses.primary]->GetPhysicalAddress(false);
968 return false;
969 }
970
971 bool CCECProcessor::SetAckMask(uint16_t iMask)
972 {
973 return m_communication->SetAckMask(iMask);
974 }
975
976 bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
977 {
978 return m_busDevices[iDestination]->TransmitKeypress(key, bWait);
979 }
980
981 bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination, bool bWait /* = true */)
982 {
983 return m_busDevices[iDestination]->TransmitKeyRelease(bWait);
984 }
985
986 bool CCECProcessor::EnablePhysicalAddressDetection(void)
987 {
988 CLibCEC::AddLog(CEC_LOG_WARNING, "deprecated method %s called", __FUNCTION__);
989 uint16_t iPhysicalAddress = m_communication->GetPhysicalAddress();
990 if (iPhysicalAddress != 0)
991 {
992 m_configuration.bAutodetectAddress = 1;
993 m_configuration.iPhysicalAddress = iPhysicalAddress;
994 m_configuration.baseDevice = CECDEVICE_UNKNOWN;
995 m_configuration.iHDMIPort = 0;
996 return SetPhysicalAddress(iPhysicalAddress);
997 }
998 return false;
999 }
1000
1001 bool CCECProcessor::StandbyDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
1002 {
1003 if (address == CECDEVICE_BROADCAST && m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_0)
1004 {
1005 bool bReturn(true);
1006 for (uint8_t iPtr = 0; iPtr <= 0xF; iPtr++)
1007 {
1008 if (m_configuration.powerOffDevices[iPtr])
1009 bReturn &= m_busDevices[iPtr]->Standby();
1010 }
1011 return bReturn;
1012 }
1013
1014 return m_busDevices[address]->Standby();
1015 }
1016
1017 bool CCECProcessor::PowerOnDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
1018 {
1019 if (address == CECDEVICE_BROADCAST && m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_0)
1020 {
1021 bool bReturn(true);
1022 for (uint8_t iPtr = 0; iPtr <= 0xF; iPtr++)
1023 {
1024 if (m_configuration.powerOffDevices[iPtr])
1025 bReturn &= m_busDevices[iPtr]->PowerOn();
1026 }
1027 return bReturn;
1028 }
1029
1030 return m_busDevices[address]->PowerOn();
1031 }
1032
1033 const char *CCECProcessor::ToString(const cec_device_type type)
1034 {
1035 switch (type)
1036 {
1037 case CEC_DEVICE_TYPE_AUDIO_SYSTEM:
1038 return "audio system";
1039 case CEC_DEVICE_TYPE_PLAYBACK_DEVICE:
1040 return "playback device";
1041 case CEC_DEVICE_TYPE_RECORDING_DEVICE:
1042 return "recording device";
1043 case CEC_DEVICE_TYPE_RESERVED:
1044 return "reserved";
1045 case CEC_DEVICE_TYPE_TUNER:
1046 return "tuner";
1047 case CEC_DEVICE_TYPE_TV:
1048 return "TV";
1049 default:
1050 return "unknown";
1051 }
1052 }
1053
1054 const char *CCECProcessor::ToString(const cec_menu_state state)
1055 {
1056 switch (state)
1057 {
1058 case CEC_MENU_STATE_ACTIVATED:
1059 return "activated";
1060 case CEC_MENU_STATE_DEACTIVATED:
1061 return "deactivated";
1062 default:
1063 return "unknown";
1064 }
1065 }
1066
1067 const char *CCECProcessor::ToString(const cec_version version)
1068 {
1069 switch (version)
1070 {
1071 case CEC_VERSION_1_2:
1072 return "1.2";
1073 case CEC_VERSION_1_2A:
1074 return "1.2a";
1075 case CEC_VERSION_1_3:
1076 return "1.3";
1077 case CEC_VERSION_1_3A:
1078 return "1.3a";
1079 case CEC_VERSION_1_4:
1080 return "1.4";
1081 default:
1082 return "unknown";
1083 }
1084 }
1085
1086 const char *CCECProcessor::ToString(const cec_power_status status)
1087 {
1088 switch (status)
1089 {
1090 case CEC_POWER_STATUS_ON:
1091 return "on";
1092 case CEC_POWER_STATUS_STANDBY:
1093 return "standby";
1094 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY:
1095 return "in transition from on to standby";
1096 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON:
1097 return "in transition from standby to on";
1098 default:
1099 return "unknown";
1100 }
1101 }
1102
1103 const char *CCECProcessor::ToString(const cec_logical_address address)
1104 {
1105 switch(address)
1106 {
1107 case CECDEVICE_AUDIOSYSTEM:
1108 return "Audio";
1109 case CECDEVICE_BROADCAST:
1110 return "Broadcast";
1111 case CECDEVICE_FREEUSE:
1112 return "Free use";
1113 case CECDEVICE_PLAYBACKDEVICE1:
1114 return "Playback 1";
1115 case CECDEVICE_PLAYBACKDEVICE2:
1116 return "Playback 2";
1117 case CECDEVICE_PLAYBACKDEVICE3:
1118 return "Playback 3";
1119 case CECDEVICE_RECORDINGDEVICE1:
1120 return "Recorder 1";
1121 case CECDEVICE_RECORDINGDEVICE2:
1122 return "Recorder 2";
1123 case CECDEVICE_RECORDINGDEVICE3:
1124 return "Recorder 3";
1125 case CECDEVICE_RESERVED1:
1126 return "Reserved 1";
1127 case CECDEVICE_RESERVED2:
1128 return "Reserved 2";
1129 case CECDEVICE_TUNER1:
1130 return "Tuner 1";
1131 case CECDEVICE_TUNER2:
1132 return "Tuner 2";
1133 case CECDEVICE_TUNER3:
1134 return "Tuner 3";
1135 case CECDEVICE_TUNER4:
1136 return "Tuner 4";
1137 case CECDEVICE_TV:
1138 return "TV";
1139 default:
1140 return "unknown";
1141 }
1142 }
1143
1144 const char *CCECProcessor::ToString(const cec_deck_control_mode mode)
1145 {
1146 switch (mode)
1147 {
1148 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND:
1149 return "skip forward wind";
1150 case CEC_DECK_CONTROL_MODE_EJECT:
1151 return "eject";
1152 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND:
1153 return "reverse rewind";
1154 case CEC_DECK_CONTROL_MODE_STOP:
1155 return "stop";
1156 default:
1157 return "unknown";
1158 }
1159 }
1160
1161 const char *CCECProcessor::ToString(const cec_deck_info status)
1162 {
1163 switch (status)
1164 {
1165 case CEC_DECK_INFO_PLAY:
1166 return "play";
1167 case CEC_DECK_INFO_RECORD:
1168 return "record";
1169 case CEC_DECK_INFO_PLAY_REVERSE:
1170 return "play reverse";
1171 case CEC_DECK_INFO_STILL:
1172 return "still";
1173 case CEC_DECK_INFO_SLOW:
1174 return "slow";
1175 case CEC_DECK_INFO_SLOW_REVERSE:
1176 return "slow reverse";
1177 case CEC_DECK_INFO_FAST_FORWARD:
1178 return "fast forward";
1179 case CEC_DECK_INFO_FAST_REVERSE:
1180 return "fast reverse";
1181 case CEC_DECK_INFO_NO_MEDIA:
1182 return "no media";
1183 case CEC_DECK_INFO_STOP:
1184 return "stop";
1185 case CEC_DECK_INFO_SKIP_FORWARD_WIND:
1186 return "info skip forward wind";
1187 case CEC_DECK_INFO_SKIP_REVERSE_REWIND:
1188 return "info skip reverse rewind";
1189 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD:
1190 return "info index search forward";
1191 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE:
1192 return "info index search reverse";
1193 case CEC_DECK_INFO_OTHER_STATUS:
1194 return "other";
1195 default:
1196 return "unknown";
1197 }
1198 }
1199
1200 const char *CCECProcessor::ToString(const cec_opcode opcode)
1201 {
1202 switch (opcode)
1203 {
1204 case CEC_OPCODE_ACTIVE_SOURCE:
1205 return "active source";
1206 case CEC_OPCODE_IMAGE_VIEW_ON:
1207 return "image view on";
1208 case CEC_OPCODE_TEXT_VIEW_ON:
1209 return "text view on";
1210 case CEC_OPCODE_INACTIVE_SOURCE:
1211 return "inactive source";
1212 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
1213 return "request active source";
1214 case CEC_OPCODE_ROUTING_CHANGE:
1215 return "routing change";
1216 case CEC_OPCODE_ROUTING_INFORMATION:
1217 return "routing information";
1218 case CEC_OPCODE_SET_STREAM_PATH:
1219 return "set stream path";
1220 case CEC_OPCODE_STANDBY:
1221 return "standby";
1222 case CEC_OPCODE_RECORD_OFF:
1223 return "record off";
1224 case CEC_OPCODE_RECORD_ON:
1225 return "record on";
1226 case CEC_OPCODE_RECORD_STATUS:
1227 return "record status";
1228 case CEC_OPCODE_RECORD_TV_SCREEN:
1229 return "record tv screen";
1230 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER:
1231 return "clear analogue timer";
1232 case CEC_OPCODE_CLEAR_DIGITAL_TIMER:
1233 return "clear digital timer";
1234 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER:
1235 return "clear external timer";
1236 case CEC_OPCODE_SET_ANALOGUE_TIMER:
1237 return "set analogue timer";
1238 case CEC_OPCODE_SET_DIGITAL_TIMER:
1239 return "set digital timer";
1240 case CEC_OPCODE_SET_EXTERNAL_TIMER:
1241 return "set external timer";
1242 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE:
1243 return "set timer program title";
1244 case CEC_OPCODE_TIMER_CLEARED_STATUS:
1245 return "timer cleared status";
1246 case CEC_OPCODE_TIMER_STATUS:
1247 return "timer status";
1248 case CEC_OPCODE_CEC_VERSION:
1249 return "cec version";
1250 case CEC_OPCODE_GET_CEC_VERSION:
1251 return "get cec version";
1252 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
1253 return "give physical address";
1254 case CEC_OPCODE_GET_MENU_LANGUAGE:
1255 return "get menu language";
1256 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS:
1257 return "report physical address";
1258 case CEC_OPCODE_SET_MENU_LANGUAGE:
1259 return "set menu language";
1260 case CEC_OPCODE_DECK_CONTROL:
1261 return "deck control";
1262 case CEC_OPCODE_DECK_STATUS:
1263 return "deck status";
1264 case CEC_OPCODE_GIVE_DECK_STATUS:
1265 return "give deck status";
1266 case CEC_OPCODE_PLAY:
1267 return "play";
1268 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS:
1269 return "give tuner status";
1270 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE:
1271 return "select analogue service";
1272 case CEC_OPCODE_SELECT_DIGITAL_SERVICE:
1273 return "set digital service";
1274 case CEC_OPCODE_TUNER_DEVICE_STATUS:
1275 return "tuner device status";
1276 case CEC_OPCODE_TUNER_STEP_DECREMENT:
1277 return "tuner step decrement";
1278 case CEC_OPCODE_TUNER_STEP_INCREMENT:
1279 return "tuner step increment";
1280 case CEC_OPCODE_DEVICE_VENDOR_ID:
1281 return "device vendor id";
1282 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
1283 return "give device vendor id";
1284 case CEC_OPCODE_VENDOR_COMMAND:
1285 return "vendor command";
1286 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
1287 return "vendor command with id";
1288 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN:
1289 return "vendor remote button down";
1290 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP:
1291 return "vendor remote button up";
1292 case CEC_OPCODE_SET_OSD_STRING:
1293 return "set osd string";
1294 case CEC_OPCODE_GIVE_OSD_NAME:
1295 return "give osd name";
1296 case CEC_OPCODE_SET_OSD_NAME:
1297 return "set osd name";
1298 case CEC_OPCODE_MENU_REQUEST:
1299 return "menu request";
1300 case CEC_OPCODE_MENU_STATUS:
1301 return "menu status";
1302 case CEC_OPCODE_USER_CONTROL_PRESSED:
1303 return "user control pressed";
1304 case CEC_OPCODE_USER_CONTROL_RELEASE:
1305 return "user control release";
1306 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
1307 return "give device power status";
1308 case CEC_OPCODE_REPORT_POWER_STATUS:
1309 return "report power status";
1310 case CEC_OPCODE_FEATURE_ABORT:
1311 return "feature abort";
1312 case CEC_OPCODE_ABORT:
1313 return "abort";
1314 case CEC_OPCODE_GIVE_AUDIO_STATUS:
1315 return "give audio status";
1316 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS:
1317 return "give audio mode status";
1318 case CEC_OPCODE_REPORT_AUDIO_STATUS:
1319 return "report audio status";
1320 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE:
1321 return "set system audio mode";
1322 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST:
1323 return "system audio mode request";
1324 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS:
1325 return "system audio mode status";
1326 case CEC_OPCODE_SET_AUDIO_RATE:
1327 return "set audio rate";
1328 case CEC_OPCODE_NONE:
1329 return "poll";
1330 default:
1331 return "UNKNOWN";
1332 }
1333 }
1334
1335 const char *CCECProcessor::ToString(const cec_system_audio_status mode)
1336 {
1337 switch(mode)
1338 {
1339 case CEC_SYSTEM_AUDIO_STATUS_ON:
1340 return "on";
1341 case CEC_SYSTEM_AUDIO_STATUS_OFF:
1342 return "off";
1343 default:
1344 return "unknown";
1345 }
1346 }
1347
1348 const char *CCECProcessor::ToString(const cec_audio_status UNUSED(status))
1349 {
1350 // TODO this is a mask
1351 return "TODO";
1352 }
1353
1354 const char *CCECProcessor::ToString(const cec_vendor_id vendor)
1355 {
1356 switch (vendor)
1357 {
1358 case CEC_VENDOR_SAMSUNG:
1359 return "Samsung";
1360 case CEC_VENDOR_LG:
1361 return "LG";
1362 case CEC_VENDOR_PANASONIC:
1363 return "Panasonic";
1364 case CEC_VENDOR_PIONEER:
1365 return "Pioneer";
1366 case CEC_VENDOR_ONKYO:
1367 return "Onkyo";
1368 case CEC_VENDOR_YAMAHA:
1369 return "Yamaha";
1370 case CEC_VENDOR_PHILIPS:
1371 return "Philips";
1372 case CEC_VENDOR_SONY:
1373 return "Sony";
1374 case CEC_VENDOR_TOSHIBA:
1375 return "Toshiba";
1376 default:
1377 return "Unknown";
1378 }
1379 }
1380
1381 const char *CCECProcessor::ToString(const cec_client_version version)
1382 {
1383 switch (version)
1384 {
1385 case CEC_CLIENT_VERSION_PRE_1_5:
1386 return "pre-1.5";
1387 case CEC_CLIENT_VERSION_1_5_0:
1388 return "1.5.0";
1389 case CEC_CLIENT_VERSION_1_5_1:
1390 return "1.5.1";
1391 case CEC_CLIENT_VERSION_1_5_2:
1392 return "1.5.2";
1393 case CEC_CLIENT_VERSION_1_5_3:
1394 return "1.5.3";
1395 case CEC_CLIENT_VERSION_1_6_0:
1396 return "1.6.0";
1397 default:
1398 return "Unknown";
1399 }
1400 }
1401
1402 const char *CCECProcessor::ToString(const cec_server_version version)
1403 {
1404 switch (version)
1405 {
1406 case CEC_SERVER_VERSION_PRE_1_5:
1407 return "pre-1.5";
1408 case CEC_SERVER_VERSION_1_5_0:
1409 return "1.5.0";
1410 case CEC_SERVER_VERSION_1_5_1:
1411 return "1.5.1";
1412 case CEC_SERVER_VERSION_1_5_2:
1413 return "1.5.2";
1414 case CEC_SERVER_VERSION_1_5_3:
1415 return "1.5.3";
1416 case CEC_SERVER_VERSION_1_6_0:
1417 return "1.6.0";
1418 default:
1419 return "Unknown";
1420 }
1421 }
1422
1423 void *CCECBusScan::Process(void)
1424 {
1425 CCECBusDevice *device(NULL);
1426 uint8_t iCounter(0);
1427
1428 while (!IsStopped())
1429 {
1430 if (++iCounter < 10)
1431 {
1432 Sleep(1000);
1433 continue;
1434 }
1435 for (unsigned int iPtr = 0; iPtr <= 11 && !IsStopped(); iPtr++)
1436 {
1437 device = m_processor->m_busDevices[iPtr];
1438 WaitUntilIdle();
1439 if (device && device->GetStatus(true) == CEC_DEVICE_STATUS_PRESENT)
1440 {
1441 WaitUntilIdle();
1442 if (!IsStopped())
1443 device->GetVendorId();
1444
1445 WaitUntilIdle();
1446 if (!IsStopped())
1447 device->GetPowerStatus(true);
1448 }
1449 }
1450 }
1451
1452 return NULL;
1453 }
1454
1455 void CCECBusScan::WaitUntilIdle(void)
1456 {
1457 if (IsStopped())
1458 return;
1459
1460 int32_t iWaitTime = 3000 - (int32_t)(GetTimeMs() - m_processor->GetLastTransmission());
1461 while (iWaitTime > 0)
1462 {
1463 Sleep(iWaitTime);
1464 iWaitTime = 3000 - (int32_t)(GetTimeMs() - m_processor->GetLastTransmission());
1465 }
1466 }
1467
1468 bool CCECProcessor::StartBootloader(const char *strPort /* = NULL */)
1469 {
1470 if (!m_communication && strPort)
1471 {
1472 bool bReturn(false);
1473 IAdapterCommunication *comm = new CUSBCECAdapterCommunication(this, strPort);
1474 CTimeout timeout(10000);
1475 int iConnectTry(0);
1476 while (timeout.TimeLeft() > 0 && (bReturn = comm->Open(NULL, (timeout.TimeLeft() / CEC_CONNECT_TRIES)), true) == false)
1477 {
1478 CLibCEC::AddLog(CEC_LOG_ERROR, "could not open a connection (try %d)", ++iConnectTry);
1479 comm->Close();
1480 Sleep(500);
1481 }
1482 if (comm->IsOpen())
1483 {
1484 bReturn = comm->StartBootloader();
1485 delete comm;
1486 }
1487 return bReturn;
1488 }
1489 else
1490 {
1491 return m_communication->StartBootloader();
1492 }
1493 }
1494
1495 bool CCECProcessor::PingAdapter(void)
1496 {
1497 return m_communication->PingAdapter();
1498 }
1499
1500 void CCECProcessor::HandlePoll(cec_logical_address initiator, cec_logical_address destination)
1501 {
1502 m_busDevices[destination]->HandlePoll(initiator);
1503 }
1504
1505 bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator)
1506 {
1507 return !m_busDevices[initiator]->HandleReceiveFailed();
1508 }
1509
1510 bool CCECProcessor::SetStreamPath(uint16_t iPhysicalAddress)
1511 {
1512 // stream path changes are sent by the TV
1513 return m_busDevices[CECDEVICE_TV]->GetHandler()->TransmitSetStreamPath(iPhysicalAddress);
1514 }
1515
1516 bool CCECProcessor::SetConfiguration(const libcec_configuration *configuration)
1517 {
1518 bool bReinit(false);
1519 CCECBusDevice *primary = IsRunning() ? GetPrimaryDevice() : NULL;
1520 cec_device_type oldPrimaryType = primary ? primary->GetType() : CEC_DEVICE_TYPE_RECORDING_DEVICE;
1521 m_configuration.clientVersion = configuration->clientVersion;
1522
1523 // client version 1.5.0
1524
1525 // device types
1526 bool bDeviceTypeChanged = IsRunning () && m_configuration.deviceTypes != configuration->deviceTypes;
1527 m_configuration.deviceTypes = configuration->deviceTypes;
1528
1529 bool bPhysicalAddressChanged(false);
1530
1531 // autodetect address
1532 bool bPhysicalAutodetected(false);
1533 if (IsRunning() && configuration->bAutodetectAddress == 1)
1534 {
1535 uint16_t iPhysicalAddress = m_communication->GetPhysicalAddress();
1536 if (iPhysicalAddress != 0)
1537 {
1538 if (IsRunning())
1539 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - autodetected physical address '%4x'", __FUNCTION__, iPhysicalAddress);
1540 bPhysicalAddressChanged = (m_configuration.iPhysicalAddress != iPhysicalAddress);
1541 m_configuration.iPhysicalAddress = iPhysicalAddress;
1542 m_configuration.iHDMIPort = 0;
1543 m_configuration.baseDevice = CECDEVICE_UNKNOWN;
1544 bPhysicalAutodetected = true;
1545 }
1546 }
1547
1548 // physical address
1549 if (!bPhysicalAutodetected)
1550 {
1551 if (configuration->iPhysicalAddress != 0)
1552 bPhysicalAddressChanged = IsRunning() && m_configuration.iPhysicalAddress != configuration->iPhysicalAddress;
1553 if (IsRunning())
1554 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - using physical address '%4x'", __FUNCTION__, configuration->iPhysicalAddress);
1555 m_configuration.iPhysicalAddress = configuration->iPhysicalAddress;
1556 }
1557
1558 bool bHdmiPortChanged(false);
1559 if (!bPhysicalAutodetected && !bPhysicalAddressChanged)
1560 {
1561 // base device
1562 bHdmiPortChanged = IsRunning() && m_configuration.baseDevice != configuration->baseDevice;
1563 if (IsRunning())
1564 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - using base device '%x'", __FUNCTION__, (int)configuration->baseDevice);
1565 m_configuration.baseDevice = configuration->baseDevice;
1566
1567 // hdmi port
1568 bHdmiPortChanged |= IsRunning() && m_configuration.iHDMIPort != configuration->iHDMIPort;
1569 if (IsRunning())
1570 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - using HDMI port '%d'", __FUNCTION__, configuration->iHDMIPort);
1571 m_configuration.iHDMIPort = configuration->iHDMIPort;
1572 }
1573 else
1574 {
1575 if (IsRunning())
1576 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - resetting HDMI port and base device to defaults", __FUNCTION__);
1577 m_configuration.baseDevice = CECDEVICE_UNKNOWN;
1578 m_configuration.iHDMIPort = 0;
1579 }
1580
1581 bReinit = bPhysicalAddressChanged || bHdmiPortChanged || bDeviceTypeChanged;
1582
1583 // device name
1584 snprintf(m_configuration.strDeviceName, 13, "%s", configuration->strDeviceName);
1585 if (primary && !primary->GetOSDName().Equals(m_configuration.strDeviceName))
1586 {
1587 primary->SetOSDName(m_configuration.strDeviceName);
1588 if (!bReinit && IsRunning())
1589 primary->TransmitOSDName(CECDEVICE_TV);
1590 }
1591
1592 // tv vendor id override
1593 if (m_configuration.tvVendor != configuration->tvVendor)
1594 {
1595 m_configuration.tvVendor= configuration->tvVendor;
1596 m_busDevices[CECDEVICE_TV]->SetVendorId((uint64_t)m_configuration.tvVendor);
1597 }
1598
1599 // wake CEC devices
1600 if (m_configuration.wakeDevices != configuration->wakeDevices)
1601 {
1602 m_configuration.wakeDevices = configuration->wakeDevices;
1603 if (!bReinit && IsRunning())
1604 PowerOnDevices();
1605 }
1606
1607 // just copy these
1608 m_configuration.clientVersion = configuration->clientVersion;
1609 m_configuration.bUseTVMenuLanguage = configuration->bUseTVMenuLanguage;
1610 m_configuration.bActivateSource = configuration->bActivateSource;
1611 m_configuration.bGetSettingsFromROM = configuration->bGetSettingsFromROM;
1612 m_configuration.powerOffDevices = configuration->powerOffDevices;
1613 m_configuration.bPowerOffScreensaver = configuration->bPowerOffScreensaver;
1614 m_configuration.bPowerOffOnStandby = configuration->bPowerOffOnStandby;
1615
1616 // client version 1.5.1
1617 if (configuration->clientVersion >= CEC_CLIENT_VERSION_1_5_1)
1618 m_configuration.bSendInactiveSource = configuration->bSendInactiveSource;
1619
1620 // ensure that there is at least 1 device type set
1621 if (m_configuration.deviceTypes.IsEmpty())
1622 m_configuration.deviceTypes.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
1623
1624 if (bReinit)
1625 {
1626 if (bDeviceTypeChanged)
1627 return ChangeDeviceType(oldPrimaryType, m_configuration.deviceTypes[0]);
1628 else if (bPhysicalAddressChanged)
1629 return SetPhysicalAddress(m_configuration.iPhysicalAddress);
1630 else
1631 return SetHDMIPort(m_configuration.baseDevice, m_configuration.iHDMIPort);
1632 }
1633 else if (m_configuration.bActivateSource == 1 && IsRunning() && !IsActiveSource(m_configuration.logicalAddresses.primary))
1634 {
1635 // activate the source if we're not already the active source
1636 SetActiveSource(m_configuration.deviceTypes.types[0]);
1637 }
1638
1639 return true;
1640 }
1641
1642 bool CCECProcessor::GetCurrentConfiguration(libcec_configuration *configuration)
1643 {
1644 // client version 1.5.0
1645 snprintf(configuration->strDeviceName, 13, "%s", m_configuration.strDeviceName);
1646 configuration->deviceTypes = m_configuration.deviceTypes;
1647 configuration->bAutodetectAddress = m_configuration.bAutodetectAddress;
1648 configuration->iPhysicalAddress = m_configuration.iPhysicalAddress;
1649 configuration->baseDevice = m_configuration.baseDevice;
1650 configuration->iHDMIPort = m_configuration.iHDMIPort;
1651 configuration->clientVersion = m_configuration.clientVersion;
1652 configuration->serverVersion = m_configuration.serverVersion;
1653 configuration->tvVendor = m_configuration.tvVendor;
1654
1655 configuration->bGetSettingsFromROM = m_configuration.bGetSettingsFromROM;
1656 configuration->bUseTVMenuLanguage = m_configuration.bUseTVMenuLanguage;
1657 configuration->bActivateSource = m_configuration.bActivateSource;
1658 configuration->wakeDevices = m_configuration.wakeDevices;
1659 configuration->powerOffDevices = m_configuration.powerOffDevices;
1660 configuration->bPowerOffScreensaver = m_configuration.bPowerOffScreensaver;
1661 configuration->bPowerOffOnStandby = m_configuration.bPowerOffOnStandby;
1662
1663 // client version 1.5.1
1664 if (configuration->clientVersion >= CEC_CLIENT_VERSION_1_5_1)
1665 configuration->bSendInactiveSource = m_configuration.bSendInactiveSource;
1666
1667 // client version 1.5.3
1668 if (configuration->clientVersion >= CEC_CLIENT_VERSION_1_5_3)
1669 configuration->logicalAddresses = m_configuration.logicalAddresses;
1670
1671 // client version 1.6.0
1672 if (configuration->clientVersion >= CEC_CLIENT_VERSION_1_5_3)
1673 configuration->logicalAddresses = m_configuration.logicalAddresses;
1674
1675 return true;
1676 }
1677
1678 bool CCECProcessor::CanPersistConfiguration(void)
1679 {
1680 return m_communication->GetFirmwareVersion() >= 2;
1681 }
1682
1683 bool CCECProcessor::PersistConfiguration(libcec_configuration *configuration)
1684 {
1685 return m_communication->PersistConfiguration(configuration);
1686 }
1687
1688 void CCECProcessor::RescanActiveDevices(void)
1689 {
1690 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
1691 m_busDevices[iPtr]->GetStatus(true);
1692 }
1693
1694 bool CCECProcessor::GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs /* = 10000 */)
1695 {
1696 if (!OpenConnection(strPort, 38400, iTimeoutMs, false))
1697 return false;
1698
1699 config->iFirmwareVersion = m_communication->GetFirmwareVersion();
1700 config->iPhysicalAddress = m_communication->GetPhysicalAddress();
1701
1702 delete m_communication;
1703 m_communication = NULL;
1704 return true;
1705 }