7e8af29c0a88abce63037bb29e2e614f5e5133f5
[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
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::SetLogicalAddress(cec_logical_address iLogicalAddress)
151 {
152 if (m_iLogicalAddress != iLogicalAddress)
153 {
154 CStdString strLog;
155 strLog.Format("<< setting logical address to %1x", iLogicalAddress);
156 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
157
158 m_iLogicalAddress = iLogicalAddress;
159 return m_communication && m_communication->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
160 }
161
162 return true;
163 }
164
165 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress)
166 {
167 m_busDevices[m_iLogicalAddress]->SetPhysicalAddress(iPhysicalAddress);
168 return m_busDevices[m_iLogicalAddress]->BroadcastActiveView();
169 }
170
171 bool CCECProcessor::SwitchMonitoring(bool bEnable)
172 {
173 CStdString strLog;
174 strLog.Format("== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
175 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
176
177 m_bMonitor = bEnable;
178 if (bEnable)
179 return m_communication && m_communication->SetAckMask(0);
180 else
181 return m_communication && m_communication->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
182 }
183
184 bool CCECProcessor::Transmit(const cec_command &data, bool bWaitForAck /* = true */)
185 {
186 bool bReturn(false);
187 LogOutput(data);
188
189 CCECAdapterMessage output(data);
190
191 CLockObject lock(&m_mutex);
192 if (!m_communication || !m_communication->Write(output))
193 return bReturn;
194
195 if (bWaitForAck)
196 {
197 bool bError(false);
198 if ((bReturn = WaitForAck(&bError, output.size(), 1000)) == false)
199 m_controller->AddLog(CEC_LOG_ERROR, "did not receive ack");
200 }
201 else
202 {
203 bReturn = true;
204 }
205
206 return bReturn;
207 }
208
209 void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, ECecAbortReason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
210 {
211 m_controller->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
212
213 cec_command command;
214 cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_FEATURE_ABORT);
215 command.parameters.push_back((uint8_t)opcode);
216 command.parameters.push_back((uint8_t)reason);
217
218 Transmit(command);
219 }
220
221 bool CCECProcessor::WaitForAck(bool *bError, uint8_t iLength, uint32_t iTimeout /* = 1000 */)
222 {
223 bool bTransmitSucceeded = false;
224 uint8_t iPacketsLeft(iLength / 4);
225 *bError = false;
226
227 int64_t iNow = GetTimeMs();
228 int64_t iTargetTime = iNow + (uint64_t) iTimeout;
229
230 while (!bTransmitSucceeded && !*bError && (iTimeout == 0 || iNow < iTargetTime))
231 {
232 CCECAdapterMessage msg;
233
234 if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000))
235 {
236 iNow = GetTimeMs();
237 continue;
238 }
239
240 switch(msg.message())
241 {
242 case MSGCODE_TIMEOUT_ERROR:
243 case MSGCODE_HIGH_ERROR:
244 case MSGCODE_LOW_ERROR:
245 {
246 CStdString logStr;
247 if (msg.message() == MSGCODE_TIMEOUT_ERROR)
248 logStr = "MSGCODE_TIMEOUT";
249 else if (msg.message() == MSGCODE_HIGH_ERROR)
250 logStr = "MSGCODE_HIGH_ERROR";
251 else
252 logStr = "MSGCODE_LOW_ERROR";
253
254 int iLine = (msg.size() >= 3) ? (msg[1] << 8) | (msg[2]) : 0;
255 uint32_t iTime = (msg.size() >= 7) ? (msg[3] << 24) | (msg[4] << 16) | (msg[5] << 8) | (msg[6]) : 0;
256 logStr.AppendFormat(" line:%i", iLine);
257 logStr.AppendFormat(" time:%u", iTime);
258 m_controller->AddLog(CEC_LOG_WARNING, logStr.c_str());
259 *bError = true;
260 }
261 break;
262 case MSGCODE_COMMAND_ACCEPTED:
263 m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_COMMAND_ACCEPTED");
264 iPacketsLeft--;
265 break;
266 case MSGCODE_TRANSMIT_SUCCEEDED:
267 m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_TRANSMIT_SUCCEEDED");
268 bTransmitSucceeded = (iPacketsLeft == 0);
269 *bError = !bTransmitSucceeded;
270 break;
271 case MSGCODE_RECEIVE_FAILED:
272 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_RECEIVE_FAILED");
273 *bError = true;
274 break;
275 case MSGCODE_COMMAND_REJECTED:
276 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_COMMAND_REJECTED");
277 *bError = true;
278 break;
279 case MSGCODE_TRANSMIT_FAILED_LINE:
280 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_LINE");
281 *bError = true;
282 break;
283 case MSGCODE_TRANSMIT_FAILED_ACK:
284 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_ACK");
285 *bError = true;
286 break;
287 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA:
288 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
289 *bError = true;
290 break;
291 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE:
292 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
293 *bError = true;
294 break;
295 default:
296 m_frameBuffer.Push(msg);
297 break;
298 }
299
300 iNow = GetTimeMs();
301 }
302
303 return bTransmitSucceeded && !*bError;
304 }
305
306 bool CCECProcessor::ParseMessage(CCECAdapterMessage &msg)
307 {
308 bool bEom = false;
309
310 if (msg.empty())
311 return bEom;
312
313 CStdString logStr;
314
315 switch(msg.message())
316 {
317 case MSGCODE_NOTHING:
318 m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_NOTHING");
319 break;
320 case MSGCODE_TIMEOUT_ERROR:
321 case MSGCODE_HIGH_ERROR:
322 case MSGCODE_LOW_ERROR:
323 {
324 if (msg.message() == MSGCODE_TIMEOUT_ERROR)
325 logStr = "MSGCODE_TIMEOUT";
326 else if (msg.message() == MSGCODE_HIGH_ERROR)
327 logStr = "MSGCODE_HIGH_ERROR";
328 else
329 logStr = "MSGCODE_LOW_ERROR";
330
331 int iLine = (msg.size() >= 3) ? (msg[1] << 8) | (msg[2]) : 0;
332 uint32_t iTime = (msg.size() >= 7) ? (msg[3] << 24) | (msg[4] << 16) | (msg[5] << 8) | (msg[6]) : 0;
333 logStr.AppendFormat(" line:%i", iLine);
334 logStr.AppendFormat(" time:%u", iTime);
335 m_controller->AddLog(CEC_LOG_WARNING, logStr.c_str());
336 }
337 break;
338 case MSGCODE_FRAME_START:
339 {
340 logStr = "MSGCODE_FRAME_START";
341 m_currentframe.clear();
342 if (msg.size() >= 2)
343 {
344 logStr.AppendFormat(" initiator:%u destination:%u ack:%s %s", msg.initiator(), msg.destination(), msg.ack() ? "high" : "low", msg.eom() ? "eom" : "");
345 m_currentframe.initiator = msg.initiator();
346 m_currentframe.destination = msg.destination();
347 m_currentframe.ack = msg.ack();
348 m_currentframe.eom = msg.eom();
349 }
350 m_controller->AddLog(CEC_LOG_DEBUG, logStr.c_str());
351 }
352 break;
353 case MSGCODE_FRAME_DATA:
354 {
355 logStr = "MSGCODE_FRAME_DATA";
356 if (msg.size() >= 2)
357 {
358 uint8_t iData = msg[1];
359 logStr.AppendFormat(" %02x", iData);
360 m_currentframe.push_back(iData);
361 m_currentframe.eom = msg.eom();
362 }
363 m_controller->AddLog(CEC_LOG_DEBUG, logStr.c_str());
364
365 bEom = msg.eom();
366 }
367 break;
368 case MSGCODE_COMMAND_ACCEPTED:
369 m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_COMMAND_ACCEPTED");
370 break;
371 case MSGCODE_TRANSMIT_SUCCEEDED:
372 m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_TRANSMIT_SUCCEEDED");
373 break;
374 case MSGCODE_RECEIVE_FAILED:
375 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_RECEIVE_FAILED");
376 break;
377 case MSGCODE_COMMAND_REJECTED:
378 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_COMMAND_REJECTED");
379 break;
380 case MSGCODE_TRANSMIT_FAILED_LINE:
381 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_LINE");
382 break;
383 case MSGCODE_TRANSMIT_FAILED_ACK:
384 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_ACK");
385 break;
386 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA:
387 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
388 break;
389 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE:
390 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
391 break;
392 default:
393 break;
394 }
395
396 return bEom;
397 }
398
399 void CCECProcessor::ParseCommand(cec_command &command)
400 {
401 CStdString dataStr;
402 dataStr.Format(">> %1x%1x:%02x", command.initiator, command.destination, command.opcode);
403 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
404 dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
405 m_controller->AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
406
407 if (!m_bMonitor)
408 m_busDevices[(uint8_t)command.initiator]->HandleCommand(command);
409 }
410
411 uint16_t CCECProcessor::GetPhysicalAddress(void) const
412 {
413 return m_busDevices[m_iLogicalAddress]->GetPhysicalAddress();
414 }
415
416 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode)
417 {
418 m_controller->SetCurrentButton(iButtonCode);
419 }
420
421 void CCECProcessor::AddCommand(const cec_command &command)
422 {
423 m_controller->AddCommand(command);
424 }
425
426 void CCECProcessor::AddKey(void)
427 {
428 m_controller->AddKey();
429 }
430
431 void CCECProcessor::AddLog(cec_log_level level, const CStdString &strMessage)
432 {
433 m_controller->AddLog(level, strMessage);
434 }