Skip to content

Commit

Permalink
apps/btshell: add support for sending Multiple handle Notifications
Browse files Browse the repository at this point in the history
Added commands for queueing notifications to be sent using Multiple
Handle Notification, sending them and clearing pending queue.
  • Loading branch information
KKopyscinski committed Oct 16, 2023
1 parent 5209243 commit 5fef664
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 0 deletions.
6 changes: 6 additions & 0 deletions apps/btshell/src/btshell.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ int btshell_write_long(uint16_t conn_handle, uint16_t attr_handle,
uint16_t offset, struct os_mbuf *om);
int btshell_write_reliable(uint16_t conn_handle,
struct ble_gatt_attr *attrs, int num_attrs);
#if MYNEWT_VAL(BLE_GATT_NOTIFY_MULTIPLE)
int btshell_enqueue_notif(uint16_t handle, uint16_t len, uint8_t *value);
int btshell_send_pending_notif(uint16_t conn_handle);
int btshell_clear_pending_notif(void);
#endif

#if MYNEWT_VAL(BLE_EXT_ADV)
int btshell_ext_adv_configure(uint8_t instance,
const struct ble_gap_ext_adv_params *params,
Expand Down
58 changes: 58 additions & 0 deletions apps/btshell/src/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -3497,6 +3497,43 @@ static const struct shell_cmd_help gatt_write_help = {
.usage = NULL,
.params = gatt_write_params,
};

/*****************************************************************************
* $gatt-enqueue-notify *
*****************************************************************************/

static const struct shell_param gatt_enqueue_notif_params[] = {
{"handle", "characteristic handle, usage: =<UINT16>"},
{"value", "usage: =<octets>"},
{NULL, NULL}
};

static const struct shell_cmd_help gatt_enqueue_notif_help = {
.summary = "enqueue notification to be sent",
.usage = NULL,
.params = gatt_enqueue_notif_params,
};

/*****************************************************************************
* $gatt-send-pending-notify *
*****************************************************************************/

static const struct shell_param gatt_send_pending_notif_params[] = {
{"conn", "connection handle, usage: =<UINT16>"},
{NULL, NULL}
};

static const struct shell_cmd_help gatt_send_pending_notif_help = {
.summary = "send pending notifications",
.usage = NULL,
.params = gatt_send_pending_notif_params,
};

static const struct shell_cmd_help gatt_clear_pending_notif_help = {
.summary = "clear pending notifications",
.usage = NULL,
.params = NULL,
};
#endif

#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM)
Expand Down Expand Up @@ -4417,6 +4454,27 @@ static const struct shell_cmd btshell_commands[] = {
.sc_cmd_func = cmd_gatt_write,
#if MYNEWT_VAL(SHELL_CMD_HELP)
.help = &gatt_write_help,
#endif
},
{
.sc_cmd = "gatt-enqueue-notif",
.sc_cmd_func = cmd_gatt_enqueue_notif,
#if MYNEWT_VAL(SHELL_CMD_HELP)
.help = &gatt_enqueue_notif_help,
#endif
},
{
.sc_cmd = "gatt-send-queued-notif",
.sc_cmd_func = cmd_gatt_send_pending_notif,
#if MYNEWT_VAL(SHELL_CMD_HELP)
.help = &gatt_send_pending_notif_help,
#endif
},
{
.sc_cmd = "gatt-clear-queued-notif",
.sc_cmd_func = cmd_gatt_clear_pending_notif,
#if MYNEWT_VAL(SHELL_CMD_HELP)
.help = &gatt_clear_pending_notif_help,
#endif
},
#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM)
Expand Down
72 changes: 72 additions & 0 deletions apps/btshell/src/cmd_gatt.c
Original file line number Diff line number Diff line change
Expand Up @@ -592,3 +592,75 @@ cmd_gatt_write(int argc, char **argv)

return rc;
}

int
cmd_gatt_enqueue_notif(int argc, char **argv)
{
#if MYNEWT_VAL(BLE_GATT_NOTIFY_MULTIPLE)
int rc;
uint16_t handle;
unsigned int len;
uint8_t value[BLE_ATT_ATTR_MAX_LEN];

rc = parse_arg_init(argc - 1, argv + 1);
if (rc != 0) {
return rc;
}

handle = parse_arg_uint16("handle", &rc);
if (rc != 0) {
console_printf("invalid 'handle' parameter\n");
return rc;
}

if (argc > 1) {
rc = parse_arg_byte_stream("value", BLE_ATT_ATTR_MAX_LEN, value, &len);
if (rc != 0) {
console_printf("invalid 'value' parameter\n");
return rc;
}
return btshell_enqueue_notif(handle, len, value);
} else {
return btshell_enqueue_notif(handle, 0, NULL);
}
#else
console_printf("To enable this features set BLE_GATT_NOTIFY_MULTIPLE\n");
return ENOTSUP;
#endif
}

