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