cec: added some guards
[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(CECDEVICE_UNKNOWN),
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 m_startCondition.Broadcast();
58 StopThread();
59 m_communication = NULL;
60 m_controller = NULL;
61 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
62 delete m_busDevices[iPtr];
63 }
64
65 bool CCECProcessor::Start(void)
66 {
67 CLockObject lock(&m_mutex);
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 {
82 if (!m_startCondition.Wait(&m_mutex))
83 {
84 m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
85 return false;
86 }
87 return true;
88 }
89 else
90 m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
91
92 return false;
93 }
94
95 void *CCECProcessor::Process(void)
96 {
97 {
98 CLockObject lock(&m_mutex);
99 m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
100 m_startCondition.Signal();
101 }
102
103 cec_command command;
104 CCECAdapterMessage msg;
105 CCECAdapterMessagePtr msgPtr;
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_frameBuffer.Pop(msgPtr))
118 bParseFrame = ParseMessage(msgPtr);
119 else if (m_communication->IsOpen() && m_communication->Read(msg, 50))
120 {
121 msgPtr = CCECAdapterMessagePtr(new CCECAdapterMessage(msg));
122 bParseFrame = ParseMessage(msgPtr);
123 }
124
125 bParseFrame &= !IsStopped();
126 if (bParseFrame)
127 command = m_currentframe;
128 }
129
130 if (bParseFrame)
131 ParseCommand(command);
132
133 m_controller->CheckKeypressTimeout();
134
135 for (unsigned int iDevicePtr = 0; iDevicePtr < 16; iDevicePtr++)
136 m_busDevices[iDevicePtr]->PollVendorId();
137
138 if (!IsStopped())
139 Sleep(5);
140 }
141
142 return NULL;
143 }
144
145 bool CCECProcessor::SetActiveView(void)
146 {
147 if (!IsRunning())
148 return false;
149
150 if (m_iLogicalAddress != CECDEVICE_UNKNOWN && m_busDevices[m_iLogicalAddress])
151 return m_busDevices[m_iLogicalAddress]->BroadcastActiveView();
152 return false;
153 }
154
155 bool CCECProcessor::SetInactiveView(void)
156 {
157 if (!IsRunning())
158 return false;
159
160 if (m_iLogicalAddress != CECDEVICE_UNKNOWN && m_busDevices[m_iLogicalAddress])
161 return m_busDevices[m_iLogicalAddress]->BroadcastInactiveView();
162 return false;
163 }
164
165 void CCECProcessor::LogOutput(const cec_command &data)
166 {
167 CStdString strTx;
168 strTx.Format("<< %02x:%02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination, (uint8_t)data.opcode);
169
170 for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++)
171 strTx.AppendFormat(":%02x", data.parameters[iPtr]);
172 m_controller->AddLog(CEC_LOG_TRAFFIC, strTx.c_str());
173 }
174
175 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress)
176 {
177 if (m_iLogicalAddress != iLogicalAddress)
178 {
179 CStdString strLog;
180 strLog.Format("<< setting logical address to %1x", iLogicalAddress);
181 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
182
183 m_iLogicalAddress = iLogicalAddress;
184 return m_communication && m_communication->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
185 }
186
187 return true;
188 }
189
190 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress)
191 {
192 if (m_iLogicalAddress != CECDEVICE_UNKNOWN && m_busDevices[m_iLogicalAddress])
193 {
194 m_busDevices[m_iLogicalAddress]->SetPhysicalAddress(iPhysicalAddress);
195 return m_busDevices[m_iLogicalAddress]->BroadcastActiveView();
196 }
197 return false;
198 }
199
200 bool CCECProcessor::SwitchMonitoring(bool bEnable)
201 {
202 CStdString strLog;
203 strLog.Format("== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
204 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
205
206 m_bMonitor = bEnable;
207 if (bEnable)
208 return m_communication && m_communication->SetAckMask(0);
209 else
210 return m_communication && m_communication->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
211 }
212
213 cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress)
214 {
215 return m_busDevices[iAddress]->GetCecVersion();
216 }
217
218 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
219 {
220 if (m_busDevices[iAddress])
221 {
222 *language = m_busDevices[iAddress]->GetMenuLanguage();
223 return (strcmp(language->language, "???") == 0);
224 }
225 return false;
226 }
227
228 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress)
229 {
230 if (m_busDevices[iAddress])
231 return m_busDevices[iAddress]->GetVendorId();
232 return false;
233 }
234
235 cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress)
236 {
237 if (m_busDevices[iAddress])
238 return m_busDevices[iAddress]->GetPowerStatus();
239 return CEC_POWER_STATUS_UNKNOWN;
240 }
241
242 bool CCECProcessor::Transmit(const cec_command &data)
243 {
244 bool bReturn(false);
245 LogOutput(data);
246
247 CCECAdapterMessagePtr output(new CCECAdapterMessage(data));
248
249 CLockObject lock(&m_mutex);
250 {
251 CLockObject msgLock(&output->mutex);
252 if (!m_communication || !m_communication->Write(output))
253 return bReturn;
254 else
255 {
256 output->condition.Wait(&output->mutex);
257 if (output->state != ADAPTER_MESSAGE_STATE_SENT)
258 {
259 m_controller->AddLog(CEC_LOG_ERROR, "command was not sent");
260 return bReturn;
261 }
262 }
263
264 if (data.ack_timeout > 0)
265 {
266 bool bError(false);
267 if ((bReturn = WaitForAck(&bError, output->size(), data.ack_timeout)) == false)
268 m_controller->AddLog(CEC_LOG_ERROR, "did not receive ack");
269 }
270 else
271 {
272 bReturn = true;
273 }
274 }
275
276 return bReturn;
277 }
278
279 void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, ECecAbortReason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
280 {
281 m_controller->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
282
283 cec_command command;
284 cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_FEATURE_ABORT);
285 command.parameters.push_back((uint8_t)opcode);
286 command.parameters.push_back((uint8_t)reason);
287
288 Transmit(command);
289 }
290
291 bool CCECProcessor::WaitForAck(bool *bError, uint8_t iLength, uint32_t iTimeout /* = 1000 */)
292 {
293 bool bTransmitSucceeded = false;
294 uint8_t iPacketsLeft(iLength / 4);
295 *bError = false;
296
297 int64_t iNow = GetTimeMs();
298 int64_t iTargetTime = iNow + (uint64_t) iTimeout;
299
300 while (!bTransmitSucceeded && !*bError && (iTimeout == 0 || iNow < iTargetTime))
301 {
302 CCECAdapterMessage msg;
303
304 if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000))
305 {
306 iNow = GetTimeMs();
307 continue;
308 }
309
310 switch(msg.message())
311 {
312 case MSGCODE_TIMEOUT_ERROR:
313 case MSGCODE_HIGH_ERROR:
314 case MSGCODE_LOW_ERROR:
315 {
316 CStdString logStr;
317 if (msg.message() == MSGCODE_TIMEOUT_ERROR)
318 logStr = "MSGCODE_TIMEOUT";
319 else if (msg.message() == MSGCODE_HIGH_ERROR)
320 logStr = "MSGCODE_HIGH_ERROR";
321 else
322 logStr = "MSGCODE_LOW_ERROR";
323
324 int iLine = (msg.size() >= 3) ? (msg[1] << 8) | (msg[2]) : 0;
325 uint32_t iTime = (msg.size() >= 7) ? (msg[3] << 24) | (msg[4] << 16) | (msg[5] << 8) | (msg[6]) : 0;
326 logStr.AppendFormat(" line:%i", iLine);
327 logStr.AppendFormat(" time:%u", iTime);
328 m_controller->AddLog(CEC_LOG_WARNING, logStr.c_str());
329 *bError = true;
330 }
331 break;
332 case MSGCODE_COMMAND_ACCEPTED:
333 m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_COMMAND_ACCEPTED");
334 iPacketsLeft--;
335 break;
336 case MSGCODE_TRANSMIT_SUCCEEDED:
337 m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_TRANSMIT_SUCCEEDED");
338 bTransmitSucceeded = (iPacketsLeft == 0);
339 *bError = !bTransmitSucceeded;
340 break;
341 case MSGCODE_RECEIVE_FAILED:
342 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_RECEIVE_FAILED");
343 *bError = true;
344 break;
345 case MSGCODE_COMMAND_REJECTED:
346 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_COMMAND_REJECTED");
347 *bError = true;
348 break;
349 case MSGCODE_TRANSMIT_FAILED_LINE:
350 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_LINE");
351 *bError = true;
352 break;
353 case MSGCODE_TRANSMIT_FAILED_ACK:
354 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_ACK");
355 *bError = true;
356 break;
357 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA:
358 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
359 *bError = true;
360 break;
361 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE:
362 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
363 *bError = true;
364 break;
365 default:
366 CCECAdapterMessagePtr msgPtr = CCECAdapterMessagePtr(new CCECAdapterMessage(msg));
367 m_frameBuffer.Push(msgPtr);
368 break;
369 }
370
371 iNow = GetTimeMs();
372 }
373
374 return bTransmitSucceeded && !*bError;
375 }
376
377 bool CCECProcessor::ParseMessage(CCECAdapterMessagePtr 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->at(1) << 8) | (msg->at(2)) : 0;
403 uint32_t iTime = (msg->size() >= 7) ? (msg->at(3) << 24) | (msg->at(4) << 16) | (msg->at(5) << 8) | (msg->at(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:%u destination:%u 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->at(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 }