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