PairingManager.cc 18.8 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 58 59 60 61 62 63 64
/****************************************************************************
 *
 *   (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.
 *
 ****************************************************************************/

#include "PairingManager.h"
#include "SettingsManager.h"
#include "MicrohardManager.h"
#include "QGCApplication.h"
#include "QGCCorePlugin.h"

#include <QSettings>
#include <QJsonObject>
#include <QStandardPaths>
#include <QMutexLocker>

QGC_LOGGING_CATEGORY(PairingManagerLog, "PairingManagerLog")

static const char* jsonFileName = "pairing.json";

//-----------------------------------------------------------------------------
static QString
random_string(uint length)
{
    auto randchar = []() -> char
    {
        const char charset[] =
            "0123456789"
            "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
            "abcdefghijklmnopqrstuvwxyz";
        const uint max_index = (sizeof(charset) - 1);
        return charset[static_cast<uint>(rand()) % max_index];
    };
    std::string str(length, 0);
    std::generate_n(str.begin(), length, randchar);
    return QString::fromStdString(str);
}

//-----------------------------------------------------------------------------
PairingManager::PairingManager(QGCApplication* app, QGCToolbox* toolbox)
    : QGCTool(app, toolbox)
    , _aes("J6+KuWh9K2!hG(F'", 0x368de30e8ec063ce)
{
    _jsonFileName = QDir::temp().filePath(jsonFileName);
    connect(this, &PairingManager::parsePairingJson, this, &PairingManager::_parsePairingJson);
    connect(this, &PairingManager::setPairingStatus, this, &PairingManager::_setPairingStatus);
    connect(this, &PairingManager::startUpload, this, &PairingManager::_startUpload);
}

//-----------------------------------------------------------------------------
PairingManager::~PairingManager()
{
}

//-----------------------------------------------------------------------------

void
PairingManager::setToolbox(QGCToolbox *toolbox)
{
    QGCTool::setToolbox(toolbox);
Gus Grubba's avatar
Gus Grubba committed
65 66
    _updatePairedDeviceNameList();
    emit pairedListChanged();
Gus Grubba's avatar
Gus Grubba committed
67 68 69 70 71 72 73 74
}

//-----------------------------------------------------------------------------
void
PairingManager::_pairingCompleted(QString name)
{
    _writeJson(_jsonDoc, _pairingCacheFile(name));
    _remotePairingMap["NM"] = name;
Gus Grubba's avatar
Gus Grubba committed
75
    _lastPaired = name;
Gus Grubba's avatar
Gus Grubba committed
76
    _updatePairedDeviceNameList();
Gus Grubba's avatar
Gus Grubba committed
77
    emit pairedListChanged();
Gus Grubba's avatar
Gus Grubba committed
78
    emit pairedVehicleChanged();
Gus Grubba's avatar
Gus Grubba committed
79
    //_app->informationMessageBoxOnMainThread("", tr("Paired with %1").arg(name));
Gus Grubba's avatar
Gus Grubba committed
80
    setPairingStatus(PairingSuccess, tr("Pairing Successfull"));
Gus Grubba's avatar
Gus Grubba committed
81 82 83 84
}

//-----------------------------------------------------------------------------
void
Gus Grubba's avatar
Gus Grubba committed
85
PairingManager::_connectionCompleted(QString /*name*/)
Gus Grubba's avatar
Gus Grubba committed
86 87 88
{
    //QString pwd = _remotePairingMap["PWD"].toString();
    //_toolbox->microhardManager()->switchToConnectionEncryptionKey(pwd);
Gus Grubba's avatar
Gus Grubba committed
89
    //_app->informationMessageBoxOnMainThread("", tr("Connected to %1").arg(name));
Gus Grubba's avatar
Gus Grubba committed
90
    setPairingStatus(PairingConnected, tr("Connection Successfull"));
Gus Grubba's avatar
Gus Grubba committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
}

