Skip to content

Commit

Permalink
fixed issues with reconnecting.
Browse files Browse the repository at this point in the history
  • Loading branch information
pollend committed Aug 29, 2018
1 parent 0e2f28a commit b1143e1
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 114 deletions.
14 changes: 14 additions & 0 deletions common/BluetoothAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,17 @@ const QString& BluetoothAddress::getMac() const{
return m_mac;
}



uint64_t BluetoothAddress::toUint64() const{
std::string mac_copy(m_mac.toStdString());
mac_copy.erase(2, 1);
mac_copy.erase(4, 1);
mac_copy.erase(6, 1);
mac_copy.erase(8, 1);
mac_copy.erase(10, 1);

size_t temp;
return std::stoull(mac_copy, &temp, 16);
}

1 change: 1 addition & 0 deletions common/BluetoothAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class BluetoothAddress{

const QString& getTitle() const;
const QString& getMac() const;
uint64_t toUint64() const;

};

Expand Down
25 changes: 9 additions & 16 deletions common/metawearwrapperbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@
#include <QDebug>


MetawearWrapperBase::MetawearWrapperBase():
MetawearWrapperBase::MetawearWrapperBase(const BluetoothAddress& address):
m_metaWearBoard(nullptr),
m_firmwareVersion(""),
m_model(""),
m_isMetawearReady(false),
m_latestEpoch(0)
m_latestEpoch(0),
m_target(address)
{
}

Expand Down Expand Up @@ -128,11 +129,9 @@ void MetawearWrapperBase::configureHandlers() {
mbl_mw_memory_free((void *) dev_info);
qDebug() << "model = " << wrapper->m_model;

if (!wrapper->m_isMetawearReady) {
emit wrapper->metawareInitialized();
emit wrapper->postMetawearInitialized();
}
wrapper->m_isMetawearReady = true;
emit wrapper->metawareInitialized();
emit wrapper->postMetawearInitialized();
emit wrapper->connected();
qDebug() << "Board initialized";
} else {
emit wrapper->metawareFailedToInitialized(status);
Expand Down Expand Up @@ -179,15 +178,12 @@ void MetawearWrapperBase::updateEpoch(qint64 epoch)
}
}

bool MetawearWrapperBase::isMetawearReady(){
return m_isMetawearReady;
}

