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