diff options
author | Luiz Augusto von Dentz <luiz.von.dentz@intel.com> | 2016-03-16 17:08:51 +0200 |
---|---|---|
committer | Johan Hedberg <johan.hedberg@intel.com> | 2016-03-22 17:26:29 +0000 |
commit | 6d461204bfce32246bf66a04a5feee6417e94d73 (patch) | |
tree | 4be01c0ad13a444c11133d6c0c68a8887be81f66 | |
parent | 265fc8703a710860273c14547608a4fba4c55a9d (diff) |
Bluetooth: BAS: Add service sample
This adds a BAS service sample which can be shared with application
samples.
Change-Id: I1f484dadf63b41632992454a40edd91077bb78fa
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
-rw-r--r-- | samples/bluetooth/gatt/bas.c | 81 | ||||
-rw-r--r-- | samples/bluetooth/gatt/bas.h | 22 | ||||
-rw-r--r-- | samples/bluetooth/peripheral/src/Makefile | 3 | ||||
-rw-r--r-- | samples/bluetooth/peripheral/src/main.c | 43 | ||||
-rw-r--r-- | samples/bluetooth/peripheral_csc/src/Makefile | 2 | ||||
-rw-r--r-- | samples/bluetooth/peripheral_csc/src/main.c | 45 | ||||
-rw-r--r-- | samples/bluetooth/peripheral_esp/src/Makefile | 2 | ||||
-rw-r--r-- | samples/bluetooth/peripheral_esp/src/main.c | 44 | ||||
-rw-r--r-- | samples/bluetooth/peripheral_hr/src/Makefile | 3 | ||||
-rw-r--r-- | samples/bluetooth/peripheral_hr/src/main.c | 43 |
10 files changed, 121 insertions, 167 deletions
diff --git a/samples/bluetooth/gatt/bas.c b/samples/bluetooth/gatt/bas.c new file mode 100644 index 000000000..81ccaa3f5 --- /dev/null +++ b/samples/bluetooth/gatt/bas.c @@ -0,0 +1,81 @@ +/** @file + * @brief BAS Service sample + */ + +/* + * Copyright (c) 2016 Intel Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <stdint.h> +#include <stddef.h> +#include <string.h> +#include <errno.h> +#include <misc/printk.h> +#include <misc/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[CONFIG_BLUETOOTH_MAX_PAIRED] = {}; +static uint8_t simulate_blvl; +static uint8_t battery = 100; + +static void blvl_ccc_cfg_changed(uint16_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, uint16_t len, uint16_t offset) +{ + const char *value = attr->user_data; + + return bt_gatt_attr_read(conn, attr, buf, len, offset, value, + sizeof(*value)); +} + +/* Battery Service Declaration */ +static struct bt_gatt_attr attrs[] = { + 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_DESCRIPTOR(BT_UUID_BAS_BATTERY_LEVEL, BT_GATT_PERM_READ, + read_blvl, NULL, &battery), + BT_GATT_CCC(blvl_ccc_cfg, blvl_ccc_cfg_changed), +}; + +void bas_init(void) +{ + bt_gatt_register(attrs, ARRAY_SIZE(attrs)); +} + +void bas_notify(void) +{ + if (!simulate_blvl) { + return; + } + + battery--; + if (!battery) { + /* Software eco battery charger */ + battery = 100; + } + + bt_gatt_notify(NULL, &attrs[2], &battery, sizeof(battery)); +} diff --git a/samples/bluetooth/gatt/bas.h b/samples/bluetooth/gatt/bas.h new file mode 100644 index 000000000..eff7990ef --- /dev/null +++ b/samples/bluetooth/gatt/bas.h @@ -0,0 +1,22 @@ +/** @file + * @brief BAS Service sample + */ + +/* + * Copyright (c) 2016 Intel Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +void bas_init(void); +void bas_notify(void); diff --git a/samples/bluetooth/peripheral/src/Makefile b/samples/bluetooth/peripheral/src/Makefile index 9d95b5a4b..040c4c91b 100644 --- a/samples/bluetooth/peripheral/src/Makefile +++ b/samples/bluetooth/peripheral/src/Makefile @@ -1,3 +1,4 @@ ccflags-y +=-I${srctree}/samples/bluetooth -obj-y = main.o ../../gatt/gap.o ../../gatt/hrs.o ../../gatt/dis.o +obj-y = main.o ../../gatt/gap.o ../../gatt/hrs.o ../../gatt/dis.o \ + ../../gatt/bas.o diff --git a/samples/bluetooth/peripheral/src/main.c b/samples/bluetooth/peripheral/src/main.c index a3d01aed5..a894f5f65 100644 --- a/samples/bluetooth/peripheral/src/main.c +++ b/samples/bluetooth/peripheral/src/main.c @@ -33,29 +33,12 @@ #include <gatt/gap.h> #include <gatt/hrs.h> #include <gatt/dis.h> +#include <gatt/bas.h> #define DEVICE_NAME "Test peripheral" #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1) #define HEART_RATE_APPEARANCE 0x0341 -static struct bt_gatt_ccc_cfg blvl_ccc_cfg[CONFIG_BLUETOOTH_MAX_PAIRED] = {}; -static uint8_t simulate_blvl = 0; -static uint8_t battery = 100; - -static void blvl_ccc_cfg_changed(uint16_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, uint16_t len, uint16_t offset) -{ - const char *value = attr->user_data; - - return bt_gatt_attr_read(conn, attr, buf, len, offset, value, - sizeof(*value)); -} - static void generate_current_time(uint8_t *buf) { uint16_t year; @@ -264,16 +247,6 @@ static const struct bt_uuid_128 vnd_signed_uuid = BT_UUID_INIT_128( 0xf3, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x13, 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x13); -/* Battery Service Declaration */ -static struct bt_gatt_attr bas_attrs[] = { - 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_DESCRIPTOR(BT_UUID_BAS_BATTERY_LEVEL, BT_GATT_PERM_READ, - read_blvl, NULL, &battery), - BT_GATT_CCC(blvl_ccc_cfg, blvl_ccc_cfg_changed), -}; - /* Current Time Service Declaration */ static struct bt_gatt_attr cts_attrs[] = { BT_GATT_PRIMARY_SERVICE(BT_UUID_CTS), @@ -360,7 +333,7 @@ static void bt_ready(int err) gap_init(DEVICE_NAME, HEART_RATE_APPEARANCE); hrs_init(0x01); - bt_gatt_register(bas_attrs, ARRAY_SIZE(bas_attrs)); + bas_init(); bt_gatt_register(cts_attrs, ARRAY_SIZE(cts_attrs)); dis_init(CONFIG_SOC, "Manufacturer"); bt_gatt_register(vnd_attrs, ARRAY_SIZE(vnd_attrs)); @@ -435,17 +408,7 @@ void main(void) hrs_notify(); /* Battery level simulation */ - if (simulate_blvl) { - battery -= 1; - - if (!battery) { - /* Software eco battery charger */ - battery = 100; - } - - bt_gatt_notify(NULL, &bas_attrs[2], &battery, - sizeof(battery)); - } + bas_notify(); /* Vendor indication simulation */ if (simulate_vnd) { diff --git a/samples/bluetooth/peripheral_csc/src/Makefile b/samples/bluetooth/peripheral_csc/src/Makefile index fb0b81f5c..1f76bcbd1 100644 --- a/samples/bluetooth/peripheral_csc/src/Makefile +++ b/samples/bluetooth/peripheral_csc/src/Makefile @@ -1,3 +1,3 @@ ccflags-y +=-I${srctree}/samples/bluetooth -obj-y = main.o ../../gatt/gap.o ../../gatt/dis.o +obj-y = main.o ../../gatt/gap.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 806cde3a9..ebbc7564c 100644 --- a/samples/bluetooth/peripheral_csc/src/main.c +++ b/samples/bluetooth/peripheral_csc/src/main.c @@ -33,6 +33,7 @@ #include <gatt/gap.h> #include <gatt/dis.h> +#include <gatt/bas.h> #define DEVICE_NAME "CSC peripheral" #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1) @@ -89,35 +90,6 @@ #define CSC_WHEEL_REV_DATA_PRESENT BIT(0) #define CSC_CRANK_REV_DATA_PRESENT BIT(1) -/* Battery Service declaration */ - -static struct bt_gatt_ccc_cfg blvl_ccc_cfg[CONFIG_BLUETOOTH_MAX_PAIRED] = {}; -static bool bas_simulate; -static uint8_t blvl = 100; - -static void blvl_ccc_cfg_changed(uint16_t value) -{ - bas_simulate = value == BT_GATT_CCC_NOTIFY; -} - -static ssize_t read_blvl(struct bt_conn *conn, const struct bt_gatt_attr *attr, - void *buf, uint16_t len, uint16_t offset) -{ - const char *value = attr->user_data; - - return bt_gatt_attr_read(conn, attr, buf, len, offset, value, - sizeof(*value)); -} - -static struct bt_gatt_attr bas_attrs[] = { - 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_DESCRIPTOR(BT_UUID_BAS_BATTERY_LEVEL, BT_GATT_PERM_READ, - read_blvl, NULL, &blvl), - BT_GATT_CCC(blvl_ccc_cfg, blvl_ccc_cfg_changed), -}; - /* Cycling Speed and Cadence Service declaration */ static struct bt_gatt_ccc_cfg csc_meas_ccc_cfg[CONFIG_BLUETOOTH_MAX_PAIRED]; @@ -421,7 +393,7 @@ static void bt_ready(int err) printk("Bluetooth initialized\n"); gap_init(DEVICE_NAME, CSC_APPEARANCE); - bt_gatt_register(bas_attrs, ARRAY_SIZE(bas_attrs)); + bas_init(); dis_init(CONFIG_SOC, "ACME"); bt_gatt_register(csc_attrs, ARRAY_SIZE(csc_attrs)); @@ -460,17 +432,6 @@ void main(void) } /* Battery level simulation */ - if (bas_simulate) { - blvl -= 1; - - if (!blvl) { - /* Software eco battery charger */ - blvl = 100; - } - - bt_gatt_notify(NULL, &bas_attrs[2], &blvl, - sizeof(blvl)); - } - + bas_notify(); } } diff --git a/samples/bluetooth/peripheral_esp/src/Makefile b/samples/bluetooth/peripheral_esp/src/Makefile index fb0b81f5c..1f76bcbd1 100644 --- a/samples/bluetooth/peripheral_esp/src/Makefile +++ b/samples/bluetooth/peripheral_esp/src/Makefile @@ -1,3 +1,3 @@ ccflags-y +=-I${srctree}/samples/bluetooth -obj-y = main.o ../../gatt/gap.o ../../gatt/dis.o +obj-y = main.o ../../gatt/gap.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 937846225..30af84fd7 100644 --- a/samples/bluetooth/peripheral_esp/src/main.c +++ b/samples/bluetooth/peripheral_esp/src/main.c @@ -33,6 +33,7 @@ #include <gatt/gap.h> #include <gatt/dis.h> +#include <gatt/bas.h> #define DEVICE_NAME "ESP peripheral" #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1) @@ -86,35 +87,6 @@ static ssize_t read_u16(struct bt_conn *conn, const struct bt_gatt_attr *attr, sizeof(value)); } -/* Battery Service Declaration */ - -static struct bt_gatt_ccc_cfg blvl_ccc_cfg[CONFIG_BLUETOOTH_MAX_PAIRED] = {}; -static bool simulate_blvl; -static uint8_t blvl = 100; - -static void blvl_ccc_cfg_changed(uint16_t value) -{ - simulate_blvl = value == BT_GATT_CCC_NOTIFY; -} - -static ssize_t read_blvl(struct bt_conn *conn, const struct bt_gatt_attr *attr, - void *buf, uint16_t len, uint16_t offset) -{ - const uint8_t *value = attr->user_data; - - return bt_gatt_attr_read(conn, attr, buf, len, offset, value, - sizeof(*value)); -} - -static struct bt_gatt_attr bas_attrs[] = { - 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_DESCRIPTOR(BT_UUID_BAS_BATTERY_LEVEL, BT_GATT_PERM_READ, - read_blvl, NULL, &blvl), - BT_GATT_CCC(blvl_ccc_cfg, blvl_ccc_cfg_changed), -}; - /* Environmental Sensing Service Declaration */ struct es_measurement { @@ -503,7 +475,7 @@ static void bt_ready(int err) gap_init(DEVICE_NAME, APPEARANCE_THERMOMETER); bt_gatt_register(ess_attrs, ARRAY_SIZE(ess_attrs)); - bt_gatt_register(bas_attrs, ARRAY_SIZE(bas_attrs)); + bas_init(); dis_init(CONFIG_SOC, "ACME"); err = bt_le_adv_start(BT_LE_ADV(BT_LE_ADV_IND), ad, ARRAY_SIZE(ad), @@ -566,16 +538,6 @@ void main(void) } /* Battery level simulation */ - if (simulate_blvl) { - blvl -= 1; - - if (!blvl) { - /* Software eco battery charger */ - blvl = 100; - } - - bt_gatt_notify(NULL, &bas_attrs[2], &blvl, - sizeof(blvl)); - } + bas_notify(); } } diff --git a/samples/bluetooth/peripheral_hr/src/Makefile b/samples/bluetooth/peripheral_hr/src/Makefile index 9d95b5a4b..040c4c91b 100644 --- a/samples/bluetooth/peripheral_hr/src/Makefile +++ b/samples/bluetooth/peripheral_hr/src/Makefile @@ -1,3 +1,4 @@ ccflags-y +=-I${srctree}/samples/bluetooth -obj-y = main.o ../../gatt/gap.o ../../gatt/hrs.o ../../gatt/dis.o +obj-y = main.o ../../gatt/gap.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 b2de74ee6..14a1bb4c0 100644 --- a/samples/bluetooth/peripheral_hr/src/main.c +++ b/samples/bluetooth/peripheral_hr/src/main.c @@ -33,6 +33,7 @@ #include <gatt/gap.h> #include <gatt/hrs.h> #include <gatt/dis.h> +#include <gatt/bas.h> #define DEVICE_NAME "Zephyr Heartrate Sensor" #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1) @@ -40,34 +41,6 @@ struct bt_conn *default_conn; -static struct bt_gatt_ccc_cfg blvl_ccc_cfg[CONFIG_BLUETOOTH_MAX_PAIRED] = {}; -static uint8_t simulate_blvl; -static uint8_t battery = 100; - -static void blvl_ccc_cfg_changed(uint16_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, uint16_t len, uint16_t offset) -{ - const char *value = attr->user_data; - - return bt_gatt_attr_read(conn, attr, buf, len, offset, value, - sizeof(*value)); -} - -/* Battery Service Declaration */ -static struct bt_gatt_attr bas_attrs[] = { - 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_DESCRIPTOR(BT_UUID_BAS_BATTERY_LEVEL, BT_GATT_PERM_READ, - read_blvl, NULL, &battery), - BT_GATT_CCC(blvl_ccc_cfg, blvl_ccc_cfg_changed), -}; - static const struct bt_data ad[] = { BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)), BT_DATA_BYTES(BT_DATA_UUID16_ALL, 0x0d, 0x18, 0x0f, 0x18, 0x05, 0x18), @@ -113,7 +86,7 @@ static void bt_ready(int err) gap_init(DEVICE_NAME, HEART_RATE_APPEARANCE); hrs_init(0x01); - bt_gatt_register(bas_attrs, ARRAY_SIZE(bas_attrs)); + bas_init(); dis_init(CONFIG_SOC, "Manufacturer"); err = bt_le_adv_start(BT_LE_ADV(BT_LE_ADV_IND), ad, ARRAY_SIZE(ad), @@ -166,16 +139,6 @@ void main(void) hrs_notify(); /* Battery level simulation */ - if (simulate_blvl) { - battery -= 1; - - if (!battery) { - /* Software eco battery charger */ - battery = 100; - } - - bt_gatt_notify(default_conn, &bas_attrs[2], - &battery, sizeof(battery)); - } + bas_notify(); } } |