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
;
107 m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
111 bool bParseFrame(false);
116 CLockObject
lock(&m_mutex
);
117 CCECAdapterMessagePtr msgPtr
;
118 if (m_frameBuffer
.Pop(msgPtr
))
119 bParseFrame
= ParseMessage(*msgPtr
.get());
120 else if (m_communication
->IsOpen() && m_communication
->Read(msg
, 50))
121 bParseFrame
= ParseMessage(msg
);
123 bParseFrame
&= !IsStopped();
125 command
= m_currentframe
;
129 ParseCommand(command
);
131 m_controller
->CheckKeypressTimeout();
133 for (unsigned int iDevicePtr
= 0; iDevicePtr
< 16; iDevicePtr
++)
134 m_busDevices
[iDevicePtr
]->PollVendorId();
143 bool CCECProcessor::SetActiveView(void)
148 if (m_iLogicalAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[m_iLogicalAddress
])
149 return m_busDevices
[m_iLogicalAddress
]->BroadcastActiveView();
153 bool CCECProcessor::SetInactiveView(void)
158 if (m_iLogicalAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[m_iLogicalAddress
])
159 return m_busDevices
[m_iLogicalAddress
]->BroadcastInactiveView();
163 void CCECProcessor::LogOutput(const cec_command
&data
)
166 strTx
.Format("<< %02x:%02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
, (uint8_t)data
.opcode
);
168 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
169 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
170 m_controller
->AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
173 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
/* = CECDEVICE_UNKNOWN */)
175 if (iLogicalAddress
!= CECDEVICE_UNKNOWN
)
178 strLog
.Format("<< setting logical address to %1x", iLogicalAddress
);
179 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
180 m_iLogicalAddress
= iLogicalAddress
;
181 m_bLogicalAddressSet
= false;
184 if (!m_bLogicalAddressSet
&& m_iLogicalAddress
!= CECDEVICE_UNKNOWN
)
185 m_bLogicalAddressSet
= m_communication
&& m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
187 return m_bLogicalAddressSet
;
190 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
)
192 if (m_iLogicalAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[m_iLogicalAddress
])
194 m_busDevices
[m_iLogicalAddress
]->SetPhysicalAddress(iPhysicalAddress
);
195 return m_busDevices
[m_iLogicalAddress
]->BroadcastActiveView();
200 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
203 strLog
.Format("== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
204 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
206 m_bMonitor
= bEnable
;
208 return m_communication
&& m_communication
->SetAckMask(0);
210 return m_communication
&& m_communication
->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
213 cec_version
CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress
)
215 return m_busDevices
[iAddress
]->GetCecVersion();
218 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
220 if (m_busDevices
[iAddress
])
222 *language
= m_busDevices
[iAddress
]->GetMenuLanguage();
223 return (strcmp(language
->language
, "???") == 0);
228 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress
)
230 if (m_busDevices
[iAddress
])
231 return m_busDevices
[iAddress
]->GetVendorId();
235 cec_power_status
CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress
)
237 if (m_busDevices
[iAddress
])
238 return m_busDevices
[iAddress
]->GetPowerStatus();
239 return CEC_POWER_STATUS_UNKNOWN
;
242 bool CCECProcessor::Transmit(const cec_command
&data
)
249 CCECAdapterMessagePtr
output(new CCECAdapterMessage(data
));
251 CLockObject
lock(&m_mutex
);
253 CLockObject
msgLock(&output
->mutex
);
254 if (!m_communication
|| !m_communication
->Write(output
))
258 output
->condition
.Wait(&output
->mutex
, 1000);
259 if (output
->state
!= ADAPTER_MESSAGE_STATE_SENT
)
261 m_controller
->AddLog(CEC_LOG_ERROR
, "command was not sent");
266 if (data
.ack_timeout
> 0)
269 if ((bReturn
= WaitForAck(&bError
, output
->size(), data
.ack_timeout
)) == false)
270 m_controller
->AddLog(CEC_LOG_ERROR
, "did not receive ack");
281 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, ECecAbortReason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
283 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
286 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_FEATURE_ABORT
);
287 command
.parameters
.push_back((uint8_t)opcode
);
288 command
.parameters
.push_back((uint8_t)reason
);
293 bool CCECProcessor::WaitForAck(bool *bError
, uint8_t iLength
, uint32_t iTimeout
/* = 1000 */)
295 bool bTransmitSucceeded
= false;
296 uint8_t iPacketsLeft(iLength
/ 4);
299 int64_t iNow
= GetTimeMs();
300 int64_t iTargetTime
= iNow
+ (uint64_t) iTimeout
;
302 while (!bTransmitSucceeded
&& !*bError
&& (iTimeout
== 0 || iNow
< iTargetTime
))
304 CCECAdapterMessage msg
;
306 if (!m_communication
->Read(msg
, iTimeout
> 0 ? (int32_t)(iTargetTime
- iNow
) : 1000))
312 switch(msg
.message())
314 case MSGCODE_TIMEOUT_ERROR
:
315 case MSGCODE_HIGH_ERROR
:
316 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());
334 case MSGCODE_COMMAND_ACCEPTED
:
335 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_COMMAND_ACCEPTED");
338 case MSGCODE_TRANSMIT_SUCCEEDED
:
339 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_TRANSMIT_SUCCEEDED");
340 bTransmitSucceeded
= (iPacketsLeft
== 0);
341 *bError
= !bTransmitSucceeded
;
343 case MSGCODE_RECEIVE_FAILED
:
344 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_RECEIVE_FAILED");
347 case MSGCODE_COMMAND_REJECTED
:
348 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_COMMAND_REJECTED");
351 case MSGCODE_TRANSMIT_FAILED_LINE
:
352 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_LINE");
355 case MSGCODE_TRANSMIT_FAILED_ACK
:
356 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_ACK");
359 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
360 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
363 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
364 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
368 CCECAdapterMessagePtr msgPtr
= CCECAdapterMessagePtr(new CCECAdapterMessage(msg
));
369 m_frameBuffer
.Push(msgPtr
);
376 return bTransmitSucceeded
&& !*bError
;
379 bool CCECProcessor::ParseMessage(const CCECAdapterMessage
&msg
)
388 switch(msg
.message())
390 case MSGCODE_NOTHING
:
391 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_NOTHING");
393 case MSGCODE_TIMEOUT_ERROR
:
394 case MSGCODE_HIGH_ERROR
:
395 case MSGCODE_LOW_ERROR
:
397 if (msg
.message() == MSGCODE_TIMEOUT_ERROR
)
398 logStr
= "MSGCODE_TIMEOUT";
399 else if (msg
.message() == MSGCODE_HIGH_ERROR
)
400 logStr
= "MSGCODE_HIGH_ERROR";
402 logStr
= "MSGCODE_LOW_ERROR";
404 int iLine
= (msg
.size() >= 3) ? (msg
[1] << 8) | (msg
[2]) : 0;
405 uint32_t iTime
= (msg
.size() >= 7) ? (msg
[3] << 24) | (msg
[4] << 16) | (msg
[5] << 8) | (msg
[6]) : 0;
406 logStr
.AppendFormat(" line:%i", iLine
);
407 logStr
.AppendFormat(" time:%u", iTime
);
408 m_controller
->AddLog(CEC_LOG_WARNING
, logStr
.c_str());
411 case MSGCODE_FRAME_START
:
413 logStr
= "MSGCODE_FRAME_START";
414 m_currentframe
.clear();
417 logStr
.AppendFormat(" initiator:%1x destination:%1x ack:%s %s", msg
.initiator(), msg
.destination(), msg
.ack() ? "high" : "low", msg
.eom() ? "eom" : "");
418 m_currentframe
.initiator
= msg
.initiator();
419 m_currentframe
.destination
= msg
.destination();
420 m_currentframe
.ack
= msg
.ack();
421 m_currentframe
.eom
= msg
.eom();
423 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
426 case MSGCODE_FRAME_DATA
:
428 logStr
= "MSGCODE_FRAME_DATA";
431 uint8_t iData
= msg
[1];
432 logStr
.AppendFormat(" %02x", iData
);
433 m_currentframe
.push_back(iData
);
434 m_currentframe
.eom
= msg
.eom();
436 m_controller
->AddLog(CEC_LOG_DEBUG
, logStr
.c_str());
441 case MSGCODE_COMMAND_ACCEPTED
:
442 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_COMMAND_ACCEPTED");
444 case MSGCODE_TRANSMIT_SUCCEEDED
:
445 m_controller
->AddLog(CEC_LOG_DEBUG
, "MSGCODE_TRANSMIT_SUCCEEDED");
447 case MSGCODE_RECEIVE_FAILED
:
448 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_RECEIVE_FAILED");
450 case MSGCODE_COMMAND_REJECTED
:
451 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_COMMAND_REJECTED");
453 case MSGCODE_TRANSMIT_FAILED_LINE
:
454 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_LINE");
456 case MSGCODE_TRANSMIT_FAILED_ACK
:
457 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_ACK");
459 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
460 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
462 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
463 m_controller
->AddLog(CEC_LOG_WARNING
, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
472 void CCECProcessor::ParseCommand(cec_command
&command
)
475 dataStr
.Format(">> %1x%1x:%02x", command
.initiator
, command
.destination
, command
.opcode
);
476 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
477 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
478 m_controller
->AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
481 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
484 uint16_t CCECProcessor::GetPhysicalAddress(void) const
486 if (m_iLogicalAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[m_iLogicalAddress
])
487 return m_busDevices
[m_iLogicalAddress
]->GetPhysicalAddress();
491 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode
)
493 m_controller
->SetCurrentButton(iButtonCode
);
496 void CCECProcessor::AddCommand(const cec_command
&command
)
498 m_controller
->AddCommand(command
);
501 void CCECProcessor::AddKey(void)
503 m_controller
->AddKey();
506 void CCECProcessor::AddLog(cec_log_level level
, const CStdString
&strMessage
)
508 m_controller
->AddLog(level
, strMessage
);