PairingManager.h 6.74 KB
Newer Older
Gus Grubba's avatar
Gus Grubba committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
/****************************************************************************
 *
 *   (c) 2019 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.
 *
 ****************************************************************************/

#pragma once

#include <QJsonDocument>
#include <QMutex>
#include <QNetworkReply>
#include <QTimer>
#include <QTime>
#include <QVariantMap>

#include "aes.h"
#include "QGCToolbox.h"
#include "QGCLoggingCategory.h"
#include "Fact.h"
#if defined QGC_ENABLE_NFC
#include "PairingNFC.h"
#endif
#if defined QGC_ENABLE_QTNFC
#include "QtNFC.h"
#endif
#ifdef __android__
#include <jni.h>
#include <QtAndroidExtras/QtAndroidExtras>
#include <QtAndroidExtras/QAndroidJniObject>
#endif

Q_DECLARE_LOGGING_CATEGORY(PairingManagerLog)

class AppSettings;
class QGCApplication;

//-----------------------------------------------------------------------------
class PairingManager : public QGCTool
{
    Q_OBJECT
public:
    explicit PairingManager (QGCApplication* app, QGCToolbox* toolbox);
    ~PairingManager         () override;

    // Override from QGCTool
    virtual void setToolbox(QGCToolbox *toolbox) override;

    enum PairingStatus {
        PairingIdle,
        PairingActive,
        PairingSuccess,
        PairingConnecting,
        PairingConnected,
        PairingRejected,
Gus Grubba's avatar
Gus Grubba committed
58 59
        PairingConnectionRejected,
        PairingError
Gus Grubba's avatar
Gus Grubba committed
60 61 62 63
    };

    Q_ENUM(PairingStatus)

Gus Grubba's avatar
Gus Grubba committed
64 65 66
    QStringList     pairingLinkTypeStrings      ();
    QString         pairingStatusStr            () const;
    QStringList     pairedDeviceNameList        () { return _deviceList; }
Gus Grubba's avatar
Gus Grubba committed
67 68
    PairingStatus   pairingStatus               () { return _status; }
    QString         pairedVehicle               () { return _lastPaired; }
Gus Grubba's avatar
Gus Grubba committed
69 70
    int             nfcIndex                    () { return _nfcIndex; }
    int             microhardIndex              () { return _microhardIndex; }
Gus Grubba's avatar
Gus Grubba committed
71 72 73 74 75
    bool            firstBoot                   () { return _firstBoot; }
    bool            errorState                  () { return _status == PairingRejected || _status == PairingConnectionRejected || _status == PairingError; }
    void            setStatusMessage            (PairingStatus status, QString statusStr) { emit setPairingStatus(status, statusStr); }
    void            jsonReceived                (QString json) { emit parsePairingJson(json); }
    void            setFirstBoot                (bool set) { _firstBoot = set; emit firstBootChanged(); }
Gus Grubba's avatar
Gus Grubba committed
76
#ifdef __android__
Gus Grubba's avatar
Gus Grubba committed
77
    static void     setNativeMethods            (void);
Gus Grubba's avatar
Gus Grubba committed
78
#endif
Gus Grubba's avatar
Gus Grubba committed
79 80
    Q_INVOKABLE void connectToPairedDevice      (QString name);
    Q_INVOKABLE void removePairedDevice         (QString name);
Gus Grubba's avatar
Gus Grubba committed
81 82 83 84 85 86 87 88 89 90 91 92 93

#if defined QGC_ENABLE_NFC || defined QGC_ENABLE_QTNFC
    Q_INVOKABLE void startNFCScan();
#endif    
#if QGC_GST_MICROHARD_ENABLED
    Q_INVOKABLE void startMicrohardPairing();
#endif
    Q_INVOKABLE void stopPairing();

