2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
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.
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.
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.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
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/
33 #include "CECProcessor.h"
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"
44 #include "util/StdString.h"
45 #include "platform/timeutils.h"
50 CCECProcessor::CCECProcessor(CLibCEC
*controller
, CAdapterCommunication
*serComm
, const char *strDeviceName
, cec_logical_address iLogicalAddress
/* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress
/* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) :
52 m_strDeviceName(strDeviceName
),
53 m_communication(serComm
),
54 m_controller(controller
),
57 m_logicalAddresses
.Clear();
58 m_logicalAddresses
.Set(iLogicalAddress
);
60 for (int iPtr
= 0; iPtr
<= 16; iPtr
++)
61 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, iPtr
== iLogicalAddress
? iPhysicalAddress
: 0);
64 CCECProcessor::CCECProcessor(CLibCEC
*controller
, CAdapterCommunication
*serComm
, const char *strDeviceName
, const cec_device_type_list
&types
) :
66 m_strDeviceName(strDeviceName
),
68 m_communication(serComm
),
69 m_controller(controller
),
72 m_logicalAddresses
.Clear();
73 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
77 case CECDEVICE_AUDIOSYSTEM
:
78 m_busDevices
[iPtr
] = new CCECAudioSystem(this, (cec_logical_address
) iPtr
, 0xFFFF);
80 case CECDEVICE_PLAYBACKDEVICE1
:
81 case CECDEVICE_PLAYBACKDEVICE2
:
82 case CECDEVICE_PLAYBACKDEVICE3
:
83 m_busDevices
[iPtr
] = new CCECPlaybackDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
85 case CECDEVICE_RECORDINGDEVICE1
:
86 case CECDEVICE_RECORDINGDEVICE2
:
87 case CECDEVICE_RECORDINGDEVICE3
:
88 m_busDevices
[iPtr
] = new CCECRecordingDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
90 case CECDEVICE_TUNER1
:
91 case CECDEVICE_TUNER2
:
92 case CECDEVICE_TUNER3
:
93 case CECDEVICE_TUNER4
:
94 m_busDevices
[iPtr
] = new CCECTuner(this, (cec_logical_address
) iPtr
, 0xFFFF);
97 m_busDevices
[iPtr
] = new CCECTV(this, (cec_logical_address
) iPtr
, 0);
100 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
106 CCECProcessor::~CCECProcessor(void)
108 m_startCondition
.Broadcast();
110 m_communication
= NULL
;
112 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
113 delete m_busDevices
[iPtr
];
116 bool CCECProcessor::Start(void)
118 CLockObject
lock(&m_mutex
);
119 if (!m_communication
|| !m_communication
->IsOpen())
121 m_controller
->AddLog(CEC_LOG_ERROR
, "connection is closed");
127 if (!m_startCondition
.Wait(&m_mutex
) || !m_bStarted
)
129 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
135 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
140 bool CCECProcessor::TryLogicalAddress(cec_logical_address address
, unsigned int iIndex
)
142 const char *strLabel
= CCECCommandHandler::ToString(address
);
144 strLog
.Format("trying logical address '%s'", strLabel
);
145 AddLog(CEC_LOG_DEBUG
, strLog
);
147 SetAckMask(0x1 << address
);
148 if (!m_busDevices
[address
]->TransmitPoll(address
))
150 strLog
.Format("using logical address '%s'", strLabel
);
151 AddLog(CEC_LOG_NOTICE
, strLog
);
153 /* only set our OSD name and active source for the primary device */
154 if (m_logicalAddresses
.IsEmpty())
156 m_busDevices
[address
]->m_strDeviceName
= m_strDeviceName
;
157 m_busDevices
[address
]->m_bActiveSource
= true;
159 m_busDevices
[address
]->m_powerStatus
= CEC_POWER_STATUS_STANDBY
;
160 m_busDevices
[address
]->m_cecVersion
= CEC_VERSION_1_3A
;
161 m_busDevices
[address
]->m_deviceStatus
= CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
;
163 m_logicalAddresses
.Set(address
);
166 m_busDevices
[address
]->SetPhysicalAddress((uint16_t)CEC_DEFAULT_PHYSICAL_ADDRESS
+ ((uint16_t)iIndex
* 0x100));
171 strLog
.Format("logical address '%s' already taken", strLabel
);
172 AddLog(CEC_LOG_DEBUG
, strLog
);
176 bool CCECProcessor::FindLogicalAddressRecordingDevice(unsigned int iIndex
)
178 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'recording device'");
179 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1
, iIndex
) ||
180 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2
, iIndex
) ||
181 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3
, iIndex
);
184 bool CCECProcessor::FindLogicalAddressTuner(unsigned int iIndex
)
186 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'tuner'");
187 return TryLogicalAddress(CECDEVICE_TUNER1
, iIndex
) ||
188 TryLogicalAddress(CECDEVICE_TUNER2
, iIndex
) ||
189 TryLogicalAddress(CECDEVICE_TUNER3
, iIndex
) ||
190 TryLogicalAddress(CECDEVICE_TUNER4
, iIndex
);
193 bool CCECProcessor::FindLogicalAddressPlaybackDevice(unsigned int iIndex
)
195 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'playback device'");
196 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1
, iIndex
) ||
197 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2
, iIndex
) ||
198 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3
, iIndex
);
201 bool CCECProcessor::FindLogicalAddressAudioSystem(unsigned int iIndex
)
203 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'audio'");
204 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM
, iIndex
);
207 bool CCECProcessor::FindLogicalAddresses(void)
210 m_logicalAddresses
.Clear();
213 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
215 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
218 strLog
.Format("%s - device %d: type %d", __FUNCTION__
, iPtr
, m_types
.types
[iPtr
]);
219 AddLog(CEC_LOG_DEBUG
, strLog
);
221 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
222 bReturn
&= FindLogicalAddressRecordingDevice(iPtr
);
223 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_TUNER
)
224 bReturn
&= FindLogicalAddressTuner(iPtr
);
225 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
)
226 bReturn
&= FindLogicalAddressPlaybackDevice(iPtr
);
227 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
228 bReturn
&= FindLogicalAddressAudioSystem(iPtr
);
234 void *CCECProcessor::Process(void)
236 bool bParseFrame(false);
238 CCECAdapterMessage msg
;
241 if (m_logicalAddresses
.IsEmpty() && !FindLogicalAddresses())
243 CLockObject
lock(&m_mutex
);
244 m_controller
->AddLog(CEC_LOG_ERROR
, "could not detect our logical addressed");
245 m_startCondition
.Signal();
249 SetAckMask(m_logicalAddresses
.AckMask());
252 CLockObject
lock(&m_mutex
);
253 m_controller
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
255 m_startCondition
.Signal();
265 CLockObject
lock(&m_mutex
);
266 if (m_commandBuffer
.Pop(command
))
270 else if (m_communication
->IsOpen() && m_communication
->Read(msg
, 50))
272 m_controller
->AddLog(msg
.is_error() ? CEC_LOG_WARNING
: CEC_LOG_DEBUG
, msg
.ToString());
273 if ((bParseFrame
= (ParseMessage(msg
) && !IsStopped())) == true)
274 command
= m_currentframe
;
279 ParseCommand(command
);
284 m_controller
->CheckKeypressTimeout();
286 for (unsigned int iDevicePtr
= 0; iDevicePtr
< 16; iDevicePtr
++)
287 m_busDevices
[iDevicePtr
]->PollVendorId();
295 bool CCECProcessor::SetActiveSource(cec_device_type type
/* = CEC_DEVICE_TYPE_RESERVED */)
302 cec_logical_address addr
= m_logicalAddresses
.primary
;
304 if (type
!= CEC_DEVICE_TYPE_RESERVED
)
306 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
308 if (m_logicalAddresses
[iPtr
] && m_busDevices
[iPtr
]->m_type
== type
)
310 addr
= (cec_logical_address
) iPtr
;
316 return SetStreamPath(m_busDevices
[addr
]->GetPhysicalAddress()) &&
317 m_busDevices
[addr
]->TransmitActiveSource();
320 bool CCECProcessor::SetActiveView(void)
322 return SetActiveSource(m_types
.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED
: m_types
[0]);
325 bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode
, bool bSendUpdate
/* = true */)
329 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
332 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode(mode
);
334 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
341 bool CCECProcessor::SetDeckInfo(cec_deck_info info
, bool bSendUpdate
/* = true */)
345 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
348 ((CCECPlaybackDevice
*) device
)->SetDeckStatus(info
);
350 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
357 bool CCECProcessor::SetStreamPath(uint16_t iStreamPath
)
361 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamPath
);
364 device
->SetActiveDevice();
371 bool CCECProcessor::SetInactiveView(void)
376 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
377 return m_busDevices
[m_logicalAddresses
.primary
]->TransmitInactiveView();
381 void CCECProcessor::LogOutput(const cec_command
&data
)
384 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
386 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
388 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
389 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
390 m_controller
->AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
393 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
395 if (m_logicalAddresses
.primary
!= iLogicalAddress
)
398 strLog
.Format("<< setting primary logical address to %1x", iLogicalAddress
);
399 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
400 m_logicalAddresses
.primary
= iLogicalAddress
;
401 m_logicalAddresses
.Set(iLogicalAddress
);
402 return SetAckMask(m_logicalAddresses
.AckMask());
408 bool CCECProcessor::SetMenuState(cec_menu_state state
, bool bSendUpdate
/* = true */)
410 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
412 if (m_logicalAddresses
[iPtr
])
413 m_busDevices
[iPtr
]->SetMenuState(state
);
417 m_busDevices
[m_logicalAddresses
.primary
]->TransmitMenuState(CECDEVICE_TV
);
422 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
)
424 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
426 m_busDevices
[m_logicalAddresses
.primary
]->SetPhysicalAddress(iPhysicalAddress
);
427 return SetActiveView();
432 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
435 strLog
.Format("== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
436 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
438 m_bMonitor
= bEnable
;
440 return SetAckMask(0);
442 return SetAckMask(m_logicalAddresses
.AckMask());
445 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
447 if (iAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[iAddress
])
448 return m_busDevices
[m_logicalAddresses
.primary
]->TransmitPoll(iAddress
);
452 CCECBusDevice
*CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
454 CCECBusDevice
*device
= NULL
;
456 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
458 if (m_busDevices
[iPtr
]->GetPhysicalAddress() == iPhysicalAddress
)
460 device
= m_busDevices
[iPtr
];
468 CCECBusDevice
*CCECProcessor::GetDeviceByType(cec_device_type type
) const
470 CCECBusDevice
*device
= NULL
;
472 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
474 if (m_busDevices
[iPtr
]->m_type
== type
)
476 device
= m_busDevices
[iPtr
];
484 cec_version
CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress
)
486 return m_busDevices
[iAddress
]->GetCecVersion();
489 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
491 if (m_busDevices
[iAddress
])
493 *language
= m_busDevices
[iAddress
]->GetMenuLanguage();
494 return (strcmp(language
->language
, "???") != 0);
499 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress
)
501 if (m_busDevices
[iAddress
])
502 return m_busDevices
[iAddress
]->GetVendorId();
506 cec_power_status
CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress
)
508 if (m_busDevices
[iAddress
])
509 return m_busDevices
[iAddress
]->GetPowerStatus();
510 return CEC_POWER_STATUS_UNKNOWN
;
513 bool CCECProcessor::Transmit(const cec_command
&data
)
518 CCECAdapterMessage
*output
= new CCECAdapterMessage(data
);
519 bReturn
= Transmit(output
);
525 bool CCECProcessor::Transmit(CCECAdapterMessage
*output
)
528 CLockObject
lock(&m_mutex
);
530 CLockObject
msgLock(&output
->mutex
);
531 if (!m_communication
|| !m_communication
->Write(output
))
535 output
->condition
.Wait(&output
->mutex
);
536 if (output
->state
!= ADAPTER_MESSAGE_STATE_SENT
)
538 m_controller
->AddLog(CEC_LOG_ERROR
, "command was not sent");
543 if (output
->transmit_timeout
> 0)
545 if ((bReturn
= WaitForTransmitSucceeded(output
->size(), output
->transmit_timeout
)) == false)
546 m_controller
->AddLog(CEC_LOG_DEBUG
, "did not receive ack");
555 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, cec_abort_reason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
557 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
561 cec_command::Format(command
, m_logicalAddresses
.primary
, address
, CEC_OPCODE_FEATURE_ABORT
);
562 command
.parameters
.PushBack((uint8_t)opcode
);
563 command
.parameters
.PushBack((uint8_t)reason
);
568 bool CCECProcessor::WaitForTransmitSucceeded(uint8_t iLength
, uint32_t iTimeout
/* = 1000 */)
571 bool bTransmitSucceeded(false);
572 uint8_t iPacketsLeft(iLength
/ 4);
574 int64_t iNow
= GetTimeMs();
575 int64_t iTargetTime
= iNow
+ (uint64_t) iTimeout
;
577 while (!bTransmitSucceeded
&& !bError
&& (iTimeout
== 0 || iNow
< iTargetTime
))
579 CCECAdapterMessage msg
;
581 if (!m_communication
->Read(msg
, iTimeout
> 0 ? (int32_t)(iTargetTime
- iNow
) : 1000))
587 if ((bError
= msg
.is_error()) == false)
589 m_controller
->AddLog(bError
? CEC_LOG_WARNING
: CEC_LOG_DEBUG
, msg
.ToString());
591 switch(msg
.message())
593 case MSGCODE_COMMAND_ACCEPTED
:
594 if (iPacketsLeft
> 0)
597 case MSGCODE_TRANSMIT_SUCCEEDED
:
598 bTransmitSucceeded
= (iPacketsLeft
== 0);
599 bError
= !bTransmitSucceeded
;
602 if (ParseMessage(msg
))
603 m_commandBuffer
.Push(m_currentframe
);
610 return bTransmitSucceeded
&& !bError
;
613 bool CCECProcessor::ParseMessage(const CCECAdapterMessage
&msg
)
620 switch(msg
.message())
622 case MSGCODE_FRAME_START
:
624 m_currentframe
.Clear();
627 m_currentframe
.initiator
= msg
.initiator();
628 m_currentframe
.destination
= msg
.destination();
629 m_currentframe
.ack
= msg
.ack();
630 m_currentframe
.eom
= msg
.eom();
634 case MSGCODE_FRAME_DATA
:
638 m_currentframe
.PushBack(msg
[1]);
639 m_currentframe
.eom
= msg
.eom();
651 void CCECProcessor::ParseCommand(cec_command
&command
)
654 dataStr
.Format(">> %1x%1x:%02x", command
.initiator
, command
.destination
, command
.opcode
);
655 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
656 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
657 m_controller
->AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
659 if (!m_bMonitor
&& command
.initiator
>= CECDEVICE_TV
&& command
.initiator
<= CECDEVICE_BROADCAST
)
660 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
663 uint16_t CCECProcessor::GetPhysicalAddress(void) const
665 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
666 return m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress();
670 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode
)
672 m_controller
->SetCurrentButton(iButtonCode
);
675 void CCECProcessor::AddCommand(const cec_command
&command
)
677 m_controller
->AddCommand(command
);
680 void CCECProcessor::AddKey(cec_keypress
&key
)
682 m_controller
->AddKey(key
);
685 void CCECProcessor::AddKey(void)
687 m_controller
->AddKey();
690 void CCECProcessor::AddLog(cec_log_level level
, const CStdString
&strMessage
)
692 m_controller
->AddLog(level
, strMessage
);
695 bool CCECProcessor::SetAckMask(uint16_t iMask
)
699 strLog
.Format("setting ackmask to %2x", iMask
);
700 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
702 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
704 output
->push_back(MSGSTART
);
705 output
->push_escaped(MSGCODE_SET_ACK_MASK
);
706 output
->push_escaped(iMask
>> 8);
707 output
->push_escaped((uint8_t)iMask
);
708 output
->push_back(MSGEND
);
710 if ((bReturn
= Transmit(output
)) == false)
711 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the ackmask");