Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix several clang-tidiy issues #6

Merged
merged 3 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/BleClient.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _SENSIRION_UPT_BLE_AUTO_DETECTION_BLECLIENT_H
#define _SENSIRION_UPT_BLE_AUTO_DETECTION_BLECLIENT_H
#ifndef SENSIRION_UPT_BLE_AUTO_DETECTION_BLE_CLIENT_H
#define SENSIRION_UPT_BLE_AUTO_DETECTION_BLE_CLIENT_H

#include "Arduino.h"
#include "BleClientCallback.h"
Expand All @@ -13,4 +13,4 @@ class BleClient {
virtual void keepAlive() = 0;
};

#endif /* _SENSIRION_UPT_BLE_AUTO_DETECTION_BLECLIENT_H */
#endif /* SENSIRION_UPT_BLE_AUTO_DETECTION_BLE_CLIENT_H */
13 changes: 6 additions & 7 deletions src/BleClientCallback.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#ifndef _SENSIRION_UPT_BLE_AUTO_DETECTION_BLECLIENTCALLBACK_H
#define _SENSIRION_UPT_BLE_AUTO_DETECTION_BLECLIENTCALLBACK_H
#ifndef SENSIRION_UPT_BLE_AUTO_DETECTION_BLE_CLIENT_CALLBACK_H
#define SENSIRION_UPT_BLE_AUTO_DETECTION_BLE_CLIENT_CALLBACK_H

class BleClientCallback {
public:
virtual ~BleClientCallback() {
}
virtual ~BleClientCallback() = default;

virtual void onAdvertisementReceived(std::string address, std::string name,
std::string data);
virtual void onAdvertisementReceived(uint64_t address, std::string name,
std::string data) = 0;
};

#endif /* _SENSIRION_UPT_BLE_AUTO_DETECTION_BLECLIENTCALLBACK_H */
#endif /* SENSIRION_UPT_BLE_AUTO_DETECTION_BLE_CLIENT_CALLBACK_H */
77 changes: 45 additions & 32 deletions src/NimBleClient.cpp
Original file line number Diff line number Diff line change
@@ -1,40 +1,33 @@
#include "NimBleClient.h"

void NimBleClient::begin(BleClientCallback* callback) {
_callback = callback;
setupAndStartBleScans();
};

void NimBleClient::keepAlive() {
// If an error occurs that stops the scan, it will be restarted here.
if (_bleScan->isScanning() == false) {
// Start scan with: duration = 0 seconds(forever), no scan end callback,
// not a continuation of a previous scan.
_bleScan->start(0, nullptr, false);
}
}