//-----------------------------------------------------------------------------
void
PairingManager::_startUpload(QString pairURL, QJsonDocument jsonDoc)
{
    QMutexLocker lock(&_uploadMutex);
    if (_uploadManager != nullptr) {
        return;
    }
    _uploadManager = new QNetworkAccessManager(this);

    QString str = jsonDoc.toJson(QJsonDocument::JsonFormat::Compact);
    qCDebug(PairingManagerLog) << "Starting upload to: " << pairURL << " " << str;
    _uploadData = QString::fromStdString(_aes.encrypt(str.toStdString()));
    _uploadURL = pairURL;
    _startUploadRequest();
}

//-----------------------------------------------------------------------------
void
PairingManager::_startUploadRequest()
{
    QNetworkRequest req;
    req.setUrl(QUrl(_uploadURL));
    req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    QNetworkReply *reply = _uploadManager->post(req, _uploadData.toUtf8());
    connect(reply, &QNetworkReply::finished, this, &PairingManager::_uploadFinished);
}

//-----------------------------------------------------------------------------
void
PairingManager::_stopUpload()
{
    QMutexLocker lock(&_uploadMutex);
    if (_uploadManager != nullptr) {
        delete _uploadManager;
        _uploadManager = nullptr;
    }
}

//-----------------------------------------------------------------------------
void
Gus Grubba's avatar
Gus Grubba committed
134
PairingManager::_uploadFinished()
Gus Grubba's avatar
Gus Grubba committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
{
    QMutexLocker lock(&_uploadMutex);
    QNetworkReply* reply = qobject_cast<QNetworkReply*>(QObject::sender());
    if (reply) {
        if (_uploadManager != nullptr) {
            if (reply->error() == QNetworkReply::NoError) {
                qCDebug(PairingManagerLog) << "Upload finished.";
                QByteArray bytes = reply->readAll();
                QString str = QString::fromUtf8(bytes.data(), bytes.size());
                qCDebug(PairingManagerLog) << "Reply: " << str;
                auto a = str.split(QRegExp("\\s+"));
                if (a[0] == "Accepted" && a.length() > 1) {
                    _pairingCompleted(a[1]);
                } else if (a[0] == "Connected" && a.length() > 1) {
                    _connectionCompleted(a[1]);
                } else if (a[0] == "Connection" && a.length() > 1) {
Gus Grubba's avatar
Gus Grubba committed
151
                    setPairingStatus(PairingConnectionRejected, tr("Connection Rejected"));
Gus Grubba's avatar
Gus Grubba committed
152 153
                    qCDebug(PairingManagerLog) << "Connection error: " << str;
                } else {
Gus Grubba's avatar
Gus Grubba committed
154
                    setPairingStatus(PairingRejected, tr("Pairing Rejected"));
Gus Grubba's avatar
Gus Grubba committed
155 156
                    qCDebug(PairingManagerLog) << "Pairing error: " << str;
                }
Gus Grubba's avatar
Gus Grubba committed
157
                _uploadManager->deleteLater();
Gus Grubba's avatar
Gus Grubba committed
158 159
                _uploadManager = nullptr;
            } else {
Gus Grubba's avatar
Gus Grubba committed
160 161
                if(++_pairRetryCount > 3) {
                    qCDebug(PairingManagerLog) << "Giving up";
Gus Grubba's avatar
Gus Grubba committed
162
                    setPairingStatus(PairingError, tr("No Response From Vehicle"));
Gus Grubba's avatar
Gus Grubba committed
163 164 165 166 167 168
                    _uploadManager->deleteLater();
                    _uploadManager = nullptr;
                } else {
                    qCDebug(PairingManagerLog) << "Upload error: " + reply->errorString();
                    _startUploadRequest();
                }
Gus Grubba's avatar
Gus Grubba committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
            }
        }
    }
}

//-----------------------------------------------------------------------------
void
PairingManager::_parsePairingJsonFile()
{
    QFile file(_jsonFileName);
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    QString json = file.readAll();
    file.remove();
    file.close();

    jsonReceived(json);
}

