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