diff mbox series

[BlueZ,4/5] btgatt-client: Add function to search descriptors

Message ID 20240122165000.279381-5-frederic.danis@collabora.com
State New
Headers show
Series Enhance GATT to pass PTS tests | expand

Commit Message

Frédéric Danis Jan. 22, 2024, 4:49 p.m. UTC
This is requested to pass PTS GATT/CL/GAD/BV-06-C test.
This search descriptors based on start and end handles.
---
 tools/btgatt-client.c | 79 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 79 insertions(+)

Comments

Luiz Augusto von Dentz Jan. 22, 2024, 6:15 p.m. UTC | #1
Hi Frédéric,

On Mon, Jan 22, 2024 at 12:43 PM Frédéric Danis
<frederic.danis@collabora.com> wrote:
>
> This is requested to pass PTS GATT/CL/GAD/BV-06-C test.
> This search descriptors based on start and end handles.

Is this test mandatory though? Afaik if we do support the discovery of
all procedure this becomes useless, because the stack can perform
these operations locally by using its cache.

> ---
>  tools/btgatt-client.c | 79 +++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 79 insertions(+)
>
> diff --git a/tools/btgatt-client.c b/tools/btgatt-client.c
> index bb0822658..a7d5d76ba 100644
> --- a/tools/btgatt-client.c
> +++ b/tools/btgatt-client.c
> @@ -1490,6 +1490,83 @@ static void cmd_search_characteristics(struct client *cli, char *cmd_str)
>                                                 NULL);
>  }
>
> +static void search_descriptors_usage(void)
> +{
> +       printf("Usage: search-descriptors <start_hanlde> <end_handle>\n"
> +               "e.g.:\n"
> +               "\tsearch-descriptors 0x0001 0xFFFF\n");
> +}
> +
> +static void search_descriptors_cb(bool success, uint8_t att_ecode,
> +                                       struct bt_gatt_result *result,
> +                                       void *user_data)
> +{
> +       struct bt_gatt_iter iter;
> +       uint16_t handle;
> +       uint128_t u128;
> +       bt_uuid_t uuid;
> +       char uuid_str[MAX_LEN_UUID_STR];
> +
> +       if (!success) {
> +               PRLOG("\nDescriptors discovery failed: %s (0x%02x)\n",
> +                               ecode_to_string(att_ecode), att_ecode);
> +               return;
> +       }
> +
> +       if (!result || !bt_gatt_iter_init(&iter, result))
> +               return;
> +
> +       printf("\n");
> +       while (bt_gatt_iter_next_descriptor(&iter, &handle, u128.data)) {
> +               bt_uuid128_create(&uuid, u128);
> +               bt_uuid_to_string(&uuid, uuid_str, sizeof(uuid_str));
> +               printf("Found handle: 0x%04x UUID: %s\n", handle, uuid_str);
> +       }
> +       PRLOG("\n");
> +}
> +
> +static void cmd_search_descriptors(struct client *cli, char *cmd_str)
> +{
> +       char *argv[3];
> +       int argc = 0;
> +       uint16_t start_handle, end_handle;
> +       char *endptr = NULL;
> +
> +       if (!bt_gatt_client_is_ready(cli->gatt)) {
> +               printf("GATT client not initialized\n");
> +               return;
> +       }
> +
> +       if (!parse_args(cmd_str, 2, argv, &argc)) {
> +               printf("Too many arguments\n");
> +               search_descriptors_usage();
> +               return;
> +       }
> +
> +       if (argc < 1) {
> +               search_descriptors_usage();
> +               return;
> +       }
> +
> +       start_handle = strtol(argv[0], &endptr, 0);
> +       if (!endptr || *endptr != '\0') {
> +               printf("Invalid start handle: %s\n", argv[0]);
> +               return;
> +       }
> +
> +       end_handle = strtol(argv[1], &endptr, 0);
> +       if (!endptr || *endptr != '\0') {
> +               printf("Invalid end handle: %s\n", argv[1]);
> +               return;
> +       }
> +
> +       bt_gatt_discover_descriptors(bt_gatt_client_get_att(cli->gatt),
> +                                               start_handle, end_handle,
> +                                               search_descriptors_cb,
> +                                               NULL,
> +                                               NULL);
> +}
> +
>  static void cmd_help(struct client *cli, char *cmd_str);
>
>  typedef void (*command_func_t)(struct client *cli, char *cmd_str);
> @@ -1530,6 +1607,8 @@ static struct {
>                                 "\tSearch service"},
>         { "search-characteristics", cmd_search_characteristics,
>                                 "\tSearch characteristics"},
> +       { "search-descriptors", cmd_search_descriptors,
> +                               "\tSearch descriptors"},
>         { }
>  };
>
> --
> 2.34.1
>
>
Frédéric Danis Jan. 23, 2024, 1:16 p.m. UTC | #2
Hi Luiz,