//-----------------------------------------------------------------------------
void
PairingManager::connectToPairedDevice(QString name)
{
Gus Grubba's avatar
Gus Grubba committed
191
    setPairingStatus(PairingConnecting, tr("Connecting to %1").arg(name));
Gus Grubba's avatar
Gus Grubba committed
192 193 194 195 196 197
    QFile file(_pairingCacheFile(name));
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    QString json = file.readAll();
    jsonReceived(json);
}

Gus Grubba's avatar
Gus Grubba committed
198 199 200 201 202 203
//-----------------------------------------------------------------------------
void
PairingManager::removePairedDevice(QString name)
{
    QFile file(_pairingCacheFile(name));
    file.remove();
Gus Grubba's avatar
Gus Grubba committed
204
    _updatePairedDeviceNameList();
Gus Grubba's avatar
Gus Grubba committed
205 206 207
    emit pairedListChanged();
}

Gus Grubba's avatar
Gus Grubba committed
208
//-----------------------------------------------------------------------------
Gus Grubba's avatar
Gus Grubba committed
209 210
void
PairingManager::_updatePairedDeviceNameList()
Gus Grubba's avatar
Gus Grubba committed
211
{
Gus Grubba's avatar
Gus Grubba committed
212
    _deviceList.clear();
Gus Grubba's avatar
Gus Grubba committed
213 214 215
    QDirIterator it(_pairingCacheDir().absolutePath(), QDir::Files);
    while (it.hasNext()) {
        QFileInfo fileInfo(it.next());
Gus Grubba's avatar
Gus Grubba committed
216
        _deviceList.append(fileInfo.fileName());
Gus Grubba's avatar
Gus Grubba committed
217
        qCDebug(PairingManagerLog) << "Listing: " << fileInfo.fileName();
Gus Grubba's avatar
Gus Grubba committed
218 219 220 221 222 223 224 225 226 227 228 229 230
    }
}

//-----------------------------------------------------------------------------
QString
PairingManager::_assumeMicrohardPairingJson()
{
    QJsonDocument json;
    QJsonObject   jsonObject;

    jsonObject.insert("LT", "MH");
    jsonObject.insert("IP", "192.168.168.10");
    jsonObject.insert("AIP", _toolbox->microhardManager()->remoteIPAddr());
Gus Grubba's avatar
Gus Grubba committed
231 232 233
    jsonObject.insert("CU",  _toolbox->microhardManager()->configUserName());
    jsonObject.insert("CP",  _toolbox->microhardManager()->configPassword());
    jsonObject.insert("EK",  _toolbox->microhardManager()->encryptionKey());
Gus Grubba's avatar
Gus Grubba committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
    json.setObject(jsonObject);

    return QString(json.toJson(QJsonDocument::Compact));
}

