From 3c8fd28caa25c77b394be7b0c526d273c224a91f Mon Sep 17 00:00:00 2001 From: leo Date: Sun, 25 Jun 2023 15:49:17 +0200 Subject: [PATCH] velues server fixes --- BLEh.c | 42 +++++++++++++++++++++++++++++- include/BLEh.h | 1 + include/simple_gatt_value_server.h | 2 +- simple_gatt_value_server.c | 27 +++++++++---------- 4 files changed, 56 insertions(+), 16 deletions(-) diff --git a/BLEh.c b/BLEh.c index a50c03e..7194642 100644 --- a/BLEh.c +++ b/BLEh.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "esp_log.h" #include "nimble/nimble_port.h" @@ -159,4 +160,43 @@ void deinitBLE(){ } free(svcs_sizes); free(notify_state); -} \ No newline at end of file +} + +void print_descriptor(struct ble_gatt_dsc_def* dsc){ + printf(" addr : %p\n", dsc); + printf(" uuid : %02x\n", ((uint16_t*)dsc->uuid)[1]); + printf(" access_cb : %p\n", dsc->access_cb); + printf(" att flags : %04x\n", dsc->att_flags); +} + +void print_char(struct ble_gatt_chr_def chr){ + printf(" uuid : %02x\n", ((uint16_t*)chr.uuid)[1]); + printf(" access_cb : %p\n", chr.access_cb); + printf(" arg : %p\n", chr.arg); + printf(" descr : %p\n", chr.descriptors); + + struct ble_gatt_dsc_def* dsc = &chr.descriptors[0]; + for(int i = 1; dsc->uuid != NULL; i++){ + print_descriptor(dsc); + dsc = &chr.descriptors[i]; + } +} + +void print_service(struct ble_gatt_svc_def* svcs){ + int svc_id = 0; + struct ble_gatt_svc_def svc = svcs[svc_id]; + for(svc_id = 0; svc.uuid != NULL; svc_id++){ + ESP_LOGI(TAG, "svc %d", svc_id); + struct ble_gatt_chr_def chr = svc.characteristics[0]; + int ind = 0; + + while(chr.uuid != NULL){ + ESP_LOGI(TAG, "char %d", ind); + print_char(chr); + ind++; + chr = svc.characteristics[ind]; + } + + svc = svcs[svc_id]; + } +} diff --git a/include/BLEh.h b/include/BLEh.h index 5065bef..cc3f5fa 100644 --- a/include/BLEh.h +++ b/include/BLEh.h @@ -18,3 +18,4 @@ 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 diff --git a/include/simple_gatt_value_server.h b/include/simple_gatt_value_server.h index 3cd46b1..e534bc1 100644 --- a/include/simple_gatt_value_server.h +++ b/include/simple_gatt_value_server.h @@ -1,9 +1,9 @@ #pragma once #include "host/ble_gatt.h" +#include "host/ble_uuid.h" #include "BLEh.h" -#include "host/ble_uuid.h" typedef void* gatt_value_server_handle_t; diff --git a/simple_gatt_value_server.c b/simple_gatt_value_server.c index 261d6b9..9591441 100644 --- a/simple_gatt_value_server.c +++ b/simple_gatt_value_server.c @@ -4,6 +4,7 @@ #include "esp_log.h" +#include "host/ble_att.h" #include "host/ble_gatt.h" #include "host/ble_uuid.h" #include "os/os_mbuf.h" @@ -14,7 +15,7 @@ #define TAG "GATT value server" struct serve_conf { - void* value; + uint8_t* value; size_t value_size; struct char_pres_format* format; }; @@ -22,17 +23,17 @@ struct serve_conf { static struct serve_conf** serve_confs; typedef struct serve_conf* gatt_value_server_handle_t; -static uint16_t svcs_num; +static uint16_t svcs_num = 0; struct char_pres_format* formats; struct ble_gatt_chr_def** chars; struct ble_gatt_dsc_def** descrs; -static uint16_t serve_num; +static uint16_t serve_num = 0; -static int read_handler(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg){ +int read_handler(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg){ int rc = 0; - struct serve_conf* conf = arg; + struct serve_conf* conf = arg; switch(ctxt->op){ case BLE_GATT_ACCESS_OP_READ_CHR: @@ -50,7 +51,7 @@ static int read_handler(uint16_t conn_handle, uint16_t attr_handle, struct ble_g ble_uuid_t* char_pres_format = BLE_UUID16_DECLARE(CHAR_PRES_FORMAT); -gatt_value_server_handle_t simple_gatt_value_server(void** 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, 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)); @@ -62,30 +63,28 @@ gatt_value_server_handle_t simple_gatt_value_server(void** values, size_t value_ for(int i = 0; i < values_num; i++){ serve_confs = realloc(serve_confs, (serve_num + 1) * sizeof(struct serve_conf*)); serve_confs[serve_num] = malloc(sizeof(struct serve_conf)); - serve_confs[serve_num]->value = values[i]; + 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]; descrs = realloc(descrs, (serve_num + 1) * sizeof(struct ble_gatt_dsc_def*)); descrs[serve_num] = malloc(2 * sizeof(struct ble_gatt_dsc_def)); - struct ble_gatt_dsc_def dsc[] = { - [0] = { + struct ble_gatt_dsc_def dsc = { .uuid = char_pres_format, .access_cb = read_handler, .arg = serve_confs[serve_num], - .att_flags = BLE_GATT_CHR_F_READ, + .att_flags = BLE_ATT_F_READ, .min_key_size = 0, - }, - { 0 }, }; - memcpy(descrs[serve_num], dsc, 2 * sizeof(struct ble_gatt_dsc_def)); + memcpy(&descrs[serve_num][0], &dsc, sizeof(struct ble_gatt_dsc_def)); + memset(&descrs[serve_num][1], 0, sizeof(struct ble_gatt_dsc_def)); struct ble_gatt_chr_def chr = { .uuid = char_uuid, .access_cb = read_handler, .arg = serve_confs[serve_num], - .flags = BLE_GATT_CHR_F_READ, + .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY, .descriptors = descrs[serve_num], }; memcpy(&chrs[i], &chr, sizeof(chr));