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