void NimBleClient::setupAndStartBleScans() {
NimBleClient::NimBleClient() : _callback(nullptr) {
// CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE (2)
// Filter by address and data, advertisements from the same address will be
// reported only once, except if the data in the advertisement has changed,
// then it will be reported again.
NimBLEDevice::setScanFilterMode(CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE);
NimBLEDevice::setScanDuplicateCacheSize(200);
NimBLEDevice::init("");

// create new scan
_bleScan = NimBLEDevice::getScan();
// Activate callback on advertisement update
_bleScan->setAdvertisedDeviceCallbacks(this, true);
// Set active scanning, this will get more data from the advertiser.
_bleScan->setActiveScan(true);
// How often the scan occurs / switches channels; in milliseconds,
_bleScan->setInterval(97);
// How long to scan during the interval; in milliseconds.
_bleScan->setWindow(37);
// do not store the scan results, use callback only.
_bleScan->setMaxResults(0);
setupBleScans();
}

NimBleClient::~NimBleClient() {
_bleScan->stop();
}

void NimBleClient::begin(BleClientCallback* callback) {
_callback = callback;
startBleScans();
}

void NimBleClient::keepAlive() {
// If an error occurs that stops the scan, it will be restarted here.
if (!_bleScan->isScanning()) {
// Start scan with: duration = 0 seconds(forever), no scan end callback,
// not a continuation of a previous scan.
startBleScans();
}
}

void NimBleClient::onResult(NimBLEAdvertisedDevice* advertisedDevice) {
Expand All @@ -43,11 +36,12 @@ void NimBleClient::onResult(NimBLEAdvertisedDevice* advertisedDevice) {
}

// MAC address contains 6 bytes of MAC address (in reversed order)
// (Note: advertisedDevice->getAddress().toString() seems broken)
const uint8_t* bleMACAddress = advertisedDevice->getAddress().getNative();
std::string address;
for (int i = 5; i >= 0; i--) {
address.push_back(bleMACAddress[i]);
uint64_t address = 0;
size_t address_size = 6;
// reverse MAC address and store it as 64-bit unsigned int
for (int ix = 0; ix < address_size; ix++) {
address = (address << 8) | bleMACAddress[address_size - 1 - ix];
}

std::string name = advertisedDevice->haveName()
Expand All @@ -56,4 +50,23 @@ void NimBleClient::onResult(NimBLEAdvertisedDevice* advertisedDevice) {
std::string manufacturerData = advertisedDevice->getManufacturerData();

_callback->onAdvertisementReceived(address, name, manufacturerData);
};
}

void NimBleClient::setupBleScans() {
// Activate callback on advertisement update
_bleScan->setAdvertisedDeviceCallbacks(this, true);
// Set active scanning, this will get more data from the advertiser.
_bleScan->setActiveScan(true);
// How often the scan occurs / switches channels; in milliseconds,
_bleScan->setInterval(97);
// How long to scan during the interval; in milliseconds.
_bleScan->setWindow(37);
// do not store the scan results, use callback only.
_bleScan->setMaxResults(0);
}

void NimBleClient::startBleScans() {
// Start scan with: duration = 0 seconds(forever), no scan end callback,
// not a continuation of a previous scan.
_bleScan->start(0, nullptr, false);
}
18 changes: 11 additions & 7 deletions src/NimBleClient.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
#ifndef _SENSIRION_UPT_BLE_AUTO_DETECTION_NIMBLECLIENT_H
#define _SENSIRION_UPT_BLE_AUTO_DETECTION_NIMBLECLIENT_H
#ifndef SENSIRION_UPT_BLE_AUTO_DETECTION_NIMBLE_CLIENT_H
#define SENSIRION_UPT_BLE_AUTO_DETECTION_NIMBLE_CLIENT_H

#include "BleClient.h"
#include "NimBLEDevice.h"

class NimBleClient: public BleClient, public NimBLEAdvertisedDeviceCallbacks {
class __attribute__((unused)) NimBleClient
: public BleClient,
public NimBLEAdvertisedDeviceCallbacks {
public:
NimBleClient() : _bleScan(nullptr), _callback(nullptr){};
NimBleClient();
~NimBleClient();
void begin(BleClientCallback* callback) override;
void keepAlive() override;

private:
NimBLEScan* _bleScan;
BleClientCallback* _callback;
void setupAndStartBleScans();
void onResult(NimBLEAdvertisedDevice* advertisedDevice);
void setupBleScans();
void startBleScans();
void onResult(NimBLEAdvertisedDevice* advertisedDevice) override;
};

#endif /* _SENSIRION_UPT_BLE_AUTO_DETECTION_NIMBLECLIENT_H */
#endif /* SENSIRION_UPT_BLE_AUTO_DETECTION_NIMBLE_CLIENT_H */
37 changes: 9 additions & 28 deletions src/Sensirion_upt_ble_auto_detection.cpp
Original file line number Diff line number Diff line change
@@ -1,39 +1,36 @@
#include "Sensirion_upt_ble_auto_detection.h"
#include "Arduino.h"
#include "NimBleClient.h"

const int COMPANY_ID_FILTER = 54534;

void SensiScan::begin() {
_bleClient = new NimBleClient();
_bleClient->begin(this);
_bleClient->keepAlive();
}

void SensiScan::getScanResults(
__attribute__((unused)) void SensiScan::getScanResults(
std::map<uint16_t, std::vector<Measurement>>& scanResults) {
for (const auto& cachedSample : _sampleCache) {
scanResults[cachedSample.first] = _sampleCache[cachedSample.first];
scanResults[cachedSample.first] = cachedSample.second;
}
_sampleCache.clear();
}

void SensiScan::keepAlive() {
__attribute__((unused)) void SensiScan::keepAlive() {
_bleClient->keepAlive();
}

void SensiScan::onAdvertisementReceived(const std::string address,
std::string name, std::string data) {
void SensiScan::onAdvertisementReceived(uint64_t address, std::string name,
std::string data) {
uint16_t companyId = (uint16_t)data[0] << 8 | (uint8_t)data[1];
if (companyId != COMPANY_ID_FILTER) {
return;
}

// Get MAC address as uint64_t
uint64_t deviceID = squashMACAddress(address);

// Build MetaData
MetaData metaData;
metaData.deviceID = deviceID;
metaData.deviceID = address;
metaData.deviceType.bleGadgetType =
bleGadgetTypeFromCompleteLocalName(name.c_str());
metaData.platform = DevicePlatform::BLE;
Expand All @@ -45,24 +42,10 @@ void SensiScan::onAdvertisementReceived(const std::string address,
return;
}

// Last two digits of MAC addr. suffice to uniquely ID a BLE device
// Last two digits of MAC addr. Suffice to uniquely ID a BLE device
_sampleCache[getDeviceId(data)] = samples;
}

/**
* @brief squash std::string address to a uint64_t
* @note MAC address is comprised of 6 bytes
*/
uint64_t SensiScan::squashMACAddress(const std::string& macAddressString) {

uint64_t deviceID = macAddressString[0];
for (size_t i = 1; i < 6; i++) {
deviceID = (deviceID << 8) | macAddressString[i];
}

return deviceID;
}

/**
* @brief last two digits of MAC address uniquely define a device
*/
Expand All @@ -71,8 +54,6 @@ uint16_t SensiScan::getDeviceId(const std::string& data) {
return deviceId;
}

extern std::map<DataType, SampleConfig> sampleConfigSelector;

/**
* @brief decode chunk of Advertisement containing encoded samples
*
Expand All @@ -81,7 +62,7 @@ extern std::map<DataType, SampleConfig> sampleConfigSelector;
*/
uint8_t SensiScan::decodeData(const MetaData& metaData, const std::string& data,
std::vector<Measurement>& samples) {
uint8_t sampleType = static_cast<uint8_t>(data[3]);
auto sampleType = static_cast<uint8_t>(data[3]);

DataType dataType = getDataTypeFromSampleType(sampleType);
SampleConfig sampleConfig = sampleConfigSelector[dataType];
Expand Down
23 changes: 11 additions & 12 deletions src/Sensirion_upt_ble_auto_detection.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _SENSIRION_UPT_BLE_AUTO_DETECTION_H_
#define _SENSIRION_UPT_BLE_AUTO_DETECTION_H_
#ifndef SENSIRION_UPT_BLE_AUTO_DETECTION_H
#define SENSIRION_UPT_BLE_AUTO_DETECTION_H

#include "Arduino.h"
#include "BleClient.h"
Expand All @@ -8,24 +8,23 @@
#include <string>
#include <vector>

class SensiScan: public BleClientCallback {
class __attribute__((unused)) SensiScan: public BleClientCallback {
public:
explicit SensiScan() : _bleClient(nullptr){};
explicit SensiScan() : _bleClient(nullptr) {};

void begin();
void
__attribute__((unused)) void
getScanResults(std::map<uint16_t, std::vector<Measurement>>& scanResults);
void keepAlive();
__attribute__((unused)) void keepAlive();

private:
BleClient* _bleClient;
std::map<uint16_t, std::vector<Measurement>> _sampleCache;
void onAdvertisementReceived(std::string address, std::string name,
void onAdvertisementReceived(uint64_t address, std::string name,
std::string data) override;
uint64_t squashMACAddress(const std::string& macAddressString);
uint16_t getDeviceId(const std::string& data);
uint8_t decodeData(const MetaData& metaData, const std::string& data,
std::vector<Measurement>& samples);
static uint16_t getDeviceId(const std::string& data);
static uint8_t decodeData(const MetaData& metaData, const std::string& data,
std::vector<Measurement>& samples);
};

#endif /* _SENSIRION_UPT_BLE_AUTO_DETECTION_H_ */
#endif /* SENSIRION_UPT_BLE_AUTO_DETECTION_H */