Skip to content

Commit

Permalink
Bluetooth: services: Move Heart rate service
Browse files Browse the repository at this point in the history
This commit moves the BLE GATT heart rate service from
samples/bluetooth/gatt to subsys/bluetooth/services and adds a Kconfig
entry to enable and configure the service.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
  • Loading branch information
joerchan authored and jhedberg committed Jul 11, 2019
1 parent 30d6580 commit 029a66a
Show file tree
Hide file tree
Showing 23 changed files with 171 additions and 65 deletions.
42 changes: 42 additions & 0 deletions include/bluetooth/services/hrs.h
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_HRS_H_
#define ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_HRS_H_

/**
* @brief Heart Rate Service (HRS)
* @defgroup bt_gatt_hrs Heart Rate Service (HRS)
* @ingroup bluetooth
* @{
*
* [Experimental] Users should note that the APIs can change
* as a part of ongoing development.
*/

#ifdef __cplusplus
extern "C" {
#endif

/** @brief Notify heart rate measurement.
*
* This will send a GATT notification to all current subscribers.
*
* @param heartrate The heartrate measuremennt in beats per minute.
*
* @return Zero in case of success and error code in case of error.
*/
int bt_gatt_hrs_notify(u16_t heartrate);

#ifdef __cplusplus
}
#endif

/**
* @}
*/

#endif /* ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_HRS_H_ */
20 changes: 0 additions & 20 deletions samples/bluetooth/gatt/hrs.h

This file was deleted.

1 change: 0 additions & 1 deletion samples/bluetooth/peripheral/CMakeLists.txt
Expand Up @@ -6,7 +6,6 @@ project(peripheral)

target_sources(app PRIVATE
src/main.c
../gatt/hrs.c
../gatt/cts.c
)

Expand Down
1 change: 1 addition & 0 deletions samples/bluetooth/peripheral/prj.conf
Expand Up @@ -9,6 +9,7 @@ CONFIG_BT_PERIPHERAL=y
CONFIG_BT_GATT_DIS=y
CONFIG_BT_ATT_PREPARE_COUNT=5
CONFIG_BT_GATT_BAS=y
CONFIG_BT_GATT_HRS=y
CONFIG_BT_PRIVACY=y
CONFIG_BT_DEVICE_NAME="Zephyr Peripheral Sample Long Name"
CONFIG_BT_DEVICE_APPEARANCE=833
Expand Down
16 changes: 14 additions & 2 deletions samples/bluetooth/peripheral/src/main.c
Expand Up @@ -22,8 +22,8 @@
#include <bluetooth/uuid.h>
#include <bluetooth/gatt.h>
#include <bluetooth/services/bas.h>
#include <bluetooth/services/hrs.h>

#include <gatt/hrs.h>
#include <gatt/cts.h>

/* Custom Service Variables */
Expand Down Expand Up @@ -254,7 +254,6 @@ static void bt_ready(int err)

printk("Bluetooth initialized\n");

hrs_init(0x01);
cts_init();

if (IS_ENABLED(CONFIG_SETTINGS)) {
Expand Down Expand Up @@ -307,6 +306,19 @@ static void bas_notify(void)
bt_gatt_bas_set_battery_level(battery_level);
}

static void hrs_notify(void)
{
static u8_t heartrate = 90U;

/* Heartrate measurements simulation */
heartrate++;
if (heartrate == 160U) {
heartrate = 90U;
}

bt_gatt_hrs_notify(heartrate);
}

void main(void)
{
int err;
Expand Down
1 change: 0 additions & 1 deletion samples/bluetooth/peripheral_hr/CMakeLists.txt
Expand Up @@ -8,7 +8,6 @@ project(peripheral_hr)
FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE
${app_sources}
../gatt/hrs.c
)

zephyr_library_include_directories($ENV{ZEPHYR_BASE}/samples/bluetooth)
1 change: 1 addition & 0 deletions samples/bluetooth/peripheral_hr/prj.conf
Expand Up @@ -5,5 +5,6 @@ CONFIG_BT_PERIPHERAL=y
CONFIG_BT_GATT_DIS=y
CONFIG_BT_GATT_DIS_PNP=n
CONFIG_BT_GATT_BAS=y
CONFIG_BT_GATT_HRS=y
CONFIG_BT_DEVICE_NAME="Zephyr Heartrate Sensor"
CONFIG_BT_DEVICE_APPEARANCE=833
18 changes: 14 additions & 4 deletions samples/bluetooth/peripheral_hr/src/main.c
Expand Up @@ -20,8 +20,7 @@
#include <bluetooth/uuid.h>
#include <bluetooth/gatt.h>
#include <bluetooth/services/bas.h>

#include <gatt/hrs.h>
#include <bluetooth/services/hrs.h>

struct bt_conn *default_conn;

Expand Down Expand Up @@ -64,8 +63,6 @@ static void bt_ready(int err)

printk("Bluetooth initialized\n");

hrs_init(0x01);

err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0);
if (err) {
printk("Advertising failed to start (err %d)\n", err);
Expand Down Expand Up @@ -101,6 +98,19 @@ static void bas_notify(void)
bt_gatt_bas_set_battery_level(battery_level);
}

