char write callback

This commit is contained in:
leo 2023-06-26 15:34:32 +02:00
parent 544c26fdfd
commit a15c1ca68e
Signed by: leo
GPG Key ID: 0DD993BFB2B307DB
2 changed files with 31 additions and 3 deletions

28
BLEh.c
View File

@ -128,6 +128,23 @@ void ble_advertise(void){
ESP_ERROR_CHECK(ble_gap_adv_start(BLE_OWN_ADDR_PUBLIC, NULL, BLE_HS_FOREVER, &adv_params, ble_gap_event, NULL));
}
int gatt_svr_chr_write_get_data(struct os_mbuf *om, uint16_t min_len, uint16_t max_len, void *dst, uint16_t *len){
uint16_t om_len;
int rc;
om_len = OS_MBUF_PKTLEN(om);
if (om_len < min_len || om_len > max_len) {
return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
}
rc = ble_hs_mbuf_to_flat(om, dst, max_len, len);
if (rc != 0) {
return BLE_ATT_ERR_UNLIKELY;
}
return 0;
}
static int char_access_handler(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg){
int rc = 0;
@ -145,6 +162,12 @@ static int char_access_handler(uint16_t conn_handle, uint16_t attr_handle, struc
on_char_read_callback(svc_ind, chr_ind, &value, &value_size);
rc = os_mbuf_append(ctxt->om, value, value_size);
break;
case BLE_GATT_ACCESS_OP_WRITE_CHR:
;
if(on_char_write_callback == NULL) return 0;
rc = on_char_write_callback(svc_ind, chr_ind, ctxt->om);
break;
case BLE_GATT_ACCESS_OP_READ_DSC:
;
struct dsc_content* content = (struct dsc_content*)ctxt->dsc->arg;
@ -155,9 +178,12 @@ static int char_access_handler(uint16_t conn_handle, uint16_t attr_handle, struc
return rc ? BLE_ATT_ERR_INSUFFICIENT_RES : 0;
}
void add_on_char_read_handler(charReadCallback cb){
void set_on_char_read_handler(charReadCallback cb){
on_char_read_callback = cb;
}
void set_on_char_write_handler(charWriteCallback cb){
on_char_write_callback = cb;
}
void set_gatt_services(struct ble_gatt_svc_def* svcs, uint16_t num){
gatt_svr_svcs = svcs;

View File

@ -21,7 +21,7 @@ struct dsc_content {
};
typedef void (*charReadCallback)(int svc_ind, int chr_ind, void** value, size_t* value_size);
typedef void (*charWriteCallback)(int svc_ind, int chr_ind, void* value, size_t value_size);
typedef int (*charWriteCallback)(int svc_ind, int chr_ind, struct os_mbuf* om);
typedef void (*dscReadCallback)(int svc_ind, int chr_ind, void** value, size_t* value_size);
void initBLE(char* name);
@ -29,4 +29,6 @@ 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);
void add_on_char_read_handler(charReadCallback cb);
void set_on_char_read_handler(charReadCallback cb);
void set_on_char_write_handler(charWriteCallback cb);
int gatt_svr_chr_write_get_data(struct os_mbuf *om, uint16_t min_len, uint16_t max_len, void *dst, uint16_t *len);