Skip to content

Commit

Permalink
Added BLE server task
Browse files Browse the repository at this point in the history
Signed-off-by: simonmicro <[email protected]>
  • Loading branch information
simonmicro committed May 19, 2024
1 parent 06b3882 commit 87c4ec6
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 3 deletions.
6 changes: 3 additions & 3 deletions include/services/OswServiceTaskBLECompanion.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef OSW_SERVICE_COMPANION_H
#define OSW_SERVICE_COMPANION_H
#pragma once

#if SERVICE_BLE_COMPANION == 1
#ifndef OSW_EMULATOR
#include <BLECharacteristic.h>
#include <BLEDevice.h>
Expand Down Expand Up @@ -43,4 +43,4 @@ class OswServiceTaskBLECompanion : public OswServiceTask {
};

#endif
#endif
#endif
38 changes: 38 additions & 0 deletions include/services/OswServiceTaskBLEServer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once
#ifdef OSW_FEATURE_BLE_SERVER
#include "osw_service.h"

#define CONFIG_BT_NIMBLE_ROLE_PERIPHERAL
#include <NimBLEDevice.h>

class OswServiceTaskBLEServer : public OswServiceTask, NimBLEServerCallbacks {
public:
OswServiceTaskBLEServer() {};
virtual void setup() override;
virtual void loop() override;
virtual void stop() override;
~OswServiceTaskBLEServer() {};

void updateName();
private:
class BatteryCharacteristicCallbacks: public NimBLECharacteristicCallbacks {
void onRead(NimBLECharacteristic* pCharacteristic);
uint8_t byte = 0; // will be read from
};

// ↓ NimBLEServerCallbacks
void onConnect(BLEServer* pServer);
void onDisconnect(BLEServer* pServer);
uint32_t onPassKeyRequest();
bool onConfirmPIN(uint32_t pass_key);

// ↓ managed by NimBLE
NimBLEServer* server = nullptr;
BLEService* serviceBat = nullptr;
NimBLECharacteristic* characteristicBat = nullptr;

// ↓ our own stuff
char name[8]; // BLE advertising only support up to 8 bytes
BatteryCharacteristicCallbacks battery;
};
#endif
4 changes: 4 additions & 0 deletions include/services/OswServiceTasks.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "osw_service.h"

class OswServiceTaskBLECompanion;
class OswServiceTaskBLEServer;
class OswServiceTaskExample;
class OswServiceTaskMemMonitor;
class OswServiceTaskNotifier;
Expand All @@ -20,6 +21,9 @@ extern OswServiceTaskWebserver webserver;
#if OSW_SERVICE_NOTIFIER == 1
extern OswServiceTaskNotifier notifier;
#endif
#ifdef OSW_FEATURE_BLE_SERVER
extern OswServiceTaskBLEServer bleServer;
#endif
extern OswServiceTaskMemMonitor memory;
}

Expand Down
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ lib_deps =
bblanchon/ArduinoJson@^6.21.2
finitespace/BME280@^3.0.0 ; TODO Use the more popular Adafruit BME280 Library instead (also true for others?)?
mprograms/QMC5883LCompass@^1.1.1
h2zero/NimBLE-Arduino@^1.4.1
upload_speed = 460800
monitor_speed = 115200
; Define additional build stage scripts - used to "compile" the html or define additional information
Expand Down
5 changes: 5 additions & 0 deletions src/osw_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <osw_hal.h> // For timezone reloading
#include <osw_ui.h> // For color reloading
#include "apps/watchfaces/OswAppWatchfaceDigital.h"
#include <services/OswServiceTasks.h>
#include <services/OswServiceTaskBLEServer.h>

std::unique_ptr<OswConfig> OswConfig::instance = nullptr;

