7#include "adapter_interface.h"
8#include "properties_interface.h"
9#include "objectmanager_interface.h"
13Q_DECLARE_LOGGING_CATEGORY(QT_NFC_NEARD)
14Q_LOGGING_CATEGORY(QT_NFC_NEARD,
"qt.nfc.neard")
16using namespace QtNfcPrivate;
21 m_neardHelper(NeardHelper::instance())
23 QDBusPendingReply<ManagedObjectList> reply = m_neardHelper->dbusObjectManager()->GetManagedObjects();
24 reply.waitForFinished();
25 if (reply.isError()) {
26 qCWarning(QT_NFC_NEARD) <<
"Error getting managed objects";
31 const QList<QDBusObjectPath> paths = reply.value().keys();
32 for (
const QDBusObjectPath &path : paths) {
33 const InterfaceList ifaceList = reply.value().value(path);
34 const QStringList ifaces = ifaceList.keys();
35 for (
const QString &iface : ifaces) {
36 if (iface == QStringLiteral(
"org.neard.Adapter")) {
38 m_adapterPath = path.path();
39 qCDebug(QT_NFC_NEARD) <<
"org.neard.Adapter found for path" << m_adapterPath;
49 qCWarning(QT_NFC_NEARD) <<
"no adapter found, neard daemon running?";
51 connect(m_neardHelper, &NeardHelper::tagFound,
52 this, &QNearFieldManagerPrivateImpl::handleTagFound);
53 connect(m_neardHelper, &NeardHelper::tagRemoved,
54 this, &QNearFieldManagerPrivateImpl::handleTagRemoved);
60 stopTargetDetection();
65 if (!m_neardHelper->dbusObjectManager()->isValid() || m_adapterPath.isNull()) {
66 qCWarning(QT_NFC_NEARD) <<
"dbus object manager invalid or adapter path invalid";
70 QDBusPendingReply<ManagedObjectList> reply = m_neardHelper->dbusObjectManager()->GetManagedObjects();
71 reply.waitForFinished();
72 if (reply.isError()) {
73 qCWarning(QT_NFC_NEARD) <<
"error getting managed objects";
77 const QList<QDBusObjectPath> paths = reply.value().keys();
78 for (
const QDBusObjectPath &path : paths) {
79 if (m_adapterPath == path.path())
88 if (m_adapterPath.isEmpty()) {
89 qCWarning(QT_NFC_NEARD) <<
"no adapter found, neard daemon running?";
93 if (!m_neardHelper->dbusObjectManager()->isValid()) {
94 qCWarning(QT_NFC_NEARD) <<
"dbus object manager invalid or adapter path invalid";
98 return accessMethod == QNearFieldTarget::NdefAccess;
103 qCDebug(QT_NFC_NEARD) <<
"starting target detection";
104 if (!isEnabled() || accessMethod != QNearFieldTarget::NdefAccess)
107 OrgFreedesktopDBusPropertiesInterface dbusProperties(QStringLiteral(
"org.neard"),
109 QDBusConnection::systemBus());
111 if (!dbusProperties.isValid()) {
112 qCWarning(QT_NFC_NEARD) <<
"dbus property interface invalid";
117 QDBusPendingReply<QDBusVariant> replyPolling = dbusProperties.Get(QStringLiteral(
"org.neard.Adapter"),
118 QStringLiteral(
"Polling"));
119 replyPolling.waitForFinished();
120 if (!replyPolling.isError()) {
121 if (replyPolling.value().variant().toBool()) {
122 qCDebug(QT_NFC_NEARD) <<
"adapter is already polling";
126 qCWarning(QT_NFC_NEARD) <<
"error getting 'Polling' state from property interface";
131 QDBusPendingReply<QDBusVariant> replyPowered = dbusProperties.Get(QStringLiteral(
"org.neard.Adapter"),
132 QStringLiteral(
"Powered"));
133 replyPowered.waitForFinished();
134 if (!replyPowered.isError()) {
135 if (replyPowered.value().variant().toBool()) {
136 qCDebug(QT_NFC_NEARD) <<
"adapter is already powered";
138 QDBusPendingReply<QDBusVariant> replyTryPowering = dbusProperties.Set(QStringLiteral(
"org.neard.Adapter"),
139 QStringLiteral(
"Powered"),
141 replyTryPowering.waitForFinished();
142 if (!replyTryPowering.isError()) {
143 qCDebug(QT_NFC_NEARD) <<
"powering adapter";
147 qCWarning(QT_NFC_NEARD) <<
"error getting 'Powered' state from property interface";
152 OrgNeardAdapterInterface neardAdapter(QStringLiteral(
"org.neard"),
154 QDBusConnection::systemBus());
157 QDBusPendingReply<> replyPollLoop = neardAdapter.StartPollLoop(QStringLiteral(
"Initiator"));
158 replyPollLoop.waitForFinished();
159 if (replyPollLoop.isError()) {
160 qCWarning(QT_NFC_NEARD) <<
"error when starting polling";
163 qCDebug(QT_NFC_NEARD) <<
"successfully started polling";
171 qCDebug(QT_NFC_NEARD) <<
"stopping target detection";
175 OrgFreedesktopDBusPropertiesInterface dbusProperties(QStringLiteral(
"org.neard"),
177 QDBusConnection::systemBus());
179 if (!dbusProperties.isValid()) {
180 qCWarning(QT_NFC_NEARD) <<
"dbus property interface invalid";
185 QDBusPendingReply<QDBusVariant> replyPolling = dbusProperties.Get(QStringLiteral(
"org.neard.Adapter"),
186 QStringLiteral(
"Polling"));
187 replyPolling.waitForFinished();
188 if (!replyPolling.isError()) {
189 if (replyPolling.value().variant().toBool()) {
191 OrgNeardAdapterInterface neardAdapter(QStringLiteral(
"org.neard"),
193 QDBusConnection::systemBus());
195 QDBusPendingReply<> replyStopPolling = neardAdapter.StopPollLoop();
196 replyStopPolling.waitForFinished();
197 if (replyStopPolling.isError())
198 qCWarning(QT_NFC_NEARD) <<
"error when stopping polling";
200 qCDebug(QT_NFC_NEARD) <<
"successfully stopped polling";
202 qCDebug(QT_NFC_NEARD) <<
"already stopped polling";
205 qCWarning(QT_NFC_NEARD) <<
"error getting 'Polling' state from property interface";
211 auto priv =
new QNearFieldTargetPrivateImpl(
this, path);
212 auto nfTag =
new QNearFieldTarget(priv,
this);
213 m_activeTags.insert(path.path(), nfTag);
214 emit targetDetected(nfTag);
219 const QString adapterPath = path.path();
220 if (m_activeTags.contains(adapterPath)) {
221 QNearFieldTarget *nfTag = m_activeTags.value(adapterPath);
222 m_activeTags.remove(adapterPath);
223 emit targetLost(nfTag);
bool isEnabled() const override
QNearFieldManagerPrivateImpl()
bool isSupported(QNearFieldTarget::AccessMethod accessMethod) const override
bool startTargetDetection(QNearFieldTarget::AccessMethod accessMethod) override
~QNearFieldManagerPrivateImpl()
void stopTargetDetection(const QString &errorMessage) override