diff options
author | Javier <dev.git@javispedro.com> | 2015-03-31 14:24:51 +0200 |
---|---|---|
committer | Javier <dev.git@javispedro.com> | 2015-03-31 14:24:51 +0200 |
commit | a8ba1dbd9a1d7955d4e6e66f1e8c2d874873ca01 (patch) | |
tree | 07df3ae96336cdd041804b135ca946d4d88fff65 /saltoqd | |
parent | f20e0e00148169542a2fbb0f231ea59014b6dcee (diff) | |
download | saltoq-a8ba1dbd9a1d7955d4e6e66f1e8c2d874873ca01.tar.gz saltoq-a8ba1dbd9a1d7955d4e6e66f1e8c2d874873ca01.zip |
listen to incoming connections too
Diffstat (limited to 'saltoqd')
-rw-r--r-- | saltoqd/fmsmanager.cpp | 6 | ||||
-rw-r--r-- | saltoqd/obexconnection.cpp | 2 | ||||
-rw-r--r-- | saltoqd/toqconnection.cpp | 69 | ||||
-rw-r--r-- | saltoqd/toqconnection.h | 9 |
4 files changed, 73 insertions, 13 deletions
diff --git a/saltoqd/fmsmanager.cpp b/saltoqd/fmsmanager.cpp index 14220b7..ac604f4 100644 --- a/saltoqd/fmsmanager.cpp +++ b/saltoqd/fmsmanager.cpp @@ -195,8 +195,10 @@ void FmsManager::handleObexError(int response) void FmsManager::cleanCurTransaction() { - qDebug() << "Clearing current transaction"; - _curTransaction = 0; + if (_curTransaction) { + qDebug() << "Clearing current transaction"; + _curTransaction = 0; + } if (_curTransfer) { _curTransfer->deleteLater(); _curTransfer = 0; diff --git a/saltoqd/obexconnection.cpp b/saltoqd/obexconnection.cpp index 5fab0ab..2e1da56 100644 --- a/saltoqd/obexconnection.cpp +++ b/saltoqd/obexconnection.cpp @@ -69,7 +69,7 @@ void ObexConnection::tryConnect() Q_ASSERT(!_socket); Q_ASSERT(_conn->isConnected()); - qDebug() << "Trying to connect"; + qDebug() << "Trying to make OBEX connection"; _socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol, this); connect(_socket, &QBluetoothSocket::connected, diff --git a/saltoqd/toqconnection.cpp b/saltoqd/toqconnection.cpp index 8ffd02f..b5265da 100644 --- a/saltoqd/toqconnection.cpp +++ b/saltoqd/toqconnection.cpp @@ -1,16 +1,20 @@ #include <zlib.h> -#include <QtDebug> -#include <QtEndian> +#include <QtCore/QtEndian> #include <QtCore/QMetaEnum> +#include <QtDebug> #include "toqconnection.h" +static const QBluetoothUuid LISTEN_UUID(QLatin1String("00000001-476D-42C4-BD11-9D377C45694F")); static const int HEADER_LENGTH = 10; -static const int FIRST_CONNECTION_INTERVAL = 1000; -static const int RETRY_CONNECTION_INTERVAL = 30000; +static const int FIRST_CONNECTION_INTERVAL = 10 * 1000; +// Because the watch will actually try to reconnect to us frequently, +// we can afford to set a relaxed interval here. +static const int RETRY_CONNECTION_INTERVAL = 10 * 60 * 1000; ToqConnection::ToqConnection(QObject *parent) : QObject(parent), + _server(new QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, this)), _socket(0), _reconnectTimer(new QTimer(this)), _lastTransactionId(0) @@ -18,7 +22,18 @@ ToqConnection::ToqConnection(QObject *parent) : connect(_reconnectTimer, &QTimer::timeout, this, &ToqConnection::tryConnect); + _reconnectTimer->setTimerType(Qt::VeryCoarseTimer); _reconnectTimer->setSingleShot(true); + + connect(_server, &QBluetoothServer::newConnection, + this, &ToqConnection::handleServerConnection); + + QBluetoothServiceInfo service = _server->listen(LISTEN_UUID, "PHubCommServer"); + if (service.isValid()) { + qDebug() << "Started listening on channel" << service.serverChannel(); + } else { + qWarning() << "Could not register the server service"; + } } ToqConnection::Message::Message(Endpoint source, Endpoint destination, quint16 transactionId, quint32 type, const QJsonDocument &payload) @@ -103,6 +118,15 @@ void ToqConnection::sendMessage(const Message &msg) } } +void ToqConnection::disconnectFromDevice() +{ + if (_socket) { + _socket->disconnectFromService(); + } else { + qWarning() << "Not connected"; + } +} + ToqConnection::Message ToqConnection::unpackMessage(const QByteArray &data) { Message msg; @@ -139,11 +163,10 @@ QByteArray ToqConnection::packMessage(const Message &msg) return data; } -void ToqConnection::tryConnect() +void ToqConnection::setSocket(QBluetoothSocket *socket) { Q_ASSERT(!_socket); - - _socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol, this); + _socket = socket; connect(_socket, &QBluetoothSocket::connected, this, &ToqConnection::handleSocketConnected); connect(_socket, &QBluetoothSocket::disconnected, @@ -152,10 +175,38 @@ void ToqConnection::tryConnect() this, &ToqConnection::handleSocketError); connect(_socket, &QBluetoothSocket::readyRead, this, &ToqConnection::handleSocketData); + if (_socket->state() == QBluetoothSocket::ConnectedState) { + handleSocketConnected(); + } +} + +void ToqConnection::tryConnect() +{ + Q_ASSERT(!_socket); + + QBluetoothSocket *socket = + new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol, this); + setSocket(socket); qDebug() << "Connecting to" << _address.toString(); - _socket->connectToService(_address, 1); + socket->connectToService(_address, 1); +} + +void ToqConnection::handleServerConnection() +{ + qDebug() << "Got a connection from the server"; + QBluetoothSocket *socket = _server->nextPendingConnection(); + if (_socket) { + // If we have an existing socket, give priority to the received one. + qDebug() << "Terminating current connection first"; + _socket->disconnectFromService(); + if (_socket) { + _socket->deleteLater(); + _socket = 0; + } + } + setSocket(socket); } void ToqConnection::handleSocketConnected() @@ -171,6 +222,8 @@ void ToqConnection::handleSocketDisconnected() { if (_socket) { qDebug() << "Disconnected"; + Q_ASSERT(_socket->state() == QBluetoothSocket::UnconnectedState || + _socket->state() == QBluetoothSocket::ClosingState); _socket->deleteLater(); _socket = 0; if (!_address.isNull()) { diff --git a/saltoqd/toqconnection.h b/saltoqd/toqconnection.h index 765c3f3..68f3445 100644 --- a/saltoqd/toqconnection.h +++ b/saltoqd/toqconnection.h @@ -3,6 +3,7 @@ #include <QtCore/QTimer> #include <QtBluetooth/QBluetoothSocket> +#include <QtBluetooth/QBluetoothServer> #include <QtCore/QJsonDocument> #include <QtCore/QJsonObject> @@ -65,6 +66,7 @@ public: public slots: void sendMessage(const Message &msg); + void disconnectFromDevice(); signals: void connected(); @@ -73,11 +75,13 @@ signals: void connectedChanged(); private: - Message unpackMessage(const QByteArray &data); - QByteArray packMessage(const Message &msg); + static Message unpackMessage(const QByteArray &data); + static QByteArray packMessage(const Message &msg); + void setSocket(QBluetoothSocket *socket); private slots: void tryConnect(); + void handleServerConnection(); void handleSocketConnected(); void handleSocketDisconnected(); void handleSocketError(QBluetoothSocket::SocketError error); @@ -85,6 +89,7 @@ private slots: private: QBluetoothAddress _address; + QBluetoothServer *_server; QBluetoothSocket *_socket; QTimer *_reconnectTimer; quint16 _lastTransactionId; |