static void hrs_notify(void)
{
static u8_t heartrate = 90U;

/* Heartrate measurements simulation */
heartrate++;
if (heartrate == 160U) {
heartrate = 90U;
}

bt_gatt_hrs_notify(heartrate);
}

void main(void)
{
int err;
Expand Down
2 changes: 2 additions & 0 deletions subsys/bluetooth/services/CMakeLists.txt
Expand Up @@ -4,3 +4,5 @@
zephyr_sources_ifdef(CONFIG_BT_GATT_DIS dis.c)

zephyr_sources_ifdef(CONFIG_BT_GATT_BAS bas.c)

zephyr_sources_ifdef(CONFIG_BT_GATT_HRS hrs.c)
2 changes: 2 additions & 0 deletions subsys/bluetooth/services/Kconfig
Expand Up @@ -13,6 +13,8 @@ source "subsys/bluetooth/services/Kconfig.dis"

source "subsys/bluetooth/services/Kconfig.bas"

source "subsys/bluetooth/services/Kconfig.hrs"

endmenu

endif #BT_CONN
28 changes: 28 additions & 0 deletions subsys/bluetooth/services/Kconfig.hrs
@@ -0,0 +1,28 @@
# Kconfig - Bluetooth GATT Heart Rate service
#
# Copyright (c) 2018 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: Apache-2.0
#

menuconfig BT_GATT_HRS
bool "Enable GATT Heart Rate service"
default n

if BT_GATT_HRS

config BT_GATT_HRS_LOG_LEVEL
int "Heart Rate service log level"
depends on LOG
range 0 4
default 0
help
Sets log level for the Heart Rate service.
Levels are:
0 OFF, do not write
1 ERROR, only write LOG_ERR
2 WARNING, write LOG_WRN in addition to previous level
3 INFO, write LOG_INF in addition to previous levels
4 DEBUG, write LOG_DBG in addition to previous levels

endif #BT_GATT_BAS
45 changes: 24 additions & 21 deletions samples/bluetooth/gatt/hrs.c → subsys/bluetooth/services/hrs.c
Expand Up @@ -12,25 +12,29 @@
#include <stddef.h>
#include <string.h>
#include <errno.h>
#include <sys/printk.h>
#include <sys/byteorder.h>
#include <zephyr.h>
#include <init.h>

#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/conn.h>
#include <bluetooth/uuid.h>
#include <bluetooth/gatt.h>

#define LOG_LEVEL CONFIG_BT_GATT_HRS_LOG_LEVEL
#include <logging/log.h>
LOG_MODULE_REGISTER(hrs);

static struct bt_gatt_ccc_cfg hrmc_ccc_cfg[BT_GATT_CCC_MAX] = {};
static u8_t simulate_hrm;
static u8_t heartrate = 90U;
static u8_t hrs_blsc;

static void hrmc_ccc_cfg_changed(const struct bt_gatt_attr *attr,
u16_t value)
static void hrmc_ccc_cfg_changed(const struct bt_gatt_attr *attr, u16_t value)
{
simulate_hrm = (value == BT_GATT_CCC_NOTIFY) ? 1 : 0;
ARG_UNUSED(attr);

bool notif_enabled = (value == BT_GATT_CCC_NOTIFY);

LOG_INF("HRS notifications %s", notif_enabled ? "enabled" : "disabled");
}

static ssize_t read_blsc(struct bt_conn *conn, const struct bt_gatt_attr *attr,
Expand All @@ -52,27 +56,26 @@ BT_GATT_SERVICE_DEFINE(hrs_svc,
BT_GATT_PERM_NONE, NULL, NULL, NULL),
);

void hrs_init(u8_t blsc)
static int hrs_init(struct device *dev)
{
hrs_blsc = blsc;
ARG_UNUSED(dev);

hrs_blsc = 0x01;

return 0;
}

void hrs_notify(void)
int bt_gatt_hrs_notify(u16_t heartrate)
{
int rc;
static u8_t hrm[2];

/* Heartrate measurements simulation */
if (!simulate_hrm) {
return;
}

heartrate++;
if (heartrate == 160U) {
heartrate = 90U;
}

hrm[0] = 0x06; /* uint8, sensor contact */
hrm[1] = heartrate;

bt_gatt_notify(NULL, &hrs_svc.attrs[1], &hrm, sizeof(hrm));
rc = bt_gatt_notify(NULL, &hrs_svc.attrs[1], &hrm, sizeof(hrm));

return rc == -ENOTCONN ? 0 : rc;
}

SYS_INIT(hrs_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);
1 change: 0 additions & 1 deletion tests/bluetooth/bsim_bt/bsim_test_app/CMakeLists.txt
Expand Up @@ -17,7 +17,6 @@ target_sources(app PRIVATE
src/test_empty.c
src/test_connect1.c
src/test_connect2.c
../../../../samples/bluetooth/gatt/hrs.c
)

zephyr_include_directories(
Expand Down
1 change: 1 addition & 0 deletions tests/bluetooth/bsim_bt/bsim_test_app/prj.conf
Expand Up @@ -6,6 +6,7 @@ CONFIG_BT_PRIVACY=y
CONFIG_BT_SMP=y
CONFIG_BT_SIGNING=y
CONFIG_BT_GATT_BAS=y
CONFIG_BT_GATT_HRS=y
CONFIG_BT_ATT_PREPARE_COUNT=2
CONFIG_BT_GATT_CLIENT=y
CONFIG_BT_L2CAP_DYNAMIC_CHANNEL=y
Expand Down
1 change: 1 addition & 0 deletions tests/bluetooth/bsim_bt/bsim_test_app/prj_split.conf
Expand Up @@ -6,6 +6,7 @@ CONFIG_BT_PRIVACY=y
CONFIG_BT_SMP=y
CONFIG_BT_SIGNING=y
CONFIG_BT_GATT_BAS=y
CONFIG_BT_GATT_HRS=y
CONFIG_BT_ATT_PREPARE_COUNT=2
CONFIG_BT_GATT_CLIENT=y
CONFIG_BT_L2CAP_DYNAMIC_CHANNEL=y
Expand Down
19 changes: 15 additions & 4 deletions tests/bluetooth/bsim_bt/bsim_test_app/src/test_connect2.c
Expand Up @@ -23,9 +23,9 @@
#include <bluetooth/uuid.h>
#include <bluetooth/gatt.h>
#include <bluetooth/services/bas.h>
#include <sys/byteorder.h>
#include <bluetooth/services/hrs.h>

#include <gatt/hrs.h>
#include <sys/byteorder.h>

static struct bt_conn *default_conn;

Expand Down Expand Up @@ -108,8 +108,6 @@ static void bt_ready(int err)

printk("Bluetooth initialized\n");

hrs_init(0x01);

err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0);
if (err) {
FAIL("Advertising failed to start (err %d)\n", err);
Expand All @@ -132,6 +130,19 @@ static void bas_notify(void)
bt_gatt_bas_set_battery_level(battery_level);
}

static void hrs_notify(void)
{
static u8_t heartrate = 90U;

/* Heartrate measurements simulation */
heartrate++;
if (heartrate == 160U) {
heartrate = 90U;
}

bt_gatt_hrs_notify(heartrate);
}

static void test_con2_main(void)
{
static int notify_count;
Expand Down
1 change: 0 additions & 1 deletion tests/bluetooth/shell/CMakeLists.txt
Expand Up @@ -7,4 +7,3 @@ project(bluetooth_shell)
zephyr_library_include_directories($ENV{ZEPHYR_BASE}/samples/bluetooth)

target_sources(app PRIVATE src/main.c)
target_sources_ifdef(CONFIG_BT_CONN app PRIVATE src/hrs.c)
1 change: 1 addition & 0 deletions tests/bluetooth/shell/mesh.conf
Expand Up @@ -17,6 +17,7 @@ CONFIG_SHELL=y
CONFIG_BT_SHELL=y
CONFIG_BT_DEVICE_NAME="test shell"
CONFIG_BT_L2CAP_TX_BUF_COUNT=6
CONFIG_BT_GATT_HRS=y

CONFIG_BT_L2CAP_RX_MTU=69
CONFIG_BT_L2CAP_TX_MTU=69
Expand Down
1 change: 1 addition & 0 deletions tests/bluetooth/shell/prj.conf
Expand Up @@ -21,6 +21,7 @@ CONFIG_BT_DEVICE_NAME_DYNAMIC=y
CONFIG_BT_L2CAP_TX_BUF_COUNT=4
CONFIG_BT_ID_MAX=2
CONFIG_BT_GATT_DYNAMIC_DB=y
CONFIG_BT_GATT_HRS=y

CONFIG_BT_SETTINGS=y
CONFIG_FLASH=y
Expand Down
1 change: 1 addition & 0 deletions tests/bluetooth/shell/prj_arduino_101.conf
Expand Up @@ -8,6 +8,7 @@ CONFIG_BT_SMP=y
CONFIG_BT_SIGNING=y
CONFIG_BT_ATT_PREPARE_COUNT=2
CONFIG_BT_GATT_CLIENT=y
CONFIG_BT_GATT_HRS=y
CONFIG_BT_L2CAP_DYNAMIC_CHANNEL=y
CONFIG_BT_TINYCRYPT_ECC=y
CONFIG_SHELL=y
Expand Down
1 change: 1 addition & 0 deletions tests/bluetooth/shell/prj_br.conf
Expand Up @@ -12,6 +12,7 @@ CONFIG_BT_SMP=y
CONFIG_BT_SIGNING=y
CONFIG_BT_ATT_PREPARE_COUNT=2
CONFIG_BT_GATT_CLIENT=y
CONFIG_BT_GATT_HRS=y
CONFIG_BT_L2CAP_DYNAMIC_CHANNEL=y
CONFIG_BT_TINYCRYPT_ECC=y
CONFIG_BT_DEVICE_NAME="test shell"
2 changes: 0 additions & 2 deletions tests/bluetooth/shell/src/hrs.c

This file was deleted.

0 comments on commit 029a66a

Please sign in to comment.