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