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 (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");
76 if (!m_startCondition
.Wait(&m_mutex
))
78 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
84 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
89 void *CCECProcessor::Process(void)
92 CCECAdapterMessage msg
;
94 SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
97 CLockObject
lock(&m_mutex
);
98 m_controller
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
99 m_startCondition
.Signal();
104 bool bParseFrame(false);
109 CLockObject
lock(&m_mutex
);
110 if (m_communication
->IsOpen() && m_communication
->Read(msg
, 50))
112 m_controller
->AddLog(msg
.is_error() ? CEC_LOG_WARNING
: CEC_LOG_DEBUG
, msg
.ToString());
113 bParseFrame
= ParseMessage(msg
) && !IsStopped();
117 command
= m_currentframe
;
121 ParseCommand(command
);
125 m_controller
->CheckKeypressTimeout();
127 for (unsigned int iDevicePtr
= 0; iDevicePtr
< 16; iDevicePtr
++)
128 m_busDevices
[iDevicePtr
]->PollVendorId();
136 bool CCECProcessor::SetActiveView(void)
141 if (m_iLogicalAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[m_iLogicalAddress
])
142 return m_busDevices
[m_iLogicalAddress
]->BroadcastActiveView();
146 bool CCECProcessor::SetInactiveView(void)
151 if (m_iLogicalAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[m_iLogicalAddress
])
152 return m_busDevices
[m_iLogicalAddress
]->BroadcastInactiveView();
156 void CCECProcessor::LogOutput(const cec_command
&data
)
159 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
161 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
163 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
164 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
165 m_controller
->AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
168 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
170 if (m_iLogicalAddress
!= iLogicalAddress
)
173 strLog
.Format("<< setting logical address to %1x", iLogicalAddress
);
174 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
175 m_iLogicalAddress
= iLogicalAddress
;
176 return SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
182 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
)
184 if (m_iLogicalAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[m_iLogicalAddress
])
186 m_busDevices
[m_iLogicalAddress
]->SetPhysicalAddress(iPhysicalAddress
);
187 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 SetAckMask(0);
202 return SetAckMask(0x1 << (uint8_t)m_iLogicalAddress
);
205 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
207 if (iAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[iAddress
])
208 return m_busDevices
[iAddress
]->PollDevice();
212 cec_version
CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress
)
214 return m_busDevices
[iAddress
]->GetCecVersion();
217 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
219 if (m_busDevices
[iAddress
])
221 *language
= m_busDevices
[iAddress
]->GetMenuLanguage();
222 return (strcmp(language
->language
, "???") != 0);
227 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress
)
229 if (m_busDevices
[iAddress
])
230 return m_busDevices
[iAddress
]->GetVendorId();
234 cec_power_status
CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress
)
236 if (m_busDevices
[iAddress
])
237 return m_busDevices
[iAddress
]->GetPowerStatus();
238 return CEC_POWER_STATUS_UNKNOWN
;
241 bool CCECProcessor::Transmit(const cec_command
&data
)
246 CCECAdapterMessage
*output
= new CCECAdapterMessage(data
);
247 bReturn
= Transmit(output
);
253 bool CCECProcessor::Transmit(CCECAdapterMessage
*output
)
256 CLockObject
lock(&m_mutex
);
258 CLockObject
msgLock(&output
->mutex
);
259 if (!m_communication
|| !m_communication
->Write(output
))
263 output
->condition
.Wait(&output
->mutex
);
264 if (output
->state
!= ADAPTER_MESSAGE_STATE_SENT
)
266 m_controller
->AddLog(CEC_LOG_ERROR
, "command was not sent");
271 if (output
->transmit_timeout
> 0)
273 if ((bReturn
= WaitForTransmitSucceeded(output
->size(), output
->transmit_timeout
)) == false)
274 m_controller
->AddLog(CEC_LOG_ERROR
, "did not receive ack");
283 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, ECecAbortReason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
285 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
288 cec_command::format(command
, m_iLogicalAddress
, address
, CEC_OPCODE_FEATURE_ABORT
);
289 command
.parameters
.push_back((uint8_t)opcode
);
290 command
.parameters
.push_back((uint8_t)reason
);
295 bool CCECProcessor::WaitForTransmitSucceeded(uint8_t iLength
, uint32_t iTimeout
/* = 1000 */)
298 bool bTransmitSucceeded(false);
299 uint8_t iPacketsLeft(iLength
/ 4);
301 int64_t iNow
= GetTimeMs();
302 int64_t iTargetTime
= iNow
+ (uint64_t) iTimeout
;
304 while (!bTransmitSucceeded
&& !bError
&& (iTimeout
== 0 || iNow
< iTargetTime
))
306 CCECAdapterMessage msg
;
308 if (!m_communication
->Read(msg
, iTimeout
> 0 ? (int32_t)(iTargetTime
- iNow
) : 1000))
314 if ((bError
= msg
.is_error()) == false)
316 m_controller
->AddLog(bError
? CEC_LOG_WARNING
: CEC_LOG_DEBUG
, msg
.ToString());
318 switch(msg
.message())
320 case MSGCODE_COMMAND_ACCEPTED
:
321 if (iPacketsLeft
> 0)
324 case MSGCODE_TRANSMIT_SUCCEEDED
:
325 bTransmitSucceeded
= (iPacketsLeft
== 0);
326 bError
= !bTransmitSucceeded
;
336 return bTransmitSucceeded
&& !bError
;
339 bool CCECProcessor::ParseMessage(const CCECAdapterMessage
&msg
)
346 switch(msg
.message())
348 case MSGCODE_FRAME_START
:
350 m_currentframe
.clear();
353 m_currentframe
.initiator
= msg
.initiator();
354 m_currentframe
.destination
= msg
.destination();
355 m_currentframe
.ack
= msg
.ack();
356 m_currentframe
.eom
= msg
.eom();
360 case MSGCODE_FRAME_DATA
:
364 m_currentframe
.push_back(msg
[1]);
365 m_currentframe
.eom
= msg
.eom();
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 if (m_iLogicalAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[m_iLogicalAddress
])
392 return m_busDevices
[m_iLogicalAddress
]->GetPhysicalAddress();
396 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode
)
398 m_controller
->SetCurrentButton(iButtonCode
);
401 void CCECProcessor::AddCommand(const cec_command
&command
)
403 m_controller
->AddCommand(command
);
406 void CCECProcessor::AddKey(cec_keypress
&key
)
408 m_controller
->AddKey(key
);
411 void CCECProcessor::AddKey(void)
413 m_controller
->AddKey();
416 void CCECProcessor::AddLog(cec_log_level level
, const CStdString
&strMessage
)
418 m_controller
->AddLog(level
, strMessage
);
421 bool CCECProcessor::SetAckMask(uint16_t iMask
)
425 strLog
.Format("setting ackmask to %2x", iMask
);
426 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
428 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
430 output
->push_back(MSGSTART
);
431 output
->push_escaped(MSGCODE_SET_ACK_MASK
);
432 output
->push_escaped(iMask
>> 8);
433 output
->push_escaped((uint8_t)iMask
);
434 output
->push_back(MSGEND
);
436 if ((bReturn
= Transmit(output
)) == false)
437 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the ackmask");