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