Skip to content

Commit

Permalink
nimble: iso: Move Rx callback param to Setup ISO Data Path command
Browse files Browse the repository at this point in the history
This moves the ISO Rx callback to be provided by application as
Setup ISO Data Path function parameter, as the callback will be used
only for HCI data path, thus there is no point to require the callback
when the broadcast sync is created.
  • Loading branch information
MariuszSkamra committed Apr 29, 2024
1 parent 4ebbbf2 commit de8a76b
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 37 deletions.
70 changes: 58 additions & 12 deletions apps/btshell/src/cmd_iso.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* under the License.
*/

#include "host/ble_hs.h"
#include "host/ble_iso.h"

#include "cmd_iso.h"
Expand All @@ -26,20 +27,68 @@

#if (MYNEWT_VAL(BLE_ISO))
static struct iso_rx_stats {
uint8_t bis_index;
uint16_t conn_handle;
bool ts_valid;
uint32_t ts;
uint16_t seq_num;
uint64_t total_cnt;
uint64_t valid_cnt;
uint64_t error_cnt;
uint64_t lost_cnt;
} rx_stats_pool[MYNEWT_VAL(BLE_ISO_MAX_BISES)];
} rx_stats_pool[MYNEWT_VAL(BLE_ISO_MAX_BISES)] = {
[0 ... MYNEWT_VAL(BLE_ISO_MAX_BISES) - 1] = {
.conn_handle = BLE_HS_CONN_HANDLE_NONE
}
};

static struct iso_rx_stats *
iso_rx_stats_lookup_conn_handle(uint16_t conn_handle)
{
for (size_t i = 0; i < ARRAY_SIZE(rx_stats_pool); i++) {
if (rx_stats_pool[i].conn_handle == conn_handle) {
return &rx_stats_pool[i];
}
}

return NULL;
}

static struct iso_rx_stats *
iso_rx_stats_get_or_new(uint16_t conn_handle)
{
struct iso_rx_stats *rx_stats;

rx_stats = iso_rx_stats_lookup_conn_handle(conn_handle);
if (rx_stats == NULL) {
rx_stats = iso_rx_stats_lookup_conn_handle(BLE_HS_CONN_HANDLE_NONE);
if (rx_stats == NULL) {
return NULL;
}
}

rx_stats->conn_handle = conn_handle;

return rx_stats;
}

static void
iso_rx_stats_reset(void)
{
for (size_t i = 0; i < ARRAY_SIZE(rx_stats_pool); i++) {
memset(&rx_stats_pool[i], 0, sizeof(rx_stats_pool[i]));
rx_stats_pool[i].conn_handle = BLE_HS_CONN_HANDLE_NONE;
}
}

