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