Skip to content

Commit 2ba6146

Browse files
Vudentznashif
authored andcommitted
Bluetooth: Shell: Add GATT command set
This introduces set command to GATT which can be used to write local attributes: uart:~$ gatt set 0x000b 62 6c 61 68 attr 0x004235a8 uuid 2a00 perm 0x09 00000000: 62 6C 61 68 Signed-off-by: Luiz Augusto von Dentz <[email protected]>
1 parent 4c80bb5 commit 2ba6146

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

subsys/bluetooth/shell/gatt.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,63 @@ static int cmd_get(const struct shell *shell, size_t argc, char *argv[])
869869
return 0;
870870
}
871871

872+
struct set_data {
873+
const struct shell *shell;
874+
size_t argc;
875+
char **argv;
876+
int err;
877+
};
878+
879+
static u8_t set_cb(const struct bt_gatt_attr *attr, void *user_data)
880+
{
881+
struct set_data *data = user_data;
882+
u8_t buf[256];
883+
int i;
884+
ssize_t ret;
885+
886+
if (!attr->write) {
887+
shell_error(data->shell, "Write not supported");
888+
data->err = -ENOENT;
889+
return BT_GATT_ITER_CONTINUE;
890+
}
891+
892+
for (i = 0; i < data->argc; i++) {
893+
buf[i] = strtoul(data->argv[i], NULL, 16);
894+
}
895+
896+
ret = attr->write(NULL, attr, (void *)buf, i, 0, 0);
897+
if (ret < 0) {
898+
data->err = ret;
899+
shell_error(data->shell, "Failed to write: %d", ret);
900+
return BT_GATT_ITER_STOP;
901+
}
902+
903+
return BT_GATT_ITER_CONTINUE;
904+
}
905+
906+
static int cmd_set(const struct shell *shell, size_t argc, char *argv[])
907+
{
908+
u16_t handle;
909+
struct set_data data;
910+
911+
handle = strtoul(argv[1], NULL, 16);
912+
913+
data.shell = shell;
914+
data.argc = argc - 2;
915+
data.argv = argv + 2;
916+
data.err = 0;
917+
918+
bt_gatt_foreach_attr(handle, handle, set_cb, &data);
919+
920+
if (data.err < 0) {
921+
return -ENOEXEC;
922+
}
923+
924+
bt_gatt_foreach_attr(handle, handle, get_cb, (void *)shell);
925+
926+
return 0;
927+
}
928+
872929
#define HELP_NONE "[none]"
873930

874931
SHELL_STATIC_SUBCMD_SET_CREATE(gatt_cmds,
@@ -912,6 +969,7 @@ SHELL_STATIC_SUBCMD_SET_CREATE(gatt_cmds,
912969
"unregister pre-predefined test service",
913970
cmd_unregister_test_svc, 1, 0),
914971
SHELL_CMD_ARG(get, NULL, "<handle>", cmd_get, 2, 0),
972+
SHELL_CMD_ARG(set, NULL, "<handle> [data...]", cmd_set, 2, 255),
915973
SHELL_SUBCMD_SET_END
916974
);
917975

0 commit comments

Comments
 (0)