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