//-----------------------------------------------------------------------------
void
PairingManager::_parsePairingJson(QString jsonEnc)
{
    QString json = QString::fromStdString(_aes.decrypt(jsonEnc.toStdString()));
    if (json == "") {
        json = jsonEnc;
    }
    qCDebug(PairingManagerLog) << "Parsing JSON: " << json;

    _jsonDoc = QJsonDocument::fromJson(json.toUtf8());

    if (_jsonDoc.isNull()) {
Gus Grubba's avatar
Gus Grubba committed
252
        setPairingStatus(PairingError, tr("Invalid Pairing File"));
Gus Grubba's avatar
Gus Grubba committed
253 254 255 256
        qCDebug(PairingManagerLog) << "Failed to create Pairing JSON doc.";
        return;
    }
    if (!_jsonDoc.isObject()) {
Gus Grubba's avatar
Gus Grubba committed
257
        setPairingStatus(PairingError, tr("Error Parsing Pairing File"));
Gus Grubba's avatar
Gus Grubba committed
258 259 260 261 262 263 264
        qCDebug(PairingManagerLog) << "Pairing JSON is not an object.";
        return;
    }

    QJsonObject jsonObj = _jsonDoc.object();

    if (jsonObj.isEmpty()) {
Gus Grubba's avatar
Gus Grubba committed
265
        setPairingStatus(PairingError, tr("Error Parsing Pairing File"));
Gus Grubba's avatar
Gus Grubba committed
266 267 268 269 270
        qCDebug(PairingManagerLog) << "Pairing JSON object is empty.";
        return;
    }

    _remotePairingMap = jsonObj.toVariantMap();
Gus Grubba's avatar
Gus Grubba committed
271 272
    QString linkType  = _remotePairingMap["LT"].toString();
    QString pport     = _remotePairingMap["PP"].toString();
Gus Grubba's avatar
Gus Grubba committed
273 274 275 276 277
    if (pport.length()==0) {
        pport = "29351";
    }

    if (linkType.length()==0) {
Gus Grubba's avatar
Gus Grubba committed
278
        setPairingStatus(PairingError, tr("Error Parsing Pairing File"));
Gus Grubba's avatar
Gus Grubba committed
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
        qCDebug(PairingManagerLog) << "Pairing JSON is malformed.";
        return;
    }

    _toolbox->microhardManager()->switchToPairingEncryptionKey();

    QString pairURL = "http://" + _remotePairingMap["IP"].toString() + ":" + pport;
    bool connecting = jsonObj.contains("PWD");
    QJsonDocument jsonDoc;

    if (!connecting) {
        pairURL +=  + "/pair";
        QString pwd = random_string(8);
        // TODO generate certificates
        QString cert1 = "";
        QString cert2 = "";
        jsonObj.insert("PWD", pwd);
        jsonObj.insert("CERT1", cert1);
        jsonObj.insert("CERT2", cert2);
        _jsonDoc.setObject(jsonObj);
        if (linkType == "ZT") {
            jsonDoc = _createZeroTierPairingJson(cert1);
        } else if (linkType == "MH") {
            jsonDoc = _createMicrohardPairingJson(pwd, cert1);
        }
    } else {
        pairURL +=  + "/connect";
        QString cert2 = _remotePairingMap["CERT2"].toString();
        if (linkType == "ZT") {
            jsonDoc = _createZeroTierConnectJson(cert2);
        } else if (linkType == "MH") {
            jsonDoc = _createMicrohardConnectJson(cert2);
        }
    }

    if (linkType == "ZT") {
        _toolbox->settingsManager()->appSettings()->enableMicrohard()->setRawValue(false);
        _toolbox->settingsManager()->appSettings()->enableTaisync()->setRawValue(false);
        emit startUpload(pairURL, jsonDoc);
    } else if (linkType == "MH") {
        _toolbox->settingsManager()->appSettings()->enableMicrohard()->setRawValue(true);
        _toolbox->settingsManager()->appSettings()->enableTaisync()->setRawValue(false);
        if (_remotePairingMap.contains("AIP")) {
            _toolbox->microhardManager()->setRemoteIPAddr(_remotePairingMap["AIP"].toString());
        }
        if (_remotePairingMap.contains("CU")) {
            _toolbox->microhardManager()->setConfigUserName(_remotePairingMap["CU"].toString());
        }
        if (_remotePairingMap.contains("CP")) {
            _toolbox->microhardManager()->setConfigPassword(_remotePairingMap["CP"].toString());
        }
        if (_remotePairingMap.contains("EK") && !connecting) {
            _toolbox->microhardManager()->setEncryptionKey(_remotePairingMap["EK"].toString());
        }
        _toolbox->microhardManager()->updateSettings();
        emit startUpload(pairURL, jsonDoc);
    }
}

//-----------------------------------------------------------------------------
QString
PairingManager::_getLocalIPInNetwork(QString remoteIP, int num)
{
    QStringList pieces = remoteIP.split(".");
    QString ipPrefix = "";
    for (int i = 0; i<num && i<pieces.length(); i++) {
        ipPrefix += pieces[i] + ".";
    }

    const QHostAddress &localhost = QHostAddress(QHostAddress::LocalHost);
    for (const QHostAddress &address: QNetworkInterface::allAddresses()) {
        if (address.protocol() == QAbstractSocket::IPv4Protocol && address != localhost) {
            if (address.toString().startsWith(ipPrefix)) {
                return address.toString();
            }
        }
    }

    return "";
}

