From 158767deec90c9b32e1acf9e4942c6d09d996781 Mon Sep 17 00:00:00 2001 From: adrien balleyguier Date: Tue, 5 May 2026 12:29:13 +0200 Subject: [PATCH] fix(nodes): adjusting broadcasted data to fit definition Refs: #3 --- nodes/src/ble_advertiser.c | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/nodes/src/ble_advertiser.c b/nodes/src/ble_advertiser.c index 465ce09..be4a6a5 100644 --- a/nodes/src/ble_advertiser.c +++ b/nodes/src/ble_advertiser.c @@ -1,6 +1,6 @@ #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_HUMIDITY = 0x02; @@ -8,22 +8,27 @@ static const char BT_KEY_TEMP = 0x03; static const char BT_KEY_CO2_LVL = 0x04; // 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 int BT_AD_DATA_INDEX_WINDOW = 1; -static const int BT_AD_DATA_INDEX_HUMIDITY = 3; -static const int BT_AD_DATA_INDEX_TEMP = 5; -static const int BT_AD_DATA_INDEX_CO2_LVL = 8; +static const int BT_AD_DATA_INDEX_WINDOW = BT_PREAMBLE_SIZE + 1; +static const int BT_AD_DATA_INDEX_HUMIDITY = BT_AD_DATA_INDEX_WINDOW + 2; +static const int BT_AD_DATA_INDEX_TEMP = BT_AD_DATA_INDEX_HUMIDITY + 2; +static const int BT_AD_DATA_INDEX_CO2_LVL = BT_AD_DATA_INDEX_TEMP + 3; // sum of all value size + size for all keys 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[] = { - 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[] = { 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 ){ enum error_code ret = write_failed; + int shift = 0; // set values here - ad_data[BT_AD_DATA_INDEX_WINDOW] = (uint8_t)(window_value == open ? 1 : 0); - ad_data[BT_AD_DATA_INDEX_HUMIDITY] = (uint8_t)(hygro_value); - ad_data[BT_AD_DATA_INDEX_TEMP] = (uint16_t)(thermo_value); - ad_data[BT_AD_DATA_INDEX_CO2_LVL] = (uint32_t)(co2_lvl_value); + 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); + for(int i=0;i>(shift)) & 0xff); + } + for(int i=0;i>(shift)) & 0xff); + } 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()){ ret = success; }else{}