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

Add ble update and service server #409

Draft
wants to merge 6 commits into
base: feature/ble/basic-server
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
78 changes: 78 additions & 0 deletions include/services/OswServiceTaskBLEServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,75 @@ class OswServiceTaskBLEServer : public OswServiceTask {

OswServiceTaskBLEServer* task;
};
class StepsTodayCharacteristicCallbacks: public NimBLECharacteristicCallbacks {
void onRead(NimBLECharacteristic* pCharacteristic);
/*
uint32_t value;
this->bytes[0] = (uint8_t) value;
this->bytes[1] = (uint8_t) (value >> 8);
this->bytes[2] = (uint8_t) (value >> 16);
this->bytes[3] = (uint8_t) (value >> 24);
*/
uint8_t bytes[4];
};
class StepsTotalWeekCharacteristicCallbacks: public NimBLECharacteristicCallbacks {
void onRead(NimBLECharacteristic* pCharacteristic);
/*
uint32_t value;
this->bytes[0] = (uint8_t) value;
this->bytes[1] = (uint8_t) (value >> 8);
this->bytes[2] = (uint8_t) (value >> 16);
this->bytes[3] = (uint8_t) (value >> 24);
*/
uint8_t bytes[4];
};
class StepsTotalCharacteristicCallbacks: public NimBLECharacteristicCallbacks {
void onRead(NimBLECharacteristic* pCharacteristic);
/*
uint32_t value;
this->bytes[0] = (uint8_t) value;
this->bytes[1] = (uint8_t) (value >> 8);
this->bytes[2] = (uint8_t) (value >> 16);
this->bytes[3] = (uint8_t) (value >> 24);
*/
uint8_t bytes[4];
};
class StepsAverageCharacteristicCallbacks: public NimBLECharacteristicCallbacks {
void onRead(NimBLECharacteristic* pCharacteristic);
/*
uint32_t value;
this->bytes[0] = (uint8_t) value;
this->bytes[1] = (uint8_t) (value >> 8);
this->bytes[2] = (uint8_t) (value >> 16);
this->bytes[3] = (uint8_t) (value >> 24);
*/
uint8_t bytes[4];
};
class StepsDayHistoryCharacteristicCallbacks: public NimBLECharacteristicCallbacks {
void onRead(NimBLECharacteristic* pCharacteristic);
/*
uint32_t value[7];
this->bytes[0] = (uint8_t) value[0];
this->bytes[1] = (uint8_t) (value[0] >> 8);
this->bytes[2] = (uint8_t) (value[0] >> 16);
this->bytes[3] = (uint8_t) (value[0] >> 24);

this->bytes[4] = (uint8_t) value[1];
this->bytes[5] = (uint8_t) (value[1] >> 8);
this->bytes[6] = (uint8_t) (value[1] >> 16);
this->bytes[7] = (uint8_t) (value[1] >> 24);

this->bytes[8] = (uint8_t) value[2];
this->bytes[9] = (uint8_t) (value[2] >> 8);
this->bytes[10] = (uint8_t) (value[2] >> 16);
this->bytes[11] = (uint8_t) (value[2] >> 24);

...

*/
uint8_t bytes[4 * 7];
RuffaloLavoisier marked this conversation as resolved.
Show resolved Hide resolved
};


