2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
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.
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.
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.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
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/
33 #include "CECProcessor.h"
35 #include "AdapterCommunication.h"
36 #include "devices/CECBusDevice.h"
38 #include "util/StdString.h"
39 #include "platform/timeutils.h"
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
),
51 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
52 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, iPtr
== iLogicalAddress
? iPhysicalAddress
: 0);
55 CCECProcessor::~CCECProcessor(void)
58 m_communication
= NULL
;
60 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
61 delete m_busDevices
[iPtr
];
64 bool CCECProcessor::Start(void)
66 if (!m_communication
|| !m_communication
->IsOpen())
68 m_controller
->AddLog(CEC_LOG_ERROR
, "connection is closed");
72 if (!SetLogicalAddress(m_iLogicalAddress
))
74 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the logical address");
81 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
86 void *CCECProcessor::Process(void)
88 m_controller
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
91 CCECAdapterMessage msg
;
92 CCECAdapterMessagePtr msgPtr
;
96 bool bParseFrame(false);
101 CLockObject
lock(&m_mutex
);
102 if (m_frameBuffer
.Pop(msgPtr
))
103 bParseFrame
= ParseMessage(msgPtr
);
104 else if (m_communication
->IsOpen() && m_communication
->Read(msg
, 50))
106 msgPtr
= CCECAdapterMessagePtr(new CCECAdapterMessage(msg
));
107 bParseFrame
= ParseMessage(msgPtr
);
110 bParseFrame
&= !IsStopped();
112 command
= m_currentframe
;
116 ParseCommand(command
);
118 m_controller
->CheckKeypressTimeout();
120 for (unsigned int iDevicePtr
= 0; iDevicePtr
< 16; iDevicePtr
++)
121 m_busDevices
[iDevicePtr
]->PollVendorId();
130 bool CCECProcessor::SetActiveView(void)
135 return m_busDevices
[m_iLogicalAddress
]->BroadcastActiveView();
138 bool CCECProcessor::SetInactiveView(void)
143 return m_busDevices
[m_iLogicalAddress
]->BroadcastInactiveView();
146 void CCECProcessor::LogOutput(const cec_command
&data
)
149 strTx
.Format("<< %02x:%02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
, (uint8_t)data
.opcode
);
151 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
152 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
153 m_controller
->AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
156 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
158 if (m_iLogicalAddress
!= iLogicalAddress
)
161 strLog
.Format("<< setting logical address to %1x", iLogicalAddress
);
162 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
164 m_iLogicalAddress
= iLogicalAddress
;
165 return m_communication
&& m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
171 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
)
173 m_busDevices
[m_iLogicalAddress
]->SetPhysicalAddress(iPhysicalAddress
);
174 return m_busDevices
[m_iLogicalAddress
]->BroadcastActiveView();
177 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
180 strLog
.Format("== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
181 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
183 m_bMonitor
= bEnable
;
185 return m_communication
&& m_communication
->SetAckMask(0);
187 return m_communication
&& m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
190 bool CCECProcessor::Transmit(const cec_command
&data
, bool bWaitForAck
/* = true */)
195 CCECAdapterMessagePtr
output(new CCECAdapterMessage(data
));
197 CLockObject
lock(&m_mutex
);
198 if (!m_communication
|| !m_communication
->Write(output
))
204 if ((bReturn
= WaitForAck(&bError
, output
->size(), 1000)) == false)
205 m_controller
->AddLog(CEC_LOG_ERROR
, "did not receive ack");
215 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, ECecAbortReason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
217 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
220 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_FEATURE_ABORT
);
221 command
.parameters
.push_back((uint8_t)opcode
);
222 command
.parameters
.push_back((uint8_t)reason
);
227 bool CCECProcessor::WaitForAck(bool *bError
, uint8_t iLength
, uint32_t iTimeout
/* = 1000 */)
229 bool bTransmitSucceeded
= false;
230 uint8_t iPacketsLeft(iLength
/ 4);
233 int64_t iNow
= GetTimeMs();
234 int64_t iTargetTime
= iNow
+ (uint64_t) iTimeout
;
236 while (!bTransmitSucceeded
&& !*bError
&& (iTimeout
== 0 || iNow
< iTargetTime
))
238 CCECAdapterMessage msg
;
240 if (!m_communication
->Read(msg
, iTimeout
> 0 ? (int32_t)(iTargetTime
- iNow
) : 1000))
246 switch(msg
.message())
248 case MSGCODE_TIMEOUT_ERROR
:
249 case MSGCODE_HIGH_ERROR
:
250 case MSGCODE_LOW_ERROR
:
253 if (msg
.message() == MSGCODE_TIMEOUT_ERROR
)
254 logStr
= "MSGCODE_TIMEOUT";
255 else if (msg
.message() == MSGCODE_HIGH_ERROR
)
256 logStr
= "MSGCODE_HIGH_ERROR";
258 logStr
= "MSGCODE_LOW_ERROR";
260 int iLine
= (msg
.size() >= 3) ? (msg
[1] << 8) | (msg
[2]) : 0;
261 uint32_t iTime
= (msg
.size() >= 7) ? (msg
[3] << 24) | (msg
[4] << 16) | (msg
[5] << 8) | (msg
[6]) : 0;
262 logStr
.AppendFormat(" line:%i", iLine
);
263 logStr
.AppendFormat(" time:%u", iTime
);
264 m_controller
->AddLog(CEC_LOG_WARNING
, logStr
.c_str());
268 case MSGCODE_COMMAND_ACCEPTED
:
269 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_COMMAND_ACCEPTED");
272 case MSGCODE_TRANSMIT_SUCCEEDED
:
273 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_TRANSMIT_SUCCEEDED");
274 bTransmitSucceeded
= (iPacketsLeft
== 0);
275 *bError
= !bTransmitSucceeded
;
277 case MSGCODE_RECEIVE_FAILED
:
278 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_RECEIVE_FAILED");
281 case MSGCODE_COMMAND_REJECTED
:
282 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_COMMAND_REJECTED");
285 case MSGCODE_TRANSMIT_FAILED_LINE
:
286 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_LINE");
289 case MSGCODE_TRANSMIT_FAILED_ACK
:
290 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_ACK");
293 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
294 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
297 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
298 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
302 CCECAdapterMessagePtr msgPtr
= CCECAdapterMessagePtr(new CCECAdapterMessage(msg
));
303 m_frameBuffer
.Push(msgPtr
);
310 return bTransmitSucceeded
&& !*bError
;
313 bool CCECProcessor::ParseMessage(CCECAdapterMessagePtr msg
)
322 switch(msg
->message())
324 case MSGCODE_NOTHING
:
325 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_NOTHING");
327 case MSGCODE_TIMEOUT_ERROR
:
328 case MSGCODE_HIGH_ERROR
:
329 case MSGCODE_LOW_ERROR
:
331 if (msg
->message() == MSGCODE_TIMEOUT_ERROR
)
332 logStr
= "MSGCODE_TIMEOUT";
333 else if (msg
->message() == MSGCODE_HIGH_ERROR
)
334 logStr
= "MSGCODE_HIGH_ERROR";
336 logStr
= "MSGCODE_LOW_ERROR";
338 int iLine
= (msg
->size() >= 3) ? (msg
->at(1) << 8) | (msg
->at(2)) : 0;
339 uint32_t iTime
= (msg
->size() >= 7) ? (msg
->at(3) << 24) | (msg
->at(4) << 16) | (msg
->at(5) << 8) | (msg
->at(6)) : 0;
340 logStr
.AppendFormat(" line:%i", iLine
);
341 logStr
.AppendFormat(" time:%u", iTime
);
342 m_controller
->AddLog(CEC_LOG_WARNING
, logStr
.c_str());
345 case MSGCODE_FRAME_START
:
347 logStr
= "MSGCODE_FRAME_START";
348 m_currentframe
.clear();
349 if (msg
->size() >= 2)
351 logStr
.AppendFormat(" initiator:%u destination:%u ack:%s %s", msg
->initiator(), msg
->destination(), msg
->ack() ? "high" : "low", msg
->eom() ? "eom" : "");
352 m_currentframe
.initiator
= msg
->initiator();
353 m_currentframe
.destination
= msg
->destination();
354 m_currentframe
.ack
= msg
->ack();
355 m_currentframe
.eom
= msg
->eom();
357 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
360 case MSGCODE_FRAME_DATA
:
362 logStr
= "MSGCODE_FRAME_DATA";
363 if (msg
->size() >= 2)
365 uint8_t iData
= msg
->at(1);
366 logStr
.AppendFormat(" %02x", iData
);
367 m_currentframe
.push_back(iData
);
368 m_currentframe
.eom
= msg
->eom();
370 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
375 case MSGCODE_COMMAND_ACCEPTED
:
376 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_COMMAND_ACCEPTED");
378 case MSGCODE_TRANSMIT_SUCCEEDED
:
379 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_TRANSMIT_SUCCEEDED");
381 case MSGCODE_RECEIVE_FAILED
:
382 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_RECEIVE_FAILED");
384 case MSGCODE_COMMAND_REJECTED
:
385 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_COMMAND_REJECTED");
387 case MSGCODE_TRANSMIT_FAILED_LINE
:
388 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_LINE");
390 case MSGCODE_TRANSMIT_FAILED_ACK
:
391 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_ACK");
393 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
394 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
396 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
397 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
406 void CCECProcessor::ParseCommand(cec_command
&command
)
409 dataStr
.Format(">> %1x%1x:%02x", command
.initiator
, command
.destination
, command
.opcode
);
410 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
411 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
412 m_controller
->AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
415 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
418 uint16_t CCECProcessor::GetPhysicalAddress(void) const
420 return m_busDevices
[m_iLogicalAddress
]->GetPhysicalAddress();
423 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode
)
425 m_controller
->SetCurrentButton(iButtonCode
);
428 void CCECProcessor::AddCommand(const cec_command
&command
)
430 m_controller
->AddCommand(command
);
433 void CCECProcessor::AddKey(void)
435 m_controller
->AddKey();
438 void CCECProcessor::AddLog(cec_log_level level
, const CStdString
&strMessage
)
440 m_controller
->AddLog(level
, strMessage
);