cosmetics
[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 CCECBusDevice *CCECProcessor::GetPrimaryDevice(void) const
657 {
658 CCECBusDevice *device(NULL);
659 cec_logical_address primary = m_logicalAddresses.primary;
660 if (primary != CECDEVICE_UNKNOWN)
661 device = m_busDevices[primary];
662 return device;
663 }
664
665 cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress)
666 {
667 return m_busDevices[iAddress]->GetCecVersion();
668 }
669
670 cec_osd_name CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress)
671 {
672 CStdString strOSDName = m_busDevices[iAddress]->GetOSDName();
673 cec_osd_name retVal;
674
675 snprintf(retVal.name, sizeof(retVal.name), "%s", strOSDName.c_str());
676 retVal.device = iAddress;
677
678 return retVal;
679 }
680
681 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
682 {
683 if (m_busDevices[iAddress])
684 {
685 *language = m_busDevices[iAddress]->GetMenuLanguage();
686 return (strcmp(language->language, "???") != 0);
687 }
688 return false;
689 }
690
691 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress)
692 {
693 if (m_busDevices[iAddress])
694 return m_busDevices[iAddress]->GetVendorId();
695 return false;
696 }
697
698 uint16_t CCECProcessor::GetDevicePhysicalAddress(cec_logical_address iAddress)
699 {
700 if (m_busDevices[iAddress])
701 return m_busDevices[iAddress]->GetPhysicalAddress(false);
702 return false;
703 }
704
705 cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress)
706 {
707 if (m_busDevices[iAddress])
708 return m_busDevices[iAddress]->GetPowerStatus();
709 return CEC_POWER_STATUS_UNKNOWN;
710 }
711
712 cec_logical_address CCECProcessor::GetActiveSource(void)
713 {
714 for (uint8_t iPtr = 0; iPtr <= 11; iPtr++)
715 {
716 if (m_busDevices[iPtr]->IsActiveSource())
717 return (cec_logical_address)iPtr;
718 }
719
720 return CECDEVICE_UNKNOWN;
721 }
722
723 bool CCECProcessor::IsActiveSource(cec_logical_address iAddress)
724 {
725 return m_busDevices[iAddress]->IsActiveSource();
726 }
727
728 bool CCECProcessor::Transmit(const cec_command &data)
729 {
730 bool bReturn(false);
731 LogOutput(data);
732
733 CCECAdapterMessage *output = new CCECAdapterMessage(data);
734
735 /* set the number of retries */
736 if (data.opcode == CEC_OPCODE_NONE)
737 output->maxTries = 1;
738 else if (data.initiator != CECDEVICE_BROADCAST)
739 output->maxTries = m_busDevices[data.initiator]->GetHandler()->GetTransmitRetries() + 1;
740
741 bReturn = Transmit(output);
742
743 /* set to "not present" on failed ack */
744 if (output->is_error() && output->reply == MSGCODE_TRANSMIT_FAILED_ACK &&
745 output->destination() != CECDEVICE_BROADCAST)
746 m_busDevices[output->destination()]->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT);
747
748 delete output;
749 return bReturn;
750 }
751
752 bool CCECProcessor::Transmit(CCECAdapterMessage *output)
753 {
754 bool bReturn(false);
755 CLockObject lock(&m_mutex);
756 {
757 m_iLastTransmission = GetTimeMs();
758 m_communication->SetLineTimeout(m_iStandardLineTimeout);
759 output->tries = 1;
760
761 do
762 {
763 if (output->tries > 0)
764 m_communication->SetLineTimeout(m_iRetryLineTimeout);
765
766 CLockObject msgLock(&output->mutex);
767 if (!m_communication || !m_communication->Write(output))
768 return bReturn;
769 else
770 {
771 output->condition.Wait(&output->mutex);
772 if (output->state != ADAPTER_MESSAGE_STATE_SENT)
773 {
774 m_controller->AddLog(CEC_LOG_ERROR, "command was not sent");
775 return bReturn;
776 }
777 }
778
779 if (output->transmit_timeout > 0)
780 {
781 if ((bReturn = WaitForTransmitSucceeded(output)) == false)
782 m_controller->AddLog(CEC_LOG_DEBUG, "did not receive ack");
783 }
784 else
785 bReturn = true;
786 }while (output->transmit_timeout > 0 && output->needs_retry() && ++output->tries < output->maxTries);
787 }
788
789 m_communication->SetLineTimeout(m_iStandardLineTimeout);
790
791 return bReturn;
792 }
793
794 void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, cec_abort_reason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
795 {
796 m_controller->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
797
798 cec_command command;
799 // TODO
800 cec_command::Format(command, m_logicalAddresses.primary, address, CEC_OPCODE_FEATURE_ABORT);
801 command.parameters.PushBack((uint8_t)opcode);
802 command.parameters.PushBack((uint8_t)reason);
803
804 Transmit(command);
805 }
806
807 bool CCECProcessor::WaitForTransmitSucceeded(CCECAdapterMessage *message)
808 {
809 bool bError(false);
810 bool bTransmitSucceeded(false);
811 uint8_t iPacketsLeft(message->size() / 4);
812
813 int64_t iNow = GetTimeMs();
814 int64_t iTargetTime = iNow + message->transmit_timeout;
815
816 while (!bTransmitSucceeded && !bError && (message->transmit_timeout == 0 || iNow < iTargetTime))
817 {
818 CCECAdapterMessage msg;
819
820 if (!m_communication->Read(msg, message->transmit_timeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000))
821 {
822 iNow = GetTimeMs();
823 continue;
824 }
825
826 if (msg.message() == MSGCODE_FRAME_START && msg.ack())
827 {
828 m_busDevices[msg.initiator()]->GetHandler()->HandlePoll(msg.initiator(), msg.destination());
829 m_lastInitiator = msg.initiator();
830 iNow = GetTimeMs();
831 continue;
832 }
833
834 bError = msg.is_error();
835 if (msg.message() == MSGCODE_RECEIVE_FAILED &&
836 m_lastInitiator != CECDEVICE_UNKNOWN &&
837 !m_busDevices[m_lastInitiator]->GetHandler()->HandleReceiveFailed())
838 {
839 iNow = GetTimeMs();
840 continue;
841 }
842
843 if (bError)
844 {
845 message->reply = msg.message();
846 m_controller->AddLog(CEC_LOG_DEBUG, msg.ToString());
847 }
848 else
849 {
850 switch(msg.message())
851 {
852 case MSGCODE_COMMAND_ACCEPTED:
853 m_controller->AddLog(CEC_LOG_DEBUG, msg.ToString());
854 if (iPacketsLeft > 0)
855 iPacketsLeft--;
856 break;
857 case MSGCODE_TRANSMIT_SUCCEEDED:
858 m_controller->AddLog(CEC_LOG_DEBUG, msg.ToString());
859 bTransmitSucceeded = (iPacketsLeft == 0);
860 bError = !bTransmitSucceeded;
861 message->reply = MSGCODE_TRANSMIT_SUCCEEDED;
862 break;
863 default:
864 // ignore other data while waiting
865 break;
866 }
867
868 iNow = GetTimeMs();
869 }
870 }
871
872 return bTransmitSucceeded && !bError;
873 }
874
875 bool CCECProcessor::ParseMessage(const CCECAdapterMessage &msg)
876 {
877 bool bEom(false);
878 bool bIsError(msg.is_error());
879
880 if (msg.empty())
881 return bEom;
882
883 switch(msg.message())
884 {
885 case MSGCODE_FRAME_START:
886 {
887 m_currentframe.Clear();
888 if (msg.size() >= 2)
889 {
890 m_currentframe.initiator = msg.initiator();
891 m_currentframe.destination = msg.destination();
892 m_currentframe.ack = msg.ack();
893 m_currentframe.eom = msg.eom();
894 }
895 if (m_currentframe.ack == 0x1)
896 {
897 m_lastInitiator = m_currentframe.initiator;
898 m_busDevices[m_lastInitiator]->GetHandler()->HandlePoll(m_currentframe.initiator, m_currentframe.destination);
899 }
900 }
901 break;
902 case MSGCODE_RECEIVE_FAILED:
903 {
904 if (m_lastInitiator != CECDEVICE_UNKNOWN)
905 bIsError = m_busDevices[m_lastInitiator]->GetHandler()->HandleReceiveFailed();
906 }
907 break;
908 case MSGCODE_FRAME_DATA:
909 {
910 if (msg.size() >= 2)
911 {
912 m_currentframe.PushBack(msg[1]);
913 m_currentframe.eom = msg.eom();
914 }
915 bEom = msg.eom();
916 }
917 break;
918 default:
919 break;
920 }
921
922 m_controller->AddLog(bIsError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
923 return bEom;
924 }
925
926 void CCECProcessor::ParseCommand(cec_command &command)
927 {
928 CStdString dataStr;
929 dataStr.Format(">> %1x%1x:%02x", command.initiator, command.destination, command.opcode);
930 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
931 dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
932 m_controller->AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
933
934 if (!m_bMonitor && command.initiator >= CECDEVICE_TV && command.initiator <= CECDEVICE_BROADCAST)
935 m_busDevices[(uint8_t)command.initiator]->HandleCommand(command);
936 }
937
938 cec_logical_addresses CCECProcessor::GetActiveDevices(void)
939 {
940 cec_logical_addresses addresses;
941 addresses.Clear();
942 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
943 {
944 if (m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
945 addresses.Set((cec_logical_address) iPtr);
946 }
947 return addresses;
948 }
949
950 bool CCECProcessor::IsPresentDevice(cec_logical_address address)
951 {
952 return m_busDevices[address]->GetStatus() == CEC_DEVICE_STATUS_PRESENT;
953 }
954
955 bool CCECProcessor::IsPresentDeviceType(cec_device_type type)
956 {
957 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
958 {
959 if (m_busDevices[iPtr]->GetType() == type && m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
960 return true;
961 }
962
963 return false;
964 }
965
966 uint16_t CCECProcessor::GetPhysicalAddress(void) const
967 {
968 if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary])
969 return m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false);
970 return false;
971 }
972
973 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode)
974 {
975 m_controller->SetCurrentButton(iButtonCode);
976 }
977
978 void CCECProcessor::AddCommand(const cec_command &command)
979 {
980 m_controller->AddCommand(command);
981 }
982
983 void CCECProcessor::AddKey(cec_keypress &key)
984 {
985 m_controller->AddKey(key);
986 }
987
988 void CCECProcessor::AddKey(void)
989 {
990 m_controller->AddKey();
991 }
992
993 void CCECProcessor::AddLog(cec_log_level level, const CStdString &strMessage)
994 {
995 m_controller->AddLog(level, strMessage);
996 }
997
998 bool CCECProcessor::SetAckMask(uint16_t iMask)
999 {
1000 bool bReturn(false);
1001 CStdString strLog;
1002 strLog.Format("setting ackmask to %2x", iMask);
1003 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
1004
1005 CCECAdapterMessage *output = new CCECAdapterMessage;
1006
1007 output->push_back(MSGSTART);
1008 output->push_escaped(MSGCODE_SET_ACK_MASK);
1009 output->push_escaped(iMask >> 8);
1010 output->push_escaped((uint8_t)iMask);
1011 output->push_back(MSGEND);
1012
1013 if ((bReturn = Transmit(output)) == false)
1014 m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask");
1015
1016 delete output;
1017
1018 return bReturn;
1019 }
1020
1021 bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
1022 {
1023 return m_busDevices[iDestination]->TransmitKeypress(key);
1024 }
1025
1026 bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination, bool bWait /* = true */)
1027 {
1028 return m_busDevices[iDestination]->TransmitKeyRelease();
1029 }
1030
1031 const char *CCECProcessor::ToString(const cec_menu_state state)
1032 {
1033 switch (state)
1034 {
1035 case CEC_MENU_STATE_ACTIVATED:
1036 return "activated";
1037 case CEC_MENU_STATE_DEACTIVATED:
1038 return "deactivated";
1039 default:
1040 return "unknown";
1041 }
1042 }
1043
1044 const char *CCECProcessor::ToString(const cec_version version)
1045 {
1046 switch (version)
1047 {
1048 case CEC_VERSION_1_2:
1049 return "1.2";
1050 case CEC_VERSION_1_2A:
1051 return "1.2a";
1052 case CEC_VERSION_1_3:
1053 return "1.3";
1054 case CEC_VERSION_1_3A:
1055 return "1.3a";
1056 case CEC_VERSION_1_4:
1057 return "1.4";
1058 default:
1059 return "unknown";
1060 }
1061 }
1062
1063 const char *CCECProcessor::ToString(const cec_power_status status)
1064 {
1065 switch (status)
1066 {
1067 case CEC_POWER_STATUS_ON:
1068 return "on";
1069 case CEC_POWER_STATUS_STANDBY:
1070 return "standby";
1071 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY:
1072 return "in transition from on to standby";
1073 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON:
1074 return "in transition from standby to on";
1075 default:
1076 return "unknown";
1077 }
1078 }
1079
1080 const char *CCECProcessor::ToString(const cec_logical_address address)
1081 {
1082 switch(address)
1083 {
1084 case CECDEVICE_AUDIOSYSTEM:
1085 return "Audio";
1086 case CECDEVICE_BROADCAST:
1087 return "Broadcast";
1088 case CECDEVICE_FREEUSE:
1089 return "Free use";
1090 case CECDEVICE_PLAYBACKDEVICE1:
1091 return "Playback 1";
1092 case CECDEVICE_PLAYBACKDEVICE2:
1093 return "Playback 2";
1094 case CECDEVICE_PLAYBACKDEVICE3:
1095 return "Playback 3";
1096 case CECDEVICE_RECORDINGDEVICE1:
1097 return "Recorder 1";
1098 case CECDEVICE_RECORDINGDEVICE2:
1099 return "Recorder 2";
1100 case CECDEVICE_RECORDINGDEVICE3:
1101 return "Recorder 3";
1102 case CECDEVICE_RESERVED1:
1103 return "Reserved 1";
1104 case CECDEVICE_RESERVED2:
1105 return "Reserved 2";
1106 case CECDEVICE_TUNER1:
1107 return "Tuner 1";
1108 case CECDEVICE_TUNER2:
1109 return "Tuner 2";
1110 case CECDEVICE_TUNER3:
1111 return "Tuner 3";
1112 case CECDEVICE_TUNER4:
1113 return "Tuner 4";
1114 case CECDEVICE_TV:
1115 return "TV";
1116 default:
1117 return "unknown";
1118 }
1119 }
1120
1121 const char *CCECProcessor::ToString(const cec_deck_control_mode mode)
1122 {
1123 switch (mode)
1124 {
1125 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND:
1126 return "skip forward wind";
1127 case CEC_DECK_CONTROL_MODE_EJECT:
1128 return "eject";
1129 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND:
1130 return "reverse rewind";
1131 case CEC_DECK_CONTROL_MODE_STOP:
1132 return "stop";
1133 default:
1134 return "unknown";
1135 }
1136 }
1137
1138 const char *CCECProcessor::ToString(const cec_deck_info status)
1139 {
1140 switch (status)
1141 {
1142 case CEC_DECK_INFO_PLAY:
1143 return "play";
1144 case CEC_DECK_INFO_RECORD:
1145 return "record";
1146 case CEC_DECK_INFO_PLAY_REVERSE:
1147 return "play reverse";
1148 case CEC_DECK_INFO_STILL:
1149 return "still";
1150 case CEC_DECK_INFO_SLOW:
1151 return "slow";
1152 case CEC_DECK_INFO_SLOW_REVERSE:
1153 return "slow reverse";
1154 case CEC_DECK_INFO_FAST_FORWARD:
1155 return "fast forward";
1156 case CEC_DECK_INFO_FAST_REVERSE:
1157 return "fast reverse";
1158 case CEC_DECK_INFO_NO_MEDIA:
1159 return "no media";
1160 case CEC_DECK_INFO_STOP:
1161 return "stop";
1162 case CEC_DECK_INFO_SKIP_FORWARD_WIND:
1163 return "info skip forward wind";
1164 case CEC_DECK_INFO_SKIP_REVERSE_REWIND:
1165 return "info skip reverse rewind";
1166 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD:
1167 return "info index search forward";
1168 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE:
1169 return "info index search reverse";
1170 case CEC_DECK_INFO_OTHER_STATUS:
1171 return "other";
1172 default:
1173 return "unknown";
1174 }
1175 }
1176
1177 const char *CCECProcessor::ToString(const cec_opcode opcode)
1178 {
1179 switch (opcode)
1180 {
1181 case CEC_OPCODE_ACTIVE_SOURCE:
1182 return "active source";
1183 case CEC_OPCODE_IMAGE_VIEW_ON:
1184 return "image view on";
1185 case CEC_OPCODE_TEXT_VIEW_ON:
1186 return "text view on";
1187 case CEC_OPCODE_INACTIVE_SOURCE:
1188 return "inactive source";
1189 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
1190 return "request active source";
1191 case CEC_OPCODE_ROUTING_CHANGE:
1192 return "routing change";
1193 case CEC_OPCODE_ROUTING_INFORMATION:
1194 return "routing information";
1195 case CEC_OPCODE_SET_STREAM_PATH:
1196 return "set stream path";
1197 case CEC_OPCODE_STANDBY:
1198 return "standby";
1199 case CEC_OPCODE_RECORD_OFF:
1200 return "record off";
1201 case CEC_OPCODE_RECORD_ON:
1202 return "record on";
1203 case CEC_OPCODE_RECORD_STATUS:
1204 return "record status";
1205 case CEC_OPCODE_RECORD_TV_SCREEN:
1206 return "record tv screen";
1207 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER:
1208 return "clear analogue timer";
1209 case CEC_OPCODE_CLEAR_DIGITAL_TIMER:
1210 return "clear digital timer";
1211 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER:
1212 return "clear external timer";
1213 case CEC_OPCODE_SET_ANALOGUE_TIMER:
1214 return "set analogue timer";
1215 case CEC_OPCODE_SET_DIGITAL_TIMER:
1216 return "set digital timer";
1217 case CEC_OPCODE_SET_EXTERNAL_TIMER:
1218 return "set external timer";
1219 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE:
1220 return "set timer program title";
1221 case CEC_OPCODE_TIMER_CLEARED_STATUS:
1222 return "timer cleared status";
1223 case CEC_OPCODE_TIMER_STATUS:
1224 return "timer status";
1225 case CEC_OPCODE_CEC_VERSION:
1226 return "cec version";
1227 case CEC_OPCODE_GET_CEC_VERSION:
1228 return "get cec version";
1229 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
1230 return "give physical address";
1231 case CEC_OPCODE_GET_MENU_LANGUAGE:
1232 return "get menu language";
1233 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS:
1234 return "report physical address";
1235 case CEC_OPCODE_SET_MENU_LANGUAGE:
1236 return "set menu language";
1237 case CEC_OPCODE_DECK_CONTROL:
1238 return "deck control";
1239 case CEC_OPCODE_DECK_STATUS:
1240 return "deck status";
1241 case CEC_OPCODE_GIVE_DECK_STATUS:
1242 return "give deck status";
1243 case CEC_OPCODE_PLAY:
1244 return "play";
1245 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS:
1246 return "give tuner status";
1247 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE:
1248 return "select analogue service";
1249 case CEC_OPCODE_SELECT_DIGITAL_SERVICE:
1250 return "set digital service";
1251 case CEC_OPCODE_TUNER_DEVICE_STATUS:
1252 return "tuner device status";
1253 case CEC_OPCODE_TUNER_STEP_DECREMENT:
1254 return "tuner step decrement";
1255 case CEC_OPCODE_TUNER_STEP_INCREMENT:
1256 return "tuner step increment";
1257 case CEC_OPCODE_DEVICE_VENDOR_ID:
1258 return "device vendor id";
1259 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
1260 return "give device vendor id";
1261 case CEC_OPCODE_VENDOR_COMMAND:
1262 return "vendor command";
1263 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
1264 return "vendor command with id";
1265 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN:
1266 return "vendor remote button down";
1267 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP:
1268 return "vendor remote button up";
1269 case CEC_OPCODE_SET_OSD_STRING:
1270 return "set osd string";
1271 case CEC_OPCODE_GIVE_OSD_NAME:
1272 return "give osd name";
1273 case CEC_OPCODE_SET_OSD_NAME:
1274 return "set osd name";
1275 case CEC_OPCODE_MENU_REQUEST:
1276 return "menu request";
1277 case CEC_OPCODE_MENU_STATUS:
1278 return "menu status";
1279 case CEC_OPCODE_USER_CONTROL_PRESSED:
1280 return "user control pressed";
1281 case CEC_OPCODE_USER_CONTROL_RELEASE:
1282 return "user control release";
1283 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
1284 return "give device power status";
1285 case CEC_OPCODE_REPORT_POWER_STATUS:
1286 return "report power status";
1287 case CEC_OPCODE_FEATURE_ABORT:
1288 return "feature abort";
1289 case CEC_OPCODE_ABORT:
1290 return "abort";
1291 case CEC_OPCODE_GIVE_AUDIO_STATUS:
1292 return "give audio status";
1293 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS:
1294 return "give audio mode status";
1295 case CEC_OPCODE_REPORT_AUDIO_STATUS:
1296 return "report audio status";
1297 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE:
1298 return "set system audio mode";
1299 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST:
1300 return "system audio mode request";
1301 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS:
1302 return "system audio mode status";
1303 case CEC_OPCODE_SET_AUDIO_RATE:
1304 return "set audio rate";
1305 default:
1306 return "UNKNOWN";
1307 }
1308 }
1309
1310 const char *CCECProcessor::ToString(const cec_system_audio_status mode)
1311 {
1312 switch(mode)
1313 {
1314 case CEC_SYSTEM_AUDIO_STATUS_ON:
1315 return "on";
1316 case CEC_SYSTEM_AUDIO_STATUS_OFF:
1317 return "off";
1318 default:
1319 return "unknown";
1320 }
1321 }
1322
1323 const char *CCECProcessor::ToString(const cec_audio_status status)
1324 {
1325 // TODO this is a mask
1326 return "TODO";
1327 }
1328
1329 const char *CCECProcessor::ToString(const cec_vendor_id vendor)
1330 {
1331 switch (vendor)
1332 {
1333 case CEC_VENDOR_SAMSUNG:
1334 return "Samsung";
1335 case CEC_VENDOR_LG:
1336 return "LG";
1337 case CEC_VENDOR_PANASONIC:
1338 return "Panasonic";
1339 case CEC_VENDOR_PIONEER:
1340 return "Pioneer";
1341 case CEC_VENDOR_ONKYO:
1342 return "Onkyo";
1343 case CEC_VENDOR_YAMAHA:
1344 return "Yamaha";
1345 case CEC_VENDOR_PHILIPS:
1346 return "Philips";
1347 default:
1348 return "Unknown";
1349 }
1350 }
1351
1352 void *CCECBusScan::Process(void)
1353 {
1354 CCECBusDevice *device(NULL);
1355 uint8_t iCounter(0);
1356
1357 while (!IsStopped())
1358 {
1359 if (++iCounter < 10)
1360 {
1361 Sleep(1000);
1362 continue;
1363 }
1364 for (unsigned int iPtr = 0; iPtr <= 11 && !IsStopped(); iPtr++)
1365 {
1366 device = m_processor->m_busDevices[iPtr];
1367 WaitUntilIdle();
1368 if (device && device->GetStatus(true) == CEC_DEVICE_STATUS_PRESENT)
1369 {
1370 WaitUntilIdle();
1371 if (!IsStopped())
1372 device->GetVendorId();
1373
1374 WaitUntilIdle();
1375 if (!IsStopped())
1376 device->GetPowerStatus(true);
1377 }
1378 }
1379 }
1380
1381 return NULL;
1382 }
1383
1384 void CCECBusScan::WaitUntilIdle(void)
1385 {
1386 if (IsStopped())
1387 return;
1388
1389 int32_t iWaitTime = 3000 - (int32_t)(GetTimeMs() - m_processor->GetLastTransmission());
1390 while (iWaitTime > 0)
1391 {
1392 Sleep(iWaitTime);
1393 iWaitTime = 3000 - (int32_t)(GetTimeMs() - m_processor->GetLastTransmission());
1394 }
1395 }
1396
1397 bool CCECProcessor::StartBootloader(void)
1398 {
1399 return m_communication->StartBootloader();
1400 }
1401
1402 bool CCECProcessor::PingAdapter(void)
1403 {
1404 return m_communication->PingAdapter();
1405 }