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