cec: show the command as string instead of int when an unexpected command was received.
[deb_libcec.git] / src / lib / CECProcessor.cpp
... / ...
CommitLineData
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
41using namespace CEC;
42using namespace std;
43
44CCECProcessor::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
56CCECProcessor::~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
66bool 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
96void *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
139bool 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
149bool 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
159void 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
169bool 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
186bool 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
196bool 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
209cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress)
210{
211 return m_busDevices[iAddress]->GetCecVersion();
212}
213
214bool 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
224uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress)
225{
226 if (m_busDevices[iAddress])
227 return m_busDevices[iAddress]->GetVendorId();
228 return false;
229}
230
231cec_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
238bool 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.transmit_timeout > 0)
263 {
264 if ((bReturn = WaitForTransmitSucceeded(output->size(), data.transmit_timeout)) == false)
265 m_controller->AddLog(CEC_LOG_ERROR, "did not receive ack");
266 }
267 else
268 bReturn = true;
269 }
270
271 return bReturn;
272}
273
274void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, ECecAbortReason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
275{
276 m_controller->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
277
278 cec_command command;
279 cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_FEATURE_ABORT);
280 command.parameters.push_back((uint8_t)opcode);
281 command.parameters.push_back((uint8_t)reason);
282
283 Transmit(command);
284}
285
286bool CCECProcessor::WaitForTransmitSucceeded(uint8_t iLength, uint32_t iTimeout /* = 1000 */)
287{
288 bool bError(false);
289 bool bTransmitSucceeded(false);
290 uint8_t iPacketsLeft(iLength / 4);
291
292 int64_t iNow = GetTimeMs();
293 int64_t iTargetTime = iNow + (uint64_t) iTimeout;
294
295 while (!bTransmitSucceeded && !bError && (iTimeout == 0 || iNow < iTargetTime))
296 {
297 CCECAdapterMessage msg;
298
299 if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000))
300 {
301 iNow = GetTimeMs();
302 continue;
303 }
304
305 bError = msg.is_error();
306 m_controller->AddLog(msg.is_error() ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
307
308 switch(msg.message())
309 {
310 case MSGCODE_COMMAND_ACCEPTED:
311 iPacketsLeft--;
312 break;
313 case MSGCODE_TRANSMIT_SUCCEEDED:
314 bTransmitSucceeded = (iPacketsLeft == 0);
315 bError = !bTransmitSucceeded;
316 break;
317 default:
318 CStdString strLog;
319 strLog.Format("received unexpected reply '%s' instead of ack", msg.MessageCodeAsString().c_str());
320 m_controller->AddLog(CEC_LOG_WARNING, strLog);
321 bError = true;
322 break;
323 }
324
325 iNow = GetTimeMs();
326 }
327
328 return bTransmitSucceeded && !bError;
329}
330
331bool CCECProcessor::ParseMessage(const CCECAdapterMessage &msg)
332{
333 bool bEom = false;
334
335 if (msg.empty())
336 return bEom;
337
338 m_controller->AddLog(msg.is_error() ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
339
340 switch(msg.message())
341 {
342 case MSGCODE_FRAME_START:
343 {
344 m_currentframe.clear();
345 if (msg.size() >= 2)
346 {
347 m_currentframe.initiator = msg.initiator();
348 m_currentframe.destination = msg.destination();
349 m_currentframe.ack = msg.ack();
350 m_currentframe.eom = msg.eom();
351 }
352 }
353 break;
354 case MSGCODE_FRAME_DATA:
355 {
356 if (msg.size() >= 2)
357 {
358 m_currentframe.push_back(msg[1]);
359 m_currentframe.eom = msg.eom();
360 }
361 bEom = msg.eom();
362 }
363 break;
364 default:
365 break;
366 }
367
368 return bEom;
369}
370
371void CCECProcessor::ParseCommand(cec_command &command)
372{
373 CStdString dataStr;
374 dataStr.Format(">> %1x%1x:%02x", command.initiator, command.destination, command.opcode);
375 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
376 dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
377 m_controller->AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
378
379 if (!m_bMonitor)
380 m_busDevices[(uint8_t)command.initiator]->HandleCommand(command);
381}
382
383uint16_t CCECProcessor::GetPhysicalAddress(void) const
384{
385 if (m_iLogicalAddress != CECDEVICE_UNKNOWN && m_busDevices[m_iLogicalAddress])
386 return m_busDevices[m_iLogicalAddress]->GetPhysicalAddress();
387 return false;
388}
389
390void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode)
391{
392 m_controller->SetCurrentButton(iButtonCode);
393}
394
395void CCECProcessor::AddCommand(const cec_command &command)
396{
397 m_controller->AddCommand(command);
398}
399
400void CCECProcessor::AddKey(void)
401{
402 m_controller->AddKey();
403}
404
405void CCECProcessor::AddLog(cec_log_level level, const CStdString &strMessage)
406{
407 m_controller->AddLog(level, strMessage);
408}