mirror of
https://github.com/espressif/esp-nimble.git
synced 2026-08-03 08:37:52 +00:00
nimble/gap: Make device name optionally writable
GAP service takes care of updating characteristic value. Application can register callback to be notified of value change and can then read this value and store somewhere (and restore on next boot). X-Original-Commit: 46e768f451bc4c7a0222348fee384b4d1c87aff1
This commit is contained in:
@@ -34,6 +34,10 @@ extern "C" {
|
||||
#define BLE_SVC_GAP_APPEARANCE_GEN_COMPUTER 128
|
||||
#define BLE_SVC_GAP_APPEARANCE_CYC_SPEED_AND_CADENCE_SENSOR 1157
|
||||
|
||||
typedef void (ble_svc_gap_chr_changed_fn) (uint16_t uuid);
|
||||
|
||||
void ble_svc_gap_set_chr_changed_cb(ble_svc_gap_chr_changed_fn *cb);
|
||||
|
||||
const char *ble_svc_gap_device_name(void);
|
||||
int ble_svc_gap_device_name_set(const char *name);
|
||||
uint16_t ble_svc_gap_device_appearance(void);
|
||||
|
||||
@@ -35,6 +35,8 @@
|
||||
/* XXX: This should be configurable. */
|
||||
#define BLE_SVC_GAP_NAME_MAX_LEN 31
|
||||
|
||||
static ble_svc_gap_chr_changed_fn *ble_svc_gap_chr_changed_cb_fn;
|
||||
|
||||
static char ble_svc_gap_name[BLE_SVC_GAP_NAME_MAX_LEN + 1] =
|
||||
MYNEWT_VAL(BLE_SVC_GAP_DEVICE_NAME);
|
||||
static const uint16_t ble_svc_gap_appearance =
|
||||
@@ -53,7 +55,12 @@ static const struct ble_gatt_svc_def ble_svc_gap_defs[] = {
|
||||
/*** Characteristic: Device Name. */
|
||||
.uuid = BLE_UUID16_DECLARE(BLE_SVC_GAP_CHR_UUID16_DEVICE_NAME),
|
||||
.access_cb = ble_svc_gap_access,
|
||||
.flags = BLE_GATT_CHR_F_READ,
|
||||
.flags = BLE_GATT_CHR_F_READ |
|
||||
#if MYNEWT_VAL(BLE_SVC_GAP_DEVICE_NAME_WRITE_PERM) >= 0
|
||||
BLE_GATT_CHR_F_WRITE |
|
||||
MYNEWT_VAL(BLE_SVC_GAP_DEVICE_NAME_WRITE_PERM) |
|
||||
#endif
|
||||
0,
|
||||
}, {
|
||||
/*** Characteristic: Appearance. */
|
||||
.uuid = BLE_UUID16_DECLARE(BLE_SVC_GAP_CHR_UUID16_APPEARANCE),
|
||||
@@ -84,6 +91,46 @@ static const struct ble_gatt_svc_def ble_svc_gap_defs[] = {
|
||||
},
|
||||
};
|
||||
|
||||
static int
|
||||
ble_svc_gap_device_name_read_access(struct ble_gatt_access_ctxt *ctxt)
|
||||
{
|
||||
int rc;
|
||||
|
||||
rc = os_mbuf_append(ctxt->om, ble_svc_gap_name, strlen(ble_svc_gap_name));
|
||||
|
||||
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
||||
}
|
||||
|
||||
static int
|
||||
ble_svc_gap_device_name_write_access(struct ble_gatt_access_ctxt *ctxt)
|
||||
{
|
||||
#if MYNEWT_VAL(BLE_SVC_GAP_DEVICE_NAME_WRITE_PERM) < 0
|
||||
assert(0);
|
||||
return 0;
|
||||
#else
|
||||
uint16_t om_len;
|
||||
int rc;
|
||||
|
||||
om_len = OS_MBUF_PKTLEN(ctxt->om);
|
||||
if (om_len > BLE_SVC_GAP_NAME_MAX_LEN) {
|
||||
return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
|
||||
}
|
||||
|
||||
rc = ble_hs_mbuf_to_flat(ctxt->om, ble_svc_gap_name, om_len, NULL);
|
||||
if (rc != 0) {
|
||||
return BLE_ATT_ERR_UNLIKELY;
|
||||
}
|
||||
|
||||
ble_svc_gap_name[om_len] = '\0';
|
||||
|
||||
if (ble_svc_gap_chr_changed_cb_fn) {
|
||||
ble_svc_gap_chr_changed_cb_fn(BLE_SVC_GAP_CHR_UUID16_DEVICE_NAME);
|
||||
}
|
||||
|
||||
return rc;
|
||||
#endif
|
||||
}
|
||||
|
||||
static int
|
||||
ble_svc_gap_access(uint16_t conn_handle, uint16_t attr_handle,
|
||||
struct ble_gatt_access_ctxt *ctxt, void *arg)
|
||||
@@ -108,10 +155,14 @@ ble_svc_gap_access(uint16_t conn_handle, uint16_t attr_handle,
|
||||
|
||||
switch (uuid16) {
|
||||
case BLE_SVC_GAP_CHR_UUID16_DEVICE_NAME:
|
||||
assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR);
|
||||
rc = os_mbuf_append(ctxt->om, ble_svc_gap_name,
|
||||
strlen(ble_svc_gap_name));
|
||||
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
||||
if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
|
||||
rc = ble_svc_gap_device_name_read_access(ctxt);
|
||||
} else if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
|
||||
rc = ble_svc_gap_device_name_write_access(ctxt);
|
||||
} else {
|
||||
assert(0);
|
||||
}
|
||||
return rc;
|
||||
|
||||
case BLE_SVC_GAP_CHR_UUID16_APPEARANCE:
|
||||
assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR);
|
||||
@@ -166,6 +217,12 @@ ble_svc_gap_device_appearance(void)
|
||||
return ble_svc_gap_appearance;
|
||||
}
|
||||
|
||||
void
|
||||
ble_svc_gap_set_chr_changed_cb(ble_svc_gap_chr_changed_fn *cb)
|
||||
{
|
||||
ble_svc_gap_chr_changed_cb_fn = cb;
|
||||
}
|
||||
|
||||
void
|
||||
ble_svc_gap_init(void)
|
||||
{
|
||||
|
||||
@@ -24,6 +24,15 @@ syscfg.defs:
|
||||
Default value for "Device Name" characteristics, unless overwritten
|
||||
by application.
|
||||
value: '"nimble"'
|
||||
BLE_SVC_GAP_DEVICE_NAME_WRITE_PERM:
|
||||
description: >
|
||||
Defines permissions for writing "Device Name" characteristics. Can
|
||||
be zero to allow write without extra permissions or combination of:
|
||||
BLE_GATT_CHR_F_WRITE_ENC
|
||||
BLE_GATT_CHR_F_WRITE_AUTHEN
|
||||
BLE_GATT_CHR_F_WRITE_AUTHOR
|
||||
Set to '-1' to make characteristic read only.
|
||||
value: -1
|
||||
BLE_SVC_GAP_APPEARANCE:
|
||||
description: 'Device appearance'
|
||||
value: 0
|
||||
|
||||
Reference in New Issue
Block a user