Skip to content
BluetoothLink.cc 10.6 KiB
Newer Older
dogmaphobic's avatar
dogmaphobic committed
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009 - 2015 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

This file is part of the QGROUNDCONTROL project

    QGROUNDCONTROL is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    QGROUNDCONTROL is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

/**
 * @file
 *   @brief Definition of Bluetooth connection for unmanned vehicles
 *   @author Gus Grubba <mavlink@grubba.com>
 *
 */

#include <QtGlobal>
#include <QTimer>
#include <QList>
#include <QDebug>
#include <iostream>

#include <QtBluetooth/QBluetoothDeviceDiscoveryAgent>
#include <QtBluetooth/QBluetoothLocalDevice>
#include <QtBluetooth/QBluetoothUuid>
dogmaphobic's avatar
dogmaphobic committed
#include <QtBluetooth/QBluetoothSocket>
dogmaphobic's avatar
dogmaphobic committed

#include "BluetoothLink.h"
#include "QGC.h"

BluetoothLink::BluetoothLink(BluetoothConfiguration* config)
    : _connectState(false)
    , _targetSocket(NULL)
dogmaphobic's avatar
dogmaphobic committed
#ifdef __ios__
    , _discoveryAgent(NULL)
#endif
    , _shutDown(false)
dogmaphobic's avatar
dogmaphobic committed
{
    Q_ASSERT(config != NULL);
    _config = config;
    _config->setLink(this);
dogmaphobic's avatar
dogmaphobic committed
    //moveToThread(this);
dogmaphobic's avatar
dogmaphobic committed
}

BluetoothLink::~BluetoothLink()
{
    // Disconnect link from configuration
    _config->setLink(NULL);
    _disconnect();
dogmaphobic's avatar
dogmaphobic committed
#ifdef __ios__
    if(_discoveryAgent) {
        _shutDown = true;
        _discoveryAgent->stop();
        _discoveryAgent->deleteLater();
        _discoveryAgent = NULL;
    }
#endif
dogmaphobic's avatar
dogmaphobic committed
}

void BluetoothLink::run()
{
}

void BluetoothLink::_restartConnection()
{
    if(this->isConnected())
    {
        _disconnect();
        _connect();
    }
}

QString BluetoothLink::getName() const
{
    return _config->name();
}

void BluetoothLink::writeBytes(const char* data, qint64 size)
{
    _sendBytes(data, size);
}

void BluetoothLink::_sendBytes(const char* data, qint64 size)
{
    if(_targetSocket)
    {
        if(_targetSocket->isWritable())
        {
            if(_targetSocket->write(data, size) > 0) {
                _logOutputDataRate(size, QDateTime::currentMSecsSinceEpoch());
            }
            else
                qWarning() << "Bluetooth write error";
        }
        else
            qWarning() << "Bluetooth not writable error";
    }
}

void BluetoothLink::readBytes()
{
    while (_targetSocket->bytesAvailable() > 0)
    {
        QByteArray datagram;
        datagram.resize(_targetSocket->bytesAvailable());
        _targetSocket->read(datagram.data(), datagram.size());
        emit bytesReceived(this, datagram);
        _logInputDataRate(datagram.length(), QDateTime::currentMSecsSinceEpoch());
    }
}

void BluetoothLink::_disconnect(void)
{
dogmaphobic's avatar
dogmaphobic committed
#ifdef __ios__
    if(_discoveryAgent) {
        _shutDown = true;
        _discoveryAgent->stop();
        _discoveryAgent->deleteLater();
        _discoveryAgent = NULL;
dogmaphobic's avatar
dogmaphobic committed
    }
dogmaphobic's avatar
dogmaphobic committed
#endif
dogmaphobic's avatar
dogmaphobic committed
    if(_targetSocket)
    {
dogmaphobic's avatar
dogmaphobic committed
        delete _targetSocket;
dogmaphobic's avatar
dogmaphobic committed
        _targetSocket = NULL;
        emit disconnected();
    }
    _connectState = false;
}

bool BluetoothLink::_connect(void)
{
dogmaphobic's avatar
dogmaphobic committed
    _hardwareConnect();
dogmaphobic's avatar
dogmaphobic committed
    return true;
}

bool BluetoothLink::_hardwareConnect()
dogmaphobic's avatar
dogmaphobic committed
{
#ifdef __ios__
    if(_discoveryAgent) {
        _shutDown = true;
        _discoveryAgent->stop();
        _discoveryAgent->deleteLater();
        _discoveryAgent = NULL;
    }
    _discoveryAgent = new QBluetoothServiceDiscoveryAgent(this);
    connect(_discoveryAgent, &QBluetoothServiceDiscoveryAgent::serviceDiscovered, this, &BluetoothLink::serviceDiscovered);
    connect(_discoveryAgent, &QBluetoothServiceDiscoveryAgent::finished, this, &BluetoothLink::discoveryFinished);
    connect(_discoveryAgent, &QBluetoothServiceDiscoveryAgent::canceled, this, &BluetoothLink::discoveryFinished);
    connect(_discoveryAgent, &QBluetoothServiceDiscoveryAgent::error,this, BluetoothLink::discoveryError);
dogmaphobic's avatar
dogmaphobic committed
    _shutDown = false;
    _discoveryAgent->start();
#else
    _createSocket();
    _targetSocket->connectToService(QBluetoothAddress(_config->device().address), QBluetoothUuid(QBluetoothUuid::Rfcomm));
#endif
    return true;
}

void BluetoothLink::_createSocket()
dogmaphobic's avatar
dogmaphobic committed
{
    if(_targetSocket)
    {
        delete _targetSocket;
        _targetSocket = NULL;
    }
    _targetSocket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol, this);
    QObject::connect(_targetSocket, &QBluetoothSocket::connected, this, &BluetoothLink::deviceConnected);
    QObject::connect(_targetSocket, &QBluetoothSocket::error, this, &BluetoothLink::deviceError);
    QObject::connect(_targetSocket, &QBluetoothSocket::readyRead, this, &