cec: pass unexpected replies to ParseMessage() in WaitForTransmitSucceeded(). added...
[deb_libcec.git] / src / lib / CECProcessor.cpp
... / ...
CommitLineData
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 "LibCEC.h"
38#include "util/StdString.h"
39#include "platform/timeutils.h"
40
41using namespace CEC;
42using namespace std;
43
44CCECProcessor::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),
49 m_bMonitor(false),
50 m_bLogicalAddressSet(false)
51{
52 for (int iPtr = 0; iPtr < 16; iPtr++)
53 m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, iPtr == iLogicalAddress ? iPhysicalAddress : 0);
54}
55
56CCECProcessor::~CCECProcessor(void)
57{
58 m_startCondition.Broadcast();
59 StopThread();
60 m_communication = NULL;
61 m_controller = NULL;
62 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
63 delete m_busDevices[iPtr];
64}
65
66bool CCECProcessor::Start(void)
67{
68 CLockObject lock(&m_mutex);
69 if (!m_communication || !m_communication->IsOpen())
70 {
71 m_controller->AddLog(CEC_LOG_ERROR, "connection is closed");
72 return false;
73 }
74
75 if (!SetLogicalAddress(m_iLogicalAddress))
76 {
77 m_controller->AddLog(CEC_LOG_ERROR, "could not set the logical address");
78 return false;
79 }
80
81 if (CreateThread())
82 {
83 if (!m_startCondition.Wait(&m_mutex))
84 {
85 m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
86 return false;
87 }
88 return true;
89 }
90 else
91 m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
92
93 return false;
94}
95
96void *CCECProcessor::Process(void)
97{
98 {
99 CLockObject lock(&m_mutex);
100 m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
101 m_startCondition.Signal();
102 }
103
104 cec_command command;
105 CCECAdapterMessage msg;
106
107 m_communication->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
108
109 while (!IsStopped())
110 {
111 bool bParseFrame(false);
112 command.clear();
113 msg.clear();
114
115 {
116 CLockObject lock(&m_mutex);
117 if (m_communication->IsOpen() && m_communication->Read(msg, 50))
118 {
119 m_controller->AddLog(msg.is_error() ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
120 bParseFrame = ParseMessage(msg) && !IsStopped();
121 }
122
123 if (bParseFrame)
124 command = m_currentframe;
125 }
126
127 if (bParseFrame)
128 ParseCommand(command);
129
130 Sleep(5);
131
132 m_controller->CheckKeypressTimeout();
133
134 for (unsigned int iDevicePtr = 0; iDevicePtr < 16; iDevicePtr++)
135 m_busDevices[iDevicePtr]->PollVendorId();
136
137 Sleep(5);
138 }
139
140 return NULL;
141}
142
143bool CCECProcessor::SetActiveView(void)
144{
145 if (!IsRunning())
146 return false;
147
148 if (m_iLogicalAddress != CECDEVICE_UNKNOWN && m_busDevices[m_iLogicalAddress])
149 return m_busDevices[m_iLogicalAddress]->BroadcastActiveView();
150 return false;
151}
152
153bool CCECProcessor::SetInactiveView(void)
154{
155 if (!IsRunning())
156 return false;
157
158 if (m_iLogicalAddress != CECDEVICE_UNKNOWN && m_busDevices[m_iLogicalAddress])
159 return m_busDevices[m_iLogicalAddress]->BroadcastInactiveView();
160 return false;
161}
162
163void CCECProcessor::LogOutput(const cec_command &data)
164{
165 CStdString strTx;
166 strTx.Format("<< %02x:%02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination, (uint8_t)data.opcode);
167
168 for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++)
169 strTx.AppendFormat(":%02x", data.parameters[iPtr]);
170 m_controller->AddLog(CEC_LOG_TRAFFIC, strTx.c_str());
171}
172
173bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress /* = CECDEVICE_UNKNOWN */)
174{
175 if (iLogicalAddress != CECDEVICE_UNKNOWN)
176 {
177 CStdString strLog;
178 strLog.Format("<< setting logical address to %1x", iLogicalAddress);
179 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
180 m_iLogicalAddress = iLogicalAddress;
181 m_bLogicalAddressSet = false;
182 }
183
184 if (!m_bLogicalAddressSet && m_iLogicalAddress != CECDEVICE_UNKNOWN)
185 m_bLogicalAddressSet = m_communication && m_communication->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
186
187 return m_bLogicalAddressSet;
188}
189
190bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress)
191{
192 if (m_iLogicalAddress != CECDEVICE_UNKNOWN && m_busDevices[m_iLogicalAddress])
193 {
194 m_busDevices[m_iLogicalAddress]->SetPhysicalAddress(iPhysicalAddress);
195 return m_busDevices[m_iLogicalAddress]->BroadcastActiveView();
196 }
197 return false;
198}
199
200bool CCECProcessor::SwitchMonitoring(bool bEnable)
201{
202 CStdString strLog;
203 strLog.Format("== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
204 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
205
206 m_bMonitor = bEnable;
207 if (bEnable)
208 return m_communication && m_communication->SetAckMask(0);
209 else
210 return m_communication && m_communication->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
211}
212
213cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress)
214{
215 return m_busDevices[iAddress]->GetCecVersion();
216}
217
218bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
219{
220 if (m_busDevices[iAddress])
221 {
222 *language = m_busDevices[iAddress]->GetMenuLanguage();
223 return (strcmp(language->language, "???") != 0);
224 }
225 return false;
226}
227
228uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress)
229{
230 if (m_busDevices[iAddress])
231 return m_busDevices[iAddress]->GetVendorId();
232 return false;
233}
234
235cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress)
236{
237 if (m_busDevices[iAddress])
238 return m_busDevices[iAddress]->GetPowerStatus();
239 return CEC_POWER_STATUS_UNKNOWN;
240}
241
242bool CCECProcessor::Transmit(const cec_command &data)
243{
244 SetLogicalAddress();
245
246 bool bReturn(false);
247 LogOutput(data);
248
249 CCECAdapterMessagePtr output(new CCECAdapterMessage(data));
250
251 CLockObject lock(&m_mutex);
252 {
253 CLockObject msgLock(&output->mutex);
254 if (!m_communication || !m_communication->Write(output))
255 return bReturn;
256 else
257 {
258 output->condition.Wait(&output->mutex, 1000);
259 if (output->state != ADAPTER_MESSAGE_STATE_SENT)
260 {
261 m_controller->AddLog(CEC_LOG_ERROR, "command was not sent");
262 return bReturn;
263 }
264 }
265
266 if (data.transmit_timeout > 0)
267 {
268 if ((bReturn = WaitForTransmitSucceeded(output->size(), data.transmit_timeout)) == false)
269 m_controller->AddLog(CEC_LOG_ERROR, "did not receive ack");
270 }
271 else
272 bReturn = true;
273 }
274
275 return bReturn;
276}
277
278void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, ECecAbortReason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
279{
280 m_controller->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
281
282 cec_command command;
283 cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_FEATURE_ABORT);
284 command.parameters.push_back((uint8_t)opcode);
285 command.parameters.push_back((uint8_t)reason);
286
287 Transmit(command);
288}
289
290bool CCECProcessor::WaitForTransmitSucceeded(uint8_t iLength, uint32_t iTimeout /* = 1000 */)
291{
292 bool bError(false);
293 bool bTransmitSucceeded(false);
294 uint8_t iPacketsLeft(iLength / 4);
295
296 int64_t iNow = GetTimeMs();
297 int64_t iTargetTime = iNow + (uint64_t) iTimeout;
298
299 while (!bTransmitSucceeded && !bError && (iTimeout == 0 || iNow < iTargetTime))
300 {
301 CCECAdapterMessage msg;
302
303 if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000))
304 {
305 iNow = GetTimeMs();
306 continue;
307 }
308
309 bError = msg.is_error();
310 m_controller->AddLog(msg.is_error() ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
311
312 switch(msg.message())
313 {
314 case MSGCODE_COMMAND_ACCEPTED:
315 iPacketsLeft--;
316 break;
317 case MSGCODE_TRANSMIT_SUCCEEDED:
318 bTransmitSucceeded = (iPacketsLeft == 0);
319 bError = !bTransmitSucceeded;
320 break;
321 default:
322 CStdString strLog;
323 strLog.Format("received unexpected reply '%s' instead of ack", msg.MessageCodeAsString().c_str());
324 m_controller->AddLog(CEC_LOG_WARNING, strLog);
325 ParseMessage(msg);
326 bError = true;
327 break;
328 }
329
330 iNow = GetTimeMs();
331 }
332
333 return bTransmitSucceeded && !bError;
334}
335
336bool CCECProcessor::ParseMessage(const CCECAdapterMessage &msg)
337{
338 bool bEom = false;
339
340 if (msg.empty())
341 return bEom;
342
343 switch(msg.message())
344 {
345 case MSGCODE_FRAME_START:
346 {
347 m_currentframe.clear();
348 if (msg.size() >= 2)
349 {
350 m_currentframe.initiator = msg.initiator();
351 m_currentframe.destination = msg.destination();
352 m_currentframe.ack = msg.ack();
353 m_currentframe.eom = msg.eom();
354 }
355 }
356 break;
357 case MSGCODE_FRAME_DATA:
358 {
359 if (msg.size() >= 2)
360 {
361 m_currentframe.push_back(msg[1]);
362 m_currentframe.eom = msg.eom();
363 }
364 bEom = msg.eom();
365 }
366 break;
367 default:
368 break;
369 }
370
371 return bEom;
372}
373
374void CCECProcessor::ParseCommand(cec_command &command)
375{
376 CStdString dataStr;
377 dataStr.Format(">> %1x%1x:%02x", command.initiator, command.destination, command.opcode);
378 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
379 dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
380 m_controller->AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
381
382 if (!m_bMonitor)
383 m_busDevices[(uint8_t)command.initiator]->HandleCommand(command);
384}
385
386uint16_t CCECProcessor::GetPhysicalAddress(void) const
387{
388 if (m_iLogicalAddress != CECDEVICE_UNKNOWN && m_busDevices[m_iLogicalAddress])
389 return m_busDevices[m_iLogicalAddress]->GetPhysicalAddress();
390 return false;
391}
392
393void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode)
394{
395 m_controller->SetCurrentButton(iButtonCode);
396}
397
398void CCECProcessor::AddCommand(const cec_command &command)
399{
400// m_controller->AddCommand(command);
401}
402
403void CCECProcessor::AddKey(void)
404{
405 m_controller->AddKey();
406}
407
408void CCECProcessor::AddLog(cec_log_level level, const CStdString &strMessage)
409{
410 m_controller->AddLog(level, strMessage);
411}