7f688dd134295872bc710464921f121d669e5a66
[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 {
58 m_logicalAddresses.Clear();
59 m_logicalAddresses.Set(iLogicalAddress);
60 m_types.clear();
61 for (int iPtr = 0; iPtr <= 16; iPtr++)
62 m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, iPtr == iLogicalAddress ? iPhysicalAddress : 0);
63 }
64
65 CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm, const char *strDeviceName, const cec_device_type_list &types) :
66 m_bStarted(false),
67 m_iHDMIPort(CEC_DEFAULT_HDMI_PORT),
68 m_strDeviceName(strDeviceName),
69 m_types(types),
70 m_communication(serComm),
71 m_controller(controller),
72 m_bMonitor(false)
73 {
74 m_logicalAddresses.Clear();
75 for (int iPtr = 0; iPtr < 16; iPtr++)
76 {
77 switch(iPtr)
78 {
79 case CECDEVICE_AUDIOSYSTEM:
80 m_busDevices[iPtr] = new CCECAudioSystem(this, (cec_logical_address) iPtr, 0xFFFF);
81 break;
82 case CECDEVICE_PLAYBACKDEVICE1:
83 case CECDEVICE_PLAYBACKDEVICE2:
84 case CECDEVICE_PLAYBACKDEVICE3:
85 m_busDevices[iPtr] = new CCECPlaybackDevice(this, (cec_logical_address) iPtr, 0xFFFF);
86 break;
87 case CECDEVICE_RECORDINGDEVICE1:
88 case CECDEVICE_RECORDINGDEVICE2:
89 case CECDEVICE_RECORDINGDEVICE3:
90 m_busDevices[iPtr] = new CCECRecordingDevice(this, (cec_logical_address) iPtr, 0xFFFF);
91 break;
92 case CECDEVICE_TUNER1:
93 case CECDEVICE_TUNER2:
94 case CECDEVICE_TUNER3:
95 case CECDEVICE_TUNER4:
96 m_busDevices[iPtr] = new CCECTuner(this, (cec_logical_address) iPtr, 0xFFFF);
97 break;
98 case CECDEVICE_TV:
99 m_busDevices[iPtr] = new CCECTV(this, (cec_logical_address) iPtr, 0);
100 break;
101 default:
102 m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, 0xFFFF);
103 break;
104 }
105 }
106 }
107
108 CCECProcessor::~CCECProcessor(void)
109 {
110 m_startCondition.Broadcast();
111 StopThread();
112 m_communication = NULL;
113 m_controller = NULL;
114 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
115 delete m_busDevices[iPtr];
116 }
117
118 bool CCECProcessor::Start(void)
119 {
120 CLockObject lock(&m_mutex);
121 if (!m_communication || !m_communication->IsOpen())
122 {
123 m_controller->AddLog(CEC_LOG_ERROR, "connection is closed");
124 return false;
125 }
126
127 if (CreateThread())
128 {
129 if (!m_startCondition.Wait(&m_mutex) || !m_bStarted)
130 {
131 m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
132 return false;
133 }
134 return true;
135 }
136 else
137 m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
138
139 return false;
140 }
141
142 bool CCECProcessor::TryLogicalAddress(cec_logical_address address)
143 {
144 if (m_busDevices[address]->TryLogicalAddress())
145 {
146 /* only set our OSD name and active source for the primary device */
147 if (m_logicalAddresses.IsEmpty())
148 {
149 m_busDevices[address]->m_strDeviceName = m_strDeviceName;
150 m_busDevices[address]->m_bActiveSource = true;
151 }
152 m_logicalAddresses.Set(address);
153 return true;
154 }
155
156 return false;
157 }
158
159 bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
160 {
161 AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'recording device'");
162 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1) ||
163 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2) ||
164 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3);
165 }
166
167 bool CCECProcessor::FindLogicalAddressTuner(void)
168 {
169 AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'tuner'");
170 return TryLogicalAddress(CECDEVICE_TUNER1) ||
171 TryLogicalAddress(CECDEVICE_TUNER2) ||
172 TryLogicalAddress(CECDEVICE_TUNER3) ||
173 TryLogicalAddress(CECDEVICE_TUNER4);
174 }
175
176 bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
177 {
178 AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'playback device'");
179 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1) ||
180 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2) ||
181 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3);
182 }
183
184 bool CCECProcessor::FindLogicalAddressAudioSystem(void)
185 {
186 AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'audio'");
187 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM);
188 }
189
190 bool CCECProcessor::FindLogicalAddresses(void)
191 {
192 bool bReturn(true);
193 m_logicalAddresses.Clear();
194 CStdString strLog;
195
196 for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
197 {
198 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RESERVED)
199 continue;
200
201 strLog.Format("%s - device %d: type %d", __FUNCTION__, iPtr, m_types.types[iPtr]);
202 AddLog(CEC_LOG_DEBUG, strLog);
203
204 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RECORDING_DEVICE)
205 bReturn &= FindLogicalAddressRecordingDevice();
206 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_TUNER)
207 bReturn &= FindLogicalAddressTuner();
208 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE)
209 bReturn &= FindLogicalAddressPlaybackDevice();
210 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
211 bReturn &= FindLogicalAddressAudioSystem();
212 }
213
214 return bReturn;
215 }
216
217 void *CCECProcessor::Process(void)
218 {
219 bool bParseFrame(false);
220 cec_command command;
221 CCECAdapterMessage msg;
222
223 {
224 if (m_logicalAddresses.IsEmpty() && !FindLogicalAddresses())
225 {
226 CLockObject lock(&m_mutex);
227 m_controller->AddLog(CEC_LOG_ERROR, "could not detect our logical addresses");
228 m_startCondition.Signal();
229 return NULL;
230 }
231
232 SetAckMask(m_logicalAddresses.AckMask());
233
234 ScanCECBus();
235
236 {
237 CLockObject lock(&m_mutex);
238 m_bStarted = true;
239 lock.Leave();
240
241 SetHDMIPort(m_iHDMIPort);
242
243 lock.Lock();
244 m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
245 m_startCondition.Signal();
246 }
247 }
248
249 while (!IsStopped())
250 {
251 command.Clear();
252 msg.clear();
253
254 {
255 CLockObject lock(&m_mutex);
256 if (m_commandBuffer.Pop(command))
257 {
258 bParseFrame = true;
259 }
260 else if (m_communication->IsOpen() && m_communication->Read(msg, 50))
261 {
262 m_controller->AddLog(msg.is_error() ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
263 if ((bParseFrame = (ParseMessage(msg) && !IsStopped())) == true)
264 command = m_currentframe;
265 }
266 }
267
268 if (bParseFrame)
269 ParseCommand(command);
270 bParseFrame = false;
271
272 Sleep(5);
273
274 m_controller->CheckKeypressTimeout();
275
276 for (unsigned int iDevicePtr = 0; iDevicePtr < 16; iDevicePtr++)
277 {
278 if (!m_logicalAddresses[iDevicePtr])
279 m_busDevices[iDevicePtr]->PollVendorId();
280 }
281
282 Sleep(5);
283 }
284
285 return NULL;
286 }
287
288 bool CCECProcessor::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RESERVED */)
289 {
290 bool bReturn(false);
291
292 if (!IsRunning())
293 return bReturn;
294
295 cec_logical_address addr = m_logicalAddresses.primary;
296
297 if (type != CEC_DEVICE_TYPE_RESERVED)
298 {
299 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
300 {
301 if (m_logicalAddresses[iPtr] && m_busDevices[iPtr]->m_type == type)
302 {
303 addr = (cec_logical_address) iPtr;
304 break;
305 }
306 }
307 }
308
309 return SetStreamPath(m_busDevices[addr]->GetPhysicalAddress(false)) &&
310 m_busDevices[addr]->TransmitActiveSource();
311 }
312
313 bool CCECProcessor::SetActiveSource(cec_logical_address iAddress)
314 {
315 return SetStreamPath(m_busDevices[iAddress]->GetPhysicalAddress(false));
316 }
317
318 bool CCECProcessor::SetActiveView(void)
319 {
320 return SetActiveSource(m_types.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED : m_types[0]);
321 }
322
323 bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode, bool bSendUpdate /* = true */)
324 {
325 bool bReturn(false);
326
327 CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
328 if (device)
329 {
330 ((CCECPlaybackDevice *) device)->SetDeckControlMode(mode);
331 if (bSendUpdate)
332 ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV);
333 bReturn = true;
334 }
335
336 return bReturn;
337 }
338
339 bool CCECProcessor::SetDeckInfo(cec_deck_info info, bool bSendUpdate /* = true */)
340 {
341 bool bReturn(false);
342
343 CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
344 if (device)
345 {
346 ((CCECPlaybackDevice *) device)->SetDeckStatus(info);
347 if (bSendUpdate)
348 ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV);
349 bReturn = true;
350 }
351
352 return bReturn;
353 }
354
355 bool CCECProcessor::SetHDMIPort(uint8_t iPort)
356 {
357 bool bReturn(false);
358
359 CStdString strLog;
360 strLog.Format("setting HDMI port to %d", iPort);
361 AddLog(CEC_LOG_DEBUG, strLog);
362
363 m_iHDMIPort = iPort;
364 if (!m_bStarted)
365 return true;
366
367 uint16_t iPhysicalAddress(0);
368 int iPos = 3;
369 while(!bReturn && iPos >= 0)
370 {
371 iPhysicalAddress += ((uint16_t)iPort * (0x1 << iPos*4));
372 strLog.Format("checking physical address %4x", iPhysicalAddress);
373 AddLog(CEC_LOG_DEBUG, strLog);
374 if (CheckPhysicalAddress(iPhysicalAddress))
375 {
376 strLog.Format("physical address %4x is in use", iPhysicalAddress);
377 AddLog(CEC_LOG_DEBUG, strLog);
378 iPos--;
379 }
380 else
381 {
382 SetPhysicalAddress(iPhysicalAddress);
383 bReturn = true;
384 }
385 }
386
387 return bReturn;
388 }
389
390 void CCECProcessor::ScanCECBus(void)
391 {
392 CCECBusDevice *device(NULL);
393 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
394 {
395 device = m_busDevices[iPtr];
396 if (device && device->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
397 {
398 device->GetPhysicalAddress();
399 device->GetVendorId();
400 device->GetCecVersion();
401 device->GetPowerStatus();
402 }
403 }
404 }
405
406 bool CCECProcessor::CheckPhysicalAddress(uint16_t iPhysicalAddress)
407 {
408 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
409 {
410 if (m_busDevices[iPtr]->GetPhysicalAddress(false) == iPhysicalAddress)
411 return true;
412 }
413 return false;
414 }
415
416 bool CCECProcessor::SetStreamPath(uint16_t iStreamPath)
417 {
418 bool bReturn(false);
419
420 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamPath);
421 if (device)
422 {
423 device->SetActiveDevice();
424 bReturn = true;
425 }
426
427 return bReturn;
428 }
429
430 bool CCECProcessor::SetInactiveView(void)
431 {
432 if (!IsRunning())
433 return false;
434
435 if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary])
436 return m_busDevices[m_logicalAddresses.primary]->TransmitInactiveView();
437 return false;
438 }
439
440 void CCECProcessor::LogOutput(const cec_command &data)
441 {
442 CStdString strTx;
443 strTx.Format("<< %02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination);
444 if (data.opcode_set)
445 strTx.AppendFormat(":%02x", (uint8_t)data.opcode);
446
447 for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++)
448 strTx.AppendFormat(":%02x", data.parameters[iPtr]);
449 m_controller->AddLog(CEC_LOG_TRAFFIC, strTx.c_str());
450 }
451
452 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress)
453 {
454 if (m_logicalAddresses.primary != iLogicalAddress)
455 {
456 CStdString strLog;
457 strLog.Format("<< setting primary logical address to %1x", iLogicalAddress);
458 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
459 m_logicalAddresses.primary = iLogicalAddress;
460 m_logicalAddresses.Set(iLogicalAddress);
461 return SetAckMask(m_logicalAddresses.AckMask());
462 }
463
464 return true;
465 }
466
467 bool CCECProcessor::SetMenuState(cec_menu_state state, bool bSendUpdate /* = true */)
468 {
469 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
470 {
471 if (m_logicalAddresses[iPtr])
472 m_busDevices[iPtr]->SetMenuState(state);
473 }
474
475 if (bSendUpdate)
476 m_busDevices[m_logicalAddresses.primary]->TransmitMenuState(CECDEVICE_TV);
477
478 return true;
479 }
480
481 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress)
482 {
483 if (!m_logicalAddresses.IsEmpty())
484 {
485 for (uint8_t iPtr = 0; iPtr < 15; iPtr++)
486 if (m_logicalAddresses[iPtr])
487 m_busDevices[iPtr]->SetPhysicalAddress(iPhysicalAddress);
488 return SetActiveView();
489 }
490 return false;
491 }
492
493 bool CCECProcessor::SwitchMonitoring(bool bEnable)
494 {
495 CStdString strLog;
496 strLog.Format("== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
497 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
498
499 m_bMonitor = bEnable;
500 if (bEnable)
501 return SetAckMask(0);
502 else
503 return SetAckMask(m_logicalAddresses.AckMask());
504 }
505
506 bool CCECProcessor::PollDevice(cec_logical_address iAddress)
507 {
508 if (iAddress != CECDEVICE_UNKNOWN && m_busDevices[iAddress])
509 {
510 return m_logicalAddresses.primary == CECDEVICE_UNKNOWN ?
511 m_busDevices[iAddress]->TransmitPoll(iAddress) :
512 m_busDevices[m_logicalAddresses.primary]->TransmitPoll(iAddress);
513 }
514 return false;
515 }
516
517 uint8_t CCECProcessor::VolumeUp(bool bWait /* = true */)
518 {
519 uint8_t status = 0;
520 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM))
521 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeUp(bWait);
522
523 return status;
524 }
525
526 uint8_t CCECProcessor::VolumeDown(bool bWait /* = true */)
527 {
528 uint8_t status = 0;
529 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM))
530 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeDown(bWait);
531
532 return status;
533 }
534
535 uint8_t CCECProcessor::MuteAudio(bool bWait /* = true */)
536 {
537 uint8_t status = 0;
538 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM))
539 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->MuteAudio(bWait);
540
541 return status;
542 }
543
544 CCECBusDevice *CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bRefresh /* = false */) const
545 {
546 if (m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false) == iPhysicalAddress)
547 return m_busDevices[m_logicalAddresses.primary];
548
549 CCECBusDevice *device = NULL;
550 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
551 {
552 if (m_busDevices[iPtr]->GetPhysicalAddress(bRefresh) == iPhysicalAddress)
553 {
554 device = m_busDevices[iPtr];
555 break;
556 }
557 }
558
559 return device;
560 }
561
562 CCECBusDevice *CCECProcessor::GetDeviceByType(cec_device_type type) const
563 {
564 CCECBusDevice *device = NULL;
565
566 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
567 {
568 if (m_busDevices[iPtr]->m_type == type)
569 {
570 device = m_busDevices[iPtr];
571 break;
572 }
573 }
574
575 return device;
576 }
577
578 cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress)
579 {
580 return m_busDevices[iAddress]->GetCecVersion();
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 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
761 {
762 if (m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
763 addresses.Set((cec_logical_address) iPtr);
764 }
765 return addresses;
766 }
767
768 bool CCECProcessor::IsActiveDevice(cec_logical_address address)
769 {
770 return m_busDevices[address]->GetStatus() == CEC_DEVICE_STATUS_PRESENT;
771 }
772
773 bool CCECProcessor::IsActiveDeviceType(cec_device_type type)
774 {
775 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
776 {
777 if (m_busDevices[iPtr]->GetType() == type && m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
778 return true;
779 }
780
781 return false;
782 }
783
784 uint16_t CCECProcessor::GetPhysicalAddress(void) const
785 {
786 if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary])
787 return m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false);
788 return false;
789 }
790
791 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode)
792 {
793 m_controller->SetCurrentButton(iButtonCode);
794 }
795
796 void CCECProcessor::AddCommand(const cec_command &command)
797 {
798 m_controller->AddCommand(command);
799 }
800
801 void CCECProcessor::AddKey(cec_keypress &key)
802 {
803 m_controller->AddKey(key);
804 }
805
806 void CCECProcessor::AddKey(void)
807 {
808 m_controller->AddKey();
809 }
810
811 void CCECProcessor::AddLog(cec_log_level level, const CStdString &strMessage)
812 {
813 m_controller->AddLog(level, strMessage);
814 }
815
816 bool CCECProcessor::SetAckMask(uint16_t iMask)
817 {
818 bool bReturn(false);
819 CStdString strLog;
820 strLog.Format("setting ackmask to %2x", iMask);
821 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
822
823 CCECAdapterMessage *output = new CCECAdapterMessage;
824
825 output->push_back(MSGSTART);
826 output->push_escaped(MSGCODE_SET_ACK_MASK);
827 output->push_escaped(iMask >> 8);
828 output->push_escaped((uint8_t)iMask);
829 output->push_back(MSGEND);
830
831 if ((bReturn = Transmit(output)) == false)
832 m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask");
833
834 delete output;
835
836 return bReturn;
837 }
838
839 bool CCECProcessor::SendKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = false */)
840 {
841 return m_busDevices[iDestination]->SendKeypress(key, bWait);
842 }
843
844 bool CCECProcessor::SendKeyRelease(cec_logical_address iDestination, bool bWait /* = false */)
845 {
846 return m_busDevices[iDestination]->SendKeyRelease(bWait);
847 }