nfcconnection.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
  3. * All rights reserved.
  4. * This component and the accompanying materials are made available
  5. * under the terms of "Eclipse Public License v1.0"
  6. * which accompanies this distribution, and is available
  7. * at the URL "http://www.eclipse.org/legal/epl-v10.html".
  8. *
  9. * Initial Contributors:
  10. * Nokia Corporation - initial contribution.
  11. *
  12. * Contributors:
  13. *
  14. * Description: Implements NFC connection functionality.
  15. */
  16. #include <qllcpserver.h>
  17. #include <qnearfieldmanager.h>
  18. #include "nfcconnection.h"
  19. #include "p2pengine.h"
  20. //! LLCP service URI that the server is listening on.
  21. static const QLatin1String p2pUri("urn:nfc:sn:com.nokia.qtmobility.p2p");
  22. NfcConnection::NfcConnection(QObject *parent, P2PEngine* engine) :
  23. QObject(parent), m_engine (engine), m_nfcServerSocket(0)
  24. {
  25. m_nfcManager = new QNearFieldManager(this);
  26. connect(m_nfcManager, SIGNAL(targetDetected(QNearFieldTarget*)),
  27. this, SLOT(targetDetected(QNearFieldTarget*)));
  28. connect(m_nfcManager, SIGNAL(targetLost(QNearFieldTarget*)),
  29. this, SLOT(targetLost(QNearFieldTarget*)));
  30. // Only detect other NFC devices. Leave the phone to handle NFC tags.
  31. m_nfcManager->setTargetAccessModes(QNearFieldManager::NdefReadTargetAccess | QNearFieldManager::NdefWriteTargetAccess);
  32. m_nfcManager->startTargetDetection(QNearFieldTarget::NfcForumDevice);
  33. m_nfcServer = new QLlcpServer(this);
  34. connect(m_nfcServer, SIGNAL(newConnection()), this, SLOT(handleNewConnection()));
  35. // Listen for incoming connections on the P2P service URI.
  36. m_nfcServer->listen(p2pUri);
  37. m_nfcClientSocket = new QLlcpSocket(this);
  38. connect(m_nfcClientSocket, SIGNAL(readyRead()), this, SLOT(readText()));
  39. connect(m_nfcClientSocket, SIGNAL(disconnected()), this, SLOT(clientSocketDisconnected()));
  40. }
  41. NfcConnection::~NfcConnection()
  42. {
  43. // Empty implementation
  44. }
  45. void NfcConnection::targetDetected(QNearFieldTarget *target)
  46. {
  47. // Check if the target supports LLCP access.
  48. QNearFieldTarget::AccessMethods accessMethods = target->accessMethods();
  49. if (accessMethods.testFlag(QNearFieldTarget::LlcpAccess))
  50. {
  51. m_nfcClientSocket->connectToService(target, p2pUri);
  52. }
  53. }
  54. void NfcConnection::targetLost(QNearFieldTarget *target)
  55. {
  56. m_nfcClientSocket->disconnectFromService();
  57. target->deleteLater();
  58. }
  59. void NfcConnection::handleNewConnection()
  60. {
  61. if (m_nfcServerSocket)
  62. {
  63. m_nfcServerSocket->deleteLater();
  64. }
  65. // The socket is a child of the server and will be deleted automatically.
  66. m_nfcServerSocket = m_nfcServer->nextPendingConnection();
  67. sendText();
  68. }
  69. void NfcConnection::clientSocketDisconnected()
  70. {
  71. // Client socket disconnected
  72. }
  73. QString NfcConnection::readText()
  74. {
  75. QByteArray rawData = m_nfcClientSocket->readAll();
  76. QString data = QString::fromUtf8(rawData.constData(), rawData.size());
  77. if (!data.isEmpty())
  78. {
  79. m_engine->setRemoteIndexes(data);
  80. }
  81. return data;
  82. }
  83. bool NfcConnection::sendText()
  84. {
  85. bool ret(false);
  86. QString text = m_engine->getLocalIndexString();
  87. if (m_nfcServerSocket && m_nfcServerSocket->isOpen() && m_nfcServerSocket->isWritable() &&
  88. m_nfcClientSocket && m_nfcClientSocket->isOpen() && !text.isEmpty())
  89. {
  90. // Send array of images
  91. m_nfcServerSocket->write(text.toUtf8());
  92. if(m_engine->isExchangeImageSet())
  93. {
  94. // Write index of exchange image
  95. text = m_engine->getExchangeIndexString();
  96. m_nfcServerSocket->write(text.toUtf8());
  97. }
  98. ret = true;
  99. }
  100. return ret;
  101. }