diff options
author | Luiz Augusto von Dentz <luiz.von.dentz@intel.com> | 2016-03-16 16:04:26 +0200 |
---|---|---|
committer | Johan Hedberg <johan.hedberg@intel.com> | 2016-03-22 17:26:15 +0000 |
commit | 265fc8703a710860273c14547608a4fba4c55a9d (patch) | |
tree | 107d659d5507a3680e5f644354b6aeec3212d8db | |
parent | 97e1938db91da7d93550b86018d1f5cce8baed7a (diff) |
Bluetooth: DIS: Add service sample
This adds a DIS service sample which can be shared with application
samples.
Change-Id: I66f7a725139c8fa381275c3bda5e7c79fa3f09c8
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
-rw-r--r-- | samples/bluetooth/gatt/dis.c | 72 | ||||
-rw-r--r-- | samples/bluetooth/gatt/dis.h | 21 | ||||
-rw-r--r-- | samples/bluetooth/peripheral/src/Makefile | 2 | ||||
-rw-r--r-- | samples/bluetooth/peripheral/src/main.c | 33 | ||||
-rw-r--r-- | samples/bluetooth/peripheral_csc/src/Makefile | 2 | ||||
-rw-r--r-- | samples/bluetooth/peripheral_csc/src/main.c | 34 | ||||
-rw-r--r-- | samples/bluetooth/peripheral_dis/src/Makefile | 2 | ||||
-rw-r--r-- | samples/bluetooth/peripheral_dis/src/main.c | 25 | ||||
-rw-r--r-- | samples/bluetooth/peripheral_esp/src/Makefile | 2 | ||||
-rw-r--r-- | samples/bluetooth/peripheral_esp/src/main.c | 26 | ||||
-rw-r--r-- | samples/bluetooth/peripheral_hr/src/Makefile | 2 | ||||
-rw-r--r-- | samples/bluetooth/peripheral_hr/src/main.c | 33 |
12 files changed, 108 insertions, 146 deletions
diff --git a/samples/bluetooth/gatt/dis.c b/samples/bluetooth/gatt/dis.c new file mode 100644 index 000000000..e19d79513 --- /dev/null +++ b/samples/bluetooth/gatt/dis.c @@ -0,0 +1,72 @@ +/** @file + * @brief DIS 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 const char *dis_model; +static const char *dis_manuf; + +static ssize_t read_model(struct bt_conn *conn, + const struct bt_gatt_attr *attr, void *buf, + uint16_t len, uint16_t offset) +{ + return bt_gatt_attr_read(conn, attr, buf, len, offset, dis_model, + strlen(dis_model)); +} + +static ssize_t read_manuf(struct bt_conn *conn, + const struct bt_gatt_attr *attr, void *buf, + uint16_t len, uint16_t offset) +{ + return bt_gatt_attr_read(conn, attr, buf, len, offset, dis_manuf, + strlen(dis_manuf)); +} + +/* Device Information Service Declaration */ +static struct bt_gatt_attr attrs[] = { + BT_GATT_PRIMARY_SERVICE(BT_UUID_DIS), + BT_GATT_CHARACTERISTIC(BT_UUID_DIS_MODEL_NUMBER, BT_GATT_CHRC_READ), + BT_GATT_DESCRIPTOR(BT_UUID_DIS_MODEL_NUMBER, BT_GATT_PERM_READ, + read_model, NULL, NULL), + BT_GATT_CHARACTERISTIC(BT_UUID_DIS_MANUFACTURER_NAME, + BT_GATT_CHRC_READ), + BT_GATT_DESCRIPTOR(BT_UUID_DIS_MANUFACTURER_NAME, BT_GATT_PERM_READ, + read_manuf, NULL, NULL), +}; + +void dis_init(const char *model, const char *manuf) +{ + dis_model = model; + dis_manuf = manuf; + + bt_gatt_register(attrs, ARRAY_SIZE(attrs)); +} diff --git a/samples/bluetooth/gatt/dis.h b/samples/bluetooth/gatt/dis.h new file mode 100644 index 000000000..66e4e948f --- /dev/null +++ b/samples/bluetooth/gatt/dis.h @@ -0,0 +1,21 @@ +/** @file + * @brief DIS 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 dis_init(const char *model, const char *manuf); diff --git a/samples/bluetooth/peripheral/src/Makefile b/samples/bluetooth/peripheral/src/Makefile index 39b60e9ee..9d95b5a4b 100644 --- a/samples/bluetooth/peripheral/src/Makefile +++ b/samples/bluetooth/peripheral/src/Makefile @@ -1,3 +1,3 @@ ccflags-y +=-I${srctree}/samples/bluetooth -obj-y = main.o ../../gatt/gap.o ../../gatt/hrs.o +obj-y = main.o ../../gatt/gap.o ../../gatt/hrs.o ../../gatt/dis.o diff --git a/samples/bluetooth/peripheral/src/main.c b/samples/bluetooth/peripheral/src/main.c index b6968cbc0..a3d01aed5 100644 --- a/samples/bluetooth/peripheral/src/main.c +++ b/samples/bluetooth/peripheral/src/main.c @@ -32,6 +32,7 @@ #include <gatt/gap.h> #include <gatt/hrs.h> +#include <gatt/dis.h> #define DEVICE_NAME "Test peripheral" #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1) @@ -116,24 +117,6 @@ static ssize_t write_ct(struct bt_conn *conn, const struct bt_gatt_attr *attr, return len; } -static ssize_t read_model(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, - strlen(value)); -} - -static ssize_t read_manuf(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, - strlen(value)); -} - /* Custom Service Variables */ static struct bt_uuid_128 vnd_uuid = BT_UUID_INIT_128( 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12, @@ -302,18 +285,6 @@ static struct bt_gatt_attr cts_attrs[] = { BT_GATT_CCC(ct_ccc_cfg, ct_ccc_cfg_changed), }; -/* Device Information Service Declaration */ -static struct bt_gatt_attr dis_attrs[] = { - BT_GATT_PRIMARY_SERVICE(BT_UUID_DIS), - BT_GATT_CHARACTERISTIC(BT_UUID_DIS_MODEL_NUMBER, BT_GATT_CHRC_READ), - BT_GATT_DESCRIPTOR(BT_UUID_DIS_MODEL_NUMBER, BT_GATT_PERM_READ, - read_model, NULL, CONFIG_SOC), - BT_GATT_CHARACTERISTIC(BT_UUID_DIS_MANUFACTURER_NAME, - BT_GATT_CHRC_READ), - BT_GATT_DESCRIPTOR(BT_UUID_DIS_MANUFACTURER_NAME, BT_GATT_PERM_READ, - read_manuf, NULL, "Manufacturer"), -}; - /* Vendor Primary Service Declaration */ static struct bt_gatt_attr vnd_attrs[] = { /* Vendor Primary Service Declaration */ @@ -391,7 +362,7 @@ static void bt_ready(int err) hrs_init(0x01); bt_gatt_register(bas_attrs, ARRAY_SIZE(bas_attrs)); bt_gatt_register(cts_attrs, ARRAY_SIZE(cts_attrs)); - bt_gatt_register(dis_attrs, ARRAY_SIZE(dis_attrs)); + dis_init(CONFIG_SOC, "Manufacturer"); bt_gatt_register(vnd_attrs, ARRAY_SIZE(vnd_attrs)); err = bt_le_adv_start(BT_LE_ADV(BT_LE_ADV_IND), ad, ARRAY_SIZE(ad), diff --git a/samples/bluetooth/peripheral_csc/src/Makefile b/samples/bluetooth/peripheral_csc/src/Makefile index 7fd5a831b..fb0b81f5c 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 +obj-y = main.o ../../gatt/gap.o ../../gatt/dis.o diff --git a/samples/bluetooth/peripheral_csc/src/main.c b/samples/bluetooth/peripheral_csc/src/main.c index 1b3c8221f..806cde3a9 100644 --- a/samples/bluetooth/peripheral_csc/src/main.c +++ b/samples/bluetooth/peripheral_csc/src/main.c @@ -32,6 +32,7 @@ #include <bluetooth/gatt.h> #include <gatt/gap.h> +#include <gatt/dis.h> #define DEVICE_NAME "CSC peripheral" #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1) @@ -88,37 +89,6 @@ #define CSC_WHEEL_REV_DATA_PRESENT BIT(0) #define CSC_CRANK_REV_DATA_PRESENT BIT(1) -/* Device Information Service declaration */ - -static ssize_t read_model(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, - strlen(value)); -} - -static ssize_t read_manuf(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, - strlen(value)); -} - -static struct bt_gatt_attr dis_attrs[] = { - BT_GATT_PRIMARY_SERVICE(BT_UUID_DIS), - BT_GATT_CHARACTERISTIC(BT_UUID_DIS_MODEL_NUMBER, BT_GATT_CHRC_READ), - BT_GATT_DESCRIPTOR(BT_UUID_DIS_MODEL_NUMBER, BT_GATT_PERM_READ, - read_model, NULL, CONFIG_SOC), - BT_GATT_CHARACTERISTIC(BT_UUID_DIS_MANUFACTURER_NAME, - BT_GATT_CHRC_READ), - BT_GATT_DESCRIPTOR(BT_UUID_DIS_MANUFACTURER_NAME, BT_GATT_PERM_READ, - read_manuf, NULL, "ACME"), -}; - /* Battery Service declaration */ static struct bt_gatt_ccc_cfg blvl_ccc_cfg[CONFIG_BLUETOOTH_MAX_PAIRED] = {}; @@ -452,7 +422,7 @@ static void bt_ready(int err) gap_init(DEVICE_NAME, CSC_APPEARANCE); bt_gatt_register(bas_attrs, ARRAY_SIZE(bas_attrs)); - bt_gatt_register(dis_attrs, ARRAY_SIZE(dis_attrs)); + dis_init(CONFIG_SOC, "ACME"); bt_gatt_register(csc_attrs, ARRAY_SIZE(csc_attrs)); err = bt_le_adv_start(BT_LE_ADV(BT_LE_ADV_IND), ad, ARRAY_SIZE(ad), diff --git a/samples/bluetooth/peripheral_dis/src/Makefile b/samples/bluetooth/peripheral_dis/src/Makefile index 7fd5a831b..fb0b81f5c 100644 --- a/samples/bluetooth/peripheral_dis/src/Makefile +++ b/samples/bluetooth/peripheral_dis/src/Makefile @@ -1,3 +1,3 @@ ccflags-y +=-I${srctree}/samples/bluetooth -obj-y = main.o ../../gatt/gap.o +obj-y = main.o ../../gatt/gap.o ../../gatt/dis.o diff --git a/samples/bluetooth/peripheral_dis/src/main.c b/samples/bluetooth/peripheral_dis/src/main.c index 8e44d3ea1..4129e491c 100644 --- a/samples/bluetooth/peripheral_dis/src/main.c +++ b/samples/bluetooth/peripheral_dis/src/main.c @@ -31,33 +31,12 @@ #include <bluetooth/gatt.h> #include <gatt/gap.h> +#include <gatt/dis.h> #define DEVICE_NAME "DIS peripheral" #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1) #define APPEARANCE 0x0000 -static ssize_t read_string(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, - strlen(value)); -} - -static struct bt_gatt_attr attrs[] = { - /* Device Information Service Declaration */ - BT_GATT_PRIMARY_SERVICE(BT_UUID_DIS), - BT_GATT_CHARACTERISTIC(BT_UUID_DIS_MODEL_NUMBER, BT_GATT_CHRC_READ), - BT_GATT_DESCRIPTOR(BT_UUID_DIS_MODEL_NUMBER, BT_GATT_PERM_READ, - read_string, NULL, CONFIG_SOC), - BT_GATT_CHARACTERISTIC(BT_UUID_DIS_MANUFACTURER_NAME, - BT_GATT_CHRC_READ), - BT_GATT_DESCRIPTOR(BT_UUID_DIS_MANUFACTURER_NAME, BT_GATT_PERM_READ, - read_string, NULL, "Manufacturer"), -}; - 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, 0x0a, 0x18), @@ -103,7 +82,7 @@ void main(void) printk("Bluetooth initialized\n"); gap_init(DEVICE_NAME, APPEARANCE); - bt_gatt_register(attrs, ARRAY_SIZE(attrs)); + dis_init(CONFIG_SOC, "Manufacturer"); bt_conn_cb_register(&conn_callbacks); diff --git a/samples/bluetooth/peripheral_esp/src/Makefile b/samples/bluetooth/peripheral_esp/src/Makefile index 7fd5a831b..fb0b81f5c 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 +obj-y = main.o ../../gatt/gap.o ../../gatt/dis.o diff --git a/samples/bluetooth/peripheral_esp/src/main.c b/samples/bluetooth/peripheral_esp/src/main.c index 5fc8c39fd..937846225 100644 --- a/samples/bluetooth/peripheral_esp/src/main.c +++ b/samples/bluetooth/peripheral_esp/src/main.c @@ -32,6 +32,7 @@ #include <bluetooth/gatt.h> #include <gatt/gap.h> +#include <gatt/dis.h> #define DEVICE_NAME "ESP peripheral" #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1) @@ -75,16 +76,6 @@ static inline uint32_t le24_to_int(const uint8_t *u24) (uint32_t)u24[2] << 16); } -static ssize_t read_string(struct bt_conn *conn, - const struct bt_gatt_attr *attr, void *buf, - uint16_t len, uint16_t offset) -{ - const char *string = attr->user_data; - - return bt_gatt_attr_read(conn, attr, buf, len, offset, string, - strlen(string)); -} - static ssize_t read_u16(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset) { @@ -124,19 +115,6 @@ static struct bt_gatt_attr bas_attrs[] = { BT_GATT_CCC(blvl_ccc_cfg, blvl_ccc_cfg_changed), }; -/* Device Information Service Declaration */ - -static struct bt_gatt_attr dis_attrs[] = { - BT_GATT_PRIMARY_SERVICE(BT_UUID_DIS), - BT_GATT_CHARACTERISTIC(BT_UUID_DIS_MODEL_NUMBER, BT_GATT_CHRC_READ), - BT_GATT_DESCRIPTOR(BT_UUID_DIS_MODEL_NUMBER, BT_GATT_PERM_READ, - read_string, NULL, CONFIG_SOC), - BT_GATT_CHARACTERISTIC(BT_UUID_DIS_MANUFACTURER_NAME, - BT_GATT_CHRC_READ), - BT_GATT_DESCRIPTOR(BT_UUID_DIS_MANUFACTURER_NAME, BT_GATT_PERM_READ, - read_string, NULL, "ACME"), -}; - /* Environmental Sensing Service Declaration */ struct es_measurement { @@ -526,7 +504,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)); - bt_gatt_register(dis_attrs, ARRAY_SIZE(dis_attrs)); + dis_init(CONFIG_SOC, "ACME"); err = bt_le_adv_start(BT_LE_ADV(BT_LE_ADV_IND), ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd)); diff --git a/samples/bluetooth/peripheral_hr/src/Makefile b/samples/bluetooth/peripheral_hr/src/Makefile index 39b60e9ee..9d95b5a4b 100644 --- a/samples/bluetooth/peripheral_hr/src/Makefile +++ b/samples/bluetooth/peripheral_hr/src/Makefile @@ -1,3 +1,3 @@ ccflags-y +=-I${srctree}/samples/bluetooth -obj-y = main.o ../../gatt/gap.o ../../gatt/hrs.o +obj-y = main.o ../../gatt/gap.o ../../gatt/hrs.o ../../gatt/dis.o diff --git a/samples/bluetooth/peripheral_hr/src/main.c b/samples/bluetooth/peripheral_hr/src/main.c index af5331d09..b2de74ee6 100644 --- a/samples/bluetooth/peripheral_hr/src/main.c +++ b/samples/bluetooth/peripheral_hr/src/main.c @@ -32,6 +32,7 @@ #include <gatt/gap.h> #include <gatt/hrs.h> +#include <gatt/dis.h> #define DEVICE_NAME "Zephyr Heartrate Sensor" #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1) @@ -57,24 +58,6 @@ static ssize_t read_blvl(struct bt_conn *conn, const struct bt_gatt_attr *attr, sizeof(*value)); } -static ssize_t read_model(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, - strlen(value)); -} - -static ssize_t read_manuf(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, - strlen(value)); -} - /* Battery Service Declaration */ static struct bt_gatt_attr bas_attrs[] = { BT_GATT_PRIMARY_SERVICE(BT_UUID_BAS), @@ -85,18 +68,6 @@ static struct bt_gatt_attr bas_attrs[] = { BT_GATT_CCC(blvl_ccc_cfg, blvl_ccc_cfg_changed), }; -/* Device Information Service Declaration */ -static struct bt_gatt_attr dis_attrs[] = { - BT_GATT_PRIMARY_SERVICE(BT_UUID_DIS), - BT_GATT_CHARACTERISTIC(BT_UUID_DIS_MODEL_NUMBER, BT_GATT_CHRC_READ), - BT_GATT_DESCRIPTOR(BT_UUID_DIS_MODEL_NUMBER, BT_GATT_PERM_READ, - read_model, NULL, CONFIG_SOC), - BT_GATT_CHARACTERISTIC(BT_UUID_DIS_MANUFACTURER_NAME, - BT_GATT_CHRC_READ), - BT_GATT_DESCRIPTOR(BT_UUID_DIS_MANUFACTURER_NAME, BT_GATT_PERM_READ, - read_manuf, NULL, "Manufacturer"), -}; - 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), @@ -143,7 +114,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)); - bt_gatt_register(dis_attrs, ARRAY_SIZE(dis_attrs)); + dis_init(CONFIG_SOC, "Manufacturer"); err = bt_le_adv_start(BT_LE_ADV(BT_LE_ADV_IND), ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd)); |