8315cd297b1f66ba6d204b924351f939b7bdd6df
[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 {
235 CLockObject lock(&m_mutex);
236 m_bStarted = true;
237 lock.Leave();
238
239 SetHDMIPort(m_iHDMIPort);
240
241 lock.Lock();
242 m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
243 m_startCondition.Signal();
244 }
245 }
246
247 while (!IsStopped())
248 {
249 command.Clear();
250 msg.clear();
251
252 {
253 CLockObject lock(&m_mutex);
254 if (m_commandBuffer.Pop(command))
255 {
256 bParseFrame = true;
257 }
258 else if (m_communication->IsOpen() && m_communication->Read(msg, 50))
259 {
260 m_controller->AddLog(msg.is_error() ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
261 if ((bParseFrame = (ParseMessage(msg) && !IsStopped())) == true)
262 command = m_currentframe;
263 }
264 }
265
266 if (bParseFrame)
267 ParseCommand(command);
268 bParseFrame = false;
269
270 Sleep(5);
271
272 m_controller->CheckKeypressTimeout();
273
274 for (unsigned int iDevicePtr = 0; iDevicePtr < 16; iDevicePtr++)
275 {
276 if (!m_logicalAddresses[iDevicePtr])
277 m_busDevices[iDevicePtr]->PollVendorId();
278 }
279
280 Sleep(5);
281 }
282
283 return NULL;
284 }
285
286 bool CCECProcessor::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RESERVED */)
287 {
288 bool bReturn(false);
289
290 if (!IsRunning())
291 return bReturn;
292
293 cec_logical_address addr = m_logicalAddresses.primary;
294
295 if (type != CEC_DEVICE_TYPE_RESERVED)
296 {
297 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
298 {
299 if (m_logicalAddresses[iPtr] && m_busDevices[iPtr]->m_type == type)
300 {
301 addr = (cec_logical_address) iPtr;
302 break;
303 }
304 }
305 }
306
307 return SetStreamPath(m_busDevices[addr]->GetPhysicalAddress(false)) &&
308 m_busDevices[addr]->TransmitActiveSource();
309 }
310
311 bool CCECProcessor::SetActiveSource(cec_logical_address iAddress)
312 {
313 return SetStreamPath(m_busDevices[iAddress]->GetPhysicalAddress(false));
314 }
315
316 bool CCECProcessor::SetActiveView(void)
317 {
318 return SetActiveSource(m_types.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED : m_types[0]);
319 }
320
321 bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode, bool bSendUpdate /* = true */)
322 {
323 bool bReturn(false);
324
325 CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
326 if (device)
327 {
328 ((CCECPlaybackDevice *) device)->SetDeckControlMode(mode);
329 if (bSendUpdate)
330 ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV);
331 bReturn = true;
332 }
333
334 return bReturn;
335 }
336
337 bool CCECProcessor::SetDeckInfo(cec_deck_info info, bool bSendUpdate /* = true */)
338 {
339 bool bReturn(false);
340
341 CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
342 if (device)
343 {
344 ((CCECPlaybackDevice *) device)->SetDeckStatus(info);
345 if (bSendUpdate)
346 ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV);
347 bReturn = true;
348 }
349
350 return bReturn;
351 }
352
353 bool CCECProcessor::SetHDMIPort(uint8_t iPort)
354 {
355 bool bReturn(false);
356
357 CStdString strLog;
358 strLog.Format("setting HDMI port to %d", iPort);
359 AddLog(CEC_LOG_DEBUG, strLog);
360
361 m_iHDMIPort = iPort;
362 if (!m_bStarted)
363 return true;
364
365 uint16_t iPhysicalAddress(0);
366 int iPos = 3;
367 while(!bReturn && iPos >= 0)
368 {
369 iPhysicalAddress += ((uint16_t)iPort * (0x1 << iPos*4));
370 strLog.Format("checking physical address %4x", iPhysicalAddress);
371 AddLog(CEC_LOG_DEBUG, strLog);
372 if (CheckPhysicalAddress(iPhysicalAddress))
373 {
374 strLog.Format("physical address %4x is in use", iPhysicalAddress);
375 AddLog(CEC_LOG_DEBUG, strLog);
376 iPos--;
377 }
378 else
379 {
380 SetPhysicalAddress(iPhysicalAddress);
381 bReturn = true;
382 }
383 }
384
385 return bReturn;
386 }
387
388 bool CCECProcessor::CheckPhysicalAddress(uint16_t iPhysicalAddress)
389 {
390 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
391 {
392 if (m_busDevices[iPtr]->GetPhysicalAddress(false) == iPhysicalAddress)
393 return true;
394 }
395 return false;
396 }
397
398 bool CCECProcessor::SetStreamPath(uint16_t iStreamPath)
399 {
400 bool bReturn(false);
401
402 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamPath);
403 if (device)
404 {
405 device->SetActiveDevice();
406 bReturn = true;
407 }
408
409 return bReturn;
410 }
411
412 bool CCECProcessor::SetInactiveView(void)
413 {
414 if (!IsRunning())
415 return false;
416
417 if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary])
418 return m_busDevices[m_logicalAddresses.primary]->TransmitInactiveView();
419 return false;
420 }
421
422 void CCECProcessor::LogOutput(const cec_command &data)
423 {
424 CStdString strTx;
425 strTx.Format("<< %02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination);
426 if (data.opcode_set)
427 strTx.AppendFormat(":%02x", (uint8_t)data.opcode);
428
429 for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++)
430 strTx.AppendFormat(":%02x", data.parameters[iPtr]);
431 m_controller->AddLog(CEC_LOG_TRAFFIC, strTx.c_str());
432 }
433
434 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress)
435 {
436 if (m_logicalAddresses.primary != iLogicalAddress)
437 {
438 CStdString strLog;
439 strLog.Format("<< setting primary logical address to %1x", iLogicalAddress);
440 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
441 m_logicalAddresses.primary = iLogicalAddress;
442 m_logicalAddresses.Set(iLogicalAddress);
443 return SetAckMask(m_logicalAddresses.AckMask());
444 }
445
446 return true;
447 }
448
449 bool CCECProcessor::SetMenuState(cec_menu_state state, bool bSendUpdate /* = true */)
450 {
451 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
452 {
453 if (m_logicalAddresses[iPtr])
454 m_busDevices[iPtr]->SetMenuState(state);
455 }
456
457 if (bSendUpdate)
458 m_busDevices[m_logicalAddresses.primary]->TransmitMenuState(CECDEVICE_TV);
459
460 return true;
461 }
462
463 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress)
464 {
465 if (!m_logicalAddresses.IsEmpty())
466 {
467 for (uint8_t iPtr = 0; iPtr < 15; iPtr++)
468 if (m_logicalAddresses[iPtr])
469 m_busDevices[iPtr]->SetPhysicalAddress(iPhysicalAddress);
470 return SetActiveView();
471 }
472 return false;
473 }
474
475 bool CCECProcessor::SwitchMonitoring(bool bEnable)
476 {
477 CStdString strLog;
478 strLog.Format("== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
479 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
480
481 m_bMonitor = bEnable;
482 if (bEnable)
483 return SetAckMask(0);
484 else
485 return SetAckMask(m_logicalAddresses.AckMask());
486 }
487
488 bool CCECProcessor::PollDevice(cec_logical_address iAddress)
489 {
490 if (iAddress != CECDEVICE_UNKNOWN && m_busDevices[iAddress])
491 return m_busDevices[m_logicalAddresses.primary]->TransmitPoll(iAddress);
492 return false;
493 }
494
495 CCECBusDevice *CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bRefresh /* = false */) const
496 {
497 if (m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false) == iPhysicalAddress)
498 return m_busDevices[m_logicalAddresses.primary];
499
500 CCECBusDevice *device = NULL;
501 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
502 {
503 if (m_busDevices[iPtr]->GetPhysicalAddress(bRefresh) == iPhysicalAddress)
504 {
505 device = m_busDevices[iPtr];
506 break;
507 }
508 }
509
510 return device;
511 }
512
513 CCECBusDevice *CCECProcessor::GetDeviceByType(cec_device_type type) const
514 {
515 CCECBusDevice *device = NULL;
516
517 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
518 {
519 if (m_busDevices[iPtr]->m_type == type)
520 {
521 device = m_busDevices[iPtr];
522 break;
523 }
524 }
525
526 return device;
527 }
528
529 cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress)
530 {
531 return m_busDevices[iAddress]->GetCecVersion();
532 }
533
534 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
535 {
536 if (m_busDevices[iAddress])
537 {
538 *language = m_busDevices[iAddress]->GetMenuLanguage();
539 return (strcmp(language->language, "???") != 0);
540 }
541 return false;
542 }
543
544 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress)
545 {
546 if (m_busDevices[iAddress])
547 return m_busDevices[iAddress]->GetVendorId();
548 return false;
549 }
550
551 cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress)
552 {
553 if (m_busDevices[iAddress])
554 return m_busDevices[iAddress]->GetPowerStatus();
555 return CEC_POWER_STATUS_UNKNOWN;
556 }
557
558 bool CCECProcessor::Transmit(const cec_command &data)
559 {
560 bool bReturn(false);
561 LogOutput(data);
562
563 CCECAdapterMessage *output = new CCECAdapterMessage(data);
564 bReturn = Transmit(output);
565 delete output;
566
567 return bReturn;
568 }
569
570 bool CCECProcessor::Transmit(CCECAdapterMessage *output)
571 {
572 bool bReturn(false);
573 CLockObject lock(&m_mutex);
574 {
575 CLockObject msgLock(&output->mutex);
576 if (!m_communication || !m_communication->Write(output))
577 return bReturn;
578 else
579 {
580 output->condition.Wait(&output->mutex);
581 if (output->state != ADAPTER_MESSAGE_STATE_SENT)
582 {
583 m_controller->AddLog(CEC_LOG_ERROR, "command was not sent");
584 return bReturn;
585 }
586 }
587
588 if (output->transmit_timeout > 0)
589 {
590 if ((bReturn = WaitForTransmitSucceeded(output->size(), output->transmit_timeout)) == false)
591 m_controller->AddLog(CEC_LOG_DEBUG, "did not receive ack");
592 }
593 else
594 bReturn = true;
595 }
596
597 return bReturn;
598 }
599
600 void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, cec_abort_reason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
601 {
602 m_controller->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
603
604 cec_command command;
605 // TODO
606 cec_command::Format(command, m_logicalAddresses.primary, address, CEC_OPCODE_FEATURE_ABORT);
607 command.parameters.PushBack((uint8_t)opcode);
608 command.parameters.PushBack((uint8_t)reason);
609
610 Transmit(command);
611 }
612
613 bool CCECProcessor::WaitForTransmitSucceeded(uint8_t iLength, uint32_t iTimeout /* = 1000 */)
614 {
615 bool bError(false);
616 bool bTransmitSucceeded(false);
617 uint8_t iPacketsLeft(iLength / 4);
618
619 int64_t iNow = GetTimeMs();
620 int64_t iTargetTime = iNow + (uint64_t) iTimeout;
621
622 while (!bTransmitSucceeded && !bError && (iTimeout == 0 || iNow < iTargetTime))
623 {
624 CCECAdapterMessage msg;
625
626 if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000))
627 {
628 iNow = GetTimeMs();
629 continue;
630 }
631
632 if ((bError = msg.is_error()) == false)
633 {
634 m_controller->AddLog(bError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
635
636 switch(msg.message())
637 {
638 case MSGCODE_COMMAND_ACCEPTED:
639 if (iPacketsLeft > 0)
640 iPacketsLeft--;
641 break;
642 case MSGCODE_TRANSMIT_SUCCEEDED:
643 bTransmitSucceeded = (iPacketsLeft == 0);
644 bError = !bTransmitSucceeded;
645 break;
646 default:
647 if (ParseMessage(msg))
648 m_commandBuffer.Push(m_currentframe);
649 }
650
651 iNow = GetTimeMs();
652 }
653 }
654
655 return bTransmitSucceeded && !bError;
656 }
657
658 bool CCECProcessor::ParseMessage(const CCECAdapterMessage &msg)
659 {
660 bool bEom = false;
661
662 if (msg.empty())
663 return bEom;
664
665 switch(msg.message())
666 {
667 case MSGCODE_FRAME_START:
668 {
669 m_currentframe.Clear();
670 if (msg.size() >= 2)
671 {
672 m_currentframe.initiator = msg.initiator();
673 m_currentframe.destination = msg.destination();
674 m_currentframe.ack = msg.ack();
675 m_currentframe.eom = msg.eom();
676 }
677 }
678 break;
679 case MSGCODE_FRAME_DATA:
680 {
681 if (msg.size() >= 2)
682 {
683 m_currentframe.PushBack(msg[1]);
684 m_currentframe.eom = msg.eom();
685 }
686 bEom = msg.eom();
687 }
688 break;
689 default:
690 break;
691 }
692
693 return bEom;
694 }
695
696 void CCECProcessor::ParseCommand(cec_command &command)
697 {
698 CStdString dataStr;
699 dataStr.Format(">> %1x%1x:%02x", command.initiator, command.destination, command.opcode);
700 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
701 dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
702 m_controller->AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
703
704 if (!m_bMonitor && command.initiator >= CECDEVICE_TV && command.initiator <= CECDEVICE_BROADCAST)
705 m_busDevices[(uint8_t)command.initiator]->HandleCommand(command);
706 }
707
708 cec_logical_addresses CCECProcessor::GetActiveDevices(void)
709 {
710 cec_logical_addresses addresses;
711 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
712 {
713 if (m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
714 addresses.Set((cec_logical_address) iPtr);
715 }
716 return addresses;
717 }
718
719 bool CCECProcessor::IsActiveDevice(cec_logical_address address)
720 {
721 return m_busDevices[address]->GetStatus() == CEC_DEVICE_STATUS_PRESENT;
722 }
723
724 bool CCECProcessor::IsActiveDeviceType(cec_device_type type)
725 {
726 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
727 {
728 if (m_busDevices[iPtr]->GetType() == type && m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
729 return true;
730 }
731
732 return false;
733 }
734
735 uint16_t CCECProcessor::GetPhysicalAddress(void) const
736 {
737 if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary])
738 return m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false);
739 return false;
740 }
741
742 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode)
743 {
744 m_controller->SetCurrentButton(iButtonCode);
745 }
746
747 void CCECProcessor::AddCommand(const cec_command &command)
748 {
749 m_controller->AddCommand(command);
750 }
751
752 void CCECProcessor::AddKey(cec_keypress &key)
753 {
754 m_controller->AddKey(key);
755 }
756
757 void CCECProcessor::AddKey(void)
758 {
759 m_controller->AddKey();
760 }
761
762 void CCECProcessor::AddLog(cec_log_level level, const CStdString &strMessage)
763 {
764 m_controller->AddLog(level, strMessage);
765 }
766
767 bool CCECProcessor::SetAckMask(uint16_t iMask)
768 {
769 bool bReturn(false);
770 CStdString strLog;
771 strLog.Format("setting ackmask to %2x", iMask);
772 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
773
774 CCECAdapterMessage *output = new CCECAdapterMessage;
775
776 output->push_back(MSGSTART);
777 output->push_escaped(MSGCODE_SET_ACK_MASK);
778 output->push_escaped(iMask >> 8);
779 output->push_escaped((uint8_t)iMask);
780 output->push_back(MSGEND);
781
782 if ((bReturn = Transmit(output)) == false)
783 m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask");
784
785 delete output;
786
787 return bReturn;
788 }