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*/) :
46 m_strDeviceName(strDeviceName
),
47 m_communication(serComm
),
48 m_controller(controller
),
51 m_logicalAddresses
.clear();
52 m_logicalAddresses
.set(iLogicalAddress
);
54 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
55 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, iPtr
== iLogicalAddress
? iPhysicalAddress
: 0);
58 CCECProcessor::CCECProcessor(CLibCEC
*controller
, CAdapterCommunication
*serComm
, const char *strDeviceName
, const cec_device_type_list
&types
) :
60 m_strDeviceName(strDeviceName
),
62 m_communication(serComm
),
63 m_controller(controller
),
66 m_logicalAddresses
.clear();
67 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
68 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, 0);
71 CCECProcessor::~CCECProcessor(void)
73 m_startCondition
.Broadcast();
75 m_communication
= NULL
;
77 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
78 delete m_busDevices
[iPtr
];
81 bool CCECProcessor::Start(void)
83 CLockObject
lock(&m_mutex
);
84 if (!m_communication
|| !m_communication
->IsOpen())
86 m_controller
->AddLog(CEC_LOG_ERROR
, "connection is closed");
92 if (!m_startCondition
.Wait(&m_mutex
) || !m_bStarted
)
94 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
100 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
105 bool CCECProcessor::TryLogicalAddress(cec_logical_address address
, const char *strLabel
)
108 strLog
.Format("trying logical address '%s'", strLabel
);
109 AddLog(CEC_LOG_DEBUG
, strLog
);
111 SetAckMask(0x1 << address
);
112 if (!m_busDevices
[address
]->PollDevice(address
))
115 strLog
.Format("using logical address '%s'", strLabel
);
116 AddLog(CEC_LOG_NOTICE
, strLog
);
117 m_logicalAddresses
.set(address
);
120 m_busDevices
[address
]->SetPhysicalAddress(CEC_DEFAULT_PHYSICAL_ADDRESS
);
125 strLog
.Format("logical address '%s' already taken", strLabel
);
126 AddLog(CEC_LOG_DEBUG
, strLog
);
130 bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
132 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'recording device'");
133 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1
, "recording 1") ||
134 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2
, "recording 2") ||
135 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3
, "recording 3");
138 bool CCECProcessor::FindLogicalAddressTuner(void)
140 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'tuner'");
141 return TryLogicalAddress(CECDEVICE_TUNER1
, "tuner 1") ||
142 TryLogicalAddress(CECDEVICE_TUNER2
, "tuner 2") ||
143 TryLogicalAddress(CECDEVICE_TUNER3
, "tuner 3") ||
144 TryLogicalAddress(CECDEVICE_TUNER4
, "tuner 4");
147 bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
149 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'playback device'");
150 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1
, "playback 1") ||
151 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2
, "playback 2") ||
152 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3
, "playback 3");
155 bool CCECProcessor::FindLogicalAddressAudioSystem(void)
157 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'audio'");
158 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM
, "audio");
161 bool CCECProcessor::FindLogicalAddresses(void)
164 m_logicalAddresses
.clear();
167 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
169 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
172 strLog
.Format("%s - device %d: type %d", __FUNCTION__
, iPtr
, m_types
.types
[iPtr
]);
173 AddLog(CEC_LOG_DEBUG
, strLog
);
175 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
176 bReturn
&= FindLogicalAddressRecordingDevice();
177 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_TUNER
)
178 bReturn
&= FindLogicalAddressTuner();
179 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
)
180 bReturn
&= FindLogicalAddressPlaybackDevice();
181 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
182 bReturn
&= FindLogicalAddressAudioSystem();
188 void *CCECProcessor::Process(void)
191 CCECAdapterMessage msg
;
194 if (m_logicalAddresses
.empty() && !FindLogicalAddresses())
196 CLockObject
lock(&m_mutex
);
197 m_controller
->AddLog(CEC_LOG_ERROR
, "could not detect our logical addressed");
198 m_startCondition
.Signal();
202 SetAckMask(m_logicalAddresses
.ackmask());
205 CLockObject
lock(&m_mutex
);
206 m_controller
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
208 m_startCondition
.Signal();
214 bool bParseFrame(false);
219 CLockObject
lock(&m_mutex
);
220 if (m_communication
->IsOpen() && m_communication
->Read(msg
, 50))
222 m_controller
->AddLog(msg
.is_error() ? CEC_LOG_WARNING
: CEC_LOG_DEBUG
, msg
.ToString());
223 bParseFrame
= ParseMessage(msg
) && !IsStopped();
227 command
= m_currentframe
;
231 ParseCommand(command
);
235 m_controller
->CheckKeypressTimeout();
237 for (unsigned int iDevicePtr
= 0; iDevicePtr
< 16; iDevicePtr
++)
238 m_busDevices
[iDevicePtr
]->PollVendorId();
246 bool CCECProcessor::SetActiveView(void)
251 if (!m_logicalAddresses
.empty() && m_busDevices
[m_logicalAddresses
.primary
])
252 return m_busDevices
[m_logicalAddresses
.primary
]->BroadcastActiveView();
256 bool CCECProcessor::SetInactiveView(void)
261 if (!m_logicalAddresses
.empty() && m_busDevices
[m_logicalAddresses
.primary
])
262 return m_busDevices
[m_logicalAddresses
.primary
]->BroadcastInactiveView();
266 void CCECProcessor::LogOutput(const cec_command
&data
)
269 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
271 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
273 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
274 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
275 m_controller
->AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
278 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
280 if (m_logicalAddresses
.primary
!= iLogicalAddress
)
283 strLog
.Format("<< setting primary logical address to %1x", iLogicalAddress
);
284 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
285 m_logicalAddresses
.primary
= iLogicalAddress
;
286 m_logicalAddresses
.set(iLogicalAddress
);
287 return SetAckMask(m_logicalAddresses
.ackmask());
293 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
)
295 if (!m_logicalAddresses
.empty() && m_busDevices
[m_logicalAddresses
.primary
])
297 m_busDevices
[m_logicalAddresses
.primary
]->SetPhysicalAddress(iPhysicalAddress
);
298 return m_busDevices
[m_logicalAddresses
.primary
]->BroadcastActiveView();
303 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
306 strLog
.Format("== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
307 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
309 m_bMonitor
= bEnable
;
311 return SetAckMask(0);
313 return SetAckMask(m_logicalAddresses
.ackmask());
316 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
318 if (iAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[iAddress
])
319 return m_busDevices
[iAddress
]->PollDevice();
323 cec_version
CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress
)
325 return m_busDevices
[iAddress
]->GetCecVersion();
328 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
330 if (m_busDevices
[iAddress
])
332 *language
= m_busDevices
[iAddress
]->GetMenuLanguage();
333 return (strcmp(language
->language
, "???") != 0);
338 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress
)
340 if (m_busDevices
[iAddress
])
341 return m_busDevices
[iAddress
]->GetVendorId();
345 cec_power_status
CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress
)
347 if (m_busDevices
[iAddress
])
348 return m_busDevices
[iAddress
]->GetPowerStatus();
349 return CEC_POWER_STATUS_UNKNOWN
;
352 bool CCECProcessor::Transmit(const cec_command
&data
)
357 CCECAdapterMessage
*output
= new CCECAdapterMessage(data
);
358 bReturn
= Transmit(output
);
364 bool CCECProcessor::Transmit(CCECAdapterMessage
*output
)
367 CLockObject
lock(&m_mutex
);
369 CLockObject
msgLock(&output
->mutex
);
370 if (!m_communication
|| !m_communication
->Write(output
))
374 output
->condition
.Wait(&output
->mutex
);
375 if (output
->state
!= ADAPTER_MESSAGE_STATE_SENT
)
377 m_controller
->AddLog(CEC_LOG_ERROR
, "command was not sent");
382 if (output
->transmit_timeout
> 0)
384 if ((bReturn
= WaitForTransmitSucceeded(output
->size(), output
->transmit_timeout
)) == false)
385 m_controller
->AddLog(CEC_LOG_ERROR
, "did not receive ack");
394 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, ECecAbortReason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
396 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
400 cec_command::format(command
, m_logicalAddresses
.primary
, address
, CEC_OPCODE_FEATURE_ABORT
);
401 command
.parameters
.push_back((uint8_t)opcode
);
402 command
.parameters
.push_back((uint8_t)reason
);
407 bool CCECProcessor::WaitForTransmitSucceeded(uint8_t iLength
, uint32_t iTimeout
/* = 1000 */)
410 bool bTransmitSucceeded(false);
411 uint8_t iPacketsLeft(iLength
/ 4);
413 int64_t iNow
= GetTimeMs();
414 int64_t iTargetTime
= iNow
+ (uint64_t) iTimeout
;
416 while (!bTransmitSucceeded
&& !bError
&& (iTimeout
== 0 || iNow
< iTargetTime
))
418 CCECAdapterMessage msg
;
420 if (!m_communication
->Read(msg
, iTimeout
> 0 ? (int32_t)(iTargetTime
- iNow
) : 1000))
426 if ((bError
= msg
.is_error()) == false)
428 m_controller
->AddLog(bError
? CEC_LOG_WARNING
: CEC_LOG_DEBUG
, msg
.ToString());
430 switch(msg
.message())
432 case MSGCODE_COMMAND_ACCEPTED
:
433 if (iPacketsLeft
> 0)
436 case MSGCODE_TRANSMIT_SUCCEEDED
:
437 bTransmitSucceeded
= (iPacketsLeft
== 0);
438 bError
= !bTransmitSucceeded
;
448 return bTransmitSucceeded
&& !bError
;
451 bool CCECProcessor::ParseMessage(const CCECAdapterMessage
&msg
)
458 switch(msg
.message())
460 case MSGCODE_FRAME_START
:
462 m_currentframe
.clear();
465 m_currentframe
.initiator
= msg
.initiator();
466 m_currentframe
.destination
= msg
.destination();
467 m_currentframe
.ack
= msg
.ack();
468 m_currentframe
.eom
= msg
.eom();
472 case MSGCODE_FRAME_DATA
:
476 m_currentframe
.push_back(msg
[1]);
477 m_currentframe
.eom
= msg
.eom();
489 void CCECProcessor::ParseCommand(cec_command
&command
)
492 dataStr
.Format(">> %1x%1x:%02x", command
.initiator
, command
.destination
, command
.opcode
);
493 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
494 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
495 m_controller
->AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
498 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
501 uint16_t CCECProcessor::GetPhysicalAddress(void) const
503 if (!m_logicalAddresses
.empty() && m_busDevices
[m_logicalAddresses
.primary
])
504 return m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress();
508 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode
)
510 m_controller
->SetCurrentButton(iButtonCode
);
513 void CCECProcessor::AddCommand(const cec_command
&command
)
515 m_controller
->AddCommand(command
);
518 void CCECProcessor::AddKey(cec_keypress
&key
)
520 m_controller
->AddKey(key
);
523 void CCECProcessor::AddKey(void)
525 m_controller
->AddKey();
528 void CCECProcessor::AddLog(cec_log_level level
, const CStdString
&strMessage
)
530 m_controller
->AddLog(level
, strMessage
);
533 bool CCECProcessor::SetAckMask(uint16_t iMask
)
537 strLog
.Format("setting ackmask to %2x", iMask
);
538 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
540 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
542 output
->push_back(MSGSTART
);
543 output
->push_escaped(MSGCODE_SET_ACK_MASK
);
544 output
->push_escaped(iMask
>> 8);
545 output
->push_escaped((uint8_t)iMask
);
546 output
->push_back(MSGEND
);
548 if ((bReturn
= Transmit(output
)) == false)
549 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the ackmask");