/*************************************************************************** ** ** Copyright (C) 2012 Research In Motion ** 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$ ** ****************************************************************************/ #include "qnxnfcmanager_p.h" #include #include #include "../qllcpsocket_qnx_p.h" #include #include QT_BEGIN_NAMESPACE QNXNFCManager *QNXNFCManager::m_instance = 0; QNXNFCManager *QNXNFCManager::instance() { if (!m_instance) { qQNXNFCDebug() << "creating manager instance"; m_instance = new QNXNFCManager; } return m_instance; } void QNXNFCManager::registerForNewInstance() { m_instanceCount++; } void QNXNFCManager::unregisterForInstance() { if (m_instanceCount>=1) { m_instanceCount--; if (m_instanceCount==0) { delete m_instance; m_instance = 0; } } else { qWarning() << Q_FUNC_INFO << "instance count below 0"; } } void QNXNFCManager::unregisterTargetDetection(QObject *obj) { Q_UNUSED(obj) //TODO another instance of the nearfieldmanager might still //want to detect targets so we have to do ref counting nfc_unregister_tag_readerwriter(); } nfc_target_t *QNXNFCManager::getLastTarget() { return m_lastTarget; } bool QNXNFCManager::isAvailable() { return m_available; } void QNXNFCManager::registerLLCPConnection(nfc_llcp_connection_listener_t listener, QObject *obj) { llcpConnections.append(QPair (listener, obj)); } void QNXNFCManager::unregisterLLCPConnection(nfc_llcp_connection_listener_t listener) { for (int i=0; i (targetId, object)); } void QNXNFCManager::unregisterTargetLost(QObject *object) { for (int i=0; iinstallOnEventDispatcher(QAbstractEventDispatcher::instance()); connect(ndefEventFilter, SIGNAL(ndefEvent(QNdefMessage)), this, SLOT(invokeNdefMessage(QNdefMessage))); } QNXNFCManager::~QNXNFCManager() { nfc_disconnect(); if (nfcNotifier) delete nfcNotifier; ndefEventFilter->uninstallEventFilter(); } QList QNXNFCManager::decodeTargetMessage(nfc_target_t *target) { unsigned int messageCount; QList ndefMessages; if (nfc_get_ndef_message_count(target, &messageCount) != NFC_RESULT_SUCCESS) qWarning() << Q_FUNC_INFO << "Could not get ndef message count"; for (unsigned int i=0; i (buffer), bytesRead); qQNXNFCDebug() << "Read LLCP data" << bytesRead << data; } else if (result == NFC_RESULT_READ_FAILED) { //This most likely means, that the target has been disconnected qWarning() << Q_FUNC_INFO << "LLCP read failed"; nfc_llcp_close(target); targetLost(targetId); return; } else { qWarning() << Q_FUNC_INFO << "LLCP read unknown error"; //return; } for (int i=0; i(nfcTargets.at(i).second)->dataRead(data); } } } void QNXNFCManager::llcpWriteComplete(nfc_event_t *nfcEvent) { nfc_target_t *target; if (nfc_get_target(nfcEvent, &target) != NFC_RESULT_SUCCESS) { qWarning() << Q_FUNC_INFO << "Could not retrieve LLCP NFC target"; return; } if (nfc_llcp_get_write_status(target) != NFC_RESULT_SUCCESS) { qWarning() << Q_FUNC_INFO << "LLCP write failed"; } else { qQNXNFCDebug() << "write completed succesfull"; } unsigned int targetId; nfc_get_target_connection_id(target, &targetId); for (int i=0; i(nfcTargets.at(i).second)->dataWritten(); } } } void QNXNFCManager::nfcReadWriteEvent(nfc_event_t *nfcEvent) { nfc_target_t *target; if (nfc_get_target(nfcEvent, &target) != NFC_RESULT_SUCCESS) { qWarning() << Q_FUNC_INFO << "Could not retrieve NFC target"; return; } tag_variant_type_t variant; nfc_get_tag_variant(target, &variant); qQNXNFCDebug() << "Variant:" << variant; QList targetMessages = decodeTargetMessage(target); NearFieldTarget *bbNFTarget = new NearFieldTarget(this, target, targetMessages); emit targetDetected(bbNFTarget, targetMessages); for (int i=0; i< targetMessages.count(); i++) { emit ndefMessage(targetMessages.at(i), reinterpret_cast (bbNFTarget)); } } void QNXNFCManager::llcpConnectionEvent(nfc_event_t *nfcEvent) { nfc_target_t *target; if (nfc_get_target(nfcEvent, &target) != NFC_RESULT_SUCCESS) { qWarning() << Q_FUNC_INFO << "Could not retrieve NFC target"; return; } nfc_llcp_connection_listener_t conListener; nfc_llcp_get_connection_status(target, &conListener); unsigned int lmiu; nfc_llcp_get_local_miu(target, &lmiu); m_lastTarget = target; qQNXNFCDebug() << "LLCP connection event; local MIU" << lmiu; for (int i=0; iarguments().first(); targetId = targetId.left(targetId.lastIndexOf(QLatin1Char('.'))); if (BPS_SUCCESS != navigator_invoke_set_filters("20", targetId.toLatin1().constData(), filters, 1)) { qWarning() << "NFC Error setting share target filter"; } else { qQNXNFCDebug() << "NFC share target filter set" << filters[0] << " Target:" << targetId; } } void QNXNFCManager::targetLostEvent(nfc_event_t *nfcEvent) { unsigned int targetId; nfc_get_notification_value(nfcEvent, &targetId); qQNXNFCDebug() << "Target lost with target ID:" << targetId; targetLost(targetId); } void QNXNFCManager::targetLost(unsigned int targetId) { for (int i=0; i filters, QObject *obj) { qQNXNFCDebug() << Q_FUNC_INFO << "NDEF Filter update"; //Updating the filters for an object if (!filters.isEmpty()) { if (ndefFilters.contains(obj)) { ndefFilters[obj] = filters; qQNXNFCDebug() << "Updateing filter list for"<< obj; } else { qQNXNFCDebug() << "Appending new filter for"<< obj; ndefFilters[obj] = filters; } } else { ndefFilters.remove(obj); } //Iterate over all registered object filters and construct a filter list for the application QList newFilters; if (ndefFilters.size() > 0) { QHash >::const_iterator it=ndefFilters.constBegin(); do { foreach (const QByteArray filter, it.value()) { if (!newFilters.contains(filter)) { newFilters.append(filter); qQNXNFCDebug() << "Appending Filter" << filter; } } it++; } while (it != ndefFilters.constEnd()); } if (newFilters != absNdefFilters) { absNdefFilters = newFilters; setupInvokeTarget(); } } QNdefMessage QNXNFCManager::decodeMessage(nfc_ndef_message_t *nextMessage) { QNdefMessage newNdefMessage; unsigned int recordCount; nfc_get_ndef_record_count(nextMessage, &recordCount); for (unsigned int j=0; j(payLoad), payLoadSize)); nfc_get_ndef_record_id(newRecord, &recordId); newNdefRecord.setId(QByteArray(recordId)); nfc_get_ndef_record_tnf(newRecord, &typeNameFormat); QNdefRecord::TypeNameFormat recordTnf = QNdefRecord::Unknown; switch (typeNameFormat) { case NDEF_TNF_WELL_KNOWN: recordTnf = QNdefRecord::NfcRtd; break; case NDEF_TNF_EMPTY: recordTnf = QNdefRecord::Empty; break; case NDEF_TNF_MEDIA: recordTnf = QNdefRecord::Mime; break; case NDEF_TNF_ABSOLUTE_URI: recordTnf = QNdefRecord::Uri; break; case NDEF_TNF_EXTERNAL: recordTnf = QNdefRecord::ExternalRtd; break; case NDEF_TNF_UNKNOWN: recordTnf = QNdefRecord::Unknown; break; //TODO add the rest case NDEF_TNF_UNCHANGED: recordTnf = QNdefRecord::Unknown; break; } newNdefRecord.setTypeNameFormat(recordTnf); qQNXNFCDebug() << "Adding NFC record"; newNdefMessage << newNdefRecord; delete recordType; delete payLoad; delete recordId; } return newNdefMessage; } QT_END_NAMESPACE