8f2ea98f24ac4529bca5339717e4a90ad1e3b0cb
[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 uint8_t CCECProcessor::VolumeUp(void)
496 {
497 uint8_t status = 0;
498 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM))
499 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeUp();
500
501 return status;
502 }
503
504 uint8_t CCECProcessor::VolumeDown(void)
505 {
506 uint8_t status = 0;
507 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM))
508 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeDown();
509
510 return status;
511 }
512
513 uint8_t CCECProcessor::MuteAudio(void)
514 {
515 uint8_t status = 0;
516 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM))
517 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->MuteAudio();
518
519 return status;
520 }
521
522 CCECBusDevice *CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bRefresh /* = false */) const
523 {
524 if (m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false) == iPhysicalAddress)
525 return m_busDevices[m_logicalAddresses.primary];
526
527 CCECBusDevice *device = NULL;
528 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
529 {
530 if (m_busDevices[iPtr]->GetPhysicalAddress(bRefresh) == iPhysicalAddress)
531 {
532 device = m_busDevices[iPtr];
533 break;
534 }
535 }
536
537 return device;
538 }
539
540 CCECBusDevice *CCECProcessor::GetDeviceByType(cec_device_type type) const
541 {
542 CCECBusDevice *device = NULL;
543
544 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
545 {
546 if (m_busDevices[iPtr]->m_type == type)
547 {
548 device = m_busDevices[iPtr];
549 break;
550 }
551 }
552
553 return device;
554 }
555
556 cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress)
557 {
558 return m_busDevices[iAddress]->GetCecVersion();
559 }
560
561 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
562 {
563 if (m_busDevices[iAddress])
564 {
565 *language = m_busDevices[iAddress]->GetMenuLanguage();
566 return (strcmp(language->language, "???") != 0);
567 }
568 return false;
569 }
570
571 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress)
572 {
573 if (m_busDevices[iAddress])
574 return m_busDevices[iAddress]->GetVendorId();
575 return false;
576 }
577
578 cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress)
579 {
580 if (m_busDevices[iAddress])
581 return m_busDevices[iAddress]->GetPowerStatus();
582 return CEC_POWER_STATUS_UNKNOWN;
583 }
584
585 bool CCECProcessor::Transmit(const cec_command &data)
586 {
587 bool bReturn(false);
588 LogOutput(data);
589
590 CCECAdapterMessage *output = new CCECAdapterMessage(data);
591 bReturn = Transmit(output);
592 delete output;
593
594 return bReturn;
595 }
596
597 bool CCECProcessor::Transmit(CCECAdapterMessage *output)
598 {
599 bool bReturn(false);
600 CLockObject lock(&m_mutex);
601 {
602 CLockObject msgLock(&output->mutex);
603 if (!m_communication || !m_communication->Write(output))
604 return bReturn;
605 else
606 {
607 output->condition.Wait(&output->mutex);
608 if (output->state != ADAPTER_MESSAGE_STATE_SENT)
609 {
610 m_controller->AddLog(CEC_LOG_ERROR, "command was not sent");
611 return bReturn;
612 }
613 }
614
615 if (output->transmit_timeout > 0)
616 {
617 if ((bReturn = WaitForTransmitSucceeded(output->size(), output->transmit_timeout)) == false)
618 m_controller->AddLog(CEC_LOG_DEBUG, "did not receive ack");
619 }
620 else
621 bReturn = true;
622 }
623
624 return bReturn;
625 }
626
627 void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, cec_abort_reason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
628 {
629 m_controller->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
630
631 cec_command command;
632 // TODO
633 cec_command::Format(command, m_logicalAddresses.primary, address, CEC_OPCODE_FEATURE_ABORT);
634 command.parameters.PushBack((uint8_t)opcode);
635 command.parameters.PushBack((uint8_t)reason);
636
637 Transmit(command);
638 }
639
640 bool CCECProcessor::WaitForTransmitSucceeded(uint8_t iLength, uint32_t iTimeout /* = 1000 */)
641 {
642 bool bError(false);
643 bool bTransmitSucceeded(false);
644 uint8_t iPacketsLeft(iLength / 4);
645
646 int64_t iNow = GetTimeMs();
647 int64_t iTargetTime = iNow + (uint64_t) iTimeout;
648
649 while (!bTransmitSucceeded && !bError && (iTimeout == 0 || iNow < iTargetTime))
650 {
651 CCECAdapterMessage msg;
652
653 if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000))
654 {
655 iNow = GetTimeMs();
656 continue;
657 }
658
659 if ((bError = msg.is_error()) == false)
660 {
661 m_controller->AddLog(bError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
662
663 switch(msg.message())
664 {
665 case MSGCODE_COMMAND_ACCEPTED:
666 if (iPacketsLeft > 0)
667 iPacketsLeft--;
668 break;
669 case MSGCODE_TRANSMIT_SUCCEEDED:
670 bTransmitSucceeded = (iPacketsLeft == 0);
671 bError = !bTransmitSucceeded;
672 break;
673 default:
674 if (ParseMessage(msg))
675 m_commandBuffer.Push(m_currentframe);
676 }
677
678 iNow = GetTimeMs();
679 }
680 }
681
682 return bTransmitSucceeded && !bError;
683 }
684
685 bool CCECProcessor::ParseMessage(const CCECAdapterMessage &msg)
686 {
687 bool bEom = false;
688
689 if (msg.empty())
690 return bEom;
691
692 switch(msg.message())
693 {
694 case MSGCODE_FRAME_START:
695 {
696 m_currentframe.Clear();
697 if (msg.size() >= 2)
698 {
699 m_currentframe.initiator = msg.initiator();
700 m_currentframe.destination = msg.destination();
701 m_currentframe.ack = msg.ack();
702 m_currentframe.eom = msg.eom();
703 }
704 }
705 break;
706 case MSGCODE_FRAME_DATA:
707 {
708 if (msg.size() >= 2)
709 {
710 m_currentframe.PushBack(msg[1]);
711 m_currentframe.eom = msg.eom();
712 }
713 bEom = msg.eom();
714 }
715 break;
716 default:
717 break;
718 }
719
720 return bEom;
721 }
722
723 void CCECProcessor::ParseCommand(cec_command &command)
724 {
725 CStdString dataStr;
726 dataStr.Format(">> %1x%1x:%02x", command.initiator, command.destination, command.opcode);
727 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
728 dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
729 m_controller->AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
730
731 if (!m_bMonitor && command.initiator >= CECDEVICE_TV && command.initiator <= CECDEVICE_BROADCAST)
732 m_busDevices[(uint8_t)command.initiator]->HandleCommand(command);
733 }
734
735 cec_logical_addresses CCECProcessor::GetActiveDevices(void)
736 {
737 cec_logical_addresses addresses;
738 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
739 {
740 if (m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
741 addresses.Set((cec_logical_address) iPtr);
742 }
743 return addresses;
744 }
745
746 bool CCECProcessor::IsActiveDevice(cec_logical_address address)
747 {
748 return m_busDevices[address]->GetStatus() == CEC_DEVICE_STATUS_PRESENT;
749 }
750
751 bool CCECProcessor::IsActiveDeviceType(cec_device_type type)
752 {
753 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
754 {
755 if (m_busDevices[iPtr]->GetType() == type && m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
756 return true;
757 }
758
759 return false;
760 }
761
762 uint16_t CCECProcessor::GetPhysicalAddress(void) const
763 {
764 if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary])
765 return m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false);
766 return false;
767 }
768
769 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode)
770 {
771 m_controller->SetCurrentButton(iButtonCode);
772 }
773
774 void CCECProcessor::AddCommand(const cec_command &command)
775 {
776 m_controller->AddCommand(command);
777 }
778
779 void CCECProcessor::AddKey(cec_keypress &key)
780 {
781 m_controller->AddKey(key);
782 }
783
784 void CCECProcessor::AddKey(void)
785 {
786 m_controller->AddKey();
787 }
788
789 void CCECProcessor::AddLog(cec_log_level level, const CStdString &strMessage)
790 {
791 m_controller->AddLog(level, strMessage);
792 }
793
794 bool CCECProcessor::SetAckMask(uint16_t iMask)
795 {
796 bool bReturn(false);
797 CStdString strLog;
798 strLog.Format("setting ackmask to %2x", iMask);
799 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
800
801 CCECAdapterMessage *output = new CCECAdapterMessage;
802
803 output->push_back(MSGSTART);
804 output->push_escaped(MSGCODE_SET_ACK_MASK);
805 output->push_escaped(iMask >> 8);
806 output->push_escaped((uint8_t)iMask);
807 output->push_back(MSGEND);
808
809 if ((bReturn = Transmit(output)) == false)
810 m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask");
811
812 delete output;
813
814 return bReturn;
815 }