// bytran -|- spectral calculations for portable devices using the HITRAN database
//
// Developed by Dzianis Pliutau (Denis Plutov) and Kostya Roslyakov
// www.bytran.org | bytran@bytran.org
// Copyright (c) 2016 Dzianis Pliutau
// Distributed under the MIT license
//
// The following examples distributed by the Qt Company under the BSD License were used to write this class
// http://doc.qt.io/qt-5/qtbluetooth-le-overview.html
// http://doc.qt.io/qt-5/qtbluetooth-lowenergyscanner-example.html
//
// The MIT License (MIT)
// Copyright (c) 2016 Dzianis Pliutau
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.



// Class is used to connect to a remote device (including SensorTag) using the Bluetooth low energy protocol
#ifndef BLUETOOTHLOWENERGY_H
#define BLUETOOTHLOWENERGY_H

#include <QObject>
#include <QBluetoothDeviceDiscoveryAgent>
#include <QBluetoothServiceDiscoveryAgent>
#include <QBluetoothLocalDevice>
#include <QBluetoothServiceInfo>
#include <QLowEnergyController>
#include "bluetoothdiscoverdevices.h"

class BluetoothLowEnergy : public QObject
{
    Q_OBJECT
public:
    explicit BluetoothLowEnergy(QObject *parent = nullptr);

    // Method to get a reference to the Bluetooth discovery object
    BluetoothDiscoverDevices *getBluetoothDevicesDiscover();

    // Method to discover services for a given device
    void discoverServicesForSelectedDevice();

    bool getIsRandomAddress() const;
    void setIsRandomAddress(bool newValue);

    // Method to connect to a bluetooth device
    void disconnectFromDevice();

    // Method to discover bluetooth services
    void discoverServices(QBluetoothDeviceInfo device);

    // This method will locate and connect to the service which starts with the string supplied
    void connectToServiceWithUuidContaining(QString uuidContains);

    // Method returns a pointer to the controller
    QLowEnergyController *getController();

signals:

    // Emited once the service discovery for a device is complete
    void servicesDiscovered();

    // Emitted when characteristics have been discovered returning a handle to the service onctaining them
    void characteristicsDiscovered(QLowEnergyService *service);

    // Signal is emitted when the device is disconnected
    void disconnected();

public slots:

private slots:

    // QLowEnergyController related slots
    void addLowEnergyService(const QBluetoothUuid &uuid);
    void deviceConnected();
    void errorReceived(QLowEnergyController::Error);    
    void deviceDisconnected();
    void serviceScanDone();
    // Slot is triggered when the service details have been discovered
    void serviceDetailsDiscovered(QLowEnergyService::ServiceState newState);

private:

    QBluetoothLocalDevice *localDevice;   // Bluetooth local device object
    QLowEnergyController *controller;     // Bluetooth low energy controller

    // Object to discover bluetooth devices
    BluetoothDiscoverDevices *devicesDiscover;

    // List of discovered bluetooth services for a selected device
    QVector<QLowEnergyService*> services;

    // Service to connect to
    QLowEnergyService *serviceToConnectTo;

    // Address of the previous service the low energy bluetooth controller used to be connected to
    QBluetoothDeviceInfo previousDevice;

    // Variable indicates if the low energy bluetooth device to connect to is public address or random address
    bool isRandomAddress;

};

#endif // BLUETOOTHLOWENERGY_H
