Skip to contentSkip to conversation

shared/shell: Don't allocate any data if env value is NULL

PATCH Applied Luiz Augusto von Dentz 1 participant 5 messages Mar 26, 2018 – Mar 29, 2018 Bluetooth drivers

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH 1/4 shared/shell: Don't allocate any data if env value is NULL

From: Luiz Augusto von Dentz <[email protected]>

If value is NULL there is no point in allocating any data if value is NULL since bt_shell_get_env would return NULL anyway, also this makes sure the existing value is freed which means passing NULL can remove an env like it was intended.

---
 src/shared/shell.c | 6 ++++++
 1 file changed, 6 insertions(+)
src/shared/shell.c +6 -0
index 33bc0d980..241878b30 100644
@@ -1142,6 +1142,8 @@ void bt_shell_set_env(const char *name, void *value)
 	struct bt_shell_env *env;
 
 	if (!data.envs) {
+		if (!value)
+			return;
 		data.envs = queue_new();
 		goto done;
 	}
@@ -1150,6 +1152,10 @@ void bt_shell_set_env(const char *name, void *value)
 	if (env)
 		env_destroy(env);
 
+	/* Don't create an env if value is not set */
+	if (!value)
+		return;
+
 done:
 	env = new0(struct bt_shell_env, 1);
 	env->name = strdup(name);
Signature · 1 line
2.14.3
author maintainer
#1 lore raw

PATCH 2/4 shared/shell: Add export command

From: Luiz Augusto von Dentz <[email protected]>

This adds export command to default menu which can be used to print the environment variables that have been set.

---
 src/shared/shell.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
src/shared/shell.c +13 -0
index 241878b30..552e066d7 100644
@@ -198,6 +198,17 @@ static bool cmd_back_exists(const struct bt_shell_menu *menu)
 	return true;
 }
 
+static void cmd_export(int argc, char *argv[])
+{
+	const struct queue_entry *entry;
+
+	for (entry = queue_get_entries(data.envs); entry; entry = entry->next) {
+		struct bt_shell_env *env = entry->data;
+
+		print_text(COLOR_HIGHLIGHT, "%s=%p", env->name, env->value);
+	}
+}
+
 static const struct bt_shell_menu_entry default_menu[] = {
 	{ "back",         NULL,       cmd_back, "Return to main menu", NULL,
 							NULL, cmd_back_exists },
@@ -209,6 +220,8 @@ static const struct bt_shell_menu_entry default_menu[] = {
 	{ "exit",         NULL,       cmd_quit, "Quit program" },
 	{ "help",         NULL,       cmd_help,
 					"Display help about this program" },
+	{ "export",       NULL,       cmd_export,
+						"Print evironment variables" },
 	{ }
 };
 
Signature · 1 line
2.14.3
author maintainer
#2 lore raw

PATCH 3/4 client: Add envs for proxies

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This adds envs for device and adapter proxies found so other files are able to access them.

---
 client/main.c | 4 ++++
 1 file changed, 4 insertions(+)
client/main.c +4 -0
index 584217a82..a36e932ac 100644
@@ -456,6 +456,7 @@ static void device_added(GDBusProxy *proxy)
 
 	adapter->devices = g_list_append(adapter->devices, proxy);
 	print_device(proxy, COLORED_NEW);
+	bt_shell_set_env(g_dbus_proxy_get_path(proxy), proxy);
 
 	if (default_dev)
 		return;
@@ -494,6 +495,7 @@ static void adapter_added(GDBusProxy *proxy)
 	adapter->proxy = proxy;
 
 	print_adapter(proxy, COLORED_NEW);
+	bt_shell_set_env(g_dbus_proxy_get_path(proxy), proxy);
 }
 
 static void ad_manager_added(GDBusProxy *proxy)
@@ -561,6 +563,7 @@ static void device_removed(GDBusProxy *proxy)
 	adapter->devices = g_list_remove(adapter->devices, proxy);
 
 	print_device(proxy, COLORED_DEL);
+	bt_shell_set_env(g_dbus_proxy_get_path(proxy), NULL);
 
 	if (default_dev == proxy)
 		set_default_device(NULL, NULL);
