From 4c151ab67c175c21cce1526ea1be7d8682b10714 Mon Sep 17 00:00:00 2001 From: Anuj Pathak Date: Thu, 22 Aug 2024 15:31:30 +0530 Subject: [PATCH] samples: bluetooth: peripheral: updated project to use cts from tree - removed custom cts implementation - enabled CTS through proj.conf and used it from the tree Signed-off-by: Anuj Pathak --- samples/bluetooth/peripheral/CMakeLists.txt | 1 - samples/bluetooth/peripheral/prj.conf | 2 + samples/bluetooth/peripheral/src/cts.c | 109 -------------------- samples/bluetooth/peripheral/src/cts.h | 20 ---- samples/bluetooth/peripheral/src/main.c | 65 ++++++++++-- 5 files changed, 60 insertions(+), 137 deletions(-) delete mode 100644 samples/bluetooth/peripheral/src/cts.c delete mode 100644 samples/bluetooth/peripheral/src/cts.h diff --git a/samples/bluetooth/peripheral/CMakeLists.txt b/samples/bluetooth/peripheral/CMakeLists.txt index 175b480068f..1ebc2d7c4e4 100644 --- a/samples/bluetooth/peripheral/CMakeLists.txt +++ b/samples/bluetooth/peripheral/CMakeLists.txt @@ -6,5 +6,4 @@ project(peripheral) target_sources(app PRIVATE src/main.c - src/cts.c ) diff --git a/samples/bluetooth/peripheral/prj.conf b/samples/bluetooth/peripheral/prj.conf index 5d577265d6e..f788fa670aa 100644 --- a/samples/bluetooth/peripheral/prj.conf +++ b/samples/bluetooth/peripheral/prj.conf @@ -11,6 +11,8 @@ CONFIG_BT_ATT_PREPARE_COUNT=5 CONFIG_BT_BAS=y CONFIG_BT_HRS=y CONFIG_BT_IAS=y +CONFIG_BT_CTS=y +CONFIG_BT_CTS_HELPER_API=y CONFIG_BT_PRIVACY=y CONFIG_BT_DEVICE_NAME="Zephyr Peripheral Sample Long Name" CONFIG_BT_DEVICE_APPEARANCE=833 diff --git a/samples/bluetooth/peripheral/src/cts.c b/samples/bluetooth/peripheral/src/cts.c deleted file mode 100644 index 20b53177d6d..00000000000 --- a/samples/bluetooth/peripheral/src/cts.c +++ /dev/null @@ -1,109 +0,0 @@ -/** @file - * @brief CTS Service sample - */ - -/* - * Copyright (c) 2016 Intel Corporation - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -static uint8_t ct[10]; -static uint8_t ct_update; - -static void ct_ccc_cfg_changed(const struct bt_gatt_attr *attr, 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 flags) -{ - 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 = 1U; - - return len; -} - -/* Current Time Service Declaration */ -BT_GATT_SERVICE_DEFINE(cts_cvs, - 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_PERM_READ | BT_GATT_PERM_WRITE, - read_ct, write_ct, ct), - BT_GATT_CCC(ct_ccc_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE), -); - -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] = 5U; /* months starting from 1 */ - buf[3] = 30U; /* day */ - buf[4] = 12U; /* hours */ - buf[5] = 45U; /* minutes */ - buf[6] = 30U; /* seconds */ - - /* 'Day of Week' part of 'Day Date Time' */ - buf[7] = 1U; /* day of week starting from 1 */ - - /* 'Fractions 256 part of 'Exact Time 256' */ - buf[8] = 0U; - - /* Adjust reason */ - buf[9] = 0U; /* No update, change, etc */ -} - -void cts_init(void) -{ - /* Simulate current time for Current Time Service */ - generate_current_time(ct); -} - -void cts_notify(void) -{ /* Current Time Service updates only when time is changed */ - if (!ct_update) { - return; - } - - ct_update = 0U; - bt_gatt_notify(NULL, &cts_cvs.attrs[1], &ct, sizeof(ct)); -} diff --git a/samples/bluetooth/peripheral/src/cts.h b/samples/bluetooth/peripheral/src/cts.h deleted file mode 100644 index 6e3cca03a0b..00000000000 --- a/samples/bluetooth/peripheral/src/cts.h +++ /dev/null @@ -1,20 +0,0 @@ -/** @file - * @brief CTS Service sample - */ - -/* - * Copyright (c) 2016 Intel Corporation - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#ifdef __cplusplus -extern "C" { -#endif - -void cts_init(void); -void cts_notify(void); - -#ifdef __cplusplus -} -#endif diff --git a/samples/bluetooth/peripheral/src/main.c b/samples/bluetooth/peripheral/src/main.c index d645ad3321d..7c133868361 100644 --- a/samples/bluetooth/peripheral/src/main.c +++ b/samples/bluetooth/peripheral/src/main.c @@ -22,11 +22,10 @@ #include #include #include +#include #include #include -#include "cts.h" - /* Custom Service Variables */ #define BT_UUID_CUSTOM_SERVICE_VAL \ BT_UUID_128_ENCODE(0x12345678, 0x1234, 0x5678, 0x1234, 0x56789abcdef0) @@ -285,8 +284,6 @@ static void bt_ready(void) printk("Bluetooth initialized\n"); - cts_init(); - if (IS_ENABLED(CONFIG_SETTINGS)) { settings_load(); } @@ -350,6 +347,55 @@ static void hrs_notify(void) bt_hrs_notify(heartrate); } +/** + * variable to hold reference milliseconds to epoch when device booted + * this is only for demo purpose, for more precise synchronization please + * review clock_settime API implementation. + */ +static int64_t unix_ms_ref; +static bool cts_notification_enabled; + +void bt_cts_notification_changed(bool enabled) +{ + cts_notification_enabled = enabled; +} + +int bt_cts_cts_time_write(struct bt_cts_time_format *cts_time) +{ + int err; + int64_t unix_ms; + + if (IS_ENABLED(CONFIG_BT_CTS_HELPER_API)) { + err = bt_cts_time_to_unix_ms(cts_time, &unix_ms); + if (err) { + return err; + } + } else { + return -ENOTSUP; + } + + /* recalculate reference value */ + unix_ms_ref = unix_ms - k_uptime_get(); + return 0; +} + +int bt_cts_fill_current_cts_time(struct bt_cts_time_format *cts_time) +{ + int64_t unix_ms = unix_ms_ref + k_uptime_get(); + + if (IS_ENABLED(CONFIG_BT_CTS_HELPER_API)) { + return bt_cts_time_from_unix_ms(cts_time, unix_ms); + } else { + return -ENOTSUP; + } +} + +const struct bt_cts_cb cts_cb = { + .notification_changed = bt_cts_notification_changed, + .cts_time_write = bt_cts_cts_time_write, + .fill_current_cts_time = bt_cts_fill_current_cts_time, +}; + int main(void) { struct bt_gatt_attr *vnd_ind_attr; @@ -363,6 +409,7 @@ int main(void) } bt_ready(); + bt_cts_init(&cts_cb); bt_gatt_cb_register(&gatt_callbacks); bt_conn_auth_cb_register(&auth_cb_display); @@ -378,9 +425,13 @@ int main(void) while (1) { k_sleep(K_SECONDS(1)); - /* Current Time Service updates only when time is changed */ - cts_notify(); - + /* Current time update notification example + * For testing purposes, we send a manual update notification every second. + * In production `bt_cts_send_notification` should only be used when time is changed + */ + if (cts_notification_enabled) { + bt_cts_send_notification(BT_CTS_UPDATE_REASON_MANUAL); + } /* Heartrate measurements simulation */ hrs_notify();