fix(nodes): adjusting broadcasted data to fit definition

Refs: #3
This commit is contained in:
adrien balleyguier
2026-05-05 12:29:13 +02:00
committed by Klagarge
parent bce2417ded
commit 158767deec

View File

@@ -1,6 +1,6 @@
#include "ble_advertiser.h" #include "ble_advertiser.h"
static const int MS_ADV_UP = 500; // time during which the advertising is active static const int BT_MS_ADV_UP = 500; // [ms] time during which the advertising is active
static const char BT_KEY_WINDOW = 0x01; static const char BT_KEY_WINDOW = 0x01;
static const char BT_KEY_HUMIDITY = 0x02; static const char BT_KEY_HUMIDITY = 0x02;
@@ -8,22 +8,27 @@ static const char BT_KEY_TEMP = 0x03;
static const char BT_KEY_CO2_LVL = 0x04; static const char BT_KEY_CO2_LVL = 0x04;
// value size [B] // value size [B]
static const char BT_PREAMBLE_SIZE = 2;
static const char BT_VALUE_SIZE_WINDOW = 1; static const char BT_VALUE_SIZE_WINDOW = 1;
static const char BT_VALUE_SIZE_HUMIDITY = 1; static const char BT_VALUE_SIZE_HUMIDITY = 1;
static const char BT_VALUE_SIZE_TEMP = 2; static const char BT_VALUE_SIZE_TEMP = 2;
static const char BT_VALUE_SIZE_CO2_LVL = 4; static const char BT_VALUE_SIZE_CO2_LVL = 4;
static const int BT_AD_DATA_INDEX_WINDOW = 1; static const int BT_AD_DATA_INDEX_WINDOW = BT_PREAMBLE_SIZE + 1;
static const int BT_AD_DATA_INDEX_HUMIDITY = 3; static const int BT_AD_DATA_INDEX_HUMIDITY = BT_AD_DATA_INDEX_WINDOW + 2;
static const int BT_AD_DATA_INDEX_TEMP = 5; static const int BT_AD_DATA_INDEX_TEMP = BT_AD_DATA_INDEX_HUMIDITY + 2;
static const int BT_AD_DATA_INDEX_CO2_LVL = 8; static const int BT_AD_DATA_INDEX_CO2_LVL = BT_AD_DATA_INDEX_TEMP + 3;
// sum of all value size + size for all keys // sum of all value size + size for all keys
static const char BT_AD_TOTAL_SIZE = static const char BT_AD_TOTAL_SIZE =
BT_VALUE_SIZE_WINDOW + BT_VALUE_SIZE_HUMIDITY + BT_VALUE_SIZE_TEMP + BT_VALUE_SIZE_CO2_LVL + 4; BT_VALUE_SIZE_WINDOW + BT_VALUE_SIZE_HUMIDITY + BT_VALUE_SIZE_TEMP + BT_VALUE_SIZE_CO2_LVL + 4 + 2;
static uint8_t ad_data[] = { static uint8_t ad_data[] = {
BT_KEY_WINDOW, 0x00, BT_KEY_HUMIDITY, 0x00, BT_KEY_TEMP, 0x00, 0x00, BT_KEY_CO2_LVL, 0x00, 0x00, 0x00, 0x00 0xff, 0xff,
BT_KEY_WINDOW, 0x00,
BT_KEY_HUMIDITY, 0x00,
BT_KEY_TEMP, 0x00, 0x00,
BT_KEY_CO2_LVL, 0x00, 0x00, 0x00, 0x00
}; };
static const struct bt_data ad[] = { static const struct bt_data ad[] = {
BT_DATA(BT_DATA_MANUFACTURER_DATA, ad_data, BT_AD_TOTAL_SIZE), BT_DATA(BT_DATA_MANUFACTURER_DATA, ad_data, BT_AD_TOTAL_SIZE),
@@ -40,13 +45,20 @@ enum error_code ble_advertise(
int co2_lvl_value int co2_lvl_value
){ ){
enum error_code ret = write_failed; enum error_code ret = write_failed;
int shift = 0;
// set values here // set values here
ad_data[BT_AD_DATA_INDEX_WINDOW] = (uint8_t)(window_value == open ? 1 : 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); ad_data[BT_AD_DATA_INDEX_HUMIDITY] = (uint8_t)(hygro_value & 0xff);
ad_data[BT_AD_DATA_INDEX_TEMP] = (uint16_t)(thermo_value); for(int i=0;i<BT_VALUE_SIZE_TEMP;i++){
ad_data[BT_AD_DATA_INDEX_CO2_LVL] = (uint32_t)(co2_lvl_value); 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, ad, ARRAY_SIZE(ad), NULL, 0)){ if(0 == bt_le_adv_start(BT_LE_ADV_NCONN, ad, ARRAY_SIZE(ad), NULL, 0)){
k_msleep(MS_ADV_UP); k_msleep(BT_MS_ADV_UP);
if(0 == bt_le_adv_stop()){ if(0 == bt_le_adv_stop()){
ret = success; ret = success;
}else{} }else{}