From 2ce2ec0e5a803896c8797eada748f2f98dc1f3fa Mon Sep 17 00:00:00 2001 From: leo Date: Sun, 25 Jun 2023 17:00:09 +0200 Subject: [PATCH] notification --- BLEh.c | 25 +++++++++++++++++++++++-- include/BLEh.h | 3 ++- include/simple_gatt_value_server.h | 6 +++++- simple_gatt_value_server.c | 18 +++++++++++++++--- 4 files changed, 45 insertions(+), 7 deletions(-) diff --git a/BLEh.c b/BLEh.c index 7194642..55bfc7a 100644 --- a/BLEh.c +++ b/BLEh.c @@ -59,6 +59,14 @@ static uint8_t* get_notify_state(uint16_t handle){ return NULL; } +static uint8_t* get_notify_state_from_indices(uint16_t svc_ind, uint16_t chr_ind){ + return ¬ify_state[svc_ind][chr_ind]; +} + +static uint16_t get_att_handle(uint16_t svc_ind, uint16_t chr_ind){ + return att_handle[svc_ind][chr_ind]; +} + static int ble_gap_event(struct ble_gap_event *event, void* arg){ switch(event->type){ case BLE_GAP_EVENT_CONNECT: @@ -132,8 +140,10 @@ void set_gatt_services(struct ble_gatt_svc_def* svcs, uint16_t num){ notify_state[svc_ind] = malloc(svcs_sizes[svc_ind] * sizeof(uint8_t)); for(int chr_ind = 0; chr_ind < svcs_sizes[svc_ind]; chr_ind++){ - struct ble_gatt_chr_def chr = svcs[svc_ind].characteristics[chr_ind]; - chr.val_handle = &att_handle[svc_ind][chr_ind]; + struct ble_gatt_chr_def* chr = &gatt_svr_svcs[svc_ind].characteristics[chr_ind]; + chr->val_handle = &att_handle[svc_ind][chr_ind]; + + notify_state[svc_ind][chr_ind] = 0; } } } @@ -200,3 +210,14 @@ void print_service(struct ble_gatt_svc_def* svcs){ svc = svcs[svc_id]; } } + +void notify(uint16_t svc_ind, uint16_t chr_ind, void* value, size_t value_size){ + uint16_t handle = get_att_handle(svc_ind, chr_ind); + + uint8_t state = *get_notify_state_from_indices(svc_ind, chr_ind); + + if(state){ + struct os_mbuf* om = ble_hs_mbuf_from_flat(value, value_size); + ESP_ERROR_CHECK(ble_gattc_notify_custom(conn_handle, handle, om)); + } +} \ No newline at end of file diff --git a/include/BLEh.h b/include/BLEh.h index cc3f5fa..ca63cb8 100644 --- a/include/BLEh.h +++ b/include/BLEh.h @@ -18,4 +18,5 @@ struct char_pres_format { void initBLE(char* name); void ble_advertise(void); void set_gatt_services(struct ble_gatt_svc_def* svcs, uint16_t num); -void print_service(struct ble_gatt_svc_def* svcs); \ No newline at end of file +void print_service(struct ble_gatt_svc_def* svcs); +void notify(uint16_t svc_ind, uint16_t chr_ind, void* value, size_t value_size); \ No newline at end of file diff --git a/include/simple_gatt_value_server.h b/include/simple_gatt_value_server.h index e534bc1..fd06926 100644 --- a/include/simple_gatt_value_server.h +++ b/include/simple_gatt_value_server.h @@ -4,7 +4,11 @@ #include "host/ble_uuid.h" #include "BLEh.h" +#include typedef void* gatt_value_server_handle_t; -gatt_value_server_handle_t simple_gatt_value_server(void* values, int values_num, size_t values_size, struct char_pres_format format, struct ble_gatt_svc_def* svc, ble_uuid_t* uuid); \ No newline at end of file +gatt_value_server_handle_t simple_gatt_value_server(void* values, int values_num, size_t values_size, struct char_pres_format format, struct ble_gatt_svc_def* svc, uint16_t svc_ind, ble_uuid_t* uuid); +void simple_gatt_value_server_deinit(); + +void simple_gatt_value_server_notify(gatt_value_server_handle_t handle, int values_num); \ No newline at end of file diff --git a/simple_gatt_value_server.c b/simple_gatt_value_server.c index 9591441..7247c6f 100644 --- a/simple_gatt_value_server.c +++ b/simple_gatt_value_server.c @@ -18,10 +18,12 @@ struct serve_conf { uint8_t* value; size_t value_size; struct char_pres_format* format; + uint16_t svc_ind; + uint16_t chr_ind; }; static struct serve_conf** serve_confs; -typedef struct serve_conf* gatt_value_server_handle_t; +typedef struct serve_conf** gatt_value_server_handle_t; static uint16_t svcs_num = 0; struct char_pres_format* formats; @@ -51,7 +53,7 @@ int read_handler(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_acc ble_uuid_t* char_pres_format = BLE_UUID16_DECLARE(CHAR_PRES_FORMAT); -gatt_value_server_handle_t simple_gatt_value_server(uint8_t* values, size_t value_size, int values_num, struct char_pres_format format, struct ble_gatt_svc_def* svc, ble_uuid_t* char_uuid){ +gatt_value_server_handle_t simple_gatt_value_server(uint8_t* values, size_t value_size, int values_num, struct char_pres_format format, struct ble_gatt_svc_def* svc, uint16_t svc_ind, ble_uuid_t* char_uuid){ struct ble_gatt_chr_def* chrs = malloc((values_num + 1) * sizeof(struct ble_gatt_chr_def)); formats = realloc(formats, (svcs_num + 1) * sizeof(struct char_pres_format)); @@ -66,6 +68,8 @@ gatt_value_server_handle_t simple_gatt_value_server(uint8_t* values, size_t valu serve_confs[serve_num]->value = values+(i*value_size); serve_confs[serve_num]->value_size = value_size; serve_confs[serve_num]->format = &formats[svcs_num]; + serve_confs[serve_num]->svc_ind = svc_ind; + serve_confs[serve_num]->chr_ind = i; descrs = realloc(descrs, (serve_num + 1) * sizeof(struct ble_gatt_dsc_def*)); descrs[serve_num] = malloc(2 * sizeof(struct ble_gatt_dsc_def)); @@ -96,7 +100,7 @@ gatt_value_server_handle_t simple_gatt_value_server(uint8_t* values, size_t valu svc->characteristics = chrs; svcs_num++; - return serve_confs[serve_num - values_num]; + return &serve_confs[serve_num - values_num]; } void simple_gatt_value_server_deinit(){ @@ -108,4 +112,12 @@ void simple_gatt_value_server_deinit(){ free(chars); for(int i = 0; i < serve_num; i++) free(descrs[i]); free(descrs); +} + +void simple_gatt_value_server_notify(gatt_value_server_handle_t handle, int values_num){ + for(int i = 0; i < values_num; i++){ + struct serve_conf* conf = ((struct serve_conf**)handle)[i]; + + notify(conf->svc_ind, conf->chr_ind, conf->value, conf->value_size); + } } \ No newline at end of file