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