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