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