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