ble logic
This commit is contained in:
parent
a098c0e68d
commit
a4f3619a2b
@ -7,9 +7,13 @@
|
|||||||
#include "esp_gatts_api.h"
|
#include "esp_gatts_api.h"
|
||||||
#include "esp_gatt_common_api.h"
|
#include "esp_gatt_common_api.h"
|
||||||
|
|
||||||
|
#include "scd4x_data.h"
|
||||||
|
|
||||||
#define ADV_CONFIG_FLAG (1 << 0)
|
#define ADV_CONFIG_FLAG (1 << 0)
|
||||||
#define SCAN_RSP_CONFIG_FLAG (1 << 1)
|
#define SCAN_RSP_CONFIG_FLAG (1 << 1)
|
||||||
|
|
||||||
|
static configuration_data_t* main_app_conf;
|
||||||
|
|
||||||
static uint8_t adv_config_done = 0;
|
static uint8_t adv_config_done = 0;
|
||||||
|
|
||||||
static struct gatts_profile_inst env_sense_profile_tab[PROFILE_NUM] = {
|
static struct gatts_profile_inst env_sense_profile_tab[PROFILE_NUM] = {
|
||||||
@ -35,7 +39,7 @@ static esp_ble_adv_data_t env_sense_adv_data = {
|
|||||||
.include_txpower = true,
|
.include_txpower = true,
|
||||||
.min_interval = 0x0006,
|
.min_interval = 0x0006,
|
||||||
.max_interval = 0x0010,
|
.max_interval = 0x0010,
|
||||||
.appearance = 0x00,
|
.appearance = 0x054A, // Carbon Dioxide Sensor
|
||||||
.manufacturer_len = 0, //TEST_MANUFACTURER_DATA_LEN,
|
.manufacturer_len = 0, //TEST_MANUFACTURER_DATA_LEN,
|
||||||
.p_manufacturer_data = NULL, //&test_manufacturer[0],
|
.p_manufacturer_data = NULL, //&test_manufacturer[0],
|
||||||
.service_data_len = 0,
|
.service_data_len = 0,
|
||||||
@ -44,6 +48,22 @@ static esp_ble_adv_data_t env_sense_adv_data = {
|
|||||||
.p_service_uuid = service_uuid,
|
.p_service_uuid = service_uuid,
|
||||||
.flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT),
|
.flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT),
|
||||||
};
|
};
|
||||||
|
static esp_ble_adv_data_t scan_rsp_data = {
|
||||||
|
.set_scan_rsp = true,
|
||||||
|
.include_name = true,
|
||||||
|
.include_txpower = true,
|
||||||
|
.min_interval = 0x0006,
|
||||||
|
.max_interval = 0x0010,
|
||||||
|
.appearance = 0x054A,
|
||||||
|
.manufacturer_len = 0, //TEST_MANUFACTURER_DATA_LEN,
|
||||||
|
.p_manufacturer_data = NULL, //&test_manufacturer[0],
|
||||||
|
.service_data_len = 0,
|
||||||
|
.p_service_data = NULL,
|
||||||
|
.service_uuid_len = sizeof(service_uuid),
|
||||||
|
.p_service_uuid = service_uuid,
|
||||||
|
.flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT),
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
static esp_ble_adv_params_t adv_params = {
|
static esp_ble_adv_params_t adv_params = {
|
||||||
.adv_int_min = 0x20,
|
.adv_int_min = 0x20,
|
||||||
@ -57,19 +77,43 @@ static esp_ble_adv_params_t adv_params = {
|
|||||||
|
|
||||||
static const uint16_t primary_service_uuid = ESP_GATT_UUID_PRI_SERVICE;
|
static const uint16_t primary_service_uuid = ESP_GATT_UUID_PRI_SERVICE;
|
||||||
static const uint16_t env_service_uuid = ENVIRONMENTAL_SENSING_UUID;
|
static const uint16_t env_service_uuid = ENVIRONMENTAL_SENSING_UUID;
|
||||||
|
static const uint16_t ES_measurement_uuid = 0x290C;
|
||||||
static const uint16_t char_dec_uuid = ESP_GATT_UUID_CHAR_DECLARE;
|
static const uint16_t char_dec_uuid = ESP_GATT_UUID_CHAR_DECLARE;
|
||||||
static const uint16_t char_co2_uuid = 0x0c39;
|
static const uint16_t char_co2_uuid = 0x0c39; // random
|
||||||
|
static const uint16_t char_temp_uuid = 0x2A6E;
|
||||||
|
static const uint16_t char_hum_uuid = 0x2A6F;
|
||||||
static const uint16_t char_prop_read = ESP_GATT_CHAR_PROP_BIT_READ;
|
static const uint16_t char_prop_read = ESP_GATT_CHAR_PROP_BIT_READ;
|
||||||
//static const uint8_t char_prop_read_write_notify = ESP_GATT_CHAR_PROP_BIT_WRITE | ESP_GATT_CHAR_PROP_BIT_READ | ESP_GATT_CHAR_PROP_BIT_NOTIFY;
|
//static const uint8_t char_prop_read_write_notify = ESP_GATT_CHAR_PROP_BIT_WRITE | ESP_GATT_CHAR_PROP_BIT_READ | ESP_GATT_CHAR_PROP_BIT_NOTIFY;
|
||||||
|
|
||||||
static const uint8_t CO2_char_val[]={
|
static const uint8_t CO2_char_descr[]={
|
||||||
0x00, 0x00, // Flags (RFU)
|
0x00, 0x00, // Flags (RFU)
|
||||||
0x01, // Sampling function : Instantaneous
|
0x01, // Sampling function : Instantaneous
|
||||||
30, // Measurement period : 30s TODO
|
0x1E, 0x00, 0x00, // Measurement period : 30s TODO
|
||||||
30, // Update Interval : 30s TODO
|
0x1E, 0x00, 0x00, // Update Interval : 30s TODO
|
||||||
0x01, // Application : Air
|
0x01, // Application : Air
|
||||||
0xFF, // Mesurement Uncertainty : TODO
|
0xFF, // Mesurement Uncertainty : TODO
|
||||||
};
|
};
|
||||||
|
static const uint8_t temp_char_descr[]={
|
||||||
|
0x00, 0x00, // Flags (RFU)
|
||||||
|
0x01, // Sampling function : Instantaneous
|
||||||
|
0x1E, 0x00, 0x00, // Measurement period : 30s TODO
|
||||||
|
0x1E, 0x00, 0x00, // Update Interval : 30s TODO
|
||||||
|
0x01, // Application : Air
|
||||||
|
0xFF, // Mesurement Uncertainty : TODO
|
||||||
|
};
|
||||||
|
static const uint8_t hum_char_descr[]={
|
||||||
|
0x00, 0x00, // Flags (RFU)
|
||||||
|
0x01, // Sampling function : Instantaneous
|
||||||
|
0x1E, 0x00, 0x00, // Measurement period : 30s TODO
|
||||||
|
0x1E, 0x00, 0x00, // Update Interval : 30s TODO
|
||||||
|
0x01, // Application : Air
|
||||||
|
0xFF, // Mesurement Uncertainty : TODO
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
static const uint16_t CO2_value = 1260;
|
||||||
|
static const int16_t temp_value = 1560;
|
||||||
|
static const uint16_t hum_value = 4200;
|
||||||
|
|
||||||
static const esp_gatts_attr_db_t gatt_db[ENV_IDX_NB] =
|
static const esp_gatts_attr_db_t gatt_db[ENV_IDX_NB] =
|
||||||
{
|
{
|
||||||
@ -85,14 +129,45 @@ static const esp_gatts_attr_db_t gatt_db[ENV_IDX_NB] =
|
|||||||
|
|
||||||
/* Characteristic Value */
|
/* Characteristic Value */
|
||||||
[CO2_IDX_MEAS_VAL] =
|
[CO2_IDX_MEAS_VAL] =
|
||||||
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&char_co2_uuid, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE,
|
{{ESP_GATT_RSP_BY_APP}, {ESP_UUID_LEN_16, (uint8_t *)&char_co2_uuid, ESP_GATT_PERM_READ,
|
||||||
GATTS_CHAR_VAL_LEN_MAX, sizeof(CO2_char_val), (uint8_t *)&CO2_char_val}},
|
GATTS_CHAR_VAL_LEN_MAX, sizeof(CO2_value), (uint8_t *)&CO2_value}},
|
||||||
|
|
||||||
/* Client Characteristic Configuration Descriptor */
|
/* Client Characteristic Configuration Descriptor */
|
||||||
/*[IDX_CHAR_CFG_A] = TODO
|
[CO2_IDX_MEAS_DESC] =
|
||||||
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_client_config_uuid, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE,
|
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&ES_measurement_uuid, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE,
|
||||||
sizeof(uint16_t), sizeof(heart_measurement_ccc), (uint8_t *)heart_measurement_ccc}},
|
sizeof(CO2_char_descr), sizeof(CO2_char_descr), (uint8_t *)CO2_char_descr}},
|
||||||
*/
|
|
||||||
|
/* Characteristic Declaration */
|
||||||
|
[T_IDX_MEAS_CHAR] =
|
||||||
|
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&char_dec_uuid, ESP_GATT_PERM_READ,
|
||||||
|
CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read}},
|
||||||
|
|
||||||
|
/* Characteristic Value */
|
||||||
|
[T_IDX_MEAS_VAL] =
|
||||||
|
{{ESP_GATT_RSP_BY_APP}, {ESP_UUID_LEN_16, (uint8_t *)&char_temp_uuid, ESP_GATT_PERM_READ,
|
||||||
|
GATTS_CHAR_VAL_LEN_MAX, sizeof(temp_value), (uint8_t *)&temp_value}},
|
||||||
|
|
||||||
|
/* Client Characteristic Configuration Descriptor */
|
||||||
|
[T_IDX_MEAS_DESC] =
|
||||||
|
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&ES_measurement_uuid, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE,
|
||||||
|
sizeof(temp_char_descr), sizeof(temp_char_descr), (uint8_t *)temp_char_descr}},
|
||||||
|
|
||||||
|
/* Characteristic Declaration */
|
||||||
|
[HUM_IDX_MEAS_CHAR] =
|
||||||
|
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&char_dec_uuid, ESP_GATT_PERM_READ,
|
||||||
|
CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read}},
|
||||||
|
|
||||||
|
/* Characteristic Value */
|
||||||
|
[HUM_IDX_MEAS_VAL] =
|
||||||
|
{{ESP_GATT_RSP_BY_APP}, {ESP_UUID_LEN_16, (uint8_t *)&char_hum_uuid, ESP_GATT_PERM_READ,
|
||||||
|
GATTS_CHAR_VAL_LEN_MAX, sizeof(hum_value), (uint8_t *)&hum_value}},
|
||||||
|
|
||||||
|
/* Client Characteristic Configuration Descriptor */
|
||||||
|
[HUM_IDX_MEAS_DESC] =
|
||||||
|
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&ES_measurement_uuid, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE,
|
||||||
|
sizeof(hum_char_descr), sizeof(hum_char_descr), (uint8_t *)hum_char_descr}},
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -166,12 +241,35 @@ void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts
|
|||||||
ESP_ERROR_CHECK(esp_ble_gap_set_device_name(device_name));
|
ESP_ERROR_CHECK(esp_ble_gap_set_device_name(device_name));
|
||||||
ESP_ERROR_CHECK(esp_ble_gap_config_adv_data(&env_sense_adv_data));
|
ESP_ERROR_CHECK(esp_ble_gap_config_adv_data(&env_sense_adv_data));
|
||||||
adv_config_done |= ADV_CONFIG_FLAG;
|
adv_config_done |= ADV_CONFIG_FLAG;
|
||||||
//ESP_ERROR_CHECK(esp_ble_gap_config_adv_data(&scan_rsp_data));
|
ESP_ERROR_CHECK(esp_ble_gap_config_adv_data(&scan_rsp_data));
|
||||||
//adv_config_done |= SCAN_RSP_CONFIG_FLAG;
|
adv_config_done |= SCAN_RSP_CONFIG_FLAG;
|
||||||
ESP_ERROR_CHECK(esp_ble_gatts_create_attr_tab(gatt_db, gatts_if, ENV_IDX_NB, ENVIRONMENTAL_SENSING_IDX));
|
ESP_ERROR_CHECK(esp_ble_gatts_create_attr_tab(gatt_db, gatts_if, ENV_IDX_NB, ENVIRONMENTAL_SENSING_IDX));
|
||||||
break;
|
break;
|
||||||
case ESP_GATTS_READ_EVT:
|
case ESP_GATTS_READ_EVT:
|
||||||
ESP_LOGI("bt", "read event NI");
|
ESP_LOGI("bt", "read event");
|
||||||
|
esp_gatt_rsp_t rsp;
|
||||||
|
memset(&rsp, 0, sizeof(esp_gatt_rsp_t));
|
||||||
|
rsp.attr_value.handle = param->read.handle;
|
||||||
|
if(param->read.handle == env_handle_table[CO2_IDX_MEAS_VAL]){
|
||||||
|
uint16_t co2 = main_app_conf->measure->co2;
|
||||||
|
rsp.attr_value.len = sizeof(co2);
|
||||||
|
memcpy(rsp.attr_value.value, &co2, sizeof(co2));
|
||||||
|
}
|
||||||
|
else if(param->read.handle == env_handle_table[T_IDX_MEAS_VAL]){
|
||||||
|
int32_t raw_temp = main_app_conf->measure->temperature;
|
||||||
|
int16_t temperature = raw_temp/10;
|
||||||
|
ESP_LOGI("bt", "temp %i", temperature);
|
||||||
|
rsp.attr_value.len = sizeof(temperature);
|
||||||
|
memcpy(rsp.attr_value.value, &temperature, sizeof(temperature));
|
||||||
|
|
||||||
|
}
|
||||||
|
else if(param->read.handle == env_handle_table[HUM_IDX_MEAS_VAL]){
|
||||||
|
int32_t raw_hum = main_app_conf->measure->humidity;
|
||||||
|
int16_t hum = raw_hum/10;
|
||||||
|
rsp.attr_value.len = sizeof(hum);
|
||||||
|
memcpy(rsp.attr_value.value, &hum, sizeof(hum));
|
||||||
|
}
|
||||||
|
esp_ble_gatts_send_response(gatts_if, param->read.conn_id, param->read.trans_id, ESP_GATT_OK, &rsp);
|
||||||
break;
|
break;
|
||||||
case ESP_GATTS_WRITE_EVT:
|
case ESP_GATTS_WRITE_EVT:
|
||||||
ESP_LOGI("bt", "write event NI");
|
ESP_LOGI("bt", "write event NI");
|
||||||
@ -221,8 +319,9 @@ void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void initBle(void){
|
void initBle(configuration_data_t* main_conf){
|
||||||
ESP_LOGI("initBle", "starting bluetooth");
|
ESP_LOGI("initBle", "starting bluetooth");
|
||||||
|
main_app_conf = main_conf;
|
||||||
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
|
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
|
||||||
ESP_ERROR_CHECK(esp_bt_controller_init(&bt_cfg));
|
ESP_ERROR_CHECK(esp_bt_controller_init(&bt_cfg));
|
||||||
ESP_ERROR_CHECK(esp_bt_controller_enable(ESP_BT_MODE_BLE));
|
ESP_ERROR_CHECK(esp_bt_controller_enable(ESP_BT_MODE_BLE));
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
idf_component_register(SRCS "BTlib.c"
|
idf_component_register(SRCS "BTlib.c"
|
||||||
INCLUDE_DIRS "include"
|
INCLUDE_DIRS "include"
|
||||||
REQUIRES bt)
|
REQUIRES bt configuration sensirion_i2c_scd4x)
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
#include "esp_gap_ble_api.h"
|
#include "esp_gap_ble_api.h"
|
||||||
#include "esp_gatts_api.h"
|
#include "esp_gatts_api.h"
|
||||||
|
|
||||||
|
#include "configuration.h"
|
||||||
|
|
||||||
#define C_WL_MODE_BLE 0b100
|
#define C_WL_MODE_BLE 0b100
|
||||||
|
|
||||||
#define SENSOR_BLE_APP_ID 0x69
|
#define SENSOR_BLE_APP_ID 0x69
|
||||||
@ -16,12 +18,15 @@ enum{
|
|||||||
ENV_IDX_SVC,
|
ENV_IDX_SVC,
|
||||||
CO2_IDX_MEAS_CHAR,
|
CO2_IDX_MEAS_CHAR,
|
||||||
CO2_IDX_MEAS_VAL,
|
CO2_IDX_MEAS_VAL,
|
||||||
|
CO2_IDX_MEAS_DESC,
|
||||||
|
|
||||||
T_IDX_MEAS_CHAR,
|
T_IDX_MEAS_CHAR,
|
||||||
T_IDX_MEAS_VAL,
|
T_IDX_MEAS_VAL,
|
||||||
|
T_IDX_MEAS_DESC,
|
||||||
|
|
||||||
HUM_IDX_MEAS_CHAR,
|
HUM_IDX_MEAS_CHAR,
|
||||||
HUM_IDX_MEAS,
|
HUM_IDX_MEAS_VAL,
|
||||||
|
HUM_IDX_MEAS_DESC,
|
||||||
ENV_IDX_NB,
|
ENV_IDX_NB,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -46,4 +51,4 @@ void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp
|
|||||||
void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t* param);
|
void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t* param);
|
||||||
|
|
||||||
|
|
||||||
void initBle(void);
|
void initBle(configuration_data_t* main_conf);
|
||||||
|
@ -58,7 +58,7 @@ void app_main(void){
|
|||||||
wifi_init_softap(conf->wifi_config);
|
wifi_init_softap(conf->wifi_config);
|
||||||
break;
|
break;
|
||||||
case C_WL_MODE_BLE:
|
case C_WL_MODE_BLE:
|
||||||
initBle();
|
initBle(conf);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user