cec: renamed WaitForAck() to WaitForTransmitSucceeded(), because it waits for the...
[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*/) :
179efe86 45 m_iLogicalAddress(iLogicalAddress),
2abe74eb
LOK
46 m_strDeviceName(strDeviceName),
47 m_communication(serComm),
8b7e5ff6 48 m_controller(controller),
179efe86
LOK
49 m_bMonitor(false),
50 m_bLogicalAddressSet(false)
abbca718 51{
7ea0d558 52 for (int iPtr = 0; iPtr < 16; iPtr++)
09c10b66 53 m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, iPtr == iLogicalAddress ? iPhysicalAddress : 0);
abbca718
LOK
54}
55
2abe74eb 56CCECProcessor::~CCECProcessor(void)
abbca718 57{
8b0ee2cc 58 m_startCondition.Broadcast();
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{
8b0ee2cc 68 CLockObject lock(&m_mutex);
2abe74eb 69 if (!m_communication || !m_communication->IsOpen())
13fd6a66
LOK
70 {
71 m_controller->AddLog(CEC_LOG_ERROR, "connection is closed");
a8f0bd18 72 return false;
13fd6a66 73 }
abbca718 74
a8f0bd18 75 if (!SetLogicalAddress(m_iLogicalAddress))
abbca718 76 {
2abe74eb 77 m_controller->AddLog(CEC_LOG_ERROR, "could not set the logical address");
a8f0bd18 78 return false;
abbca718
LOK
79 }
80
60fa4578 81 if (CreateThread())
8b0ee2cc
LOK
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 }
a8f0bd18 88 return true;
8b0ee2cc 89 }
a8f0bd18 90 else
2abe74eb 91 m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
abbca718 92
a8f0bd18 93 return false;
abbca718
LOK
94}
95
2abe74eb 96void *CCECProcessor::Process(void)
f99bc831 97{
8b0ee2cc
LOK
98 {
99 CLockObject lock(&m_mutex);
100 m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
101 m_startCondition.Signal();
102 }
abbca718 103
271e7778
LOK
104 cec_command command;
105 CCECAdapterMessage msg;
9dee1670 106
b9eea66d
LOK
107 m_communication->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
108
13fd6a66 109 while (!IsStopped())
abbca718 110 {
60fa4578 111 bool bParseFrame(false);
9dee1670 112 command.clear();
76321de4
LOK
113 msg.clear();
114
60fa4578
LOK
115 {
116 CLockObject lock(&m_mutex);
88c43521
LOK
117 if (m_communication->IsOpen() && m_communication->Read(msg, 50))
118 bParseFrame = ParseMessage(msg) && !IsStopped();
76321de4
LOK
119
120 if (bParseFrame)
9dee1670 121 command = m_currentframe;
60fa4578
LOK
122 }
123
13fd6a66 124 if (bParseFrame)
9dee1670 125 ParseCommand(command);
abbca718 126
13fd6a66
LOK
127 m_controller->CheckKeypressTimeout();
128
0ab58650
LOK
129 for (unsigned int iDevicePtr = 0; iDevicePtr < 16; iDevicePtr++)
130 m_busDevices[iDevicePtr]->PollVendorId();
131
13fd6a66 132 if (!IsStopped())
d5bffd3c 133 Sleep(5);
abbca718
LOK
134 }
135
60fa4578 136 return NULL;
abbca718
LOK
137}
138
2abe74eb 139bool CCECProcessor::SetActiveView(void)
abbca718 140{
60fa4578 141 if (!IsRunning())
f99bc831
LOK
142 return false;
143
cc60ab1c
LOK
144 if (m_iLogicalAddress != CECDEVICE_UNKNOWN && m_busDevices[m_iLogicalAddress])
145 return m_busDevices[m_iLogicalAddress]->BroadcastActiveView();
146 return false;
abbca718
LOK
147}
148
2abe74eb 149bool CCECProcessor::SetInactiveView(void)
abbca718 150{
60fa4578 151 if (!IsRunning())
f99bc831
LOK
152 return false;
153
cc60ab1c
LOK
154 if (m_iLogicalAddress != CECDEVICE_UNKNOWN && m_busDevices[m_iLogicalAddress])
155 return m_busDevices[m_iLogicalAddress]->BroadcastInactiveView();
156 return false;
abbca718
LOK
157}
158
9dee1670 159void CCECProcessor::LogOutput(const cec_command &data)
abbca718 160{
1d3ca3de
LOK
161 CStdString strTx;
162 strTx.Format("<< %02x:%02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination, (uint8_t)data.opcode);
9dee1670 163
06a1f7ce 164 for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++)
1d3ca3de
LOK
165 strTx.AppendFormat(":%02x", data.parameters[iPtr]);
166 m_controller->AddLog(CEC_LOG_TRAFFIC, strTx.c_str());
9dee1670 167}
2abe74eb 168
179efe86 169bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress /* = CECDEVICE_UNKNOWN */)
abbca718 170{
179efe86 171 if (iLogicalAddress != CECDEVICE_UNKNOWN)
06775107
LOK
172 {
173 CStdString strLog;
174 strLog.Format("<< setting logical address to %1x", iLogicalAddress);
175 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
06775107 176 m_iLogicalAddress = iLogicalAddress;
179efe86 177 m_bLogicalAddressSet = false;
06775107 178 }
2abe74eb 179
179efe86
LOK
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;
abbca718 184}
825ddb96 185
2492216a
LOK
186bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress)
187{
cc60ab1c
LOK
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;
1969b140
LOK
194}
195
8b7e5ff6
LOK
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
6a1c0009
LOK
209cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress)
210{
211 return m_busDevices[iAddress]->GetCecVersion();
212}
213
a3269a0a
LOK
214bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
215{
cc60ab1c
LOK
216 if (m_busDevices[iAddress])
217 {
218 *language = m_busDevices[iAddress]->GetMenuLanguage();
219 return (strcmp(language->language, "???") == 0);
220 }
221 return false;
a3269a0a
LOK
222}
223
44c74256
LOK
224uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress)
225{
cc60ab1c
LOK
226 if (m_busDevices[iAddress])
227 return m_busDevices[iAddress]->GetVendorId();
228 return false;
44c74256
LOK
229}
230
e55f3f70
LOK
231cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress)
232{
cc60ab1c
LOK
233 if (m_busDevices[iAddress])
234 return m_busDevices[iAddress]->GetPowerStatus();
235 return CEC_POWER_STATUS_UNKNOWN;
e55f3f70
LOK
236}
237
8d84e2c0 238bool CCECProcessor::Transmit(const cec_command &data)
825ddb96 239{
179efe86
LOK
240 SetLogicalAddress();
241
7ea0d558 242 bool bReturn(false);
13929cff
LOK
243 LogOutput(data);
244
5a1e87c8 245 CCECAdapterMessagePtr output(new CCECAdapterMessage(data));
13929cff 246
2abe74eb 247 CLockObject lock(&m_mutex);
eb35e4ca
LOK
248 {
249 CLockObject msgLock(&output->mutex);
250 if (!m_communication || !m_communication->Write(output))
251 return bReturn;
252 else
0e31a62c 253 {
e4613795 254 output->condition.Wait(&output->mutex, 1000);
0e31a62c
LOK
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 }
2abe74eb 261
1f0e9e0f 262 if (data.transmit_timeout > 0)
cd505625 263 {
1f0e9e0f 264 if ((bReturn = WaitForTransmitSucceeded(output->size(), data.transmit_timeout)) == false)
cd505625
LOK
265 m_controller->AddLog(CEC_LOG_ERROR, "did not receive ack");
266 }
267 else
cd505625 268 bReturn = true;
2abe74eb
LOK
269 }
270
7ea0d558 271 return bReturn;
825ddb96 272}
abbca718 273
2abe74eb 274void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, ECecAbortReason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
abbca718 275{
9dee1670
LOK
276 m_controller->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
277
06a1f7ce
LOK
278 cec_command command;
279 cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_FEATURE_ABORT);
9dee1670
LOK
280 command.parameters.push_back((uint8_t)opcode);
281 command.parameters.push_back((uint8_t)reason);
282
283 Transmit(command);
abbca718
LOK
284}
285
1f0e9e0f 286bool CCECProcessor::WaitForTransmitSucceeded(uint8_t iLength, uint32_t iTimeout /* = 1000 */)
abbca718 287{
1f0e9e0f
LOK
288 bool bError(false);
289 bool bTransmitSucceeded(false);
7ea0d558 290 uint8_t iPacketsLeft(iLength / 4);
abbca718
LOK
291
292 int64_t iNow = GetTimeMs();
25701fa6 293 int64_t iTargetTime = iNow + (uint64_t) iTimeout;
abbca718 294
1f0e9e0f 295 while (!bTransmitSucceeded && !bError && (iTimeout == 0 || iNow < iTargetTime))
abbca718 296 {
220537f2 297 CCECAdapterMessage msg;
25701fa6 298
06a1f7ce 299 if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000))
abbca718 300 {
abbca718 301 iNow = GetTimeMs();
5d38b0b6
LOK
302 continue;
303 }
304
1f0e9e0f 305 bError = msg.is_error();
e41b06f9
LOK
306 m_controller->AddLog(msg.is_error() ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
307
7ea0d558
LOK
308 switch(msg.message())
309 {
7ea0d558 310 case MSGCODE_COMMAND_ACCEPTED:
7ea0d558
LOK
311 iPacketsLeft--;
312 break;
313 case MSGCODE_TRANSMIT_SUCCEEDED:
7ea0d558 314 bTransmitSucceeded = (iPacketsLeft == 0);
1f0e9e0f 315 bError = !bTransmitSucceeded;
7ea0d558 316 break;
7ea0d558 317 default:
88c43521 318 CStdString strLog;
e41b06f9 319 strLog.Format("received unexpected reply '%1x' instead of ack", msg.message());
88c43521 320 m_controller->AddLog(CEC_LOG_WARNING, strLog);
1f0e9e0f 321 bError = true;
7ea0d558
LOK
322 break;
323 }
324
5d38b0b6 325 iNow = GetTimeMs();
abbca718
LOK
326 }
327
1f0e9e0f 328 return bTransmitSucceeded && !bError;
abbca718
LOK
329}
330
0e7b6c54 331bool CCECProcessor::ParseMessage(const CCECAdapterMessage &msg)
abbca718 332{
7ea0d558 333 bool bEom = false;
60fa4578 334
0e7b6c54 335 if (msg.empty())
7ea0d558 336 return bEom;
abbca718 337
e41b06f9 338 m_controller->AddLog(msg.is_error() ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
abbca718 339
0e7b6c54 340 switch(msg.message())
abbca718 341 {
abbca718
LOK
342 case MSGCODE_FRAME_START:
343 {
7ea0d558 344 m_currentframe.clear();
0e7b6c54 345 if (msg.size() >= 2)
abbca718 346 {
0e7b6c54
LOK
347 m_currentframe.initiator = msg.initiator();
348 m_currentframe.destination = msg.destination();
349 m_currentframe.ack = msg.ack();
350 m_currentframe.eom = msg.eom();
abbca718 351 }
abbca718
LOK
352 }
353 break;
354 case MSGCODE_FRAME_DATA:
355 {
0e7b6c54 356 if (msg.size() >= 2)
78e8010a 357 {
e41b06f9 358 m_currentframe.push_back(msg[1]);
0e7b6c54 359 m_currentframe.eom = msg.eom();
abbca718 360 }
0e7b6c54 361 bEom = msg.eom();
abbca718 362 }
78e8010a 363 break;
abbca718
LOK
364 default:
365 break;
366 }
7ea0d558
LOK
367
368 return bEom;
abbca718
LOK
369}
370
e9de9629 371void CCECProcessor::ParseCommand(cec_command &command)
abbca718 372{
e9de9629 373 CStdString dataStr;
1d3ca3de 374 dataStr.Format(">> %1x%1x:%02x", command.initiator, command.destination, command.opcode);
e9de9629
LOK
375 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
376 dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
1d3ca3de 377 m_controller->AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
dc113aca 378
e9de9629
LOK
379 if (!m_bMonitor)
380 m_busDevices[(uint8_t)command.initiator]->HandleCommand(command);
dc113aca
LOK
381}
382
0f23c85c
LOK
383uint16_t CCECProcessor::GetPhysicalAddress(void) const
384{
cc60ab1c
LOK
385 if (m_iLogicalAddress != CECDEVICE_UNKNOWN && m_busDevices[m_iLogicalAddress])
386 return m_busDevices[m_iLogicalAddress]->GetPhysicalAddress();
387 return false;
0f23c85c
LOK
388}
389
e9de9629 390void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode)
dc113aca 391{
e9de9629 392 m_controller->SetCurrentButton(iButtonCode);
dc113aca
LOK
393}
394
e9de9629 395void CCECProcessor::AddCommand(const cec_command &command)
dc113aca 396{
e9de9629 397 m_controller->AddCommand(command);
dc113aca
LOK
398}
399
e9de9629 400void CCECProcessor::AddKey(void)
dc113aca 401{
e9de9629 402 m_controller->AddKey();
abbca718 403}
acec5f48 404
e9de9629 405void CCECProcessor::AddLog(cec_log_level level, const CStdString &strMessage)
acec5f48 406{
e9de9629 407 m_controller->AddLog(level, strMessage);
acec5f48 408}