Skip to content

Commit

Permalink
Minor formatting and email update
Browse files Browse the repository at this point in the history
  • Loading branch information
vChavezB committed Aug 15, 2024
1 parent a3b7fa5 commit 924134f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 75 deletions.
13 changes: 6 additions & 7 deletions include/ble_utils/ble_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -282,15 +282,15 @@ 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
* @details should be called before @ref init
*
* @param chrc Pointer to characteristic object
*/
void register_char(const Characteristic * chrc);
void register_char(const Characteristic * chrc);

/**
* @brief Initialize the BLE Service
Expand All @@ -300,7 +300,6 @@ class Service
*/
int init();


/**
* @brief Get the UUID of the service
*
Expand All @@ -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;
Expand Down
106 changes: 51 additions & 55 deletions include/ble_utils/uuid.hpp
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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. <br>
Example: <br>
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: <br>
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
Expand All @@ -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<uint8_t>(w48 & 0xFF);
const uint8_t b1 = static_cast<uint8_t>(w48 >>8 & 0xFF);
const uint8_t b2 = static_cast<uint8_t>(w48 >>16 & 0xFF);
const uint8_t b3 = static_cast<uint8_t>(w48 >>24 & 0xFF);
const uint8_t b4 = static_cast<uint8_t>(w48 >>32 & 0xFF);
const uint8_t b5 = static_cast<uint8_t>(w48 >>40 & 0xFF);
const uint8_t b6 = static_cast<uint8_t>(w3 & 0xFF);
const uint8_t b7 = static_cast<uint8_t>(w3 >>8 & 0xFF);
const uint8_t b8 = static_cast<uint8_t>(w2 & 0xFF);
const uint8_t b9 = static_cast<uint8_t>(w2 >> 8 & 0xFF);
const uint8_t b10 = static_cast<uint8_t>(w1 & 0xFF);
const uint8_t b11 = static_cast<uint8_t>(w1 >> 8 & 0xFF);
const uint8_t b12 = static_cast<uint8_t>(w32 & 0xFF);
const uint8_t b13 = static_cast<uint8_t>(w32 >> 8 & 0xFF);
const uint8_t b14 = static_cast<uint8_t>(w32 >>16 & 0xFF);
const uint8_t b15 = static_cast<uint8_t>(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<uint8_t>(w48 & 0xFF);
const uint8_t b1 = static_cast<uint8_t>(w48 >>8 & 0xFF);
const uint8_t b2 = static_cast<uint8_t>(w48 >>16 & 0xFF);
const uint8_t b3 = static_cast<uint8_t>(w48 >>24 & 0xFF);
const uint8_t b4 = static_cast<uint8_t>(w48 >>32 & 0xFF);
const uint8_t b5 = static_cast<uint8_t>(w48 >>40 & 0xFF);
const uint8_t b6 = static_cast<uint8_t>(w3 & 0xFF);
const uint8_t b7 = static_cast<uint8_t>(w3 >>8 & 0xFF);
const uint8_t b8 = static_cast<uint8_t>(w2 & 0xFF);
const uint8_t b9 = static_cast<uint8_t>(w2 >> 8 & 0xFF);
const uint8_t b10 = static_cast<uint8_t>(w1 & 0xFF);
const uint8_t b11 = static_cast<uint8_t>(w1 >> 8 & 0xFF);
const uint8_t b12 = static_cast<uint8_t>(w32 & 0xFF);
const uint8_t b13 = static_cast<uint8_t>(w32 >> 8 & 0xFF);
const uint8_t b14 = static_cast<uint8_t>(w32 >>16 & 0xFF);
const uint8_t b15 = static_cast<uint8_t>(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

20 changes: 7 additions & 13 deletions src/ble_utils.cpp
Original file line number Diff line number Diff line change
@@ -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 <ble_utils/ble_utils.hpp>
Expand Down Expand Up @@ -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<const bt_uuid *>(static_cast<const void *>(&uuid::PRIMARY_SVC)),
.read = bt_gatt_attr_read_service,
.write = nullptr,
Expand All @@ -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<const ICharacteristicCCC*>(chrc);
attrs[m_gatt_service.attr_count++] = char_ccc->m_ccc_attr;
}
Expand Down Expand Up @@ -169,12 +167,9 @@ void ICharacteristicCCC::_ccc_changed(const bt_gatt_attr *attr, uint16_t value)
{
auto ccc_data = static_cast<const gatt_ccc*>(attr->user_data);
auto instance = static_cast<ICharacteristicCCC*>(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<CCCValue_e>(value);
instance->ccc_changed(ccc_value);
}
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 924134f

Please sign in to comment.