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