Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
plskeggs committed Jan 8, 2025
1 parent 5b5c80d commit 849e319
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 52 deletions.
41 changes: 24 additions & 17 deletions samples/cellular/nrf_cloud_ble_gateway/src/ble/ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "ui.h"

#define SEND_NOTIFY_STACK_SIZE 2048
#define SEND_NOTIFY_PRIORITY 9
#define SEND_NOTIFY_PRIORITY 5
#define SUBSCRIPTION_LIMIT 16
#define NOTIFICATION_QUEUE_LIMIT 10
#define MAX_BUF_SIZE 11000
Expand Down Expand Up @@ -234,8 +234,11 @@ void ble_register_notify_callback(notification_cb_t callback)
}

/* Thread responsible for transferring ble data over MQTT */
void send_notify_data(int unused1, int unused2, int unused3)
static void send_ble_read_data(int unused1, int unused2, int unused3)
{
ARG_UNUSED(unused1);
ARG_UNUSED(unused2);
ARG_UNUSED(unused3);
char uuid[BT_UUID_STR_LEN];
char path[BT_MAX_PATH_LEN];

Expand All @@ -251,6 +254,9 @@ void send_notify_data(int unused1, int unused2, int unused3)
if (rx_data == NULL) {
continue;
}
atomic_dec(&queued_notifications);
LOG_DBG("Queued: %ld", atomic_get(&queued_notifications));

err = ble_conn_mgr_get_conn_by_addr(rx_data->addr_trunc, &connected_ptr);
if (err) {
LOG_ERR("Connection not found for addr %s",
Expand All @@ -262,13 +268,14 @@ void send_notify_data(int unused1, int unused2, int unused3)

if (rx_data->read) {
handle = rx_data->read_params.single.handle;
LOG_INF("Read: Addr %s Handle %d",
LOG_INF("Dequeued gatt_read results, addr %s handle %d",
rx_data->addr_trunc, handle);
} else {
handle = rx_data->sub_params.value_handle;
LOG_DBG("Notify Addr %s Handle %d",
LOG_DBG("Dequeued BLE notification data, addr %s handle %d",
rx_data->addr_trunc, handle);
}
LOG_HEXDUMP_DBG(rx_data->data, rx_data->length, "notify");

err = ble_conn_mgr_get_uuid_by_handle(handle, uuid, connected_ptr);
if (err) {
Expand Down Expand Up @@ -296,8 +303,6 @@ void send_notify_data(int unused1, int unused2, int unused3)
goto cleanup;
}

LOG_HEXDUMP_DBG(rx_data->data, rx_data->length, "notify");

if (!rx_data->read && notify_callback) {
err = notify_callback(rx_data->addr_trunc, uuid,
rx_data->data, rx_data->length);
Expand Down Expand Up @@ -335,9 +340,11 @@ void send_notify_data(int unused1, int unused2, int unused3)
LOG_ERR("Unable to encode: %d", err);
goto cleanup;
}
/*
LOG_DBG("UUID %s, path %s, len %u, json %s",
uuid, path,
rx_data->length, (char *)output.data.ptr);
*/
err = g2c_send(&output.data);
k_mutex_unlock(&output.lock);
if (err) {
Expand All @@ -346,12 +353,11 @@ void send_notify_data(int unused1, int unused2, int unused3)

cleanup:
k_free(rx_data);
atomic_dec(&queued_notifications);
}
}

K_THREAD_DEFINE(ble_rec_thread, SEND_NOTIFY_STACK_SIZE,
send_notify_data, NULL, NULL, NULL,
send_ble_read_data, NULL, NULL, NULL,
SEND_NOTIFY_PRIORITY, 0, 0);

static void discovery_completed(struct bt_gatt_dm *disc, void *ctx)
Expand Down Expand Up @@ -460,8 +466,6 @@ static uint8_t gatt_read_callback(struct bt_conn *conn, uint8_t err,

bt_to_upper(addr_trunc, BT_ADDR_LE_STR_LEN);

LOG_INF("Read Addr %s", addr_trunc);

struct rec_data_t read_data = {
.fifo_reserved = NULL,
.length = length,
Expand All @@ -488,6 +492,8 @@ static uint8_t gatt_read_callback(struct bt_conn *conn, uint8_t err,
atomic_inc(&queued_notifications);
memcpy(mem_ptr, &read_data, size);
k_fifo_put(&rec_fifo, mem_ptr);
LOG_DBG("Enqueued gatt_read results %ld",
atomic_get(&queued_notifications));
}
}

Expand Down Expand Up @@ -631,10 +637,11 @@ int gatt_write_without_response(char *ble_addr, char *chrc_uuid, uint8_t *data,

err = bt_gatt_write_without_response(conn, handle, data, data_len,
false);
bt_conn_unref(conn);
return err;
}

static uint8_t on_received(struct bt_conn *conn,
static uint8_t on_notification_received(struct bt_conn *conn,
struct bt_gatt_subscribe_params *params,
const void *data, uint16_t length)
{
Expand Down Expand Up @@ -669,8 +676,7 @@ static uint8_t on_received(struct bt_conn *conn,

if (atomic_get(&queued_notifications) >=
NOTIFICATION_QUEUE_LIMIT) {
struct rec_data_t *rx_data = k_fifo_get(&rec_fifo,
K_NO_WAIT);
struct rec_data_t *rx_data = k_fifo_get(&rec_fifo, K_NO_WAIT);

LOG_INF("Dropping oldest message");
if (rx_data != NULL) {
Expand Down Expand Up @@ -701,6 +707,8 @@ static uint8_t on_received(struct bt_conn *conn,
atomic_inc(&queued_notifications);
memcpy(mem_ptr, &tx_data, size);
k_fifo_put(&rec_fifo, mem_ptr);
LOG_DBG("Enqueued BLE notification data %ld",
atomic_get(&queued_notifications));
}
}

Expand Down Expand Up @@ -809,7 +817,7 @@ int ble_subscribe(char *ble_addr, char *chrc_uuid, uint8_t value_type)
ble_addr, handle);
} else if (curr_subs < SUBSCRIPTION_LIMIT) {
if (conn != NULL) {
sub_param[next_sub_index].notify = on_received;
sub_param[next_sub_index].notify = on_notification_received;
sub_param[next_sub_index].value = value_type;
sub_param[next_sub_index].value_handle = handle;
sub_param[next_sub_index].ccc_handle = handle + 1;
Expand Down Expand Up @@ -1185,7 +1193,6 @@ static void connected(struct bt_conn *conn, uint8_t conn_err)
if (connection_ptr) {
ble_conn_set_connected(connection_ptr, false);
}
bt_conn_unref(conn);
return;
}

Expand All @@ -1204,7 +1211,7 @@ static void connected(struct bt_conn *conn, uint8_t conn_err)
}
}

//ui_led_set_pattern(UI_BLE_CONNECTED, PWM_DEV_1);
/* ui_led_set_pattern(UI_BLE_CONNECTED, PWM_DEV_1); */

if (!connection_ptr->connected) {
LOG_INF("Connected: %s", addr);
Expand Down Expand Up @@ -1269,7 +1276,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)
}
}

//ui_led_set_pattern(UI_BLE_DISCONNECTED, PWM_DEV_1);
/* ui_led_set_pattern(UI_BLE_DISCONNECTED, PWM_DEV_1); */

/* Start the timer to begin scanning again. */
k_timer_start(&auto_conn_start_timer, K_SECONDS(3), K_SECONDS(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,22 @@ static void process_connection(int i)
void connection_manager(int unused1, int unused2, int unused3)
{
int i;
bool printed = false;

ble_conn_mgr_init();
while (1) {

for (i = 0; i < CONFIG_BT_MAX_CONN; i++) {
/* Manager is busy. Do nothing. */
if (connected_ble_devices[i].discovering) {
LOG_DBG("Connection work busy.");
if (!printed) {
LOG_DBG("Connection work busy.");
printed = true;
}
goto end;
}
}
printed = false;

for (i = 0; i < CONFIG_BT_MAX_CONN; i++) {
process_connection(i);
Expand Down
Loading

0 comments on commit 849e319

Please sign in to comment.