From 924134f6793a45d8b9e879ce3411f91676c22ea3 Mon Sep 17 00:00:00 2001 From: Victor Chavez Date: Thu, 15 Aug 2024 10:58:10 +0200 Subject: [PATCH] Minor formatting and email update --- include/ble_utils/ble_utils.hpp | 13 ++-- include/ble_utils/uuid.hpp | 106 +++++++++++++++----------------- src/ble_utils.cpp | 20 +++--- 3 files changed, 64 insertions(+), 75 deletions(-) diff --git a/include/ble_utils/ble_utils.hpp b/include/ble_utils/ble_utils.hpp index 154c2f9..880257d 100644 --- a/include/ble_utils/ble_utils.hpp +++ b/include/ble_utils/ble_utils.hpp @@ -118,9 +118,9 @@ class Characteristic Characteristic(const bt_uuid * uuid,uint8_t props,uint8_t perm,bool ccc_enable); static ssize_t _read_cb(struct bt_conn *conn, - const struct bt_gatt_attr *attr, - void *buf, uint16_t len, - uint16_t offset); + const struct bt_gatt_attr *attr, + void *buf, uint16_t len, + uint16_t offset); static ssize_t _write_cb(struct bt_conn *conn, const struct bt_gatt_attr *attr, @@ -282,7 +282,7 @@ class Service * * @param service_uuid UUID assigned to the service */ - Service(const bt_uuid *service_uuid); + Service(const bt_uuid *service_uuid); /** * @brief Register a characteristic to the service @@ -290,7 +290,7 @@ class Service * * @param chrc Pointer to characteristic object */ - void register_char(const Characteristic * chrc); + void register_char(const Characteristic * chrc); /** * @brief Initialize the BLE Service @@ -300,7 +300,6 @@ class Service */ int init(); - /** * @brief Get the UUID of the service * @@ -312,7 +311,7 @@ class Service static constexpr uint8_t MAX_ATTR = CONFIG_BLE_UTILS_MAX_ATTR; /*! @brief Total attributes (i.e. bt_gatt_attr) - * required to represent a BLE Service. + * required to represent a BLE Service. * @details This value is obtained from the zephyr macro BT_GATT_SERVICE_DEFINE. */ static constexpr uint8_t SVC_ATTR_SIZE = 1; diff --git a/include/ble_utils/uuid.hpp b/include/ble_utils/uuid.hpp index 25910e2..09bdb94 100644 --- a/include/ble_utils/uuid.hpp +++ b/include/ble_utils/uuid.hpp @@ -1,8 +1,8 @@ /*!***************************************************************** -* Copyright 2023, Victor Chavez +* Copyright 2023-2024 Victor Chavez * SPDX-License-Identifier: Apache-2.0 * @file uuid.hpp -* @author Victor Chavez (chavez-bermudez@fh-aachen.de) +* @author Victor Chavez (vchavezb@protonmail.com) * * @brief * Utilities to generate Zephyr 128 bit UUIDs @@ -21,21 +21,18 @@ namespace ble_utils::uuid { /** @brief Get the raw bytes of a bt_uuid_128 datatype @details Useful to initialize uuid in adv. data.
- Example:
- bt_uuid_128 my_uuid; - static constexpr bt_data adv_data[] = - { - BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL)), - BT_DATA_BYTES(BT_DATA_UUID128_ALL, - RAW_UUID_128(my_uuid)), - } - }; - @param UUID_128 128-bit UUID bt_uuid_128 struct + Example:
+ bt_uuid_128 my_uuid; + static constexpr bt_data adv_data[] = { + BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL)), + BT_DATA_BYTES(BT_DATA_UUID128_ALL, RAW_UUID_128(my_uuid)), + }; + @param UUID_128 128-bit UUID bt_uuid_128 struct */ -#define RAW_UUID_128(UUID_128) UUID_128.val[0],UUID_128.val[1],UUID_128.val[2],UUID_128.val[3], \ - UUID_128.val[4],UUID_128.val[5],UUID_128.val[6],UUID_128.val[7], \ - UUID_128.val[8],UUID_128.val[9],UUID_128.val[10],UUID_128.val[11], \ - UUID_128.val[12],UUID_128.val[13],UUID_128.val[14],UUID_128.val[15] +#define RAW_UUID_128(UUID_128) UUID_128.val[0],UUID_128.val[1],UUID_128.val[2],UUID_128.val[3], \ + UUID_128.val[4],UUID_128.val[5],UUID_128.val[6],UUID_128.val[7], \ + UUID_128.val[8],UUID_128.val[9],UUID_128.val[10],UUID_128.val[11], \ + UUID_128.val[12],UUID_128.val[13],UUID_128.val[14],UUID_128.val[15] /** * @brief Initialize a 128 bit UUID @@ -49,54 +46,53 @@ namespace ble_utils::uuid */ static constexpr bt_uuid_128 uuid128_init(uint32_t w32,uint16_t w1,uint16_t w2,uint16_t w3,uint64_t w48) { - const uint8_t b0 = static_cast(w48 & 0xFF); - const uint8_t b1 = static_cast(w48 >>8 & 0xFF); - const uint8_t b2 = static_cast(w48 >>16 & 0xFF); - const uint8_t b3 = static_cast(w48 >>24 & 0xFF); - const uint8_t b4 = static_cast(w48 >>32 & 0xFF); - const uint8_t b5 = static_cast(w48 >>40 & 0xFF); - const uint8_t b6 = static_cast(w3 & 0xFF); - const uint8_t b7 = static_cast(w3 >>8 & 0xFF); - const uint8_t b8 = static_cast(w2 & 0xFF); - const uint8_t b9 = static_cast(w2 >> 8 & 0xFF); - const uint8_t b10 = static_cast(w1 & 0xFF); - const uint8_t b11 = static_cast(w1 >> 8 & 0xFF); - const uint8_t b12 = static_cast(w32 & 0xFF); - const uint8_t b13 = static_cast(w32 >> 8 & 0xFF); - const uint8_t b14 = static_cast(w32 >>16 & 0xFF); - const uint8_t b15 = static_cast(w32 >> 24 & 0xFF); - bt_uuid_128 uuid - { - .uuid = {.type = BT_UUID_TYPE_128}, - .val = {b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15} - }; - return uuid; + const uint8_t b0 = static_cast(w48 & 0xFF); + const uint8_t b1 = static_cast(w48 >>8 & 0xFF); + const uint8_t b2 = static_cast(w48 >>16 & 0xFF); + const uint8_t b3 = static_cast(w48 >>24 & 0xFF); + const uint8_t b4 = static_cast(w48 >>32 & 0xFF); + const uint8_t b5 = static_cast(w48 >>40 & 0xFF); + const uint8_t b6 = static_cast(w3 & 0xFF); + const uint8_t b7 = static_cast(w3 >>8 & 0xFF); + const uint8_t b8 = static_cast(w2 & 0xFF); + const uint8_t b9 = static_cast(w2 >> 8 & 0xFF); + const uint8_t b10 = static_cast(w1 & 0xFF); + const uint8_t b11 = static_cast(w1 >> 8 & 0xFF); + const uint8_t b12 = static_cast(w32 & 0xFF); + const uint8_t b13 = static_cast(w32 >> 8 & 0xFF); + const uint8_t b14 = static_cast(w32 >>16 & 0xFF); + const uint8_t b15 = static_cast(w32 >> 24 & 0xFF); + bt_uuid_128 uuid { + .uuid = {.type = BT_UUID_TYPE_128}, + .val = {b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15} + }; + return uuid; } /*! - * @brief Create a Derived UUID from a Base UUID - * - * @param [in] base The 128 bit uuid base - * @param [in] uuid_short The 16 bit short uuid - * @details Replaces the 2 bytes previous to the end of the base UUID - Example - - Base UUID 6E400000-B5A3-F393-E0A9-E50E24DCCA9E - Derived UUID 6E40xxxx-B5A3-F393-E0A9-E50E24DCCA9E + * @brief Create a Derived UUID from a Base UUID + * + * @param [in] base The 128 bit uuid base + * @param [in] uuid_short The 16 bit short uuid + * @details Replaces the 2 bytes previous to the end of the base UUID + Example + + Base UUID 6E400000-B5A3-F393-E0A9-E50E24DCCA9E + Derived UUID 6E40xxxx-B5A3-F393-E0A9-E50E24DCCA9E - where xxxx are the replaced bytes + where xxxx are the replaced bytes - * @retval bt_uuid_128 UUID derived from base uuid + * @retval bt_uuid_128 UUID derived from base uuid */ constexpr bt_uuid_128 derive_uuid(bt_uuid_128 base,uint16_t uuid_short) { - const uint8_t uuid_short_msb = BT_UUID_SIZE_128-3; - const uint8_t uuid_short_lsb = BT_UUID_SIZE_128-4; - bt_uuid_128 derived_uuid = base; - derived_uuid.val[uuid_short_msb] = uuid_short >> 8; - derived_uuid.val[uuid_short_lsb] = uuid_short & 0xFF; - return derived_uuid; + const uint8_t uuid_short_msb = BT_UUID_SIZE_128-3; + const uint8_t uuid_short_lsb = BT_UUID_SIZE_128-4; + bt_uuid_128 derived_uuid = base; + derived_uuid.val[uuid_short_msb] = uuid_short >> 8; + derived_uuid.val[uuid_short_lsb] = uuid_short & 0xFF; + return derived_uuid; } } // namespace ble_utils diff --git a/src/ble_utils.cpp b/src/ble_utils.cpp index 997fcfd..5102a3f 100644 --- a/src/ble_utils.cpp +++ b/src/ble_utils.cpp @@ -1,8 +1,8 @@ /*!***************************************************************** -* Copyright 2023, Victor Chavez +* Copyright 2023-2024 Victor Chavez * SPDX-License-Identifier: Apache-2.0 * @file ble_utils.cpp -* @author Victor Chavez (chavez-bermudez@fh-aachen.de) +* @author Victor Chavez (vchavezb@protonmail.com) ********************************************************************/ #include @@ -91,8 +91,7 @@ Service::Service(const bt_uuid *uuid): } ) { - const bt_gatt_attr svc_attr = - { + const bt_gatt_attr svc_attr = { .uuid = static_cast(static_cast(&uuid::PRIMARY_SVC)), .read = bt_gatt_attr_read_service, .write = nullptr, @@ -113,8 +112,7 @@ void Service::register_char(const Characteristic * chrc) __ASSERT(req_size <= MAX_ATTR, "Max. attribute size reached"); attrs[m_gatt_service.attr_count++] = chrc->m_attr; attrs[m_gatt_service.attr_count++] = chrc->m_attr_value; - if(chrc->m_ccc_enable) - { + if (chrc->m_ccc_enable) { auto char_ccc = static_cast(chrc); attrs[m_gatt_service.attr_count++] = char_ccc->m_ccc_attr; } @@ -169,12 +167,9 @@ void ICharacteristicCCC::_ccc_changed(const bt_gatt_attr *attr, uint16_t value) { auto ccc_data = static_cast(attr->user_data); auto instance = static_cast(ccc_data->ctx); - if(value > BT_GATT_CCC_INDICATE) - { + if (value > BT_GATT_CCC_INDICATE) { instance->ccc_changed(CCCValue_e::NA); - } - else - { + } else { const CCCValue_e ccc_value = static_cast(value); instance->ccc_changed(ccc_value); } @@ -223,8 +218,7 @@ int CharacteristicIndicate::indicate(const void * data, const uint16_t len) { indicate_params.data = data; indicate_params.len = len; - const int gatt_res = bt_gatt_indicate(nullptr, - &indicate_params); + const int gatt_res = bt_gatt_indicate(nullptr, &indicate_params); return gatt_res; }