Skip to content

Commit

Permalink
Added "Battery Level Status"
Browse files Browse the repository at this point in the history
Signed-off-by: simonmicro <simon@simonmicro.de>
  • Loading branch information
simonmicro committed May 19, 2024
1 parent 9ea4233 commit 4c75d58
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
10 changes: 8 additions & 2 deletions include/services/OswServiceTaskBLEServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ class OswServiceTaskBLEServer : public OswServiceTask, NimBLEServerCallbacks {

void updateName();
private:
class BatteryCharacteristicCallbacks: public NimBLECharacteristicCallbacks {
class BatteryLevelCharacteristicCallbacks: public NimBLECharacteristicCallbacks {
void onRead(NimBLECharacteristic* pCharacteristic);
uint8_t byte = 0; // will be read from
};
class BatteryLevelStatusCharacteristicCallbacks: public NimBLECharacteristicCallbacks {
void onRead(NimBLECharacteristic* pCharacteristic);
uint8_t bytes[1+2+1]; // will be read from (1 flag, 2 power-state, 1 battery-level)
};

/// apply the desired BLE state
void updateBLEConfig();
Expand All @@ -37,11 +41,13 @@ class OswServiceTaskBLEServer : public OswServiceTask, NimBLEServerCallbacks {
NimBLEServer* server = nullptr; // if set, this is considered as "enabled"
BLEService* serviceBat = nullptr;
NimBLECharacteristic* characteristicBat = nullptr;
NimBLECharacteristic* characteristicBatStat = nullptr;

// ↓ our own stuff
bool bootDone = false;
bool enabled = false;
char name[8]; // BLE advertising only support up to 8 bytes
BatteryCharacteristicCallbacks battery;
BatteryLevelCharacteristicCallbacks battery;
BatteryLevelStatusCharacteristicCallbacks batteryStatus;
};
#endif
57 changes: 52 additions & 5 deletions src/services/OswServiceTaskBLEServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#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"

void OswServiceTaskBLEServer::setup() {
OswServiceTask::setup();
Expand Down Expand Up @@ -58,13 +59,20 @@ void OswServiceTaskBLEServer::updateBLEConfig() {
// Create the BLE Service
serviceBat = this->server->createService(BATTERY_SERVICE_UUID);

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

// Create a BLE Characteristic: "Battery Level Status"
this->characteristicBatStat = serviceBat->createCharacteristic(
BATTERY_LEVEL_STATUS_CHARACTERISTIC_UUID,
NIMBLE_PROPERTY::READ
);
this->characteristicBatStat->setCallbacks(&batteryStatus);

// Start the service
this->serviceBat->start();
}
Expand All @@ -82,6 +90,7 @@ void OswServiceTaskBLEServer::updateBLEConfig() {
OSW_LOG_D("Off");
BLEDevice::stopAdvertising();
this->serviceBat->removeCharacteristic(this->characteristicBat, true);
this->serviceBat->removeCharacteristic(this->characteristicBatStat, true);
this->server->removeService(this->serviceBat, true);
this->server = nullptr;
}
Expand Down Expand Up @@ -113,14 +122,52 @@ bool OswServiceTaskBLEServer::onConfirmPIN(uint32_t pass_key) {
return true;
}


void OswServiceTaskBLEServer::BatteryCharacteristicCallbacks::onRead(NimBLECharacteristic* pCharacteristic) {
void OswServiceTaskBLEServer::BatteryLevelCharacteristicCallbacks::onRead(NimBLECharacteristic* pCharacteristic) {
// get the current battery level into the inner byte buffer
if(OswHal::getInstance()->isCharging()) {
byte = 255; // invalid value
byte = 0xFF; // invalid value
} else {
this->byte = OswHal::getInstance()->getBatteryPercent();
}
pCharacteristic->setValue(&this->byte, 1);
pCharacteristic->setValue(&this->byte, sizeof(this->byte));
}

void OswServiceTaskBLEServer::BatteryLevelStatusCharacteristicCallbacks::onRead(NimBLECharacteristic* pCharacteristic) {
// see https://www.bluetooth.com/specifications/specs/battery-service/
// see https://www.bluetooth.com/specifications/specs/gatt-specification-supplement-8-2/
bool isCharging = OswHal::getInstance()->isCharging();
// flags
if(isCharging) {
this->bytes[0] = 0b00000000; // No additional information
} else {
this->bytes[0] = 0b00000010; // Battery Level Present
}
// power-state
this->bytes[1] = 0b00000001; // Battery Present: Yes
this->bytes[2] = 0b00000000;
if(isCharging) {
this->bytes[1] |= 0b00000010; // Wired External Power Source Connected: Yes
this->bytes[1] |= 0b00100000; // Battery Charge State: Charging
} else {
this->bytes[1] |= 0b01000000; // Battery Charge State: Discharging: Active
if(OswHal::getInstance()->getBatteryPercent() > 50) {
this->bytes[1] |= 0b10000000; // Battery Charge Level: Good
} else if(OswHal::getInstance()->getBatteryPercent() > 25) {
// Battery Charge Level: Low
this->bytes[1] |= 0b00000000;
this->bytes[2] |= 0b00000001;
} else {
// Battery Charge Level: Critical
this->bytes[1] |= 0b10000000;
this->bytes[2] |= 0b00000001;
}
}
// battery-level
if(isCharging) {
this->bytes[3] = 0xFF; // invalid value (should not be sent if charging)
} else {
this->bytes[3] = OswHal::getInstance()->getBatteryPercent();
}
pCharacteristic->setValue(this->bytes, isCharging ? (sizeof(this->bytes) - 1) : sizeof(this->bytes));
}
#endif

0 comments on commit 4c75d58

Please sign in to comment.