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