Bluetooth: GATT: Add GAP service by default
GAP is mandatory service and now that the db can only be build dynamically there is no reason to keep the applications registering it. Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
This commit is contained in:
parent
6058c699b4
commit
d837a6ec4b
28 changed files with 66 additions and 120 deletions
|
@ -1,61 +0,0 @@
|
|||
/** @file
|
||||
* @brief GAP Service sample
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2016 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <zephyr/types.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <misc/printk.h>
|
||||
#include <misc/byteorder.h>
|
||||
#include <zephyr.h>
|
||||
|
||||
#include <bluetooth/bluetooth.h>
|
||||
#include <bluetooth/hci.h>
|
||||
#include <bluetooth/conn.h>
|
||||
#include <bluetooth/uuid.h>
|
||||
#include <bluetooth/gatt.h>
|
||||
|
||||
static const char *gap_name;
|
||||
static u16_t gap_appearance;
|
||||
|
||||
static ssize_t read_name(struct bt_conn *conn, const struct bt_gatt_attr *attr,
|
||||
void *buf, u16_t len, u16_t offset)
|
||||
{
|
||||
return bt_gatt_attr_read(conn, attr, buf, len, offset, gap_name,
|
||||
strlen(gap_name));
|
||||
}
|
||||
|
||||
static ssize_t read_appearance(struct bt_conn *conn,
|
||||
const struct bt_gatt_attr *attr, void *buf,
|
||||
u16_t len, u16_t offset)
|
||||
{
|
||||
u16_t appearance = sys_cpu_to_le16(gap_appearance);
|
||||
|
||||
return bt_gatt_attr_read(conn, attr, buf, len, offset, &appearance,
|
||||
sizeof(appearance));
|
||||
}
|
||||
|
||||
static struct bt_gatt_attr attrs[] = {
|
||||
BT_GATT_PRIMARY_SERVICE(BT_UUID_GAP),
|
||||
BT_GATT_CHARACTERISTIC(BT_UUID_GAP_DEVICE_NAME, BT_GATT_CHRC_READ),
|
||||
BT_GATT_DESCRIPTOR(BT_UUID_GAP_DEVICE_NAME, BT_GATT_PERM_READ,
|
||||
read_name, NULL, NULL),
|
||||
BT_GATT_CHARACTERISTIC(BT_UUID_GAP_APPEARANCE, BT_GATT_CHRC_READ),
|
||||
BT_GATT_DESCRIPTOR(BT_UUID_GAP_APPEARANCE, BT_GATT_PERM_READ,
|
||||
read_appearance, NULL, NULL),
|
||||
};
|
||||
|
||||
void gap_init(const char *name, u16_t appearance)
|
||||
{
|
||||
gap_name = name;
|
||||
gap_appearance = appearance;
|
||||
|
||||
bt_gatt_register(attrs, ARRAY_SIZE(attrs));
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
/** @file
|
||||
* @brief GAP Service sample
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2016 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void gap_init(const char *name, u16_t appearance);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
|
@ -6,3 +6,4 @@ CONFIG_BLUETOOTH_PERIPHERAL=y
|
|||
CONFIG_BLUETOOTH_ATT_PREPARE_COUNT=2
|
||||
CONFIG_BLUETOOTH_PRIVACY=y
|
||||
CONFIG_BLUETOOTH_DEVICE_NAME="Test peripheral"
|
||||
CONFIG_BLUETOOTH_DEVICE_APPEARANCE=833
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
ccflags-y +=-I${ZEPHYR_BASE}/samples/bluetooth
|
||||
|
||||
obj-y = main.o ../../gatt/gap.o ../../gatt/hrs.o ../../gatt/dis.o \
|
||||
obj-y = main.o ../../gatt/hrs.o ../../gatt/dis.o \
|
||||
../../gatt/bas.o ../../gatt/cts.o
|
||||
|
|
|
@ -20,7 +20,6 @@
|
|||
#include <bluetooth/uuid.h>
|
||||
#include <bluetooth/gatt.h>
|
||||
|
||||
#include <gatt/gap.h>
|
||||
#include <gatt/hrs.h>
|
||||
#include <gatt/dis.h>
|
||||
#include <gatt/bas.h>
|
||||
|
@ -28,7 +27,6 @@
|
|||
|
||||
#define DEVICE_NAME CONFIG_BLUETOOTH_DEVICE_NAME
|
||||
#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)
|
||||
#define HEART_RATE_APPEARANCE 0x0341
|
||||
|
||||
/* Custom Service Variables */
|
||||
static struct bt_uuid_128 vnd_uuid = BT_UUID_INIT_128(
|
||||
|
@ -236,7 +234,6 @@ static void bt_ready(int err)
|
|||
|
||||
printk("Bluetooth initialized\n");
|
||||
|
||||
gap_init(DEVICE_NAME, HEART_RATE_APPEARANCE);
|
||||
hrs_init(0x01);
|
||||
bas_init();
|
||||
cts_init();
|
||||
|
|
|
@ -3,3 +3,4 @@ CONFIG_TEST_RANDOM_GENERATOR=y
|
|||
CONFIG_BLUETOOTH=y
|
||||
CONFIG_BLUETOOTH_PERIPHERAL=y
|
||||
CONFIG_BLUETOOTH_DEVICE_NAME="CSC peripheral"
|
||||
CONFIG_BLUETOOTH_DEVICE_APPEARANCE=1157
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
ccflags-y +=-I${ZEPHYR_BASE}/samples/bluetooth
|
||||
|
||||
obj-y = main.o ../../gatt/gap.o ../../gatt/dis.o ../../gatt/bas.o
|
||||
obj-y = main.o ../../gatt/dis.o ../../gatt/bas.o
|
||||
|
|
|
@ -21,13 +21,11 @@
|
|||
#include <bluetooth/uuid.h>
|
||||
#include <bluetooth/gatt.h>
|
||||
|
||||
#include <gatt/gap.h>
|
||||
#include <gatt/dis.h>
|
||||
#include <gatt/bas.h>
|
||||
|
||||
#define DEVICE_NAME CONFIG_BLUETOOTH_DEVICE_NAME
|
||||
#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)
|
||||
#define CSC_APPEARANCE 0x0485
|
||||
#define CSC_SUPPORTED_LOCATIONS { CSC_LOC_OTHER, \
|
||||
CSC_LOC_FRONT_WHEEL, \
|
||||
CSC_LOC_REAR_WHEEL, \
|
||||
|
@ -385,7 +383,6 @@ static void bt_ready(int err)
|
|||
|
||||
printk("Bluetooth initialized\n");
|
||||
|
||||
gap_init(DEVICE_NAME, CSC_APPEARANCE);
|
||||
bas_init();
|
||||
dis_init(CONFIG_SOC, "ACME");
|
||||
bt_gatt_register(csc_attrs, ARRAY_SIZE(csc_attrs));
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
ccflags-y +=-I${ZEPHYR_BASE}/samples/bluetooth
|
||||
|
||||
obj-y = main.o ../../gatt/gap.o ../../gatt/dis.o
|
||||
obj-y = main.o ../../gatt/dis.o
|
||||
|
|
|
@ -20,12 +20,10 @@
|
|||
#include <bluetooth/uuid.h>
|
||||
#include <bluetooth/gatt.h>
|
||||
|
||||
#include <gatt/gap.h>
|
||||
#include <gatt/dis.h>
|
||||
|
||||
#define DEVICE_NAME CONFIG_BLUETOOTH_DEVICE_NAME
|
||||
#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)
|
||||
#define APPEARANCE 0x0000
|
||||
|
||||
static const struct bt_data ad[] = {
|
||||
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
|
||||
|
@ -67,7 +65,6 @@ void main(void)
|
|||
|
||||
printk("Bluetooth initialized\n");
|
||||
|
||||
gap_init(DEVICE_NAME, APPEARANCE);
|
||||
dis_init(CONFIG_SOC, "Manufacturer");
|
||||
|
||||
bt_conn_cb_register(&conn_callbacks);
|
||||
|
|
|
@ -3,3 +3,4 @@ CONFIG_BLUETOOTH_PERIPHERAL=y
|
|||
CONFIG_BLUETOOTH_SMP=y
|
||||
CONFIG_TINYCRYPT=y
|
||||
CONFIG_BLUETOOTH_DEVICE_NAME="ESP peripheral"
|
||||
CONFIG_BLUETOOTH_DEVICE_APPEARANCE=768
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
ccflags-y +=-I${ZEPHYR_BASE}/samples/bluetooth
|
||||
|
||||
obj-y = main.o ../../gatt/gap.o ../../gatt/dis.o ../../gatt/bas.o
|
||||
obj-y = main.o ../../gatt/dis.o ../../gatt/bas.o
|
||||
|
|
|
@ -21,7 +21,6 @@
|
|||
#include <bluetooth/uuid.h>
|
||||
#include <bluetooth/gatt.h>
|
||||
|
||||
#include <gatt/gap.h>
|
||||
#include <gatt/dis.h>
|
||||
#include <gatt/bas.h>
|
||||
|
||||
|
@ -30,7 +29,6 @@
|
|||
#define SENSOR_1_NAME "Temperature Sensor 1"
|
||||
#define SENSOR_2_NAME "Temperature Sensor 2"
|
||||
#define SENSOR_3_NAME "Humidity Sensor"
|
||||
#define APPEARANCE_THERMOMETER 0x0300
|
||||
|
||||
/* Sensor Internal Update Interval [seconds] */
|
||||
#define SENSOR_1_UPDATE_IVAL 5
|
||||
|
@ -396,7 +394,6 @@ static void bt_ready(int err)
|
|||
|
||||
printk("Bluetooth initialized\n");
|
||||
|
||||
gap_init(DEVICE_NAME, APPEARANCE_THERMOMETER);
|
||||
bt_gatt_register(ess_attrs, ARRAY_SIZE(ess_attrs));
|
||||
bas_init();
|
||||
dis_init(CONFIG_SOC, "ACME");
|
||||
|
|
|
@ -3,3 +3,4 @@ CONFIG_BLUETOOTH_DEBUG_LOG=y
|
|||
CONFIG_BLUETOOTH_SMP=y
|
||||
CONFIG_BLUETOOTH_PERIPHERAL=y
|
||||
CONFIG_BLUETOOTH_DEVICE_NAME="Test HoG mouse"
|
||||
CONFIG_BLUETOOTH_DEVICE_APPEARANCE=962
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
ccflags-y +=-I${ZEPHYR_BASE}/samples/bluetooth
|
||||
|
||||
obj-y = main.o ../../gatt/gap.o ../../gatt/dis.o ../../gatt/bas.o \
|
||||
obj-y = main.o ../../gatt/dis.o ../../gatt/bas.o \
|
||||
../../gatt/hog.o
|
||||
|
|
|
@ -20,14 +20,12 @@
|
|||
#include <bluetooth/uuid.h>
|
||||
#include <bluetooth/gatt.h>
|
||||
|
||||
#include <gatt/gap.h>
|
||||
#include <gatt/dis.h>
|
||||
#include <gatt/bas.h>
|
||||
#include <gatt/hog.h>
|
||||
|
||||
#define DEVICE_NAME CONFIG_BLUETOOTH_DEVICE_NAME
|
||||
#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)
|
||||
#define HOG_APPEARANCE 0x03c2
|
||||
|
||||
static const struct bt_data ad[] = {
|
||||
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
|
||||
|
@ -91,7 +89,6 @@ static void bt_ready(int err)
|
|||
|
||||
printk("Bluetooth initialized\n");
|
||||
|
||||
gap_init(DEVICE_NAME, HOG_APPEARANCE);
|
||||
bas_init();
|
||||
dis_init(CONFIG_SOC, "Manufacturer");
|
||||
hog_init();
|
||||
|
|
|
@ -3,3 +3,4 @@ CONFIG_BLUETOOTH_DEBUG_LOG=y
|
|||
CONFIG_BLUETOOTH_SMP=y
|
||||
CONFIG_BLUETOOTH_PERIPHERAL=y
|
||||
CONFIG_BLUETOOTH_DEVICE_NAME="Zephyr Heartrate Sensor"
|
||||
CONFIG_BLUETOOTH_DEVICE_APPEARANCE=833
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
ccflags-y +=-I${ZEPHYR_BASE}/samples/bluetooth
|
||||
|
||||
obj-y = main.o ../../gatt/gap.o ../../gatt/hrs.o ../../gatt/dis.o \
|
||||
obj-y = main.o ../../gatt/hrs.o ../../gatt/dis.o \
|
||||
../../gatt/bas.o
|
||||
|
|
|
@ -20,14 +20,12 @@
|
|||
#include <bluetooth/uuid.h>
|
||||
#include <bluetooth/gatt.h>
|
||||
|
||||
#include <gatt/gap.h>
|
||||
#include <gatt/hrs.h>
|
||||
#include <gatt/dis.h>
|
||||
#include <gatt/bas.h>
|
||||
|
||||
#define DEVICE_NAME CONFIG_BLUETOOTH_DEVICE_NAME
|
||||
#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)
|
||||
#define HEART_RATE_APPEARANCE 0x0341
|
||||
|
||||
struct bt_conn *default_conn;
|
||||
|
||||
|
@ -74,7 +72,6 @@ static void bt_ready(int err)
|
|||
|
||||
printk("Bluetooth initialized\n");
|
||||
|
||||
gap_init(DEVICE_NAME, HEART_RATE_APPEARANCE);
|
||||
hrs_init(0x01);
|
||||
bas_init();
|
||||
dis_init(CONFIG_SOC, "Manufacturer");
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
ccflags-y +=-I${ZEPHYR_BASE}/samples/bluetooth
|
||||
|
||||
obj-y = main.o ../../gatt/gap.o
|
||||
obj-y = main.o
|
||||
|
|
|
@ -20,11 +20,9 @@
|
|||
#include <bluetooth/uuid.h>
|
||||
#include <bluetooth/gatt.h>
|
||||
|
||||
#include <gatt/gap.h>
|
||||
|
||||
#define DEVICE_NAME CONFIG_BLUETOOTH_DEVICE_NAME
|
||||
#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)
|
||||
#define APPEARANCE 0x0000
|
||||
|
||||
static const struct bt_data ad[] = {
|
||||
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
|
||||
|
@ -125,7 +123,6 @@ void main(void)
|
|||
|
||||
printk("Bluetooth initialized\n");
|
||||
|
||||
gap_init(DEVICE_NAME, APPEARANCE);
|
||||
|
||||
bt_conn_auth_cb_register(&auth_cb_display);
|
||||
bt_conn_cb_register(&conn_callbacks);
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
ccflags-y +=-I${ZEPHYR_BASE}/samples/bluetooth
|
||||
|
||||
obj-y = main.o ble.o ../../../../bluetooth/gatt/gap.o
|
||||
obj-y = main.o ble.o
|
||||
|
|
|
@ -18,7 +18,6 @@
|
|||
#include <bluetooth/conn.h>
|
||||
#include <bluetooth/gatt.h>
|
||||
|
||||
#include <gatt/gap.h>
|
||||
|
||||
#include "pong.h"
|
||||
|
||||
|
@ -540,7 +539,6 @@ void ble_init(void)
|
|||
|
||||
bt_conn_cb_register(&conn_callbacks);
|
||||
|
||||
gap_init(DEVICE_NAME, APPEARANCE);
|
||||
|
||||
local_attr = &pong_attrs[2];
|
||||
bt_gatt_register(pong_attrs, ARRAY_SIZE(pong_attrs));
|
||||
|
|
|
@ -2132,6 +2132,8 @@ void bt_att_init(void)
|
|||
};
|
||||
|
||||
bt_l2cap_le_fixed_chan_register(&chan);
|
||||
|
||||
bt_gatt_init();
|
||||
}
|
||||
|
||||
u16_t bt_att_get_mtu(struct bt_conn *conn)
|
||||
|
|
|
@ -35,17 +35,44 @@
|
|||
static sys_slist_t subscriptions;
|
||||
#endif /* CONFIG_BLUETOOTH_GATT_CLIENT */
|
||||
|
||||
static const char *gap_name = CONFIG_BLUETOOTH_DEVICE_NAME;
|
||||
static const u16_t gap_appearance = CONFIG_BLUETOOTH_DEVICE_APPEARANCE;
|
||||
|
||||
static sys_slist_t db;
|
||||
|
||||
int bt_gatt_register(struct bt_gatt_attr *attrs, size_t count)
|
||||
static ssize_t read_name(struct bt_conn *conn, const struct bt_gatt_attr *attr,
|
||||
void *buf, u16_t len, u16_t offset)
|
||||
{
|
||||
return bt_gatt_attr_read(conn, attr, buf, len, offset, gap_name,
|
||||
strlen(gap_name));
|
||||
}
|
||||
|
||||
static ssize_t read_appearance(struct bt_conn *conn,
|
||||
const struct bt_gatt_attr *attr, void *buf,
|
||||
u16_t len, u16_t offset)
|
||||
{
|
||||
u16_t appearance = sys_cpu_to_le16(gap_appearance);
|
||||
|
||||
return bt_gatt_attr_read(conn, attr, buf, len, offset, &appearance,
|
||||
sizeof(appearance));
|
||||
}
|
||||
|
||||
static struct bt_gatt_attr gap_attrs[] = {
|
||||
BT_GATT_PRIMARY_SERVICE(BT_UUID_GAP),
|
||||
BT_GATT_CHARACTERISTIC(BT_UUID_GAP_DEVICE_NAME, BT_GATT_CHRC_READ),
|
||||
BT_GATT_DESCRIPTOR(BT_UUID_GAP_DEVICE_NAME, BT_GATT_PERM_READ,
|
||||
read_name, NULL, NULL),
|
||||
BT_GATT_CHARACTERISTIC(BT_UUID_GAP_APPEARANCE, BT_GATT_CHRC_READ),
|
||||
BT_GATT_DESCRIPTOR(BT_UUID_GAP_APPEARANCE, BT_GATT_PERM_READ,
|
||||
read_appearance, NULL, NULL),
|
||||
};
|
||||
|
||||
static int gatt_register(struct bt_gatt_attr *attrs, size_t count)
|
||||
{
|
||||
sys_slist_t list;
|
||||
struct bt_gatt_attr *last;
|
||||
u16_t handle;
|
||||
|
||||
__ASSERT(attrs, "invalid parameters\n");
|
||||
__ASSERT(count, "invalid parameters\n");
|
||||
|
||||
sys_slist_init(&list);
|
||||
|
||||
if (sys_slist_is_empty(&db)) {
|
||||
|
@ -85,6 +112,24 @@ populate:
|
|||
return 0;
|
||||
}
|
||||
|
||||
void bt_gatt_init(void)
|
||||
{
|
||||
gatt_register(gap_attrs, ARRAY_SIZE(gap_attrs));
|
||||
}
|
||||
|
||||
int bt_gatt_register(struct bt_gatt_attr *attrs, size_t count)
|
||||
{
|
||||
__ASSERT(attrs, "invalid parameters\n");
|
||||
__ASSERT(count, "invalid parameters\n");
|
||||
|
||||
/* Do no allow to register mandatory services twice */
|
||||
if (!bt_uuid_cmp(attrs->uuid, BT_UUID_GAP)) {
|
||||
return -EALREADY;
|
||||
}
|
||||
|
||||
return gatt_register(attrs, count);
|
||||
}
|
||||
|
||||
ssize_t bt_gatt_attr_read(struct bt_conn *conn, const struct bt_gatt_attr *attr,
|
||||
void *buf, u16_t buf_len, u16_t offset,
|
||||
const void *value, u16_t value_len)
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
void bt_gatt_init(void);
|
||||
void bt_gatt_connected(struct bt_conn *conn);
|
||||
void bt_gatt_disconnected(struct bt_conn *conn);
|
||||
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
ccflags-y +=-I$(ZEPHYR_BASE)/include/drivers
|
||||
ccflags-y +=-I$(ZEPHYR_BASE)/samples/bluetooth
|
||||
|
||||
obj-y = main.o ../../../../samples/bluetooth/gatt/gap.o \
|
||||
../../../../samples/bluetooth/gatt/hrs.o
|
||||
obj-y = main.o ../../../../samples/bluetooth/gatt/hrs.o
|
||||
|
|
|
@ -23,14 +23,12 @@
|
|||
|
||||
#include <shell/shell.h>
|
||||
|
||||
#include <gatt/gap.h>
|
||||
#include <gatt/hrs.h>
|
||||
|
||||
#define DEVICE_NAME CONFIG_BLUETOOTH_DEVICE_NAME
|
||||
|
||||
#define MY_SHELL_MODULE "btshell"
|
||||
|
||||
static u16_t appearance_value = 0x0001;
|
||||
static bool hrs_simulate;
|
||||
|
||||
static int cmd_hrs_simulate(int argc, char *argv[])
|
||||
|
@ -44,7 +42,6 @@ static int cmd_hrs_simulate(int argc, char *argv[])
|
|||
|
||||
if (!hrs_registered) {
|
||||
printk("Registering HRS Service\n");
|
||||
gap_init(DEVICE_NAME, appearance_value);
|
||||
hrs_init(0x01);
|
||||
hrs_registered = true;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue