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)
57 m_startCondition
.Broadcast();
59 m_communication
= NULL
;
61 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
62 delete m_busDevices
[iPtr
];
65 bool CCECProcessor::Start(void)
67 CLockObject
lock(&m_mutex
);
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");
82 if (!m_startCondition
.Wait(&m_mutex
))
84 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
90 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
95 void *CCECProcessor::Process(void)
98 CLockObject
lock(&m_mutex
);
99 m_controller
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
100 m_startCondition
.Signal();
104 CCECAdapterMessage msg
;
105 CCECAdapterMessagePtr msgPtr
;
107 m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
111 bool bParseFrame(false);
116 CLockObject
lock(&m_mutex
);
117 if (m_frameBuffer
.Pop(msgPtr
))
118 bParseFrame
= ParseMessage(msgPtr
);
119 else if (m_communication
->IsOpen() && m_communication
->Read(msg
, 50))
121 msgPtr
= CCECAdapterMessagePtr(new CCECAdapterMessage(msg
));
122 bParseFrame
= ParseMessage(msgPtr
);
125 bParseFrame
&= !IsStopped();
127 command
= m_currentframe
;
131 ParseCommand(command
);
133 m_controller
->CheckKeypressTimeout();
135 for (unsigned int iDevicePtr
= 0; iDevicePtr
< 16; iDevicePtr
++)
136 m_busDevices
[iDevicePtr
]->PollVendorId();
145 bool CCECProcessor::SetActiveView(void)
150 return m_busDevices
[m_iLogicalAddress
]->BroadcastActiveView();
153 bool CCECProcessor::SetInactiveView(void)
158 return m_busDevices
[m_iLogicalAddress
]->BroadcastInactiveView();
161 void CCECProcessor::LogOutput(const cec_command
&data
)
164 strTx
.Format("<< %02x:%02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
, (uint8_t)data
.opcode
);
166 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
167 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
168 m_controller
->AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
171 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
173 if (m_iLogicalAddress
!= iLogicalAddress
)
176 strLog
.Format("<< setting logical address to %1x", iLogicalAddress
);
177 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
179 m_iLogicalAddress
= iLogicalAddress
;
180 return m_communication
&& m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
186 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
)
188 m_busDevices
[m_iLogicalAddress
]->SetPhysicalAddress(iPhysicalAddress
);
189 return m_busDevices
[m_iLogicalAddress
]->BroadcastActiveView();
192 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
195 strLog
.Format("== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
196 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
198 m_bMonitor
= bEnable
;
200 return m_communication
&& m_communication
->SetAckMask(0);
202 return m_communication
&& m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
205 cec_version
CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress
)
207 return m_busDevices
[iAddress
]->GetCecVersion();
210 bool CCECProcessor::Transmit(const cec_command
&data
)
215 CCECAdapterMessagePtr
output(new CCECAdapterMessage(data
));
217 CLockObject
lock(&m_mutex
);
219 CLockObject
msgLock(&output
->mutex
);
220 if (!m_communication
|| !m_communication
->Write(output
))
224 output
->condition
.Wait(&output
->mutex
);
225 if (output
->state
!= ADAPTER_MESSAGE_STATE_SENT
)
227 m_controller
->AddLog(CEC_LOG_ERROR
, "command was not sent");
232 if (data
.ack_timeout
> 0)
235 if ((bReturn
= WaitForAck(&bError
, output
->size(), data
.ack_timeout
)) == false)
236 m_controller
->AddLog(CEC_LOG_ERROR
, "did not receive ack");
247 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, ECecAbortReason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
249 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
252 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_FEATURE_ABORT
);
253 command
.parameters
.push_back((uint8_t)opcode
);
254 command
.parameters
.push_back((uint8_t)reason
);
259 bool CCECProcessor::WaitForAck(bool *bError
, uint8_t iLength
, uint32_t iTimeout
/* = 1000 */)
261 bool bTransmitSucceeded
= false;
262 uint8_t iPacketsLeft(iLength
/ 4);
265 int64_t iNow
= GetTimeMs();
266 int64_t iTargetTime
= iNow
+ (uint64_t) iTimeout
;
268 while (!bTransmitSucceeded
&& !*bError
&& (iTimeout
== 0 || iNow
< iTargetTime
))
270 CCECAdapterMessage msg
;
272 if (!m_communication
->Read(msg
, iTimeout
> 0 ? (int32_t)(iTargetTime
- iNow
) : 1000))
278 switch(msg
.message())
280 case MSGCODE_TIMEOUT_ERROR
:
281 case MSGCODE_HIGH_ERROR
:
282 case MSGCODE_LOW_ERROR
:
285 if (msg
.message() == MSGCODE_TIMEOUT_ERROR
)
286 logStr
= "MSGCODE_TIMEOUT";
287 else if (msg
.message() == MSGCODE_HIGH_ERROR
)
288 logStr
= "MSGCODE_HIGH_ERROR";
290 logStr
= "MSGCODE_LOW_ERROR";
292 int iLine
= (msg
.size() >= 3) ? (msg
[1] << 8) | (msg
[2]) : 0;
293 uint32_t iTime
= (msg
.size() >= 7) ? (msg
[3] << 24) | (msg
[4] << 16) | (msg
[5] << 8) | (msg
[6]) : 0;
294 logStr
.AppendFormat(" line:%i", iLine
);
295 logStr
.AppendFormat(" time:%u", iTime
);
296 m_controller
->AddLog(CEC_LOG_WARNING
, logStr
.c_str());
300 case MSGCODE_COMMAND_ACCEPTED
:
301 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_COMMAND_ACCEPTED");
304 case MSGCODE_TRANSMIT_SUCCEEDED
:
305 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_TRANSMIT_SUCCEEDED");
306 bTransmitSucceeded
= (iPacketsLeft
== 0);
307 *bError
= !bTransmitSucceeded
;
309 case MSGCODE_RECEIVE_FAILED
:
310 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_RECEIVE_FAILED");
313 case MSGCODE_COMMAND_REJECTED
:
314 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_COMMAND_REJECTED");
317 case MSGCODE_TRANSMIT_FAILED_LINE
:
318 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_LINE");
321 case MSGCODE_TRANSMIT_FAILED_ACK
:
322 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_ACK");
325 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
326 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
329 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
330 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
334 CCECAdapterMessagePtr msgPtr
= CCECAdapterMessagePtr(new CCECAdapterMessage(msg
));
335 m_frameBuffer
.Push(msgPtr
);
342 return bTransmitSucceeded
&& !*bError
;
345 bool CCECProcessor::ParseMessage(CCECAdapterMessagePtr msg
)
354 switch(msg
->message())
356 case MSGCODE_NOTHING
:
357 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_NOTHING");
359 case MSGCODE_TIMEOUT_ERROR
:
360 case MSGCODE_HIGH_ERROR
:
361 case MSGCODE_LOW_ERROR
:
363 if (msg
->message() == MSGCODE_TIMEOUT_ERROR
)
364 logStr
= "MSGCODE_TIMEOUT";
365 else if (msg
->message() == MSGCODE_HIGH_ERROR
)
366 logStr
= "MSGCODE_HIGH_ERROR";
368 logStr
= "MSGCODE_LOW_ERROR";
370 int iLine
= (msg
->size() >= 3) ? (msg
->at(1) << 8) | (msg
->at(2)) : 0;
371 uint32_t iTime
= (msg
->size() >= 7) ? (msg
->at(3) << 24) | (msg
->at(4) << 16) | (msg
->at(5) << 8) | (msg
->at(6)) : 0;
372 logStr
.AppendFormat(" line:%i", iLine
);
373 logStr
.AppendFormat(" time:%u", iTime
);
374 m_controller
->AddLog(CEC_LOG_WARNING
, logStr
.c_str());
377 case MSGCODE_FRAME_START
:
379 logStr
= "MSGCODE_FRAME_START";
380 m_currentframe
.clear();
381 if (msg
->size() >= 2)
383 logStr
.AppendFormat(" initiator:%u destination:%u ack:%s %s", msg
->initiator(), msg
->destination(), msg
->ack() ? "high" : "low", msg
->eom() ? "eom" : "");
384 m_currentframe
.initiator
= msg
->initiator();
385 m_currentframe
.destination
= msg
->destination();
386 m_currentframe
.ack
= msg
->ack();
387 m_currentframe
.eom
= msg
->eom();
389 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
392 case MSGCODE_FRAME_DATA
:
394 logStr
= "MSGCODE_FRAME_DATA";
395 if (msg
->size() >= 2)
397 uint8_t iData
= msg
->at(1);
398 logStr
.AppendFormat(" %02x", iData
);
399 m_currentframe
.push_back(iData
);
400 m_currentframe
.eom
= msg
->eom();
402 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
407 case MSGCODE_COMMAND_ACCEPTED
:
408 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_COMMAND_ACCEPTED");
410 case MSGCODE_TRANSMIT_SUCCEEDED
:
411 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_TRANSMIT_SUCCEEDED");
413 case MSGCODE_RECEIVE_FAILED
:
414 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_RECEIVE_FAILED");
416 case MSGCODE_COMMAND_REJECTED
:
417 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_COMMAND_REJECTED");
419 case MSGCODE_TRANSMIT_FAILED_LINE
:
420 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_LINE");
422 case MSGCODE_TRANSMIT_FAILED_ACK
:
423 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_ACK");
425 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
426 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
428 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
429 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
438 void CCECProcessor::ParseCommand(cec_command
&command
)
441 dataStr
.Format(">> %1x%1x:%02x", command
.initiator
, command
.destination
, command
.opcode
);
442 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
443 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
444 m_controller
->AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
447 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
450 uint16_t CCECProcessor::GetPhysicalAddress(void) const
452 return m_busDevices
[m_iLogicalAddress
]->GetPhysicalAddress();
455 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode
)
457 m_controller
->SetCurrentButton(iButtonCode
);
460 void CCECProcessor::AddCommand(const cec_command
&command
)
462 m_controller
->AddCommand(command
);
465 void CCECProcessor::AddKey(void)
467 m_controller
->AddKey();
470 void CCECProcessor::AddLog(cec_log_level level
, const CStdString
&strMessage
)
472 m_controller
->AddLog(level
, strMessage
);