linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 1/4] client: Group discovery filter variables into a struct
@ 2017-12-15 16:09 Luiz Augusto von Dentz
  2017-12-15 16:09 ` [PATCH v2 2/4] client: Make scan:clear clear individual fields Luiz Augusto von Dentz
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Luiz Augusto von Dentz @ 2017-12-15 16:09 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

This should be easier to read and maintain.
---
 client/main.c | 82 +++++++++++++++++++++++++----------------------------------
 1 file changed, 35 insertions(+), 47 deletions(-)

diff --git a/client/main.c b/client/main.c
index 04937bc4b..5f7ec0103 100644
--- a/client/main.c
+++ b/client/main.c
@@ -1267,31 +1267,19 @@ static void set_discovery_filter_reply(DBusMessage *message, void *user_data)
 	bt_shell_printf("SetDiscoveryFilter success\n");
 }
 
-static gint filtered_scan_rssi = DISTANCE_VAL_INVALID;
-static gint filtered_scan_pathloss = DISTANCE_VAL_INVALID;
-static char **filtered_scan_uuids;
-static size_t filtered_scan_uuids_len;
-static char *filtered_scan_transport;
-static bool filtered_scan_duplicate_data;
+static struct set_discovery_filter_args filter = {
+	.rssi = DISTANCE_VAL_INVALID,
+	.pathloss = DISTANCE_VAL_INVALID,
+};
 
 static void cmd_set_scan_filter_commit(void)
 {
-	struct set_discovery_filter_args args;
-
-	args.uuids = NULL;
-	args.pathloss = filtered_scan_pathloss;
-	args.rssi = filtered_scan_rssi;
-	args.transport = filtered_scan_transport;
-	args.uuids = filtered_scan_uuids;
-	args.uuids_len = filtered_scan_uuids_len;
-	args.duplicate = filtered_scan_duplicate_data;
-
 	if (check_default_ctrl() == FALSE)
 		return;
 
 	if (g_dbus_proxy_method_call(default_ctrl->proxy, "SetDiscoveryFilter",
 		set_discovery_filter_setup, set_discovery_filter_reply,
-		&args, NULL) == FALSE) {
+		&filter, NULL) == FALSE) {
 		bt_shell_printf("Failed to set discovery filter\n");
 		return;
 	}
@@ -1302,26 +1290,26 @@ static void cmd_scan_filter_uuids(int argc, char *argv[])
 	if (argc < 2 || !strlen(argv[1])) {
 		char **uuid;
 
-		for (uuid = filtered_scan_uuids; uuid && *uuid; uuid++)
+		for (uuid = filter.uuids; uuid && *uuid; uuid++)
 			print_uuid(*uuid);
 
 		return;
 	}
 
-	g_strfreev(filtered_scan_uuids);
-	filtered_scan_uuids = NULL;
-	filtered_scan_uuids_len = 0;
+	g_strfreev(filter.uuids);
+	filter.uuids = NULL;
+	filter.uuids_len = 0;
 
 	if (!strcmp(argv[1], "all"))
 		goto commit;
 
-	filtered_scan_uuids = g_strdupv(&argv[1]);
-	if (!filtered_scan_uuids) {
+	filter.uuids = g_strdupv(&argv[1]);
+	if (!filter.uuids) {
 		bt_shell_printf("Failed to parse input\n");
 		return;
 	}
 
-	filtered_scan_uuids_len = g_strv_length(filtered_scan_uuids);
+	filter.uuids_len = g_strv_length(filter.uuids);
 
 commit:
 	cmd_set_scan_filter_commit();
@@ -1330,13 +1318,13 @@ commit:
 static void cmd_scan_filter_rssi(int argc, char *argv[])
 {
 	if (argc < 2 || !strlen(argv[1])) {
-		if (filtered_scan_rssi != DISTANCE_VAL_INVALID)
-			bt_shell_printf("RSSI: %d\n", filtered_scan_rssi);
+		if (filter.rssi != DISTANCE_VAL_INVALID)
+			bt_shell_printf("RSSI: %d\n", filter.rssi);
 		return;
 	}
 
-	filtered_scan_pathloss = DISTANCE_VAL_INVALID;
-	filtered_scan_rssi = atoi(argv[1]);
+	filter.pathloss = DISTANCE_VAL_INVALID;
+	filter.rssi = atoi(argv[1]);
 
 	cmd_set_scan_filter_commit();
 }
@@ -1344,14 +1332,14 @@ static void cmd_scan_filter_rssi(int argc, char *argv[])
 static void cmd_scan_filter_pathloss(int argc, char *argv[])
 {
 	if (argc < 2 || !strlen(argv[1])) {
-		if (filtered_scan_pathloss != DISTANCE_VAL_INVALID)
+		if (filter.pathloss != DISTANCE_VAL_INVALID)
 			bt_shell_printf("Pathloss: %d\n",
-						filtered_scan_pathloss);
+						filter.pathloss);
 		return;
 	}
 
-	filtered_scan_rssi = DISTANCE_VAL_INVALID;
-	filtered_scan_pathloss = atoi(argv[1]);
+	filter.rssi = DISTANCE_VAL_INVALID;
+	filter.pathloss = atoi(argv[1]);
 
 	cmd_set_scan_filter_commit();
 }
@@ -1359,14 +1347,14 @@ static void cmd_scan_filter_pathloss(int argc, char *argv[])
 static void cmd_scan_filter_transport(int argc, char *argv[])
 {
 	if (argc < 2 || !strlen(argv[1])) {
-		if (filtered_scan_transport)
+		if (filter.transport)
 			bt_shell_printf("Transport: %s\n",
-					filtered_scan_transport);
+					filter.transport);
 		return;
 	}
 
-	g_free(filtered_scan_transport);
-	filtered_scan_transport = g_strdup(argv[1]);
+	g_free(filter.transport);
+	filter.transport = g_strdup(argv[1]);
 
 	cmd_set_scan_filter_commit();
 }
@@ -1375,14 +1363,14 @@ static void cmd_scan_filter_duplicate_data(int argc, char *argv[])
 {
 	if (argc < 2 || !strlen(argv[1])) {
 		bt_shell_printf("DuplicateData: %s\n",
-				filtered_scan_duplicate_data ? "on" : "off");
+				filter.duplicate ? "on" : "off");
 		return;
 	}
 
 	if (!strcmp(argv[1], "on"))
-		filtered_scan_duplicate_data = true;
+		filter.duplicate = true;
 	else if (!strcmp(argv[1], "off"))
-		filtered_scan_duplicate_data = false;
+		filter.duplicate = false;
 	else {
 		bt_shell_printf("Invalid option: %s\n", argv[1]);
 		return;
@@ -1407,14 +1395,14 @@ static void clear_discovery_filter_setup(DBusMessageIter *iter, void *user_data)
 static void cmd_scan_filter_clear(int argc, char *argv[])
 {
 	/* set default values for all options */
-	filtered_scan_rssi = DISTANCE_VAL_INVALID;
-	filtered_scan_pathloss = DISTANCE_VAL_INVALID;
-	g_strfreev(filtered_scan_uuids);
-	filtered_scan_uuids = NULL;
-	filtered_scan_uuids_len = 0;
-	g_free(filtered_scan_transport);
-	filtered_scan_transport = NULL;
-	filtered_scan_duplicate_data = false;
+	filter.rssi = DISTANCE_VAL_INVALID;
+	filter.pathloss = DISTANCE_VAL_INVALID;
+	g_strfreev(filter.uuids);
+	filter.uuids = NULL;
+	filter.uuids_len = 0;
+	g_free(filter.transport);
+	filter.transport = NULL;
+	filter.duplicate = false;
 
 	if (check_default_ctrl() == FALSE)
 		return;
-- 
2.13.6


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 2/4] client: Make scan:clear clear individual fields
  2017-12-15 16:09 [PATCH v2 1/4] client: Group discovery filter variables into a struct Luiz Augusto von Dentz
@ 2017-12-15 16:09 ` Luiz Augusto von Dentz
  2017-12-15 16:09 ` [PATCH v2 3/4] client: Add generator for scan:clear Luiz Augusto von Dentz
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Luiz Augusto von Dentz @ 2017-12-15 16:09 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

This reintroduces the option to clear individual fields which was
removed when redesining the commands which now read the fields when
no arguments are provided.
---
 client/main.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 56 insertions(+), 5 deletions(-)

diff --git a/client/main.c b/client/main.c
index 5f7ec0103..53e03f56f 100644
--- a/client/main.c
+++ b/client/main.c
@@ -1392,18 +1392,68 @@ static void clear_discovery_filter_setup(DBusMessageIter *iter, void *user_data)
 	dbus_message_iter_close_container(iter, &dict);
 }
 
-static void cmd_scan_filter_clear(int argc, char *argv[])
+static void filter_clear_uuids(void)
 {
-	/* set default values for all options */
-	filter.rssi = DISTANCE_VAL_INVALID;
-	filter.pathloss = DISTANCE_VAL_INVALID;
 	g_strfreev(filter.uuids);
 	filter.uuids = NULL;
 	filter.uuids_len = 0;
+}
+
+static void filter_clear_rssi(void)
+{
+	filter.rssi = DISTANCE_VAL_INVALID;
+}
+
+static void filter_clear_pathloss(void)
+{
+	filter.pathloss = DISTANCE_VAL_INVALID;
+}
+
+static void filter_clear_transport(void)
+{
 	g_free(filter.transport);
 	filter.transport = NULL;
+}
+
+static void filter_clear_duplicate(void)
+{
 	filter.duplicate = false;
+}
+
+static const struct filter_clear {
+	const char *name;
+	void (*clear) (void);
+} filter_clear[] = {
+	{ "uuids", filter_clear_uuids },
+	{ "rssi", filter_clear_rssi },
+	{ "pathloss", filter_clear_pathloss },
+	{ "transport", filter_clear_transport },
+	{ "duplicate-data", filter_clear_duplicate },
+	{}
+};
+
+static void cmd_scan_filter_clear(int argc, char *argv[])
+{
+	const struct filter_clear *fc;
+	bool all = false;
+
+	if (argc < 2 || !strlen(argv[1]))
+		all = true;
 
+	for (fc = filter_clear; fc && fc->name; fc++) {
+		if (all || !strcmp(fc->name, argv[1])) {
+			fc->clear();
+			if (!all)
+				goto done;
+		}
+	}
+
+	if (!all) {
+		bt_shell_printf("Invalid argument %s\n", argv[1]);
+		return;
+	}
+
+done:
 	if (check_default_ctrl() == FALSE)
 		return;
 
@@ -2256,7 +2306,8 @@ static const struct bt_shell_menu scan_menu = {
 	{ "duplicate-data", "[on/off]", cmd_scan_filter_duplicate_data,
 				"Set/Get duplicate data filter",
 				mode_generator },
-	{ "clear", NULL, cmd_scan_filter_clear,
+	{ "clear", "[uuids/rssi/pathloss/transport/duplicate-data]",
+				cmd_scan_filter_clear,
 				"Clears discovery filter." },
 	{ } },
 };
-- 
2.13.6


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 3/4] client: Add generator for scan:clear
  2017-12-15 16:09 [PATCH v2 1/4] client: Group discovery filter variables into a struct Luiz Augusto von Dentz
  2017-12-15 16:09 ` [PATCH v2 2/4] client: Make scan:clear clear individual fields Luiz Augusto von Dentz
