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 (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
52 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, 0);
54 m_busDevices
[m_iLogicalAddress
]->SetPhysicalAddress(iPhysicalAddress
);
57 CCECProcessor::~CCECProcessor(void)
60 m_communication
= NULL
;
62 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
63 delete m_busDevices
[iPtr
];
66 bool CCECProcessor::Start(void)
68 if (!m_communication
|| !m_communication
->IsOpen())
70 m_controller
->AddLog(CEC_LOG_ERROR
, "connection is closed");
74 if (!SetLogicalAddress(m_iLogicalAddress
))
76 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the logical address");
83 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
88 void *CCECProcessor::Process(void)
90 m_controller
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
93 cec_adapter_message msg
;
97 bool bParseFrame(false);
99 bool bTransmitSucceeded(false);
104 CLockObject
lock(&m_mutex
);
105 if (m_communication
->IsOpen() && m_communication
->Read(msg
, 50))
106 ParseMessage(msg
, &bError
, &bTransmitSucceeded
, &bParseFrame
);
108 bParseFrame
&= !IsStopped();
110 command
= m_currentframe
;
114 ParseCommand(command
);
116 m_controller
->CheckKeypressTimeout();
118 for (unsigned int iDevicePtr
= 0; iDevicePtr
< 16; iDevicePtr
++)
119 m_busDevices
[iDevicePtr
]->PollVendorId();
128 bool CCECProcessor::SetActiveView(void)
133 return m_busDevices
[m_iLogicalAddress
]->BroadcastActiveView();
136 bool CCECProcessor::SetInactiveView(void)
141 return m_busDevices
[m_iLogicalAddress
]->BroadcastInactiveView();
144 void CCECProcessor::LogOutput(const cec_command
&data
)
147 strTx
.Format("<< %02x:%02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
, (uint8_t)data
.opcode
);
149 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
150 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
151 m_controller
->AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
154 bool CCECProcessor::Transmit(const cec_command
&data
, bool bWaitForAck
/* = true */)
158 cec_adapter_message output
;
160 CAdapterCommunication::FormatAdapterMessage(data
, output
);
162 return TransmitFormatted(output
, bWaitForAck
);
165 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
168 strLog
.Format("<< setting logical address to %1x", iLogicalAddress
);
169 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
171 m_iLogicalAddress
= iLogicalAddress
;
172 return m_communication
&& m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
175 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
)
177 m_busDevices
[m_iLogicalAddress
]->SetPhysicalAddress(iPhysicalAddress
);
178 return m_busDevices
[m_iLogicalAddress
]->BroadcastActiveView();
181 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
184 strLog
.Format("== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
185 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
187 m_bMonitor
= bEnable
;
189 return m_communication
&& m_communication
->SetAckMask(0);
191 return m_communication
&& m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
194 bool CCECProcessor::TransmitFormatted(const cec_adapter_message
&data
, bool bWaitForAck
/* = true */)
196 CLockObject
lock(&m_mutex
);
197 if (!m_communication
|| !m_communication
->Write(data
))
202 uint64_t now
= GetTimeMs();
203 uint64_t target
= now
+ 1000;
207 while (!bGotAck
&& now
< target
)
209 bGotAck
= WaitForAck(&bError
, (uint32_t) (target
- now
));
212 if (bError
&& now
< target
)
214 m_controller
->AddLog(CEC_LOG_ERROR
, "retransmitting previous frame");
215 if (!m_communication
->Write(data
))
224 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, ECecAbortReason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
226 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
229 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_FEATURE_ABORT
);
230 command
.parameters
.push_back((uint8_t)opcode
);
231 command
.parameters
.push_back((uint8_t)reason
);
236 bool CCECProcessor::WaitForAck(bool *bError
, uint32_t iTimeout
/* = 1000 */)
238 bool bTransmitSucceeded
= false, bEom
= false;
241 int64_t iNow
= GetTimeMs();
242 int64_t iTargetTime
= iNow
+ (uint64_t) iTimeout
;
244 while (!bTransmitSucceeded
&& !*bError
&& (iTimeout
== 0 || iNow
< iTargetTime
))
246 cec_adapter_message msg
;
249 if (!m_communication
->Read(msg
, iTimeout
> 0 ? (int32_t)(iTargetTime
- iNow
) : 1000))
255 ParseMessage(msg
, bError
, &bTransmitSucceeded
, &bEom
, false);
259 return bTransmitSucceeded
&& !*bError
;
262 void CCECProcessor::ParseMessage(cec_adapter_message
&msg
, bool *bError
, bool *bTransmitSucceeded
, bool *bEom
, bool bProcessMessages
/* = true */)
265 *bTransmitSucceeded
= false;
273 switch(msg
.message())
275 case MSGCODE_NOTHING
:
276 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_NOTHING");
278 case MSGCODE_TIMEOUT_ERROR
:
279 case MSGCODE_HIGH_ERROR
:
280 case MSGCODE_LOW_ERROR
:
282 if (msg
.message() == MSGCODE_TIMEOUT_ERROR
)
283 logStr
= "MSGCODE_TIMEOUT";
284 else if (msg
.message() == MSGCODE_HIGH_ERROR
)
285 logStr
= "MSGCODE_HIGH_ERROR";
287 logStr
= "MSGCODE_LOW_ERROR";
289 int iLine
= (msg
.size() >= 3) ? (msg
[1] << 8) | (msg
[2]) : 0;
290 uint32_t iTime
= (msg
.size() >= 7) ? (msg
[3] << 24) | (msg
[4] << 16) | (msg
[5] << 8) | (msg
[6]) : 0;
291 logStr
.AppendFormat(" line:%i", iLine
);
292 logStr
.AppendFormat(" time:%u", iTime
);
293 m_controller
->AddLog(CEC_LOG_WARNING
, logStr
.c_str());
297 case MSGCODE_FRAME_START
:
299 if (bProcessMessages
)
301 logStr
= "MSGCODE_FRAME_START";
302 m_currentframe
.clear();
305 logStr
.AppendFormat(" initiator:%u destination:%u ack:%s %s", msg
.initiator(), msg
.destination(), msg
.ack() ? "high" : "low", msg
.eom() ? "eom" : "");
306 m_currentframe
.initiator
= msg
.initiator();
307 m_currentframe
.destination
= msg
.destination();
308 m_currentframe
.ack
= msg
.ack();
309 m_currentframe
.eom
= msg
.eom();
311 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
315 m_frameBuffer
.Push(msg
);
319 case MSGCODE_FRAME_DATA
:
321 if (bProcessMessages
)
323 logStr
= "MSGCODE_FRAME_DATA";
326 uint8_t iData
= msg
[1];
327 logStr
.AppendFormat(" %02x", iData
);
328 m_currentframe
.push_back(iData
);
329 m_currentframe
.eom
= msg
.eom();
331 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
335 m_frameBuffer
.Push(msg
);
341 case MSGCODE_COMMAND_ACCEPTED
:
342 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_COMMAND_ACCEPTED");
344 case MSGCODE_TRANSMIT_SUCCEEDED
:
345 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_TRANSMIT_SUCCEEDED");
346 *bTransmitSucceeded
= true;
348 case MSGCODE_RECEIVE_FAILED
:
349 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_RECEIVE_FAILED");
352 case MSGCODE_COMMAND_REJECTED
:
353 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_COMMAND_REJECTED");
356 case MSGCODE_TRANSMIT_FAILED_LINE
:
357 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_LINE");
360 case MSGCODE_TRANSMIT_FAILED_ACK
:
361 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_ACK");
364 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
365 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
368 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
369 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
377 void CCECProcessor::ParseCommand(cec_command
&command
)
380 dataStr
.Format(">> %1x%1x:%02x", command
.initiator
, command
.destination
, command
.opcode
);
381 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
382 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
383 m_controller
->AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
386 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
389 uint16_t CCECProcessor::GetPhysicalAddress(void) const
391 return m_busDevices
[m_iLogicalAddress
]->GetPhysicalAddress();
394 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode
)
396 m_controller
->SetCurrentButton(iButtonCode
);
399 void CCECProcessor::AddCommand(const cec_command
&command
)
401 m_controller
->AddCommand(command
);
404 void CCECProcessor::AddKey(void)
406 m_controller
->AddKey();
409 void CCECProcessor::AddLog(cec_log_level level
, const CStdString
&strMessage
)
411 m_controller
->AddLog(level
, strMessage
);