@@ -575,6 +578,7 @@ static void adapter_removed(GDBusProxy *proxy)
 
 		if (adapter->proxy == proxy) {
 			print_adapter(proxy, COLORED_DEL);
+			bt_shell_set_env(g_dbus_proxy_get_path(proxy), NULL);
 
 			if (default_ctrl && default_ctrl->proxy == proxy) {
 				default_ctrl = NULL;
Signature · 1 line
2.14.3
author maintainer
#3 lore raw

PATCH 4/4 client: Attempt to convert the device path to address

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

Printing the object path is not only long but it also may change.

---
 client/gatt.c | 32 ++++++++++++++++++++++++--------
 1 file changed, 24 insertions(+), 8 deletions(-)
client/gatt.c +24 -8
index cbf7adf57..7103c4f83 100644
@@ -1415,6 +1415,20 @@ static const GDBusPropertyTable chrc_properties[] = {
 	{ }
 };
 
+static const char *path_to_address(const char *path)
+{
+	GDBusProxy *proxy;
+	DBusMessageIter iter;
+	const char *address = path;
+
+	proxy = bt_shell_get_env(path);
+
+	if (g_dbus_proxy_get_property(proxy, "Address", &iter))
+		dbus_message_iter_get_basic(&iter, &address);
+
+	return address;
+}
+
 static int parse_options(DBusMessageIter *iter, uint16_t *offset, uint16_t *mtu,
 						char **device, char **link)
 {
@@ -1539,8 +1553,8 @@ static DBusMessage *chrc_read_value(DBusConnection *conn, DBusMessage *msg,
 					"org.bluez.Error.InvalidArguments",
 					NULL);
 
-	bt_shell_printf("ReadValue: %s offset %u link %s\n", device, offset,
-								link);
+	bt_shell_printf("ReadValue: %s offset %u link %s\n",
+			path_to_address(device), offset, link);
 
 	if (chrc->authorization_req && offset == 0)
 		chrc->authorized = false;
@@ -1722,7 +1736,8 @@ static DBusMessage *chrc_acquire_write(DBusConnection *conn, DBusMessage *msg,
 					"org.bluez.Error.InvalidArguments",
 					NULL);
 
-	bt_shell_printf("AcquireWrite: %s link %s\n", device, link);
+	bt_shell_printf("AcquireWrite: %s link %s\n", path_to_address(device),
+									link);
 
 	reply = chrc_create_pipe(chrc, msg);
 
@@ -1753,7 +1768,8 @@ static DBusMessage *chrc_acquire_notify(DBusConnection *conn, DBusMessage *msg,
 					"org.bluez.Error.InvalidArguments",
 					NULL);
 
-	bt_shell_printf("AcquireNotify: %s link %s\n", device, link);
+	bt_shell_printf("AcquireNotify: %s link %s\n", path_to_address(device),
+									link);
 
 	reply = chrc_create_pipe(chrc, msg);
 
@@ -1961,8 +1977,8 @@ static DBusMessage *desc_read_value(DBusConnection *conn, DBusMessage *msg,
 					"org.bluez.Error.InvalidArguments",
 					NULL);
 
-	bt_shell_printf("ReadValue: %s offset %u link %s\n", device, offset,
-								link);
+	bt_shell_printf("ReadValue: %s offset %u link %s\n",
+			path_to_address(device), offset, link);
 
 	if (offset > desc->value_len)
 		return g_dbus_create_error(msg, "org.bluez.Error.InvalidOffset",
@@ -1993,8 +2009,8 @@ static DBusMessage *desc_write_value(DBusConnection *conn, DBusMessage *msg,
 					"org.bluez.Error.InvalidArguments",
 					NULL);
 
-	bt_shell_printf("WriteValue: %s offset %u link %s\n", device, offset,
-								link);
+	bt_shell_printf("WriteValue: %s offset %u link %s\n",
+			path_to_address(device), offset, link);
 
 	bt_shell_printf("[" COLORED_CHG "] Attribute %s written" , desc->path);
 
Signature · 1 line
2.14.3
author maintainer Applied
#4 lore raw
In reply to Luiz Augusto von Dentz

Hi,

Quoted 35 lines · Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

If value is NULL there is no point in allocating any data if value is NULL since bt_shell_get_env would return NULL anyway, also this makes sure the existing value is freed which means passing NULL can remove an env like it was intended.

---
 src/shared/shell.c | 6 ++++++
 1 file changed, 6 insertions(+)
src/shared/shell.c +6 -1
index 33bc0d980..241878b30 100644
@@ -1142,6 +1142,8 @@ void bt_shell_set_env(const char *name, void *value)
        struct bt_shell_env *env;
 
        if (!data.envs) {
+               if (!value)
+                       return;
                data.envs = queue_new();
                goto done;
        }
@@ -1150,6 +1152,10 @@ void bt_shell_set_env(const char *name, void *value)
        if (env)
                env_destroy(env);
 
+       /* Don't create an env if value is not set */
+       if (!value)
+               return;
+
 done:
        env = new0(struct bt_shell_env, 1);
        env->name = strdup(name);
--
Quoted 1 line

2.14.3

Applied.

Signature · 1 line
Luiz Augusto von Dentz