From a098c0e68dfd0ab533efd7f27dd5f35964f4ec44 Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 11 Apr 2022 17:03:43 +0200 Subject: [PATCH] gatt event handler --- components/BTlib/BTlib.c | 32 +++++++++++++++++++++++++++----- components/BTlib/include/BTlib.h | 1 + 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/components/BTlib/BTlib.c b/components/BTlib/BTlib.c index ba3c176..66e65bd 100644 --- a/components/BTlib/BTlib.c +++ b/components/BTlib/BTlib.c @@ -12,7 +12,7 @@ static uint8_t adv_config_done = 0; -static struct gatts_profile_inst env_sense_profile_tab[1] = { +static struct gatts_profile_inst env_sense_profile_tab[PROFILE_NUM] = { [ENVIRONMENTAL_SENSING_IDX] = { .gatts_cb = gatts_profile_event_handler, .gatts_if = ESP_GATT_IF_NONE, @@ -21,9 +21,12 @@ static struct gatts_profile_inst env_sense_profile_tab[1] = { uint16_t env_handle_table[ENV_IDX_NB]; -static uint8_t service_uuid[2] = { +static uint8_t service_uuid[16] = { + 0x2f, 0x3f, 0x08, 0x63, 0x3b, 0x44, 0x40, 0xa4, 0x71, 0x4a, 0xdc, 0x0c, ENVIRONMENTAL_SENSING_UUID & 0xFF, ENVIRONMENTAL_SENSING_UUID & 0xFF00 >> 8, + 0x00, + 0x00, }; static esp_ble_adv_data_t env_sense_adv_data = { @@ -56,7 +59,8 @@ 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 char_dec_uuid = ESP_GATT_UUID_CHAR_DECLARE; static const uint16_t char_co2_uuid = 0x0c39; -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 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 CO2_char_val[]={ 0x00, 0x00, // Flags (RFU) @@ -77,7 +81,7 @@ static const esp_gatts_attr_db_t gatt_db[ENV_IDX_NB] = /* Characteristic Declaration */ [CO2_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_write_notify}}, + CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read}}, /* Characteristic Value */ [CO2_IDX_MEAS_VAL] = @@ -135,7 +139,25 @@ void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t* par } void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t* param){ - + if (event == ESP_GATTS_REG_EVT) { + if (param->reg.status == ESP_GATT_OK) + env_sense_profile_tab[ENVIRONMENTAL_SENSING_IDX].gatts_if = gatts_if; + else{ + ESP_LOGE("bt", "reg app failed, app_id %04x, status %d",param->reg.app_id,param->reg.status); + return; + } + } + do{ + int idx; + for (idx = 0; idx < PROFILE_NUM; idx++) { + /* ESP_GATT_IF_NONE, not specify a certain gatt_if, need to call every profile cb function */ + if (gatts_if == ESP_GATT_IF_NONE || gatts_if == env_sense_profile_tab[idx].gatts_if) { + if (env_sense_profile_tab[idx].gatts_cb) + env_sense_profile_tab[idx].gatts_cb(event, gatts_if, param); + } + } + } + while (0); } void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t* param){ diff --git a/components/BTlib/include/BTlib.h b/components/BTlib/include/BTlib.h index e8235d4..53e4075 100644 --- a/components/BTlib/include/BTlib.h +++ b/components/BTlib/include/BTlib.h @@ -5,6 +5,7 @@ #define SENSOR_BLE_APP_ID 0x69 #define ENVIRONMENTAL_SENSING_IDX 0 +#define PROFILE_NUM 1 #define ENVIRONMENTAL_SENSING_UUID 0x181A