cec: added poweroff devices parameter to the config (instead of a boolean parameter...
[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 ReplaceHandlers();
444 if (m_commandBuffer.Pop(command))
445 ParseCommand(command);
446
447 m_controller->CheckKeypressTimeout();
448 Sleep(5);
449 }
450
451 return NULL;
452 }
453
454 bool CCECProcessor::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RESERVED */)
455 {
456 bool bReturn(false);
457
458 if (!IsRunning())
459 return bReturn;
460
461 cec_logical_address addr = m_logicalAddresses.primary;
462
463 if (type != CEC_DEVICE_TYPE_RESERVED)
464 {
465 for (uint8_t iPtr = 0; iPtr <= 11; iPtr++)
466 {
467 if (m_logicalAddresses[iPtr] && m_busDevices[iPtr]->m_type == type)
468 {
469 addr = (cec_logical_address) iPtr;
470 break;
471 }
472 }
473 }
474
475 m_busDevices[addr]->SetActiveSource();
476 if (m_busDevices[addr]->GetPhysicalAddress(false) != 0xFFFF)
477 bReturn = m_busDevices[addr]->ActivateSource();
478
479 return bReturn;
480 }
481
482 bool CCECProcessor::SetActiveSource(uint16_t iStreamPath)
483 {
484 bool bReturn(false);
485
486 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamPath);
487 if (device)
488 {
489 device->SetActiveSource();
490 bReturn = true;
491 }
492
493 return bReturn;
494 }
495
496 void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout)
497 {
498 CLockObject lock(m_mutex);
499 m_iStandardLineTimeout = iTimeout;
500 }
501
502 void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout)
503 {
504 CLockObject lock(m_mutex);
505 m_iRetryLineTimeout = iTimeout;
506 }
507
508 bool CCECProcessor::SetActiveView(void)
509 {
510 return SetActiveSource(m_configuration.deviceTypes.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED : m_configuration.deviceTypes[0]);
511 }
512
513 bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode, bool bSendUpdate /* = true */)
514 {
515 bool bReturn(false);
516
517 CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
518 if (device)
519 {
520 ((CCECPlaybackDevice *) device)->SetDeckControlMode(mode);
521 if (bSendUpdate)
522 ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV);
523 bReturn = true;
524 }
525
526 return bReturn;
527 }
528
529 bool CCECProcessor::SetDeckInfo(cec_deck_info info, bool bSendUpdate /* = true */)
530 {
531 bool bReturn(false);
532
533 CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
534 if (device)
535 {
536 ((CCECPlaybackDevice *) device)->SetDeckStatus(info);
537 if (bSendUpdate)
538 ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV);
539 bReturn = true;
540 }
541
542 return bReturn;
543 }
544
545 bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice, uint8_t iPort, bool bForce /* = false */)
546 {
547 bool bReturn(false);
548 CLockObject lock(m_mutex);
549
550 m_configuration.baseDevice = iBaseDevice;
551 m_configuration.iHDMIPort = iPort;
552 if (!IsRunning() && !bForce)
553 return true;
554
555 CLibCEC::AddLog(CEC_LOG_DEBUG, "setting HDMI port to %d on device %s (%d)", iPort, ToString(iBaseDevice), (int)iBaseDevice);
556
557 uint16_t iPhysicalAddress(0);
558 if (iBaseDevice > CECDEVICE_TV)
559 {
560 lock.Unlock();
561 iPhysicalAddress = m_busDevices[iBaseDevice]->GetPhysicalAddress();
562 lock.Lock();
563 }
564
565 if (iPhysicalAddress < 0xffff)
566 {
567 if (iPhysicalAddress == 0)
568 iPhysicalAddress += 0x1000 * iPort;
569 else if (iPhysicalAddress % 0x1000 == 0)
570 iPhysicalAddress += 0x100 * iPort;
571 else if (iPhysicalAddress % 0x100 == 0)
572 iPhysicalAddress += 0x10 * iPort;
573 else if (iPhysicalAddress % 0x10 == 0)
574 iPhysicalAddress += iPort;
575
576 bReturn = true;
577 }
578
579 if (!bReturn)
580 CLibCEC::AddLog(CEC_LOG_ERROR, "failed to set the physical address");
581 else
582 {
583 lock.Unlock();
584 SetPhysicalAddress(iPhysicalAddress);
585 }
586
587 return bReturn;
588 }
589
590 bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress)
591 {
592 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
593 {
594 if (m_busDevices[iPtr]->GetPhysicalAddress(false) == iPhysicalAddress)
595 return true;
596 }
597 return false;
598 }
599
600 bool CCECProcessor::TransmitInactiveSource(void)
601 {
602 if (!IsRunning())
603 return false;
604
605 if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary])
606 return m_busDevices[m_logicalAddresses.primary]->TransmitInactiveSource();
607 return false;
608 }
609
610 void CCECProcessor::LogOutput(const cec_command &data)
611 {
612 CStdString strTx;
613 strTx.Format("<< %02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination);
614 if (data.opcode_set)
615 strTx.AppendFormat(":%02x", (uint8_t)data.opcode);
616
617 for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++)
618 strTx.AppendFormat(":%02x", data.parameters[iPtr]);
619 CLibCEC::AddLog(CEC_LOG_TRAFFIC, strTx.c_str());
620 }
621
622 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress)
623 {
624 CLockObject lock(m_mutex);
625 if (m_logicalAddresses.primary != iLogicalAddress)
626 {
627 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< setting primary logical address to %1x", iLogicalAddress);
628 m_logicalAddresses.primary = iLogicalAddress;
629 m_logicalAddresses.Set(iLogicalAddress);
630 return SetAckMask(m_logicalAddresses.AckMask());
631 }
632
633 return true;
634 }
635
636 bool CCECProcessor::SetMenuState(cec_menu_state state, bool bSendUpdate /* = true */)
637 {
638 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
639 {
640 if (m_logicalAddresses[iPtr])
641 m_busDevices[iPtr]->SetMenuState(state);
642 }
643
644 if (bSendUpdate)
645 m_busDevices[m_logicalAddresses.primary]->TransmitMenuState(CECDEVICE_TV);
646
647 return true;
648 }
649
650 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress, bool bSendUpdate /* = true */)
651 {
652 bool bSendActiveView(false);
653 bool bReturn(false);
654 cec_logical_addresses sendUpdatesTo;
655 sendUpdatesTo.Clear();
656
657 {
658 CLockObject lock(m_mutex);
659 m_configuration.iPhysicalAddress = iPhysicalAddress;
660
661 if (!m_logicalAddresses.IsEmpty())
662 {
663 bool bWasActiveSource(false);
664 for (uint8_t iPtr = 0; iPtr < 15; iPtr++)
665 if (m_logicalAddresses[iPtr])
666 {
667 bWasActiveSource |= m_busDevices[iPtr]->IsActiveSource();
668 m_busDevices[iPtr]->SetInactiveSource();
669 m_busDevices[iPtr]->SetPhysicalAddress(iPhysicalAddress);
670 if (bSendUpdate)
671 sendUpdatesTo.Set((cec_logical_address)iPtr);
672 }
673
674 bSendActiveView = bWasActiveSource && bSendUpdate;
675 bReturn = true;
676 }
677 }
678
679 for (uint8_t iPtr = 0; iPtr < 15; iPtr++)
680 if (sendUpdatesTo[iPtr])
681 m_busDevices[iPtr]->TransmitPhysicalAddress();
682
683 if (bSendActiveView)
684 SetActiveView();
685
686 if (bReturn)
687 CLibCEC::ConfigurationChanged(m_configuration);
688
689 return bReturn;
690 }
691
692 bool CCECProcessor::SwitchMonitoring(bool bEnable)
693 {
694 CLibCEC::AddLog(CEC_LOG_NOTICE, "== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
695
696 {
697 CLockObject lock(m_mutex);
698 m_bMonitor = bEnable;
699 }
700
701 if (bEnable)
702 return SetAckMask(0);
703 else
704 return SetAckMask(m_logicalAddresses.AckMask());
705 }
706
707 bool CCECProcessor::PollDevice(cec_logical_address iAddress)
708 {
709 if (iAddress != CECDEVICE_UNKNOWN && m_busDevices[iAddress])
710 {
711 return m_logicalAddresses.primary == CECDEVICE_UNKNOWN ?
712 m_busDevices[iAddress]->TransmitPoll(iAddress) :
713 m_busDevices[m_logicalAddresses.primary]->TransmitPoll(iAddress);
714 }
715 return false;
716 }
717
718 uint8_t CCECProcessor::VolumeUp(bool bSendRelease /* = true */)
719 {
720 uint8_t status = 0;
721 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM))
722 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeUp(bSendRelease);
723
724 return status;
725 }
726
727 uint8_t CCECProcessor::VolumeDown(bool bSendRelease /* = true */)
728 {
729 uint8_t status = 0;
730 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM))
731 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeDown(bSendRelease);
732
733 return status;
734 }
735
736 uint8_t CCECProcessor::MuteAudio(bool bSendRelease /* = true */)
737 {
738 uint8_t status = 0;
739 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM))
740 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->MuteAudio(bSendRelease);
741
742 return status;
743 }
744
745 CCECBusDevice *CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bRefresh /* = false */) const
746 {
747 if (m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false) == iPhysicalAddress)
748 return m_busDevices[m_logicalAddresses.primary];
749
750 CCECBusDevice *device = NULL;
751 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
752 {
753 if (m_busDevices[iPtr]->GetPhysicalAddress(bRefresh) == iPhysicalAddress)
754 {
755 device = m_busDevices[iPtr];
756 break;
757 }
758 }
759
760 return device;
761 }
762
763 CCECBusDevice *CCECProcessor::GetDeviceByType(cec_device_type type) const
764 {
765 CCECBusDevice *device = NULL;
766
767 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
768 {
769 if (m_busDevices[iPtr]->m_type == type && m_logicalAddresses[iPtr])
770 {
771 device = m_busDevices[iPtr];
772 break;
773 }
774 }
775
776 return device;
777 }
778
779 CCECBusDevice *CCECProcessor::GetPrimaryDevice(void) const
780 {
781 CCECBusDevice *device(NULL);
782 cec_logical_address primary = m_logicalAddresses.primary;
783 if (primary != CECDEVICE_UNKNOWN)
784 device = m_busDevices[primary];
785 return device;
786 }
787
788 cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress)
789 {
790 return m_busDevices[iAddress]->GetCecVersion();
791 }
792
793 cec_osd_name CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress)
794 {
795 CStdString strOSDName = m_busDevices[iAddress]->GetOSDName();
796 cec_osd_name retVal;
797
798 snprintf(retVal.name, sizeof(retVal.name), "%s", strOSDName.c_str());
799 retVal.device = iAddress;
800
801 return retVal;
802 }
803
804 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
805 {
806 if (m_busDevices[iAddress])
807 {
808 *language = m_busDevices[iAddress]->GetMenuLanguage();
809 return (strcmp(language->language, "???") != 0);
810 }
811 return false;
812 }
813
814 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress)
815 {
816 if (m_busDevices[iAddress])
817 return m_busDevices[iAddress]->GetVendorId();
818 return false;
819 }
820
821 uint16_t CCECProcessor::GetDevicePhysicalAddress(cec_logical_address iAddress)
822 {
823 if (m_busDevices[iAddress])
824 return m_busDevices[iAddress]->GetPhysicalAddress(false);
825 return false;
826 }
827
828 cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress)
829 {
830 if (m_busDevices[iAddress])
831 return m_busDevices[iAddress]->GetPowerStatus();
832 return CEC_POWER_STATUS_UNKNOWN;
833 }
834
835 cec_logical_address CCECProcessor::GetActiveSource(void)
836 {
837 for (uint8_t iPtr = 0; iPtr <= 11; iPtr++)
838 {
839 if (m_busDevices[iPtr]->IsActiveSource())
840 return (cec_logical_address)iPtr;
841 }
842
843 return CECDEVICE_UNKNOWN;
844 }
845
846 bool CCECProcessor::IsActiveSource(cec_logical_address iAddress)
847 {
848 return m_busDevices[iAddress]->IsActiveSource();
849 }
850
851 bool CCECProcessor::Transmit(const cec_command &data)
852 {
853 cec_adapter_message_state retVal(ADAPTER_MESSAGE_STATE_UNKNOWN);
854 {
855 CLockObject lock(m_mutex);
856 LogOutput(data);
857 m_iLastTransmission = GetTimeMs();
858 if (!m_communication)
859 return false;
860 uint8_t iMaxTries = m_busDevices[data.initiator]->GetHandler()->GetTransmitRetries() + 1;
861 retVal = m_communication->Write(data, iMaxTries, m_iLineTimeout, m_iRetryLineTimeout);
862 }
863
864 /* set to "not present" on failed ack */
865 if (retVal == ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED &&
866 data.destination != CECDEVICE_BROADCAST)
867 m_busDevices[data.destination]->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT);
868
869 return retVal == ADAPTER_MESSAGE_STATE_SENT_ACKED;
870 }
871
872 void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, cec_abort_reason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
873 {
874 CLibCEC::AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
875
876 cec_command command;
877 // TODO
878 cec_command::Format(command, m_logicalAddresses.primary, address, CEC_OPCODE_FEATURE_ABORT);
879 command.parameters.PushBack((uint8_t)opcode);
880 command.parameters.PushBack((uint8_t)reason);
881
882 Transmit(command);
883 }
884
885 void CCECProcessor::ParseCommand(const cec_command &command)
886 {
887 CStdString dataStr;
888 dataStr.Format(">> %1x%1x:%02x", command.initiator, command.destination, command.opcode);
889 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
890 dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
891 CLibCEC::AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
892
893 if (!m_bMonitor && command.initiator >= CECDEVICE_TV && command.initiator <= CECDEVICE_BROADCAST)
894 m_busDevices[(uint8_t)command.initiator]->HandleCommand(command);
895 }
896
897 cec_logical_addresses CCECProcessor::GetActiveDevices(void)
898 {
899 cec_logical_addresses addresses;
900 addresses.Clear();
901 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
902 {
903 if (m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
904 addresses.Set((cec_logical_address) iPtr);
905 }
906 return addresses;
907 }
908
909 bool CCECProcessor::IsPresentDevice(cec_logical_address address)
910 {
911 return m_busDevices[address]->GetStatus() == CEC_DEVICE_STATUS_PRESENT;
912 }
913
914 bool CCECProcessor::IsPresentDeviceType(cec_device_type type)
915 {
916 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
917 {
918 if (m_busDevices[iPtr]->GetType() == type && m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
919 return true;
920 }
921
922 return false;
923 }
924
925 uint16_t CCECProcessor::GetPhysicalAddress(void) const
926 {
927 if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary])
928 return m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false);
929 return false;
930 }
931
932 bool CCECProcessor::SetAckMask(uint16_t iMask)
933 {
934 return m_communication->SetAckMask(iMask);
935 }
936
937 bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
938 {
939 return m_busDevices[iDestination]->TransmitKeypress(key, bWait);
940 }
941
942 bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination, bool bWait /* = true */)
943 {
944 return m_busDevices[iDestination]->TransmitKeyRelease(bWait);
945 }
946
947 bool CCECProcessor::StandbyDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
948 {
949 if (address == CECDEVICE_BROADCAST && m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_0)
950 {
951 bool bReturn(true);
952 for (uint8_t iPtr = 0; iPtr <= 0xF; iPtr++)
953 {
954 if (m_configuration.powerOffDevices[iPtr])
955 bReturn &= m_busDevices[iPtr]->Standby();
956 }
957 return bReturn;
958 }
959
960 return m_busDevices[address]->Standby();
961 }
962
963 bool CCECProcessor::PowerOnDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
964 {
965 if (address == CECDEVICE_BROADCAST && m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_0)
966 {
967 bool bReturn(true);
968 for (uint8_t iPtr = 0; iPtr <= 0xF; iPtr++)
969 {
970 if (m_configuration.powerOffDevices[iPtr])
971 bReturn &= m_busDevices[iPtr]->PowerOn();
972 }
973 return bReturn;
974 }
975
976 return m_busDevices[address]->PowerOn();
977 }
978
979 const char *CCECProcessor::ToString(const cec_device_type type)
980 {
981 switch (type)
982 {
983 case CEC_DEVICE_TYPE_AUDIO_SYSTEM:
984 return "audio system";
985 case CEC_DEVICE_TYPE_PLAYBACK_DEVICE:
986 return "playback device";
987 case CEC_DEVICE_TYPE_RECORDING_DEVICE:
988 return "recording device";
989 case CEC_DEVICE_TYPE_RESERVED:
990 return "reserved";
991 case CEC_DEVICE_TYPE_TUNER:
992 return "tuner";
993 case CEC_DEVICE_TYPE_TV:
994 return "TV";
995 default:
996 return "unknown";
997 }
998 }
999
1000 const char *CCECProcessor::ToString(const cec_menu_state state)
1001 {
1002 switch (state)
1003 {
1004 case CEC_MENU_STATE_ACTIVATED:
1005 return "activated";
1006 case CEC_MENU_STATE_DEACTIVATED:
1007 return "deactivated";
1008 default:
1009 return "unknown";
1010 }
1011 }
1012
1013 const char *CCECProcessor::ToString(const cec_version version)
1014 {
1015 switch (version)
1016 {
1017 case CEC_VERSION_1_2:
1018 return "1.2";
1019 case CEC_VERSION_1_2A:
1020 return "1.2a";
1021 case CEC_VERSION_1_3:
1022 return "1.3";
1023 case CEC_VERSION_1_3A:
1024 return "1.3a";
1025 case CEC_VERSION_1_4:
1026 return "1.4";
1027 default:
1028 return "unknown";
1029 }
1030 }
1031
1032 const char *CCECProcessor::ToString(const cec_power_status status)
1033 {
1034 switch (status)
1035 {
1036 case CEC_POWER_STATUS_ON:
1037 return "on";
1038 case CEC_POWER_STATUS_STANDBY:
1039 return "standby";
1040 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY:
1041 return "in transition from on to standby";
1042 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON:
1043 return "in transition from standby to on";
1044 default:
1045 return "unknown";
1046 }
1047 }
1048
1049 const char *CCECProcessor::ToString(const cec_logical_address address)
1050 {
1051 switch(address)
1052 {
1053 case CECDEVICE_AUDIOSYSTEM:
1054 return "Audio";
1055 case CECDEVICE_BROADCAST:
1056 return "Broadcast";
1057 case CECDEVICE_FREEUSE:
1058 return "Free use";
1059 case CECDEVICE_PLAYBACKDEVICE1:
1060 return "Playback 1";
1061 case CECDEVICE_PLAYBACKDEVICE2:
1062 return "Playback 2";
1063 case CECDEVICE_PLAYBACKDEVICE3:
1064 return "Playback 3";
1065 case CECDEVICE_RECORDINGDEVICE1:
1066 return "Recorder 1";
1067 case CECDEVICE_RECORDINGDEVICE2:
1068 return "Recorder 2";
1069 case CECDEVICE_RECORDINGDEVICE3:
1070 return "Recorder 3";
1071 case CECDEVICE_RESERVED1:
1072 return "Reserved 1";
1073 case CECDEVICE_RESERVED2:
1074 return "Reserved 2";
1075 case CECDEVICE_TUNER1:
1076 return "Tuner 1";
1077 case CECDEVICE_TUNER2:
1078 return "Tuner 2";
1079 case CECDEVICE_TUNER3:
1080 return "Tuner 3";
1081 case CECDEVICE_TUNER4:
1082 return "Tuner 4";
1083 case CECDEVICE_TV:
1084 return "TV";
1085 default:
1086 return "unknown";
1087 }
1088 }
1089
1090 const char *CCECProcessor::ToString(const cec_deck_control_mode mode)
1091 {
1092 switch (mode)
1093 {
1094 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND:
1095 return "skip forward wind";
1096 case CEC_DECK_CONTROL_MODE_EJECT:
1097 return "eject";
1098 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND:
1099 return "reverse rewind";
1100 case CEC_DECK_CONTROL_MODE_STOP:
1101 return "stop";
1102 default:
1103 return "unknown";
1104 }
1105 }
1106
1107 const char *CCECProcessor::ToString(const cec_deck_info status)
1108 {
1109 switch (status)
1110 {
1111 case CEC_DECK_INFO_PLAY:
1112 return "play";
1113 case CEC_DECK_INFO_RECORD:
1114 return "record";
1115 case CEC_DECK_INFO_PLAY_REVERSE:
1116 return "play reverse";
1117 case CEC_DECK_INFO_STILL:
1118 return "still";
1119 case CEC_DECK_INFO_SLOW:
1120 return "slow";
1121 case CEC_DECK_INFO_SLOW_REVERSE:
1122 return "slow reverse";
1123 case CEC_DECK_INFO_FAST_FORWARD:
1124 return "fast forward";
1125 case CEC_DECK_INFO_FAST_REVERSE:
1126 return "fast reverse";
1127 case CEC_DECK_INFO_NO_MEDIA:
1128 return "no media";
1129 case CEC_DECK_INFO_STOP:
1130 return "stop";
1131 case CEC_DECK_INFO_SKIP_FORWARD_WIND:
1132 return "info skip forward wind";
1133 case CEC_DECK_INFO_SKIP_REVERSE_REWIND:
1134 return "info skip reverse rewind";
1135 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD:
1136 return "info index search forward";
1137 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE:
1138 return "info index search reverse";
1139 case CEC_DECK_INFO_OTHER_STATUS:
1140 return "other";
1141 default:
1142 return "unknown";
1143 }
1144 }
1145
1146 const char *CCECProcessor::ToString(const cec_opcode opcode)
1147 {
1148 switch (opcode)
1149 {
1150 case CEC_OPCODE_ACTIVE_SOURCE:
1151 return "active source";
1152 case CEC_OPCODE_IMAGE_VIEW_ON:
1153 return "image view on";
1154 case CEC_OPCODE_TEXT_VIEW_ON:
1155 return "text view on";
1156 case CEC_OPCODE_INACTIVE_SOURCE:
1157 return "inactive source";
1158 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
1159 return "request active source";
1160 case CEC_OPCODE_ROUTING_CHANGE:
1161 return "routing change";
1162 case CEC_OPCODE_ROUTING_INFORMATION:
1163 return "routing information";
1164 case CEC_OPCODE_SET_STREAM_PATH:
1165 return "set stream path";
1166 case CEC_OPCODE_STANDBY:
1167 return "standby";
1168 case CEC_OPCODE_RECORD_OFF:
1169 return "record off";
1170 case CEC_OPCODE_RECORD_ON:
1171 return "record on";
1172 case CEC_OPCODE_RECORD_STATUS:
1173 return "record status";
1174 case CEC_OPCODE_RECORD_TV_SCREEN:
1175 return "record tv screen";
1176 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER:
1177 return "clear analogue timer";
1178 case CEC_OPCODE_CLEAR_DIGITAL_TIMER:
1179 return "clear digital timer";
1180 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER:
1181 return "clear external timer";
1182 case CEC_OPCODE_SET_ANALOGUE_TIMER:
1183 return "set analogue timer";
1184 case CEC_OPCODE_SET_DIGITAL_TIMER:
1185 return "set digital timer";
1186 case CEC_OPCODE_SET_EXTERNAL_TIMER:
1187 return "set external timer";
1188 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE:
1189 return "set timer program title";
1190 case CEC_OPCODE_TIMER_CLEARED_STATUS:
1191 return "timer cleared status";
1192 case CEC_OPCODE_TIMER_STATUS:
1193 return "timer status";
1194 case CEC_OPCODE_CEC_VERSION:
1195 return "cec version";
1196 case CEC_OPCODE_GET_CEC_VERSION:
1197 return "get cec version";
1198 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
1199 return "give physical address";
1200 case CEC_OPCODE_GET_MENU_LANGUAGE:
1201 return "get menu language";
1202 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS:
1203 return "report physical address";
1204 case CEC_OPCODE_SET_MENU_LANGUAGE:
1205 return "set menu language";
1206 case CEC_OPCODE_DECK_CONTROL:
1207 return "deck control";
1208 case CEC_OPCODE_DECK_STATUS:
1209 return "deck status";
1210 case CEC_OPCODE_GIVE_DECK_STATUS:
1211 return "give deck status";
1212 case CEC_OPCODE_PLAY:
1213 return "play";
1214 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS:
1215 return "give tuner status";
1216 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE:
1217 return "select analogue service";
1218 case CEC_OPCODE_SELECT_DIGITAL_SERVICE:
1219 return "set digital service";
1220 case CEC_OPCODE_TUNER_DEVICE_STATUS:
1221 return "tuner device status";
1222 case CEC_OPCODE_TUNER_STEP_DECREMENT:
1223 return "tuner step decrement";
1224 case CEC_OPCODE_TUNER_STEP_INCREMENT:
1225 return "tuner step increment";
1226 case CEC_OPCODE_DEVICE_VENDOR_ID:
1227 return "device vendor id";
1228 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
1229 return "give device vendor id";
1230 case CEC_OPCODE_VENDOR_COMMAND:
1231 return "vendor command";
1232 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
1233 return "vendor command with id";
1234 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN:
1235 return "vendor remote button down";
1236 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP:
1237 return "vendor remote button up";
1238 case CEC_OPCODE_SET_OSD_STRING:
1239 return "set osd string";
1240 case CEC_OPCODE_GIVE_OSD_NAME:
1241 return "give osd name";
1242 case CEC_OPCODE_SET_OSD_NAME:
1243 return "set osd name";
1244 case CEC_OPCODE_MENU_REQUEST:
1245 return "menu request";
1246 case CEC_OPCODE_MENU_STATUS:
1247 return "menu status";
1248 case CEC_OPCODE_USER_CONTROL_PRESSED:
1249 return "user control pressed";
1250 case CEC_OPCODE_USER_CONTROL_RELEASE:
1251 return "user control release";
1252 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
1253 return "give device power status";
1254 case CEC_OPCODE_REPORT_POWER_STATUS:
1255 return "report power status";
1256 case CEC_OPCODE_FEATURE_ABORT:
1257 return "feature abort";
1258 case CEC_OPCODE_ABORT:
1259 return "abort";
1260 case CEC_OPCODE_GIVE_AUDIO_STATUS:
1261 return "give audio status";
1262 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS:
1263 return "give audio mode status";
1264 case CEC_OPCODE_REPORT_AUDIO_STATUS:
1265 return "report audio status";
1266 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE:
1267 return "set system audio mode";
1268 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST:
1269 return "system audio mode request";
1270 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS:
1271 return "system audio mode status";
1272 case CEC_OPCODE_SET_AUDIO_RATE:
1273 return "set audio rate";
1274 default:
1275 return "UNKNOWN";
1276 }
1277 }
1278
1279 const char *CCECProcessor::ToString(const cec_system_audio_status mode)
1280 {
1281 switch(mode)
1282 {
1283 case CEC_SYSTEM_AUDIO_STATUS_ON:
1284 return "on";
1285 case CEC_SYSTEM_AUDIO_STATUS_OFF:
1286 return "off";
1287 default:
1288 return "unknown";
1289 }
1290 }
1291
1292 const char *CCECProcessor::ToString(const cec_audio_status UNUSED(status))
1293 {
1294 // TODO this is a mask
1295 return "TODO";
1296 }
1297
1298 const char *CCECProcessor::ToString(const cec_vendor_id vendor)
1299 {
1300 switch (vendor)
1301 {
1302 case CEC_VENDOR_SAMSUNG:
1303 return "Samsung";
1304 case CEC_VENDOR_LG:
1305 return "LG";
1306 case CEC_VENDOR_PANASONIC:
1307 return "Panasonic";
1308 case CEC_VENDOR_PIONEER:
1309 return "Pioneer";
1310 case CEC_VENDOR_ONKYO:
1311 return "Onkyo";
1312 case CEC_VENDOR_YAMAHA:
1313 return "Yamaha";
1314 case CEC_VENDOR_PHILIPS:
1315 return "Philips";
1316 case CEC_VENDOR_SONY:
1317 return "Sony";
1318 default:
1319 return "Unknown";
1320 }
1321 }
1322
1323 const char *CCECProcessor::ToString(const cec_client_version version)
1324 {
1325 switch (version)
1326 {
1327 case CEC_CLIENT_VERSION_PRE_1_5:
1328 return "pre-1.5";
1329 case CEC_CLIENT_VERSION_1_5_0:
1330 return "1.5.0";
1331 default:
1332 return "Unknown";
1333 }
1334 }
1335
1336 void *CCECBusScan::Process(void)
1337 {
1338 CCECBusDevice *device(NULL);
1339 uint8_t iCounter(0);
1340
1341 while (!IsStopped())
1342 {
1343 if (++iCounter < 10)
1344 {
1345 Sleep(1000);
1346 continue;
1347 }
1348 for (unsigned int iPtr = 0; iPtr <= 11 && !IsStopped(); iPtr++)
1349 {
1350 device = m_processor->m_busDevices[iPtr];
1351 WaitUntilIdle();
1352 if (device && device->GetStatus(true) == CEC_DEVICE_STATUS_PRESENT)
1353 {
1354 WaitUntilIdle();
1355 if (!IsStopped())
1356 device->GetVendorId();
1357
1358 WaitUntilIdle();
1359 if (!IsStopped())
1360 device->GetPowerStatus(true);
1361 }
1362 }
1363 }
1364
1365 return NULL;
1366 }
1367
1368 void CCECBusScan::WaitUntilIdle(void)
1369 {
1370 if (IsStopped())
1371 return;
1372
1373 int32_t iWaitTime = 3000 - (int32_t)(GetTimeMs() - m_processor->GetLastTransmission());
1374 while (iWaitTime > 0)
1375 {
1376 Sleep(iWaitTime);
1377 iWaitTime = 3000 - (int32_t)(GetTimeMs() - m_processor->GetLastTransmission());
1378 }
1379 }
1380
1381 bool CCECProcessor::StartBootloader(void)
1382 {
1383 return m_communication->StartBootloader();
1384 }
1385
1386 bool CCECProcessor::PingAdapter(void)
1387 {
1388 return m_communication->PingAdapter();
1389 }
1390
1391 void CCECProcessor::HandlePoll(cec_logical_address initiator, cec_logical_address destination)
1392 {
1393 m_busDevices[initiator]->HandlePoll(destination);
1394 }
1395
1396 bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator)
1397 {
1398 return !m_busDevices[initiator]->HandleReceiveFailed();
1399 }
1400
1401 bool CCECProcessor::SetStreamPath(uint16_t iPhysicalAddress)
1402 {
1403 // stream path changes are sent by the TV
1404 return m_busDevices[CECDEVICE_TV]->GetHandler()->TransmitSetStreamPath(iPhysicalAddress);
1405 }
1406
1407 bool CCECProcessor::SetConfiguration(const libcec_configuration *configuration)
1408 {
1409 bool bNeedsReinit(false);
1410 CCECBusDevice *primary = IsRunning() ? GetPrimaryDevice() : NULL;
1411 m_configuration.clientVersion = configuration->clientVersion;
1412
1413 // client version 1.5.0
1414
1415 // device types
1416 bNeedsReinit |= IsRunning () && m_configuration.deviceTypes != configuration->deviceTypes;
1417 m_configuration.deviceTypes = configuration->deviceTypes;
1418
1419 // physical address
1420 bNeedsReinit |= IsRunning() && m_configuration.iPhysicalAddress != configuration->iPhysicalAddress;
1421 m_configuration.iPhysicalAddress = configuration->iPhysicalAddress;
1422
1423 // base device
1424 bNeedsReinit |= IsRunning() && m_configuration.baseDevice != configuration->baseDevice;
1425 m_configuration.baseDevice = configuration->baseDevice;
1426
1427 // hdmi port
1428 bNeedsReinit |= IsRunning() && m_configuration.iHDMIPort != configuration->iHDMIPort;
1429 m_configuration.iHDMIPort = configuration->iHDMIPort;
1430
1431 // device name
1432 snprintf(m_configuration.strDeviceName, 13, "%s", configuration->strDeviceName);
1433 if (primary && !primary->GetOSDName().Equals(m_configuration.strDeviceName))
1434 {
1435 primary->SetOSDName(m_configuration.strDeviceName);
1436 if (!bNeedsReinit && IsRunning())
1437 primary->TransmitOSDName(CECDEVICE_TV);
1438 }
1439
1440 // tv vendor id override
1441 if (m_configuration.tvVendor != configuration->tvVendor)
1442 {
1443 m_configuration.tvVendor= configuration->tvVendor;
1444 m_busDevices[CECDEVICE_TV]->SetVendorId((uint64_t)m_configuration.tvVendor);
1445 }
1446
1447 // wake CEC devices
1448 if (m_configuration.wakeDevices != configuration->wakeDevices)
1449 {
1450 m_configuration.wakeDevices = configuration->wakeDevices;
1451 if (!bNeedsReinit && IsRunning())
1452 PowerOnDevices();
1453 }
1454
1455 // just copy these
1456 m_configuration.bGetSettingsFromROM = configuration->bGetSettingsFromROM;
1457 m_configuration.powerOffDevices = configuration->powerOffDevices;
1458 m_configuration.bPowerOffScreensaver = configuration->bPowerOffScreensaver;
1459 m_configuration.bPowerOffOnStandby = configuration->bPowerOffOnStandby;
1460
1461 // ensure that there is at least 1 device type set
1462 if (m_configuration.deviceTypes.IsEmpty())
1463 m_configuration.deviceTypes.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
1464
1465 if (bNeedsReinit)
1466 {
1467 SetInitialised(false);
1468 m_logicalAddresses.Clear();
1469 return Initialise();
1470 }
1471
1472 return true;
1473 }
1474
1475 bool CCECProcessor::GetCurrentConfiguration(libcec_configuration *configuration)
1476 {
1477 m_configuration.tvVendor = m_busDevices[CECDEVICE_TV]->GetVendorId();
1478
1479 // client version 1.5.0
1480 configuration->clientVersion = m_configuration.clientVersion;
1481 snprintf(configuration->strDeviceName, 13, "%s", m_configuration.strDeviceName);
1482 configuration->deviceTypes = m_configuration.deviceTypes;
1483 configuration->iPhysicalAddress = m_configuration.iPhysicalAddress;
1484 configuration->baseDevice = m_configuration.baseDevice;
1485 configuration->iHDMIPort = m_configuration.iHDMIPort;
1486 configuration->tvVendor = m_configuration.tvVendor;
1487 configuration->wakeDevices = m_configuration.wakeDevices;
1488 configuration->bGetSettingsFromROM = m_configuration.bGetSettingsFromROM;
1489 configuration->powerOffDevices = m_configuration.powerOffDevices;
1490 configuration->bPowerOffScreensaver = m_configuration.bPowerOffScreensaver;
1491 configuration->bPowerOffOnStandby = m_configuration.bPowerOffOnStandby;
1492
1493 return true;
1494 }
1495
1496 bool CCECProcessor::CanPersistConfiguration(void)
1497 {
1498 return m_communication->GetFirmwareVersion() >= 2;
1499 }
1500
1501 bool CCECProcessor::PersistConfiguration(libcec_configuration *configuration)
1502 {
1503 return m_communication->PersistConfiguration(configuration);
1504 }