772f1e2e2e36e0417a9d2ae92afdd594b0387003
[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 (uint8_t 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->GetCecVersion();
400 device->GetVendorId();
401 }
402 }
403 }
404
405 bool CCECProcessor::CheckPhysicalAddress(uint16_t iPhysicalAddress)
406 {
407 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
408 {
409 if (m_busDevices[iPtr]->GetPhysicalAddress(false) == iPhysicalAddress)
410 return true;
411 }
412 return false;
413 }
414
415 bool CCECProcessor::SetStreamPath(uint16_t iStreamPath)
416 {
417 bool bReturn(false);
418
419 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamPath);
420 if (device)
421 {
422 device->SetActiveDevice();
423 bReturn = true;
424 }
425
426 return bReturn;
427 }
428
429 bool CCECProcessor::SetInactiveView(void)
430 {
431 if (!IsRunning())
432 return false;
433
434 if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary])
435 return m_busDevices[m_logicalAddresses.primary]->TransmitInactiveView();
436 return false;
437 }
438
439 void CCECProcessor::LogOutput(const cec_command &data)
440 {
441 CStdString strTx;
442 strTx.Format("<< %02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination);
443 if (data.opcode_set)
444 strTx.AppendFormat(":%02x", (uint8_t)data.opcode);
445
446 for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++)
447 strTx.AppendFormat(":%02x", data.parameters[iPtr]);
448 m_controller->AddLog(CEC_LOG_TRAFFIC, strTx.c_str());
449 }
450
451 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress)
452 {
453 if (m_logicalAddresses.primary != iLogicalAddress)
454 {
455 CStdString strLog;
456 strLog.Format("<< setting primary logical address to %1x", iLogicalAddress);
457 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
458 m_logicalAddresses.primary = iLogicalAddress;
459 m_logicalAddresses.Set(iLogicalAddress);
460 return SetAckMask(m_logicalAddresses.AckMask());
461 }
462
463 return true;
464 }
465
466 bool CCECProcessor::SetMenuState(cec_menu_state state, bool bSendUpdate /* = true */)
467 {
468 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
469 {
470 if (m_logicalAddresses[iPtr])
471 m_busDevices[iPtr]->SetMenuState(state);
472 }
473
474 if (bSendUpdate)
475 m_busDevices[m_logicalAddresses.primary]->TransmitMenuState(CECDEVICE_TV);
476
477 return true;
478 }
479
480 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress)
481 {
482 if (!m_logicalAddresses.IsEmpty())
483 {
484 for (uint8_t iPtr = 0; iPtr < 15; iPtr++)
485 if (m_logicalAddresses[iPtr])
486 m_busDevices[iPtr]->SetPhysicalAddress(iPhysicalAddress);
487 return SetActiveView();
488 }
489 return false;
490 }
491
492 bool CCECProcessor::SwitchMonitoring(bool bEnable)
493 {
494 CStdString strLog;
495 strLog.Format("== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
496 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
497
498 m_bMonitor = bEnable;
499 if (bEnable)
500 return SetAckMask(0);
501 else
502 return SetAckMask(m_logicalAddresses.AckMask());
503 }
504
505 bool CCECProcessor::PollDevice(cec_logical_address iAddress)
506 {
507 if (iAddress != CECDEVICE_UNKNOWN && m_busDevices[iAddress])
508 {
509 return m_logicalAddresses.primary == CECDEVICE_UNKNOWN ?
510 m_busDevices[iAddress]->TransmitPoll(iAddress) :
511 m_busDevices[m_logicalAddresses.primary]->TransmitPoll(iAddress);
512 }
513 return false;
514 }
515
516 uint8_t CCECProcessor::VolumeUp(bool bWait /* = true */)
517 {
518 uint8_t status = 0;
519 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM))
520 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeUp(bWait);
521
522 return status;
523 }
524
525 uint8_t CCECProcessor::VolumeDown(bool bWait /* = true */)
526 {
527 uint8_t status = 0;
528 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM))
529 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeDown(bWait);
530
531 return status;
532 }
533
534 uint8_t CCECProcessor::MuteAudio(bool bWait /* = true */)
535 {
536 uint8_t status = 0;
537 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM))
538 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->MuteAudio(bWait);
539
540 return status;
541 }
542
543 CCECBusDevice *CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bRefresh /* = false */) const
544 {
545 if (m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false) == iPhysicalAddress)
546 return m_busDevices[m_logicalAddresses.primary];
547
548 CCECBusDevice *device = NULL;
549 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
550 {
551 if (m_busDevices[iPtr]->GetPhysicalAddress(bRefresh) == iPhysicalAddress)
552 {
553 device = m_busDevices[iPtr];
554 break;
555 }
556 }
557
558 return device;
559 }
560
561 CCECBusDevice *CCECProcessor::GetDeviceByType(cec_device_type type) const
562 {
563 CCECBusDevice *device = NULL;
564
565 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
566 {
567 if (m_busDevices[iPtr]->m_type == type)
568 {
569 device = m_busDevices[iPtr];
570 break;
571 }
572 }
573
574 return device;
575 }
576
577 cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress)
578 {
579 return m_busDevices[iAddress]->GetCecVersion();
580 }
581
582 cec_osd_name CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress)
583 {
584 CStdString strOSDName = m_busDevices[iAddress]->GetOSDName();
585 cec_osd_name retVal;
586
587 snprintf(retVal.name, sizeof(retVal.name), "%s", strOSDName.c_str());
588 retVal.device = iAddress;
589
590 return retVal;
591 }
592
593 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
594 {
595 if (m_busDevices[iAddress])
596 {
597 *language = m_busDevices[iAddress]->GetMenuLanguage();
598 return (strcmp(language->language, "???") != 0);
599 }
600 return false;
601 }
602
603 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress)
604 {
605 if (m_busDevices[iAddress])
606 return m_busDevices[iAddress]->GetVendorId();
607 return false;
608 }
609
610 cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress)
611 {
612 if (m_busDevices[iAddress])
613 return m_busDevices[iAddress]->GetPowerStatus();
614 return CEC_POWER_STATUS_UNKNOWN;
615 }
616
617 bool CCECProcessor::Transmit(const cec_command &data)
618 {
619 bool bReturn(false);
620 LogOutput(data);
621
622 CCECAdapterMessage *output = new CCECAdapterMessage(data);
623 bReturn = Transmit(output);
624 delete output;
625
626 return bReturn;
627 }
628
629 bool CCECProcessor::Transmit(CCECAdapterMessage *output)
630 {
631 bool bReturn(false);
632 CLockObject lock(&m_mutex);
633 {
634 CLockObject msgLock(&output->mutex);
635 if (!m_communication || !m_communication->Write(output))
636 return bReturn;
637 else
638 {
639 output->condition.Wait(&output->mutex);
640 if (output->state != ADAPTER_MESSAGE_STATE_SENT)
641 {
642 m_controller->AddLog(CEC_LOG_ERROR, "command was not sent");
643 return bReturn;
644 }
645 }
646
647 if (output->transmit_timeout > 0)
648 {
649 if ((bReturn = WaitForTransmitSucceeded(output->size(), output->transmit_timeout)) == false)
650 m_controller->AddLog(CEC_LOG_DEBUG, "did not receive ack");
651 }
652 else
653 bReturn = true;
654 }
655
656 return bReturn;
657 }
658
659 void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, cec_abort_reason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
660 {
661 m_controller->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
662
663 cec_command command;
664 // TODO
665 cec_command::Format(command, m_logicalAddresses.primary, address, CEC_OPCODE_FEATURE_ABORT);
666 command.parameters.PushBack((uint8_t)opcode);
667 command.parameters.PushBack((uint8_t)reason);
668
669 Transmit(command);
670 }
671
672 bool CCECProcessor::WaitForTransmitSucceeded(uint8_t iLength, uint32_t iTimeout /* = 1000 */)
673 {
674 bool bError(false);
675 bool bTransmitSucceeded(false);
676 uint8_t iPacketsLeft(iLength / 4);
677
678 int64_t iNow = GetTimeMs();
679 int64_t iTargetTime = iNow + (uint64_t) iTimeout;
680
681 while (!bTransmitSucceeded && !bError && (iTimeout == 0 || iNow < iTargetTime))
682 {
683 CCECAdapterMessage msg;
684
685 if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000))
686 {
687 iNow = GetTimeMs();
688 continue;
689 }
690
691 if ((bError = msg.is_error()) == false)
692 {
693 m_controller->AddLog(bError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
694
695 switch(msg.message())
696 {
697 case MSGCODE_COMMAND_ACCEPTED:
698 if (iPacketsLeft > 0)
699 iPacketsLeft--;
700 break;
701 case MSGCODE_TRANSMIT_SUCCEEDED:
702 bTransmitSucceeded = (iPacketsLeft == 0);
703 bError = !bTransmitSucceeded;
704 break;
705 default:
706 if (ParseMessage(msg))
707 m_commandBuffer.Push(m_currentframe);
708 }
709
710 iNow = GetTimeMs();
711 }
712 }
713
714 return bTransmitSucceeded && !bError;
715 }
716
717 bool CCECProcessor::ParseMessage(const CCECAdapterMessage &msg)
718 {
719 bool bEom = false;
720
721 if (msg.empty())
722 return bEom;
723
724 switch(msg.message())
725 {
726 case MSGCODE_FRAME_START:
727 {
728 m_currentframe.Clear();
729 if (msg.size() >= 2)
730 {
731 m_currentframe.initiator = msg.initiator();
732 m_currentframe.destination = msg.destination();
733 m_currentframe.ack = msg.ack();
734 m_currentframe.eom = msg.eom();
735 }
736 }
737 break;
738 case MSGCODE_FRAME_DATA:
739 {
740 if (msg.size() >= 2)
741 {
742 m_currentframe.PushBack(msg[1]);
743 m_currentframe.eom = msg.eom();
744 }
745 bEom = msg.eom();
746 }
747 break;
748 default:
749 break;
750 }
751
752 return bEom;
753 }
754
755 void CCECProcessor::ParseCommand(cec_command &command)
756 {
757 CStdString dataStr;
758 dataStr.Format(">> %1x%1x:%02x", command.initiator, command.destination, command.opcode);
759 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
760 dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
761 m_controller->AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
762
763 if (!m_bMonitor && command.initiator >= CECDEVICE_TV && command.initiator <= CECDEVICE_BROADCAST)
764 m_busDevices[(uint8_t)command.initiator]->HandleCommand(command);
765 }
766
767 cec_logical_addresses CCECProcessor::GetActiveDevices(void)
768 {
769 cec_logical_addresses addresses;
770 addresses.Clear();
771 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
772 {
773 if (m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
774 addresses.Set((cec_logical_address) iPtr);
775 }
776 return addresses;
777 }
778
779 bool CCECProcessor::IsActiveDevice(cec_logical_address address)
780 {
781 return m_busDevices[address]->GetStatus() == CEC_DEVICE_STATUS_PRESENT;
782 }
783
784 bool CCECProcessor::IsActiveDeviceType(cec_device_type type)
785 {
786 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
787 {
788 if (m_busDevices[iPtr]->GetType() == type && m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
789 return true;
790 }
791
792 return false;
793 }
794
795 uint16_t CCECProcessor::GetPhysicalAddress(void) const
796 {
797 if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary])
798 return m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false);
799 return false;
800 }
801
802 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode)
803 {
804 m_controller->SetCurrentButton(iButtonCode);
805 }
806
807 void CCECProcessor::AddCommand(const cec_command &command)
808 {
809 m_controller->AddCommand(command);
810 }
811
812 void CCECProcessor::AddKey(cec_keypress &key)
813 {
814 m_controller->AddKey(key);
815 }
816
817 void CCECProcessor::AddKey(void)
818 {
819 m_controller->AddKey();
820 }
821
822 void CCECProcessor::AddLog(cec_log_level level, const CStdString &strMessage)
823 {
824 m_controller->AddLog(level, strMessage);
825 }
826
827 bool CCECProcessor::SetAckMask(uint16_t iMask)
828 {
829 bool bReturn(false);
830 CStdString strLog;
831 strLog.Format("setting ackmask to %2x", iMask);
832 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
833
834 CCECAdapterMessage *output = new CCECAdapterMessage;
835
836 output->push_back(MSGSTART);
837 output->push_escaped(MSGCODE_SET_ACK_MASK);
838 output->push_escaped(iMask >> 8);
839 output->push_escaped((uint8_t)iMask);
840 output->push_back(MSGEND);
841
842 if ((bReturn = Transmit(output)) == false)
843 m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask");
844
845 delete output;
846
847 return bReturn;
848 }
849
850 bool CCECProcessor::SendKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = false */)
851 {
852 return m_busDevices[iDestination]->SendKeypress(key, bWait);
853 }
854
855 bool CCECProcessor::SendKeyRelease(cec_logical_address iDestination, bool bWait /* = false */)
856 {
857 return m_busDevices[iDestination]->SendKeyRelease(bWait);
858 }