Skip to contentSkip to conversation

client: support multi-line print for long values

PATCH In review James Prestwood 2 participants 3 messages Jun 29, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 client: support multi-line print for long values

The generic proxy property display was limited to a width for names/value which makes the output look nice and uniform, but will cut off any values which are longer than this limit.

This patch adds some logic to detect this, and continue displaying the value on the next line.

The width arguments were also updated to be unsigned, which allows checking the length without a cast.

---
 client/dbus-proxy.c | 21 +++++++++++++++++++--
 client/dbus-proxy.h |  3 ++-
 2 files changed, 21 insertions(+), 3 deletions(-)
client/dbus-proxy.c +19 -2
index 624ce4aa..9f05c0e7 100644
@@ -49,7 +49,8 @@ static struct l_queue *proxy_interface_types;
 
 void proxy_properties_display(const struct proxy_interface *proxy,
 				const char *caption, const char *margin,
-				int name_column_width, int value_column_width)
+				unsigned int name_column_width,
+				unsigned int value_column_width)
 {
 	const void *data;
 	const struct proxy_interface_property *properties;
@@ -67,14 +68,30 @@ void proxy_properties_display(const struct proxy_interface *proxy,
 	properties = proxy->type->properties;
 
 	for (i = 0; properties[i].name; i++) {
+		const char *str;
+		size_t len;
+		size_t j;
+
 		if (!properties[i].tostr)
 			continue;
 
+		str = properties[i].tostr(data);
+		len = str ? strlen(str) : 0;
+
 		display("%s%*s  %-*s%-.*s\n", margin,
 			8, properties[i].is_read_write ?
 				COLOR_BOLDGRAY "       *" COLOR_OFF : "",
 			name_column_width, properties[i].name,
-			value_column_width, properties[i].tostr(data) ? : "");
+			value_column_width, str ? : "");
+
+		if (len <= value_column_width)
+			continue;
+
+		/* Display remaining data */
+		for (j = value_column_width; j < len; j += value_column_width)
+			display("%s%*s  %-*s%-.*s\n", margin, 8, "",
+				name_column_width, "", value_column_width,
+				str + j);
 	}
 }
 
client/dbus-proxy.h +2 -1
index 1d6af5de..34917e82 100644
@@ -89,7 +89,8 @@ bool proxy_interface_method_call(const struct proxy_interface *proxy,
 
 void proxy_properties_display(const struct proxy_interface *proxy,
 				const char *caption, const char *margin,
-				int name_column_width, int value_column_width);
+				unsigned int name_column_width,
+				unsigned int value_column_width);
 
 char *proxy_property_str_completion(const struct proxy_interface_type *type,
 					proxy_property_match_func_t function,
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/2 client: add "dpp <wlan> show" command

This will show the newly added DPP properties

---
 client/dpp.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 117 insertions(+)
client/dpp.c +117 -0
index 120dd9ee..af69d2c0 100644
@@ -34,8 +34,111 @@
 #include "client/device.h"
 #include "client/display.h"
 
+struct dpp {
+	bool started;
+	char *role;
+	char *uri;
+};
+
+static void *dpp_create(void)
+{
+	return l_new(struct dpp, 1);
+}
+
+static void dpp_destroy(void *data)
+{
+	struct dpp *dpp = data;
+
+	if (dpp->role)
+		l_free(dpp->role);
+	if (dpp->uri)
+		l_free(dpp->uri);
+
+	l_free(dpp);
+}
+
+static void update_started(void *data, struct l_dbus_message_iter *variant)
+{
+	struct dpp *dpp = data;
+	bool value;
+
+	if (!l_dbus_message_iter_get_variant(variant, "b", &value)) {
+		dpp->started = false;
+		return;
+	}
+
+	dpp->started = value;
+}
+
+static const char *started_tostr(const void *data)
+{
+	const struct dpp *dpp = data;
+
+	return dpp->started ? "yes" : "no";
+}
+
+static void update_role(void *data, struct l_dbus_message_iter *variant)
+{
+	struct dpp *dpp = data;
+	const char *value;
+
+	if (dpp->role)
+		l_free(dpp->role);
+
+	if (!l_dbus_message_iter_get_variant(variant, "s", &value)) {
+		dpp->role = NULL;
+		return;
+	}
+
+	dpp->role = l_strdup(value);
+}
+
+static const char *role_tostr(const void *data)
+{
+	const struct dpp *dpp = data;
+
+	return dpp->role;
+}
+
+static void update_uri(void *data, struct l_dbus_message_iter *variant)
+{
+	struct dpp *dpp = data;
+	const char *value;
+
+	if (dpp->uri)
+		l_free(dpp->uri);
+
+	if (!l_dbus_message_iter_get_variant(variant, "s", &value)) {
+		dpp->uri = NULL;
+		return;
+	}
+
+	dpp->uri = l_strdup(value);
+}
+
+static const char *uri_tostr(const void *data)
+{
+	const struct dpp *dpp = data;
+
+	return dpp->uri;
+}
+
+static const struct proxy_interface_property dpp_properties[] = {
+	{ "Started",	"b", update_started,	started_tostr },
+	{ "Role",	"s", update_role, 	role_tostr },
+	{ "URI",	"s", update_uri,	uri_tostr },
+	{ }
+};
+
+static const struct proxy_interface_type_ops dpp_ops = {
+	.create = dpp_create,
+	.destroy = dpp_destroy,
+};
+
 static struct proxy_interface_type dpp_interface_type = {
 	.interface = IWD_DPP_INTERFACE,
+	.properties = dpp_properties,
+	.ops = &dpp_ops,
 };
 
 static void check_errors_method_callback(struct l_dbus_message *message,
@@ -174,6 +277,19 @@ static enum cmd_status cmd_stop(const char *device_name,
 	return CMD_STATUS_TRIGGERED;
 }
 
+static enum cmd_status cmd_show(const char *device_name,
+						char **argv, int argc)
+{
+	const struct proxy_interface *proxy =
+			device_proxy_find(device_name, IWD_DPP_INTERFACE);
+	char *caption = l_strdup_printf("%s: %s", "DPP", device_name);
+
+	proxy_properties_display(proxy, caption, MARGIN, 20, 47);
+	l_free(caption);
+
+	return CMD_STATUS_DONE;
+}
+
 static const struct command dpp_commands[] = {
 	{ NULL, "list", NULL, cmd_list, "List DPP-capable devices", true },
 	{ "<wlan>", "start-enrollee", NULL, cmd_start_enrollee,
@@ -181,6 +297,7 @@ static const struct command dpp_commands[] = {
 	{ "<wlan>", "start-configurator", NULL, cmd_start_configurator,
 							"Starts a DPP Configurator" },
 	{ "<wlan>", "stop", NULL, cmd_stop, "Aborts DPP operations" },
+	{ "<wlan>", "show", NULL, cmd_show, "Shows the DPP state" },
 	{ }
 };
 
Signature · 1 line
2.34.1
#2 lore raw
In reply to James Prestwood

Hi James,

Quoted 7 lines · James Prestwood

The generic proxy property display was limited to a width for names/value which makes the output look nice and uniform, but will cut off any values which are longer than this limit.

This patch adds some logic to detect this, and continue displaying the value on the next line.

Quoted 7 lines

The width arguments were also updated to be unsigned, which allows checking the length without a cast.

---
  client/dbus-proxy.c | 21 +++++++++++++++++++--
  client/dbus-proxy.h |  3 ++-
  2 files changed, 21 insertions(+), 3 deletions(-)

Both applied, thanks.

Regards,
-Denis