QString MetawearWrapperBase::getFirmwareVersion(){
const QString& MetawearWrapperBase::getFirmwareVersion() const{
return m_firmwareVersion;
}

QString MetawearWrapperBase::getModel(){
const QString& MetawearWrapperBase::getModel() const{
return m_model;
}

Expand All @@ -196,10 +192,7 @@ qint64 MetawearWrapperBase::getLatestEpoch()
return m_latestEpoch;
}

QString MetawearWrapperBase::getHost() const {
return m_host;
}

QString MetawearWrapperBase::getTarget() const {
const BluetoothAddress& MetawearWrapperBase::getTarget() const {
return m_target;
}
21 changes: 10 additions & 11 deletions common/metawearwrapperbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "metawear/sensor/gyro_bmi160.h"
#include "metawear/sensor/magnetometer_bmm150.h"
#include "metawear/core/datasignal.h"
#include "common/BluetoothAddress.h"


class MetawearWrapperBase : public QObject {
Expand All @@ -36,16 +37,17 @@ Q_OBJECT
QString m_model;
qint64 m_latestEpoch;

QString m_host;
QString m_target;

protected:
MblMwMetaWearBoard *m_metaWearBoard;

BluetoothAddress m_target;
void configureHandlers();

public:
MetawearWrapperBase();
MetawearWrapperBase(const BluetoothAddress& address);

virtual void connectToDevice() = 0;

virtual bool isConnected() const = 0;

void configureAccelerometer(float, float);

Expand Down Expand Up @@ -75,17 +77,14 @@ Q_OBJECT

void updateEpoch(qint64 epoch);

QString getModel();
const QString& getModel() const;

qint64 getLatestEpoch();

QString getFirmwareVersion();

bool isMetawearReady();
const QString& getFirmwareVersion() const;

QString getHost() const;

QString getTarget() const;
const BluetoothAddress& getTarget() const;

signals:

Expand Down
9 changes: 6 additions & 3 deletions forms/sensorpanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ SensorPanel::SensorPanel(const BluetoothAddress &target, QWidget *parent)
m_isReadyToCapture(false){
ui->setupUi(this);
m_reconnectTimer.setSingleShot(true);
m_wrapper->connectToDevice();

connect(&m_reconnectTimer,&QTimer::timeout,this,[=](){
// this->m_wrapper->resetControllerAndTryAgain();
Expand All @@ -55,16 +56,18 @@ SensorPanel::SensorPanel(const BluetoothAddress &target, QWidget *parent)
}
m_reconnectTimer.setInterval(m_reconnectTimer.interval() * 2);
qDebug() << "trying to reconnect to " << m_currentDevice.getMac() << " with timeout " << m_reconnectTimer.interval();

m_reconnectTimer.start();
if(!m_wrapper->isConnected()) {
m_wrapper->connectToDevice();
m_reconnectTimer.start();
}
});

connect(this->m_wrapper,&MetawearWrapper::metawareInitialized,this,&SensorPanel::metawearInitilized);
connect(this->m_wrapper,&MetawearWrapper::disconnected,this,&SensorPanel::disconnect);
connect(this->m_wrapper,&MetawearWrapper::connected,this,&SensorPanel::connected);

connect(this->m_wrapper,&MetawearWrapper::connected,this,[=](){
m_reconnectTimer.stop();
m_reconnectTimer.stop();
m_reconnectTimer.setInterval(0);
});
connect(this->m_wrapper,&MetawearWrapper::disconnected,this,[=](){
Expand Down
3 changes: 1 addition & 2 deletions platform/windows/common/DiscoveryAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ DiscoveryAgent::DiscoveryAgent() : BaseDiscoveryAgent() {

QString mac = QString::fromWCharArray(ret.str().c_str());
QString name = QString::fromWCharArray(localname.c_str());
BluetoothAddress address(mac,name );
// qDebug() << "mac:" << mac << " name: " << name;
BluetoothAddress address(mac,name);
emit deviceDiscovered(address);
});
}
Expand Down
114 changes: 51 additions & 63 deletions platform/windows/common/metawearwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
#include <iomanip>
#include <winsock.h>
#include <QMetaObject>
#include <QDebug>

#define MAX_LEN_UUID_STR 37
//auto leDevice = co_await winrt::Windows::Devices::Bluetooth::BluetoothDevice::FromBluetoothAddressAsync(0);

using namespace Windows::Foundation;


GattCharacteristic^ MetawearWrapper::findCharacterstic(uint64_t low, uint64_t high){


unsigned int data0 = uint8_t((high >> 56) & 0xFF) | uint8_t((high >> 48) & 0xFF) << 8 | uint8_t((high >> 40) & 0xFF) << 16 | uint8_t((high >> 32) & 0xFF) << 24;
unsigned short data1 = uint8_t((high >> 24) & 0xFF) | uint8_t((high >> 16) & 0xFF) << 8;
unsigned short data2 = uint8_t((high >> 8) & 0xFF) | uint8_t(high & 0xFF) << 8;
Expand Down Expand Up @@ -50,6 +50,8 @@ GattCharacteristic^ MetawearWrapper::findCharacterstic(uint64_t low, uint64_t h

}



void MetawearWrapper::read_gatt_char(void *context, const void *caller, const MblMwGattChar *characteristic,
MblMwFnIntVoidPtrArray handler) {
auto *wrapper = static_cast<MetawearWrapper *>(context);
Expand Down Expand Up @@ -119,47 +121,39 @@ void MetawearWrapper::on_disconnect(void *context, const void *caller, MblMwFnVo
}



MetawearWrapper::MetawearWrapper(const BluetoothAddress &target):
MetawearWrapperBase::MetawearWrapperBase(){

std::string mac_copy(target.getMac().toStdString());
mac_copy.erase(2, 1);
mac_copy.erase(4, 1);
mac_copy.erase(6, 1);
mac_copy.erase(8, 1);
mac_copy.erase(10, 1);

size_t temp;
uint64_t mac_ulong = std::stoull(mac_copy.c_str(), &temp, 16);


create_task(BluetoothLEDevice::FromBluetoothAddressAsync(mac_ulong)).then([=](BluetoothLEDevice^ leDevice) {
if (leDevice == nullptr) {
qWarning() << "Failed to discover device";
}
else {
leDevice->ConnectionStatusChanged += ref new TypedEventHandler<BluetoothLEDevice^, Platform::Object^>([=](BluetoothLEDevice^ sender, Platform::Object^ args) {
switch (sender->ConnectionStatus) {
case BluetoothConnectionStatus::Disconnected:
qWarning() << "Failed to connect to device";
// this->cleanup();
break;
}
});
this->m_device = leDevice;
this->startDiscovery();
}
});/* .then([](task<void> previous) {
try {
previous.wait();
} catch (const exception& e) {
qWarning() << QString(e->Message->Data());
} catch (Exception^ e) {
qWarning() << QString(e->Message->Data());
}
});*/
MetawearWrapperBase::MetawearWrapperBase(target){
}

void MetawearWrapper::connectToDevice(){
if(this->m_device == nullptr) {
create_task(BluetoothLEDevice::FromBluetoothAddressAsync(this->m_target.toUint64())).then(
[=](BluetoothLEDevice^leDevice) {
if (leDevice == nullptr) {
qWarning() << "Failed to discover device";
} else {
leDevice->ConnectionStatusChanged += ref new TypedEventHandler<BluetoothLEDevice ^ , Platform::Object^>(
[=](BluetoothLEDevice^sender, Platform::Object^args) {
switch (sender->ConnectionStatus) {
case BluetoothConnectionStatus::Disconnected:
qWarning() << "Failed to connect to device";
emit disconnected();
break;
}
});
this->m_device = leDevice;
this->startDiscovery();
}
});
}
else{
qDebug() << "Device already connected";
}
}


bool MetawearWrapper::isConnected() const{
return this->m_device != nullptr;
}

void MetawearWrapper::startDiscovery() {
Expand All @@ -172,13 +166,12 @@ void MetawearWrapper::startDiscovery() {
for (uint x = 0; x < result->Services->Size; ++x) {
auto service = result->Services->GetAt(x);
m_services.emplace(service->Uuid, service);
find_gattchar_tasks.push_back(
create_task(service->GetCharacteristicsAsync(BluetoothCacheMode::Uncached)));
find_gattchar_tasks.push_back(create_task(service->GetCharacteristicsAsync(BluetoothCacheMode::Uncached)));
}
return when_all(std::begin(find_gattchar_tasks), std::end(find_gattchar_tasks));
}
qWarning() << "Failed fo discover Gatt service";
throw ref new Platform::Exception(-1, "Failed fo discover Gatt service");
qWarning() << "Failed to discover Gatt service";
throw ref new Platform::Exception(-1, "Failed to discover Gatt service");
}).then([=](std::vector<GattCharacteristicsResult ^ > results) {
for (auto it : results) {
if (it->Status == GattCommunicationStatus::Success) {
Expand All @@ -205,29 +198,24 @@ void MetawearWrapper::startDiscovery() {
}
catch (Platform::Exception^e) {
qWarning() << QString::fromWCharArray(e->Message->Data());
this->cleanup();

for (auto it : m_characterstics)
delete it.second;
for (auto it : m_services)
delete it.second;

m_characterstics.clear();
m_services.clear();
if (m_device != nullptr) {
delete m_device;
m_device = nullptr;
}
emit disconnected();
}
}

MetawearWrapper::~MetawearWrapper() {
cleanup();
}

void MetawearWrapper::cleanup()
{
qDebug() << "Cleanup";

for (auto it : m_characterstics)
delete it.second;
for (auto it : m_services)
delete it.second;

m_characterstics.clear();
m_services.clear();
if (m_device != nullptr) {
delete m_device;
m_device = nullptr;
}
}


Loading

0 comments on commit b1143e1

Please sign in to comment.