On 22/01/2024 19:15, Luiz Augusto von Dentz wrote:
> Hi Frédéric,
>
> On Mon, Jan 22, 2024 at 12:43 PM Frédéric Danis
> <frederic.danis@collabora.com>  wrote:
>> This is requested to pass PTS GATT/CL/GAD/BV-06-C test.
>> This search descriptors based on start and end handles.
> Is this test mandatory though? Afaik if we do support the discovery of
> all procedure this becomes useless, because the stack can perform
> these operations locally by using its cache.
This test is optional, as tests GATT/CL/GAR/BI-10-C and 
GATT/CL/GAR/BI-11-C which also needs this patch to pass. 
GATT/CL/GAR/BI-10-C and GATT/CL/GAR/BI-11-C request the ability to 
prevent security upgrade. This may need to add a security retry flag to 
bt_gatt_read_by_type() in src/shared/gatt-helpers.{ch} to be able to 
call bt_att_set_retry() introduced in the first patch of this series. 
Does it seem correct?
>> ---
>>   tools/btgatt-client.c | 79 +++++++++++++++++++++++++++++++++++++++++++
>>   1 file changed, 79 insertions(+)
>>
>> diff --git a/tools/btgatt-client.c b/tools/btgatt-client.c
>> index bb0822658..a7d5d76ba 100644
>> --- a/tools/btgatt-client.c
>> +++ b/tools/btgatt-client.c
>> @@ -1490,6 +1490,83 @@ static void cmd_search_characteristics(struct client *cli, char *cmd_str)
>>                                                  NULL);
>>   }
>>
>> +static void search_descriptors_usage(void)
>> +{
>> +       printf("Usage: search-descriptors <start_hanlde> <end_handle>\n"
>> +               "e.g.:\n"
>> +               "\tsearch-descriptors 0x0001 0xFFFF\n");
>> +}
>> +
>> +static void search_descriptors_cb(bool success, uint8_t att_ecode,
>> +                                       struct bt_gatt_result *result,
>> +                                       void *user_data)
>> +{
>> +       struct bt_gatt_iter iter;
>> +       uint16_t handle;
>> +       uint128_t u128;
>> +       bt_uuid_t uuid;
>> +       char uuid_str[MAX_LEN_UUID_STR];
>> +
>> +       if (!success) {
>> +               PRLOG("\nDescriptors discovery failed: %s (0x%02x)\n",
>> +                               ecode_to_string(att_ecode), att_ecode);
>> +               return;
>> +       }
>> +
>> +       if (!result || !bt_gatt_iter_init(&iter, result))
>> +               return;
>> +
>> +       printf("\n");
>> +       while (bt_gatt_iter_next_descriptor(&iter, &handle, u128.data)) {
>> +               bt_uuid128_create(&uuid, u128);
>> +               bt_uuid_to_string(&uuid, uuid_str, sizeof(uuid_str));
>> +               printf("Found handle: 0x%04x UUID: %s\n", handle, uuid_str);
>> +       }
>> +       PRLOG("\n");
>> +}
>> +
>> +static void cmd_search_descriptors(struct client *cli, char *cmd_str)
>> +{
>> +       char *argv[3];
>> +       int argc = 0;
>> +       uint16_t start_handle, end_handle;
>> +       char *endptr = NULL;
>> +
>> +       if (!bt_gatt_client_is_ready(cli->gatt)) {
>> +               printf("GATT client not initialized\n");
>> +               return;
>> +       }
>> +
>> +       if (!parse_args(cmd_str, 2, argv, &argc)) {
>> +               printf("Too many arguments\n");
>> +               search_descriptors_usage();
>> +               return;
>> +       }
>> +
>> +       if (argc < 1) {
>> +               search_descriptors_usage();
>> +               return;
>> +       }
>> +
>> +       start_handle = strtol(argv[0], &endptr, 0);
>> +       if (!endptr || *endptr != '\0') {
>> +               printf("Invalid start handle: %s\n", argv[0]);
>> +               return;
>> +       }
>> +
>> +       end_handle = strtol(argv[1], &endptr, 0);
>> +       if (!endptr || *endptr != '\0') {
>> +               printf("Invalid end handle: %s\n", argv[1]);
>> +               return;
>> +       }
>> +
>> +       bt_gatt_discover_descriptors(bt_gatt_client_get_att(cli->gatt),
>> +                                               start_handle, end_handle,
>> +                                               search_descriptors_cb,
>> +                                               NULL,
>> +                                               NULL);
>> +}
>> +
>>   static void cmd_help(struct client *cli, char *cmd_str);
>>
>>   typedef void (*command_func_t)(struct client *cli, char *cmd_str);
>> @@ -1530,6 +1607,8 @@ static struct {
>>                                  "\tSearch service"},
>>          { "search-characteristics", cmd_search_characteristics,
>>                                  "\tSearch characteristics"},
>> +       { "search-descriptors", cmd_search_descriptors,
>> +                               "\tSearch descriptors"},
>>          { }
>>   };
>>
>> --
>> 2.34.1
>>
>>
diff mbox series