static void
iso_rx_stats_update(uint16_t conn_handle, const struct ble_iso_rx_data_info *info,
struct iso_rx_stats *stats)
iso_rx_stats_update(uint16_t conn_handle, const struct ble_iso_rx_data_info *info, void *arg)
{
struct iso_rx_stats *stats = arg;

if (!stats) {
return;
}

stats->ts_valid = info->ts_valid;
if (stats->ts_valid) {
stats->ts = info->ts;
Expand All @@ -58,9 +107,9 @@ iso_rx_stats_update(uint16_t conn_handle, const struct ble_iso_rx_data_info *inf
stats->total_cnt++;

if ((stats->total_cnt % 100) == 0) {
console_printf("BIS=%d, seq_num=%d, num_rx=%lld, "
console_printf("conn_handle=0x%04x, seq_num=%d, num_rx=%lld, "
"(valid=%lld, error=%lld, lost=%lld) ",
stats->bis_index, stats->seq_num,
stats->conn_handle, stats->seq_num,
stats->total_cnt, stats->valid_cnt,
stats->error_cnt, stats->lost_cnt);

Expand Down Expand Up @@ -122,6 +171,7 @@ ble_iso_event_handler(struct ble_iso_event *event, void *arg)
console_printf("BIG Sync Terminated handle=0x%02x reason: %u\n",
event->big_terminated.big_handle,
event->big_terminated.reason);
iso_rx_stats_reset();
break;

case BLE_ISO_EVENT_ISO_RX:
Expand Down Expand Up @@ -349,12 +399,6 @@ cmd_iso_big_sync_create(int argc, char **argv)

for (uint8_t i = 0; i < params.bis_cnt; i++) {
bis_params[i].bis_index = bis_idxs[i];
bis_params[i].cb = ble_iso_event_handler;
bis_params[i].cb_arg = &rx_stats_pool[i];

/* Reset stats */
memset(&rx_stats_pool[i], 0, sizeof(rx_stats_pool[i]));
rx_stats_pool[i].bis_index = bis_idxs[i];
}

params.bis_params = bis_params;
Expand Down Expand Up @@ -457,6 +501,8 @@ cmd_iso_data_path_setup(int argc, char **argv)
}

/* For now, the Data Path ID is set to HCI by default */
params.cb = ble_iso_event_handler;
params.cb_arg = iso_rx_stats_get_or_new(params.conn_handle);

rc = ble_iso_data_path_setup(&params);
if (rc != 0) {
Expand Down
17 changes: 9 additions & 8 deletions nimble/host/include/host/ble_iso.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,14 +333,6 @@ int ble_iso_terminate_big(uint8_t big_handle);
struct ble_iso_bis_params {
/** BIS index */
uint8_t bis_index;

/** The callback to associate with the BIS.
* Received ISO data is reported through this callback.
*/
ble_iso_event_fn *cb;

/** The optional argument to pass to the callback function */
void *cb_arg;
};

/** @brief BIG Sync parameters for @ref ble_iso_big_sync_create */
Expand Down Expand Up @@ -443,6 +435,15 @@ struct ble_iso_data_path_setup_params {

/** Codec Configuration */
const uint8_t *codec_config;

/**
* The ISO Data callback. Must be set if @p data_path_id is HCI.
* Received ISO data is reported through this callback.
*/
ble_iso_event_fn *cb;

/** The optional argument to pass to the callback function */
void *cb_arg;
};

/**
Expand Down
56 changes: 39 additions & 17 deletions nimble/host/src/ble_iso.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,20 @@ ble_iso_big_free(struct ble_iso_big *big)
return 0;
}

static struct ble_iso_conn *
ble_iso_conn_lookup_handle(uint16_t handle)
{
struct ble_iso_conn *conn;

SLIST_FOREACH(conn, &ble_iso_conns, next) {
if (conn->handle == handle) {
return conn;
}
}

return NULL;
}

#if MYNEWT_VAL(BLE_ISO_BROADCAST_SOURCE)
int
ble_iso_create_big(const struct ble_iso_create_big_params *create_params,
Expand Down Expand Up @@ -496,20 +510,6 @@ ble_iso_tx(uint16_t conn_handle, void *data, uint16_t data_len)
#endif /* BLE_ISO_BROADCAST_SOURCE */

#if MYNEWT_VAL(BLE_ISO_BROADCAST_SINK)
static struct ble_iso_conn *
ble_iso_conn_lookup_handle(uint16_t handle)
{
struct ble_iso_conn *conn;

SLIST_FOREACH(conn, &ble_iso_conns, next) {
if (conn->handle == handle) {
return conn;
}
}

return NULL;
}

int
ble_iso_big_sync_create(const struct ble_iso_big_sync_create_params *param,
uint8_t *big_handle)
Expand Down Expand Up @@ -553,9 +553,6 @@ ble_iso_big_sync_create(const struct ble_iso_big_sync_create_params *param,
return BLE_HS_ENOMEM;
}

bis->conn.cb = param->bis_params[i].cb;
bis->conn.cb_arg = param->bis_params[i].cb_arg;

cp->bis[i] = param->bis_params[i].bis_index;
}

Expand Down Expand Up @@ -867,8 +864,15 @@ ble_iso_data_path_setup(const struct ble_iso_data_path_setup_params *param)
struct ble_hci_le_setup_iso_data_path_rp rp;
struct ble_hci_le_setup_iso_data_path_cp *cp;
uint8_t buf[sizeof(*cp) + UINT8_MAX];
struct ble_iso_conn *conn;
int rc;

conn = ble_iso_conn_lookup_handle(param->conn_handle);
if (conn == NULL) {
BLE_HS_LOG_ERROR("invalid conn_handle\n");
return BLE_HS_ENOTCONN;
}

if (param->codec_config_len > 0 && param->codec_config == NULL) {
BLE_HS_LOG_ERROR("Missing codec_config\n");
return BLE_HS_EINVAL;
Expand All @@ -886,6 +890,14 @@ ble_iso_data_path_setup(const struct ble_iso_data_path_setup_params *param)
if (param->data_path_dir & BLE_ISO_DATA_DIR_RX) {
/* Output (Controller to Host) */
cp->data_path_dir |= BLE_HCI_ISO_DATA_PATH_DIR_OUTPUT;

if (cp->data_path_id == BLE_HCI_ISO_DATA_PATH_ID_HCI && param->cb == NULL) {
BLE_HS_LOG_ERROR("param->cb is NULL\n");
return BLE_HS_EINVAL;
}

conn->cb = param->cb;
conn->cb_arg = param->cb_arg;
}

cp->data_path_id = param->data_path_id;
Expand All @@ -910,8 +922,15 @@ ble_iso_data_path_remove(const struct ble_iso_data_path_remove_params *param)
{
struct ble_hci_le_remove_iso_data_path_rp rp;
struct ble_hci_le_remove_iso_data_path_cp cp = { 0 };
struct ble_iso_conn *conn;
int rc;

conn = ble_iso_conn_lookup_handle(param->conn_handle);
if (conn == NULL) {
BLE_HS_LOG_ERROR("invalid conn_handle\n");
return BLE_HS_ENOTCONN;
}

put_le16(&cp.conn_handle, param->conn_handle);

if (param->data_path_dir & BLE_ISO_DATA_DIR_TX) {
Expand All @@ -922,6 +941,9 @@ ble_iso_data_path_remove(const struct ble_iso_data_path_remove_params *param)
if (param->data_path_dir & BLE_ISO_DATA_DIR_RX) {
/* Output (Controller to Host) */
cp.data_path_dir |= BLE_HCI_ISO_DATA_PATH_DIR_OUTPUT;

conn->cb = NULL;
conn->cb_arg = NULL;
}

rc = ble_hs_hci_cmd_tx(BLE_HCI_OP(BLE_HCI_OGF_LE,
Expand Down
2 changes: 2 additions & 0 deletions nimble/include/nimble/hci_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -2423,6 +2423,8 @@ struct hci_data_hdr
#define BLE_HCI_ISO_DATA_PATH_DIR_INPUT 0x00
#define BLE_HCI_ISO_DATA_PATH_DIR_OUTPUT 0x01

#define BLE_HCI_ISO_DATA_PATH_ID_HCI 0x00

struct ble_hci_iso {
uint16_t handle;
uint16_t length;
Expand Down

0 comments on commit de8a76b

Please sign in to comment.