notification

This commit is contained in:
leo 2023-06-25 17:00:09 +02:00
parent 3c8fd28caa
commit 2ce2ec0e5a
Signed by: leo
GPG Key ID: 0DD993BFB2B307DB
4 changed files with 45 additions and 7 deletions

25
BLEh.c
View File

@ -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 &notify_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));
}
}

View File

@ -19,3 +19,4 @@ 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);
void notify(uint16_t svc_ind, uint16_t chr_ind, void* value, size_t value_size);

View File

@ -4,7 +4,11 @@
#include "host/ble_uuid.h"
#include "BLEh.h"
#include <stdint.h>
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);
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);

View File

@ -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(){
@ -109,3 +113,11 @@ void simple_gatt_value_server_deinit(){
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);
}
}