Skip to content

Commit

Permalink
Initial changes to include adv data in the sample
Browse files Browse the repository at this point in the history
  • Loading branch information
vChavezB committed Apr 13, 2024
1 parent 87f6a72 commit ae849d6
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 42 deletions.
19 changes: 17 additions & 2 deletions include/ble_utils/ble_utils.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 ble_utils.hpp
* @author Victor Chavez ([email protected])
* @author Victor Chavez ([email protected])
*
* @brief
* BLE C++ Utilities for development of BLE GATT applications with the Zephyr OS
Expand Down Expand Up @@ -85,6 +85,13 @@ class Characteristic
return 0;
}

/**
* @brief Get the UUID of the characteristic
*
* @return const bt_uuid* Pointer to the UUID of the characteristic
*/
const bt_uuid * get_uuid();

private:
friend ICharacteristicCCC;
friend Service;
Expand Down Expand Up @@ -287,6 +294,14 @@ class Service
*/
int init();


/**
* @brief Get the UUID of the service
*
* @return const bt_uuid* Pointer to the UUID of the service
*/
const bt_uuid * get_uuid();

private:
static constexpr uint8_t MAX_ATTR = CONFIG_BLE_UTILS_MAX_ATTR;

Expand Down
63 changes: 51 additions & 12 deletions samples/uptime/src/ble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ LOG_MODULE_REGISTER(ble, CONFIG_LOG_DEFAULT_LEVEL);
namespace ble
{

constexpr size_t MAX_SD_SIZE {5U};
static size_t sd_cnt = 0;

static struct bt_data sd_data[MAX_SD_SIZE] = {};
static constexpr bt_le_adv_param adv_param = BT_LE_ADV_PARAM_INIT(BT_LE_ADV_OPT_CONNECTABLE |
BT_LE_ADV_OPT_USE_NAME,
BT_GAP_ADV_FAST_INT_MIN_2,
BT_GAP_ADV_FAST_INT_MAX_2,
NULL);

static constexpr bt_data adv_data[] =
{
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR))
};

static void connected(bt_conn *conn, uint8_t conn_err)
{
int err;
Expand Down Expand Up @@ -57,28 +72,52 @@ BT_CONN_CB_DEFINE(conn_callbacks) =

static int start_adv(void)
{
static constexpr bt_le_adv_param adv_param = BT_LE_ADV_PARAM_INIT(BT_LE_ADV_OPT_CONNECTABLE |
BT_LE_ADV_OPT_USE_NAME,
BT_GAP_ADV_FAST_INT_MIN_2,
BT_GAP_ADV_FAST_INT_MAX_2,
NULL);

static constexpr bt_data adv_data[] =
{
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR))
};
int err = bt_le_adv_start(&adv_param,
adv_data,
ARRAY_SIZE(adv_data),
nullptr,
0);
sd_data,
sd_cnt);

if (err) {
LOG_ERR("Failed to create advertiser set (err %d)", err);
return err;
}
return 0;
}

bool register_svc_to_scan_rsp(const bt_uuid *uuid)
{
uint8_t data_len;
const uint8_t * p_data;
switch(uuid->type){
case BT_UUID_TYPE_16:
data_len = 2;
p_data = (const uint8_t *)&((bt_uuid_16 *)uuid)->val;
break;
case BT_UUID_TYPE_32:
data_len = 4;
p_data = (const uint8_t *)&((bt_uuid_32 *)uuid)->val;
break;
case BT_UUID_TYPE_128:
data_len = 16;
p_data = (const uint8_t *)&((bt_uuid_128 *)uuid)->val[0];
break;
default:
return false;
}

if (sd_cnt < MAX_SD_SIZE) {
bt_data data = {.
type = uuid->type,
.data_len = data_len,
.data = p_data
};
sd_data[sd_cnt++] = data;
return true;
}
return false;
}

