118 lines
3.1 KiB
C
118 lines
3.1 KiB
C
#include <stdint.h>
|
|
|
|
#include "host/ble_att.h"
|
|
#include "host/ble_gatt.h"
|
|
#include "host/ble_hs_mbuf.h"
|
|
#include "os/os_mbuf.h"
|
|
#include "esp_log.h"
|
|
|
|
#include "gatt_svcs.h"
|
|
#include "BLE_UUID.h"
|
|
|
|
static struct char_pres_format current_char_pres_format = {
|
|
.format = FORMAT_UINT32,
|
|
.exponent = -3,
|
|
.unit = AMPERE_UNIT_UUID,
|
|
.namespc = 1,
|
|
.descrH = NSP_DESC_MAIN & 0xff,
|
|
.descrL = (NSP_DESC_MAIN>>8) & 0xff,
|
|
};
|
|
|
|
static struct char_pres_format raw_voltage_char_pres_format = {
|
|
.format = FORMAT_UINT32,
|
|
.exponent = -3,
|
|
.unit = VOLTS_UNIT_UUID,
|
|
.namespc = 1,
|
|
.descrH = NSP_DESC_MAIN & 0xff,
|
|
.descrL = (NSP_DESC_MAIN>>8) & 0xff,
|
|
};
|
|
|
|
static struct char_pres_format refrsh_rate_char_pres_format = {
|
|
.format = FORMAT_UINT32,
|
|
.exponent = -3,
|
|
.unit = SECONDS_UNIT_UUID,
|
|
.namespc = 1,
|
|
.descrH = NSP_DESC_MAIN & 0xff,
|
|
.descrL = (NSP_DESC_MAIN>>8) & 0xff,
|
|
};
|
|
|
|
static int
|
|
gatt_svr_chr_write(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;
|
|
}
|
|
|
|
int gatt_char_access_cs(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg){
|
|
int rc = 0;
|
|
|
|
switch(ctxt->op){
|
|
case BLE_GATT_ACCESS_OP_READ_CHR:
|
|
;
|
|
uint16_t* handles = arg;
|
|
|
|
int id = -1;
|
|
for(int i = 0; i < EC_CHRS_NUM; i++) if(handles[i] == attr_handle) id = i;
|
|
uint32_t data = id;
|
|
rc = os_mbuf_append(ctxt->om, &data, sizeof(data));
|
|
break;
|
|
case BLE_GATT_ACCESS_OP_READ_DSC:
|
|
rc = os_mbuf_append(ctxt->om, ¤t_char_pres_format, CHAR_PRESENTATION_FORMAT_SIZE);
|
|
break;
|
|
}
|
|
|
|
return rc ? BLE_ATT_ERR_INSUFFICIENT_RES : 0;
|
|
}
|
|
|
|
int gatt_char_access_ev(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg){
|
|
int rc = 0;
|
|
|
|
switch(ctxt->op){
|
|
case BLE_GATT_ACCESS_OP_READ_CHR:
|
|
;
|
|
uint16_t* handles = arg;
|
|
|
|
int id = -1;
|
|
for(int i = 0; i < EV_CHRS_NUM; i++) if(handles[i] == attr_handle) id = i;
|
|
uint32_t data = id;
|
|
rc = os_mbuf_append(ctxt->om, &data, sizeof(data));
|
|
break;
|
|
case BLE_GATT_ACCESS_OP_READ_DSC:
|
|
rc = os_mbuf_append(ctxt->om, &raw_voltage_char_pres_format, CHAR_PRESENTATION_FORMAT_SIZE);
|
|
break;
|
|
}
|
|
|
|
return rc ? BLE_ATT_ERR_INSUFFICIENT_RES : 0;
|
|
}
|
|
|
|
int gatt_char_access_sampling_rate(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg){
|
|
int rc = 0;
|
|
|
|
switch(ctxt->op){
|
|
case BLE_GATT_ACCESS_OP_WRITE_CHR:
|
|
;
|
|
uint32_t data;
|
|
rc = gatt_svr_chr_write(ctxt->om, sizeof(data), sizeof(data), &data, NULL);
|
|
ESP_LOGI("BLE", "Refresh rate : %lu", data);
|
|
break;
|
|
case BLE_GATT_ACCESS_OP_READ_DSC:
|
|
rc = os_mbuf_append(ctxt->om, &refrsh_rate_char_pres_format, CHAR_PRESENTATION_FORMAT_SIZE);
|
|
break;
|
|
}
|
|
|
|
return rc ? BLE_ATT_ERR_INSUFFICIENT_RES : 0;
|
|
}
|