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"
38 #include "util/StdString.h"
39 #include "platform/timeutils.h"
44 CCECProcessor::CCECProcessor(CLibCEC
*controller
, CAdapterCommunication
*serComm
, const char *strDeviceName
, cec_logical_address iLogicalAddress
/* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress
/* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) :
45 m_iLogicalAddress(iLogicalAddress
),
46 m_strDeviceName(strDeviceName
),
47 m_communication(serComm
),
48 m_controller(controller
),
50 m_bLogicalAddressSet(false)
52 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
53 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, iPtr
== iLogicalAddress
? iPhysicalAddress
: 0);
56 CCECProcessor::~CCECProcessor(void)
58 m_startCondition
.Broadcast();
60 m_communication
= NULL
;
62 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
63 delete m_busDevices
[iPtr
];
66 bool CCECProcessor::Start(void)
68 CLockObject
lock(&m_mutex
);
69 if (!m_communication
|| !m_communication
->IsOpen())
71 m_controller
->AddLog(CEC_LOG_ERROR
, "connection is closed");
75 if (!SetLogicalAddress(m_iLogicalAddress
))
77 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the logical address");
83 if (!m_startCondition
.Wait(&m_mutex
))
85 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
91 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
96 void *CCECProcessor::Process(void)
99 CLockObject
lock(&m_mutex
);
100 m_controller
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
101 m_startCondition
.Signal();
105 CCECAdapterMessage msg
;
107 m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
111 bool bParseFrame(false);
116 CLockObject
lock(&m_mutex
);
117 if (m_communication
->IsOpen() && m_communication
->Read(msg
, 50))
118 bParseFrame
= ParseMessage(msg
) && !IsStopped();
121 command
= m_currentframe
;
125 ParseCommand(command
);
127 m_controller
->CheckKeypressTimeout();
129 for (unsigned int iDevicePtr
= 0; iDevicePtr
< 16; iDevicePtr
++)
130 m_busDevices
[iDevicePtr
]->PollVendorId();
139 bool CCECProcessor::SetActiveView(void)
144 if (m_iLogicalAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[m_iLogicalAddress
])
145 return m_busDevices
[m_iLogicalAddress
]->BroadcastActiveView();
149 bool CCECProcessor::SetInactiveView(void)
154 if (m_iLogicalAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[m_iLogicalAddress
])
155 return m_busDevices
[m_iLogicalAddress
]->BroadcastInactiveView();
159 void CCECProcessor::LogOutput(const cec_command
&data
)
162 strTx
.Format("<< %02x:%02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
, (uint8_t)data
.opcode
);
164 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
165 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
166 m_controller
->AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
169 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
/* = CECDEVICE_UNKNOWN */)
171 if (iLogicalAddress
!= CECDEVICE_UNKNOWN
)
174 strLog
.Format("<< setting logical address to %1x", iLogicalAddress
);
175 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
176 m_iLogicalAddress
= iLogicalAddress
;
177 m_bLogicalAddressSet
= false;
180 if (!m_bLogicalAddressSet
&& m_iLogicalAddress
!= CECDEVICE_UNKNOWN
)
181 m_bLogicalAddressSet
= m_communication
&& m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
183 return m_bLogicalAddressSet
;
186 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
)
188 if (m_iLogicalAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[m_iLogicalAddress
])
190 m_busDevices
[m_iLogicalAddress
]->SetPhysicalAddress(iPhysicalAddress
);
191 return m_busDevices
[m_iLogicalAddress
]->BroadcastActiveView();
196 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
199 strLog
.Format("== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
200 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
202 m_bMonitor
= bEnable
;
204 return m_communication
&& m_communication
->SetAckMask(0);
206 return m_communication
&& m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
209 cec_version
CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress
)
211 return m_busDevices
[iAddress
]->GetCecVersion();
214 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
216 if (m_busDevices
[iAddress
])
218 *language
= m_busDevices
[iAddress
]->GetMenuLanguage();
219 return (strcmp(language
->language
, "???") == 0);
224 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress
)
226 if (m_busDevices
[iAddress
])
227 return m_busDevices
[iAddress
]->GetVendorId();
231 cec_power_status
CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress
)
233 if (m_busDevices
[iAddress
])
234 return m_busDevices
[iAddress
]->GetPowerStatus();
235 return CEC_POWER_STATUS_UNKNOWN
;
238 bool CCECProcessor::Transmit(const cec_command
&data
)
245 CCECAdapterMessagePtr
output(new CCECAdapterMessage(data
));
247 CLockObject
lock(&m_mutex
);
249 CLockObject
msgLock(&output
->mutex
);
250 if (!m_communication
|| !m_communication
->Write(output
))
254 output
->condition
.Wait(&output
->mutex
, 1000);
255 if (output
->state
!= ADAPTER_MESSAGE_STATE_SENT
)
257 m_controller
->AddLog(CEC_LOG_ERROR
, "command was not sent");
262 if (data
.transmit_timeout
> 0)
264 if ((bReturn
= WaitForTransmitSucceeded(output
->size(), data
.transmit_timeout
)) == false)
265 m_controller
->AddLog(CEC_LOG_ERROR
, "did not receive ack");
274 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, ECecAbortReason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
276 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
279 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_FEATURE_ABORT
);
280 command
.parameters
.push_back((uint8_t)opcode
);
281 command
.parameters
.push_back((uint8_t)reason
);
286 bool CCECProcessor::WaitForTransmitSucceeded(uint8_t iLength
, uint32_t iTimeout
/* = 1000 */)
289 bool bTransmitSucceeded(false);
290 uint8_t iPacketsLeft(iLength
/ 4);
292 int64_t iNow
= GetTimeMs();
293 int64_t iTargetTime
= iNow
+ (uint64_t) iTimeout
;
295 while (!bTransmitSucceeded
&& !bError
&& (iTimeout
== 0 || iNow
< iTargetTime
))
297 CCECAdapterMessage msg
;
299 if (!m_communication
->Read(msg
, iTimeout
> 0 ? (int32_t)(iTargetTime
- iNow
) : 1000))
305 bError
= msg
.is_error();
306 m_controller
->AddLog(msg
.is_error() ? CEC_LOG_WARNING
: CEC_LOG_DEBUG
, msg
.ToString());
308 switch(msg
.message())
310 case MSGCODE_COMMAND_ACCEPTED
:
313 case MSGCODE_TRANSMIT_SUCCEEDED
:
314 bTransmitSucceeded
= (iPacketsLeft
== 0);
315 bError
= !bTransmitSucceeded
;
319 strLog
.Format("received unexpected reply '%s' instead of ack", msg
.MessageCodeAsString().c_str());
320 m_controller
->AddLog(CEC_LOG_WARNING
, strLog
);
328 return bTransmitSucceeded
&& !bError
;
331 bool CCECProcessor::ParseMessage(const CCECAdapterMessage
&msg
)
338 m_controller
->AddLog(msg
.is_error() ? CEC_LOG_WARNING
: CEC_LOG_DEBUG
, msg
.ToString());
340 switch(msg
.message())
342 case MSGCODE_FRAME_START
:
344 m_currentframe
.clear();
347 m_currentframe
.initiator
= msg
.initiator();
348 m_currentframe
.destination
= msg
.destination();
349 m_currentframe
.ack
= msg
.ack();
350 m_currentframe
.eom
= msg
.eom();
354 case MSGCODE_FRAME_DATA
:
358 m_currentframe
.push_back(msg
[1]);
359 m_currentframe
.eom
= msg
.eom();
371 void CCECProcessor::ParseCommand(cec_command
&command
)
374 dataStr
.Format(">> %1x%1x:%02x", command
.initiator
, command
.destination
, command
.opcode
);
375 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
376 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
377 m_controller
->AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
380 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
383 uint16_t CCECProcessor::GetPhysicalAddress(void) const
385 if (m_iLogicalAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[m_iLogicalAddress
])
386 return m_busDevices
[m_iLogicalAddress
]->GetPhysicalAddress();
390 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode
)
392 m_controller
->SetCurrentButton(iButtonCode
);
395 void CCECProcessor::AddCommand(const cec_command
&command
)
397 m_controller
->AddCommand(command
);
400 void CCECProcessor::AddKey(void)
402 m_controller
->AddKey();
405 void CCECProcessor::AddLog(cec_log_level level
, const CStdString
&strMessage
)
407 m_controller
->AddLog(level
, strMessage
);