Skip to content

Commit

Permalink
Bluetooth: shell: Add "add-subscription" command
Browse files Browse the repository at this point in the history
Add the command add-subscription to the shell to test the
bt_gatt_add_subscription API.

Signed-off-by: Joakim Andersson <[email protected]>
  • Loading branch information
joerchan authored and carlescufi committed Sep 4, 2020
1 parent 76119c5 commit 48606a2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion subsys/bluetooth/shell/bt.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#define DEVICE_NAME CONFIG_BT_DEVICE_NAME
#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)

static uint8_t selected_id = BT_ID_DEFAULT;
uint8_t selected_id = BT_ID_DEFAULT;
const struct shell *ctx_shell;

#if defined(CONFIG_BT_CONN)
Expand Down
43 changes: 43 additions & 0 deletions subsys/bluetooth/shell/gatt.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

#define CHAR_SIZE_MAX 512

extern uint8_t selected_id;

#if defined(CONFIG_BT_GATT_CLIENT)
static void exchange_func(struct bt_conn *conn, uint8_t err,
struct bt_gatt_exchange_params *params)
Expand Down Expand Up @@ -586,6 +588,44 @@ static int cmd_subscribe(const struct shell *shell, size_t argc, char *argv[])
return err;
}

static int cmd_resubscribe(const struct shell *shell, size_t argc,
char *argv[])
{
bt_addr_le_t addr;
int err;

if (subscribe_params.value_handle) {
shell_error(shell, "Cannot resubscribe: subscription to %x"
" already exists", subscribe_params.value_handle);
return -ENOEXEC;
}

err = bt_addr_le_from_str(argv[1], argv[2], &addr);
if (err) {
shell_error(shell, "Invalid peer address (err %d)", err);
return -ENOEXEC;
}

subscribe_params.ccc_handle = strtoul(argv[3], NULL, 16);
subscribe_params.value_handle = strtoul(argv[4], NULL, 16);
subscribe_params.value = BT_GATT_CCC_NOTIFY;
subscribe_params.notify = notify_func;

if (argc > 5 && !strcmp(argv[5], "ind")) {
subscribe_params.value = BT_GATT_CCC_INDICATE;
}

err = bt_gatt_resubscribe(selected_id, &addr, &subscribe_params);
if (err) {
subscribe_params.value_handle = 0U;
shell_error(shell, "Resubscribe failed (err %d)", err);
} else {
shell_print(shell, "Resubscribed");
}

return err;
}

static int cmd_unsubscribe(const struct shell *shell,
size_t argc, char *argv[])
{
Expand Down Expand Up @@ -1081,6 +1121,7 @@ int cmd_att_mtu(const struct shell *shell, size_t argc, char *argv[])
}

#define HELP_NONE "[none]"
#define HELP_ADDR_LE "<address: XX:XX:XX:XX:XX:XX> <type: (public|random)>"

SHELL_STATIC_SUBCMD_SET_CREATE(gatt_cmds,
#if defined(CONFIG_BT_GATT_CLIENT)
Expand All @@ -1106,6 +1147,8 @@ SHELL_STATIC_SUBCMD_SET_CREATE(gatt_cmds,
cmd_write_without_rsp, 3, 2),
SHELL_CMD_ARG(subscribe, NULL, "<CCC handle> <value handle> [ind]",
cmd_subscribe, 3, 1),
SHELL_CMD_ARG(resubscribe, NULL, HELP_ADDR_LE" <CCC handle> "
"<value handle> [ind]", cmd_resubscribe, 5, 1),
SHELL_CMD_ARG(write, NULL, "<handle> <offset> <data> [length]",
cmd_write, 4, 1),
SHELL_CMD_ARG(write-without-response, NULL,
Expand Down

0 comments on commit 48606a2

Please sign in to comment.