Expand Down Expand Up @@ -160,4 +162,7 @@ void OswConfig::notifyChange() {
// OswUI::getInstance()->resetTextColors(); // nope - this is done by the ui itself
OswHal::getInstance()->updateTimezoneOffsets();
OswAppWatchfaceDigital::refreshDateFormatCache();
#ifdef OSW_FEATURE_BLE_SERVER
OswServiceAllTasks::bleServer.updateName();
#endif
}
2 changes: 2 additions & 0 deletions src/services/OswServiceTaskBLECompanion.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if SERVICE_BLE_COMPANION == 1
#ifndef OSW_EMULATOR
#include "./services/OswServiceTaskBLECompanion.h"
#include "osw_hal.h"
Expand Down Expand Up @@ -94,4 +95,5 @@ void OswServiceTaskBLECompanion::loop() {
void OswServiceTaskBLECompanion::stop() {
OswServiceTask::stop();
}
#endif
#endif
97 changes: 97 additions & 0 deletions src/services/OswServiceTaskBLEServer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#ifdef OSW_FEATURE_BLE_SERVER
#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"

void OswServiceTaskBLEServer::setup() {
OswServiceTask::setup();
this->updateName();

NimBLEDevice::setSecurityAuth(true, false, false); // support bonding, but no mitm-protection or secure pairing
NimBLEDevice::setSecurityIOCap(BLE_HS_IO_DISPLAY_YESNO); // we can display yes/no with a given key to the user

// Create the BLE Server
server = NimBLEDevice::getServer();
if(server == nullptr) {
OSW_LOG_D("No server found, creating a new one.");
server = NimBLEDevice::createServer();
}
server->setCallbacks(this); // make sure, we are the servers authority

{
// Create the BLE Service
serviceBat = server->createService(BATTERY_SERVICE_UUID);

// Create a BLE Characteristic
characteristicBat = serviceBat->createCharacteristic(
BATTERY_LEVEL_CHARACTERISTIC_UUID,
NIMBLE_PROPERTY::READ
);
characteristicBat->setCallbacks(&battery);

// Start the service
serviceBat->start();
}

// Start advertising
{
BLEAdvertising* pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(BATTERY_SERVICE_UUID);
pAdvertising->setScanResponse(false);
/** Note, this could be left out as that is the default value */
pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter
BLEDevice::startAdvertising();
}
}

void OswServiceTaskBLEServer::loop() {

}

void OswServiceTaskBLEServer::stop() {
OswServiceTask::stop();

BLEDevice::stopAdvertising();
serviceBat->removeCharacteristic(characteristicBat, true);
server->removeService(serviceBat, true);
}

void OswServiceTaskBLEServer::updateName() {
memset(this->name, 0, 8); // clear the name buffer
strncpy(this->name, OswConfigAllKeys::hostname.get().c_str(), 8);
NimBLEDevice::init(this->name);
}

void OswServiceTaskBLEServer::onConnect(BLEServer* pServer) {
OSW_LOG_D("A client has connected!");
}

void OswServiceTaskBLEServer::onDisconnect(BLEServer* pServer) {
OSW_LOG_D("A client has disconnected!");
}

uint32_t OswServiceTaskBLEServer::onPassKeyRequest() {
// TODO connecting client asked for a pin to confirm
OSW_LOG_I("Server PassKeyRequest: ", 123456);
return 123456;
}

bool OswServiceTaskBLEServer::onConfirmPIN(uint32_t pass_key) {
// TODO user must confirm if this is the correct key sent by the other device
OSW_LOG_I("The passkey YES/NO number: ", pass_key);
return true;
}


void OswServiceTaskBLEServer::BatteryCharacteristicCallbacks::onRead(NimBLECharacteristic* pCharacteristic) {
// get the current battery level into the inner byte buffer
if(OswHal::getInstance()->isCharging()) {
byte = 255; // invalid value
} else {
this->byte = OswHal::getInstance()->getBatteryPercent();
}
pCharacteristic->setValue(&this->byte, 1);
}
#endif
7 changes: 7 additions & 0 deletions src/services/OswServiceTasks.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "services/OswServiceTasks.h"

#include "services/OswServiceTaskBLECompanion.h"
#include "services/OswServiceTaskBLEServer.h"
#include "services/OswServiceTaskExample.h"
#include "services/OswServiceTaskGPS.h"
#include "services/OswServiceTaskMemMonitor.h"
Expand All @@ -24,6 +25,9 @@ OswServiceTaskGPS gps;
OswServiceTaskWiFi wifi;
OswServiceTaskWebserver webserver;
#endif
#ifdef OSW_FEATURE_BLE_SERVER
OswServiceTaskBLEServer bleServer;
#endif
#if OSW_SERVICE_NOTIFIER == 1
OswServiceTaskNotifier notifier;
#endif
Expand All @@ -44,6 +48,9 @@ OswServiceTask* oswServiceTasks[] = {
#ifdef OSW_FEATURE_WIFI
& OswServiceAllTasks::wifi, &OswServiceAllTasks::webserver,
#endif
#ifdef OSW_FEATURE_BLE_SERVER
& OswServiceAllTasks::bleServer,
#endif
#if OSW_SERVICE_NOTIFIER == 1
& OswServiceAllTasks::notifier,
#endif
Expand Down

0 comments on commit 87c4ec6

Please sign in to comment.