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