161 lines
3.1 KiB
C++
161 lines
3.1 KiB
C++
#include "Manager.h"
|
|
|
|
#ifdef ANDROID
|
|
#include <QtAndroidExtras/QAndroidJniObject>
|
|
#endif
|
|
|
|
#include <iostream>
|
|
#include <QDebug>
|
|
#include <sstream>
|
|
#include <chrono>
|
|
#include <iomanip>
|
|
|
|
#include <QStandardPaths>
|
|
#include <QtGlobal>
|
|
#include <QDir>
|
|
|
|
const std::string NUC1 = "38:de:ad:6d:77:25";
|
|
const std::string NUC2 = "38:de:ad:6d:60:ff";
|
|
const std::string NUC3 = "1c:1b:b5:ef:a2:9a";
|
|
const std::string NUC4 = "1c:1b:b5:ec:d1:82";
|
|
|
|
|
|
static QString GetCurrentTimeForFileName()
|
|
{
|
|
auto time = std::time(nullptr);
|
|
std::stringstream ss;
|
|
ss << std::put_time(std::localtime(&time), "%F_%T"); // ISO 8601 without timezone information.
|
|
auto s = ss.str();
|
|
std::replace(s.begin(), s.end(), ':', '-');
|
|
return QString::fromStdString(s);
|
|
}
|
|
|
|
Manager::Manager() {
|
|
|
|
|
|
}
|
|
|
|
void Manager::trigger() {
|
|
|
|
QString folder = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation) + "/ftm/";
|
|
|
|
dataLogger = std::make_shared<QFile>(folder+"/ftm_"+GetCurrentTimeForFileName()+".txt");
|
|
|
|
if (!dataLogger->exists()) {
|
|
// create the folder, if necessary
|
|
QDir dir(folder);
|
|
if (!dir.exists()) {
|
|
qWarning("creating new folder");
|
|
if (!dir.mkpath(".")) {
|
|
qWarning() << "Failed to create new folder";
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!dataLogger->open(QIODevice::ReadWrite)) {
|
|
qWarning() << "Failed to create data logger file" << dataLogger->fileName();
|
|
dataLogger = nullptr;
|
|
}
|
|
|
|
#ifdef ANDROID
|
|
|
|
QAndroidJniObject::callStaticMethod<int>("android/net/wifi/RTT", "start", "()I");
|
|
|
|
#else
|
|
|
|
//onData("38:de:ad:6d:77:25;FAILED");
|
|
onData("1337;"+NUC1+";6230;1231");
|
|
onData("1337;"+NUC2+";3430;3423");
|
|
onData("1337;"+NUC3+";5630;2341");
|
|
onData("1337;"+NUC4+";8830;2241");
|
|
|
|
#endif
|
|
}
|
|
|
|
void Manager::stop() {
|
|
#ifdef ANDROID
|
|
QAndroidJniObject::callStaticMethod<int>("android/net/wifi/RTT", "stop", "()I");
|
|
#endif
|
|
|
|
dataLogger->flush();
|
|
dataLogger->close();
|
|
}
|
|
|
|
void Manager::onData(std::string str) {
|
|
|
|
qDebug() << QString(str.c_str());
|
|
|
|
if (dataLogger) {
|
|
dataLogger->write(str.c_str());
|
|
dataLogger->write("\n");
|
|
}
|
|
|
|
std::stringstream lineStream(str);
|
|
std::string cell;
|
|
|
|
std::string mac;
|
|
|
|
int distIndex = 0;
|
|
int i = 0;
|
|
const float alpha = 0.7f;
|
|
|
|
while (std::getline(lineStream, cell, ';')) {
|
|
|
|
switch(i) {
|
|
|
|
case 0: {
|
|
|
|
break;
|
|
}
|
|
case 1: {
|
|
if(NUC1 == cell) {distIndex = 0;}
|
|
if(NUC2 == cell) {distIndex = 1;}
|
|
if(NUC3 == cell) {distIndex = 2;}
|
|
if(NUC4 == cell) {distIndex = 3;}
|
|
break;
|
|
}
|
|
|
|
case 2: {
|
|
if ("FAILED" == cell) {
|
|
_dist[distIndex] = 0;
|
|
} else {
|
|
//_dist[distIndex] = std::stoi(cell);
|
|
_dist[distIndex] = _dist[distIndex] * alpha + atoi(cell.c_str()) * (1-alpha);
|
|
}
|
|
break;
|
|
}
|
|
|
|
case 3: {
|
|
_stdDev[distIndex] = atoi(cell.c_str());
|
|
break;
|
|
}
|
|
|
|
case 4: {
|
|
// RSSI
|
|
}
|
|
|
|
}
|
|
++i;
|
|
}
|
|
|
|
emit distChanged();
|
|
|
|
}
|
|
|
|
Manager mgmt;
|
|
|
|
#ifdef ANDROID
|
|
extern "C" {
|
|
|
|
JNIEXPORT void JNICALL Java_android_net_wifi_RTT_onRTTComplete(JNIEnv* env, jobject jobj, jbyteArray arrayID) {
|
|
(void) env; (void) jobj;
|
|
jsize length = env->GetArrayLength(arrayID);
|
|
jbyte* data = env->GetByteArrayElements(arrayID, 0);
|
|
std::string str((char*)data, length);
|
|
env->ReleaseByteArrayElements(arrayID, data, JNI_ABORT);
|
|
mgmt.onData(str);
|
|
}
|
|
|
|
}
|
|
#endif
|