cec: perform the cec bus scan in another thread
[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, CAdapterCommunication *serComm, 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_strDeviceName(strDeviceName),
54 m_communication(serComm),
55 m_controller(controller),
56 m_bMonitor(false),
57 m_busScan(NULL)
58 {
59 m_logicalAddresses.Clear();
60 m_logicalAddresses.Set(iLogicalAddress);
61 m_types.clear();
62 for (int iPtr = 0; iPtr <= 16; iPtr++)
63 m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, iPtr == iLogicalAddress ? iPhysicalAddress : 0);
64 }
65
66 CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm, const char *strDeviceName, const cec_device_type_list &types) :
67 m_bStarted(false),
68 m_iHDMIPort(CEC_DEFAULT_HDMI_PORT),
69 m_strDeviceName(strDeviceName),
70 m_types(types),
71 m_communication(serComm),
72 m_controller(controller),
73 m_bMonitor(false)
74 {
75 m_logicalAddresses.Clear();
76 for (int iPtr = 0; iPtr < 16; iPtr++)
77 {
78 switch(iPtr)
79 {
80 case CECDEVICE_AUDIOSYSTEM:
81 m_busDevices[iPtr] = new CCECAudioSystem(this, (cec_logical_address) iPtr, 0xFFFF);
82 break;
83 case CECDEVICE_PLAYBACKDEVICE1:
84 case CECDEVICE_PLAYBACKDEVICE2:
85 case CECDEVICE_PLAYBACKDEVICE3:
86 m_busDevices[iPtr] = new CCECPlaybackDevice(this, (cec_logical_address) iPtr, 0xFFFF);
87 break;
88 case CECDEVICE_RECORDINGDEVICE1:
89 case CECDEVICE_RECORDINGDEVICE2:
90 case CECDEVICE_RECORDINGDEVICE3:
91 m_busDevices[iPtr] = new CCECRecordingDevice(this, (cec_logical_address) iPtr, 0xFFFF);
92 break;
93 case CECDEVICE_TUNER1:
94 case CECDEVICE_TUNER2:
95 case CECDEVICE_TUNER3:
96 case CECDEVICE_TUNER4:
97 m_busDevices[iPtr] = new CCECTuner(this, (cec_logical_address) iPtr, 0xFFFF);
98 break;
99 case CECDEVICE_TV:
100 m_busDevices[iPtr] = new CCECTV(this, (cec_logical_address) iPtr, 0);
101 break;
102 default:
103 m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, 0xFFFF);
104 break;
105 }
106 }
107 }
108
109 CCECProcessor::~CCECProcessor(void)
110 {
111 if (m_busScan)
112 {
113 m_busScan->StopThread();
114 delete m_busScan;
115 }
116
117 m_startCondition.Broadcast();
118 StopThread();
119
120 m_communication = NULL;
121 m_controller = NULL;
122 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
123 delete m_busDevices[iPtr];
124 }
125
126 bool CCECProcessor::Start(void)
127 {
128 CLockObject lock(&m_mutex);
129 if (!m_communication || !m_communication->IsOpen())
130 {
131 m_controller->AddLog(CEC_LOG_ERROR, "connection is closed");
132 return false;
133 }
134
135 if (CreateThread())
136 {
137 if (!m_startCondition.Wait(&m_mutex) || !m_bStarted)
138 {
139 m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
140 return false;
141 }
142
143 lock.Leave();
144 if (SetAckMask(m_logicalAddresses.AckMask()) &&
145 SetHDMIPort(m_iHDMIPort, true))
146 {
147 m_busScan = new CCECBusScan(this);
148 m_busScan->CreateThread(true);
149 return true;
150 }
151 }
152 else
153 m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
154
155 return false;
156 }
157
158 bool CCECProcessor::TryLogicalAddress(cec_logical_address address)
159 {
160 if (m_busDevices[address]->TryLogicalAddress())
161 {
162 /* only set our OSD name and active source for the primary device */
163 if (m_logicalAddresses.IsEmpty())
164 {
165 m_busDevices[address]->m_strDeviceName = m_strDeviceName;
166 m_busDevices[address]->m_bActiveSource = true;
167 }
168 m_logicalAddresses.Set(address);
169 return true;
170 }
171
172 return false;
173 }
174
175 bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
176 {
177 AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'recording device'");
178 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1) ||
179 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2) ||
180 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3);
181 }
182
183 bool CCECProcessor::FindLogicalAddressTuner(void)
184 {
185 AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'tuner'");
186 return TryLogicalAddress(CECDEVICE_TUNER1) ||
187 TryLogicalAddress(CECDEVICE_TUNER2) ||
188 TryLogicalAddress(CECDEVICE_TUNER3) ||
189 TryLogicalAddress(CECDEVICE_TUNER4);
190 }
191
192 bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
193 {
194 AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'playback device'");
195 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1) ||
196 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2) ||
197 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3);
198 }
199
200 bool CCECProcessor::FindLogicalAddressAudioSystem(void)
201 {
202 AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'audio'");
203 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM);
204 }
205
206 bool CCECProcessor::FindLogicalAddresses(void)
207 {
208 bool bReturn(true);
209 m_logicalAddresses.Clear();
210 CStdString strLog;
211
212 for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
213 {
214 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RESERVED)
215 continue;
216
217 strLog.Format("%s - device %d: type %d", __FUNCTION__, iPtr, m_types.types[iPtr]);
218 AddLog(CEC_LOG_DEBUG, strLog);
219
220 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RECORDING_DEVICE)
221 bReturn &= FindLogicalAddressRecordingDevice();
222 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_TUNER)
223 bReturn &= FindLogicalAddressTuner();
224 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE)
225 bReturn &= FindLogicalAddressPlaybackDevice();
226 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
227 bReturn &= FindLogicalAddressAudioSystem();
228 }
229
230 return bReturn;
231 }
232
233 void *CCECProcessor::Process(void)
234 {
235 bool bParseFrame(false);
236 cec_command command;
237 CCECAdapterMessage msg;
238
239 if (m_logicalAddresses.IsEmpty() && !FindLogicalAddresses())
240 {
241 CLockObject lock(&m_mutex);
242 m_controller->AddLog(CEC_LOG_ERROR, "could not detect our logical addresses");
243 m_startCondition.Signal();
244 return NULL;
245 }
246 else
247 {
248 CLockObject lock(&m_mutex);
249 m_bStarted = true;
250 m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
251 m_startCondition.Signal();
252 }
253
254 while (!IsStopped())
255 {
256 command.Clear();
257 msg.clear();
258
259 {
260 CLockObject lock(&m_mutex);
261 if (m_commandBuffer.Pop(command))
262 {
263 bParseFrame = true;
264 }
265 else if (m_communication->IsOpen() && m_communication->Read(msg, 50))
266 {
267 m_controller->AddLog(msg.is_error() ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
268 if ((bParseFrame = (ParseMessage(msg) && !IsStopped())) == true)
269 command = m_currentframe;
270 }
271 }
272
273 if (bParseFrame)
274 ParseCommand(command);
275 bParseFrame = false;
276
277 Sleep(5);
278
279 m_controller->CheckKeypressTimeout();
280
281 for (uint8_t iDevicePtr = 0; iDevicePtr < 16; iDevicePtr++)
282 {
283 if (!m_logicalAddresses[iDevicePtr])
284 m_busDevices[iDevicePtr]->PollVendorId();
285 }
286
287 Sleep(5);
288 }
289
290 return NULL;
291 }
292
293 bool CCECProcessor::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RESERVED */)
294 {
295 bool bReturn(false);
296
297 if (!IsRunning())
298 return bReturn;
299
300 cec_logical_address addr = m_logicalAddresses.primary;
301
302 if (type != CEC_DEVICE_TYPE_RESERVED)
303 {
304 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
305 {
306 if (m_logicalAddresses[iPtr] && m_busDevices[iPtr]->m_type == type)
307 {
308 addr = (cec_logical_address) iPtr;
309 break;
310 }
311 }
312 }
313
314 return SetStreamPath(m_busDevices[addr]->GetPhysicalAddress(false)) &&
315 m_busDevices[addr]->TransmitActiveSource();
316 }
317
318 bool CCECProcessor::SetActiveSource(cec_logical_address iAddress)
319 {
320 return SetStreamPath(m_busDevices[iAddress]->GetPhysicalAddress(false));
321 }
322
323 bool CCECProcessor::SetActiveView(void)
324 {
325 return SetActiveSource(m_types.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED : m_types[0]);
326 }
327
328 bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode, bool bSendUpdate /* = true */)
329 {
330 bool bReturn(false);
331
332 CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
333 if (device)
334 {
335 ((CCECPlaybackDevice *) device)->SetDeckControlMode(mode);
336 if (bSendUpdate)
337 ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV);
338 bReturn = true;
339 }
340
341 return bReturn;
342 }
343
344 bool CCECProcessor::SetDeckInfo(cec_deck_info info, bool bSendUpdate /* = true */)
345 {
346 bool bReturn(false);
347
348 CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
349 if (device)
350 {
351 ((CCECPlaybackDevice *) device)->SetDeckStatus(info);
352 if (bSendUpdate)
353 ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV);
354 bReturn = true;
355 }
356
357 return bReturn;
358 }
359
360 bool CCECProcessor::SetHDMIPort(uint8_t iPort, bool bForce /* = false */)
361 {
362 bool bReturn(false);
363
364 CStdString strLog;
365 strLog.Format("setting HDMI port to %d", iPort);
366 AddLog(CEC_LOG_DEBUG, strLog);
367
368 m_iHDMIPort = iPort;
369 if (!m_bStarted && !bForce)
370 return true;
371
372 uint16_t iPhysicalAddress(0);
373 int iPos = 3;
374 while(!bReturn && iPos >= 0)
375 {
376 iPhysicalAddress += ((uint16_t)iPort * (0x1 << iPos*4));
377 strLog.Format("checking physical address %4x", iPhysicalAddress);
378 AddLog(CEC_LOG_DEBUG, strLog);
379 if (CheckPhysicalAddress(iPhysicalAddress))
380 {
381 strLog.Format("physical address %4x is in use", iPhysicalAddress);
382 AddLog(CEC_LOG_DEBUG, strLog);
383 iPos--;
384 }
385 else
386 {
387 SetPhysicalAddress(iPhysicalAddress);
388 bReturn = true;
389 }
390 }
391
392 return bReturn;
393 }
394
395 bool CCECProcessor::CheckPhysicalAddress(uint16_t iPhysicalAddress)
396 {
397 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
398 {
399 if (m_busDevices[iPtr]->GetPhysicalAddress(false) == iPhysicalAddress)
400 return true;
401 }
402 return false;
403 }
404
405 bool CCECProcessor::SetStreamPath(uint16_t iStreamPath)
406 {
407 bool bReturn(false);
408
409 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamPath);
410 if (device)
411 {
412 device->SetActiveDevice();
413 bReturn = true;
414 }
415
416 return bReturn;
417 }
418
419 bool CCECProcessor::TransmitInactiveSource(void)
420 {
421 if (!IsRunning())
422 return false;
423
424 if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary])
425 return m_busDevices[m_logicalAddresses.primary]->TransmitInactiveSource();
426 return false;
427 }
428
429 void CCECProcessor::LogOutput(const cec_command &data)
430 {
431 CStdString strTx;
432 strTx.Format("<< %02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination);
433 if (data.opcode_set)
434 strTx.AppendFormat(":%02x", (uint8_t)data.opcode);
435
436 for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++)
437 strTx.AppendFormat(":%02x", data.parameters[iPtr]);
438 m_controller->AddLog(CEC_LOG_TRAFFIC, strTx.c_str());
439 }
440
441 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress)
442 {
443 if (m_logicalAddresses.primary != iLogicalAddress)
444 {
445 CStdString strLog;
446 strLog.Format("<< setting primary logical address to %1x", iLogicalAddress);
447 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
448 m_logicalAddresses.primary = iLogicalAddress;
449 m_logicalAddresses.Set(iLogicalAddress);
450 return SetAckMask(m_logicalAddresses.AckMask());
451 }
452
453 return true;
454 }
455
456 bool CCECProcessor::SetMenuState(cec_menu_state state, bool bSendUpdate /* = true */)
457 {
458 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
459 {
460 if (m_logicalAddresses[iPtr])
461 m_busDevices[iPtr]->SetMenuState(state);
462 }
463
464 if (bSendUpdate)
465 m_busDevices[m_logicalAddresses.primary]->TransmitMenuState(CECDEVICE_TV);
466
467 return true;
468 }
469
470 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress)
471 {
472 if (!m_logicalAddresses.IsEmpty())
473 {
474 for (uint8_t iPtr = 0; iPtr < 15; iPtr++)
475 if (m_logicalAddresses[iPtr])
476 m_busDevices[iPtr]->SetPhysicalAddress(iPhysicalAddress);
477 return SetActiveView();
478 }
479 return false;
480 }
481
482 bool CCECProcessor::SwitchMonitoring(bool bEnable)
483 {
484 CStdString strLog;
485 strLog.Format("== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
486 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
487
488 m_bMonitor = bEnable;
489 if (bEnable)
490 return SetAckMask(0);
491 else
492 return SetAckMask(m_logicalAddresses.AckMask());
493 }
494
495 bool CCECProcessor::PollDevice(cec_logical_address iAddress)
496 {
497 if (iAddress != CECDEVICE_UNKNOWN && m_busDevices[iAddress])
498 {
499 return m_logicalAddresses.primary == CECDEVICE_UNKNOWN ?
500 m_busDevices[iAddress]->TransmitPoll(iAddress) :
501 m_busDevices[m_logicalAddresses.primary]->TransmitPoll(iAddress);
502 }
503 return false;
504 }
505
506 uint8_t CCECProcessor::VolumeUp(bool bWait /* = true */)
507 {
508 uint8_t status = 0;
509 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM))
510 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeUp(bWait);
511
512 return status;
513 }
514
515 uint8_t CCECProcessor::VolumeDown(bool bWait /* = true */)
516 {
517 uint8_t status = 0;
518 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM))
519 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeDown(bWait);
520
521 return status;
522 }
523
524 uint8_t CCECProcessor::MuteAudio(bool bWait /* = true */)
525 {
526 uint8_t status = 0;
527 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM))
528 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->MuteAudio(bWait);
529
530 return status;
531 }
532
533 CCECBusDevice *CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bRefresh /* = false */) const
534 {
535 if (m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false) == iPhysicalAddress)
536 return m_busDevices[m_logicalAddresses.primary];
537
538 CCECBusDevice *device = NULL;
539 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
540 {
541 if (m_busDevices[iPtr]->GetPhysicalAddress(bRefresh) == iPhysicalAddress)
542 {
543 device = m_busDevices[iPtr];
544 break;
545 }
546 }
547
548 return device;
549 }
550
551 CCECBusDevice *CCECProcessor::GetDeviceByType(cec_device_type type) const
552 {
553 CCECBusDevice *device = NULL;
554
555 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
556 {
557 if (m_busDevices[iPtr]->m_type == type)
558 {
559 device = m_busDevices[iPtr];
560 break;
561 }
562 }
563
564 return device;
565 }
566
567 cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress)
568 {
569 return m_busDevices[iAddress]->GetCecVersion();
570 }
571
572 cec_osd_name CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress)
573 {
574 CStdString strOSDName = m_busDevices[iAddress]->GetOSDName();
575 cec_osd_name retVal;
576
577 snprintf(retVal.name, sizeof(retVal.name), "%s", strOSDName.c_str());
578 retVal.device = iAddress;
579
580 return retVal;
581 }
582
583 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
584 {
585 if (m_busDevices[iAddress])
586 {
587 *language = m_busDevices[iAddress]->GetMenuLanguage();
588 return (strcmp(language->language, "???") != 0);
589 }
590 return false;
591 }
592
593 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress)
594 {
595 if (m_busDevices[iAddress])
596 return m_busDevices[iAddress]->GetVendorId();
597 return false;
598 }
599
600 cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress)
601 {
602 if (m_busDevices[iAddress])
603 return m_busDevices[iAddress]->GetPowerStatus();
604 return CEC_POWER_STATUS_UNKNOWN;
605 }
606
607 bool CCECProcessor::Transmit(const cec_command &data)
608 {
609 bool bReturn(false);
610 LogOutput(data);
611
612 CCECAdapterMessage *output = new CCECAdapterMessage(data);
613 bReturn = Transmit(output);
614 delete output;
615
616 return bReturn;
617 }
618
619 bool CCECProcessor::Transmit(CCECAdapterMessage *output)
620 {
621 bool bReturn(false);
622 CLockObject lock(&m_mutex);
623 {
624 CLockObject msgLock(&output->mutex);
625 if (!m_communication || !m_communication->Write(output))
626 return bReturn;
627 else
628 {
629 output->condition.Wait(&output->mutex);
630 if (output->state != ADAPTER_MESSAGE_STATE_SENT)
631 {
632 m_controller->AddLog(CEC_LOG_ERROR, "command was not sent");
633 return bReturn;
634 }
635 }
636
637 if (output->transmit_timeout > 0)
638 {
639 if ((bReturn = WaitForTransmitSucceeded(output->size(), output->transmit_timeout)) == false)
640 m_controller->AddLog(CEC_LOG_DEBUG, "did not receive ack");
641 }
642 else
643 bReturn = true;
644 }
645
646 return bReturn;
647 }
648
649 void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, cec_abort_reason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
650 {
651 m_controller->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
652
653 cec_command command;
654 // TODO
655 cec_command::Format(command, m_logicalAddresses.primary, address, CEC_OPCODE_FEATURE_ABORT);
656 command.parameters.PushBack((uint8_t)opcode);
657 command.parameters.PushBack((uint8_t)reason);
658
659 Transmit(command);
660 }
661
662 bool CCECProcessor::WaitForTransmitSucceeded(uint8_t iLength, uint32_t iTimeout /* = 1000 */)
663 {
664 bool bError(false);
665 bool bTransmitSucceeded(false);
666 uint8_t iPacketsLeft(iLength / 4);
667
668 int64_t iNow = GetTimeMs();
669 int64_t iTargetTime = iNow + (uint64_t) iTimeout;
670
671 while (!bTransmitSucceeded && !bError && (iTimeout == 0 || iNow < iTargetTime))
672 {
673 CCECAdapterMessage msg;
674
675 if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000))
676 {
677 iNow = GetTimeMs();
678 continue;
679 }
680
681 if ((bError = msg.is_error()) == false)
682 {
683 m_controller->AddLog(bError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
684
685 switch(msg.message())
686 {
687 case MSGCODE_COMMAND_ACCEPTED:
688 if (iPacketsLeft > 0)
689 iPacketsLeft--;
690 break;
691 case MSGCODE_TRANSMIT_SUCCEEDED:
692 bTransmitSucceeded = (iPacketsLeft == 0);
693 bError = !bTransmitSucceeded;
694 break;
695 default:
696 if (ParseMessage(msg))
697 m_commandBuffer.Push(m_currentframe);
698 }
699
700 iNow = GetTimeMs();
701 }
702 }
703
704 return bTransmitSucceeded && !bError;
705 }
706
707 bool CCECProcessor::ParseMessage(const CCECAdapterMessage &msg)
708 {
709 bool bEom = false;
710
711 if (msg.empty())
712 return bEom;
713
714 switch(msg.message())
715 {
716 case MSGCODE_FRAME_START:
717 {
718 m_currentframe.Clear();
719 if (msg.size() >= 2)
720 {
721 m_currentframe.initiator = msg.initiator();
722 m_currentframe.destination = msg.destination();
723 m_currentframe.ack = msg.ack();
724 m_currentframe.eom = msg.eom();
725 }
726 }
727 break;
728 case MSGCODE_FRAME_DATA:
729 {
730 if (msg.size() >= 2)
731 {
732 m_currentframe.PushBack(msg[1]);
733 m_currentframe.eom = msg.eom();
734 }
735 bEom = msg.eom();
736 }
737 break;
738 default:
739 break;
740 }
741
742 return bEom;
743 }
744
745 void CCECProcessor::ParseCommand(cec_command &command)
746 {
747 CStdString dataStr;
748 dataStr.Format(">> %1x%1x:%02x", command.initiator, command.destination, command.opcode);
749 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
750 dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
751 m_controller->AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
752
753 if (!m_bMonitor && command.initiator >= CECDEVICE_TV && command.initiator <= CECDEVICE_BROADCAST)
754 m_busDevices[(uint8_t)command.initiator]->HandleCommand(command);
755 }
756
757 cec_logical_addresses CCECProcessor::GetActiveDevices(void)
758 {
759 cec_logical_addresses addresses;
760 addresses.Clear();
761 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
762 {
763 if (m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
764 addresses.Set((cec_logical_address) iPtr);
765 }
766 return addresses;
767 }
768
769 bool CCECProcessor::IsActiveDevice(cec_logical_address address)
770 {
771 return m_busDevices[address]->GetStatus() == CEC_DEVICE_STATUS_PRESENT;
772 }
773
774 bool CCECProcessor::IsActiveDeviceType(cec_device_type type)
775 {
776 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
777 {
778 if (m_busDevices[iPtr]->GetType() == type && m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
779 return true;
780 }
781
782 return false;
783 }
784
785 uint16_t CCECProcessor::GetPhysicalAddress(void) const
786 {
787 if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary])
788 return m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false);
789 return false;
790 }
791
792 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode)
793 {
794 m_controller->SetCurrentButton(iButtonCode);
795 }
796
797 void CCECProcessor::AddCommand(const cec_command &command)
798 {
799 m_controller->AddCommand(command);
800 }
801
802 void CCECProcessor::AddKey(cec_keypress &key)
803 {
804 m_controller->AddKey(key);
805 }
806
807 void CCECProcessor::AddKey(void)
808 {
809 m_controller->AddKey();
810 }
811
812 void CCECProcessor::AddLog(cec_log_level level, const CStdString &strMessage)
813 {
814 m_controller->AddLog(level, strMessage);
815 }
816
817 bool CCECProcessor::SetAckMask(uint16_t iMask)
818 {
819 bool bReturn(false);
820 CStdString strLog;
821 strLog.Format("setting ackmask to %2x", iMask);
822 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
823
824 CCECAdapterMessage *output = new CCECAdapterMessage;
825
826 output->push_back(MSGSTART);
827 output->push_escaped(MSGCODE_SET_ACK_MASK);
828 output->push_escaped(iMask >> 8);
829 output->push_escaped((uint8_t)iMask);
830 output->push_back(MSGEND);
831
832 if ((bReturn = Transmit(output)) == false)
833 m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask");
834
835 delete output;
836
837 return bReturn;
838 }
839
840 bool CCECProcessor::SendKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = false */)
841 {
842 return m_busDevices[iDestination]->SendKeypress(key, bWait);
843 }
844
845 bool CCECProcessor::SendKeyRelease(cec_logical_address iDestination, bool bWait /* = false */)
846 {
847 return m_busDevices[iDestination]->SendKeyRelease(bWait);
848 }
849
850 void *CCECBusScan::Process(void)
851 {
852 CCECBusDevice *device(NULL);
853 for (unsigned int iPtr = 0; iPtr < 15 && !IsStopped(); iPtr++)
854 {
855 device = m_processor->m_busDevices[iPtr];
856 if (device && device->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
857 {
858 if (!IsStopped())
859 device->GetPhysicalAddress();
860 Sleep(5);
861
862 if (!IsStopped())
863 device->GetCecVersion();
864 Sleep(5);
865
866 if (!IsStopped())
867 device->GetVendorId();
868 Sleep(5);
869 }
870 }
871 return NULL;
872 }