Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added API to get UUID, use adv data in the uptime sample #3

Merged
merged 7 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/build_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
env:
CMAKE_PREFIX_PATH: /opt/toolchains
ZEPHYR_VERSION: 3.6.0
# Board to use with renode
BOARD: nrf52840dk_nrf52840
steps:
- name: Checkout
Expand All @@ -25,22 +26,31 @@ jobs:
west init --mr v$ZEPHYR_VERSION
west update -o=--depth=1 -n

# Sample as DUT for testing with Renode
- name: Build Uptime Sample
working-directory: /tmp/
run: |
west build $GITHUB_WORKSPACE/samples/uptime -b $BOARD --build-dir $GITHUB_WORKSPACE/samples/uptime/build

# BLE Central for testing with Renode
- name: Build Central
working-directory: /tmp/
run: |
west build $GITHUB_WORKSPACE/tests/renode/ble_central -b $BOARD --build-dir $GITHUB_WORKSPACE/tests/renode/ble_central/build

# Posix build for debugging with bluetooth virtual controller
- name: Build Posix
working-directory: /tmp/
run: |
west build $GITHUB_WORKSPACE/samples/uptime -b native_posix_64 --build-dir $GITHUB_WORKSPACE/samples/uptime/build_posix -- -DOVERLAY_CONFIG=$GITHUB_WORKSPACE/samples/uptime/boards/native_posix_64.conf

- name : Upload Firmware
uses: actions/upload-artifact@v4
with:
name: zephyr-build
path: |
samples/uptime/build/zephyr/zephyr.elf
samples/uptime/build_posix/zephyr/zephyr.exe
tests/renode/ble_central/build/zephyr/zephyr.elf

test:
Expand Down
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
2 changes: 1 addition & 1 deletion include/ble_utils/uuid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static constexpr bt_uuid_128 uuid128_init(uint32_t w32,uint16_t w1,uint16_t w2,u
const uint8_t b15 = static_cast<uint8_t>(w32 >> 24 & 0xFF);
bt_uuid_128 uuid
{
.uuid = BT_UUID_TYPE_128,
.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;
Expand Down
3 changes: 3 additions & 0 deletions samples/boards/native_posix_64.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Disable HCI ACL for use with Android Emulator
# Refer to https://github.com/google/bumble/issues/220#issuecomment-1651200456
CONFIG_BT_HCI_ACL_FLOW_CONTROL=n
2 changes: 1 addition & 1 deletion samples/uptime/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ CONFIG_NEWLIB_LIBC=y
CONFIG_BLE_UTILS=y
CONFIG_BT_ASSERT=n
CONFIG_LOG=y
CONFIG_BT_HCI_ACL_FLOW_CONTROL=n
CONFIG_BT_EXT_ADV=y
67 changes: 55 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,56 @@ 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;
uint8_t adv_type;
switch(uuid->type){
case BT_UUID_TYPE_16:
data_len = 2;
adv_type = BT_DATA_UUID16_SOME;
p_data = (const uint8_t *)&((bt_uuid_16 *)uuid)->val;
break;
case BT_UUID_TYPE_32:
data_len = 4;
adv_type = BT_DATA_UUID32_SOME;
p_data = (const uint8_t *)&((bt_uuid_32 *)uuid)->val;
break;
case BT_UUID_TYPE_128:
data_len = 16;
adv_type = BT_DATA_UUID128_SOME;
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 = adv_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
40 changes: 15 additions & 25 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 All @@ -203,9 +193,9 @@ static void device_found_cb(const bt_addr_le_t *addr, int8_t rssi, uint8_t type,
//LOG_INF("[DEVICE]: %s, AD evt type %u, AD data len %u, RSSI %i",
// dev, type, ad->len, rssi);

/* We're only interested in connectable events */
if (type == BT_GAP_ADV_TYPE_ADV_IND ||
type == BT_GAP_ADV_TYPE_ADV_DIRECT_IND) {
type == BT_GAP_ADV_TYPE_ADV_DIRECT_IND ||
type == BT_GAP_ADV_TYPE_SCAN_RSP) {
bt_data_parse(ad, adv_data_cb, (void *)addr);
}
}
Expand Down
7 changes: 5 additions & 2 deletions tests/renode/uptime_test.robot
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
*** Copyright (c) 2024, Victor Chavez ***
*** SPDX-License-Identifier: Apache-2.0 ***
*** Comments ***
Copyright (c) 2024, Victor Chavez
SPDX-License-Identifier: Apache-2.0

*** Variables ***
${UART} sysbus.uart0

Expand Down Expand Up @@ -51,6 +53,7 @@ Uptime Demo
Wait For Line On Uart Booting Zephyr testerId=${per_uart}
Wait For Line On Uart Scanning successfully started testerId=${cen_uart}
Wait For Line On Uart Bluetooth initialized testerId=${per_uart}
Wait For Line On Uart Matched Uptime adv. UUID testerId=${cen_uart}
Wait For Line On Uart Connected testerId=${per_uart}
Wait For Line On Uart Connected testerId=${cen_uart}
Wait For Line On Uart Service found testerId=${cen_uart}
Expand Down
Loading