velues server fixes

This commit is contained in:
leo 2023-06-25 15:49:17 +02:00
parent 6a651ce129
commit 3c8fd28caa
Signed by: leo
GPG Key ID: 0DD993BFB2B307DB
4 changed files with 56 additions and 16 deletions

42
BLEh.c
View File

@ -2,6 +2,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <strings.h>
#include "esp_log.h" #include "esp_log.h"
#include "nimble/nimble_port.h" #include "nimble/nimble_port.h"
@ -159,4 +160,43 @@ void deinitBLE(){
} }
free(svcs_sizes); free(svcs_sizes);
free(notify_state); free(notify_state);
} }
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];
}
}

View File

@ -18,3 +18,4 @@ struct char_pres_format {
void initBLE(char* name); void initBLE(char* name);
void ble_advertise(void); void ble_advertise(void);
void set_gatt_services(struct ble_gatt_svc_def* svcs, uint16_t num); void set_gatt_services(struct ble_gatt_svc_def* svcs, uint16_t num);
void print_service(struct ble_gatt_svc_def* svcs);

View File

@ -1,9 +1,9 @@
#pragma once #pragma once
#include "host/ble_gatt.h" #include "host/ble_gatt.h"
#include "host/ble_uuid.h"
#include "BLEh.h" #include "BLEh.h"
#include "host/ble_uuid.h"
typedef void* gatt_value_server_handle_t; typedef void* gatt_value_server_handle_t;

View File

@ -4,6 +4,7 @@
#include "esp_log.h" #include "esp_log.h"
#include "host/ble_att.h"
#include "host/ble_gatt.h" #include "host/ble_gatt.h"
#include "host/ble_uuid.h" #include "host/ble_uuid.h"
#include "os/os_mbuf.h" #include "os/os_mbuf.h"
@ -14,7 +15,7 @@
#define TAG "GATT value server" #define TAG "GATT value server"
struct serve_conf { struct serve_conf {
void* value; uint8_t* value;
size_t value_size; size_t value_size;
struct char_pres_format* format; struct char_pres_format* format;
}; };
@ -22,17 +23,17 @@ struct serve_conf {
static struct serve_conf** serve_confs; 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; static uint16_t svcs_num = 0;
struct char_pres_format* formats; struct char_pres_format* formats;
struct ble_gatt_chr_def** chars; struct ble_gatt_chr_def** chars;
struct ble_gatt_dsc_def** descrs; 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; int rc = 0;
struct serve_conf* conf = arg; struct serve_conf* conf = arg;
switch(ctxt->op){ switch(ctxt->op){
case BLE_GATT_ACCESS_OP_READ_CHR: 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); 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)); 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)); 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++){ for(int i = 0; i < values_num; i++){
serve_confs = realloc(serve_confs, (serve_num + 1) * sizeof(struct serve_conf*)); 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] = 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]->value_size = value_size;
serve_confs[serve_num]->format = &formats[svcs_num]; serve_confs[serve_num]->format = &formats[svcs_num];
descrs = realloc(descrs, (serve_num + 1) * sizeof(struct ble_gatt_dsc_def*)); descrs = realloc(descrs, (serve_num + 1) * sizeof(struct ble_gatt_dsc_def*));
descrs[serve_num] = malloc(2 * sizeof(struct ble_gatt_dsc_def)); descrs[serve_num] = malloc(2 * sizeof(struct ble_gatt_dsc_def));
struct ble_gatt_dsc_def dsc[] = { struct ble_gatt_dsc_def dsc = {
[0] = {
.uuid = char_pres_format, .uuid = char_pres_format,
.access_cb = read_handler, .access_cb = read_handler,
.arg = serve_confs[serve_num], .arg = serve_confs[serve_num],
.att_flags = BLE_GATT_CHR_F_READ, .att_flags = BLE_ATT_F_READ,
.min_key_size = 0, .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 = { struct ble_gatt_chr_def chr = {
.uuid = char_uuid, .uuid = char_uuid,
.access_cb = read_handler, .access_cb = read_handler,
.arg = serve_confs[serve_num], .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], .descriptors = descrs[serve_num],
}; };
memcpy(&chrs[i], &chr, sizeof(chr)); memcpy(&chrs[i], &chr, sizeof(chr));