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