Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vChavezB committed Apr 12, 2024
1 parent 4283604 commit 4cff12b
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 24 deletions.
18 changes: 10 additions & 8 deletions samples/uptime/src/ble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
* - OS: Zephyr v3.2.x
********************************************************************/
#include <zephyr/bluetooth/conn.h>
#include <zephyr/logging/log.h>
#include "ble.hpp"
LOG_MODULE_REGISTER(ble, CONFIG_LOG_DEFAULT_LEVEL);

namespace ble
{
Expand All @@ -27,24 +29,24 @@ static void connected(bt_conn *conn, uint8_t conn_err)

if (conn_err)
{
printk("Connection failed (err %d)\n", conn_err);
LOG_INF("Connection failed (err %d)", conn_err);
return;
}

err = bt_conn_get_info(conn, &info);
if (err)
{
printk("Failed to get connection info (err %d)\n", err);
LOG_ERR("Failed to get connection info (err %d)", err);
}
else
{
printk("Connected: %s\n", addr);
LOG_INF("Connected: %s\n", addr);
}
}

static void disconnected(struct bt_conn *conn, uint8_t reason)
{
printk("Disconnected (reason 0x%02x)\n", reason);
LOG_INF("Disconnected (reason 0x%02x)", reason);
}

BT_CONN_CB_DEFINE(conn_callbacks) =
Expand All @@ -71,7 +73,7 @@ static int start_adv(void)
nullptr,
0);
if (err) {
printk("Failed to create advertiser set (err %d)\n", err);
LOG_ERR("Failed to create advertiser set (err %d)", err);
return err;
}
return 0;
Expand All @@ -85,15 +87,15 @@ int init()
err = bt_enable(NULL);
if (err)
{
printk("Bluetooth init failed (err %d)\n", err);
LOG_ERR("Bluetooth init failed (err %d)", err);
break;
}

printk("Bluetooth initialized\n");
LOG_INF("Bluetooth initialized\n");
err = start_adv();
if (err)
{
printk("Advertising failed to create (err %d)\n", err);
LOG_ERR("Advertising failed to create (err %d)", err);
break;
}
}while(0);
Expand Down
7 changes: 5 additions & 2 deletions samples/uptime/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@
#include "ble.hpp"
#include <zephyr/kernel.h>
#include "uptime_service.hpp"
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(main, CONFIG_LOG_DEFAULT_LEVEL);

uptime::Service uptime_service;

