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
),
50 m_bLogicalAddressSet(false)
52 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
53 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, iPtr
== iLogicalAddress
? iPhysicalAddress
: 0);
56 CCECProcessor::~CCECProcessor(void)
58 m_startCondition
.Broadcast();
60 m_communication
= NULL
;
62 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
63 delete m_busDevices
[iPtr
];
66 bool CCECProcessor::Start(void)
68 CLockObject
lock(&m_mutex
);
69 if (!m_communication
|| !m_communication
->IsOpen())
71 m_controller
->AddLog(CEC_LOG_ERROR
, "connection is closed");
75 if (!SetLogicalAddress(m_iLogicalAddress
))
77 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the logical address");
83 if (!m_startCondition
.Wait(&m_mutex
))
85 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
91 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
96 void *CCECProcessor::Process(void)
99 CLockObject
lock(&m_mutex
);
100 m_controller
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
101 m_startCondition
.Signal();
105 CCECAdapterMessage msg
;
106 CCECAdapterMessagePtr msgPtr
;
108 m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
112 bool bParseFrame(false);
117 CLockObject
lock(&m_mutex
);
118 if (m_frameBuffer
.Pop(msgPtr
))
119 bParseFrame
= ParseMessage(msgPtr
);
120 else if (m_communication
->IsOpen() && m_communication
->Read(msg
, 50))
122 msgPtr
= CCECAdapterMessagePtr(new CCECAdapterMessage(msg
));
123 bParseFrame
= ParseMessage(msgPtr
);
126 bParseFrame
&= !IsStopped();
128 command
= m_currentframe
;
132 ParseCommand(command
);
134 m_controller
->CheckKeypressTimeout();
136 for (unsigned int iDevicePtr
= 0; iDevicePtr
< 16; iDevicePtr
++)
137 m_busDevices
[iDevicePtr
]->PollVendorId();
146 bool CCECProcessor::SetActiveView(void)
151 if (m_iLogicalAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[m_iLogicalAddress
])
152 return m_busDevices
[m_iLogicalAddress
]->BroadcastActiveView();
156 bool CCECProcessor::SetInactiveView(void)
161 if (m_iLogicalAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[m_iLogicalAddress
])
162 return m_busDevices
[m_iLogicalAddress
]->BroadcastInactiveView();
166 void CCECProcessor::LogOutput(const cec_command
&data
)
169 strTx
.Format("<< %02x:%02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
, (uint8_t)data
.opcode
);
171 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
172 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
173 m_controller
->AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
176 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
/* = CECDEVICE_UNKNOWN */)
178 if (iLogicalAddress
!= CECDEVICE_UNKNOWN
)
181 strLog
.Format("<< setting logical address to %1x", iLogicalAddress
);
182 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
183 m_iLogicalAddress
= iLogicalAddress
;
184 m_bLogicalAddressSet
= false;
187 if (!m_bLogicalAddressSet
&& m_iLogicalAddress
!= CECDEVICE_UNKNOWN
)
188 m_bLogicalAddressSet
= m_communication
&& m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
190 return m_bLogicalAddressSet
;
193 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
)
195 if (m_iLogicalAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[m_iLogicalAddress
])
197 m_busDevices
[m_iLogicalAddress
]->SetPhysicalAddress(iPhysicalAddress
);
198 return m_busDevices
[m_iLogicalAddress
]->BroadcastActiveView();
203 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
206 strLog
.Format("== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
207 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
209 m_bMonitor
= bEnable
;
211 return m_communication
&& m_communication
->SetAckMask(0);
213 return m_communication
&& m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
216 cec_version
CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress
)
218 return m_busDevices
[iAddress
]->GetCecVersion();
221 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
223 if (m_busDevices
[iAddress
])
225 *language
= m_busDevices
[iAddress
]->GetMenuLanguage();
226 return (strcmp(language
->language
, "???") == 0);
231 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress
)
233 if (m_busDevices
[iAddress
])
234 return m_busDevices
[iAddress
]->GetVendorId();
238 cec_power_status
CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress
)
240 if (m_busDevices
[iAddress
])
241 return m_busDevices
[iAddress
]->GetPowerStatus();
242 return CEC_POWER_STATUS_UNKNOWN
;
245 bool CCECProcessor::Transmit(const cec_command
&data
)
252 CCECAdapterMessagePtr
output(new CCECAdapterMessage(data
));
254 CLockObject
lock(&m_mutex
);
256 CLockObject
msgLock(&output
->mutex
);
257 if (!m_communication
|| !m_communication
->Write(output
))
261 output
->condition
.Wait(&output
->mutex
, 1000);
262 if (output
->state
!= ADAPTER_MESSAGE_STATE_SENT
)
264 m_controller
->AddLog(CEC_LOG_ERROR
, "command was not sent");
269 if (data
.ack_timeout
> 0)
272 if ((bReturn
= WaitForAck(&bError
, output
->size(), data
.ack_timeout
)) == false)
273 m_controller
->AddLog(CEC_LOG_ERROR
, "did not receive ack");
284 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, ECecAbortReason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
286 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
289 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_FEATURE_ABORT
);
290 command
.parameters
.push_back((uint8_t)opcode
);
291 command
.parameters
.push_back((uint8_t)reason
);
296 bool CCECProcessor::WaitForAck(bool *bError
, uint8_t iLength
, uint32_t iTimeout
/* = 1000 */)
298 bool bTransmitSucceeded
= false;
299 uint8_t iPacketsLeft(iLength
/ 4);
302 int64_t iNow
= GetTimeMs();
303 int64_t iTargetTime
= iNow
+ (uint64_t) iTimeout
;
305 while (!bTransmitSucceeded
&& !*bError
&& (iTimeout
== 0 || iNow
< iTargetTime
))
307 CCECAdapterMessage msg
;
309 if (!m_communication
->Read(msg
, iTimeout
> 0 ? (int32_t)(iTargetTime
- iNow
) : 1000))
315 switch(msg
.message())
317 case MSGCODE_TIMEOUT_ERROR
:
318 case MSGCODE_HIGH_ERROR
:
319 case MSGCODE_LOW_ERROR
:
322 if (msg
.message() == MSGCODE_TIMEOUT_ERROR
)
323 logStr
= "MSGCODE_TIMEOUT";
324 else if (msg
.message() == MSGCODE_HIGH_ERROR
)
325 logStr
= "MSGCODE_HIGH_ERROR";
327 logStr
= "MSGCODE_LOW_ERROR";
329 int iLine
= (msg
.size() >= 3) ? (msg
[1] << 8) | (msg
[2]) : 0;
330 uint32_t iTime
= (msg
.size() >= 7) ? (msg
[3] << 24) | (msg
[4] << 16) | (msg
[5] << 8) | (msg
[6]) : 0;
331 logStr
.AppendFormat(" line:%i", iLine
);
332 logStr
.AppendFormat(" time:%u", iTime
);
333 m_controller
->AddLog(CEC_LOG_WARNING
, logStr
.c_str());
337 case MSGCODE_COMMAND_ACCEPTED
:
338 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_COMMAND_ACCEPTED");
341 case MSGCODE_TRANSMIT_SUCCEEDED
:
342 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_TRANSMIT_SUCCEEDED");
343 bTransmitSucceeded
= (iPacketsLeft
== 0);
344 *bError
= !bTransmitSucceeded
;
346 case MSGCODE_RECEIVE_FAILED
:
347 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_RECEIVE_FAILED");
350 case MSGCODE_COMMAND_REJECTED
:
351 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_COMMAND_REJECTED");
354 case MSGCODE_TRANSMIT_FAILED_LINE
:
355 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_LINE");
358 case MSGCODE_TRANSMIT_FAILED_ACK
:
359 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_ACK");
362 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
363 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
366 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
367 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
371 CCECAdapterMessagePtr msgPtr
= CCECAdapterMessagePtr(new CCECAdapterMessage(msg
));
372 m_frameBuffer
.Push(msgPtr
);
379 return bTransmitSucceeded
&& !*bError
;
382 bool CCECProcessor::ParseMessage(CCECAdapterMessagePtr msg
)
391 switch(msg
->message())
393 case MSGCODE_NOTHING
:
394 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_NOTHING");
396 case MSGCODE_TIMEOUT_ERROR
:
397 case MSGCODE_HIGH_ERROR
:
398 case MSGCODE_LOW_ERROR
:
400 if (msg
->message() == MSGCODE_TIMEOUT_ERROR
)
401 logStr
= "MSGCODE_TIMEOUT";
402 else if (msg
->message() == MSGCODE_HIGH_ERROR
)
403 logStr
= "MSGCODE_HIGH_ERROR";
405 logStr
= "MSGCODE_LOW_ERROR";
407 int iLine
= (msg
->size() >= 3) ? (msg
->at(1) << 8) | (msg
->at(2)) : 0;
408 uint32_t iTime
= (msg
->size() >= 7) ? (msg
->at(3) << 24) | (msg
->at(4) << 16) | (msg
->at(5) << 8) | (msg
->at(6)) : 0;
409 logStr
.AppendFormat(" line:%i", iLine
);
410 logStr
.AppendFormat(" time:%u", iTime
);
411 m_controller
->AddLog(CEC_LOG_WARNING
, logStr
.c_str());
414 case MSGCODE_FRAME_START
:
416 logStr
= "MSGCODE_FRAME_START";
417 m_currentframe
.clear();
418 if (msg
->size() >= 2)
420 logStr
.AppendFormat(" initiator:%u destination:%u ack:%s %s", msg
->initiator(), msg
->destination(), msg
->ack() ? "high" : "low", msg
->eom() ? "eom" : "");
421 m_currentframe
.initiator
= msg
->initiator();
422 m_currentframe
.destination
= msg
->destination();
423 m_currentframe
.ack
= msg
->ack();
424 m_currentframe
.eom
= msg
->eom();
426 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
429 case MSGCODE_FRAME_DATA
:
431 logStr
= "MSGCODE_FRAME_DATA";
432 if (msg
->size() >= 2)
434 uint8_t iData
= msg
->at(1);
435 logStr
.AppendFormat(" %02x", iData
);
436 m_currentframe
.push_back(iData
);
437 m_currentframe
.eom
= msg
->eom();
439 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
444 case MSGCODE_COMMAND_ACCEPTED
:
445 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_COMMAND_ACCEPTED");
447 case MSGCODE_TRANSMIT_SUCCEEDED
:
448 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_TRANSMIT_SUCCEEDED");
450 case MSGCODE_RECEIVE_FAILED
:
451 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_RECEIVE_FAILED");
453 case MSGCODE_COMMAND_REJECTED
:
454 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_COMMAND_REJECTED");
456 case MSGCODE_TRANSMIT_FAILED_LINE
:
457 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_LINE");
459 case MSGCODE_TRANSMIT_FAILED_ACK
:
460 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_ACK");
462 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
463 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
465 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
466 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
475 void CCECProcessor::ParseCommand(cec_command
&command
)
478 dataStr
.Format(">> %1x%1x:%02x", command
.initiator
, command
.destination
, command
.opcode
);
479 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
480 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
481 m_controller
->AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
484 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
487 uint16_t CCECProcessor::GetPhysicalAddress(void) const
489 if (m_iLogicalAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[m_iLogicalAddress
])
490 return m_busDevices
[m_iLogicalAddress
]->GetPhysicalAddress();
494 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode
)
496 m_controller
->SetCurrentButton(iButtonCode
);
499 void CCECProcessor::AddCommand(const cec_command
&command
)
501 m_controller
->AddCommand(command
);
504 void CCECProcessor::AddKey(void)
506 m_controller
->AddKey();
509 void CCECProcessor::AddLog(cec_log_level level
, const CStdString
&strMessage
)
511 m_controller
->AddLog(level
, strMessage
);