Skip to content

Commit 911f049

Browse files
committed
Add BLE utils for conditional notifications
Introduced ble_utils.h/cpp with setAndNotifyOnChange to update BLE characteristics and notify only on value changes. Updated bms_service.cpp to use this utility for failure level and MOS state characteristics, reducing unnecessary BLE notifications. Also added notification properties and descriptors to relevant characteristics.
1 parent 29dca5c commit 911f049

File tree

4 files changed

+87
-20
lines changed

4 files changed

+87
-20
lines changed

inc/sp140/ble/ble_utils.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#ifndef INC_SP140_BLE_BLE_UTILS_H_
2+
#define INC_SP140_BLE_BLE_UTILS_H_
3+
4+
#include <BLECharacteristic.h>
5+
6+
// Forward declare deviceConnected from ble.h
7+
extern bool deviceConnected;
8+
9+
/**
10+
* Sets a BLE characteristic value and conditionally notifies only when the value changes.
11+
* Returns true if the value changed and a notification was sent, false otherwise.
12+
*
13+
* @param characteristic The BLE characteristic to update
14+
* @param newValue The new value to set
15+
* @param lastValue Reference to the variable tracking the previous value
16+
* @return true if value changed and notification sent, false if no change
17+
*/
18+
template<typename T>
19+
bool setAndNotifyOnChange(BLECharacteristic* characteristic, T newValue, T& lastValue) {
20+
if (characteristic == nullptr) {
21+
return false;
22+
}
23+
24+
characteristic->setValue(newValue);
25+
26+
if (newValue != lastValue) {
27+
lastValue = newValue;
28+
if (deviceConnected) {
29+
characteristic->notify();
30+
}
31+
return true; // Value changed
32+
}
33+
return false; // No change
34+
}
35+
36+
/**
37+
* Specialization for uint8_t that uses pointer-based setValue (for binary data).
38+
* Returns true if the value changed and a notification was sent, false otherwise.
39+
*
40+
* @param characteristic The BLE characteristic to update
41+
* @param newValue The new uint8_t value to set
42+
* @param lastValue Reference to the variable tracking the previous value
43+
* @return true if value changed and notification sent, false if no change
44+
*/
45+
bool setAndNotifyOnChange(BLECharacteristic* characteristic, uint8_t newValue, uint8_t& lastValue);
46+
47+
#endif // INC_SP140_BLE_BLE_UTILS_H_

src/sp140/ble/ble_utils.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "sp140/ble/ble_utils.h"
2+
3+
/**
4+
* Specialization for uint8_t that uses pointer-based setValue (for binary data).
5+
*/
6+
bool setAndNotifyOnChange(BLECharacteristic* characteristic, uint8_t newValue, uint8_t& lastValue) {
7+
if (characteristic == nullptr) {
8+
return false;
9+
}
10+
11+
characteristic->setValue(&newValue, sizeof(newValue));
12+
13+
if (newValue != lastValue) {
14+
lastValue = newValue;
15+
if (deviceConnected) {
16+
characteristic->notify();
17+
}
18+
return true; // Value changed
19+
}
20+
return false; // No change
21+
}

src/sp140/ble/bms_service.cpp

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "sp140/ble.h"
66
#include "sp140/ble/ble_ids.h"
7+
#include "sp140/ble/ble_utils.h"
78

89
namespace {
910

@@ -22,6 +23,11 @@ BLECharacteristic* pBMSCellVoltages = nullptr;
2223
BLECharacteristic* pBMSChargeMos = nullptr;
2324
BLECharacteristic* pBMSDischargeMos = nullptr;
2425

26+
// Track previous values to only notify on change
27+
uint8_t lastFailureLevel = 0;
28+
uint8_t lastChargeMos = 0;
29+
uint8_t lastDischargeMos = 0;
30+
2531
} // namespace
2632

