Skip to content
BluetoothLink.cc 10.2 KiB
Newer Older
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
dogmaphobic's avatar
dogmaphobic committed


/**
 * @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>
#include "QGCApplication.h"
dogmaphobic's avatar
dogmaphobic committed
#include "BluetoothLink.h"
#include "QGC.h"

BluetoothLink::BluetoothLink(SharedLinkConfigurationPointer& config)
    : LinkInterface(config)
    , _config(qobject_cast<BluetoothConfiguration*>(config.data()))
    , _connectState(false)
    , _targetSocket(nullptr)
dogmaphobic's avatar
dogmaphobic committed
#ifdef __ios__
    , _discoveryAgent(nullptr)
dogmaphobic's avatar
dogmaphobic committed
#endif
    , _shutDown(false)
dogmaphobic's avatar
dogmaphobic committed
{
dogmaphobic's avatar
dogmaphobic committed
}

BluetoothLink::~BluetoothLink()
{
    _disconnect();
dogmaphobic's avatar
dogmaphobic committed
#ifdef __ios__
    if(_discoveryAgent) {
        _shutDown = true;
        _discoveryAgent->stop();
        _discoveryAgent->deleteLater();
        _discoveryAgent = nullptr;
dogmaphobic's avatar
dogmaphobic committed
    }
#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 QByteArray bytes)
dogmaphobic's avatar
dogmaphobic committed
{
    if (_targetSocket) {
        if(_targetSocket->write(bytes) > 0) {
Pierre TILAK's avatar
Pierre TILAK committed
            emit bytesSent(this, bytes);
            _logOutputDataRate(bytes.size(), QDateTime::currentMSecsSinceEpoch());
        } else {
            qWarning() << "Bluetooth write error";
dogmaphobic's avatar
dogmaphobic committed
        }
    }
}

void BluetoothLink::readBytes()
{
    if (_targetSocket) {
        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());
        }
dogmaphobic's avatar
dogmaphobic committed
    }
}

void BluetoothLink::_disconnect(void)
{
dogmaphobic's avatar
dogmaphobic committed
#ifdef __ios__
    if(_discoveryAgent) {
        _shutDown = true;
        _discoveryAgent->stop();
        _discoveryAgent->deleteLater();
        _discoveryAgent = nullptr;
dogmaphobic's avatar
dogmaphobic committed
    }
dogmaphobic's avatar
dogmaphobic committed
#endif
dogmaphobic's avatar
dogmaphobic committed
    if(_targetSocket)
    {
        _targetSocket->deleteLater();
        _targetSocket = nullptr;
dogmaphobic's avatar
dogmaphobic committed
        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 = nullptr;
dogmaphobic's avatar
dogmaphobic committed
    }
    _discoveryAgent = new QBluetoothServiceDiscoveryAgent(this);
Tomaz Canabrava's avatar
Tomaz Canabrava committed
    QObject::connect(_discoveryAgent, &QBluetoothServiceDiscoveryAgent::serviceDiscovered, this, &BluetoothLink::serviceDiscovered);
    QObject::connect(_discoveryAgent, &QBluetoothServiceDiscoveryAgent::finished, this, &BluetoothLink::discoveryFinished);
    QObject::connect(_discoveryAgent, &QBluetoothServiceDiscoveryAgent::canceled, this, &BluetoothLink::discoveryFinished);
dogmaphobic's avatar
dogmaphobic committed
    _shutDown = false;
    _discoveryAgent->start();
#else
    _createSocket();
    _targetSocket->connectToService(QBluetoothAddress(_config->device().address), QBluetoothUuid(QBluetoothUuid::SerialPort));
dogmaphobic's avatar
dogmaphobic committed
#endif
    return true;
}

void BluetoothLink::_createSocket()
dogmaphobic's avatar
dogmaphobic committed
{
    if(_targetSocket)
    {
        delete _targetSocket;
        _targetSocket = nullptr;
dogmaphobic's avatar
dogmaphobic committed
    }
    _targetSocket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol, this);
    QObject::connect(_targetSocket, &QBluetoothSocket::connected, this, &BluetoothLink::deviceConnected);
    QObject::connect(_targetSocket, &QBluetoothSocket::readyRead, this, &BluetoothLink::readBytes);
    QObject::connect(_targetSocket, &QBluetoothSocket::disconnected, this, &BluetoothLink::deviceDisconnected);
Tomaz Canabrava's avatar
Tomaz Canabrava committed

    QObject::connect(_targetSocket, static_cast<void (QBluetoothSocket::*)(QBluetoothSocket::SocketError)>(&QBluetoothSocket::error),
            this, &BluetoothLink::deviceError);
dogmaphobic's avatar
dogmaphobic committed
#ifdef __ios__
void BluetoothLink::serviceDiscovered(const QBluetoothServiceInfo& info)
{
    if(!info.device().name().isEmpty() && !_targetSocket)
    {
        if(_config->device().uuid == info.device().deviceUuid() && _config->device().name == info.device().name())
        {
            _createSocket();
            _targetSocket->connectToService(info);
        }
    }
}
#endif

#ifdef __ios__
void BluetoothLink::discoveryFinished()
{
    if(_discoveryAgent && !_shutDown)
    {
        _shutDown = true;
        _discoveryAgent->deleteLater();
        _discoveryAgent = nullptr;
dogmaphobic's avatar
dogmaphobic committed
        if(!_targetSocket)
        {
            _connectState = false;
            emit communicationError("Could not locate Bluetooth device:", _config->device().name);
        }
    }
}
#endif

dogmaphobic's avatar
dogmaphobic committed
void BluetoothLink::deviceConnected()
{
    _connectState = true;
    emit connected();
}

void BluetoothLink::deviceDisconnected()
{
    _connectState = false;
    qWarning() << "Bluetooth disconnected";
}

void BluetoothLink::deviceError(QBluetoothSocket::SocketError error)
{
    _connectState = false;
    qWarning() << "Bluetooth error" << error;
    emit communicationError(tr("Bluetooth Link Error"), _targetSocket->errorString());
dogmaphobic's avatar
dogmaphobic committed
}

bool BluetoothLink::isConnected() const
{
    return _connectState;
}

qint64 BluetoothLink::getConnectionSpeed() const
{
    return 1000000; // 1 Mbit
}

qint64 BluetoothLink::getCurrentInDataRate() const
{
    return 0;
}

qint64 BluetoothLink::getCurrentOutDataRate() const
{
    return 0;
}

//--------------------------------------------------------------------------
//-- BluetoothConfiguration

BluetoothConfiguration::BluetoothConfiguration(const QString& name)
    : LinkConfiguration(name)
    , _deviceDiscover(nullptr)
dogmaphobic's avatar
dogmaphobic committed
{

}

BluetoothConfiguration::BluetoothConfiguration(BluetoothConfiguration* source)
    : LinkConfiguration(source)
    , _deviceDiscover(nullptr)
    , _device(source->device())
dogmaphobic's avatar
dogmaphobic committed
{
}

BluetoothConfiguration::~BluetoothConfiguration()
{
    if(_deviceDiscover)
    {
        _deviceDiscover->stop();
        delete _deviceDiscover;
    }
}

QString BluetoothConfiguration::settingsTitle()
{
    if(qgcApp()->toolbox()->linkManager()->isBluetoothAvailable()) {
        return tr("Bluetooth Link Settings");
    } else {
        return tr("Bluetooth Not Available");
    }
}

dogmaphobic's avatar
dogmaphobic committed
void BluetoothConfiguration::copyFrom(LinkConfiguration *source)
{
    LinkConfiguration::copyFrom(source);
    auto* usource = qobject_cast<BluetoothConfiguration*>(source);
    Q_ASSERT(usource != nullptr);
    _device = usource->device();
dogmaphobic's avatar
dogmaphobic committed
}

void BluetoothConfiguration::saveSettings(QSettings& settings, const QString& root)
{
    settings.beginGroup(root);
dogmaphobic's avatar
dogmaphobic committed
    settings.setValue("deviceName", _device.name);
#ifdef __ios__
    settings.setValue("uuid", _device.uuid.toString());
#else
    settings.setValue("address",_device.address);
#endif
dogmaphobic's avatar
dogmaphobic committed
    settings.endGroup();
}

void BluetoothConfiguration::loadSettings(QSettings& settings, const QString& root)
{
    settings.beginGroup(root);
dogmaphobic's avatar
dogmaphobic committed
    _device.name    = settings.value("deviceName", _device.name).toString();
#ifdef __ios__
    QString suuid   = settings.value("uuid", _device.uuid.toString()).toString();
    _device.uuid    = QUuid(suuid);
#else
    _device.address = settings.value("address", _device.address).toString();
dogmaphobic's avatar
dogmaphobic committed
#endif
dogmaphobic's avatar
dogmaphobic committed
    settings.endGroup();
}

void BluetoothConfiguration::updateSettings()
{
    if(_link) {
        auto* ulink = qobject_cast<BluetoothLink*>(_link);
dogmaphobic's avatar
dogmaphobic committed
        if(ulink) {
            ulink->_restartConnection();
        }
    }
}

void BluetoothConfiguration::stopScan()
{
    if(_deviceDiscover)
    {
        _deviceDiscover->stop();
        _deviceDiscover->deleteLater();
        _deviceDiscover = nullptr;
        emit scanningChanged();
dogmaphobic's avatar
dogmaphobic committed
    }
}

void BluetoothConfiguration::startScan()
{
    if(!_deviceDiscover)
    {
        _deviceDiscover = new QBluetoothDeviceDiscoveryAgent(this);
        connect(_deviceDiscover, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered,  this, &BluetoothConfiguration::deviceDiscovered);
        connect(_deviceDiscover, &QBluetoothDeviceDiscoveryAgent::finished,          this, &BluetoothConfiguration::doneScanning);
dogmaphobic's avatar
dogmaphobic committed
        emit scanningChanged();
    }
    else
    {
        _deviceDiscover->stop();
    }
    _nameList.clear();
dogmaphobic's avatar
dogmaphobic committed
    _deviceList.clear();
    emit nameListChanged();
dogmaphobic's avatar
dogmaphobic committed
    _deviceDiscover->setInquiryType(QBluetoothDeviceDiscoveryAgent::GeneralUnlimitedInquiry);
    _deviceDiscover->start();
}

void BluetoothConfiguration::deviceDiscovered(QBluetoothDeviceInfo info)
{
    if(!info.name().isEmpty() && info.isValid())
    {
#if 0
        qDebug() << "Name:           " << info.name();
        qDebug() << "Address:        " << info.address().toString();
        qDebug() << "Service Classes:" << info.serviceClasses();
        QList<QBluetoothUuid> uuids = info.serviceUuids();
        for (QBluetoothUuid uuid: uuids) {
            qDebug() << "Service UUID:   " << uuid.toString();
        }
#endif
        BluetoothData data;
        data.name    = info.name();
dogmaphobic's avatar
dogmaphobic committed
#ifdef __ios__
        data.uuid    = info.deviceUuid();
#else
        data.address = info.address().toString();
dogmaphobic's avatar
dogmaphobic committed
#endif
        if(!_deviceList.contains(data))
        {
            _deviceList += data;
            _nameList   += data.name;
            emit nameListChanged();
            return;
        }
    }
dogmaphobic's avatar
dogmaphobic committed
}

void BluetoothConfiguration::doneScanning()
{
    if(_deviceDiscover)
    {
        _deviceDiscover->deleteLater();
        _deviceDiscover = nullptr;
dogmaphobic's avatar
dogmaphobic committed
        emit scanningChanged();
    }
}

void BluetoothConfiguration::setDevName(const QString &name)
dogmaphobic's avatar
dogmaphobic committed
{
    for(const BluetoothData& data: _deviceList)
dogmaphobic's avatar
dogmaphobic committed
    {
        if(data.name == name)
        {
            _device = data;
            emit devNameChanged();
dogmaphobic's avatar
dogmaphobic committed
#ifndef __ios__
            emit addressChanged();
dogmaphobic's avatar
dogmaphobic committed
#endif
dogmaphobic's avatar
dogmaphobic committed
QString BluetoothConfiguration::address()
{
#ifdef __ios__
dogmaphobic's avatar
dogmaphobic committed
#else
    return _device.address;
#endif
}