//-----------------------------------------------------------------------------
QDir
PairingManager::_pairingCacheDir()
{
    const QString spath(QFileInfo(QSettings().fileName()).dir().absolutePath());
    QDir dir = spath + QDir::separator() + "PairingCache";
    if (!dir.exists()) {
        dir.mkpath(".");
    }

    return dir;
}

//-----------------------------------------------------------------------------
QString
PairingManager::_pairingCacheFile(QString uavName)
{
    return _pairingCacheDir().filePath(uavName);
}

//-----------------------------------------------------------------------------
void
PairingManager::_writeJson(QJsonDocument &jsonDoc, QString fileName)
{
    QString val = jsonDoc.toJson(QJsonDocument::JsonFormat::Compact);
    qCDebug(PairingManagerLog) << "Write json " << val;
    QString enc = QString::fromStdString(_aes.encrypt(val.toStdString()));

    QFile file(fileName);
    file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate);
    file.write(enc.toUtf8());
    file.close();
}

//-----------------------------------------------------------------------------
QJsonDocument
PairingManager::_createZeroTierPairingJson(QString cert1)
{
    QString localIP = _getLocalIPInNetwork(_remotePairingMap["IP"].toString(), 2);

    QJsonObject jsonObj;
    jsonObj.insert("LT", "ZT");
    jsonObj.insert("IP", localIP);
    jsonObj.insert("P", 14550);
    jsonObj.insert("CERT1", cert1);
    return QJsonDocument(jsonObj);
}

//-----------------------------------------------------------------------------
QJsonDocument
PairingManager::_createMicrohardPairingJson(QString pwd, QString cert1)
{
    QString localIP = _getLocalIPInNetwork(_remotePairingMap["IP"].toString(), 3);

    QJsonObject jsonObj;
    jsonObj.insert("LT", "MH");
    jsonObj.insert("IP", localIP);
    jsonObj.insert("P", 14550);
    jsonObj.insert("PWD", pwd);
    jsonObj.insert("CERT1", cert1);
    return QJsonDocument(jsonObj);
}

//-----------------------------------------------------------------------------
QJsonDocument
PairingManager::_createZeroTierConnectJson(QString cert2)
{
    QString localIP = _getLocalIPInNetwork(_remotePairingMap["IP"].toString(), 2);

    QJsonObject jsonObj;
    jsonObj.insert("LT", "ZT");
    jsonObj.insert("IP", localIP);
    jsonObj.insert("P", 14550);
    jsonObj.insert("CERT2", cert2);
    return QJsonDocument(jsonObj);
}

//-----------------------------------------------------------------------------
QJsonDocument
PairingManager::_createMicrohardConnectJson(QString cert2)
{
    QString localIP = _getLocalIPInNetwork(_remotePairingMap["IP"].toString(), 3);

    QJsonObject jsonObj;
    jsonObj.insert("LT", "MH");
    jsonObj.insert("IP", localIP);
    jsonObj.insert("P", 14550);
    jsonObj.insert("CERT2", cert2);
    return QJsonDocument(jsonObj);
}

//-----------------------------------------------------------------------------

QStringList
Gus Grubba's avatar
Gus Grubba committed
454
PairingManager::pairingLinkTypeStrings()
Gus Grubba's avatar
Gus Grubba committed
455 456 457 458 459
{
    //-- Must follow same order as enum LinkType in LinkConfiguration.h
    static QStringList list;
    int i = 0;
    if (!list.size()) {
460
#if defined QGC_ENABLE_QTNFC
Gus Grubba's avatar
Gus Grubba committed
461 462 463 464 465 466 467 468 469 470 471 472 473
        list += tr("NFC");
        _nfcIndex = i++;
#endif
#if defined QGC_GST_MICROHARD_ENABLED
        list += tr("Microhard");
        _microhardIndex = i++;
#endif
    }
    return list;
}

//-----------------------------------------------------------------------------
void
Gus Grubba's avatar
Gus Grubba committed
474
PairingManager::_setPairingStatus(PairingStatus status, QString statusStr)
Gus Grubba's avatar
Gus Grubba committed
475
{
Gus Grubba's avatar
Gus Grubba committed
476 477
    _status = status;
    _statusString = statusStr;
Gus Grubba's avatar
Gus Grubba committed
478 479 480 481 482
    emit pairingStatusChanged();
}

