From e3f30be05d16a4c5b2005d945b112a83fe570e8c Mon Sep 17 00:00:00 2001 From: Luiz Augusto von Dentz Date: Fri, 7 Sep 2018 12:44:40 +0300 Subject: [PATCH] Bluetooth: GATT: Auto init if bt_gatt_service_register is called This ensures the core services are always registered first and the gatt_sc work is initialized. Fixes #9785 Signed-off-by: Luiz Augusto von Dentz Signed-off-by: Anas Nashif --- subsys/bluetooth/host/gatt.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/subsys/bluetooth/host/gatt.c b/subsys/bluetooth/host/gatt.c index 75f3710c4c949c..6b057dc03e932b 100644 --- a/subsys/bluetooth/host/gatt.c +++ b/subsys/bluetooth/host/gatt.c @@ -50,6 +50,7 @@ static sys_slist_t subscriptions; static const u16_t gap_appearance = CONFIG_BT_DEVICE_APPEARANCE; static sys_slist_t db; +static atomic_t init; static ssize_t read_name(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, u16_t len, u16_t offset) @@ -237,6 +238,10 @@ static void sc_process(struct k_work *work) void bt_gatt_init(void) { + if (!atomic_cas(&init, 0, 1)) { + return; + } + /* Register mandatory services */ gatt_register(&gap_svc); gatt_register(&gatt_svc); @@ -297,6 +302,9 @@ int bt_gatt_service_register(struct bt_gatt_service *svc) __ASSERT(svc->attrs, "invalid parameters\n"); __ASSERT(svc->attr_count, "invalid parameters\n"); + /* Init GATT core services */ + bt_gatt_init(); + /* Do no allow to register mandatory services twice */ if (!bt_uuid_cmp(svc->attrs[0].uuid, BT_UUID_GAP) || !bt_uuid_cmp(svc->attrs[0].uuid, BT_UUID_GATT)) {