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