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