PairingManager.h 6.64 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
/****************************************************************************
 *
 *   (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_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
55 56
        PairingConnectionRejected,
        PairingError
Gus Grubba's avatar
Gus Grubba committed
57 58 59 60
    };

    Q_ENUM(PairingStatus)

Gus Grubba's avatar
Gus Grubba committed
61 62 63
    QStringList     pairingLinkTypeStrings      ();
    QString         pairingStatusStr            () const;
    QStringList     pairedDeviceNameList        () { return _deviceList; }
Gus Grubba's avatar
Gus Grubba committed
64 65
    PairingStatus   pairingStatus               () { return _status; }
    QString         pairedVehicle               () { return _lastPaired; }
Gus Grubba's avatar
Gus Grubba committed
66 67
    int             nfcIndex                    () { return _nfcIndex; }
    int             microhardIndex              () { return _microhardIndex; }
Gus Grubba's avatar
Gus Grubba committed
68 69 70 71 72
    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
73
#ifdef __android__
Gus Grubba's avatar
Gus Grubba committed
74
    static void     setNativeMethods            (void);
Gus Grubba's avatar
Gus Grubba committed
75
#endif
Gus Grubba's avatar
Gus Grubba committed
76 77
    Q_INVOKABLE void connectToPairedDevice      (QString name);
    Q_INVOKABLE void removePairedDevice         (QString name);
Gus Grubba's avatar
Gus Grubba committed
78

79
#if defined defined QGC_ENABLE_QTNFC
Gus Grubba's avatar
Gus Grubba committed
80 81 82 83 84 85 86 87 88 89 90
    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
91 92
    Q_PROPERTY(QString          pairedVehicle           READ pairedVehicle          NOTIFY pairedVehicleChanged)
    Q_PROPERTY(bool             errorState              READ errorState             NOTIFY pairingStatusChanged)
Gus Grubba's avatar
Gus Grubba committed
93 94
    Q_PROPERTY(int              nfcIndex                READ nfcIndex               CONSTANT)
    Q_PROPERTY(int              microhardIndex          READ microhardIndex         CONSTANT)
Gus Grubba's avatar
Gus Grubba committed
95
    Q_PROPERTY(bool             firstBoot               READ firstBoot              WRITE setFirstBoot  NOTIFY firstBootChanged)
Gus Grubba's avatar
Gus Grubba committed
96 97

signals:
Gus Grubba's avatar
Gus Grubba committed
98 99 100 101 102 103 104 105 106 107
    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
108 109

private slots:
Gus Grubba's avatar
Gus Grubba committed
110 111 112 113 114
    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
115 116

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

150
#if defined QGC_ENABLE_QTNFC
Gus Grubba's avatar
Gus Grubba committed
151
    PairingNFC              pairingNFC;
Gus Grubba's avatar
Gus Grubba committed
152 153
#endif
};