Skip to content
Permalink
Browse files

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>
  • Loading branch information...
lemrey authored and jhedberg committed Sep 11, 2018
1 parent 6a0fcf1 commit 30d65809fcaeebe644fa4bb03ccb37a7652ba9be
Showing with 269 additions and 115 deletions.
  1. +55 −0 include/bluetooth/services/bas.h
  2. +0 −70 samples/bluetooth/gatt/bas.c
  3. +0 −20 samples/bluetooth/gatt/bas.h
  4. +0 −1 samples/bluetooth/peripheral/CMakeLists.txt
  5. +1 −0 samples/bluetooth/peripheral/prj.conf
  6. +14 −1 samples/bluetooth/peripheral/src/main.c
  7. +0 −1 samples/bluetooth/peripheral_csc/CMakeLists.txt
  8. +1 −0 samples/bluetooth/peripheral_csc/prj.conf
  9. +14 −4 samples/bluetooth/peripheral_csc/src/main.c
  10. +0 −1 samples/bluetooth/peripheral_esp/CMakeLists.txt
  11. +1 −0 samples/bluetooth/peripheral_esp/prj.conf
  12. +14 −4 samples/bluetooth/peripheral_esp/src/main.c
  13. +0 −1 samples/bluetooth/peripheral_hids/CMakeLists.txt
  14. +1 −0 samples/bluetooth/peripheral_hids/prj.conf
  15. +0 −2 samples/bluetooth/peripheral_hids/src/main.c
  16. +0 −1 samples/bluetooth/peripheral_hr/CMakeLists.txt
  17. +1 −0 samples/bluetooth/peripheral_hr/prj.conf
  18. +14 −2 samples/bluetooth/peripheral_hr/src/main.c
  19. +0 −1 samples/bluetooth/peripheral_ht/CMakeLists.txt
  20. +1 −0 samples/bluetooth/peripheral_ht/prj.conf
  21. +14 −2 samples/bluetooth/peripheral_ht/src/main.c
  22. +2 −0 subsys/bluetooth/services/CMakeLists.txt
  23. +3 −1 subsys/bluetooth/services/Kconfig
  24. +29 −0 subsys/bluetooth/services/Kconfig.bas
  25. +88 −0 subsys/bluetooth/services/bas.c
  26. +0 −1 tests/bluetooth/bsim_bt/bsim_test_app/CMakeLists.txt
  27. +1 −0 tests/bluetooth/bsim_bt/bsim_test_app/prj.conf
  28. +1 −0 tests/bluetooth/bsim_bt/bsim_test_app/prj_split.conf
  29. +14 −2 tests/bluetooth/bsim_bt/bsim_test_app/src/test_connect2.c
@@ -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_ */

This file was deleted.

This file was deleted.

@@ -7,7 +7,6 @@ project(peripheral)
target_sources(app PRIVATE
src/main.c
../gatt/hrs.c
../gatt/bas.c
../gatt/cts.c
)

@@ -8,6 +8,7 @@ CONFIG_BT_SIGNING=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_GATT_DIS=y
CONFIG_BT_ATT_PREPARE_COUNT=5
CONFIG_BT_GATT_BAS=y
CONFIG_BT_PRIVACY=y
CONFIG_BT_DEVICE_NAME="Zephyr Peripheral Sample Long Name"
CONFIG_BT_DEVICE_APPEARANCE=833
@@ -21,9 +21,9 @@
#include <bluetooth/conn.h>
#include <bluetooth/uuid.h>
#include <bluetooth/gatt.h>
#include <bluetooth/services/bas.h>

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

/* Custom Service Variables */
@@ -294,6 +294,19 @@ static struct bt_conn_auth_cb auth_cb_display = {
.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)
{
int err;
@@ -7,7 +7,6 @@ project(peripheral_csc)
FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE
${app_sources}
../gatt/bas.c
)

zephyr_library_include_directories($ENV{ZEPHYR_BASE}/samples/bluetooth)
@@ -4,5 +4,6 @@ CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_GATT_DIS=y
CONFIG_BT_GATT_DIS_PNP=n
CONFIG_BT_GATT_BAS=y
CONFIG_BT_DEVICE_NAME="CSC peripheral"
CONFIG_BT_DEVICE_APPEARANCE=1157
@@ -20,8 +20,7 @@
#include <bluetooth/conn.h>
#include <bluetooth/uuid.h>
#include <bluetooth/gatt.h>

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