/// apply the desired BLE state
void updateBLEConfig();
Expand All @@ -84,6 +153,15 @@ class OswServiceTaskBLEServer : public OswServiceTask {
NimBLECharacteristic* characteristicSoftRev = nullptr;
NimBLEService* serviceOsw = nullptr;
NimBLECharacteristic* characteristicToast = nullptr;
#if OSW_PLATFORM_ENVIRONMENT_ACCELEROMETER == 1
NimBLECharacteristic* characteristicStepCountTotal = nullptr;
NimBLECharacteristic* characteristicStepCountTotalWeek = nullptr;
NimBLECharacteristic* characteristicStepCountToday = nullptr;
#ifdef OSW_FEATURE_STATS_STEPS
NimBLECharacteristic* characteristicStepCountAverage = nullptr;
NimBLECharacteristic* characteristicStepCountHistory = nullptr;
#endif
#endif

// ↓ our own stuff
NotifierClient notify = NotifierClient("osw.ble.server");
Expand Down
129 changes: 118 additions & 11 deletions src/services/OswServiceTaskBLEServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@
#include "./services/OswServiceTaskBLEServer.h"
#include "osw_hal.h"

#define BATTERY_SERVICE_UUID "0000180F-0000-1000-8000-00805f9b34fb"
#define BATTERY_LEVEL_CHARACTERISTIC_UUID "00002A19-0000-1000-8000-00805f9b34fb"
#define BATTERY_LEVEL_STATUS_CHARACTERISTIC_UUID "00002bed-0000-1000-8000-00805f9b34fb"
#define TIME_SERVICE_UUID "00001805-0000-1000-8000-00805f9b34fb"
#define CURRENT_TIME_CHARACTERISTIC_UUID "00002a0c-0000-1000-8000-00805f9b34fb"
#define DEVICE_INFORMATION_SERVICE_UUID "0000180a-0000-1000-8000-00805f9b34fb"
#define FIRMWARE_REVISION_CHARACTERISTIC_UUID "00002a26-0000-1000-8000-00805f9b34fb"
#define HARDWARE_REVISION_CHARACTERISTIC_UUID "00002a27-0000-1000-8000-00805f9b34fb"
#define SOFTWARE_REVISION_CHARACTERISTIC_UUID "00002a28-0000-1000-8000-00805f9b34fb"
#define OSW_SERVICE_UUID "6ab9b834-3e0b-4770-a938-ebaa58d6b852"
#define TOAST_CHARACTERISTIC_UUID "9cadf570-4ec8-40b0-a8d3-cfdb4a90940b"
#define BATTERY_SERVICE_UUID "0000180F-0000-1000-8000-00805f9b34fb"
#define BATTERY_LEVEL_CHARACTERISTIC_UUID "00002A19-0000-1000-8000-00805f9b34fb"
#define BATTERY_LEVEL_STATUS_CHARACTERISTIC_UUID "00002bed-0000-1000-8000-00805f9b34fb"
#define TIME_SERVICE_UUID "00001805-0000-1000-8000-00805f9b34fb"
#define CURRENT_TIME_CHARACTERISTIC_UUID "00002a0c-0000-1000-8000-00805f9b34fb"
#define DEVICE_INFORMATION_SERVICE_UUID "0000180a-0000-1000-8000-00805f9b34fb"
#define FIRMWARE_REVISION_CHARACTERISTIC_UUID "00002a26-0000-1000-8000-00805f9b34fb"
#define HARDWARE_REVISION_CHARACTERISTIC_UUID "00002a27-0000-1000-8000-00805f9b34fb"
#define SOFTWARE_REVISION_CHARACTERISTIC_UUID "00002a28-0000-1000-8000-00805f9b34fb"

#define OSW_SERVICE_UUID "6ab9b834-3e0b-4770-a938-ebaa58d6b852"
#define TOAST_CHARACTERISTIC_UUID "9cadf570-4ec8-40b0-a8d3-cfdb4a90940b"
#define STEP_COUNT_AVERAGE_CHARACTERISTIC_UUID "3d27dd91-b714-4fe4-8067-620ff7cf13c0"
#define STEP_COUNT_TOTAL_CHARACTERISTIC_UUID "6567e61b-41df-4416-96fc-4e068628bd37"
#define STEP_COUNT_TOTAL_WEEK_CHARACTERISTIC_UUID "0b97315f-883b-4e1e-a745-bc2dd14aedb4"
#define STEP_COUNT_TODAY_CHARACTERISTIC_UUID "143a6279-67ce-43c2-8db2-082a9fbca140"
#define STEP_COUNT_DAY_HISTORY_CHARACTERISTIC_UUID "6b078d24-79ae-4fff-bf3a-2b71dce2b2bb"

void OswServiceTaskBLEServer::setup() {
OswServiceTask::setup();
Expand Down Expand Up @@ -143,6 +149,38 @@ void OswServiceTaskBLEServer::updateBLEConfig() {
);
this->characteristicToast->setCallbacks(new ToastCharacteristicCallbacks(this));

#if OSW_PLATFORM_ENVIRONMENT_ACCELEROMETER == 1
this->characteristicStepCountToday = this->serviceOsw->createCharacteristic(
STEP_COUNT_TODAY_CHARACTERISTIC_UUID,
NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::READ_ENC | NIMBLE_PROPERTY::READ_AUTHEN
);
this->characteristicStepCountToday->setCallbacks(new StepsTodayCharacteristicCallbacks());

this->characteristicStepCountTotal = this->serviceOsw->createCharacteristic(
STEP_COUNT_TOTAL_CHARACTERISTIC_UUID,
NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::READ_ENC | NIMBLE_PROPERTY::READ_AUTHEN
);
this->characteristicStepCountTotal->setCallbacks(new StepsTotalCharacteristicCallbacks());

this->characteristicStepCountTotalWeek = this->serviceOsw->createCharacteristic(
STEP_COUNT_TOTAL_WEEK_CHARACTERISTIC_UUID,
NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::READ_ENC | NIMBLE_PROPERTY::READ_AUTHEN
);
this->characteristicStepCountTotalWeek->setCallbacks(new StepsTotalWeekCharacteristicCallbacks());
#ifdef OSW_FEATURE_STATS_STEPS
this->characteristicStepCountAverage = this->serviceOsw->createCharacteristic(
STEP_COUNT_AVERAGE_CHARACTERISTIC_UUID,
NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::READ_ENC | NIMBLE_PROPERTY::READ_AUTHEN
);
this->characteristicStepCountAverage->setCallbacks(new StepsAverageCharacteristicCallbacks());

this->characteristicStepCountHistory = this->serviceOsw->createCharacteristic(
STEP_COUNT_DAY_HISTORY_CHARACTERISTIC_UUID,
NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::READ_ENC | NIMBLE_PROPERTY::READ_AUTHEN
);
this->characteristicStepCountHistory->setCallbacks(new StepsDayHistoryCharacteristicCallbacks()); // Create a BLE Characteristic: "Hardware Revision String"
#endif
#endif
// Start the service
this->serviceOsw->start();
}
Expand Down Expand Up @@ -172,6 +210,15 @@ void OswServiceTaskBLEServer::updateBLEConfig() {
this->serviceDevice->removeCharacteristic(this->characteristicSoftRev, true);
this->server->removeService(this->serviceDevice, true);
this->serviceOsw->removeCharacteristic(this->characteristicToast, true);
#if OSW_PLATFORM_ENVIRONMENT_ACCELEROMETER == 1
this->serviceOsw->removeCharacteristic(this->characteristicStepCountToday, true);
this->serviceOsw->removeCharacteristic(this->characteristicStepCountTotal, true);
this->serviceOsw->removeCharacteristic(this->characteristicStepCountTotalWeek, true);
#ifdef OSW_FEATURE_STATS_STEPS
this->serviceOsw->removeCharacteristic(this->characteristicStepCountAverage, true);
this->serviceOsw->removeCharacteristic(this->characteristicStepCountHistory, true);
#endif
#endif
this->server->removeService(this->serviceOsw, true);
this->server = nullptr;
NimBLEDevice::deinit(true);
Expand Down Expand Up @@ -286,7 +333,67 @@ void OswServiceTaskBLEServer::HardwareRevisionCharacteristicCallbacks::onRead(Ni
this->value = String(PIO_ENV_NAME);
pCharacteristic->setValue((uint8_t*) this->value.c_str(), this->value.length());
}
#if OSW_PLATFORM_ENVIRONMENT_ACCELEROMETER == 1
void OswServiceTaskBLEServer::StepsTodayCharacteristicCallbacks::onRead(NimBLECharacteristic* pCharacteristic) {
uint32_t stepsToday = OswHal::getInstance()->environment()->getStepsToday();

this->bytes[0] = (uint8_t) stepsToday;
this->bytes[1] = (uint8_t) (stepsToday >> 8);
this->bytes[2] = (uint8_t) (stepsToday >> 16);
this->bytes[3] = (uint8_t) (stepsToday >> 24);

pCharacteristic->setValue(this->bytes, sizeof(this->bytes));
}

void OswServiceTaskBLEServer::StepsTotalCharacteristicCallbacks::onRead(NimBLECharacteristic* pCharacteristic) {
uint32_t stepsTotal = OswHal::getInstance()->environment()->getStepsTotal();

this->bytes[0] = (uint8_t) stepsTotal;
this->bytes[1] = (uint8_t) (stepsTotal >> 8);
this->bytes[2] = (uint8_t) (stepsTotal >> 16);
this->bytes[3] = (uint8_t) (stepsTotal >> 24);

pCharacteristic->setValue(this->bytes, sizeof(this->bytes));
}

void OswServiceTaskBLEServer::StepsTotalWeekCharacteristicCallbacks::onRead(NimBLECharacteristic* pCharacteristic) {
uint32_t stepsTotalWeek = OswHal::getInstance()->environment()->getStepsTotalWeek();

this->bytes[0] = (uint8_t) stepsTotalWeek;
this->bytes[1] = (uint8_t) (stepsTotalWeek >> 8);
this->bytes[2] = (uint8_t) (stepsTotalWeek >> 16);
this->bytes[3] = (uint8_t) (stepsTotalWeek >> 24);

pCharacteristic->setValue(this->bytes, sizeof(this->bytes));
}
#ifdef OSW_FEATURE_STATS_STEPS
void OswServiceTaskBLEServer::StepsAverageCharacteristicCallbacks::onRead(NimBLECharacteristic* pCharacteristic) {
uint32_t stepsAverage = OswHal::getInstance()->environment()->getStepsAverage();

this->bytes[0] = (uint8_t) stepsAverage;
this->bytes[1] = (uint8_t) (stepsAverage >> 8);
this->bytes[2] = (uint8_t) (stepsAverage >> 16);
this->bytes[3] = (uint8_t) (stepsAverage >> 24);

pCharacteristic->setValue(this->bytes, sizeof(this->bytes));
}

void OswServiceTaskBLEServer::StepsDayHistoryCharacteristicCallbacks::onRead(NimBLECharacteristic* pCharacteristic) {

for (uint8_t indexOfWeek = 0; indexOfWeek < 7; indexOfWeek++)
{
uint32_t value = OswHal::getInstance()->environment()->getStepsOnDay(indexOfWeek, true);
this->bytes[0 + indexOfWeek * 4] = (uint8_t) value;
this->bytes[1 + indexOfWeek * 4] = (uint8_t) (value >> 8);
this->bytes[2 + indexOfWeek * 4] = (uint8_t) (value >> 16);
this->bytes[3 + indexOfWeek * 4] = (uint8_t) (value >> 24);
/* code */
RuffaloLavoisier marked this conversation as resolved.
Show resolved Hide resolved
}

pCharacteristic->setValue(this->bytes, sizeof(this->bytes));
}
#endif
#endif
void OswServiceTaskBLEServer::SoftwareRevisionCharacteristicCallbacks::onRead(NimBLECharacteristic* pCharacteristic) {
this->value = String(GIT_COMMIT_HASH) + " (" + String(GIT_BRANCH_NAME) + ")";
pCharacteristic->setValue((uint8_t*) this->value.c_str(), this->value.length());
Expand Down
Loading