0749e5375f00c0362d1dc9a93127f49b8cfe4976
[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 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 (CreateThread())
75 {
76 if (!m_startCondition.Wait(&m_mutex))
77 {
78 m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
79 return false;
80 }
81 return true;
82 }
83 else
84 m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
85
86 return false;
87 }
88
89 void *CCECProcessor::Process(void)
90 {
91 cec_command command;
92 CCECAdapterMessage msg;
93
94 SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
95
96 {
97 CLockObject lock(&m_mutex);
98 m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
99 m_startCondition.Signal();
100 }
101
102 while (!IsStopped())
103 {
104 bool bParseFrame(false);
105 command.clear();
106 msg.clear();
107
108 {
109 CLockObject lock(&m_mutex);
110 if (m_communication->IsOpen() && m_communication->Read(msg, 50))
111 {
112 m_controller->AddLog(msg.is_error() ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
113 bParseFrame = ParseMessage(msg) && !IsStopped();
114 }
115
116 if (bParseFrame)
117 command = m_currentframe;
118 }
119
120 if (bParseFrame)
121 ParseCommand(command);
122
123 Sleep(5);
124
125 m_controller->CheckKeypressTimeout();
126
127 for (unsigned int iDevicePtr = 0; iDevicePtr < 16; iDevicePtr++)
128 m_busDevices[iDevicePtr]->PollVendorId();
129
130 Sleep(5);
131 }
132
133 return NULL;
134 }
135
136 bool CCECProcessor::SetActiveView(void)
137 {
138 if (!IsRunning())
139 return false;
140
141 if (m_iLogicalAddress != CECDEVICE_UNKNOWN && m_busDevices[m_iLogicalAddress])
142 return m_busDevices[m_iLogicalAddress]->BroadcastActiveView();
143 return false;
144 }
145
146 bool CCECProcessor::SetInactiveView(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]->BroadcastInactiveView();
153 return false;
154 }
155
156 void CCECProcessor::LogOutput(const cec_command &data)
157 {
158 CStdString strTx;
159 strTx.Format("<< %02x:%02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination, (uint8_t)data.opcode);
160
161 for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++)
162 strTx.AppendFormat(":%02x", data.parameters[iPtr]);
163 m_controller->AddLog(CEC_LOG_TRAFFIC, strTx.c_str());
164 }
165
166 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress)
167 {
168 if (m_iLogicalAddress != iLogicalAddress)
169 {
170 CStdString strLog;
171 strLog.Format("<< setting logical address to %1x", iLogicalAddress);
172 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
173 m_iLogicalAddress = iLogicalAddress;
174 return SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
175 }
176
177 return true;
178 }
179
180 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress)
181 {
182 if (m_iLogicalAddress != CECDEVICE_UNKNOWN && m_busDevices[m_iLogicalAddress])
183 {
184 m_busDevices[m_iLogicalAddress]->SetPhysicalAddress(iPhysicalAddress);
185 return m_busDevices[m_iLogicalAddress]->BroadcastActiveView();
186 }
187 return false;
188 }
189
190 bool CCECProcessor::SwitchMonitoring(bool bEnable)
191 {
192 CStdString strLog;
193 strLog.Format("== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
194 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
195
196 m_bMonitor = bEnable;
197 if (bEnable)
198 return SetAckMask(0);
199 else
200 return SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
201 }
202
203 cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress)
204 {
205 return m_busDevices[iAddress]->GetCecVersion();
206 }
207
208 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
209 {
210 if (m_busDevices[iAddress])
211 {
212 *language = m_busDevices[iAddress]->GetMenuLanguage();
213 return (strcmp(language->language, "???") != 0);
214 }
215 return false;
216 }
217
218 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress)
219 {
220 if (m_busDevices[iAddress])
221 return m_busDevices[iAddress]->GetVendorId();
222 return false;
223 }
224
225 cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress)
226 {
227 if (m_busDevices[iAddress])
228 return m_busDevices[iAddress]->GetPowerStatus();
229 return CEC_POWER_STATUS_UNKNOWN;
230 }
231
232 bool CCECProcessor::Transmit(const cec_command &data)
233 {
234 LogOutput(data);
235
236 CCECAdapterMessagePtr output(new CCECAdapterMessage(data));
237 return Transmit(output);
238 }
239
240 bool CCECProcessor::Transmit(CCECAdapterMessagePtr output)
241 {
242 bool bReturn(false);
243 CLockObject lock(&m_mutex);
244 {
245 CLockObject msgLock(&output->mutex);
246 if (!m_communication || !m_communication->Write(output))
247 return bReturn;
248 else
249 {
250 output->condition.Wait(&output->mutex, 1000);
251 if (output->state != ADAPTER_MESSAGE_STATE_SENT)
252 {
253 m_controller->AddLog(CEC_LOG_ERROR, "command was not sent");
254 return bReturn;
255 }
256 }
257
258 if (output->transmit_timeout > 0)
259 {
260 if ((bReturn = WaitForTransmitSucceeded(output->size(), output->transmit_timeout)) == false)
261 m_controller->AddLog(CEC_LOG_ERROR, "did not receive ack");
262 }
263 else
264 bReturn = true;
265 }
266
267 return bReturn;
268 }
269
270 void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, ECecAbortReason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
271 {
272 m_controller->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
273
274 cec_command command;
275 cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_FEATURE_ABORT);
276 command.parameters.push_back((uint8_t)opcode);
277 command.parameters.push_back((uint8_t)reason);
278
279 Transmit(command);
280 }
281
282 bool CCECProcessor::WaitForTransmitSucceeded(uint8_t iLength, uint32_t iTimeout /* = 1000 */)
283 {
284 bool bError(false);
285 bool bTransmitSucceeded(false);
286 uint8_t iPacketsLeft(iLength / 4);
287
288 int64_t iNow = GetTimeMs();
289 int64_t iTargetTime = iNow + (uint64_t) iTimeout;
290
291 while (!bTransmitSucceeded && !bError && (iTimeout == 0 || iNow < iTargetTime))
292 {
293 CCECAdapterMessage msg;
294
295 if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000))
296 {
297 iNow = GetTimeMs();
298 continue;
299 }
300
301 if ((bError = msg.is_error()) == false)
302 {
303 m_controller->AddLog(bError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
304
305 switch(msg.message())
306 {
307 case MSGCODE_COMMAND_ACCEPTED:
308 if (iPacketsLeft > 0)
309 iPacketsLeft--;
310 break;
311 case MSGCODE_TRANSMIT_SUCCEEDED:
312 bTransmitSucceeded = (iPacketsLeft == 0);
313 bError = !bTransmitSucceeded;
314 break;
315 default:
316 ParseMessage(msg);
317 }
318
319 iNow = GetTimeMs();
320 }
321 }
322
323 return bTransmitSucceeded && !bError;
324 }
325
326 bool CCECProcessor::ParseMessage(const CCECAdapterMessage &msg)
327 {
328 bool bEom = false;
329
330 if (msg.empty())
331 return bEom;
332
333 switch(msg.message())
334 {
335 case MSGCODE_FRAME_START:
336 {
337 m_currentframe.clear();
338 if (msg.size() >= 2)
339 {
340 m_currentframe.initiator = msg.initiator();
341 m_currentframe.destination = msg.destination();
342 m_currentframe.ack = msg.ack();
343 m_currentframe.eom = msg.eom();
344 }
345 }
346 break;
347 case MSGCODE_FRAME_DATA:
348 {
349 if (msg.size() >= 2)
350 {
351 m_currentframe.push_back(msg[1]);
352 m_currentframe.eom = msg.eom();
353 }
354 bEom = msg.eom();
355 }
356 break;
357 default:
358 break;
359 }
360
361 return bEom;
362 }
363
364 void CCECProcessor::ParseCommand(cec_command &command)
365 {
366 CStdString dataStr;
367 dataStr.Format(">> %1x%1x:%02x", command.initiator, command.destination, command.opcode);
368 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
369 dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
370 m_controller->AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
371
372 if (!m_bMonitor)
373 m_busDevices[(uint8_t)command.initiator]->HandleCommand(command);
374 }
375
376 uint16_t CCECProcessor::GetPhysicalAddress(void) const
377 {
378 if (m_iLogicalAddress != CECDEVICE_UNKNOWN && m_busDevices[m_iLogicalAddress])
379 return m_busDevices[m_iLogicalAddress]->GetPhysicalAddress();
380 return false;
381 }
382
383 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode)
384 {
385 m_controller->SetCurrentButton(iButtonCode);
386 }
387
388 void CCECProcessor::AddCommand(const cec_command &command)
389 {
390 m_controller->AddCommand(command);
391 }
392
393 void CCECProcessor::AddKey(void)
394 {
395 m_controller->AddKey();
396 }
397
398 void CCECProcessor::AddLog(cec_log_level level, const CStdString &strMessage)
399 {
400 m_controller->AddLog(level, strMessage);
401 }
402
403 bool CCECProcessor::SetAckMask(uint16_t iMask)
404 {
405 CStdString strLog;
406 strLog.Format("setting ackmask to %2x", iMask);
407 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
408
409 CCECAdapterMessagePtr output(new CCECAdapterMessage);
410
411 output->push_back(MSGSTART);
412 output->push_escaped(MSGCODE_SET_ACK_MASK);
413 output->push_escaped(iMask >> 8);
414 output->push_escaped((uint8_t)iMask);
415 output->push_back(MSGEND);
416
417 if (!Transmit(output))
418 {
419 m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask");
420 return false;
421 }
422
423 return true;
424 }