diff --git a/samples/bluetooth/gatt/gap.c b/samples/bluetooth/gatt/gap.c deleted file mode 100644 index 780cbf12a82..00000000000 --- a/samples/bluetooth/gatt/gap.c +++ /dev/null @@ -1,61 +0,0 @@ -/** @file - * @brief GAP Service sample - */ - -/* - * Copyright (c) 2016 Intel Corporation - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -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)); -} diff --git a/samples/bluetooth/gatt/gap.h b/samples/bluetooth/gatt/gap.h deleted file mode 100644 index 92aec4801d5..00000000000 --- a/samples/bluetooth/gatt/gap.h +++ /dev/null @@ -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 diff --git a/samples/bluetooth/peripheral/prj.conf b/samples/bluetooth/peripheral/prj.conf index a76d3f51be2..bcb34a4cf65 100644 --- a/samples/bluetooth/peripheral/prj.conf +++ b/samples/bluetooth/peripheral/prj.conf @@ -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 diff --git a/samples/bluetooth/peripheral/src/Makefile b/samples/bluetooth/peripheral/src/Makefile index 95263c805ce..c1424cb3547 100644 --- a/samples/bluetooth/peripheral/src/Makefile +++ b/samples/bluetooth/peripheral/src/Makefile @@ -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 diff --git a/samples/bluetooth/peripheral/src/main.c b/samples/bluetooth/peripheral/src/main.c index 59bfc256429..894315d663e 100644 --- a/samples/bluetooth/peripheral/src/main.c +++ b/samples/bluetooth/peripheral/src/main.c @@ -20,7 +20,6 @@ #include #include -#include #include #include #include @@ -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(); diff --git a/samples/bluetooth/peripheral_csc/prj.conf b/samples/bluetooth/peripheral_csc/prj.conf index 375a2206603..9e1d821bcd6 100644 --- a/samples/bluetooth/peripheral_csc/prj.conf +++ b/samples/bluetooth/peripheral_csc/prj.conf @@ -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 diff --git a/samples/bluetooth/peripheral_csc/src/Makefile b/samples/bluetooth/peripheral_csc/src/Makefile index 1dec2c56d42..e31428cd905 100644 --- a/samples/bluetooth/peripheral_csc/src/Makefile +++ b/samples/bluetooth/peripheral_csc/src/Makefile @@ -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 diff --git a/samples/bluetooth/peripheral_csc/src/main.c b/samples/bluetooth/peripheral_csc/src/main.c index 5a8de7cd79f..b56a1598d58 100644 --- a/samples/bluetooth/peripheral_csc/src/main.c +++ b/samples/bluetooth/peripheral_csc/src/main.c @@ -21,13 +21,11 @@ #include #include -#include #include #include #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)); diff --git a/samples/bluetooth/peripheral_dis/src/Makefile b/samples/bluetooth/peripheral_dis/src/Makefile index 3adbf1b1801..a132f5ec828 100644 --- a/samples/bluetooth/peripheral_dis/src/Makefile +++ b/samples/bluetooth/peripheral_dis/src/Makefile @@ -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 diff --git a/samples/bluetooth/peripheral_dis/src/main.c b/samples/bluetooth/peripheral_dis/src/main.c index 50220366280..92217affa2c 100644 --- a/samples/bluetooth/peripheral_dis/src/main.c +++ b/samples/bluetooth/peripheral_dis/src/main.c @@ -20,12 +20,10 @@ #include #include -#include #include #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); diff --git a/samples/bluetooth/peripheral_esp/prj.conf b/samples/bluetooth/peripheral_esp/prj.conf index 0720078e62a..796c7c33886 100644 --- a/samples/bluetooth/peripheral_esp/prj.conf +++ b/samples/bluetooth/peripheral_esp/prj.conf @@ -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 diff --git a/samples/bluetooth/peripheral_esp/src/Makefile b/samples/bluetooth/peripheral_esp/src/Makefile index 1dec2c56d42..e31428cd905 100644 --- a/samples/bluetooth/peripheral_esp/src/Makefile +++ b/samples/bluetooth/peripheral_esp/src/Makefile @@ -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 diff --git a/samples/bluetooth/peripheral_esp/src/main.c b/samples/bluetooth/peripheral_esp/src/main.c index 093e8b62c0e..4144724c090 100644 --- a/samples/bluetooth/peripheral_esp/src/main.c +++ b/samples/bluetooth/peripheral_esp/src/main.c @@ -21,7 +21,6 @@ #include #include -#include #include #include @@ -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"); diff --git a/samples/bluetooth/peripheral_hids/prj.conf b/samples/bluetooth/peripheral_hids/prj.conf index 1ef61bc9409..4869d7a2258 100644 --- a/samples/bluetooth/peripheral_hids/prj.conf +++ b/samples/bluetooth/peripheral_hids/prj.conf @@ -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 diff --git a/samples/bluetooth/peripheral_hids/src/Makefile b/samples/bluetooth/peripheral_hids/src/Makefile index 16f30525880..88fe2681176 100644 --- a/samples/bluetooth/peripheral_hids/src/Makefile +++ b/samples/bluetooth/peripheral_hids/src/Makefile @@ -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 diff --git a/samples/bluetooth/peripheral_hids/src/main.c b/samples/bluetooth/peripheral_hids/src/main.c index 5bada4aa374..dcb025f02ab 100644 --- a/samples/bluetooth/peripheral_hids/src/main.c +++ b/samples/bluetooth/peripheral_hids/src/main.c @@ -20,14 +20,12 @@ #include #include -#include #include #include #include #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(); diff --git a/samples/bluetooth/peripheral_hr/prj.conf b/samples/bluetooth/peripheral_hr/prj.conf index a857641eae5..454329ea725 100644 --- a/samples/bluetooth/peripheral_hr/prj.conf +++ b/samples/bluetooth/peripheral_hr/prj.conf @@ -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 diff --git a/samples/bluetooth/peripheral_hr/src/Makefile b/samples/bluetooth/peripheral_hr/src/Makefile index 47c6f4feb43..b510b4364ca 100644 --- a/samples/bluetooth/peripheral_hr/src/Makefile +++ b/samples/bluetooth/peripheral_hr/src/Makefile @@ -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 diff --git a/samples/bluetooth/peripheral_hr/src/main.c b/samples/bluetooth/peripheral_hr/src/main.c index 595b1e71d17..63e6a2fb431 100644 --- a/samples/bluetooth/peripheral_hr/src/main.c +++ b/samples/bluetooth/peripheral_hr/src/main.c @@ -20,14 +20,12 @@ #include #include -#include #include #include #include #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"); diff --git a/samples/bluetooth/peripheral_sc_only/src/Makefile b/samples/bluetooth/peripheral_sc_only/src/Makefile index 54390d2a2fa..2e79ed45efd 100644 --- a/samples/bluetooth/peripheral_sc_only/src/Makefile +++ b/samples/bluetooth/peripheral_sc_only/src/Makefile @@ -1,3 +1,3 @@ ccflags-y +=-I${ZEPHYR_BASE}/samples/bluetooth -obj-y = main.o ../../gatt/gap.o +obj-y = main.o diff --git a/samples/bluetooth/peripheral_sc_only/src/main.c b/samples/bluetooth/peripheral_sc_only/src/main.c index a6661e9fc47..dd4aea03d7c 100644 --- a/samples/bluetooth/peripheral_sc_only/src/main.c +++ b/samples/bluetooth/peripheral_sc_only/src/main.c @@ -20,11 +20,9 @@ #include #include -#include #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); diff --git a/samples/boards/microbit/pong/src/Makefile b/samples/boards/microbit/pong/src/Makefile index 1ec06befff5..08d10d3c5c5 100644 --- a/samples/boards/microbit/pong/src/Makefile +++ b/samples/boards/microbit/pong/src/Makefile @@ -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 diff --git a/samples/boards/microbit/pong/src/ble.c b/samples/boards/microbit/pong/src/ble.c index aa4475d874d..bf31723bdbf 100644 --- a/samples/boards/microbit/pong/src/ble.c +++ b/samples/boards/microbit/pong/src/ble.c @@ -18,7 +18,6 @@ #include #include -#include #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)); diff --git a/subsys/bluetooth/host/att.c b/subsys/bluetooth/host/att.c index 04b9e531128..7bf96870e1e 100644 --- a/subsys/bluetooth/host/att.c +++ b/subsys/bluetooth/host/att.c @@ -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) diff --git a/subsys/bluetooth/host/gatt.c b/subsys/bluetooth/host/gatt.c index 471051ac0da..8cf6e924f11 100644 --- a/subsys/bluetooth/host/gatt.c +++ b/subsys/bluetooth/host/gatt.c @@ -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) diff --git a/subsys/bluetooth/host/gatt_internal.h b/subsys/bluetooth/host/gatt_internal.h index 14842fdc016..968762c5efd 100644 --- a/subsys/bluetooth/host/gatt_internal.h +++ b/subsys/bluetooth/host/gatt_internal.h @@ -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); diff --git a/tests/bluetooth/shell/src/Makefile b/tests/bluetooth/shell/src/Makefile index 367e98bee08..fbb846248f7 100644 --- a/tests/bluetooth/shell/src/Makefile +++ b/tests/bluetooth/shell/src/Makefile @@ -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 diff --git a/tests/bluetooth/shell/src/main.c b/tests/bluetooth/shell/src/main.c index 8933d4cb8cf..f7d16268869 100644 --- a/tests/bluetooth/shell/src/main.c +++ b/tests/bluetooth/shell/src/main.c @@ -23,14 +23,12 @@ #include -#include #include #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; }