2733
void initBmsBleService(BLEServer* server) {
@@ -57,7 +63,8 @@ void initBmsBleService(BLEServer* server) {
5763
BLEUUID(BMS_LOW_TEMP_UUID), BLECharacteristic::PROPERTY_READ);
5864

5965
pBMSFailureLevel = pBmsService->createCharacteristic(
60-
BLEUUID(BMS_FAILURE_LEVEL_UUID), BLECharacteristic::PROPERTY_READ);
66+
BLEUUID(BMS_FAILURE_LEVEL_UUID), BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
67+
pBMSFailureLevel->addDescriptor(new BLE2902());
6168

6269
pBMSVoltageDiff = pBmsService->createCharacteristic(
6370
BLEUUID(BMS_VOLTAGE_DIFF_UUID), BLECharacteristic::PROPERTY_READ);
@@ -66,10 +73,12 @@ void initBmsBleService(BLEServer* server) {
6673
BLEUUID(BMS_CELL_VOLTAGES_UUID), BLECharacteristic::PROPERTY_READ);
6774

6875
pBMSChargeMos = pBmsService->createCharacteristic(
69-
BLEUUID(BMS_CHARGE_MOS_UUID), BLECharacteristic::PROPERTY_READ);
76+
BLEUUID(BMS_CHARGE_MOS_UUID), BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
77+
pBMSChargeMos->addDescriptor(new BLE2902());
7078

7179
pBMSDischargeMos = pBmsService->createCharacteristic(
72-
BLEUUID(BMS_DISCHARGE_MOS_UUID), BLECharacteristic::PROPERTY_READ);
80+
BLEUUID(BMS_DISCHARGE_MOS_UUID), BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
81+
pBMSDischargeMos->addDescriptor(new BLE2902());
7382

7483
// Ensure characteristics have deterministic startup values.
7584
uint16_t initial_cell_values[BMS_CELLS_NUM] = {0};
@@ -107,22 +116,8 @@ void updateBMSTelemetry(const STR_BMS_TELEMETRY_140& telemetry) {
107116
if (pBMSLowCell) pBMSLowCell->setValue(lowCell);
108117
if (pBMSHighTemp) pBMSHighTemp->setValue(highTemp);
109118
if (pBMSLowTemp) pBMSLowTemp->setValue(lowTemp);
110-
if (pBMSFailureLevel) {
111-
uint8_t failureLevel = telemetry.battery_failure_level;
112-
pBMSFailureLevel->setValue(&failureLevel, sizeof(failureLevel));
113-
}
114119
if (pBMSVoltageDiff) pBMSVoltageDiff->setValue(voltageDiff);
115120

116-
if (pBMSChargeMos) {
117-
uint8_t chargeMos = telemetry.is_charge_mos ? 1 : 0;
118-
pBMSChargeMos->setValue(&chargeMos, sizeof(chargeMos));
119-
}
120-
121-
if (pBMSDischargeMos) {
122-
uint8_t dischargeMos = telemetry.is_discharge_mos ? 1 : 0;
123-
pBMSDischargeMos->setValue(&dischargeMos, sizeof(dischargeMos));
124-
}
125-
126121
if (pBMSCellVoltages) {
127122
uint16_t cell_millivolts[BMS_CELLS_NUM];
128123
for (uint8_t i = 0; i < BMS_CELLS_NUM; i++) {
@@ -133,6 +128,11 @@ void updateBMSTelemetry(const STR_BMS_TELEMETRY_140& telemetry) {
133128
BMS_CELLS_NUM * sizeof(uint16_t));
134129
}
135130

131+
// Handle state-change characteristics - only notify on change
132+
setAndNotifyOnChange(pBMSFailureLevel, telemetry.battery_failure_level, lastFailureLevel);
133+
setAndNotifyOnChange(pBMSChargeMos, static_cast<uint8_t>(telemetry.is_charge_mos ? 1 : 0), lastChargeMos);
134+
setAndNotifyOnChange(pBMSDischargeMos, static_cast<uint8_t>(telemetry.is_discharge_mos ? 1 : 0), lastDischargeMos);
135+
136136
if (!deviceConnected) {
137137
return; // No notifications needed without a subscriber.
138138
}
@@ -145,8 +145,6 @@ void updateBMSTelemetry(const STR_BMS_TELEMETRY_140& telemetry) {
145145
if (pBMSLowCell) pBMSLowCell->notify();
146146
if (pBMSHighTemp) pBMSHighTemp->notify();
147147
if (pBMSLowTemp) pBMSLowTemp->notify();
148-
if (pBMSFailureLevel) pBMSFailureLevel->notify();
149148
if (pBMSVoltageDiff) pBMSVoltageDiff->notify();
150-
if (pBMSChargeMos) pBMSChargeMos->notify();
151-
if (pBMSDischargeMos) pBMSDischargeMos->notify();
149+
// Note: pBMSFailureLevel, pBMSChargeMos, pBMSDischargeMos notify separately above, only on change
152150
}

src/sp140/ble/config_service.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "sp140/ble.h"
1414
#include "sp140/ble/ble_ids.h"
15+
#include "sp140/ble/ble_utils.h"
1516
#include "sp140/device_state.h"
1617
#include "sp140/globals.h"
1718
#include "sp140/throttle.h"

0 commit comments

Comments
 (0)