int
cmd_gatt_send_pending_notif(int argc, char **argv)
{
#if MYNEWT_VAL(BLE_GATT_NOTIFY_MULTIPLE)
uint16_t conn_handle;
int rc;

rc = parse_arg_init(argc - 1, argv + 1);
if (rc != 0) {
return rc;
}

conn_handle = parse_arg_uint16("conn", &rc);
if (rc != 0) {
console_printf("invalid 'conn' parameter\n");
return rc;
}

return btshell_send_pending_notif(conn_handle);
#else
console_printf("To enable this features set BLE_GATT_NOTIFY_MULTIPLE\n");
return ENOTSUP;
#endif
}

int
cmd_gatt_clear_pending_notif(int argc, char **argv)
{
#if MYNEWT_VAL(BLE_GATT_NOTIFY_MULTIPLE)
return btshell_clear_pending_notif();
#else
console_printf("To enable this features set BLE_GATT_NOTIFY_MULTIPLE\n");
return ENOTSUP;
#endif
}
3 changes: 3 additions & 0 deletions apps/btshell/src/cmd_gatt.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@ int cmd_gatt_service_visibility(int argc, char **argv);
int cmd_gatt_show(int argc, char **argv);
int cmd_gatt_show_local(int argc, char **argv);
int cmd_gatt_write(int argc, char **argv);
int cmd_gatt_enqueue_notif(int argc, char **argv);
int cmd_gatt_send_pending_notif(int argc, char **argv);
int cmd_gatt_clear_pending_notif(int argc, char **argv);

#endif
76 changes: 76 additions & 0 deletions apps/btshell/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ struct os_mbuf_pool sdu_os_mbuf_pool;
static struct os_mempool sdu_coc_mbuf_mempool;
#endif

static struct ble_gatt_notif pending_notif[BTSHELL_MAX_CHRS];
static uint16_t pending_notif_cnt;

static struct os_callout btshell_tx_timer;
struct btshell_tx_data_s
{
Expand Down Expand Up @@ -1761,6 +1764,79 @@ btshell_write_reliable(uint16_t conn_handle,
return rc;
}

#if MYNEWT_VAL(BLE_GATT_NOTIFY_MULTIPLE)
int
btshell_enqueue_notif(uint16_t handle, uint16_t len, uint8_t *value)
{
struct ble_gatt_notif *notify = NULL;
struct os_mbuf *val_ptr;
int i;

for (i = 0; i < BTSHELL_MAX_CHRS; i++) {
if (pending_notif[i].handle == 0) {
notify = &pending_notif[i];
break;
}
}

if (notify == NULL) {
return ENOMEM;
}

notify->handle = handle;
if (value != NULL) {
notify->value = os_msys_get(0, 0);

val_ptr = os_mbuf_extend(notify->value, len);
if (val_ptr == NULL) {
return ENOMEM;
}
memcpy(val_ptr, value, len);
}

pending_notif_cnt++;

return 0;
}

int
btshell_send_pending_notif(uint16_t conn_handle)
{
int rc = 0;
int i;

if (pending_notif_cnt == 0) {
return EALREADY;
}

rc = ble_gatts_notify_multiple_custom(conn_handle, pending_notif_cnt,
pending_notif);
for (i = 0; i < pending_notif_cnt; i++) {
pending_notif[i].handle = 0;
pending_notif[i].value = NULL;
}

pending_notif_cnt = 0;

return rc;
}

int
btshell_clear_pending_notif(void)
{
int i;

for (i = 0; i < pending_notif_cnt; i++) {
pending_notif[i].handle = 0;
pending_notif[i].value = NULL;
}

pending_notif_cnt = 0;

return 0;
}
#endif

#if MYNEWT_VAL(BLE_EXT_ADV)
int
btshell_ext_adv_configure(uint8_t instance,
Expand Down

0 comments on commit 5fef664

Please sign in to comment.