Patch

diff --git a/tools/btgatt-client.c b/tools/btgatt-client.c
index bb0822658..a7d5d76ba 100644
--- a/tools/btgatt-client.c
+++ b/tools/btgatt-client.c
@@ -1490,6 +1490,83 @@  static void cmd_search_characteristics(struct client *cli, char *cmd_str)
 						NULL);
 }
 
+static void search_descriptors_usage(void)
+{
+	printf("Usage: search-descriptors <start_hanlde> <end_handle>\n"
+		"e.g.:\n"
+		"\tsearch-descriptors 0x0001 0xFFFF\n");
+}
+
+static void search_descriptors_cb(bool success, uint8_t att_ecode,
+					struct bt_gatt_result *result,
+					void *user_data)
+{
+	struct bt_gatt_iter iter;
+	uint16_t handle;
+	uint128_t u128;
+	bt_uuid_t uuid;
+	char uuid_str[MAX_LEN_UUID_STR];
+
+	if (!success) {
+		PRLOG("\nDescriptors discovery failed: %s (0x%02x)\n",
+				ecode_to_string(att_ecode), att_ecode);
+		return;
+	}
+
+	if (!result || !bt_gatt_iter_init(&iter, result))
+		return;
+
+	printf("\n");
+	while (bt_gatt_iter_next_descriptor(&iter, &handle, u128.data)) {
+		bt_uuid128_create(&uuid, u128);
+		bt_uuid_to_string(&uuid, uuid_str, sizeof(uuid_str));
+		printf("Found handle: 0x%04x UUID: %s\n", handle, uuid_str);
+	}
+	PRLOG("\n");
+}
+
+static void cmd_search_descriptors(struct client *cli, char *cmd_str)
+{
+	char *argv[3];
+	int argc = 0;
+	uint16_t start_handle, end_handle;
+	char *endptr = NULL;
+
+	if (!bt_gatt_client_is_ready(cli->gatt)) {
+		printf("GATT client not initialized\n");
+		return;
+	}
+
+	if (!parse_args(cmd_str, 2, argv, &argc)) {
+		printf("Too many arguments\n");
+		search_descriptors_usage();
+		return;
+	}
+
+	if (argc < 1) {
+		search_descriptors_usage();
+		return;
+	}
+
+	start_handle = strtol(argv[0], &endptr, 0);
+	if (!endptr || *endptr != '\0') {
+		printf("Invalid start handle: %s\n", argv[0]);
+		return;
+	}
+
+	end_handle = strtol(argv[1], &endptr, 0);
+	if (!endptr || *endptr != '\0') {
+		printf("Invalid end handle: %s\n", argv[1]);
+		return;
+	}
+
+	bt_gatt_discover_descriptors(bt_gatt_client_get_att(cli->gatt),
+						start_handle, end_handle,
+						search_descriptors_cb,
+						NULL,
+						NULL);
+}
+
 static void cmd_help(struct client *cli, char *cmd_str);
 
 typedef void (*command_func_t)(struct client *cli, char *cmd_str);
@@ -1530,6 +1607,8 @@  static struct {
 				"\tSearch service"},
 	{ "search-characteristics", cmd_search_characteristics,
 				"\tSearch characteristics"},
+	{ "search-descriptors", cmd_search_descriptors,
+				"\tSearch descriptors"},
 	{ }
 };