cec: removed the 10ms extra delay in CAdapterCommunication::SendMessageToAdapter()
[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_iBaseDevice((cec_logical_address)CEC_DEFAULT_BASE_DEVICE),
54 m_strDeviceName(strDeviceName),
55 m_communication(serComm),
56 m_controller(controller),
57 m_bMonitor(false),
58 m_busScan(NULL)
59 {
60 m_logicalAddresses.Clear();
61 m_logicalAddresses.Set(iLogicalAddress);
62 m_types.clear();
63 for (int iPtr = 0; iPtr <= 16; iPtr++)
64 m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, iPtr == iLogicalAddress ? iPhysicalAddress : 0);
65 }
66
67 CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm, const char *strDeviceName, const cec_device_type_list &types) :
68 m_bStarted(false),
69 m_iHDMIPort(CEC_DEFAULT_HDMI_PORT),
70 m_iBaseDevice((cec_logical_address)CEC_DEFAULT_BASE_DEVICE),
71 m_strDeviceName(strDeviceName),
72 m_types(types),
73 m_communication(serComm),
74 m_controller(controller),
75 m_bMonitor(false)
76 {
77 m_logicalAddresses.Clear();
78 for (int iPtr = 0; iPtr < 16; iPtr++)
79 {
80 switch(iPtr)
81 {
82 case CECDEVICE_AUDIOSYSTEM:
83 m_busDevices[iPtr] = new CCECAudioSystem(this, (cec_logical_address) iPtr, 0xFFFF);
84 break;
85 case CECDEVICE_PLAYBACKDEVICE1:
86 case CECDEVICE_PLAYBACKDEVICE2:
87 case CECDEVICE_PLAYBACKDEVICE3:
88 m_busDevices[iPtr] = new CCECPlaybackDevice(this, (cec_logical_address) iPtr, 0xFFFF);
89 break;
90 case CECDEVICE_RECORDINGDEVICE1:
91 case CECDEVICE_RECORDINGDEVICE2:
92 case CECDEVICE_RECORDINGDEVICE3:
93 m_busDevices[iPtr] = new CCECRecordingDevice(this, (cec_logical_address) iPtr, 0xFFFF);
94 break;
95 case CECDEVICE_TUNER1:
96 case CECDEVICE_TUNER2:
97 case CECDEVICE_TUNER3:
98 case CECDEVICE_TUNER4:
99 m_busDevices[iPtr] = new CCECTuner(this, (cec_logical_address) iPtr, 0xFFFF);
100 break;
101 case CECDEVICE_TV:
102 m_busDevices[iPtr] = new CCECTV(this, (cec_logical_address) iPtr, 0);
103 break;
104 default:
105 m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, 0xFFFF);
106 break;
107 }
108 }
109 }
110
111 CCECProcessor::~CCECProcessor(void)
112 {
113 if (m_busScan)
114 {
115 m_busScan->StopThread();
116 delete m_busScan;
117 }
118
119 m_startCondition.Broadcast();
120 StopThread();
121
122 m_communication = NULL;
123 m_controller = NULL;
124 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
125 delete m_busDevices[iPtr];
126 }
127
128 bool CCECProcessor::Start(void)
129 {
130 CLockObject lock(&m_mutex);
131 if (!m_communication || !m_communication->IsOpen())
132 {
133 m_controller->AddLog(CEC_LOG_ERROR, "connection is closed");
134 return false;
135 }
136
137 if (CreateThread())
138 {
139 if (!m_startCondition.Wait(&m_mutex) || !m_bStarted)
140 {
141 m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
142 return false;
143 }
144
145 lock.Leave();
146 if (SetAckMask(m_logicalAddresses.AckMask()) &&
147 SetHDMIPort(m_iBaseDevice, m_iHDMIPort, true))
148 {
149 m_busScan = new CCECBusScan(this);
150 m_busScan->CreateThread(true);
151 return true;
152 }
153 }
154 else
155 m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
156
157 return false;
158 }
159
160 bool CCECProcessor::TryLogicalAddress(cec_logical_address address)
161 {
162 if (m_busDevices[address]->TryLogicalAddress())
163 {
164 /* only set our OSD name and active source for the primary device */
165 if (m_logicalAddresses.IsEmpty())
166 {
167 m_busDevices[address]->m_strDeviceName = m_strDeviceName;
168 m_busDevices[address]->m_bActiveSource = true;
169 }
170 m_logicalAddresses.Set(address);
171 return true;
172 }
173
174 return false;
175 }
176
177 bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
178 {
179 AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'recording device'");
180 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1) ||
181 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2) ||
182 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3);
183 }
184
185 bool CCECProcessor::FindLogicalAddressTuner(void)
186 {
187 AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'tuner'");
188 return TryLogicalAddress(CECDEVICE_TUNER1) ||
189 TryLogicalAddress(CECDEVICE_TUNER2) ||
190 TryLogicalAddress(CECDEVICE_TUNER3) ||
191 TryLogicalAddress(CECDEVICE_TUNER4);
192 }
193
194 bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
195 {
196 AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'playback device'");
197 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1) ||
198 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2) ||
199 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3);
200 }
201
202 bool CCECProcessor::FindLogicalAddressAudioSystem(void)
203 {
204 AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'audio'");
205 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM);
206 }
207
208 bool CCECProcessor::FindLogicalAddresses(void)
209 {
210 bool bReturn(true);
211 m_logicalAddresses.Clear();
212 CStdString strLog;
213
214 for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
215 {
216 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RESERVED)
217 continue;
218
219 strLog.Format("%s - device %d: type %d", __FUNCTION__, iPtr, m_types.types[iPtr]);
220 AddLog(CEC_LOG_DEBUG, strLog);
221
222 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RECORDING_DEVICE)
223 bReturn &= FindLogicalAddressRecordingDevice();
224 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_TUNER)
225 bReturn &= FindLogicalAddressTuner();
226 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE)
227 bReturn &= FindLogicalAddressPlaybackDevice();
228 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
229 bReturn &= FindLogicalAddressAudioSystem();
230 }
231
232 return bReturn;
233 }
234
235 void *CCECProcessor::Process(void)
236 {
237 bool bParseFrame(false);
238 cec_command command;
239 CCECAdapterMessage msg;
240
241 if (m_logicalAddresses.IsEmpty() && !FindLogicalAddresses())
242 {
243 CLockObject lock(&m_mutex);
244 m_controller->AddLog(CEC_LOG_ERROR, "could not detect our logical addresses");
245 m_startCondition.Signal();
246 return NULL;
247 }
248 else
249 {
250 CLockObject lock(&m_mutex);
251 m_bStarted = true;
252 m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
253 m_startCondition.Signal();
254 }
255
256 while (!IsStopped())
257 {
258 command.Clear();
259 msg.clear();
260
261 {
262 CLockObject lock(&m_mutex);
263 if (m_commandBuffer.Pop(command))
264 {
265 bParseFrame = true;
266 }
267 else if (m_communication->IsOpen() && m_communication->Read(msg, 50))
268 {
269 m_controller->AddLog(msg.is_error() ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
270 if ((bParseFrame = (ParseMessage(msg) && !IsStopped())) == true)
271 command = m_currentframe;
272 }
273 }
274
275 if (bParseFrame)
276 ParseCommand(command);
277 bParseFrame = false;
278
279 Sleep(5);
280
281 m_controller->CheckKeypressTimeout();
282 }
283
284 return NULL;
285 }
286
287 bool CCECProcessor::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RESERVED */)
288 {
289 bool bReturn(false);
290
291 if (!IsRunning())
292 return bReturn;
293
294 cec_logical_address addr = m_logicalAddresses.primary;
295
296 if (type != CEC_DEVICE_TYPE_RESERVED)
297 {
298 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
299 {
300 if (m_logicalAddresses[iPtr] && m_busDevices[iPtr]->m_type == type)
301 {
302 addr = (cec_logical_address) iPtr;
303 break;
304 }
305 }
306 }
307
308 return SetStreamPath(m_busDevices[addr]->GetPhysicalAddress(false)) &&
309 m_busDevices[addr]->TransmitActiveSource();
310 }
311
312 bool CCECProcessor::SetActiveSource(cec_logical_address iAddress)
313 {
314 return SetStreamPath(m_busDevices[iAddress]->GetPhysicalAddress(false));
315 }
316
317 bool CCECProcessor::SetActiveView(void)
318 {
319 return SetActiveSource(m_types.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED : m_types[0]);
320 }
321
322 bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode, bool bSendUpdate /* = true */)
323 {
324 bool bReturn(false);
325
326 CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
327 if (device)
328 {
329 ((CCECPlaybackDevice *) device)->SetDeckControlMode(mode);
330 if (bSendUpdate)
331 ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV);
332 bReturn = true;
333 }
334
335 return bReturn;
336 }
337
338 bool CCECProcessor::SetDeckInfo(cec_deck_info info, bool bSendUpdate /* = true */)
339 {
340 bool bReturn(false);
341
342 CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
343 if (device)
344 {
345 ((CCECPlaybackDevice *) device)->SetDeckStatus(info);
346 if (bSendUpdate)
347 ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV);
348 bReturn = true;
349 }
350
351 return bReturn;
352 }
353
354 bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice, uint8_t iPort, bool bForce /* = false */)
355 {
356 bool bReturn(false);
357
358 CStdString strLog;
359 strLog.Format("setting HDMI port to %d on device %s (%d)", iPort, ToString(iBaseDevice), (int)iBaseDevice);
360 AddLog(CEC_LOG_DEBUG, strLog);
361
362 m_iBaseDevice = iBaseDevice;
363 m_iHDMIPort = iPort;
364 if (!m_bStarted && !bForce)
365 return true;
366
367 uint16_t iPhysicalAddress(0);
368 iPhysicalAddress = m_busDevices[iBaseDevice]->GetPhysicalAddress();
369 uint16_t iPos = 0;
370 if (iPhysicalAddress == 0)
371 iPos = 0x1000;
372 else if (iPhysicalAddress % 0x1000 == 0)
373 iPos = 0x100;
374 else if (iPhysicalAddress % 0x100 == 0)
375 iPos = 0x10;
376 else if (iPhysicalAddress % 0x10 == 0)
377 iPos = 0x1;
378
379 while(!bReturn && iPos > 0)
380 {
381 iPhysicalAddress += (uint16_t)(iPort * iPos);
382 strLog.Format("checking physical address %4x", iPhysicalAddress);
383 AddLog(CEC_LOG_DEBUG, strLog);
384 if (CheckPhysicalAddress(iPhysicalAddress))
385 {
386 strLog.Format("physical address %4x is in use", iPhysicalAddress);
387 AddLog(CEC_LOG_DEBUG, strLog);
388 iPos = (iPos == 1) ? 0 : iPos / 0x10;
389 }
390 else
391 {
392 SetPhysicalAddress(iPhysicalAddress);
393 bReturn = true;
394 }
395 }
396
397 return bReturn;
398 }
399
400 bool CCECProcessor::CheckPhysicalAddress(uint16_t iPhysicalAddress)
401 {
402 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
403 {
404 if (m_busDevices[iPtr]->GetPhysicalAddress(false) == iPhysicalAddress)
405 return true;
406 }
407 return false;
408 }
409
410 bool CCECProcessor::SetStreamPath(uint16_t iStreamPath)
411 {
412 bool bReturn(false);
413
414 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamPath);
415 if (device)
416 {
417 device->SetActiveDevice();
418 bReturn = true;
419 }
420
421 return bReturn;
422 }
423
424 bool CCECProcessor::TransmitInactiveSource(void)
425 {
426 if (!IsRunning())
427 return false;
428
429 if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary])
430 return m_busDevices[m_logicalAddresses.primary]->TransmitInactiveSource();
431 return false;
432 }
433
434 void CCECProcessor::LogOutput(const cec_command &data)
435 {
436 CStdString strTx;
437 strTx.Format("<< %02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination);
438 if (data.opcode_set)
439 strTx.AppendFormat(":%02x", (uint8_t)data.opcode);
440
441 for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++)
442 strTx.AppendFormat(":%02x", data.parameters[iPtr]);
443 m_controller->AddLog(CEC_LOG_TRAFFIC, strTx.c_str());
444 }
445
446 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress)
447 {
448 if (m_logicalAddresses.primary != iLogicalAddress)
449 {
450 CStdString strLog;
451 strLog.Format("<< setting primary logical address to %1x", iLogicalAddress);
452 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
453 m_logicalAddresses.primary = iLogicalAddress;
454 m_logicalAddresses.Set(iLogicalAddress);
455 return SetAckMask(m_logicalAddresses.AckMask());
456 }
457
458 return true;
459 }
460
461 bool CCECProcessor::SetMenuState(cec_menu_state state, bool bSendUpdate /* = true */)
462 {
463 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
464 {
465 if (m_logicalAddresses[iPtr])
466 m_busDevices[iPtr]->SetMenuState(state);
467 }
468
469 if (bSendUpdate)
470 m_busDevices[m_logicalAddresses.primary]->TransmitMenuState(CECDEVICE_TV);
471
472 return true;
473 }
474
475 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress)
476 {
477 if (!m_logicalAddresses.IsEmpty())
478 {
479 for (uint8_t iPtr = 0; iPtr < 15; iPtr++)
480 if (m_logicalAddresses[iPtr])
481 m_busDevices[iPtr]->SetPhysicalAddress(iPhysicalAddress);
482 return SetActiveView();
483 }
484 return false;
485 }
486
487 bool CCECProcessor::SwitchMonitoring(bool bEnable)
488 {
489 CStdString strLog;
490 strLog.Format("== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
491 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
492
493 m_bMonitor = bEnable;
494 if (bEnable)
495 return SetAckMask(0);
496 else
497 return SetAckMask(m_logicalAddresses.AckMask());
498 }
499
500 bool CCECProcessor::PollDevice(cec_logical_address iAddress)
501 {
502 if (iAddress != CECDEVICE_UNKNOWN && m_busDevices[iAddress])
503 {
504 return m_logicalAddresses.primary == CECDEVICE_UNKNOWN ?
505 m_busDevices[iAddress]->TransmitPoll(iAddress) :
506 m_busDevices[m_logicalAddresses.primary]->TransmitPoll(iAddress);
507 }
508 return false;
509 }
510
511 uint8_t CCECProcessor::VolumeUp(bool bWait /* = true */)
512 {
513 uint8_t status = 0;
514 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM))
515 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeUp(bWait);
516
517 return status;
518 }
519
520 uint8_t CCECProcessor::VolumeDown(bool bWait /* = true */)
521 {
522 uint8_t status = 0;
523 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM))
524 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeDown(bWait);
525
526 return status;
527 }
528
529 uint8_t CCECProcessor::MuteAudio(bool bWait /* = true */)
530 {
531 uint8_t status = 0;
532 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM))
533 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->MuteAudio(bWait);
534
535 return status;
536 }
537
538 CCECBusDevice *CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bRefresh /* = false */) const
539 {
540 if (m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false) == iPhysicalAddress)
541 return m_busDevices[m_logicalAddresses.primary];
542
543 CCECBusDevice *device = NULL;
544 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
545 {
546 if (m_busDevices[iPtr]->GetPhysicalAddress(bRefresh) == iPhysicalAddress)
547 {
548 device = m_busDevices[iPtr];
549 break;
550 }
551 }
552
553 return device;
554 }
555
556 CCECBusDevice *CCECProcessor::GetDeviceByType(cec_device_type type) const
557 {
558 CCECBusDevice *device = NULL;
559
560 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
561 {
562 if (m_busDevices[iPtr]->m_type == type)
563 {
564 device = m_busDevices[iPtr];
565 break;
566 }
567 }
568
569 return device;
570 }
571
572 cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress)
573 {
574 return m_busDevices[iAddress]->GetCecVersion();
575 }
576
577 cec_osd_name CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress)
578 {
579 CStdString strOSDName = m_busDevices[iAddress]->GetOSDName();
580 cec_osd_name retVal;
581
582 snprintf(retVal.name, sizeof(retVal.name), "%s", strOSDName.c_str());
583 retVal.device = iAddress;
584
585 return retVal;
586 }
587
588 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
589 {
590 if (m_busDevices[iAddress])
591 {
592 *language = m_busDevices[iAddress]->GetMenuLanguage();
593 return (strcmp(language->language, "???") != 0);
594 }
595 return false;
596 }
597
598 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress)
599 {
600 if (m_busDevices[iAddress])
601 return m_busDevices[iAddress]->GetVendorId();
602 return false;
603 }
604
605 cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress)
606 {
607 if (m_busDevices[iAddress])
608 return m_busDevices[iAddress]->GetPowerStatus();
609 return CEC_POWER_STATUS_UNKNOWN;
610 }
611
612 bool CCECProcessor::Transmit(const cec_command &data)
613 {
614 bool bReturn(false);
615 LogOutput(data);
616
617 CCECAdapterMessage *output = new CCECAdapterMessage(data);
618 bReturn = Transmit(output);
619 delete output;
620
621 return bReturn;
622 }
623
624 bool CCECProcessor::Transmit(CCECAdapterMessage *output)
625 {
626 bool bReturn(false);
627 CLockObject lock(&m_mutex);
628 {
629 CLockObject msgLock(&output->mutex);
630 if (!m_communication || !m_communication->Write(output))
631 return bReturn;
632 else
633 {
634 output->condition.Wait(&output->mutex);
635 if (output->state != ADAPTER_MESSAGE_STATE_SENT)
636 {
637 m_controller->AddLog(CEC_LOG_ERROR, "command was not sent");
638 return bReturn;
639 }
640 }
641
642 if (output->transmit_timeout > 0)
643 {
644 if ((bReturn = WaitForTransmitSucceeded(output->size(), output->transmit_timeout)) == false)
645 m_controller->AddLog(CEC_LOG_DEBUG, "did not receive ack");
646 }
647 else
648 bReturn = true;
649 }
650
651 return bReturn;
652 }
653
654 void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, cec_abort_reason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
655 {
656 m_controller->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
657
658 cec_command command;
659 // TODO
660 cec_command::Format(command, m_logicalAddresses.primary, address, CEC_OPCODE_FEATURE_ABORT);
661 command.parameters.PushBack((uint8_t)opcode);
662 command.parameters.PushBack((uint8_t)reason);
663
664 Transmit(command);
665 }
666
667 bool CCECProcessor::WaitForTransmitSucceeded(uint8_t iLength, uint32_t iTimeout /* = 1000 */)
668 {
669 bool bError(false);
670 bool bTransmitSucceeded(false);
671 uint8_t iPacketsLeft(iLength / 4);
672
673 int64_t iNow = GetTimeMs();
674 int64_t iTargetTime = iNow + (uint64_t) iTimeout;
675
676 while (!bTransmitSucceeded && !bError && (iTimeout == 0 || iNow < iTargetTime))
677 {
678 CCECAdapterMessage msg;
679
680 if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000))
681 {
682 iNow = GetTimeMs();
683 continue;
684 }
685
686 if ((bError = msg.is_error()) == false)
687 {
688 m_controller->AddLog(bError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
689
690 switch(msg.message())
691 {
692 case MSGCODE_COMMAND_ACCEPTED:
693 if (iPacketsLeft > 0)
694 iPacketsLeft--;
695 break;
696 case MSGCODE_TRANSMIT_SUCCEEDED:
697 bTransmitSucceeded = (iPacketsLeft == 0);
698 bError = !bTransmitSucceeded;
699 break;
700 default:
701 if (ParseMessage(msg))
702 m_commandBuffer.Push(m_currentframe);
703 }
704
705 iNow = GetTimeMs();
706 }
707 }
708
709 return bTransmitSucceeded && !bError;
710 }
711
712 bool CCECProcessor::ParseMessage(const CCECAdapterMessage &msg)
713 {
714 bool bEom = false;
715
716 if (msg.empty())
717 return bEom;
718
719 switch(msg.message())
720 {
721 case MSGCODE_FRAME_START:
722 {
723 m_currentframe.Clear();
724 if (msg.size() >= 2)
725 {
726 m_currentframe.initiator = msg.initiator();
727 m_currentframe.destination = msg.destination();
728 m_currentframe.ack = msg.ack();
729 m_currentframe.eom = msg.eom();
730 }
731 }
732 break;
733 case MSGCODE_FRAME_DATA:
734 {
735 if (msg.size() >= 2)
736 {
737 m_currentframe.PushBack(msg[1]);
738 m_currentframe.eom = msg.eom();
739 }
740 bEom = msg.eom();
741 }
742 break;
743 default:
744 break;
745 }
746
747 return bEom;
748 }
749
750 void CCECProcessor::ParseCommand(cec_command &command)
751 {
752 CStdString dataStr;
753 dataStr.Format(">> %1x%1x:%02x", command.initiator, command.destination, command.opcode);
754 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
755 dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
756 m_controller->AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
757
758 if (!m_bMonitor && command.initiator >= CECDEVICE_TV && command.initiator <= CECDEVICE_BROADCAST)
759 m_busDevices[(uint8_t)command.initiator]->HandleCommand(command);
760 }
761
762 cec_logical_addresses CCECProcessor::GetActiveDevices(void)
763 {
764 cec_logical_addresses addresses;
765 addresses.Clear();
766 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
767 {
768 if (m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
769 addresses.Set((cec_logical_address) iPtr);
770 }
771 return addresses;
772 }
773
774 bool CCECProcessor::IsActiveDevice(cec_logical_address address)
775 {
776 return m_busDevices[address]->GetStatus() == CEC_DEVICE_STATUS_PRESENT;
777 }
778
779 bool CCECProcessor::IsActiveDeviceType(cec_device_type type)
780 {
781 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
782 {
783 if (m_busDevices[iPtr]->GetType() == type && m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
784 return true;
785 }
786
787 return false;
788 }
789
790 uint16_t CCECProcessor::GetPhysicalAddress(void) const
791 {
792 if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary])
793 return m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false);
794 return false;
795 }
796
797 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode)
798 {
799 m_controller->SetCurrentButton(iButtonCode);
800 }
801
802 void CCECProcessor::AddCommand(const cec_command &command)
803 {
804 m_controller->AddCommand(command);
805 }
806
807 void CCECProcessor::AddKey(cec_keypress &key)
808 {
809 m_controller->AddKey(key);
810 }
811
812 void CCECProcessor::AddKey(void)
813 {
814 m_controller->AddKey();
815 }
816
817 void CCECProcessor::AddLog(cec_log_level level, const CStdString &strMessage)
818 {
819 m_controller->AddLog(level, strMessage);
820 }
821
822 bool CCECProcessor::SetAckMask(uint16_t iMask)
823 {
824 bool bReturn(false);
825 CStdString strLog;
826 strLog.Format("setting ackmask to %2x", iMask);
827 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
828
829 CCECAdapterMessage *output = new CCECAdapterMessage;
830
831 output->push_back(MSGSTART);
832 output->push_escaped(MSGCODE_SET_ACK_MASK);
833 output->push_escaped(iMask >> 8);
834 output->push_escaped((uint8_t)iMask);
835 output->push_back(MSGEND);
836
837 if ((bReturn = Transmit(output)) == false)
838 m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask");
839
840 delete output;
841
842 return bReturn;
843 }
844
845 bool CCECProcessor::SendKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = false */)
846 {
847 return m_busDevices[iDestination]->SendKeypress(key, bWait);
848 }
849
850 bool CCECProcessor::SendKeyRelease(cec_logical_address iDestination, bool bWait /* = false */)
851 {
852 return m_busDevices[iDestination]->SendKeyRelease(bWait);
853 }
854
855 const char *CCECProcessor::ToString(const cec_menu_state state)
856 {
857 switch (state)
858 {
859 case CEC_MENU_STATE_ACTIVATED:
860 return "activated";
861 case CEC_MENU_STATE_DEACTIVATED:
862 return "deactivated";
863 default:
864 return "unknown";
865 }
866 }
867
868 const char *CCECProcessor::ToString(const cec_version version)
869 {
870 switch (version)
871 {
872 case CEC_VERSION_1_2:
873 return "1.2";
874 case CEC_VERSION_1_2A:
875 return "1.2a";
876 case CEC_VERSION_1_3:
877 return "1.3";
878 case CEC_VERSION_1_3A:
879 return "1.3a";
880 case CEC_VERSION_1_4:
881 return "1.4";
882 default:
883 return "unknown";
884 }
885 }
886
887 const char *CCECProcessor::ToString(const cec_power_status status)
888 {
889 switch (status)
890 {
891 case CEC_POWER_STATUS_ON:
892 return "on";
893 case CEC_POWER_STATUS_STANDBY:
894 return "standby";
895 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY:
896 return "in transition from on to standby";
897 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON:
898 return "in transition from standby to on";
899 default:
900 return "unknown";
901 }
902 }
903
904 const char *CCECProcessor::ToString(const cec_logical_address address)
905 {
906 switch(address)
907 {
908 case CECDEVICE_AUDIOSYSTEM:
909 return "Audio";
910 case CECDEVICE_BROADCAST:
911 return "Broadcast";
912 case CECDEVICE_FREEUSE:
913 return "Free use";
914 case CECDEVICE_PLAYBACKDEVICE1:
915 return "Playback 1";
916 case CECDEVICE_PLAYBACKDEVICE2:
917 return "Playback 2";
918 case CECDEVICE_PLAYBACKDEVICE3:
919 return "Playback 3";
920 case CECDEVICE_RECORDINGDEVICE1:
921 return "Recorder 1";
922 case CECDEVICE_RECORDINGDEVICE2:
923 return "Recorder 2";
924 case CECDEVICE_RECORDINGDEVICE3:
925 return "Recorder 3";
926 case CECDEVICE_RESERVED1:
927 return "Reserved 1";
928 case CECDEVICE_RESERVED2:
929 return "Reserved 2";
930 case CECDEVICE_TUNER1:
931 return "Tuner 1";
932 case CECDEVICE_TUNER2:
933 return "Tuner 2";
934 case CECDEVICE_TUNER3:
935 return "Tuner 3";
936 case CECDEVICE_TUNER4:
937 return "Tuner 4";
938 case CECDEVICE_TV:
939 return "TV";
940 default:
941 return "unknown";
942 }
943 }
944
945 const char *CCECProcessor::ToString(const cec_deck_control_mode mode)
946 {
947 switch (mode)
948 {
949 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND:
950 return "skip forward wind";
951 case CEC_DECK_CONTROL_MODE_EJECT:
952 return "eject";
953 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND:
954 return "reverse rewind";
955 case CEC_DECK_CONTROL_MODE_STOP:
956 return "stop";
957 default:
958 return "unknown";
959 }
960 }
961
962 const char *CCECProcessor::ToString(const cec_deck_info status)
963 {
964 switch (status)
965 {
966 case CEC_DECK_INFO_PLAY:
967 return "play";
968 case CEC_DECK_INFO_RECORD:
969 return "record";
970 case CEC_DECK_INFO_PLAY_REVERSE:
971 return "play reverse";
972 case CEC_DECK_INFO_STILL:
973 return "still";
974 case CEC_DECK_INFO_SLOW:
975 return "slow";
976 case CEC_DECK_INFO_SLOW_REVERSE:
977 return "slow reverse";
978 case CEC_DECK_INFO_FAST_FORWARD:
979 return "fast forward";
980 case CEC_DECK_INFO_FAST_REVERSE:
981 return "fast reverse";
982 case CEC_DECK_INFO_NO_MEDIA:
983 return "no media";
984 case CEC_DECK_INFO_STOP:
985 return "stop";
986 case CEC_DECK_INFO_SKIP_FORWARD_WIND:
987 return "info skip forward wind";
988 case CEC_DECK_INFO_SKIP_REVERSE_REWIND:
989 return "info skip reverse rewind";
990 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD:
991 return "info index search forward";
992 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE:
993 return "info index search reverse";
994 case CEC_DECK_INFO_OTHER_STATUS:
995 return "other";
996 default:
997 return "unknown";
998 }
999 }
1000
1001 const char *CCECProcessor::ToString(const cec_opcode opcode)
1002 {
1003 switch (opcode)
1004 {
1005 case CEC_OPCODE_ACTIVE_SOURCE:
1006 return "active source";
1007 case CEC_OPCODE_IMAGE_VIEW_ON:
1008 return "image view on";
1009 case CEC_OPCODE_TEXT_VIEW_ON:
1010 return "text view on";
1011 case CEC_OPCODE_INACTIVE_SOURCE:
1012 return "inactive source";
1013 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
1014 return "request active source";
1015 case CEC_OPCODE_ROUTING_CHANGE:
1016 return "routing change";
1017 case CEC_OPCODE_ROUTING_INFORMATION:
1018 return "routing information";
1019 case CEC_OPCODE_SET_STREAM_PATH:
1020 return "set stream path";
1021 case CEC_OPCODE_STANDBY:
1022 return "standby";
1023 case CEC_OPCODE_RECORD_OFF:
1024 return "record off";
1025 case CEC_OPCODE_RECORD_ON:
1026 return "record on";
1027 case CEC_OPCODE_RECORD_STATUS:
1028 return "record status";
1029 case CEC_OPCODE_RECORD_TV_SCREEN:
1030 return "record tv screen";
1031 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER:
1032 return "clear analogue timer";
1033 case CEC_OPCODE_CLEAR_DIGITAL_TIMER:
1034 return "clear digital timer";
1035 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER:
1036 return "clear external timer";
1037 case CEC_OPCODE_SET_ANALOGUE_TIMER:
1038 return "set analogue timer";
1039 case CEC_OPCODE_SET_DIGITAL_TIMER:
1040 return "set digital timer";
1041 case CEC_OPCODE_SET_EXTERNAL_TIMER:
1042 return "set external timer";
1043 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE:
1044 return "set timer program title";
1045 case CEC_OPCODE_TIMER_CLEARED_STATUS:
1046 return "timer cleared status";
1047 case CEC_OPCODE_TIMER_STATUS:
1048 return "timer status";
1049 case CEC_OPCODE_CEC_VERSION:
1050 return "cec version";
1051 case CEC_OPCODE_GET_CEC_VERSION:
1052 return "get cec version";
1053 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
1054 return "give physical address";
1055 case CEC_OPCODE_GET_MENU_LANGUAGE:
1056 return "get menu language";
1057 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS:
1058 return "report physical address";
1059 case CEC_OPCODE_SET_MENU_LANGUAGE:
1060 return "set menu language";
1061 case CEC_OPCODE_DECK_CONTROL:
1062 return "deck control";
1063 case CEC_OPCODE_DECK_STATUS:
1064 return "deck status";
1065 case CEC_OPCODE_GIVE_DECK_STATUS:
1066 return "give deck status";
1067 case CEC_OPCODE_PLAY:
1068 return "play";
1069 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS:
1070 return "give tuner status";
1071 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE:
1072 return "select analogue service";
1073 case CEC_OPCODE_SELECT_DIGITAL_SERVICE:
1074 return "set digital service";
1075 case CEC_OPCODE_TUNER_DEVICE_STATUS:
1076 return "tuner device status";
1077 case CEC_OPCODE_TUNER_STEP_DECREMENT:
1078 return "tuner step decrement";
1079 case CEC_OPCODE_TUNER_STEP_INCREMENT:
1080 return "tuner step increment";
1081 case CEC_OPCODE_DEVICE_VENDOR_ID:
1082 return "device vendor id";
1083 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
1084 return "give device vendor id";
1085 case CEC_OPCODE_VENDOR_COMMAND:
1086 return "vendor command";
1087 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
1088 return "vendor command with id";
1089 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN:
1090 return "vendor remote button down";
1091 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP:
1092 return "vendor remote button up";
1093 case CEC_OPCODE_SET_OSD_STRING:
1094 return "set osd string";
1095 case CEC_OPCODE_GIVE_OSD_NAME:
1096 return "give osd name";
1097 case CEC_OPCODE_SET_OSD_NAME:
1098 return "set osd name";
1099 case CEC_OPCODE_MENU_REQUEST:
1100 return "menu request";
1101 case CEC_OPCODE_MENU_STATUS:
1102 return "menu status";
1103 case CEC_OPCODE_USER_CONTROL_PRESSED:
1104 return "user control pressed";
1105 case CEC_OPCODE_USER_CONTROL_RELEASE:
1106 return "user control release";
1107 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
1108 return "give device power status";
1109 case CEC_OPCODE_REPORT_POWER_STATUS:
1110 return "report power status";
1111 case CEC_OPCODE_FEATURE_ABORT:
1112 return "feature abort";
1113 case CEC_OPCODE_ABORT:
1114 return "abort";
1115 case CEC_OPCODE_GIVE_AUDIO_STATUS:
1116 return "give audio status";
1117 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS:
1118 return "give audio mode status";
1119 case CEC_OPCODE_REPORT_AUDIO_STATUS:
1120 return "report audio status";
1121 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE:
1122 return "set system audio mode";
1123 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST:
1124 return "system audio mode request";
1125 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS:
1126 return "system audio mode status";
1127 case CEC_OPCODE_SET_AUDIO_RATE:
1128 return "set audio rate";
1129 default:
1130 return "UNKNOWN";
1131 }
1132 }
1133
1134 const char *CCECProcessor::ToString(const cec_system_audio_status mode)
1135 {
1136 switch(mode)
1137 {
1138 case CEC_SYSTEM_AUDIO_STATUS_ON:
1139 return "on";
1140 case CEC_SYSTEM_AUDIO_STATUS_OFF:
1141 return "off";
1142 default:
1143 return "unknown";
1144 }
1145 }
1146
1147 const char *CCECProcessor::ToString(const cec_audio_status status)
1148 {
1149 // TODO this is a mask
1150 return "TODO";
1151 }
1152
1153 const char *CCECProcessor::ToString(const cec_vendor_id vendor)
1154 {
1155 switch (vendor)
1156 {
1157 case CEC_VENDOR_SAMSUNG:
1158 return "Samsung";
1159 case CEC_VENDOR_LG:
1160 return "LG";
1161 case CEC_VENDOR_PANASONIC:
1162 return "Panasonic";
1163 case CEC_VENDOR_PIONEER:
1164 return "Pioneer";
1165 case CEC_VENDOR_ONKYO:
1166 return "Onkyo";
1167 case CEC_VENDOR_YAMAHA:
1168 return "Yamaha";
1169 case CEC_VENDOR_PHILIPS:
1170 return "Philips";
1171 default:
1172 return "Unknown";
1173 }
1174 }
1175
1176 void *CCECBusScan::Process(void)
1177 {
1178 CCECBusDevice *device(NULL);
1179 while (!IsStopped())
1180 {
1181 for (unsigned int iPtr = 0; iPtr < 15 && !IsStopped(); iPtr++)
1182 {
1183 device = m_processor->m_busDevices[iPtr];
1184 if (device && device->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
1185 {
1186 if (!IsStopped())
1187 device->GetPhysicalAddress(false);
1188 Sleep(5);
1189
1190 if (!IsStopped())
1191 device->GetCecVersion();
1192 Sleep(5);
1193
1194 if (!IsStopped())
1195 device->GetVendorId();
1196 Sleep(5);
1197 }
1198 }
1199 Sleep(1000);
1200 }
1201 return NULL;
1202 }