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(CECDEVICE_UNKNOWN
),
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
;
94 m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
98 bool bParseFrame(false);
103 CLockObject
lock(&m_mutex
);
104 if (m_frameBuffer
.Pop(msgPtr
))
105 bParseFrame
= ParseMessage(msgPtr
);
106 else if (m_communication
->IsOpen() && m_communication
->Read(msg
, 50))
108 msgPtr
= CCECAdapterMessagePtr(new CCECAdapterMessage(msg
));
109 bParseFrame
= ParseMessage(msgPtr
);
112 bParseFrame
&= !IsStopped();
114 command
= m_currentframe
;
118 ParseCommand(command
);
120 m_controller
->CheckKeypressTimeout();
122 for (unsigned int iDevicePtr
= 0; iDevicePtr
< 16; iDevicePtr
++)
123 m_busDevices
[iDevicePtr
]->PollVendorId();
132 bool CCECProcessor::SetActiveView(void)
137 return m_busDevices
[m_iLogicalAddress
]->BroadcastActiveView();
140 bool CCECProcessor::SetInactiveView(void)
145 return m_busDevices
[m_iLogicalAddress
]->BroadcastInactiveView();
148 void CCECProcessor::LogOutput(const cec_command
&data
)
151 strTx
.Format("<< %02x:%02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
, (uint8_t)data
.opcode
);
153 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
154 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
155 m_controller
->AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
158 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
160 if (m_iLogicalAddress
!= iLogicalAddress
)
163 strLog
.Format("<< setting logical address to %1x", iLogicalAddress
);
164 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
166 m_iLogicalAddress
= iLogicalAddress
;
167 return m_communication
&& m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
173 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
)
175 m_busDevices
[m_iLogicalAddress
]->SetPhysicalAddress(iPhysicalAddress
);
176 return m_busDevices
[m_iLogicalAddress
]->BroadcastActiveView();
179 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
182 strLog
.Format("== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
183 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
185 m_bMonitor
= bEnable
;
187 return m_communication
&& m_communication
->SetAckMask(0);
189 return m_communication
&& m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
192 bool CCECProcessor::Transmit(const cec_command
&data
)
197 CCECAdapterMessagePtr
output(new CCECAdapterMessage(data
));
199 CLockObject
lock(&m_mutex
);
201 CLockObject
msgLock(&output
->mutex
);
202 if (!m_communication
|| !m_communication
->Write(output
))
206 output
->condition
.Wait(&output
->mutex
);
207 if (output
->state
!= ADAPTER_MESSAGE_STATE_SENT
)
209 m_controller
->AddLog(CEC_LOG_ERROR
, "command was not sent");
215 if (data
.ack_timeout
> 0)
218 if ((bReturn
= WaitForAck(&bError
, output
->size(), data
.ack_timeout
)) == false)
219 m_controller
->AddLog(CEC_LOG_ERROR
, "did not receive ack");
229 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, ECecAbortReason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
231 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
234 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_FEATURE_ABORT
);
235 command
.parameters
.push_back((uint8_t)opcode
);
236 command
.parameters
.push_back((uint8_t)reason
);
241 bool CCECProcessor::WaitForAck(bool *bError
, uint8_t iLength
, uint32_t iTimeout
/* = 1000 */)
243 bool bTransmitSucceeded
= false;
244 uint8_t iPacketsLeft(iLength
/ 4);
247 int64_t iNow
= GetTimeMs();
248 int64_t iTargetTime
= iNow
+ (uint64_t) iTimeout
;
250 while (!bTransmitSucceeded
&& !*bError
&& (iTimeout
== 0 || iNow
< iTargetTime
))
252 CCECAdapterMessage msg
;
254 if (!m_communication
->Read(msg
, iTimeout
> 0 ? (int32_t)(iTargetTime
- iNow
) : 1000))
260 switch(msg
.message())
262 case MSGCODE_TIMEOUT_ERROR
:
263 case MSGCODE_HIGH_ERROR
:
264 case MSGCODE_LOW_ERROR
:
267 if (msg
.message() == MSGCODE_TIMEOUT_ERROR
)
268 logStr
= "MSGCODE_TIMEOUT";
269 else if (msg
.message() == MSGCODE_HIGH_ERROR
)
270 logStr
= "MSGCODE_HIGH_ERROR";
272 logStr
= "MSGCODE_LOW_ERROR";
274 int iLine
= (msg
.size() >= 3) ? (msg
[1] << 8) | (msg
[2]) : 0;
275 uint32_t iTime
= (msg
.size() >= 7) ? (msg
[3] << 24) | (msg
[4] << 16) | (msg
[5] << 8) | (msg
[6]) : 0;
276 logStr
.AppendFormat(" line:%i", iLine
);
277 logStr
.AppendFormat(" time:%u", iTime
);
278 m_controller
->AddLog(CEC_LOG_WARNING
, logStr
.c_str());
282 case MSGCODE_COMMAND_ACCEPTED
:
283 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_COMMAND_ACCEPTED");
286 case MSGCODE_TRANSMIT_SUCCEEDED
:
287 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_TRANSMIT_SUCCEEDED");
288 bTransmitSucceeded
= (iPacketsLeft
== 0);
289 *bError
= !bTransmitSucceeded
;
291 case MSGCODE_RECEIVE_FAILED
:
292 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_RECEIVE_FAILED");
295 case MSGCODE_COMMAND_REJECTED
:
296 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_COMMAND_REJECTED");
299 case MSGCODE_TRANSMIT_FAILED_LINE
:
300 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_LINE");
303 case MSGCODE_TRANSMIT_FAILED_ACK
:
304 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_ACK");
307 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
308 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
311 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
312 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
316 CCECAdapterMessagePtr msgPtr
= CCECAdapterMessagePtr(new CCECAdapterMessage(msg
));
317 m_frameBuffer
.Push(msgPtr
);
324 return bTransmitSucceeded
&& !*bError
;
327 bool CCECProcessor::ParseMessage(CCECAdapterMessagePtr msg
)
336 switch(msg
->message())
338 case MSGCODE_NOTHING
:
339 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_NOTHING");
341 case MSGCODE_TIMEOUT_ERROR
:
342 case MSGCODE_HIGH_ERROR
:
343 case MSGCODE_LOW_ERROR
:
345 if (msg
->message() == MSGCODE_TIMEOUT_ERROR
)
346 logStr
= "MSGCODE_TIMEOUT";
347 else if (msg
->message() == MSGCODE_HIGH_ERROR
)
348 logStr
= "MSGCODE_HIGH_ERROR";
350 logStr
= "MSGCODE_LOW_ERROR";
352 int iLine
= (msg
->size() >= 3) ? (msg
->at(1) << 8) | (msg
->at(2)) : 0;
353 uint32_t iTime
= (msg
->size() >= 7) ? (msg
->at(3) << 24) | (msg
->at(4) << 16) | (msg
->at(5) << 8) | (msg
->at(6)) : 0;
354 logStr
.AppendFormat(" line:%i", iLine
);
355 logStr
.AppendFormat(" time:%u", iTime
);
356 m_controller
->AddLog(CEC_LOG_WARNING
, logStr
.c_str());
359 case MSGCODE_FRAME_START
:
361 logStr
= "MSGCODE_FRAME_START";
362 m_currentframe
.clear();
363 if (msg
->size() >= 2)
365 logStr
.AppendFormat(" initiator:%u destination:%u ack:%s %s", msg
->initiator(), msg
->destination(), msg
->ack() ? "high" : "low", msg
->eom() ? "eom" : "");
366 m_currentframe
.initiator
= msg
->initiator();
367 m_currentframe
.destination
= msg
->destination();
368 m_currentframe
.ack
= msg
->ack();
369 m_currentframe
.eom
= msg
->eom();
371 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
374 case MSGCODE_FRAME_DATA
:
376 logStr
= "MSGCODE_FRAME_DATA";
377 if (msg
->size() >= 2)
379 uint8_t iData
= msg
->at(1);
380 logStr
.AppendFormat(" %02x", iData
);
381 m_currentframe
.push_back(iData
);
382 m_currentframe
.eom
= msg
->eom();
384 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
389 case MSGCODE_COMMAND_ACCEPTED
:
390 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_COMMAND_ACCEPTED");
392 case MSGCODE_TRANSMIT_SUCCEEDED
:
393 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_TRANSMIT_SUCCEEDED");
395 case MSGCODE_RECEIVE_FAILED
:
396 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_RECEIVE_FAILED");
398 case MSGCODE_COMMAND_REJECTED
:
399 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_COMMAND_REJECTED");
401 case MSGCODE_TRANSMIT_FAILED_LINE
:
402 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_LINE");
404 case MSGCODE_TRANSMIT_FAILED_ACK
:
405 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_ACK");
407 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
408 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
410 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
411 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
420 void CCECProcessor::ParseCommand(cec_command
&command
)
423 dataStr
.Format(">> %1x%1x:%02x", command
.initiator
, command
.destination
, command
.opcode
);
424 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
425 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
426 m_controller
->AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
429 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
432 uint16_t CCECProcessor::GetPhysicalAddress(void) const
434 return m_busDevices
[m_iLogicalAddress
]->GetPhysicalAddress();
437 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode
)
439 m_controller
->SetCurrentButton(iButtonCode
);
442 void CCECProcessor::AddCommand(const cec_command
&command
)
444 m_controller
->AddCommand(command
);
447 void CCECProcessor::AddKey(void)
449 m_controller
->AddKey();
452 void CCECProcessor::AddLog(cec_log_level level
, const CStdString
&strMessage
)
454 m_controller
->AddLog(level
, strMessage
);