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