Bluetooth: services: battery service enhancements
This commit moves the BLE GATT Battery service from /samples/bluetooth/gatt to /subsys/bluetooth/services and adds a Kconfig entry to enable and configure the service; when enabled, it will register itself automatically. Signed-off-by: Emanuele Di Santo <emdi@nordicsemi.no> Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
This commit is contained in:
parent
6a0fcf1b45
commit
30d65809fc
55
include/bluetooth/services/bas.h
Normal file
55
include/bluetooth/services/bas.h
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2018 Nordic Semiconductor ASA
|
||||||
|
* Copyright (c) 2016 Intel Corporation
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_BAS_H_
|
||||||
|
#define ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_BAS_H_
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Battery Service (BAS)
|
||||||
|
* @defgroup bt_gatt_bas Battery Service (BAS)
|
||||||
|
* @ingroup bluetooth
|
||||||
|
* @{
|
||||||
|
*
|
||||||
|
* [Experimental] Users should note that the APIs can change
|
||||||
|
* as a part of ongoing development.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <zephyr/types.h>
|
||||||
|
|
||||||
|
/** @brief Read battery level value.
|
||||||
|
*
|
||||||
|
* Read the characteristic value of the battery level
|
||||||
|
*
|
||||||
|
* @return The battery level in percent.
|
||||||
|
*/
|
||||||
|
u8_t bt_gatt_bas_get_battery_level(void);
|
||||||
|
|
||||||
|
/** @brief Update battery level value.
|
||||||
|
*
|
||||||
|
* Update the characteristic value of the battery level
|
||||||
|
* This will send a GATT notification to all current subscribers.
|
||||||
|
*
|
||||||
|
* @param level The battery level in percent.
|
||||||
|
*
|
||||||
|
* @return Zero in case of success and error code in case of error.
|
||||||
|
*/
|
||||||
|
int bt_gatt_bas_set_battery_level(u8_t level);
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @}
|
||||||
|
*/
|
||||||
|
|
||||||
|
#endif /* ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_BAS_H_ */
|
|
@ -1,70 +0,0 @@
|
||||||
/** @file
|
|
||||||
* @brief BAS 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 <sys/printk.h>
|
|
||||||
#include <sys/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 struct bt_gatt_ccc_cfg blvl_ccc_cfg[BT_GATT_CCC_MAX] = {};
|
|
||||||
static u8_t simulate_blvl;
|
|
||||||
static u8_t battery = 100U;
|
|
||||||
|
|
||||||
static void blvl_ccc_cfg_changed(const struct bt_gatt_attr *attr,
|
|
||||||
u16_t value)
|
|
||||||
{
|
|
||||||
simulate_blvl = (value == BT_GATT_CCC_NOTIFY) ? 1 : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static ssize_t read_blvl(struct bt_conn *conn, const struct bt_gatt_attr *attr,
|
|
||||||
void *buf, u16_t len, u16_t offset)
|
|
||||||
{
|
|
||||||
const char *value = attr->user_data;
|
|
||||||
|
|
||||||
return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
|
|
||||||
sizeof(*value));
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Battery Service Declaration */
|
|
||||||
BT_GATT_SERVICE_DEFINE(bas_svc,
|
|
||||||
BT_GATT_PRIMARY_SERVICE(BT_UUID_BAS),
|
|
||||||
BT_GATT_CHARACTERISTIC(BT_UUID_BAS_BATTERY_LEVEL,
|
|
||||||
BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY,
|
|
||||||
BT_GATT_PERM_READ, read_blvl, NULL, &battery),
|
|
||||||
BT_GATT_CCC(blvl_ccc_cfg, blvl_ccc_cfg_changed),
|
|
||||||
);
|
|
||||||
|
|
||||||
void bas_init(void)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void bas_notify(void)
|
|
||||||
{
|
|
||||||
if (!simulate_blvl) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
battery--;
|
|
||||||
if (!battery) {
|
|
||||||
/* Software eco battery charger */
|
|
||||||
battery = 100U;
|
|
||||||
}
|
|
||||||
|
|
||||||
bt_gatt_notify(NULL, &bas_svc.attrs[1], &battery, sizeof(battery));
|
|
||||||
}
|
|
|
@ -1,20 +0,0 @@
|
||||||
/** @file
|
|
||||||
* @brief BAS Service sample
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Copyright (c) 2016 Intel Corporation
|
|
||||||
*
|
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void bas_init(void);
|
|
||||||
void bas_notify(void);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
|
@ -7,7 +7,6 @@ project(peripheral)
|
||||||
target_sources(app PRIVATE
|
target_sources(app PRIVATE
|
||||||
src/main.c
|
src/main.c
|
||||||
../gatt/hrs.c
|
../gatt/hrs.c
|
||||||
../gatt/bas.c
|
|
||||||
../gatt/cts.c
|
../gatt/cts.c
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ CONFIG_BT_SIGNING=y
|
||||||
CONFIG_BT_PERIPHERAL=y
|
CONFIG_BT_PERIPHERAL=y
|
||||||
CONFIG_BT_GATT_DIS=y
|
CONFIG_BT_GATT_DIS=y
|
||||||
CONFIG_BT_ATT_PREPARE_COUNT=5
|
CONFIG_BT_ATT_PREPARE_COUNT=5
|
||||||
|
CONFIG_BT_GATT_BAS=y
|
||||||
CONFIG_BT_PRIVACY=y
|
CONFIG_BT_PRIVACY=y
|
||||||
CONFIG_BT_DEVICE_NAME="Zephyr Peripheral Sample Long Name"
|
CONFIG_BT_DEVICE_NAME="Zephyr Peripheral Sample Long Name"
|
||||||
CONFIG_BT_DEVICE_APPEARANCE=833
|
CONFIG_BT_DEVICE_APPEARANCE=833
|
||||||
|
|
|
@ -21,9 +21,9 @@
|
||||||
#include <bluetooth/conn.h>
|
#include <bluetooth/conn.h>
|
||||||
#include <bluetooth/uuid.h>
|
#include <bluetooth/uuid.h>
|
||||||
#include <bluetooth/gatt.h>
|
#include <bluetooth/gatt.h>
|
||||||
|
#include <bluetooth/services/bas.h>
|
||||||
|
|
||||||
#include <gatt/hrs.h>
|
#include <gatt/hrs.h>
|
||||||
#include <gatt/bas.h>
|
|
||||||
#include <gatt/cts.h>
|
#include <gatt/cts.h>
|
||||||
|
|
||||||
/* Custom Service Variables */
|
/* Custom Service Variables */
|
||||||
|
@ -294,6 +294,19 @@ static struct bt_conn_auth_cb auth_cb_display = {
|
||||||
.cancel = auth_cancel,
|
.cancel = auth_cancel,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static void bas_notify(void)
|
||||||
|
{
|
||||||
|
u8_t battery_level = bt_gatt_bas_get_battery_level();
|
||||||
|
|
||||||
|
battery_level--;
|
||||||
|
|
||||||
|
if (!battery_level) {
|
||||||
|
battery_level = 100U;
|
||||||
|
}
|
||||||
|
|
||||||
|
bt_gatt_bas_set_battery_level(battery_level);
|
||||||
|
}
|
||||||
|
|
||||||
void main(void)
|
void main(void)
|
||||||
{
|
{
|
||||||
int err;
|
int err;
|
||||||
|
|
|
@ -7,7 +7,6 @@ project(peripheral_csc)
|
||||||
FILE(GLOB app_sources src/*.c)
|
FILE(GLOB app_sources src/*.c)
|
||||||
target_sources(app PRIVATE
|
target_sources(app PRIVATE
|
||||||
${app_sources}
|
${app_sources}
|
||||||
../gatt/bas.c
|
|
||||||
)
|
)
|
||||||
|
|
||||||
zephyr_library_include_directories($ENV{ZEPHYR_BASE}/samples/bluetooth)
|
zephyr_library_include_directories($ENV{ZEPHYR_BASE}/samples/bluetooth)
|
||||||
|
|
|
@ -4,5 +4,6 @@ CONFIG_BT=y
|
||||||
CONFIG_BT_PERIPHERAL=y
|
CONFIG_BT_PERIPHERAL=y
|
||||||
CONFIG_BT_GATT_DIS=y
|
CONFIG_BT_GATT_DIS=y
|
||||||
CONFIG_BT_GATT_DIS_PNP=n
|
CONFIG_BT_GATT_DIS_PNP=n
|
||||||
|
CONFIG_BT_GATT_BAS=y
|
||||||
CONFIG_BT_DEVICE_NAME="CSC peripheral"
|
CONFIG_BT_DEVICE_NAME="CSC peripheral"
|
||||||
CONFIG_BT_DEVICE_APPEARANCE=1157
|
CONFIG_BT_DEVICE_APPEARANCE=1157
|
||||||
|
|
|
@ -20,8 +20,7 @@
|
||||||
#include <bluetooth/conn.h>
|
#include <bluetooth/conn.h>
|
||||||
#include <bluetooth/uuid.h>
|
#include <bluetooth/uuid.h>
|
||||||
#include <bluetooth/gatt.h>
|
#include <bluetooth/gatt.h>
|
||||||
|
#include <bluetooth/services/bas.h>
|
||||||
#include <gatt/bas.h>
|
|
||||||
|
|
||||||
#define CSC_SUPPORTED_LOCATIONS { CSC_LOC_OTHER, \
|
#define CSC_SUPPORTED_LOCATIONS { CSC_LOC_OTHER, \
|
||||||
CSC_LOC_FRONT_WHEEL, \
|
CSC_LOC_FRONT_WHEEL, \
|
||||||
|
@ -375,8 +374,6 @@ static void bt_ready(int err)
|
||||||
|
|
||||||
printk("Bluetooth initialized\n");
|
printk("Bluetooth initialized\n");
|
||||||
|
|
||||||
bas_init();
|
|
||||||
|
|
||||||
err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0);
|
err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0);
|
||||||
if (err) {
|
if (err) {
|
||||||
printk("Advertising failed to start (err %d)\n", err);
|
printk("Advertising failed to start (err %d)\n", err);
|
||||||
|
@ -386,6 +383,19 @@ static void bt_ready(int err)
|
||||||
printk("Advertising successfully started\n");
|
printk("Advertising successfully started\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void bas_notify(void)
|
||||||
|
{
|
||||||
|
u8_t battery_level = bt_gatt_bas_get_battery_level();
|
||||||
|
|
||||||
|
battery_level--;
|
||||||
|
|
||||||
|
if (!battery_level) {
|
||||||
|
battery_level = 100U;
|
||||||
|
}
|
||||||
|
|
||||||
|
bt_gatt_bas_set_battery_level(battery_level);
|
||||||
|
}
|
||||||
|
|
||||||
void main(void)
|
void main(void)
|
||||||
{
|
{
|
||||||
int err;
|
int err;
|
||||||
|
|
|
@ -6,7 +6,6 @@ project(peripheral_esp)
|
||||||
|
|
||||||
target_sources(app PRIVATE
|
target_sources(app PRIVATE
|
||||||
src/main.c
|
src/main.c
|
||||||
../gatt/bas.c
|
|
||||||
)
|
)
|
||||||
|
|
||||||
zephyr_library_include_directories($ENV{ZEPHYR_BASE}/samples/bluetooth)
|
zephyr_library_include_directories($ENV{ZEPHYR_BASE}/samples/bluetooth)
|
||||||
|
|
|
@ -5,4 +5,5 @@ CONFIG_TINYCRYPT=y
|
||||||
CONFIG_BT_DEVICE_NAME="ESP peripheral"
|
CONFIG_BT_DEVICE_NAME="ESP peripheral"
|
||||||
CONFIG_BT_GATT_DIS=y
|
CONFIG_BT_GATT_DIS=y
|
||||||
CONFIG_BT_GATT_DIS_PNP=n
|
CONFIG_BT_GATT_DIS_PNP=n
|
||||||
|
CONFIG_BT_GATT_BAS=y
|
||||||
CONFIG_BT_DEVICE_APPEARANCE=768
|
CONFIG_BT_DEVICE_APPEARANCE=768
|
||||||
|
|
|
@ -20,8 +20,7 @@
|
||||||
#include <bluetooth/conn.h>
|
#include <bluetooth/conn.h>
|
||||||
#include <bluetooth/uuid.h>
|
#include <bluetooth/uuid.h>
|
||||||
#include <bluetooth/gatt.h>
|
#include <bluetooth/gatt.h>
|
||||||
|
#include <bluetooth/services/bas.h>
|
||||||
#include <gatt/bas.h>
|
|
||||||
|
|
||||||
#define SENSOR_1_NAME "Temperature Sensor 1"
|
#define SENSOR_1_NAME "Temperature Sensor 1"
|
||||||
#define SENSOR_2_NAME "Temperature Sensor 2"
|
#define SENSOR_2_NAME "Temperature Sensor 2"
|
||||||
|
@ -387,8 +386,6 @@ static void bt_ready(int err)
|
||||||
|
|
||||||
printk("Bluetooth initialized\n");
|
printk("Bluetooth initialized\n");
|
||||||
|
|
||||||
bas_init();
|
|
||||||
|
|
||||||
err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0);
|
err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0);
|
||||||
if (err) {
|
if (err) {
|
||||||
printk("Advertising failed to start (err %d)\n", err);
|
printk("Advertising failed to start (err %d)\n", err);
|
||||||
|
@ -422,6 +419,19 @@ static struct bt_conn_auth_cb auth_cb_display = {
|
||||||
.cancel = auth_cancel,
|
.cancel = auth_cancel,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static void bas_notify(void)
|
||||||
|
{
|
||||||
|
u8_t battery_level = bt_gatt_bas_get_battery_level();
|
||||||
|
|
||||||
|
battery_level--;
|
||||||
|
|
||||||
|
if (!battery_level) {
|
||||||
|
battery_level = 100U;
|
||||||
|
}
|
||||||
|
|
||||||
|
bt_gatt_bas_set_battery_level(battery_level);
|
||||||
|
}
|
||||||
|
|
||||||
void main(void)
|
void main(void)
|
||||||
{
|
{
|
||||||
int err;
|
int err;
|
||||||
|
|
|
@ -8,7 +8,6 @@ FILE(GLOB app_sources src/*.c)
|
||||||
target_sources(app PRIVATE
|
target_sources(app PRIVATE
|
||||||
${app_sources}
|
${app_sources}
|
||||||
../gatt/hog.c
|
../gatt/hog.c
|
||||||
../gatt/bas.c
|
|
||||||
)
|
)
|
||||||
|
|
||||||
zephyr_library_include_directories($ENV{ZEPHYR_BASE}/samples/bluetooth)
|
zephyr_library_include_directories($ENV{ZEPHYR_BASE}/samples/bluetooth)
|
||||||
|
|
|
@ -6,6 +6,7 @@ CONFIG_BT_DEBUG_LOG=y
|
||||||
CONFIG_BT_SMP=y
|
CONFIG_BT_SMP=y
|
||||||
CONFIG_BT_PERIPHERAL=y
|
CONFIG_BT_PERIPHERAL=y
|
||||||
CONFIG_BT_GATT_DIS=y
|
CONFIG_BT_GATT_DIS=y
|
||||||
|
CONFIG_BT_GATT_BAS=y
|
||||||
CONFIG_BT_DEVICE_NAME="Test HoG mouse"
|
CONFIG_BT_DEVICE_NAME="Test HoG mouse"
|
||||||
CONFIG_BT_DEVICE_APPEARANCE=962
|
CONFIG_BT_DEVICE_APPEARANCE=962
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,6 @@
|
||||||
#include <bluetooth/uuid.h>
|
#include <bluetooth/uuid.h>
|
||||||
#include <bluetooth/gatt.h>
|
#include <bluetooth/gatt.h>
|
||||||
|
|
||||||
#include <gatt/bas.h>
|
|
||||||
#include <gatt/hog.h>
|
#include <gatt/hog.h>
|
||||||
|
|
||||||
static const struct bt_data ad[] = {
|
static const struct bt_data ad[] = {
|
||||||
|
@ -83,7 +82,6 @@ static void bt_ready(int err)
|
||||||
|
|
||||||
printk("Bluetooth initialized\n");
|
printk("Bluetooth initialized\n");
|
||||||
|
|
||||||
bas_init();
|
|
||||||
hog_init();
|
hog_init();
|
||||||
|
|
||||||
if (IS_ENABLED(CONFIG_SETTINGS)) {
|
if (IS_ENABLED(CONFIG_SETTINGS)) {
|
||||||
|
|
|
@ -9,7 +9,6 @@ FILE(GLOB app_sources src/*.c)
|
||||||
target_sources(app PRIVATE
|
target_sources(app PRIVATE
|
||||||
${app_sources}
|
${app_sources}
|
||||||
../gatt/hrs.c
|
../gatt/hrs.c
|
||||||
../gatt/bas.c
|
|
||||||
)
|
)
|
||||||
|
|
||||||
zephyr_library_include_directories($ENV{ZEPHYR_BASE}/samples/bluetooth)
|
zephyr_library_include_directories($ENV{ZEPHYR_BASE}/samples/bluetooth)
|
||||||
|
|
|
@ -4,5 +4,6 @@ CONFIG_BT_SMP=y
|
||||||
CONFIG_BT_PERIPHERAL=y
|
CONFIG_BT_PERIPHERAL=y
|
||||||
CONFIG_BT_GATT_DIS=y
|
CONFIG_BT_GATT_DIS=y
|
||||||
CONFIG_BT_GATT_DIS_PNP=n
|
CONFIG_BT_GATT_DIS_PNP=n
|
||||||
|
CONFIG_BT_GATT_BAS=y
|
||||||
CONFIG_BT_DEVICE_NAME="Zephyr Heartrate Sensor"
|
CONFIG_BT_DEVICE_NAME="Zephyr Heartrate Sensor"
|
||||||
CONFIG_BT_DEVICE_APPEARANCE=833
|
CONFIG_BT_DEVICE_APPEARANCE=833
|
||||||
|
|
|
@ -19,9 +19,9 @@
|
||||||
#include <bluetooth/conn.h>
|
#include <bluetooth/conn.h>
|
||||||
#include <bluetooth/uuid.h>
|
#include <bluetooth/uuid.h>
|
||||||
#include <bluetooth/gatt.h>
|
#include <bluetooth/gatt.h>
|
||||||
|
#include <bluetooth/services/bas.h>
|
||||||
|
|
||||||
#include <gatt/hrs.h>
|
#include <gatt/hrs.h>
|
||||||
#include <gatt/bas.h>
|
|
||||||
|
|
||||||
struct bt_conn *default_conn;
|
struct bt_conn *default_conn;
|
||||||
|
|
||||||
|
@ -65,7 +65,6 @@ static void bt_ready(int err)
|
||||||
printk("Bluetooth initialized\n");
|
printk("Bluetooth initialized\n");
|
||||||
|
|
||||||
hrs_init(0x01);
|
hrs_init(0x01);
|
||||||
bas_init();
|
|
||||||
|
|
||||||
err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0);
|
err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0);
|
||||||
if (err) {
|
if (err) {
|
||||||
|
@ -89,6 +88,19 @@ static struct bt_conn_auth_cb auth_cb_display = {
|
||||||
.cancel = auth_cancel,
|
.cancel = auth_cancel,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static void bas_notify(void)
|
||||||
|
{
|
||||||
|
u8_t battery_level = bt_gatt_bas_get_battery_level();
|
||||||
|
|
||||||
|
battery_level--;
|
||||||
|
|
||||||
|
if (!battery_level) {
|
||||||
|
battery_level = 100U;
|
||||||
|
}
|
||||||
|
|
||||||
|
bt_gatt_bas_set_battery_level(battery_level);
|
||||||
|
}
|
||||||
|
|
||||||
void main(void)
|
void main(void)
|
||||||
{
|
{
|
||||||
int err;
|
int err;
|
||||||
|
|
|
@ -9,7 +9,6 @@ FILE(GLOB app_sources src/*.c)
|
||||||
target_sources(app PRIVATE
|
target_sources(app PRIVATE
|
||||||
${app_sources}
|
${app_sources}
|
||||||
../gatt/hts.c
|
../gatt/hts.c
|
||||||
../gatt/bas.c
|
|
||||||
)
|
)
|
||||||
|
|
||||||
zephyr_library_include_directories($ENV{ZEPHYR_BASE}/samples/bluetooth)
|
zephyr_library_include_directories($ENV{ZEPHYR_BASE}/samples/bluetooth)
|
||||||
|
|
|
@ -4,6 +4,7 @@ CONFIG_BT_SMP=y
|
||||||
CONFIG_BT_PERIPHERAL=y
|
CONFIG_BT_PERIPHERAL=y
|
||||||
CONFIG_BT_GATT_DIS=y
|
CONFIG_BT_GATT_DIS=y
|
||||||
CONFIG_BT_GATT_DIS_PNP=n
|
CONFIG_BT_GATT_DIS_PNP=n
|
||||||
|
CONFIG_BT_GATT_BAS=y
|
||||||
CONFIG_BT_DEVICE_NAME="Zephyr Health Thermometer"
|
CONFIG_BT_DEVICE_NAME="Zephyr Health Thermometer"
|
||||||
CONFIG_BT_DEVICE_APPEARANCE=768
|
CONFIG_BT_DEVICE_APPEARANCE=768
|
||||||
CONFIG_BT_ATT_ENFORCE_FLOW=n
|
CONFIG_BT_ATT_ENFORCE_FLOW=n
|
||||||
|
|
|
@ -19,9 +19,9 @@
|
||||||
#include <bluetooth/conn.h>
|
#include <bluetooth/conn.h>
|
||||||
#include <bluetooth/uuid.h>
|
#include <bluetooth/uuid.h>
|
||||||
#include <bluetooth/gatt.h>
|
#include <bluetooth/gatt.h>
|
||||||
|
#include <bluetooth/services/bas.h>
|
||||||
|
|
||||||
#include <gatt/hts.h>
|
#include <gatt/hts.h>
|
||||||
#include <gatt/bas.h>
|
|
||||||
|
|
||||||
struct bt_conn *default_conn;
|
struct bt_conn *default_conn;
|
||||||
|
|
||||||
|
@ -68,7 +68,6 @@ static void bt_ready(int err)
|
||||||
printk("Bluetooth initialized\n");
|
printk("Bluetooth initialized\n");
|
||||||
|
|
||||||
hts_init();
|
hts_init();
|
||||||
bas_init();
|
|
||||||
|
|
||||||
err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0);
|
err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0);
|
||||||
if (err) {
|
if (err) {
|
||||||
|
@ -92,6 +91,19 @@ static struct bt_conn_auth_cb auth_cb_display = {
|
||||||
.cancel = auth_cancel,
|
.cancel = auth_cancel,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static void bas_notify(void)
|
||||||
|
{
|
||||||
|
u8_t battery_level = bt_gatt_bas_get_battery_level();
|
||||||
|
|
||||||
|
battery_level--;
|
||||||
|
|
||||||
|
if (!battery_level) {
|
||||||
|
battery_level = 100U;
|
||||||
|
}
|
||||||
|
|
||||||
|
bt_gatt_bas_set_battery_level(battery_level);
|
||||||
|
}
|
||||||
|
|
||||||
void main(void)
|
void main(void)
|
||||||
{
|
{
|
||||||
int err;
|
int err;
|
||||||
|
|
|
@ -2,3 +2,5 @@
|
||||||
|
|
||||||
|
|
||||||
zephyr_sources_ifdef(CONFIG_BT_GATT_DIS dis.c)
|
zephyr_sources_ifdef(CONFIG_BT_GATT_DIS dis.c)
|
||||||
|
|
||||||
|
zephyr_sources_ifdef(CONFIG_BT_GATT_BAS bas.c)
|
||||||
|
|
|
@ -11,6 +11,8 @@ menu "GATT Services"
|
||||||
|
|
||||||
source "subsys/bluetooth/services/Kconfig.dis"
|
source "subsys/bluetooth/services/Kconfig.dis"
|
||||||
|
|
||||||
|
source "subsys/bluetooth/services/Kconfig.bas"
|
||||||
|
|
||||||
endmenu
|
endmenu
|
||||||
|
|
||||||
endif # BT_CONN
|
endif #BT_CONN
|
||||||
|
|
29
subsys/bluetooth/services/Kconfig.bas
Normal file
29
subsys/bluetooth/services/Kconfig.bas
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
# Kconfig - Bluetooth GATT Battery service
|
||||||
|
#
|
||||||
|
# Copyright (c) 2018 Nordic Semiconductor ASA
|
||||||
|
#
|
||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
#
|
||||||
|
|
||||||
|
menuconfig BT_GATT_BAS
|
||||||
|
bool "Enable GATT Battery service"
|
||||||
|
select SENSOR
|
||||||
|
default n
|
||||||
|
|
||||||
|
if BT_GATT_BAS
|
||||||
|
|
||||||
|
config BT_GATT_BAS_LOG_LEVEL
|
||||||
|
int "Battery service log level"
|
||||||
|
depends on LOG
|
||||||
|
range 0 4
|
||||||
|
default 0
|
||||||
|
help
|
||||||
|
Sets log level for the Battery 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
|
88
subsys/bluetooth/services/bas.c
Normal file
88
subsys/bluetooth/services/bas.c
Normal file
|
@ -0,0 +1,88 @@
|
||||||
|
/** @file
|
||||||
|
* @brief GATT Battery Service
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2018 Nordic Semiconductor ASA
|
||||||
|
* Copyright (c) 2016 Intel Corporation
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <init.h>
|
||||||
|
#include <misc/__assert.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <zephyr/types.h>
|
||||||
|
|
||||||
|
#include <bluetooth/bluetooth.h>
|
||||||
|
#include <bluetooth/conn.h>
|
||||||
|
#include <bluetooth/gatt.h>
|
||||||
|
#include <bluetooth/uuid.h>
|
||||||
|
#include <bluetooth/services/bas.h>
|
||||||
|
|
||||||
|
#define LOG_LEVEL CONFIG_BT_GATT_BAS_LOG_LEVEL
|
||||||
|
#include <logging/log.h>
|
||||||
|
LOG_MODULE_REGISTER(bas);
|
||||||
|
|
||||||
|
static struct bt_gatt_ccc_cfg blvl_ccc_cfg[BT_GATT_CCC_MAX] = {};
|
||||||
|
|
||||||
|
static u8_t battery_level = 100U;
|
||||||
|
|
||||||
|
static void blvl_ccc_cfg_changed(const struct bt_gatt_attr *attr,
|
||||||
|
u16_t value)
|
||||||
|
{
|
||||||
|
ARG_UNUSED(attr);
|
||||||
|
|
||||||
|
bool notif_enabled = (value == BT_GATT_CCC_NOTIFY);
|
||||||
|
|
||||||
|
LOG_INF("BAS Notifications %s", notif_enabled ? "enabled" : "disabled");
|
||||||
|
}
|
||||||
|
|
||||||
|
static ssize_t read_blvl(struct bt_conn *conn,
|
||||||
|
const struct bt_gatt_attr *attr, void *buf,
|
||||||
|
u16_t len, u16_t offset)
|
||||||
|
{
|
||||||
|
u8_t lvl8 = battery_level;
|
||||||
|
|
||||||
|
return bt_gatt_attr_read(conn, attr, buf, len, offset, &lvl8,
|
||||||
|
sizeof(lvl8));
|
||||||
|
}
|
||||||
|
|
||||||
|
BT_GATT_SERVICE_DEFINE(bas,
|
||||||
|
BT_GATT_PRIMARY_SERVICE(BT_UUID_BAS),
|
||||||
|
BT_GATT_CHARACTERISTIC(BT_UUID_BAS_BATTERY_LEVEL,
|
||||||
|
BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY,
|
||||||
|
BT_GATT_PERM_READ, read_blvl, NULL,
|
||||||
|
&battery_level),
|
||||||
|
BT_GATT_CCC(blvl_ccc_cfg, blvl_ccc_cfg_changed),
|
||||||
|
);
|
||||||
|
|
||||||
|
static int bas_init(struct device *dev)
|
||||||
|
{
|
||||||
|
ARG_UNUSED(dev);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
u8_t bt_gatt_bas_get_battery_level(void)
|
||||||
|
{
|
||||||
|
return battery_level;
|
||||||
|
}
|
||||||
|
|
||||||
|
int bt_gatt_bas_set_battery_level(u8_t level)
|
||||||
|
{
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
if (level > 100U) {
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
battery_level = level;
|
||||||
|
|
||||||
|
rc = bt_gatt_notify(NULL, &bas.attrs[1], &level, sizeof(level));
|
||||||
|
|
||||||
|
return rc == -ENOTCONN ? 0 : rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
SYS_INIT(bas_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);
|
|
@ -18,7 +18,6 @@ target_sources(app PRIVATE
|
||||||
src/test_connect1.c
|
src/test_connect1.c
|
||||||
src/test_connect2.c
|
src/test_connect2.c
|
||||||
../../../../samples/bluetooth/gatt/hrs.c
|
../../../../samples/bluetooth/gatt/hrs.c
|
||||||
../../../../samples/bluetooth/gatt/bas.c
|
|
||||||
)
|
)
|
||||||
|
|
||||||
zephyr_include_directories(
|
zephyr_include_directories(
|
||||||
|
|
|
@ -5,6 +5,7 @@ CONFIG_BT_PERIPHERAL=y
|
||||||
CONFIG_BT_PRIVACY=y
|
CONFIG_BT_PRIVACY=y
|
||||||
CONFIG_BT_SMP=y
|
CONFIG_BT_SMP=y
|
||||||
CONFIG_BT_SIGNING=y
|
CONFIG_BT_SIGNING=y
|
||||||
|
CONFIG_BT_GATT_BAS=y
|
||||||
CONFIG_BT_ATT_PREPARE_COUNT=2
|
CONFIG_BT_ATT_PREPARE_COUNT=2
|
||||||
CONFIG_BT_GATT_CLIENT=y
|
CONFIG_BT_GATT_CLIENT=y
|
||||||
CONFIG_BT_L2CAP_DYNAMIC_CHANNEL=y
|
CONFIG_BT_L2CAP_DYNAMIC_CHANNEL=y
|
||||||
|
|
|
@ -5,6 +5,7 @@ CONFIG_BT_PERIPHERAL=y
|
||||||
CONFIG_BT_PRIVACY=y
|
CONFIG_BT_PRIVACY=y
|
||||||
CONFIG_BT_SMP=y
|
CONFIG_BT_SMP=y
|
||||||
CONFIG_BT_SIGNING=y
|
CONFIG_BT_SIGNING=y
|
||||||
|
CONFIG_BT_GATT_BAS=y
|
||||||
CONFIG_BT_ATT_PREPARE_COUNT=2
|
CONFIG_BT_ATT_PREPARE_COUNT=2
|
||||||
CONFIG_BT_GATT_CLIENT=y
|
CONFIG_BT_GATT_CLIENT=y
|
||||||
CONFIG_BT_L2CAP_DYNAMIC_CHANNEL=y
|
CONFIG_BT_L2CAP_DYNAMIC_CHANNEL=y
|
||||||
|
|
|
@ -22,10 +22,10 @@
|
||||||
#include <bluetooth/conn.h>
|
#include <bluetooth/conn.h>
|
||||||
#include <bluetooth/uuid.h>
|
#include <bluetooth/uuid.h>
|
||||||
#include <bluetooth/gatt.h>
|
#include <bluetooth/gatt.h>
|
||||||
|
#include <bluetooth/services/bas.h>
|
||||||
#include <sys/byteorder.h>
|
#include <sys/byteorder.h>
|
||||||
|
|
||||||
#include <gatt/hrs.h>
|
#include <gatt/hrs.h>
|
||||||
#include <gatt/bas.h>
|
|
||||||
|
|
||||||
static struct bt_conn *default_conn;
|
static struct bt_conn *default_conn;
|
||||||
|
|
||||||
|
@ -109,7 +109,6 @@ static void bt_ready(int err)
|
||||||
printk("Bluetooth initialized\n");
|
printk("Bluetooth initialized\n");
|
||||||
|
|
||||||
hrs_init(0x01);
|
hrs_init(0x01);
|
||||||
bas_init();
|
|
||||||
|
|
||||||
err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0);
|
err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0);
|
||||||
if (err) {
|
if (err) {
|
||||||
|
@ -120,6 +119,19 @@ static void bt_ready(int err)
|
||||||
printk("Advertising successfully started\n");
|
printk("Advertising successfully started\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void bas_notify(void)
|
||||||
|
{
|
||||||
|
u8_t battery_level = bt_gatt_bas_get_battery_level();
|
||||||
|
|
||||||
|
battery_level--;
|
||||||
|
|
||||||
|
if (!battery_level) {
|
||||||
|
battery_level = 100U;
|
||||||
|
}
|
||||||
|
|
||||||
|
bt_gatt_bas_set_battery_level(battery_level);
|
||||||
|
}
|
||||||
|
|
||||||
static void test_con2_main(void)
|
static void test_con2_main(void)
|
||||||
{
|
{
|
||||||
static int notify_count;
|
static int notify_count;
|
||||||
|
|
Loading…
Reference in a new issue