int main(void)
{
printk("Starting Uptime BLE Utils sample\n");
LOG_INF("Starting Uptime BLE Utils sample");
uptime_service.init();
ble::init();
for (;;)
{
const uint32_t uptime_ms = k_uptime_get_32();
const uint32_t uptime_ms = k_uptime_get_32();
uptime_service.update(uptime_ms/1000U);
LOG_INF("Uptime: %u", uptime_ms);
k_sleep(K_MSEC(1000));
}
return 0;
Expand Down
3 changes: 2 additions & 1 deletion samples/uptime/src/uptime_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ Service::Service():
ble_utils::gatt::Service((const bt_uuid*)&uuid::svc_base)
{
register_char(&m_basic);
register_char(&m_notify);

register_char(&m_indicate);
register_char(&m_notify);
}

void Service::update(uint32_t uptime)
Expand Down
1 change: 0 additions & 1 deletion test/renode/load.resc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ emulation CreateBLEMedium "wireless"
mach add "central"
machine LoadPlatformDescription @platforms/cpus/nrf52840.repl
connector Connect sysbus.radio wireless

showAnalyzer uart0

mach create "peripheral"
Expand Down
2 changes: 1 addition & 1 deletion test/renode/uptime_central/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ CONFIG_BT=y
CONFIG_LOG=y
CONFIG_BT_CENTRAL=y
CONFIG_BT_SMP=y
CONFIG_BT_GATT_CLIENT=y
CONFIG_BT_GATT_CLIENT=y
25 changes: 14 additions & 11 deletions test/renode/uptime_central/src/discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ static constexpr uint8_t TOTAL_CHARACTERISTICS = 3;
static const bt_uuid * uptime_characteristics [TOTAL_CHARACTERISTICS] =
{
&uptime::uuid::char_basic.uuid,
&uptime::uuid::char_notify.uuid,
&uptime::uuid::char_indicate.uuid
&uptime::uuid::char_indicate.uuid,
&uptime::uuid::char_notify.uuid
};

static uint8_t uptime_notify_cb(bt_conn *conn,
Expand All @@ -60,7 +60,7 @@ static uint8_t uptime_notify_cb(bt_conn *conn,
if (!data) {
LOG_INF("[UNSUBSCRIBED]");
params->value_handle = 0U;
return BT_GATT_ITER_STOP;
return BT_GATT_ITER_CONTINUE;
}

if(length != sizeof(uint32_t)) {
Expand Down Expand Up @@ -98,6 +98,7 @@ static uint8_t service_discover_cb(bt_conn *conn,
if (!device_found) {
LOG_ERR("BLE peripheral not found");
} else {
LOG_INF("BLE peripheral found");
device_found = false;
}
device_char_cnt = 0;
Expand All @@ -113,22 +114,23 @@ static uint8_t service_discover_cb(bt_conn *conn,
} else if(char_found) {
if (bt_uuid_cmp(params->uuid,
&uptime::uuid::char_notify.uuid) == 0) {
LOG_INF("notify found");
// Subscribe to uptime notification
subscribe_params.notify = uptime_notify_cb;
subscribe_params.value = BT_GATT_CCC_NOTIFY;
subscribe_params.ccc_handle = attr->handle;
int err = bt_gatt_subscribe(conn, &subscribe_params);
if (err && err != -EALREADY) {
const int err = bt_gatt_subscribe(conn, &subscribe_params);
if (err != 0 && err != -EALREADY) {
LOG_ERR("Subscribe failed (err %d)", err);
} else {
LOG_INF("[SUBSCRIBED]");
}
}
if (device_char_cnt == TOTAL_CHARACTERISTICS-1) {
device_found = true;
LOG_INF("Characteristics found");

LOG_INF("All Chars found");
} else if (device_char_cnt < TOTAL_CHARACTERISTICS) {
LOG_INF("Char found");
device_char_cnt++;
discover_characteristics(conn, attr, device_char_cnt);
}
Expand All @@ -142,10 +144,11 @@ static bool adv_data_cb(bt_data *data, void *user_data)
{
auto addr = static_cast<bt_addr_le_t*>(user_data);
int i;
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 true;
return false;
}

LOG_INF("Connecting..");
Expand All @@ -155,7 +158,7 @@ static bool adv_data_cb(bt_data *data, void *user_data)
LOG_ERR("Create conn failed (err %d)", err);
start_scan();
}
return false;
return true;

/*
TODO wait for upcoming changes with uuid advertisement
Expand Down Expand Up @@ -194,8 +197,8 @@ static void device_found_cb(const bt_addr_le_t *addr, int8_t rssi, uint8_t type,
char dev[BT_ADDR_LE_STR_LEN];

bt_addr_le_to_str(addr, dev, sizeof(dev));
LOG_INF("[DEVICE]: %s, AD evt type %u, AD data len %u, RSSI %i",
dev, type, ad->len, rssi);
//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 ||
Expand Down
4 changes: 4 additions & 0 deletions test/renode/uptime_central/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@ int main()
LOG_ERR("Error starting scan,err %d",rc);
return rc;
}
while(1) {
LOG_INF("Sleeping");
k_sleep(K_MSEC(1000));
}
return 0;
}
2 changes: 2 additions & 0 deletions test/renode/uptime_test.robot
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ Uptime Demo
Wait For Line On Uart Booting Zephyr testerId=${cen_uart}
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}

0 comments on commit 4cff12b

Please sign in to comment.