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