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
;
95 bool bParseFrame(false);
100 CLockObject
lock(&m_mutex
);
101 if (m_communication
->IsOpen() && m_communication
->Read(msg
, 50))
102 bParseFrame
= ParseMessage(msg
);
104 bParseFrame
&= !IsStopped();
106 command
= m_currentframe
;
110 ParseCommand(command
);
112 m_controller
->CheckKeypressTimeout();
114 for (unsigned int iDevicePtr
= 0; iDevicePtr
< 16; iDevicePtr
++)
115 m_busDevices
[iDevicePtr
]->PollVendorId();
124 bool CCECProcessor::SetActiveView(void)
129 return m_busDevices
[m_iLogicalAddress
]->BroadcastActiveView();
132 bool CCECProcessor::SetInactiveView(void)
137 return m_busDevices
[m_iLogicalAddress
]->BroadcastInactiveView();
140 void CCECProcessor::LogOutput(const cec_command
&data
)
143 strTx
.Format("<< %02x:%02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
, (uint8_t)data
.opcode
);
145 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
146 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
147 m_controller
->AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
150 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
153 strLog
.Format("<< setting logical address to %1x", iLogicalAddress
);
154 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
156 m_iLogicalAddress
= iLogicalAddress
;
157 return m_communication
&& m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
160 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
)
162 m_busDevices
[m_iLogicalAddress
]->SetPhysicalAddress(iPhysicalAddress
);
163 return m_busDevices
[m_iLogicalAddress
]->BroadcastActiveView();
166 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
169 strLog
.Format("== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
170 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
172 m_bMonitor
= bEnable
;
174 return m_communication
&& m_communication
->SetAckMask(0);
176 return m_communication
&& m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
179 bool CCECProcessor::Transmit(const cec_command
&data
, bool bWaitForAck
/* = true */)
184 CCECAdapterMessage
output(data
);
186 CLockObject
lock(&m_mutex
);
187 if (!m_communication
|| !m_communication
->Write(output
))
193 if ((bReturn
= WaitForAck(&bError
, output
.size(), 1000)) == false)
194 m_controller
->AddLog(CEC_LOG_ERROR
, "did not receive ack");
204 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, ECecAbortReason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
206 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
209 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_FEATURE_ABORT
);
210 command
.parameters
.push_back((uint8_t)opcode
);
211 command
.parameters
.push_back((uint8_t)reason
);
216 bool CCECProcessor::WaitForAck(bool *bError
, uint8_t iLength
, uint32_t iTimeout
/* = 1000 */)
218 bool bTransmitSucceeded
= false;
219 uint8_t iPacketsLeft(iLength
/ 4);
222 int64_t iNow
= GetTimeMs();
223 int64_t iTargetTime
= iNow
+ (uint64_t) iTimeout
;
225 while (!bTransmitSucceeded
&& !*bError
&& (iTimeout
== 0 || iNow
< iTargetTime
))
227 CCECAdapterMessage msg
;
229 if (!m_communication
->Read(msg
, iTimeout
> 0 ? (int32_t)(iTargetTime
- iNow
) : 1000))
235 switch(msg
.message())
237 case MSGCODE_TIMEOUT_ERROR
:
238 case MSGCODE_HIGH_ERROR
:
239 case MSGCODE_LOW_ERROR
:
242 if (msg
.message() == MSGCODE_TIMEOUT_ERROR
)
243 logStr
= "MSGCODE_TIMEOUT";
244 else if (msg
.message() == MSGCODE_HIGH_ERROR
)
245 logStr
= "MSGCODE_HIGH_ERROR";
247 logStr
= "MSGCODE_LOW_ERROR";
249 int iLine
= (msg
.size() >= 3) ? (msg
[1] << 8) | (msg
[2]) : 0;
250 uint32_t iTime
= (msg
.size() >= 7) ? (msg
[3] << 24) | (msg
[4] << 16) | (msg
[5] << 8) | (msg
[6]) : 0;
251 logStr
.AppendFormat(" line:%i", iLine
);
252 logStr
.AppendFormat(" time:%u", iTime
);
253 m_controller
->AddLog(CEC_LOG_WARNING
, logStr
.c_str());
257 case MSGCODE_COMMAND_ACCEPTED
:
258 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_COMMAND_ACCEPTED");
261 case MSGCODE_TRANSMIT_SUCCEEDED
:
262 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_TRANSMIT_SUCCEEDED");
263 bTransmitSucceeded
= (iPacketsLeft
== 0);
264 *bError
= !bTransmitSucceeded
;
266 case MSGCODE_RECEIVE_FAILED
:
267 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_RECEIVE_FAILED");
270 case MSGCODE_COMMAND_REJECTED
:
271 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_COMMAND_REJECTED");
274 case MSGCODE_TRANSMIT_FAILED_LINE
:
275 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_LINE");
278 case MSGCODE_TRANSMIT_FAILED_ACK
:
279 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_ACK");
282 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
283 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
286 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
287 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
291 m_frameBuffer
.Push(msg
);
298 return bTransmitSucceeded
&& !*bError
;
301 bool CCECProcessor::ParseMessage(CCECAdapterMessage
&msg
)
310 switch(msg
.message())
312 case MSGCODE_NOTHING
:
313 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_NOTHING");
315 case MSGCODE_TIMEOUT_ERROR
:
316 case MSGCODE_HIGH_ERROR
:
317 case MSGCODE_LOW_ERROR
:
319 if (msg
.message() == MSGCODE_TIMEOUT_ERROR
)
320 logStr
= "MSGCODE_TIMEOUT";
321 else if (msg
.message() == MSGCODE_HIGH_ERROR
)
322 logStr
= "MSGCODE_HIGH_ERROR";
324 logStr
= "MSGCODE_LOW_ERROR";
326 int iLine
= (msg
.size() >= 3) ? (msg
[1] << 8) | (msg
[2]) : 0;
327 uint32_t iTime
= (msg
.size() >= 7) ? (msg
[3] << 24) | (msg
[4] << 16) | (msg
[5] << 8) | (msg
[6]) : 0;
328 logStr
.AppendFormat(" line:%i", iLine
);
329 logStr
.AppendFormat(" time:%u", iTime
);
330 m_controller
->AddLog(CEC_LOG_WARNING
, logStr
.c_str());
333 case MSGCODE_FRAME_START
:
335 logStr
= "MSGCODE_FRAME_START";
336 m_currentframe
.clear();
339 logStr
.AppendFormat(" initiator:%u destination:%u ack:%s %s", msg
.initiator(), msg
.destination(), msg
.ack() ? "high" : "low", msg
.eom() ? "eom" : "");
340 m_currentframe
.initiator
= msg
.initiator();
341 m_currentframe
.destination
= msg
.destination();
342 m_currentframe
.ack
= msg
.ack();
343 m_currentframe
.eom
= msg
.eom();
345 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
348 case MSGCODE_FRAME_DATA
:
350 logStr
= "MSGCODE_FRAME_DATA";
353 uint8_t iData
= msg
[1];
354 logStr
.AppendFormat(" %02x", iData
);
355 m_currentframe
.push_back(iData
);
356 m_currentframe
.eom
= msg
.eom();
358 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
363 case MSGCODE_COMMAND_ACCEPTED
:
364 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_COMMAND_ACCEPTED");
366 case MSGCODE_TRANSMIT_SUCCEEDED
:
367 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_TRANSMIT_SUCCEEDED");
369 case MSGCODE_RECEIVE_FAILED
:
370 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_RECEIVE_FAILED");
372 case MSGCODE_COMMAND_REJECTED
:
373 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_COMMAND_REJECTED");
375 case MSGCODE_TRANSMIT_FAILED_LINE
:
376 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_LINE");
378 case MSGCODE_TRANSMIT_FAILED_ACK
:
379 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_ACK");
381 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
382 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
384 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
385 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
394 void CCECProcessor::ParseCommand(cec_command
&command
)
397 dataStr
.Format(">> %1x%1x:%02x", command
.initiator
, command
.destination
, command
.opcode
);
398 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
399 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
400 m_controller
->AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
403 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
406 uint16_t CCECProcessor::GetPhysicalAddress(void) const
408 return m_busDevices
[m_iLogicalAddress
]->GetPhysicalAddress();
411 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode
)
413 m_controller
->SetCurrentButton(iButtonCode
);
416 void CCECProcessor::AddCommand(const cec_command
&command
)
418 m_controller
->AddCommand(command
);
421 void CCECProcessor::AddKey(void)
423 m_controller
->AddKey();
426 void CCECProcessor::AddLog(cec_log_level level
, const CStdString
&strMessage
)
428 m_controller
->AddLog(level
, strMessage
);