    Q_PROPERTY(QString          pairingStatusStr        READ pairingStatusStr       NOTIFY pairingStatusChanged)
    Q_PROPERTY(PairingStatus    pairingStatus           READ pairingStatus          NOTIFY pairingStatusChanged)
    Q_PROPERTY(QStringList      pairedDeviceNameList    READ pairedDeviceNameList   NOTIFY pairedListChanged)
    Q_PROPERTY(QStringList      pairingLinkTypeStrings  READ pairingLinkTypeStrings CONSTANT)
Gus Grubba's avatar
Gus Grubba committed
94 95
    Q_PROPERTY(QString          pairedVehicle           READ pairedVehicle          NOTIFY pairedVehicleChanged)
    Q_PROPERTY(bool             errorState              READ errorState             NOTIFY pairingStatusChanged)
Gus Grubba's avatar
Gus Grubba committed
96 97
    Q_PROPERTY(int              nfcIndex                READ nfcIndex               CONSTANT)
    Q_PROPERTY(int              microhardIndex          READ microhardIndex         CONSTANT)
Gus Grubba's avatar
Gus Grubba committed
98
    Q_PROPERTY(bool             firstBoot               READ firstBoot              WRITE setFirstBoot  NOTIFY firstBootChanged)
Gus Grubba's avatar
Gus Grubba committed
99 100

signals:
Gus Grubba's avatar
Gus Grubba committed
101 102 103 104 105 106 107 108 109 110
    void startUpload                            (QString pairURL, QJsonDocument);
    void closeConnection                        ();
    void pairingConfigurationsChanged           ();
    void nameListChanged                        ();
    void pairingStatusChanged                   ();
    void parsePairingJson                       (QString json);
    void setPairingStatus                       (PairingStatus status, QString pairingStatus);
    void pairedListChanged                      ();
    void pairedVehicleChanged                   ();
    void firstBootChanged                       ();
Gus Grubba's avatar
Gus Grubba committed
111 112

private slots:
Gus Grubba's avatar
Gus Grubba committed
113 114 115 116 117
    void _startUpload                           (QString pairURL, QJsonDocument);
    void _stopUpload                            ();
    void _startUploadRequest                    ();
    void _parsePairingJson                      (QString jsonEnc);
    void _setPairingStatus                      (PairingStatus status, QString pairingStatus);
Gus Grubba's avatar
Gus Grubba committed
118 119

private:
Gus Grubba's avatar
Gus Grubba committed
120
    QString                 _statusString;
Gus Grubba's avatar
Gus Grubba committed
121
    QString                 _jsonFileName;
Gus Grubba's avatar
Gus Grubba committed
122
    QString                 _lastPaired;
Gus Grubba's avatar
Gus Grubba committed
123 124 125
    QVariantMap             _remotePairingMap;
    int                     _nfcIndex = -1;
    int                     _microhardIndex = -1;
Gus Grubba's avatar
Gus Grubba committed
126
    int                     _pairRetryCount = 0;
Gus Grubba's avatar
Gus Grubba committed
127 128 129 130 131 132 133
    PairingStatus           _status = PairingIdle;
    AES                     _aes;
    QJsonDocument           _jsonDoc{};
    QMutex                  _uploadMutex{};
    QNetworkAccessManager*  _uploadManager = nullptr;
    QString                 _uploadURL{};
    QString                 _uploadData{};
Gus Grubba's avatar
Gus Grubba committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    bool                    _firstBoot = true;
    QStringList             _deviceList;

    void                    _parsePairingJsonFile       ();
    QJsonDocument           _createZeroTierConnectJson  (QString cert2);
    QJsonDocument           _createMicrohardConnectJson (QString cert2);
    QJsonDocument           _createZeroTierPairingJson  (QString cert1);
    QJsonDocument           _createMicrohardPairingJson (QString pwd, QString cert1);
    QString                 _assumeMicrohardPairingJson ();
    void                    _writeJson                  (QJsonDocument &jsonDoc, QString fileName);
    QString                 _getLocalIPInNetwork        (QString remoteIP, int num);
    void                    _uploadFinished             ();
    void                    _uploadError                (QNetworkReply::NetworkError code);
    void                    _pairingCompleted           (QString name);
    void                    _connectionCompleted        (QString name);
    QDir                    _pairingCacheDir            ();
    QString                 _pairingCacheFile           (QString uavName);
    void                    _updatePairedDeviceNameList ();
Gus Grubba's avatar
Gus Grubba committed
152 153

#if defined QGC_ENABLE_NFC || defined QGC_ENABLE_QTNFC
Gus Grubba's avatar
Gus Grubba committed
154
    PairingNFC              pairingNFC;
Gus Grubba's avatar
Gus Grubba committed
155 156
#endif
};