int init()
{
int err;
Expand Down
13 changes: 13 additions & 0 deletions samples/uptime/src/ble.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* - language: C++17
* - OS: Zephyr v3.2.x
********************************************************************/
#include <zephyr/bluetooth/uuid.h>

namespace ble
{
/**
Expand All @@ -19,4 +21,15 @@ namespace ble
* @return zephyr bluetooth error value
*/
int init();

/**
* @brief Registers a service to the scan response message
* when advertising.
*
* @param uuid The 128 bit uuid of the service.
* @return true on success.
* @return false when the maximum number of configurable services is reached.
*/
bool register_svc_to_scan_rsp(const bt_uuid *uuid);

} // namespace ble
4 changes: 2 additions & 2 deletions samples/uptime/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ int main(void)
{
LOG_INF("Starting Uptime BLE Utils sample");
uptime_service.init();
ble::register_svc_to_scan_rsp(uptime_service.get_uuid());
ble::init();
for (;;)
{
for (;;) {
const uint32_t uptime_ms = k_uptime_get_32();
uptime_service.update(uptime_ms/1000U);
k_sleep(K_MSEC(1000));
Expand Down
2 changes: 0 additions & 2 deletions samples/uptime/src/uptime_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ LOG_MODULE_REGISTER(uptime_svc, CONFIG_LOG_DEFAULT_LEVEL);
namespace uptime
{



namespace characteristic
{

Expand Down
16 changes: 15 additions & 1 deletion src/ble_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ Characteristic::Characteristic(const bt_uuid * uuid, uint8_t props, uint8_t perm
m_ccc_enable(ccc_enable)
{
}



const bt_uuid * Characteristic::get_uuid()
{
return m_attr_value.uuid;
}

ssize_t Characteristic::_read_cb(struct bt_conn *conn,
const struct bt_gatt_attr *attr,
void *buf, uint16_t len,
Expand Down Expand Up @@ -113,12 +121,18 @@ void Service::register_char(const Characteristic * chrc)
attrs[m_gatt_service.attr_count++] = char_ccc->m_ccc_attr;
}
}

int Service::init()
{
const int res = bt_gatt_service_register(&m_gatt_service);
return res;
}

const bt_uuid * Service::get_uuid()
{
return static_cast<const bt_uuid *>(m_gatt_service.attrs[0].user_data);
}

/**
* @brief Constructor that defines a BLE Characteristic CCC
* @details The member list initializer gives an insight on how zephyr OS Requires
Expand Down Expand Up @@ -149,8 +163,8 @@ ICharacteristicCCC::ICharacteristicCCC(const bt_uuid * uuid, uint8_t props, uint
}
)
{

}

ICharacteristicCCC::~ICharacteristicCCC() {}

void ICharacteristicCCC::_ccc_changed(const bt_gatt_attr *attr, uint16_t value)
Expand Down
36 changes: 13 additions & 23 deletions tests/renode/ble_central/src/discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,26 +148,6 @@ static bool adv_data_cb(bt_data *data, void *user_data)
{
auto addr = static_cast<bt_addr_le_t*>(user_data);
LOG_INF("Adv data type %u len %u", data->type, data->data_len);
int err = bt_le_scan_stop();
if (err) {
LOG_INF("Stop LE scan failed (err %d)", err);
return false;
}

LOG_INF("Connecting..");
err = bt_conn_le_create(addr, &conn_create_param,
&conn_default_param, &default_conn);
if (err) {
LOG_ERR("Create conn failed (err %d)", err);
start_scan();
}
return true;

/*
TODO wait for upcoming changes with uuid advertisement
until then do not compare uuid in adv data
*/
/*
switch (data->type) {
case BT_DATA_UUID128_SOME:
case BT_DATA_UUID128_ALL:
Expand All @@ -185,13 +165,23 @@ static bool adv_data_cb(bt_data *data, void *user_data)
continue;
}
LOG_INF("Matched Uptime adv. UUID");
err = bt_le_scan_stop();
if (err) {
LOG_INF("Stop LE scan failed (err %d)", err);
return false;
}

LOG_INF("Connecting..");
err = bt_conn_le_create(addr, &conn_create_param,
&conn_default_param, &default_conn);
if (err) {
LOG_ERR("Create conn failed (err %d)", err);
start_scan();
}
return false;
}
}
return true;
*/
}

static void device_found_cb(const bt_addr_le_t *addr, int8_t rssi, uint8_t type,
Expand Down

0 comments on commit ae849d6

Please sign in to comment.