From 0ea4cc4d1e84b01e589e5e19f0323a2c80503219 Mon Sep 17 00:00:00 2001 From: Fabian Bumberger Date: Wed, 25 Jun 2014 22:56:56 +0200 Subject: Clean up neard implementation Besides some code cleanups this patch makes sure to only build the neard implementation if dbus is available. This patch also deals with the possibility that the neard daemon is not running on the system. Change-Id: I8546d34b808d8236e0434e00cf5950b31e20cb72 Reviewed-by: Alex Blasche --- src/nfc/nfc.pro | 19 ++++---- src/nfc/qnearfieldmanager.cpp | 2 + src/nfc/qnearfieldmanager_neard.cpp | 62 +++++++++++++++--------- src/nfc/qnearfieldmanager_neard.h | 89 ---------------------------------- src/nfc/qnearfieldmanager_neard_p.h | 95 +++++++++++++++++++++++++++++++++++++ 5 files changed, 147 insertions(+), 120 deletions(-) delete mode 100644 src/nfc/qnearfieldmanager_neard.h create mode 100644 src/nfc/qnearfieldmanager_neard_p.h (limited to 'src') diff --git a/src/nfc/nfc.pro b/src/nfc/nfc.pro index 3d445415..8342cf2a 100644 --- a/src/nfc/nfc.pro +++ b/src/nfc/nfc.pro @@ -53,8 +53,7 @@ SOURCES += \ qqmlndefrecord.cpp \ qndefnfcsmartposterrecord.cpp \ qnearfieldsharemanager.cpp \ - qnearfieldsharetarget.cpp \ - qnearfieldmanager_neard.cpp + qnearfieldsharetarget.cpp CONFIG(blackberry) { NFC_BACKEND_AVAILABLE = yes @@ -100,23 +99,27 @@ CONFIG(blackberry) { } } -linux { +linux:qtHaveModule(dbus) { NFC_BACKEND_AVAILABLE = yes QT += dbus + DEFINES += NEARD_NFC + PRIVATE_HEADERS += \ qllcpsocket_p_p.h \ qllcpserver_p_p.h \ - qnearfieldmanager_neard.h \ + qnearfieldmanager_neard_p.h \ qnearfieldsharemanagerimpl_p.h \ - qnearfieldsharetargetimpl_p.h + qnearfieldsharetargetimpl_p.h \ + qnearfieldtarget_neard_p.h SOURCES += \ qllcpsocket_p.cpp \ qllcpserver_p.cpp \ qnearfieldsharemanagerimpl_p.cpp \ - qnearfieldsharetargetimpl_p.cpp + qnearfieldsharetargetimpl_p.cpp \ + qnearfieldmanager_neard.cpp include(neard/neard.pri) } @@ -162,6 +165,4 @@ isEmpty(NFC_BACKEND_AVAILABLE) { qnearfieldsharetargetimpl_p.cpp } -HEADERS += $$PUBLIC_HEADERS $$PRIVATE_HEADERS \ - qnearfieldmanager_neard.h \ - qnearfieldtarget_neard_p.h +HEADERS += $$PUBLIC_HEADERS $$PRIVATE_HEADERS diff --git a/src/nfc/qnearfieldmanager.cpp b/src/nfc/qnearfieldmanager.cpp index 88656a6e..0f8c1a8f 100644 --- a/src/nfc/qnearfieldmanager.cpp +++ b/src/nfc/qnearfieldmanager.cpp @@ -46,6 +46,8 @@ #include "qnearfieldmanager_simulator_p.h" #elif defined(QNX_NFC) #include "qnearfieldmanager_qnx_p.h" +#elif defined(NEARD_NFC) +#include "qnearfieldmanager_neard_p.h" #else #include "qnearfieldmanagerimpl_p.h" #endif diff --git a/src/nfc/qnearfieldmanager_neard.cpp b/src/nfc/qnearfieldmanager_neard.cpp index 6bf82236..76ea6635 100644 --- a/src/nfc/qnearfieldmanager_neard.cpp +++ b/src/nfc/qnearfieldmanager_neard.cpp @@ -39,33 +39,41 @@ ** ****************************************************************************/ -#include "qnearfieldmanager_neard.h" -#include "neard/adapter_p.h" -#include "neard/manager_p.h" +#include "qnearfieldmanager_neard_p.h" #include "qnearfieldtarget_neard_p.h" +#include "neard/manager_p.h" +#include "neard/adapter_p.h" + QT_BEGIN_NAMESPACE -// TODO We need a constructor that lets us select a adapter +// TODO We need a constructor that lets us select an adapter QNearFieldManagerPrivateImpl::QNearFieldManagerPrivateImpl() - : m_adapter(0) + : QNearFieldManagerPrivate(), m_adapter(0), m_manager(0) { - // Look for a NFC adapter - OrgNeardManagerInterface manager(QStringLiteral("org.neard"), QStringLiteral("/"), - QDBusConnection::systemBus()); - QDBusPendingReply reply = manager.GetProperties(); + m_manager = new OrgNeardManagerInterface(QStringLiteral("org.neard"), QStringLiteral("/"), + QDBusConnection::systemBus(), this); + if (!m_manager->isValid()) { + qDebug() << "Could not connect to manager" << "Neard daemon running?"; + return; + } + + QDBusPendingReply reply = m_manager->GetProperties(); reply.waitForFinished(); - if (reply.isError()) - qDebug() << "Error getting manager properties." << "Neard daemon running?"; + if (reply.isError()) { + qDebug() << "Error getting manager properties."; + return; + } + // Now we check if the adapter still exists (it might have been unplugged) const QDBusArgument &paths = reply.value().value(QStringLiteral("Adapters")).value(); paths.beginArray(); while (!paths.atEnd()) { QDBusObjectPath path; paths >> path; + // Select the first adapter m_adapterPath = path.path(); - //qDebug() << "path is" << m_adapterPath; break; } paths.endArray(); @@ -78,23 +86,31 @@ QNearFieldManagerPrivateImpl::~QNearFieldManagerPrivateImpl() bool QNearFieldManagerPrivateImpl::isAvailable() const { - OrgNeardManagerInterface manager(QStringLiteral("org.neard"), QStringLiteral("/"), - QDBusConnection::systemBus()); - QDBusPendingReply reply = manager.GetProperties(); + if (!m_manager->isValid() || m_adapterPath.isNull()) { + return false; + } + + QDBusPendingReply reply = m_manager->GetProperties(); reply.waitForFinished(); - if (reply.isError()) - qDebug() << "Error getting manager properties." << "Neard daemon running?"; + if (reply.isError()) { + qDebug() << "Error getting manager properties."; + return false; + } - QDBusArgument paths = reply.value().value(QStringLiteral("Adapters")).value(); + // Now we check if the adapter still exists (it might have been unplugged) + const QDBusArgument &paths = reply.value().value(QStringLiteral("Adapters")).value(); paths.beginArray(); while (!paths.atEnd()) { QDBusObjectPath path; paths >> path; - //Check if the adapter exists - if (path.path() == m_adapterPath) + // Check if the adapter exists + if (path.path() == m_adapterPath) { + paths.endArray(); return true; + } } + paths.endArray(); return false; } @@ -109,7 +125,7 @@ bool QNearFieldManagerPrivateImpl::startTargetDetection() //qDebug() << "Constructing interface with path" << m_adapterPath; m_adapter = new OrgNeardAdapterInterface(QStringLiteral("org.neard"), m_adapterPath, - QDBusConnection::systemBus()); + QDBusConnection::systemBus(), this); { QDBusPendingReply reply = m_adapter->GetProperties(); @@ -143,7 +159,9 @@ bool QNearFieldManagerPrivateImpl::startTargetDetection() void QNearFieldManagerPrivateImpl::stopTargetDetection() { - //qDebug() << "Stop target detection"; + if (!isAvailable()) + return; + QDBusPendingReply<> reply = m_adapter->StopPollLoop(); reply.waitForFinished(); // TODO Should we really power down the adapter? diff --git a/src/nfc/qnearfieldmanager_neard.h b/src/nfc/qnearfieldmanager_neard.h deleted file mode 100644 index 1848d16a..00000000 --- a/src/nfc/qnearfieldmanager_neard.h +++ /dev/null @@ -1,89 +0,0 @@ -/*************************************************************************** -** -** Copyright (C) 2014 BlackBerry Limited. All rights reserved. -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtNfc module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QNEARFIELDMANAGER_NEARD_H -#define QNEARFIELDMANAGER_NEARD_H - -#include "qnearfieldmanager_p.h" -#include "qnearfieldmanager.h" -#include "qnearfieldtarget.h" - -#include "neard/adapter_p.h" - -QT_BEGIN_NAMESPACE - -class QNearFieldManagerPrivateImpl : public QNearFieldManagerPrivate -{ - Q_OBJECT - -public: - QNearFieldManagerPrivateImpl(); - ~QNearFieldManagerPrivateImpl(); - - bool isAvailable() const; - - bool startTargetDetection(); - - void stopTargetDetection(); - - int registerNdefMessageHandler(QObject *object, const QMetaMethod &method); - - int registerNdefMessageHandler(const QNdefFilter &filter, QObject *object, const QMetaMethod &method); - - bool unregisterNdefMessageHandler(int handlerId); - - void requestAccess(QNearFieldManager::TargetAccessModes accessModes); - - void releaseAccess(QNearFieldManager::TargetAccessModes accessModes); - -private Q_SLOTS: - void tagFound(const QDBusObjectPath&); - void propertyChanged(QString,QDBusVariant); - -private: - QString m_adapterPath; - OrgNeardAdapterInterface *m_adapter; -}; - -QT_END_NAMESPACE - - -#endif // QNEARFIELDMANAGER_NEARD_H diff --git a/src/nfc/qnearfieldmanager_neard_p.h b/src/nfc/qnearfieldmanager_neard_p.h new file mode 100644 index 00000000..4984a531 --- /dev/null +++ b/src/nfc/qnearfieldmanager_neard_p.h @@ -0,0 +1,95 @@ +/*************************************************************************** +** +** Copyright (C) 2014 BlackBerry Limited. All rights reserved. +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtNfc module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QNEARFIELDMANAGER_NEARD_H +#define QNEARFIELDMANAGER_NEARD_H + +#include "qnearfieldmanager_p.h" +#include "qnearfieldmanager.h" +#include "qnearfieldtarget.h" + +#include +#include + +class OrgNeardAdapterInterface; +class OrgNeardManagerInterface; + +QT_BEGIN_NAMESPACE + +class QNearFieldManagerPrivateImpl : public QNearFieldManagerPrivate +{ + Q_OBJECT + +public: + QNearFieldManagerPrivateImpl(); + ~QNearFieldManagerPrivateImpl(); + + bool isAvailable() const; + + bool startTargetDetection(); + + void stopTargetDetection(); + + int registerNdefMessageHandler(QObject *object, const QMetaMethod &method); + + int registerNdefMessageHandler(const QNdefFilter &filter, QObject *object, const QMetaMethod &method); + + bool unregisterNdefMessageHandler(int handlerId); + + void requestAccess(QNearFieldManager::TargetAccessModes accessModes); + + void releaseAccess(QNearFieldManager::TargetAccessModes accessModes); + +private Q_SLOTS: + void tagFound(const QDBusObjectPath&); + void propertyChanged(QString,QDBusVariant); + +private: + QString m_adapterPath; + OrgNeardAdapterInterface *m_adapter; + OrgNeardManagerInterface *m_manager; + +}; + +QT_END_NAMESPACE + + +#endif // QNEARFIELDMANAGER_NEARD_H -- cgit v1.2.3