44
55#include " sp140/ble.h"
66#include " sp140/ble/ble_ids.h"
7+ #include " sp140/ble/ble_utils.h"
78
89namespace {
910
@@ -22,6 +23,11 @@ BLECharacteristic* pBMSCellVoltages = nullptr;
2223BLECharacteristic* pBMSChargeMos = nullptr ;
2324BLECharacteristic* 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
2733void 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}
0 commit comments