//-----------------------------------------------------------------------------
QString
Gus Grubba's avatar
Gus Grubba committed
483
PairingManager::pairingStatusStr() const
Gus Grubba's avatar
Gus Grubba committed
484
{
Gus Grubba's avatar
Gus Grubba committed
485
    return _statusString;
Gus Grubba's avatar
Gus Grubba committed
486 487 488 489 490 491 492 493
}

#if QGC_GST_MICROHARD_ENABLED
//-----------------------------------------------------------------------------
void
PairingManager::startMicrohardPairing()
{
    stopPairing();
Gus Grubba's avatar
Gus Grubba committed
494 495
    _pairRetryCount = 0;
    setPairingStatus(PairingActive, tr("Pairing..."));
Gus Grubba's avatar
Gus Grubba committed
496 497 498 499 500 501 502 503
    _parsePairingJson(_assumeMicrohardPairingJson());
}
#endif

//-----------------------------------------------------------------------------
void
PairingManager::stopPairing()
{
504
#if defined QGC_ENABLE_QTNFC
Gus Grubba's avatar
Gus Grubba committed
505 506 507
    pairingNFC.stop();
#endif
    _stopUpload();
Gus Grubba's avatar
Gus Grubba committed
508
    setPairingStatus(PairingIdle, "");
Gus Grubba's avatar
Gus Grubba committed
509 510
}

511
#if defined QGC_ENABLE_QTNFC
Gus Grubba's avatar
Gus Grubba committed
512 513 514 515 516
//-----------------------------------------------------------------------------
void
PairingManager::startNFCScan()
{
    stopPairing();
Gus Grubba's avatar
Gus Grubba committed
517
    setPairingStatus(PairingActive, tr("Pairing..."));
Gus Grubba's avatar
Gus Grubba committed
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
    pairingNFC.start();
}

#endif

//-----------------------------------------------------------------------------
#ifdef __android__
static const char kJniClassName[] {"org/mavlink/qgroundcontrol/QGCActivity"};

//-----------------------------------------------------------------------------
static void jniNFCTagReceived(JNIEnv *envA, jobject thizA, jstring messageA)
{
    Q_UNUSED(thizA);

    const char *stringL = envA->GetStringUTFChars(messageA, nullptr);
    QString ndef = QString::fromUtf8(stringL);
    envA->ReleaseStringUTFChars(messageA, stringL);
    if (envA->ExceptionCheck())
        envA->ExceptionClear();
    qCDebug(PairingManagerLog) << "NDEF Tag Received: " << ndef;
    qgcApp()->toolbox()->pairingManager()->jsonReceived(ndef);
}

//-----------------------------------------------------------------------------
void PairingManager::setNativeMethods(void)
{
    //  REGISTER THE C++ FUNCTION WITH JNI
    JNINativeMethod javaMethods[] {
        {"nativeNFCTagReceived", "(Ljava/lang/String;)V", reinterpret_cast<void *>(jniNFCTagReceived)}
    };

    QAndroidJniEnvironment jniEnv;
    if (jniEnv->ExceptionCheck()) {
        jniEnv->ExceptionDescribe();
        jniEnv->ExceptionClear();
    }

    jclass objectClass = jniEnv->FindClass(kJniClassName);
    if(!objectClass) {
        qWarning() << "Couldn't find class:" << kJniClassName;
        return;
    }

    jint val = jniEnv->RegisterNatives(objectClass, javaMethods, sizeof(javaMethods) / sizeof(javaMethods[0]));

    if (val < 0) {
        qWarning() << "Error registering methods: " << val;
    } else {
        qCDebug(PairingManagerLog) << "Native Functions Registered";
    }

    if (jniEnv->ExceptionCheck()) {
        jniEnv->ExceptionDescribe();
        jniEnv->ExceptionClear();
    }
}
#endif
//-----------------------------------------------------------------------------