@@ -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<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, 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{}
|
||||
|
||||
Reference in New Issue
Block a user