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