diff options
author | Luiz Augusto von Dentz <luiz.von.dentz@intel.com> | 2016-03-16 17:44:47 +0200 |
---|---|---|
committer | Johan Hedberg <johan.hedberg@intel.com> | 2016-03-22 17:26:49 +0000 |
commit | 7a585ee1372ccabf722cafc930ffdc98ecf7bf3b (patch) | |
tree | 4a4f986915aea3ee80a41e28a1326afb55ede9ff | |
parent | 6d461204bfce32246bf66a04a5feee6417e94d73 (diff) |
Bluetooth: CTS: Add service sample
This adds a CTS service sample which can be shared with application
samples.
Change-Id: I38889f71f6ce8fc7b784d0d04af6c97408e2d662
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
-rw-r--r-- | samples/bluetooth/gatt/cts.c | 122 | ||||
-rw-r--r-- | samples/bluetooth/gatt/cts.h | 22 | ||||
-rw-r--r-- | samples/bluetooth/peripheral/src/Makefile | 2 | ||||
-rw-r--r-- | samples/bluetooth/peripheral/src/main.c | 83 |
4 files changed, 148 insertions, 81 deletions
diff --git a/samples/bluetooth/gatt/cts.c b/samples/bluetooth/gatt/cts.c new file mode 100644 index 000000000..4e510cedb --- /dev/null +++ b/samples/bluetooth/gatt/cts.c @@ -0,0 +1,122 @@ +/** @file + * @brief CTS 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" CTSIS, + * 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 ct_ccc_cfg[CONFIG_BLUETOOTH_MAX_PAIRED] = {}; +static uint8_t ct[10]; +static uint8_t ct_update; + +static void ct_ccc_cfg_changed(uint16_t value) +{ + /* TODO: Handle value */ +} + +static ssize_t read_ct(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(ct)); +} + +static ssize_t write_ct(struct bt_conn *conn, const struct bt_gatt_attr *attr, + const void *buf, uint16_t len, uint16_t offset) +{ + uint8_t *value = attr->user_data; + + if (offset + len > sizeof(ct)) { + return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET); + } + + memcpy(value + offset, buf, len); + ct_update = 1; + + return len; +} + +/* Current Time Service Declaration */ +static struct bt_gatt_attr attrs[] = { + BT_GATT_PRIMARY_SERVICE(BT_UUID_CTS), + BT_GATT_CHARACTERISTIC(BT_UUID_CTS_CURRENT_TIME, BT_GATT_CHRC_READ | + BT_GATT_CHRC_NOTIFY | BT_GATT_CHRC_WRITE), + BT_GATT_DESCRIPTOR(BT_UUID_CTS_CURRENT_TIME, + BT_GATT_PERM_READ | BT_GATT_PERM_WRITE, + read_ct, write_ct, ct), + BT_GATT_CCC(ct_ccc_cfg, ct_ccc_cfg_changed), +}; + +static void generate_current_time(uint8_t *buf) +{ + uint16_t year; + + /* 'Exact Time 256' contains 'Day Date Time' which contains + * 'Date Time' - characteristic contains fields for: + * year, month, day, hours, minutes and seconds. + */ + + year = sys_cpu_to_le16(2015); + memcpy(buf, &year, 2); /* year */ + buf[2] = 5; /* months starting from 1 */ + buf[3] = 30; /* day */ + buf[4] = 12; /* hours */ + buf[5] = 45; /* minutes */ + buf[6] = 30; /* seconds */ + + /* 'Day of Week' part of 'Day Date Time' */ + buf[7] = 1; /* day of week starting from 1 */ + + /* 'Fractions 256 part of 'Exact Time 256' */ + buf[8] = 0; + + /* Adjust reason */ + buf[9] = 0; /* No update, change, etc */ +} + +void cts_init(void) +{ + /* Simulate current time for Current Time Service */ + generate_current_time(ct); + + bt_gatt_register(attrs, ARRAY_SIZE(attrs)); +} + +void cts_notify(void) +{ /* Current Time Service updates only when time is changed */ + if (!ct_update) { + return; + } + + ct_update = 0; + bt_gatt_notify(NULL, &attrs[3], &ct, sizeof(ct)); +} diff --git a/samples/bluetooth/gatt/cts.h b/samples/bluetooth/gatt/cts.h new file mode 100644 index 000000000..47c42404f --- /dev/null +++ b/samples/bluetooth/gatt/cts.h @@ -0,0 +1,22 @@ +/** @file + * @brief CTS 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" CTSIS, + * 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 cts_init(void); +void cts_notify(void); diff --git a/samples/bluetooth/peripheral/src/Makefile b/samples/bluetooth/peripheral/src/Makefile index 040c4c91b..e0bbf9f07 100644 --- a/samples/bluetooth/peripheral/src/Makefile +++ b/samples/bluetooth/peripheral/src/Makefile @@ -1,4 +1,4 @@ ccflags-y +=-I${srctree}/samples/bluetooth obj-y = main.o ../../gatt/gap.o ../../gatt/hrs.o ../../gatt/dis.o \ - ../../gatt/bas.o + ../../gatt/bas.o ../../gatt/cts.o diff --git a/samples/bluetooth/peripheral/src/main.c b/samples/bluetooth/peripheral/src/main.c index a894f5f65..f35e29c72 100644 --- a/samples/bluetooth/peripheral/src/main.c +++ b/samples/bluetooth/peripheral/src/main.c @@ -34,72 +34,12 @@ #include <gatt/hrs.h> #include <gatt/dis.h> #include <gatt/bas.h> +#include <gatt/cts.h> #define DEVICE_NAME "Test peripheral" #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1) #define HEART_RATE_APPEARANCE 0x0341 -static void generate_current_time(uint8_t *buf) -{ - uint16_t year; - - /* 'Exact Time 256' contains 'Day Date Time' which contains - * 'Date Time' - characteristic contains fields for: - * year, month, day, hours, minutes and seconds. - */ - - year = sys_cpu_to_le16(2015); - memcpy(buf, &year, 2); /* year */ - buf[2] = 5; /* months starting from 1 */ - buf[3] = 30; /* day */ - buf[4] = 12; /* hours */ - buf[5] = 45; /* minutes */ - buf[6] = 30; /* seconds */ - - /* 'Day of Week' part of 'Day Date Time' */ - buf[7] = 1; /* day of week starting from 1 */ - - /* 'Fractions 256 part of 'Exact Time 256' */ - buf[8] = 0; - - /* Adjust reason */ - buf[9] = 0; /* No update, change, etc */ -} - -static struct bt_gatt_ccc_cfg ct_ccc_cfg[CONFIG_BLUETOOTH_MAX_PAIRED] = {}; - -static void ct_ccc_cfg_changed(uint16_t value) -{ - /* TODO: Handle value */ -} - -static uint8_t ct[10]; -static uint8_t ct_update = 0; - -static ssize_t read_ct(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(ct)); -} - -static ssize_t write_ct(struct bt_conn *conn, const struct bt_gatt_attr *attr, - const void *buf, uint16_t len, uint16_t offset) -{ - uint8_t *value = attr->user_data; - - if (offset + len > sizeof(ct)) { - return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET); - } - - memcpy(value + offset, buf, len); - ct_update = 1; - - return len; -} - /* Custom Service Variables */ static struct bt_uuid_128 vnd_uuid = BT_UUID_INIT_128( 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12, @@ -247,17 +187,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); -/* Current Time Service Declaration */ -static struct bt_gatt_attr cts_attrs[] = { - BT_GATT_PRIMARY_SERVICE(BT_UUID_CTS), - BT_GATT_CHARACTERISTIC(BT_UUID_CTS_CURRENT_TIME, BT_GATT_CHRC_READ | - BT_GATT_CHRC_NOTIFY | BT_GATT_CHRC_WRITE), - BT_GATT_DESCRIPTOR(BT_UUID_CTS_CURRENT_TIME, - BT_GATT_PERM_READ | BT_GATT_PERM_WRITE, - read_ct, write_ct, ct), - BT_GATT_CCC(ct_ccc_cfg, ct_ccc_cfg_changed), -}; - /* Vendor Primary Service Declaration */ static struct bt_gatt_attr vnd_attrs[] = { /* Vendor Primary Service Declaration */ @@ -334,7 +263,7 @@ static void bt_ready(int err) gap_init(DEVICE_NAME, HEART_RATE_APPEARANCE); hrs_init(0x01); bas_init(); - bt_gatt_register(cts_attrs, ARRAY_SIZE(cts_attrs)); + cts_init(); dis_init(CONFIG_SOC, "Manufacturer"); bt_gatt_register(vnd_attrs, ARRAY_SIZE(vnd_attrs)); @@ -386,9 +315,6 @@ void main(void) return; } - /* Simulate current time for Current Time Service */ - generate_current_time(ct); - bt_conn_cb_register(&conn_callbacks); bt_conn_auth_cb_register(&auth_cb_display); @@ -399,10 +325,7 @@ void main(void) task_sleep(sys_clock_ticks_per_sec); /* Current Time Service updates only when time is changed */ - if (ct_update) { - ct_update = 0; - bt_gatt_notify(NULL, &cts_attrs[3], &ct, sizeof(ct)); - } + cts_notify(); /* Heartrate measurements simulation */ hrs_notify(); |