cec: close the connection when a comm error was detected, and notify the client via...
[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_threadMutex);
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 bool bRetry(true);
935 uint8_t iTries(0);
936 uint8_t iLineTimeout = m_iStandardLineTimeout;
937 cec_adapter_message_state adapterState = ADAPTER_MESSAGE_STATE_UNKNOWN;
938
939 while (bRetry && ++iTries < iMaxTries)
940 {
941 if (m_busDevices[data.initiator]->IsUnsupportedFeature(data.opcode))
942 return false;
943
944 adapterState = m_communication->Write(data, bRetry, iLineTimeout);
945 iLineTimeout = m_iRetryLineTimeout;
946 }
947
948 return adapterState == ADAPTER_MESSAGE_STATE_SENT_ACKED;
949 }
950
951 void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, cec_abort_reason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
952 {
953 CLibCEC::AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
954
955 cec_command command;
956 // TODO
957 cec_command::Format(command, m_configuration.logicalAddresses.primary, address, CEC_OPCODE_FEATURE_ABORT);
958 command.parameters.PushBack((uint8_t)opcode);
959 command.parameters.PushBack((uint8_t)reason);
960
961 Transmit(command);
962 }
963
964 void CCECProcessor::ParseCommand(const cec_command &command)
965 {
966 CStdString dataStr;
967 dataStr.Format(">> %1x%1x", command.initiator, command.destination);
968 if (command.opcode_set == 1)
969 dataStr.AppendFormat(":%02x", command.opcode);
970 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
971 dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
972 CLibCEC::AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
973
974 if (!m_bMonitor && command.initiator >= CECDEVICE_TV && command.initiator <= CECDEVICE_BROADCAST)
975 m_busDevices[(uint8_t)command.initiator]->HandleCommand(command);
976 }
977
978 cec_logical_addresses CCECProcessor::GetActiveDevices(void)
979 {
980 cec_logical_addresses addresses;
981 addresses.Clear();
982 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
983 {
984 if (m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
985 addresses.Set((cec_logical_address) iPtr);
986 }
987 return addresses;
988 }
989
990 bool CCECProcessor::IsPresentDevice(cec_logical_address address)
991 {
992 return m_busDevices[address]->GetStatus() == CEC_DEVICE_STATUS_PRESENT;
993 }
994
995 bool CCECProcessor::IsPresentDeviceType(cec_device_type type)
996 {
997 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
998 {
999 if (m_busDevices[iPtr]->GetType() == type && m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
1000 return true;
1001 }
1002
1003 return false;
1004 }
1005
1006 uint16_t CCECProcessor::GetPhysicalAddress(void) const
1007 {
1008 if (!m_configuration.logicalAddresses.IsEmpty() && m_busDevices[m_configuration.logicalAddresses.primary])
1009 return m_busDevices[m_configuration.logicalAddresses.primary]->GetPhysicalAddress(false);
1010 return false;
1011 }
1012
1013 bool CCECProcessor::SetAckMask(uint16_t iMask)
1014 {
1015 return m_communication ? m_communication->SetAckMask(iMask) : false;
1016 }
1017
1018 bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
1019 {
1020 return m_busDevices[iDestination]->TransmitKeypress(key, bWait);
1021 }
1022
1023 bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination, bool bWait /* = true */)
1024 {
1025 return m_busDevices[iDestination]->TransmitKeyRelease(bWait);
1026 }
1027
1028 bool CCECProcessor::EnablePhysicalAddressDetection(void)
1029 {
1030 CLibCEC::AddLog(CEC_LOG_WARNING, "deprecated method %s called", __FUNCTION__);
1031 uint16_t iPhysicalAddress = m_communication->GetPhysicalAddress();
1032 if (iPhysicalAddress != 0)
1033 {
1034 m_configuration.bAutodetectAddress = 1;
1035 m_configuration.iPhysicalAddress = iPhysicalAddress;
1036 m_configuration.baseDevice = CECDEVICE_UNKNOWN;
1037 m_configuration.iHDMIPort = 0;
1038 return SetPhysicalAddress(iPhysicalAddress);
1039 }
1040 return false;
1041 }
1042
1043 bool CCECProcessor::StandbyDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
1044 {
1045 if (address == CECDEVICE_BROADCAST && m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_0)
1046 {
1047 bool bReturn(true);
1048 for (uint8_t iPtr = 0; iPtr <= 0xF; iPtr++)
1049 {
1050 if (m_configuration.powerOffDevices[iPtr])
1051 bReturn &= m_busDevices[iPtr]->Standby();
1052 }
1053 return bReturn;
1054 }
1055
1056 return m_busDevices[address]->Standby();
1057 }
1058
1059 bool CCECProcessor::PowerOnDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
1060 {
1061 if (address == CECDEVICE_BROADCAST && m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_0)
1062 {
1063 bool bReturn(true);
1064 for (uint8_t iPtr = 0; iPtr <= 0xF; iPtr++)
1065 {
1066 if (m_configuration.powerOffDevices[iPtr])
1067 bReturn &= m_busDevices[iPtr]->PowerOn();
1068 }
1069 return bReturn;
1070 }
1071
1072 return m_busDevices[address]->PowerOn();
1073 }
1074
1075 const char *CCECProcessor::ToString(const cec_device_type type)
1076 {
1077 switch (type)
1078 {
1079 case CEC_DEVICE_TYPE_AUDIO_SYSTEM:
1080 return "audio system";
1081 case CEC_DEVICE_TYPE_PLAYBACK_DEVICE:
1082 return "playback device";
1083 case CEC_DEVICE_TYPE_RECORDING_DEVICE:
1084 return "recording device";
1085 case CEC_DEVICE_TYPE_RESERVED:
1086 return "reserved";
1087 case CEC_DEVICE_TYPE_TUNER:
1088 return "tuner";
1089 case CEC_DEVICE_TYPE_TV:
1090 return "TV";
1091 default:
1092 return "unknown";
1093 }
1094 }
1095
1096 const char *CCECProcessor::ToString(const cec_menu_state state)
1097 {
1098 switch (state)
1099 {
1100 case CEC_MENU_STATE_ACTIVATED:
1101 return "activated";
1102 case CEC_MENU_STATE_DEACTIVATED:
1103 return "deactivated";
1104 default:
1105 return "unknown";
1106 }
1107 }
1108
1109 const char *CCECProcessor::ToString(const cec_version version)
1110 {
1111 switch (version)
1112 {
1113 case CEC_VERSION_1_2:
1114 return "1.2";
1115 case CEC_VERSION_1_2A:
1116 return "1.2a";
1117 case CEC_VERSION_1_3:
1118 return "1.3";
1119 case CEC_VERSION_1_3A:
1120 return "1.3a";
1121 case CEC_VERSION_1_4:
1122 return "1.4";
1123 default:
1124 return "unknown";
1125 }
1126 }
1127
1128 const char *CCECProcessor::ToString(const cec_power_status status)
1129 {
1130 switch (status)
1131 {
1132 case CEC_POWER_STATUS_ON:
1133 return "on";
1134 case CEC_POWER_STATUS_STANDBY:
1135 return "standby";
1136 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY:
1137 return "in transition from on to standby";
1138 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON:
1139 return "in transition from standby to on";
1140 default:
1141 return "unknown";
1142 }
1143 }
1144
1145 const char *CCECProcessor::ToString(const cec_logical_address address)
1146 {
1147 switch(address)
1148 {
1149 case CECDEVICE_AUDIOSYSTEM:
1150 return "Audio";
1151 case CECDEVICE_BROADCAST:
1152 return "Broadcast";
1153 case CECDEVICE_FREEUSE:
1154 return "Free use";
1155 case CECDEVICE_PLAYBACKDEVICE1:
1156 return "Playback 1";
1157 case CECDEVICE_PLAYBACKDEVICE2:
1158 return "Playback 2";
1159 case CECDEVICE_PLAYBACKDEVICE3:
1160 return "Playback 3";
1161 case CECDEVICE_RECORDINGDEVICE1:
1162 return "Recorder 1";
1163 case CECDEVICE_RECORDINGDEVICE2:
1164 return "Recorder 2";
1165 case CECDEVICE_RECORDINGDEVICE3:
1166 return "Recorder 3";
1167 case CECDEVICE_RESERVED1:
1168 return "Reserved 1";
1169 case CECDEVICE_RESERVED2:
1170 return "Reserved 2";
1171 case CECDEVICE_TUNER1:
1172 return "Tuner 1";
1173 case CECDEVICE_TUNER2:
1174 return "Tuner 2";
1175 case CECDEVICE_TUNER3:
1176 return "Tuner 3";
1177 case CECDEVICE_TUNER4:
1178 return "Tuner 4";
1179 case CECDEVICE_TV:
1180 return "TV";
1181 default:
1182 return "unknown";
1183 }
1184 }
1185
1186 const char *CCECProcessor::ToString(const cec_deck_control_mode mode)
1187 {
1188 switch (mode)
1189 {
1190 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND:
1191 return "skip forward wind";
1192 case CEC_DECK_CONTROL_MODE_EJECT:
1193 return "eject";
1194 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND:
1195 return "reverse rewind";
1196 case CEC_DECK_CONTROL_MODE_STOP:
1197 return "stop";
1198 default:
1199 return "unknown";
1200 }
1201 }
1202
1203 const char *CCECProcessor::ToString(const cec_deck_info status)
1204 {
1205 switch (status)
1206 {
1207 case CEC_DECK_INFO_PLAY:
1208 return "play";
1209 case CEC_DECK_INFO_RECORD:
1210 return "record";
1211 case CEC_DECK_INFO_PLAY_REVERSE:
1212 return "play reverse";
1213 case CEC_DECK_INFO_STILL:
1214 return "still";
1215 case CEC_DECK_INFO_SLOW:
1216 return "slow";
1217 case CEC_DECK_INFO_SLOW_REVERSE:
1218 return "slow reverse";
1219 case CEC_DECK_INFO_FAST_FORWARD:
1220 return "fast forward";
1221 case CEC_DECK_INFO_FAST_REVERSE:
1222 return "fast reverse";
1223 case CEC_DECK_INFO_NO_MEDIA:
1224 return "no media";
1225 case CEC_DECK_INFO_STOP:
1226 return "stop";
1227 case CEC_DECK_INFO_SKIP_FORWARD_WIND:
1228 return "info skip forward wind";
1229 case CEC_DECK_INFO_SKIP_REVERSE_REWIND:
1230 return "info skip reverse rewind";
1231 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD:
1232 return "info index search forward";
1233 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE:
1234 return "info index search reverse";
1235 case CEC_DECK_INFO_OTHER_STATUS:
1236 return "other";
1237 default:
1238 return "unknown";
1239 }
1240 }
1241
1242 const char *CCECProcessor::ToString(const cec_opcode opcode)
1243 {
1244 switch (opcode)
1245 {
1246 case CEC_OPCODE_ACTIVE_SOURCE:
1247 return "active source";
1248 case CEC_OPCODE_IMAGE_VIEW_ON:
1249 return "image view on";
1250 case CEC_OPCODE_TEXT_VIEW_ON:
1251 return "text view on";
1252 case CEC_OPCODE_INACTIVE_SOURCE:
1253 return "inactive source";
1254 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
1255 return "request active source";
1256 case CEC_OPCODE_ROUTING_CHANGE:
1257 return "routing change";
1258 case CEC_OPCODE_ROUTING_INFORMATION:
1259 return "routing information";
1260 case CEC_OPCODE_SET_STREAM_PATH:
1261 return "set stream path";
1262 case CEC_OPCODE_STANDBY:
1263 return "standby";
1264 case CEC_OPCODE_RECORD_OFF:
1265 return "record off";
1266 case CEC_OPCODE_RECORD_ON:
1267 return "record on";
1268 case CEC_OPCODE_RECORD_STATUS:
1269 return "record status";
1270 case CEC_OPCODE_RECORD_TV_SCREEN:
1271 return "record tv screen";
1272 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER:
1273 return "clear analogue timer";
1274 case CEC_OPCODE_CLEAR_DIGITAL_TIMER:
1275 return "clear digital timer";
1276 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER:
1277 return "clear external timer";
1278 case CEC_OPCODE_SET_ANALOGUE_TIMER:
1279 return "set analogue timer";
1280 case CEC_OPCODE_SET_DIGITAL_TIMER:
1281 return "set digital timer";
1282 case CEC_OPCODE_SET_EXTERNAL_TIMER:
1283 return "set external timer";
1284 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE:
1285 return "set timer program title";
1286 case CEC_OPCODE_TIMER_CLEARED_STATUS:
1287 return "timer cleared status";
1288 case CEC_OPCODE_TIMER_STATUS:
1289 return "timer status";
1290 case CEC_OPCODE_CEC_VERSION:
1291 return "cec version";
1292 case CEC_OPCODE_GET_CEC_VERSION:
1293 return "get cec version";
1294 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
1295 return "give physical address";
1296 case CEC_OPCODE_GET_MENU_LANGUAGE:
1297 return "get menu language";
1298 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS:
1299 return "report physical address";
1300 case CEC_OPCODE_SET_MENU_LANGUAGE:
1301 return "set menu language";
1302 case CEC_OPCODE_DECK_CONTROL:
1303 return "deck control";
1304 case CEC_OPCODE_DECK_STATUS:
1305 return "deck status";
1306 case CEC_OPCODE_GIVE_DECK_STATUS:
1307 return "give deck status";
1308 case CEC_OPCODE_PLAY:
1309 return "play";
1310 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS:
1311 return "give tuner status";
1312 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE:
1313 return "select analogue service";
1314 case CEC_OPCODE_SELECT_DIGITAL_SERVICE:
1315 return "set digital service";
1316 case CEC_OPCODE_TUNER_DEVICE_STATUS:
1317 return "tuner device status";
1318 case CEC_OPCODE_TUNER_STEP_DECREMENT:
1319 return "tuner step decrement";
1320 case CEC_OPCODE_TUNER_STEP_INCREMENT:
1321 return "tuner step increment";
1322 case CEC_OPCODE_DEVICE_VENDOR_ID:
1323 return "device vendor id";
1324 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
1325 return "give device vendor id";
1326 case CEC_OPCODE_VENDOR_COMMAND:
1327 return "vendor command";
1328 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
1329 return "vendor command with id";
1330 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN:
1331 return "vendor remote button down";
1332 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP:
1333 return "vendor remote button up";
1334 case CEC_OPCODE_SET_OSD_STRING:
1335 return "set osd string";
1336 case CEC_OPCODE_GIVE_OSD_NAME:
1337 return "give osd name";
1338 case CEC_OPCODE_SET_OSD_NAME:
1339 return "set osd name";
1340 case CEC_OPCODE_MENU_REQUEST:
1341 return "menu request";
1342 case CEC_OPCODE_MENU_STATUS:
1343 return "menu status";
1344 case CEC_OPCODE_USER_CONTROL_PRESSED:
1345 return "user control pressed";
1346 case CEC_OPCODE_USER_CONTROL_RELEASE:
1347 return "user control release";
1348 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
1349 return "give device power status";
1350 case CEC_OPCODE_REPORT_POWER_STATUS:
1351 return "report power status";
1352 case CEC_OPCODE_FEATURE_ABORT:
1353 return "feature abort";
1354 case CEC_OPCODE_ABORT:
1355 return "abort";
1356 case CEC_OPCODE_GIVE_AUDIO_STATUS:
1357 return "give audio status";
1358 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS:
1359 return "give audio mode status";
1360 case CEC_OPCODE_REPORT_AUDIO_STATUS:
1361 return "report audio status";
1362 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE:
1363 return "set system audio mode";
1364 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST:
1365 return "system audio mode request";
1366 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS:
1367 return "system audio mode status";
1368 case CEC_OPCODE_SET_AUDIO_RATE:
1369 return "set audio rate";
1370 case CEC_OPCODE_NONE:
1371 return "poll";
1372 default:
1373 return "UNKNOWN";
1374 }
1375 }
1376
1377 const char *CCECProcessor::ToString(const cec_system_audio_status mode)
1378 {
1379 switch(mode)
1380 {
1381 case CEC_SYSTEM_AUDIO_STATUS_ON:
1382 return "on";
1383 case CEC_SYSTEM_AUDIO_STATUS_OFF:
1384 return "off";
1385 default:
1386 return "unknown";
1387 }
1388 }
1389
1390 const char *CCECProcessor::ToString(const cec_audio_status UNUSED(status))
1391 {
1392 // TODO this is a mask
1393 return "TODO";
1394 }
1395
1396 const char *CCECProcessor::ToString(const cec_vendor_id vendor)
1397 {
1398 switch (vendor)
1399 {
1400 case CEC_VENDOR_SAMSUNG:
1401 return "Samsung";
1402 case CEC_VENDOR_LG:
1403 return "LG";
1404 case CEC_VENDOR_PANASONIC:
1405 return "Panasonic";
1406 case CEC_VENDOR_PIONEER:
1407 return "Pioneer";
1408 case CEC_VENDOR_ONKYO:
1409 return "Onkyo";
1410 case CEC_VENDOR_YAMAHA:
1411 return "Yamaha";
1412 case CEC_VENDOR_PHILIPS:
1413 return "Philips";
1414 case CEC_VENDOR_SONY:
1415 return "Sony";
1416 case CEC_VENDOR_TOSHIBA:
1417 return "Toshiba";
1418 default:
1419 return "Unknown";
1420 }
1421 }
1422
1423 const char *CCECProcessor::ToString(const cec_client_version version)
1424 {
1425 switch (version)
1426 {
1427 case CEC_CLIENT_VERSION_PRE_1_5:
1428 return "pre-1.5";
1429 case CEC_CLIENT_VERSION_1_5_0:
1430 return "1.5.0";
1431 case CEC_CLIENT_VERSION_1_5_1:
1432 return "1.5.1";
1433 case CEC_CLIENT_VERSION_1_5_2:
1434 return "1.5.2";
1435 case CEC_CLIENT_VERSION_1_5_3:
1436 return "1.5.3";
1437 case CEC_CLIENT_VERSION_1_6_0:
1438 return "1.6.0";
1439 case CEC_CLIENT_VERSION_1_6_1:
1440 return "1.6.1";
1441 case CEC_CLIENT_VERSION_1_6_2:
1442 return "1.6.2";
1443 default:
1444 return "Unknown";
1445 }
1446 }
1447
1448 const char *CCECProcessor::ToString(const cec_server_version version)
1449 {
1450 switch (version)
1451 {
1452 case CEC_SERVER_VERSION_PRE_1_5:
1453 return "pre-1.5";
1454 case CEC_SERVER_VERSION_1_5_0:
1455 return "1.5.0";
1456 case CEC_SERVER_VERSION_1_5_1:
1457 return "1.5.1";
1458 case CEC_SERVER_VERSION_1_5_2:
1459 return "1.5.2";
1460 case CEC_SERVER_VERSION_1_5_3:
1461 return "1.5.3";
1462 case CEC_SERVER_VERSION_1_6_0:
1463 return "1.6.0";
1464 case CEC_SERVER_VERSION_1_6_1:
1465 return "1.6.1";
1466 case CEC_SERVER_VERSION_1_6_2:
1467 return "1.6.2";
1468 default:
1469 return "Unknown";
1470 }
1471 }
1472
1473 void *CCECBusScan::Process(void)
1474 {
1475 CCECBusDevice *device(NULL);
1476 uint8_t iCounter(0);
1477
1478 while (!IsStopped())
1479 {
1480 if (++iCounter < 10)
1481 {
1482 Sleep(1000);
1483 continue;
1484 }
1485 for (unsigned int iPtr = 0; iPtr <= 11 && !IsStopped(); iPtr++)
1486 {
1487 device = m_processor->m_busDevices[iPtr];
1488 WaitUntilIdle();
1489 if (device && device->GetStatus(true) == CEC_DEVICE_STATUS_PRESENT)
1490 {
1491 WaitUntilIdle();
1492 if (!IsStopped())
1493 device->GetVendorId();
1494
1495 WaitUntilIdle();
1496 if (!IsStopped())
1497 device->GetPowerStatus(true);
1498 }
1499 }
1500 }
1501
1502 return NULL;
1503 }
1504
1505 void CCECBusScan::WaitUntilIdle(void)
1506 {
1507 if (IsStopped())
1508 return;
1509
1510 int32_t iWaitTime = 3000 - (int32_t)(GetTimeMs() - m_processor->GetLastTransmission());
1511 while (iWaitTime > 0)
1512 {
1513 Sleep(iWaitTime);
1514 iWaitTime = 3000 - (int32_t)(GetTimeMs() - m_processor->GetLastTransmission());
1515 }
1516 }
1517
1518 bool CCECProcessor::StartBootloader(const char *strPort /* = NULL */)
1519 {
1520 if (!m_communication && strPort)
1521 {
1522 bool bReturn(false);
1523 IAdapterCommunication *comm = new CUSBCECAdapterCommunication(this, strPort);
1524 CTimeout timeout(10000);
1525 int iConnectTry(0);
1526 while (timeout.TimeLeft() > 0 && (bReturn = comm->Open(timeout.TimeLeft() / CEC_CONNECT_TRIES, true)) == false)
1527 {
1528 CLibCEC::AddLog(CEC_LOG_ERROR, "could not open a connection (try %d)", ++iConnectTry);
1529 comm->Close();
1530 Sleep(500);
1531 }
1532 if (comm->IsOpen())
1533 {
1534 bReturn = comm->StartBootloader();
1535 delete comm;
1536 }
1537 return bReturn;
1538 }
1539 else
1540 {
1541 return m_communication->StartBootloader();
1542 }
1543 }
1544
1545 bool CCECProcessor::PingAdapter(void)
1546 {
1547 return m_communication->PingAdapter();
1548 }
1549
1550 void CCECProcessor::HandlePoll(cec_logical_address initiator, cec_logical_address destination)
1551 {
1552 if (destination < CECDEVICE_BROADCAST)
1553 m_busDevices[destination]->HandlePollFrom(initiator);
1554 }
1555
1556 bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator)
1557 {
1558 return !m_busDevices[initiator]->HandleReceiveFailed();
1559 }
1560
1561 bool CCECProcessor::SetStreamPath(uint16_t iPhysicalAddress)
1562 {
1563 // stream path changes are sent by the TV
1564 return m_busDevices[CECDEVICE_TV]->GetHandler()->TransmitSetStreamPath(iPhysicalAddress);
1565 }
1566
1567 bool CCECProcessor::SetConfiguration(const libcec_configuration *configuration)
1568 {
1569 bool bReinit(false);
1570 CCECBusDevice *primary = IsRunning() ? GetPrimaryDevice() : NULL;
1571 cec_device_type oldPrimaryType = primary ? primary->GetType() : CEC_DEVICE_TYPE_RECORDING_DEVICE;
1572 m_configuration.clientVersion = configuration->clientVersion;
1573 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - using client version '%s'", __FUNCTION__, ToString((cec_client_version)configuration->clientVersion));
1574
1575 // client version 1.5.0
1576
1577 // device types
1578 bool bDeviceTypeChanged = IsRunning () && m_configuration.deviceTypes != configuration->deviceTypes;
1579 m_configuration.deviceTypes = configuration->deviceTypes;
1580 if (bDeviceTypeChanged)
1581 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - using primary device type '%s'", __FUNCTION__, ToString(configuration->deviceTypes[0]));
1582
1583 bool bPhysicalAddressChanged(false);
1584
1585 // autodetect address
1586 bool bPhysicalAutodetected(false);
1587 if (IsRunning() && configuration->bAutodetectAddress == 1)
1588 {
1589 uint16_t iPhysicalAddress = m_communication->GetPhysicalAddress();
1590 if (iPhysicalAddress != 0)
1591 {
1592 if (IsRunning())
1593 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - autodetected physical address '%4x'", __FUNCTION__, iPhysicalAddress);
1594 else
1595 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - using physical address '%x'", __FUNCTION__, iPhysicalAddress);
1596 bPhysicalAddressChanged = (m_configuration.iPhysicalAddress != iPhysicalAddress);
1597 m_configuration.iPhysicalAddress = iPhysicalAddress;
1598 m_configuration.iHDMIPort = 0;
1599 m_configuration.baseDevice = CECDEVICE_UNKNOWN;
1600 bPhysicalAutodetected = true;
1601 }
1602 }
1603
1604 // physical address
1605 if (!bPhysicalAutodetected)
1606 {
1607 if (configuration->iPhysicalAddress != 0)
1608 bPhysicalAddressChanged = IsRunning() && m_configuration.iPhysicalAddress != configuration->iPhysicalAddress;
1609 if (bPhysicalAddressChanged)
1610 {
1611 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - physical address '%x'", __FUNCTION__, configuration->iPhysicalAddress);
1612 m_configuration.iPhysicalAddress = configuration->iPhysicalAddress;
1613 }
1614 }
1615
1616 bool bHdmiPortChanged(false);
1617 if (!bPhysicalAutodetected && !bPhysicalAddressChanged)
1618 {
1619 // base device
1620 bHdmiPortChanged = IsRunning() && m_configuration.baseDevice != configuration->baseDevice;
1621 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - using base device '%x'", __FUNCTION__, (int)configuration->baseDevice);
1622 m_configuration.baseDevice = configuration->baseDevice;
1623
1624 // hdmi port
1625 bHdmiPortChanged |= IsRunning() && m_configuration.iHDMIPort != configuration->iHDMIPort;
1626 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - using HDMI port '%d'", __FUNCTION__, configuration->iHDMIPort);
1627 m_configuration.iHDMIPort = configuration->iHDMIPort;
1628 }
1629 else
1630 {
1631 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - resetting HDMI port and base device to defaults", __FUNCTION__);
1632 m_configuration.baseDevice = CECDEVICE_UNKNOWN;
1633 m_configuration.iHDMIPort = 0;
1634 }
1635
1636 bReinit = bPhysicalAddressChanged || bHdmiPortChanged || bDeviceTypeChanged;
1637
1638 // device name
1639 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - using OSD name '%s'", __FUNCTION__, configuration->strDeviceName);
1640 snprintf(m_configuration.strDeviceName, 13, "%s", configuration->strDeviceName);
1641 if (primary && !primary->GetOSDName().Equals(m_configuration.strDeviceName))
1642 {
1643 primary->SetOSDName(m_configuration.strDeviceName);
1644 if (!bReinit && IsRunning())
1645 primary->TransmitOSDName(CECDEVICE_TV);
1646 }
1647
1648 // tv vendor id override
1649 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - vendor id '%s'", __FUNCTION__, ToString((cec_vendor_id)configuration->tvVendor));
1650 if (m_configuration.tvVendor != configuration->tvVendor)
1651 {
1652 m_configuration.tvVendor= configuration->tvVendor;
1653 m_busDevices[CECDEVICE_TV]->SetVendorId((uint64_t)m_configuration.tvVendor);
1654 }
1655
1656 // wake CEC devices
1657 if (m_configuration.wakeDevices != configuration->wakeDevices)
1658 {
1659 m_configuration.wakeDevices = configuration->wakeDevices;
1660 if (!bReinit && IsRunning())
1661 PowerOnDevices();
1662 }
1663
1664 // just copy these
1665 m_configuration.bUseTVMenuLanguage = configuration->bUseTVMenuLanguage;
1666 m_configuration.bActivateSource = configuration->bActivateSource;
1667 m_configuration.bGetSettingsFromROM = configuration->bGetSettingsFromROM;
1668 m_configuration.powerOffDevices = configuration->powerOffDevices;
1669 m_configuration.bPowerOffScreensaver = configuration->bPowerOffScreensaver;
1670 m_configuration.bPowerOffOnStandby = configuration->bPowerOffOnStandby;
1671
1672 // client version 1.5.1
1673 if (configuration->clientVersion >= CEC_CLIENT_VERSION_1_5_1)
1674 m_configuration.bSendInactiveSource = configuration->bSendInactiveSource;
1675
1676 // client version 1.6.0
1677 if (configuration->clientVersion >= CEC_CLIENT_VERSION_1_6_0)
1678 {
1679 m_configuration.bPowerOffDevicesOnStandby = configuration->bPowerOffDevicesOnStandby;
1680 m_configuration.bShutdownOnStandby = configuration->bShutdownOnStandby;
1681 }
1682
1683 // client version 1.6.2
1684 if (configuration->clientVersion >= CEC_CLIENT_VERSION_1_6_2)
1685 {
1686 snprintf(m_configuration.strDeviceLanguage, 3, "%s", configuration->strDeviceLanguage);
1687 }
1688
1689 // ensure that there is at least 1 device type set
1690 if (m_configuration.deviceTypes.IsEmpty())
1691 m_configuration.deviceTypes.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
1692
1693 // persist the configuration
1694 if (IsRunning())
1695 m_communication->PersistConfiguration(&m_configuration);
1696
1697 if (bReinit || m_configuration.logicalAddresses.IsEmpty())
1698 {
1699 if (bDeviceTypeChanged)
1700 return ChangeDeviceType(oldPrimaryType, m_configuration.deviceTypes[0]);
1701 else if (bPhysicalAddressChanged)
1702 return SetPhysicalAddress(m_configuration.iPhysicalAddress);
1703 else
1704 return SetHDMIPort(m_configuration.baseDevice, m_configuration.iHDMIPort);
1705 }
1706 else if (m_configuration.bActivateSource == 1 && IsRunning() && !IsActiveSource(m_configuration.logicalAddresses.primary))
1707 {
1708 // activate the source if we're not already the active source
1709 SetActiveSource(m_configuration.deviceTypes.types[0]);
1710 }
1711
1712 return true;
1713 }
1714
1715 bool CCECProcessor::GetCurrentConfiguration(libcec_configuration *configuration)
1716 {
1717 // client version 1.5.0
1718 snprintf(configuration->strDeviceName, 13, "%s", m_configuration.strDeviceName);
1719 configuration->deviceTypes = m_configuration.deviceTypes;
1720 configuration->bAutodetectAddress = m_configuration.bAutodetectAddress;
1721 configuration->iPhysicalAddress = m_configuration.iPhysicalAddress;
1722 configuration->baseDevice = m_configuration.baseDevice;
1723 configuration->iHDMIPort = m_configuration.iHDMIPort;
1724 configuration->clientVersion = m_configuration.clientVersion;
1725 configuration->serverVersion = m_configuration.serverVersion;
1726 configuration->tvVendor = m_configuration.tvVendor;
1727
1728 configuration->bGetSettingsFromROM = m_configuration.bGetSettingsFromROM;
1729 configuration->bUseTVMenuLanguage = m_configuration.bUseTVMenuLanguage;
1730 configuration->bActivateSource = m_configuration.bActivateSource;
1731 configuration->wakeDevices = m_configuration.wakeDevices;
1732 configuration->powerOffDevices = m_configuration.powerOffDevices;
1733 configuration->bPowerOffScreensaver = m_configuration.bPowerOffScreensaver;
1734 configuration->bPowerOffOnStandby = m_configuration.bPowerOffOnStandby;
1735
1736 // client version 1.5.1
1737 if (configuration->clientVersion >= CEC_CLIENT_VERSION_1_5_1)
1738 configuration->bSendInactiveSource = m_configuration.bSendInactiveSource;
1739
1740 // client version 1.5.3
1741 if (configuration->clientVersion >= CEC_CLIENT_VERSION_1_5_3)
1742 configuration->logicalAddresses = m_configuration.logicalAddresses;
1743
1744 // client version 1.6.0
1745 if (configuration->clientVersion >= CEC_CLIENT_VERSION_1_6_0)
1746 {
1747 configuration->iFirmwareVersion = m_configuration.iFirmwareVersion;
1748 configuration->bPowerOffDevicesOnStandby = m_configuration.bPowerOffDevicesOnStandby;
1749 configuration->bShutdownOnStandby = m_configuration.bShutdownOnStandby;
1750 }
1751
1752 return true;
1753 }
1754
1755 bool CCECProcessor::CanPersistConfiguration(void)
1756 {
1757 return m_communication ? m_communication->GetFirmwareVersion() >= 2 : false;
1758 }
1759
1760 bool CCECProcessor::PersistConfiguration(libcec_configuration *configuration)
1761 {
1762 return m_communication ? m_communication->PersistConfiguration(configuration) : false;
1763 }
1764
1765 void CCECProcessor::RescanActiveDevices(void)
1766 {
1767 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
1768 m_busDevices[iPtr]->GetStatus(true);
1769 }
1770
1771 bool CCECProcessor::GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs /* = 10000 */)
1772 {
1773 if (!OpenConnection(strPort, 38400, iTimeoutMs, false))
1774 return false;
1775
1776 config->iFirmwareVersion = m_communication->GetFirmwareVersion();
1777 config->iPhysicalAddress = m_communication->GetPhysicalAddress();
1778
1779 delete m_communication;
1780 m_communication = NULL;
1781 return true;
1782 }