Files
MSE-PI-E2EEDA-Plein-de-eeee…/nodes/src/ble_advertiser.c
adrien balleyguier 8a559024a6 feat(nodes): add the battery level in the ble frame
wip: The battery_setup fails for some unknown reasons
2026-06-04 21:01:43 +02:00

80 lines
3.0 KiB
C

#include "ble_advertiser.h"
static const int BT_MS_ADV_UP = 500; // [ms] time during which the advertising is active
// keys as defined in the specs
static const char BT_KEY_SIZE = 1; // [B]
static const char BT_KEY_WINDOW = 0x01;
static const char BT_KEY_HUMIDITY = 0x02;
static const char BT_KEY_TEMP = 0x03;
static const char BT_KEY_CO2_LVL = 0x04;
static const char BT_KEY_BATT = 0x05;
// value size [B]
static const char BT_PREAMBLE_SIZE = 2;
static const char BT_VALUE_SIZE_WINDOW = 1;
static const char BT_VALUE_SIZE_HUMIDITY = 1;
static const char BT_VALUE_SIZE_TEMP = 2;
static const char BT_VALUE_SIZE_CO2_LVL = 4;
static const char BT_VALUE_SIZE_BATT = 1;
// value start index in the frame - equal to {prior field index + prior value size + key size}
static const int BT_AD_DATA_INDEX_WINDOW = BT_PREAMBLE_SIZE + BT_KEY_SIZE;
static const int BT_AD_DATA_INDEX_HUMIDITY = BT_AD_DATA_INDEX_WINDOW + BT_VALUE_SIZE_WINDOW + BT_KEY_SIZE;
static const int BT_AD_DATA_INDEX_TEMP = BT_AD_DATA_INDEX_HUMIDITY + BT_VALUE_SIZE_HUMIDITY + BT_KEY_SIZE;
static const int BT_AD_DATA_INDEX_CO2_LVL = BT_AD_DATA_INDEX_TEMP + BT_VALUE_SIZE_TEMP + BT_KEY_SIZE;
static const int BT_AD_DATA_INDEX_BATT = BT_AD_DATA_INDEX_CO2_LVL + BT_VALUE_SIZE_CO2_LVL + BT_KEY_SIZE;
// sum of all value size + size for all keys + size preamble
static const char BT_AD_TOTAL_SIZE =
BT_PREAMBLE_SIZE + (5 * BT_KEY_SIZE) +
BT_VALUE_SIZE_WINDOW + BT_VALUE_SIZE_HUMIDITY + BT_VALUE_SIZE_TEMP + BT_VALUE_SIZE_CO2_LVL + BT_VALUE_SIZE_BATT;
// data field in the broadcasted frame
// all values are set to 0 and the "company id" field is set to 0xffff
static uint8_t ad_data[] = {
0xff, 0xff,
BT_KEY_WINDOW, 0x00,
BT_KEY_HUMIDITY, 0x00,
BT_KEY_TEMP, 0x00, 0x00,
BT_KEY_CO2_LVL, 0x00, 0x00, 0x00, 0x00,
BT_KEY_BATT, 0x00
};
// frame that will be broadcasted
static const struct bt_data ad[] = {
BT_DATA(BT_DATA_MANUFACTURER_DATA, ad_data, BT_AD_TOTAL_SIZE),
};
enum error_code ble_init(){
return ((0 == bt_enable(NULL)) ? success : init_failed);
}
enum error_code ble_advertise(
enum window_status window_value,
int hygro_value,
int thermo_value,
int co2_lvl_value,
int batt_value
){
enum error_code ret = write_failed;
int shift = 0;
ad_data[BT_AD_DATA_INDEX_WINDOW] = (uint8_t)(window_value == open ? 0x1 : 0x0);
ad_data[BT_AD_DATA_INDEX_HUMIDITY] = (uint8_t)(hygro_value & 0xff);
ad_data[BT_AD_DATA_INDEX_BATT] = (uint8_t)(batt_value & 0xff);
for(int i=0;i<BT_VALUE_SIZE_TEMP;i++){
shift = 8 * (BT_VALUE_SIZE_TEMP - i - 1);
ad_data[BT_AD_DATA_INDEX_TEMP + i] = (uint8_t)((thermo_value>>(shift)) & 0xff);
}
for(int i=0;i<BT_VALUE_SIZE_CO2_LVL;i++){
shift = 8 * (BT_VALUE_SIZE_CO2_LVL - i - 1);
ad_data[BT_AD_DATA_INDEX_CO2_LVL + i] = (uint8_t)((co2_lvl_value>>(shift)) & 0xff);
}
if(0 == bt_le_adv_start(BT_LE_ADV_NCONN_IDENTITY, ad, ARRAY_SIZE(ad), NULL, 0)){
k_msleep(BT_MS_ADV_UP);
if(0 == bt_le_adv_stop()){
ret = success;
}else{}
}else{}
return ret;
}