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