Bluetooth: att: Add option to disable GATT writable name
This allow to set name at runtime while leaving GAP name characteristic read only. Signed-off-by: Szymon Janc <szymon.janc@codecoup.pl>
This commit is contained in:
parent
4b22ba7e4b
commit
504584a998
|
@ -10,6 +10,7 @@ CONFIG_BT_ATT_PREPARE_COUNT=2
|
|||
CONFIG_BT_PRIVACY=y
|
||||
CONFIG_BT_DEVICE_NAME="Zephyr Peripheral Sample Long Name"
|
||||
CONFIG_BT_DEVICE_APPEARANCE=833
|
||||
CONFIG_BT_DEVICE_NAME_DYNAMIC=y
|
||||
CONFIG_BT_DEVICE_NAME_MAX=65
|
||||
|
||||
CONFIG_BT_SETTINGS=y
|
||||
|
|
|
@ -341,16 +341,28 @@ config BT_SCAN_WITH_IDENTITY
|
|||
disclosing local identity information. However, if the use case
|
||||
requires disclosing it then enable this option.
|
||||
|
||||
config BT_DEVICE_NAME_DYNAMIC
|
||||
bool "Allow to set Bluetooth device name on runtime"
|
||||
help
|
||||
Enabling this option allows for runtime configuration of Bluetooth
|
||||
device name.
|
||||
|
||||
config BT_DEVICE_NAME_MAX
|
||||
int "Maximum size in bytes for device name"
|
||||
depends on BT_DEVICE_NAME_DYNAMIC
|
||||
default 28
|
||||
default 0 if !BT_SETTINGS
|
||||
range 0 248
|
||||
range 2 248
|
||||
help
|
||||
Bluetooth device name storage size. Storage can be up to 248 bytes
|
||||
long (excluding NULL termination). 0 means that no storage space is
|
||||
allocated for device name in which case the device name cannot be
|
||||
changed at runtime.
|
||||
long (excluding NULL termination).
|
||||
|
||||
config BT_DEVICE_NAME_GATT_WRITABLE
|
||||
bool "Allow to write name by remote GATT clients"
|
||||
depends on BT_CONN && BT_DEVICE_NAME_DYNAMIC
|
||||
default y
|
||||
help
|
||||
Enabling this option allows remote GATT clients to write to device
|
||||
name GAP characteristic.
|
||||
|
||||
config BT_DEVICE_NAME
|
||||
string "Bluetooth device name"
|
||||
|
|
|
@ -53,13 +53,13 @@ static sys_slist_t db;
|
|||
static ssize_t read_name(struct bt_conn *conn, const struct bt_gatt_attr *attr,
|
||||
void *buf, u16_t len, u16_t offset)
|
||||
{
|
||||
const char *name = attr->user_data;
|
||||
const char *name = bt_get_name();
|
||||
|
||||
return bt_gatt_attr_read(conn, attr, buf, len, offset, name,
|
||||
strlen(name));
|
||||
}
|
||||
|
||||
#if CONFIG_BT_DEVICE_NAME_MAX > 0
|
||||
#if defined(CONFIG_BT_DEVICE_NAME_GATT_WRITABLE)
|
||||
|
||||
static ssize_t write_name(struct bt_conn *conn, const struct bt_gatt_attr *attr,
|
||||
const void *buf, u16_t len, u16_t offset,
|
||||
|
@ -86,7 +86,7 @@ static ssize_t write_name(struct bt_conn *conn, const struct bt_gatt_attr *attr,
|
|||
return len;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_BT_DEVICE_NAME */
|
||||
#endif /* CONFIG_BT_DEVICE_NAME_GATT_WRITABLE */
|
||||
|
||||
static ssize_t read_appearance(struct bt_conn *conn,
|
||||
const struct bt_gatt_attr *attr, void *buf,
|
||||
|
@ -100,7 +100,7 @@ static ssize_t read_appearance(struct bt_conn *conn,
|
|||
|
||||
static struct bt_gatt_attr gap_attrs[] = {
|
||||
BT_GATT_PRIMARY_SERVICE(BT_UUID_GAP),
|
||||
#if CONFIG_BT_DEVICE_NAME_MAX > 0
|
||||
#if defined(CONFIG_BT_DEVICE_NAME_GATT_WRITABLE)
|
||||
/* Require pairing for writes to device name */
|
||||
BT_GATT_CHARACTERISTIC(BT_UUID_GAP_DEVICE_NAME,
|
||||
BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
|
||||
|
@ -108,9 +108,8 @@ static struct bt_gatt_attr gap_attrs[] = {
|
|||
read_name, write_name, bt_dev.name),
|
||||
#else
|
||||
BT_GATT_CHARACTERISTIC(BT_UUID_GAP_DEVICE_NAME, BT_GATT_CHRC_READ,
|
||||
BT_GATT_PERM_READ, read_name, NULL,
|
||||
CONFIG_BT_DEVICE_NAME),
|
||||
#endif
|
||||
BT_GATT_PERM_READ, read_name, NULL, NULL),
|
||||
#endif /* CONFIG_BT_DEVICE_NAME_GATT_WRITABLE */
|
||||
BT_GATT_CHARACTERISTIC(BT_UUID_GAP_APPEARANCE, BT_GATT_CHRC_READ,
|
||||
BT_GATT_PERM_READ, read_appearance, NULL, NULL),
|
||||
};
|
||||
|
|
|
@ -4749,7 +4749,7 @@ static int set_ad(u16_t hci_op, const struct bt_ad *ad, size_t ad_len)
|
|||
|
||||
int bt_set_name(const char *name)
|
||||
{
|
||||
#if CONFIG_BT_DEVICE_NAME_MAX > 0
|
||||
#if defined(CONFIG_BT_DEVICE_NAME_DYNAMIC)
|
||||
size_t len = strlen(name);
|
||||
int err;
|
||||
|
||||
|
@ -4800,7 +4800,7 @@ int bt_set_name(const char *name)
|
|||
|
||||
const char *bt_get_name(void)
|
||||
{
|
||||
#if CONFIG_BT_DEVICE_NAME_MAX > 0
|
||||
#if defined(CONFIG_BT_DEVICE_NAME_DYNAMIC)
|
||||
return bt_dev.name;
|
||||
#else
|
||||
return CONFIG_BT_DEVICE_NAME;
|
||||
|
|
|
@ -166,7 +166,9 @@ struct bt_dev {
|
|||
#endif
|
||||
|
||||
/* Local Name */
|
||||
#if defined(CONFIG_BT_DEVICE_NAME_DYNAMIC)
|
||||
char name[CONFIG_BT_DEVICE_NAME_MAX];
|
||||
#endif
|
||||
};
|
||||
|
||||
extern struct bt_dev bt_dev;
|
||||
|
|
|
@ -121,6 +121,7 @@ static int set(int argc, char **argv, char *val)
|
|||
return 0;
|
||||
}
|
||||
|
||||
#if defined(CONFIG_BT_DEVICE_NAME_DYNAMIC)
|
||||
if (!strcmp(argv[0], "name")) {
|
||||
len = sizeof(bt_dev.name) - 1;
|
||||
settings_bytes_from_str(val, &bt_dev.name, &len);
|
||||
|
@ -129,6 +130,7 @@ static int set(int argc, char **argv, char *val)
|
|||
BT_DBG("Name set to %s", bt_dev.name);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(CONFIG_BT_PRIVACY)
|
||||
if (!strcmp(argv[0], "irk")) {
|
||||
|
@ -212,7 +214,7 @@ static int commit(void)
|
|||
generate_static_addr();
|
||||
}
|
||||
|
||||
#if CONFIG_BT_DEVICE_NAME_MAX > 0
|
||||
#if defined(CONFIG_BT_DEVICE_NAME_DYNAMIC)
|
||||
if (bt_dev.name[0] == '\0') {
|
||||
bt_set_name(CONFIG_BT_DEVICE_NAME);
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ CONFIG_BT_TINYCRYPT_ECC=y
|
|||
CONFIG_CONSOLE_SHELL=y
|
||||
CONFIG_BT_SHELL=y
|
||||
CONFIG_BT_DEVICE_NAME="test shell"
|
||||
CONFIG_BT_DEVICE_NAME_MAX=28
|
||||
CONFIG_BT_DEVICE_NAME_DYNAMIC=y
|
||||
CONFIG_BT_L2CAP_TX_BUF_COUNT=6
|
||||
|
||||
CONFIG_BT_SETTINGS=y
|
||||
|
|
Loading…
Reference in a new issue