cec: moved device specific logic from CCECProcessor to CCECBusDevice
[deb_libcec.git] / src / lib / CECProcessor.cpp
CommitLineData
abbca718
LOK
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
2abe74eb 33#include "CECProcessor.h"
abbca718 34
2abe74eb 35#include "AdapterCommunication.h"
eafa9d46 36#include "devices/CECBusDevice.h"
2abe74eb 37#include "LibCEC.h"
abbca718 38#include "util/StdString.h"
b9187cc6 39#include "platform/timeutils.h"
abbca718
LOK
40
41using namespace CEC;
42using namespace std;
43
2b4d8297 44CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm, const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) :
df7339c6 45 m_iLogicalAddress(iLogicalAddress),
2abe74eb
LOK
46 m_strDeviceName(strDeviceName),
47 m_communication(serComm),
8b7e5ff6
LOK
48 m_controller(controller),
49 m_bMonitor(false)
abbca718 50{
e9de9629
LOK
51 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
52 m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, 0);
0f23c85c
LOK
53
54 m_busDevices[m_iLogicalAddress]->SetPhysicalAddress(iPhysicalAddress);
abbca718
LOK
55}
56
2abe74eb 57CCECProcessor::~CCECProcessor(void)
abbca718 58{
2abe74eb
LOK
59 StopThread();
60 m_communication = NULL;
61 m_controller = NULL;
d1998c7b
LOK
62 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
63 delete m_busDevices[iPtr];
abbca718
LOK
64}
65
2abe74eb 66bool CCECProcessor::Start(void)
abbca718 67{
2abe74eb 68 if (!m_communication || !m_communication->IsOpen())
13fd6a66
LOK
69 {
70 m_controller->AddLog(CEC_LOG_ERROR, "connection is closed");
a8f0bd18 71 return false;
13fd6a66 72 }
abbca718 73
a8f0bd18 74 if (!SetLogicalAddress(m_iLogicalAddress))
abbca718 75 {
2abe74eb 76 m_controller->AddLog(CEC_LOG_ERROR, "could not set the logical address");
a8f0bd18 77 return false;
abbca718
LOK
78 }
79
60fa4578 80 if (CreateThread())
a8f0bd18 81 return true;
a8f0bd18 82 else
2abe74eb 83 m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
abbca718 84
a8f0bd18 85 return false;
abbca718
LOK
86}
87
2abe74eb 88void *CCECProcessor::Process(void)
f99bc831 89{
2abe74eb 90 m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
abbca718 91
9dee1670
LOK
92 cec_command command;
93 cec_adapter_message msg;
94
13fd6a66 95 while (!IsStopped())
abbca718 96 {
60fa4578 97 bool bParseFrame(false);
78e8010a
LOK
98 bool bError(false);
99 bool bTransmitSucceeded(false);
9dee1670 100 command.clear();
76321de4
LOK
101 msg.clear();
102
60fa4578
LOK
103 {
104 CLockObject lock(&m_mutex);
76321de4 105 if (m_communication->IsOpen() && m_communication->Read(msg, 50))
78e8010a 106 ParseMessage(msg, &bError, &bTransmitSucceeded, &bParseFrame);
76321de4 107
78e8010a 108 bParseFrame &= !IsStopped();
76321de4 109 if (bParseFrame)
9dee1670 110 command = m_currentframe;
60fa4578
LOK
111 }
112
13fd6a66 113 if (bParseFrame)
9dee1670 114 ParseCommand(command);
abbca718 115
13fd6a66
LOK
116 m_controller->CheckKeypressTimeout();
117
0ab58650
LOK
118 for (unsigned int iDevicePtr = 0; iDevicePtr < 16; iDevicePtr++)
119 m_busDevices[iDevicePtr]->PollVendorId();
120
13fd6a66 121 if (!IsStopped())
d5bffd3c 122 Sleep(5);
abbca718
LOK
123 }
124
60fa4578 125 return NULL;
abbca718
LOK
126}
127
2abe74eb 128bool CCECProcessor::SetActiveView(void)
abbca718 129{
60fa4578 130 if (!IsRunning())
f99bc831
LOK
131 return false;
132
0f23c85c 133 return m_busDevices[m_iLogicalAddress]->BroadcastActiveView();
abbca718
LOK
134}
135
2abe74eb 136bool CCECProcessor::SetInactiveView(void)
abbca718 137{
60fa4578 138 if (!IsRunning())
f99bc831
LOK
139 return false;
140
0f23c85c 141 return m_busDevices[m_iLogicalAddress]->BroadcastInactiveView();
abbca718
LOK
142}
143
9dee1670 144void CCECProcessor::LogOutput(const cec_command &data)
abbca718 145{
1d3ca3de
LOK
146 CStdString strTx;
147 strTx.Format("<< %02x:%02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination, (uint8_t)data.opcode);
9dee1670 148
06a1f7ce 149 for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++)
1d3ca3de
LOK
150 strTx.AppendFormat(":%02x", data.parameters[iPtr]);
151 m_controller->AddLog(CEC_LOG_TRAFFIC, strTx.c_str());
9dee1670 152}
2abe74eb 153
9dee1670
LOK
154bool CCECProcessor::Transmit(const cec_command &data, bool bWaitForAck /* = true */)
155{
156 LogOutput(data);
2abe74eb 157
9dee1670 158 cec_adapter_message output;
25701fa6 159 output.clear();
9dee1670 160 CAdapterCommunication::FormatAdapterMessage(data, output);
2abe74eb
LOK
161
162 return TransmitFormatted(output, bWaitForAck);
abbca718
LOK
163}
164
2abe74eb 165bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress)
abbca718 166{
2abe74eb 167 CStdString strLog;
2492216a 168 strLog.Format("<< setting logical address to %1x", iLogicalAddress);
2abe74eb
LOK
169 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
170
171 m_iLogicalAddress = iLogicalAddress;
172 return m_communication && m_communication->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
abbca718 173}
825ddb96 174
2492216a
LOK
175bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress)
176{
0f23c85c
LOK
177 m_busDevices[m_iLogicalAddress]->SetPhysicalAddress(iPhysicalAddress);
178 return m_busDevices[m_iLogicalAddress]->BroadcastActiveView();
1969b140
LOK
179}
180
8b7e5ff6
LOK
181bool CCECProcessor::SwitchMonitoring(bool bEnable)
182{
183 CStdString strLog;
184 strLog.Format("== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
185 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
186
187 m_bMonitor = bEnable;
188 if (bEnable)
189 return m_communication && m_communication->SetAckMask(0);
190 else
191 return m_communication && m_communication->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
192}
193
9dee1670 194bool CCECProcessor::TransmitFormatted(const cec_adapter_message &data, bool bWaitForAck /* = true */)
825ddb96 195{
2abe74eb
LOK
196 CLockObject lock(&m_mutex);
197 if (!m_communication || !m_communication->Write(data))
198 return false;
199
050df0f9 200 if (bWaitForAck)
2abe74eb 201 {
050df0f9
LOK
202 uint64_t now = GetTimeMs();
203 uint64_t target = now + 1000;
204 bool bError(false);
205 bool bGotAck(false);
206
207 while (!bGotAck && now < target)
208 {
06a1f7ce 209 bGotAck = WaitForAck(&bError, (uint32_t) (target - now));
050df0f9
LOK
210 now = GetTimeMs();
211
212 if (bError && now < target)
213 {
214 m_controller->AddLog(CEC_LOG_ERROR, "retransmitting previous frame");
215 if (!m_communication->Write(data))
216 return false;
217 }
218 }
2abe74eb
LOK
219 }
220
221 return true;
825ddb96 222}
abbca718 223
2abe74eb 224void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, ECecAbortReason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
abbca718 225{
9dee1670
LOK
226 m_controller->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
227
06a1f7ce
LOK
228 cec_command command;
229 cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_FEATURE_ABORT);
9dee1670
LOK
230 command.parameters.push_back((uint8_t)opcode);
231 command.parameters.push_back((uint8_t)reason);
232
233 Transmit(command);
abbca718
LOK
234}
235
050df0f9 236bool CCECProcessor::WaitForAck(bool *bError, uint32_t iTimeout /* = 1000 */)
abbca718 237{
78e8010a 238 bool bTransmitSucceeded = false, bEom = false;
050df0f9 239 *bError = false;
abbca718
LOK
240
241 int64_t iNow = GetTimeMs();
25701fa6 242 int64_t iTargetTime = iNow + (uint64_t) iTimeout;
abbca718 243
78e8010a 244 while (!bTransmitSucceeded && !*bError && (iTimeout == 0 || iNow < iTargetTime))
abbca718 245 {
9dee1670 246 cec_adapter_message msg;
25701fa6
LOK
247 msg.clear();
248
06a1f7ce 249 if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000))
abbca718 250 {
abbca718 251 iNow = GetTimeMs();
5d38b0b6
LOK
252 continue;
253 }
254
78e8010a 255 ParseMessage(msg, bError, &bTransmitSucceeded, &bEom, false);
5d38b0b6 256 iNow = GetTimeMs();
abbca718
LOK
257 }
258
78e8010a 259 return bTransmitSucceeded && !*bError;
abbca718
LOK
260}
261
78e8010a 262void CCECProcessor::ParseMessage(cec_adapter_message &msg, bool *bError, bool *bTransmitSucceeded, bool *bEom, bool bProcessMessages /* = true */)
abbca718 263{
78e8010a
LOK
264 *bError = false;
265 *bTransmitSucceeded = false;
266 *bEom = false;
60fa4578 267
a6b6469c 268 if (msg.empty())
78e8010a 269 return;
abbca718
LOK
270
271 CStdString logStr;
abbca718 272
a6b6469c 273 switch(msg.message())
abbca718
LOK
274 {
275 case MSGCODE_NOTHING:
2abe74eb 276 m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_NOTHING");
abbca718
LOK
277 break;
278 case MSGCODE_TIMEOUT_ERROR:
279 case MSGCODE_HIGH_ERROR:
280 case MSGCODE_LOW_ERROR:
281 {
a6b6469c 282 if (msg.message() == MSGCODE_TIMEOUT_ERROR)
abbca718 283 logStr = "MSGCODE_TIMEOUT";
a6b6469c 284 else if (msg.message() == MSGCODE_HIGH_ERROR)
abbca718
LOK
285 logStr = "MSGCODE_HIGH_ERROR";
286 else
287 logStr = "MSGCODE_LOW_ERROR";
288
a6b6469c
LOK
289 int iLine = (msg.size() >= 3) ? (msg[1] << 8) | (msg[2]) : 0;
290 uint32_t iTime = (msg.size() >= 7) ? (msg[3] << 24) | (msg[4] << 16) | (msg[5] << 8) | (msg[6]) : 0;
abbca718
LOK
291 logStr.AppendFormat(" line:%i", iLine);
292 logStr.AppendFormat(" time:%u", iTime);
2abe74eb 293 m_controller->AddLog(CEC_LOG_WARNING, logStr.c_str());
78e8010a 294 *bError = true;
abbca718
LOK
295 }
296 break;
297 case MSGCODE_FRAME_START:
298 {
78e8010a 299 if (bProcessMessages)
abbca718 300 {
78e8010a
LOK
301 logStr = "MSGCODE_FRAME_START";
302 m_currentframe.clear();
303 if (msg.size() >= 2)
304 {
305 logStr.AppendFormat(" initiator:%u destination:%u ack:%s %s", msg.initiator(), msg.destination(), msg.ack() ? "high" : "low", msg.eom() ? "eom" : "");
306 m_currentframe.initiator = msg.initiator();
307 m_currentframe.destination = msg.destination();
308 m_currentframe.ack = msg.ack();
309 m_currentframe.eom = msg.eom();
310 }
311 m_controller->AddLog(CEC_LOG_DEBUG, logStr.c_str());
312 }
313 else
314 {
315 m_frameBuffer.Push(msg);
abbca718 316 }
abbca718
LOK
317 }
318 break;
319 case MSGCODE_FRAME_DATA:
320 {
78e8010a
LOK
321 if (bProcessMessages)
322 {
323 logStr = "MSGCODE_FRAME_DATA";
324 if (msg.size() >= 2)
325 {
326 uint8_t iData = msg[1];
327 logStr.AppendFormat(" %02x", iData);
328 m_currentframe.push_back(iData);
329 m_currentframe.eom = msg.eom();
330 }
331 m_controller->AddLog(CEC_LOG_DEBUG, logStr.c_str());
332 }
333 else
abbca718 334 {
78e8010a 335 m_frameBuffer.Push(msg);
abbca718 336 }
78e8010a
LOK
337
338 *bEom = msg.eom();
abbca718 339 }
78e8010a
LOK
340 break;
341 case MSGCODE_COMMAND_ACCEPTED:
342 m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_COMMAND_ACCEPTED");
343 break;
344 case MSGCODE_TRANSMIT_SUCCEEDED:
345 m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_TRANSMIT_SUCCEEDED");
346 *bTransmitSucceeded = true;
347 break;
348 case MSGCODE_RECEIVE_FAILED:
349 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_RECEIVE_FAILED");
350 *bError = true;
351 break;
352 case MSGCODE_COMMAND_REJECTED:
353 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_COMMAND_REJECTED");
354 *bError = true;
355 break;
356 case MSGCODE_TRANSMIT_FAILED_LINE:
357 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_LINE");
358 *bError = true;
359 break;
360 case MSGCODE_TRANSMIT_FAILED_ACK:
361 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_ACK");
362 *bError = true;
363 break;
364 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA:
365 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
366 *bError = true;
367 break;
368 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE:
369 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
370 *bError = true;
abbca718
LOK
371 break;
372 default:
373 break;
374 }
375}
376
e9de9629 377void CCECProcessor::ParseCommand(cec_command &command)
abbca718 378{
e9de9629 379 CStdString dataStr;
1d3ca3de 380 dataStr.Format(">> %1x%1x:%02x", command.initiator, command.destination, command.opcode);
e9de9629
LOK
381 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
382 dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
1d3ca3de 383 m_controller->AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
dc113aca 384
e9de9629
LOK
385 if (!m_bMonitor)
386 m_busDevices[(uint8_t)command.initiator]->HandleCommand(command);
dc113aca
LOK
387}
388
0f23c85c
LOK
389uint16_t CCECProcessor::GetPhysicalAddress(void) const
390{
391 return m_busDevices[m_iLogicalAddress]->GetPhysicalAddress();
392}
393
e9de9629 394void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode)
dc113aca 395{
e9de9629 396 m_controller->SetCurrentButton(iButtonCode);
dc113aca
LOK
397}
398
e9de9629 399void CCECProcessor::AddCommand(const cec_command &command)
dc113aca 400{
e9de9629 401 m_controller->AddCommand(command);
dc113aca
LOK
402}
403
e9de9629 404void CCECProcessor::AddKey(void)
dc113aca 405{
e9de9629 406 m_controller->AddKey();
abbca718 407}
acec5f48 408
e9de9629 409void CCECProcessor::AddLog(cec_log_level level, const CStdString &strMessage)
acec5f48 410{
e9de9629 411 m_controller->AddLog(level, strMessage);
acec5f48 412}