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