@ 2017-12-15 16:09 ` Luiz Augusto von Dentz
  2017-12-15 16:09 ` [PATCH v2 4/4] clien: Only set the discovery filter when necessary Luiz Augusto von Dentz
  2017-12-16 16:21 ` [PATCH v2 1/4] client: Group discovery filter variables into a struct Johan Hedberg
  3 siblings, 0 replies; 5+ messages in thread
From: Luiz Augusto von Dentz @ 2017-12-15 16:09 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

This adds tab generator for scan:clear:

[bluetooth]# clear
duplicate-data  pathloss        rssi            transport       uuids
---
 client/main.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/client/main.c b/client/main.c
index 53e03f56f..9c77a34b9 100644
--- a/client/main.c
+++ b/client/main.c
@@ -1432,6 +1432,26 @@ static const struct filter_clear {
 	{}
 };
 
+static char *filter_clear_generator(const char *text, int state)
+{
+	static int index, len;
+	const char *arg;
+
+	if (!state) {
+		index = 0;
+		len = strlen(text);
+	}
+
+	while ((arg = filter_clear[index].name)) {
+		index++;
+
+		if (!strncmp(arg, text, len))
+			return strdup(arg);
+	}
+
+	return NULL;
+}
+
 static void cmd_scan_filter_clear(int argc, char *argv[])
 {
 	const struct filter_clear *fc;
@@ -2308,7 +2328,8 @@ static const struct bt_shell_menu scan_menu = {
 				mode_generator },
 	{ "clear", "[uuids/rssi/pathloss/transport/duplicate-data]",
 				cmd_scan_filter_clear,
-				"Clears discovery filter." },
+				"Clears discovery filter.",
+				filter_clear_generator },
 	{ } },
 };
 
-- 
2.13.6


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 4/4] clien: Only set the discovery filter when necessary
  2017-12-15 16:09 [PATCH v2 1/4] client: Group discovery filter variables into a struct Luiz Augusto von Dentz
  2017-12-15 16:09 ` [PATCH v2 2/4] client: Make scan:clear clear individual fields Luiz Augusto von Dentz
  2017-12-15 16:09 ` [PATCH v2 3/4] client: Add generator for scan:clear Luiz Augusto von Dentz
@ 2017-12-15 16:09 ` Luiz Augusto von Dentz
  2017-12-16 16:21 ` [PATCH v2 1/4] client: Group discovery filter variables into a struct Johan Hedberg
  3 siblings, 0 replies; 5+ messages in thread
From: Luiz Augusto von Dentz @ 2017-12-15 16:09 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Only set the discovery filter when the user wants execute the scan
command.
---
 client/main.c | 101 ++++++++++++++++++++++++++--------------------------------
 1 file changed, 45 insertions(+), 56 deletions(-)

diff --git a/client/main.c b/client/main.c
index 9c77a34b9..3b43ed777 100644
--- a/client/main.c
+++ b/client/main.c
@@ -1097,31 +1097,6 @@ static void start_discovery_reply(DBusMessage *message, void *user_data)
 	bt_shell_printf("Discovery %s\n", enable == TRUE ? "started" : "stopped");
 }
 
-static void cmd_scan(int argc, char *argv[])
-{
-	dbus_bool_t enable;
-	const char *method;
-
-	if (!parse_argument(argc, argv, NULL, NULL, &enable, NULL))
-		return;
-
-	if (check_default_ctrl() == FALSE)
-		return;
-
-	if (enable == TRUE)
-		method = "StartDiscovery";
-	else
-		method = "StopDiscovery";
-
-	if (g_dbus_proxy_method_call(default_ctrl->proxy, method,
-				NULL, start_discovery_reply,
-				GUINT_TO_POINTER(enable), NULL) == FALSE) {
-		bt_shell_printf("Failed to %s discovery\n",
-					enable == TRUE ? "start" : "stop");
-		return;
-	}
-}
-
 static void append_variant(DBusMessageIter *iter, int type, void *val)
 {
 	DBusMessageIter value;
@@ -1211,13 +1186,18 @@ static void dict_append_array(DBusMessageIter *dict, const char *key, int type,
 
 #define	DISTANCE_VAL_INVALID	0x7FFF
 
-struct set_discovery_filter_args {
+static struct set_discovery_filter_args {
 	char *transport;
 	dbus_uint16_t rssi;
 	dbus_int16_t pathloss;
 	char **uuids;
 	size_t uuids_len;
 	dbus_bool_t duplicate;
+	bool set;
+} filter = {
+	.rssi = DISTANCE_VAL_INVALID,
+	.pathloss = DISTANCE_VAL_INVALID,
+	.set = true,
 };
 
 static void set_discovery_filter_setup(DBusMessageIter *iter, void *user_data)
@@ -1264,17 +1244,14 @@ static void set_discovery_filter_reply(DBusMessage *message, void *user_data)
 		return;
 	}
 
+	filter.set = true;
+
 	bt_shell_printf("SetDiscoveryFilter success\n");
 }
 
-static struct set_discovery_filter_args filter = {
-	.rssi = DISTANCE_VAL_INVALID,
-	.pathloss = DISTANCE_VAL_INVALID,
-};
-
-static void cmd_set_scan_filter_commit(void)
+static void set_discovery_filter(void)
 {
-	if (check_default_ctrl() == FALSE)
+	if (check_default_ctrl() == FALSE || filter.set)
 		return;
 
 	if (g_dbus_proxy_method_call(default_ctrl->proxy, "SetDiscoveryFilter",
@@ -1283,6 +1260,34 @@ static void cmd_set_scan_filter_commit(void)
 		bt_shell_printf("Failed to set discovery filter\n");
 		return;
 	}
+
+	filter.set = true;
+}
+
+static void cmd_scan(int argc, char *argv[])
+{
+	dbus_bool_t enable;
+	const char *method;
+
+	if (!parse_argument(argc, argv, NULL, NULL, &enable, NULL))
+		return;
+
+	if (check_default_ctrl() == FALSE)
+		return;
+
+	if (enable == TRUE) {
+		set_discovery_filter();
+		method = "StartDiscovery";
+	} else
+		method = "StopDiscovery";
+
+	if (g_dbus_proxy_method_call(default_ctrl->proxy, method,
+				NULL, start_discovery_reply,
+				GUINT_TO_POINTER(enable), NULL) == FALSE) {
+		bt_shell_printf("Failed to %s discovery\n",
+					enable == TRUE ? "start" : "stop");
+		return;
+	}
 }
 
 static void cmd_scan_filter_uuids(int argc, char *argv[])
@@ -1312,7 +1317,7 @@ static void cmd_scan_filter_uuids(int argc, char *argv[])
 	filter.uuids_len = g_strv_length(filter.uuids);
 
 commit:
-	cmd_set_scan_filter_commit();
+	filter.set = false;
 }
 
 static void cmd_scan_filter_rssi(int argc, char *argv[])
@@ -1326,7 +1331,7 @@ static void cmd_scan_filter_rssi(int argc, char *argv[])
 	filter.pathloss = DISTANCE_VAL_INVALID;
 	filter.rssi = atoi(argv[1]);
 
-	cmd_set_scan_filter_commit();
+	filter.set = false;
 }
 
 static void cmd_scan_filter_pathloss(int argc, char *argv[])
@@ -1341,7 +1346,7 @@ static void cmd_scan_filter_pathloss(int argc, char *argv[])
 	filter.rssi = DISTANCE_VAL_INVALID;
 	filter.pathloss = atoi(argv[1]);
 
-	cmd_set_scan_filter_commit();
+	filter.set = false;
 }
 
 static void cmd_scan_filter_transport(int argc, char *argv[])
@@ -1356,7 +1361,7 @@ static void cmd_scan_filter_transport(int argc, char *argv[])
 	g_free(filter.transport);
 	filter.transport = g_strdup(argv[1]);
 
-	cmd_set_scan_filter_commit();
+	filter.set = false;
 }
 
 static void cmd_scan_filter_duplicate_data(int argc, char *argv[])
@@ -1376,20 +1381,7 @@ static void cmd_scan_filter_duplicate_data(int argc, char *argv[])
 		return;
 	}
 
-	cmd_set_scan_filter_commit();
-}
-
-static void clear_discovery_filter_setup(DBusMessageIter *iter, void *user_data)
-{
-	DBusMessageIter dict;
-
-	dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
-				DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
-				DBUS_TYPE_STRING_AS_STRING
-				DBUS_TYPE_VARIANT_AS_STRING
-				DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
-
-	dbus_message_iter_close_container(iter, &dict);
+	filter.set = false;
 }
 
 static void filter_clear_uuids(void)
@@ -1463,6 +1455,7 @@ static void cmd_scan_filter_clear(int argc, char *argv[])
 	for (fc = filter_clear; fc && fc->name; fc++) {
 		if (all || !strcmp(fc->name, argv[1])) {
 			fc->clear();
+			filter.set = false;
 			if (!all)
 				goto done;
 		}
@@ -1477,11 +1470,7 @@ done:
 	if (check_default_ctrl() == FALSE)
 		return;
 
-	if (g_dbus_proxy_method_call(default_ctrl->proxy, "SetDiscoveryFilter",
-		clear_discovery_filter_setup, set_discovery_filter_reply,
-		NULL, NULL) == FALSE) {
-		bt_shell_printf("Failed to clear discovery filter\n");
-	}
+	set_discovery_filter();
 }
 
 static struct GDBusProxy *find_device(int argc, char *argv[])
-- 
2.13.6


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 1/4] client: Group discovery filter variables into a struct
  2017-12-15 16:09 [PATCH v2 1/4] client: Group discovery filter variables into a struct Luiz Augusto von Dentz
                   ` (2 preceding siblings ...)
  2017-12-15 16:09 ` [PATCH v2 4/4] clien: Only set the discovery filter when necessary Luiz Augusto von Dentz
@ 2017-12-16 16:21 ` Johan Hedberg
  3 siblings, 0 replies; 5+ messages in thread
From: Johan Hedberg @ 2017-12-16 16:21 UTC (permalink / raw)
  To: Luiz Augusto von Dentz; +Cc: linux-bluetooth

Hi Luiz,

On Fri, Dec 15, 2017, Luiz Augusto von Dentz wrote:
> This should be easier to read and maintain.
> ---
>  client/main.c | 82 +++++++++++++++++++++++++----------------------------------
>  1 file changed, 35 insertions(+), 47 deletions(-)

All patches in this set have been applied. Thanks.

Johan

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-12-16 16:21 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-12-15 16:09 [PATCH v2 1/4] client: Group discovery filter variables into a struct Luiz Augusto von Dentz
2017-12-15 16:09 ` [PATCH v2 2/4] client: Make scan:clear clear individual fields Luiz Augusto von Dentz
2017-12-15 16:09 ` [PATCH v2 3/4] client: Add generator for scan:clear Luiz Augusto von Dentz
2017-12-15 16:09 ` [PATCH v2 4/4] clien: Only set the discovery filter when necessary Luiz Augusto von Dentz
2017-12-16 16:21 ` [PATCH v2 1/4] client: Group discovery filter variables into a struct Johan Hedberg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).