#define CSC_SUPPORTED_LOCATIONS { CSC_LOC_OTHER, \
CSC_LOC_FRONT_WHEEL, \
@@ -375,8 +374,6 @@ static void bt_ready(int err)

printk("Bluetooth initialized\n");

bas_init();

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);
@@ -386,6 +383,19 @@ static void bt_ready(int err)
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)
{
int err;
@@ -6,7 +6,6 @@ project(peripheral_esp)

target_sources(app PRIVATE
src/main.c
../gatt/bas.c
)

zephyr_library_include_directories($ENV{ZEPHYR_BASE}/samples/bluetooth)
@@ -5,4 +5,5 @@ CONFIG_TINYCRYPT=y
CONFIG_BT_DEVICE_NAME="ESP peripheral"
CONFIG_BT_GATT_DIS=y
CONFIG_BT_GATT_DIS_PNP=n
CONFIG_BT_GATT_BAS=y
CONFIG_BT_DEVICE_APPEARANCE=768
@@ -20,8 +20,7 @@
#include <bluetooth/conn.h>
#include <bluetooth/uuid.h>
#include <bluetooth/gatt.h>

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

#define SENSOR_1_NAME "Temperature Sensor 1"
#define SENSOR_2_NAME "Temperature Sensor 2"
@@ -387,8 +386,6 @@ static void bt_ready(int err)

printk("Bluetooth initialized\n");

bas_init();

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);
@@ -422,6 +419,19 @@ static struct bt_conn_auth_cb auth_cb_display = {
.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)
{
int err;
@@ -8,7 +8,6 @@ FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE
${app_sources}
../gatt/hog.c
../gatt/bas.c
)

zephyr_library_include_directories($ENV{ZEPHYR_BASE}/samples/bluetooth)
@@ -6,6 +6,7 @@ CONFIG_BT_DEBUG_LOG=y
CONFIG_BT_SMP=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_GATT_DIS=y
CONFIG_BT_GATT_BAS=y
CONFIG_BT_DEVICE_NAME="Test HoG mouse"
CONFIG_BT_DEVICE_APPEARANCE=962

@@ -22,7 +22,6 @@
#include <bluetooth/uuid.h>
#include <bluetooth/gatt.h>

#include <gatt/bas.h>
#include <gatt/hog.h>

static const struct bt_data ad[] = {
@@ -83,7 +82,6 @@ static void bt_ready(int err)

printk("Bluetooth initialized\n");

bas_init();
hog_init();

if (IS_ENABLED(CONFIG_SETTINGS)) {
@@ -9,7 +9,6 @@ FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE
${app_sources}
../gatt/hrs.c
../gatt/bas.c
)

zephyr_library_include_directories($ENV{ZEPHYR_BASE}/samples/bluetooth)
@@ -4,5 +4,6 @@ CONFIG_BT_SMP=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_GATT_DIS=y
CONFIG_BT_GATT_DIS_PNP=n
CONFIG_BT_GATT_BAS=y
CONFIG_BT_DEVICE_NAME="Zephyr Heartrate Sensor"
CONFIG_BT_DEVICE_APPEARANCE=833
@@ -19,9 +19,9 @@
#include <bluetooth/conn.h>
#include <bluetooth/uuid.h>
#include <bluetooth/gatt.h>
#include <bluetooth/services/bas.h>

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

struct bt_conn *default_conn;

@@ -65,7 +65,6 @@ static void bt_ready(int err)
printk("Bluetooth initialized\n");

hrs_init(0x01);
bas_init();

err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0);
if (err) {
@@ -89,6 +88,19 @@ static struct bt_conn_auth_cb auth_cb_display = {
.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)
{
int err;
@@ -9,7 +9,6 @@ FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE
${app_sources}
../gatt/hts.c
../gatt/bas.c
)

zephyr_library_include_directories($ENV{ZEPHYR_BASE}/samples/bluetooth)

0 comments on commit 30d6580

Please sign in to comment.
You can’t perform that action at this time.