Skip to contentSkip to conversation

advertising: Ensure all instances are reset during init

PATCH Applied Luiz Augusto von Dentz 1 participant 15 messages Aug 10, 2017 – Aug 11, 2017 Bluetooth drivers

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH v8 1/14 advertising: Ensure all instances are reset during init

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

If for some reason bluetoothd crashes or is killed it may leave instances behind.

---
 src/advertising.c | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)
src/advertising.c +23 -1
index c2af3acb4..64d5a27bb 100644
@@ -144,6 +144,22 @@ static void client_destroy(void *data)
 	client_free(data);
 }
 
+static void remove_advertising(struct btd_adv_manager *manager,
+						uint8_t instance)
+{
+	struct mgmt_cp_remove_advertising cp;
+
+	if (instance)
+		DBG("instance %u", instance);
+	else
+		DBG("all instances");
+
+	cp.instance = instance;
+
+	mgmt_send(manager->mgmt, MGMT_OP_REMOVE_ADVERTISING,
+			manager->mgmt_index, sizeof(cp), &cp, NULL, NULL, NULL);
+}
+
 static void client_remove(void *data)
 {
 	struct btd_adv_client *client = data;
@@ -791,8 +807,14 @@ static void read_adv_features_callback(uint8_t status, uint16_t length,
 	if (!g_dbus_register_interface(btd_get_dbus_connection(),
 					adapter_get_path(manager->adapter),
 					LE_ADVERTISING_MGR_IFACE, methods,
-					NULL, properties, manager, NULL))
+					NULL, properties, manager, NULL)) {
 		error("Failed to register " LE_ADVERTISING_MGR_IFACE);
+		return;
+	}
+
+	/* Reset existing instances */
+	if (feat->num_instances)
+		remove_advertising(manager, 0);
 }
 
 static struct btd_adv_manager *manager_create(struct btd_adapter *adapter)
Signature · 1 line
2.13.4
author maintainer
#1 lore raw

PATCH v8 2/14 advertising: Rename AdvertisementInstaces to SupportedInstances

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

This fixes the typo and removes the term advertisement since it is implicit given the interface name, also make it experimental since AdvertisingManager is still experimental.

---
 doc/advertising-api.txt | 4 ++--
 src/advertising.c       | 9 ++++-----
 2 files changed, 6 insertions(+), 7 deletions(-)
doc/advertising-api.txt +2 -2
index 7a29b7095..4daa30b75 100644
@@ -113,6 +113,6 @@ Methods		RegisterAdvertisement(object advertisement, dict options)
 			Possible errors: org.bluez.Error.InvalidArguments
 					 org.bluez.Error.DoesNotExist
 
-Properties	byte AdvertisementInstaces
+Properties	byte SupportedInstances
 
-			Number of available advertisement instances.
+			Number of available advertising instances.
src/advertising.c +4 -5
index 64d5a27bb..f92d5d33f 100644
@@ -179,8 +179,7 @@ static void client_remove(void *data)
 
 	g_dbus_emit_property_changed(btd_get_dbus_connection(),
 				adapter_get_path(client->manager->adapter),
-				LE_ADVERTISING_MGR_IFACE,
-				"AdvertisementInstances");
+				LE_ADVERTISING_MGR_IFACE, "SupportedInstances");
 }
 
 static void client_disconnect_cb(DBusConnection *conn, void *user_data)
@@ -469,8 +468,7 @@ static void add_adv_callback(uint8_t status, uint16_t length,
 
 	g_dbus_emit_property_changed(btd_get_dbus_connection(),
 				adapter_get_path(client->manager->adapter),
-				LE_ADVERTISING_MGR_IFACE,
-				"AdvertisementInstances");
+				LE_ADVERTISING_MGR_IFACE, "SupportedInstances");
 
 done:
 	add_client_complete(client, status);
@@ -755,7 +753,8 @@ static gboolean get_instances(const GDBusPropertyTable *property,
 }
 
 static const GDBusPropertyTable properties[] = {
-	{ "AdvertisementInstances", "y", get_instances },
+	{ "SupportedInstances", "y", get_instances, NULL, NULL,
+					G_DBUS_PROPERTY_FLAG_EXPERIMENTAL },
 };
 
 static const GDBusMethodTable methods[] = {
Signature · 1 line
2.13.4
author maintainer
#2 lore raw

PATCH v8 3/14 advertising: Add ActiveInstances property

In reply to Luiz Augusto von Dentz

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

This property indicates how many instance are currently in use.

---
 doc/advertising-api.txt |  6 +++++-
 src/advertising.c       | 24 ++++++++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)
doc/advertising-api.txt +5 -1
index 4daa30b75..a8a738c0f 100644
@@ -113,6 +113,10 @@ Methods		RegisterAdvertisement(object advertisement, dict options)
 			Possible errors: org.bluez.Error.InvalidArguments
 					 org.bluez.Error.DoesNotExist
 
-Properties	byte SupportedInstances
+Properties	byte ActiveInstances
+
+			Number of active advertising instances.
+
+		byte SupportedInstances
 
 			Number of available advertising instances.
src/advertising.c +24 -0
index f92d5d33f..0d3487cd3 100644
@@ -180,6 +180,10 @@ static void client_remove(void *data)
 	g_dbus_emit_property_changed(btd_get_dbus_connection(),
 				adapter_get_path(client->manager->adapter),
 				LE_ADVERTISING_MGR_IFACE, "SupportedInstances");
+
+	g_dbus_emit_property_changed(btd_get_dbus_connection(),
+				adapter_get_path(client->manager->adapter),
+				LE_ADVERTISING_MGR_IFACE, "ActiveInstances");
 }
 
 static void client_disconnect_cb(DBusConnection *conn, void *user_data)
@@ -470,6 +474,10 @@ static void add_adv_callback(uint8_t status, uint16_t length,
 				adapter_get_path(client->manager->adapter),
 				LE_ADVERTISING_MGR_IFACE, "SupportedInstances");
 
+	g_dbus_emit_property_changed(btd_get_dbus_connection(),
+				adapter_get_path(client->manager->adapter),
+				LE_ADVERTISING_MGR_IFACE, "ActiveInstances");
+
 done:
 	add_client_complete(client, status);
 }
@@ -752,9 +760,25 @@ static gboolean get_instances(const GDBusPropertyTable *property,
 	return TRUE;
 }
 
+static gboolean get_active_instances(const GDBusPropertyTable *property,
+					DBusMessageIter *iter, void *data)
+{
+	struct btd_adv_manager *manager = data;
+	uint8_t instances;
+
+	instances = queue_length(manager->clients);
+
+	dbus_message_iter_append_basic(iter, DBUS_TYPE_BYTE, &instances);
+
+	return TRUE;
+}
+
 static const GDBusPropertyTable properties[] = {
+	{ "ActiveInstances", "y", get_active_instances, NULL, NULL,
+					G_DBUS_PROPERTY_FLAG_EXPERIMENTAL },
 	{ "SupportedInstances", "y", get_instances, NULL, NULL,
 					G_DBUS_PROPERTY_FLAG_EXPERIMENTAL },
+	{ }
 };
 
 static const GDBusMethodTable methods[] = {
Signature · 1 line
2.13.4
author maintainer
#3 lore raw

PATCH v8 4/14 advertising: Add SupportedIncludes

In reply to Luiz Augusto von Dentz

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

This adds SupportedIncludes so application can detect what the system can include.

---
 doc/advertising-api.txt |  8 ++++++++
 src/advertising.c       | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+)
doc/advertising-api.txt +8 -0
index a8a738c0f..c39b5ec55 100644
@@ -120,3 +120,11 @@ Properties	byte ActiveInstances
 		byte SupportedInstances
 
 			Number of available advertising instances.
+
+		array{string} SupportedIncludes
+
+			List of supported system includes.
+
+			Possible values: "tx-power"
+					 "appearance"
+					 "local-name"
src/advertising.c +42 -0

65 lines not on this page · raw message

Signature · 1 line
2.13.4
author maintainer
#4 lore raw

PATCH v8 5/14 advertising: Add Includes property

In reply to Luiz Augusto von Dentz

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

This enables using the all the supporting includes.

Note: IncludeTxPower has been moved under Includes as well since it was still experimental.

---
 doc/advertising-api.txt |   8 +-
 src/advertising.c       | 196 ++++++++++++++++++++++++------------------------
 2 files changed, 104 insertions(+), 100 deletions(-)
doc/advertising-api.txt +5 -3
index c39b5ec55..65f1331a9 100644
@@ -61,11 +61,13 @@ Properties	string Type
 			Service Data elements to include. The keys are the
 			UUID to associate with the data.
 
-		bool IncludeTxPower
+		array{string} Includes
 
-			Includes the Tx Power in the advertising packet.
-			If missing, the Tx Power is not included.
+			List of features to be included in the advertising
+			packet.
 
+			Possible values: as found on
+					LEAdvertisingManager.SupportedIncludes
 
 LE Advertising Manager hierarchy
 ================================
src/advertising.c +99 -97

335 lines not on this page · raw message

Signature · 1 line
2.13.4
author maintainer
#5 lore raw

PATCH v8 6/14 client: Add set-advertise-name and set-advertise-appearance

In reply to Luiz Augusto von Dentz

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

set-advertise-appearance enables the use of adapter's appearance in the scan response:

[bluetooth]# set-advertise-appearance on
[bluetooth]# advertise on

@ MGMT Command: Add Advertising (0x003e) plen 11
Instance: 1
Flags: 0x00000023
Switch into Connectable mode Advertise as Discoverable
Add Appearance field to Scan Response
Duration: 0
Timeout: 0
Advertising data length: 0
Scan response length: 0

set-advertise-name enables the use of adapter's name/alias in the scan response:

[bluetooth]# set-advertise-name on
[bluetooth]# advertise on

@ MGMT Command: Add Advertising (0x003e) plen 11
Instance: 1
Flags: 0x00000043
Switch into Connectable mode Advertise as Discoverable
Add Local Name in Scan Response
Duration: 0
Timeout: 0
Advertising data length: 0
Scan response length: 0

---
 client/advertising.c | 166 +++++++++++++++++++++++++++++++++------------------
 client/advertising.h |   4 +-
 client/main.c        |  48 ++++++++++++++-
 3 files changed, 156 insertions(+), 62 deletions(-)
client/advertising.c +107 -59
index 81fa85118..35e582afc 100644
@@ -28,6 +28,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
+#include <stdbool.h>
 #include <readline/readline.h>
 #include <wordexp.h>
 
@@ -38,21 +39,36 @@
 #define AD_PATH "/org/bluez/advertising"
 #define AD_IFACE "org.bluez.LEAdvertisement1"
 
-static gboolean registered = FALSE;
-static char *ad_type = NULL;
-static char **ad_uuids = NULL;
-static size_t ad_uuids_len = 0;
-static char *ad_service_uuid = NULL;
-static uint8_t ad_service_data[25];
-static uint8_t ad_service_data_len = 0;
-static uint16_t ad_manufacturer_id;
-static uint8_t ad_manufacturer_data[25];
-static uint8_t ad_manufacturer_data_len = 0;
-static gboolean ad_tx_power = FALSE;
+struct ad_data {
+	uint8_t data[25];
+	uint8_t len;
+};
+
+struct service_data {
+	char *uuid;
+	struct ad_data data;
+};
+
+struct manufacturer_data {
+	uint16_t id;
+	struct ad_data data;
+};
+
+static struct ad {
+	bool registered;
+	char *type;
+	char **uuids;
+	size_t uuids_len;
+	struct service_data service;
+	struct manufacturer_data manufacturer;
+	bool tx_power;
+	bool name;
+	bool appearance;
+} ad;
 
 static void ad_release(DBusConnection *conn)
 {
-	registered = FALSE;
+	ad.registered = false;
 
 	g_dbus_unregister_interface(conn, AD_PATH, AD_IFACE);
 }
@@ -94,7 +110,7 @@ static void register_reply(DBusMessage *message, void *user_data)
 	dbus_error_init(&error);
 
 	if (dbus_set_error_from_message(&error, message) == FALSE) {
-		registered = TRUE;
+		ad.registered = true;
 		rl_printf("Advertising object registered\n");
 	} else {
 		rl_printf("Failed to register advertisement: %s\n", error.name);
@@ -111,8 +127,8 @@ static gboolean get_type(const GDBusPropertyTable *property,
 {
 	const char *type = "peripheral";
 
-	if (!ad_type || strlen(ad_type) > 0)
-		type = ad_type;
+	if (!ad.type || strlen(ad.type) > 0)
+		type = ad.type;
 
 	dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &type);
 
@@ -121,7 +137,7 @@ static gboolean get_type(const GDBusPropertyTable *property,
 
 static gboolean uuids_exists(const GDBusPropertyTable *property, void *data)
 {
-	return ad_uuids_len != 0;
+	return ad.uuids_len != 0;
 }
 
 static gboolean get_uuids(const GDBusPropertyTable *property,
@@ -132,9 +148,9 @@ static gboolean get_uuids(const GDBusPropertyTable *property,
 
 	dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "as", &array);
 
-	for (i = 0; i < ad_uuids_len; i++)
+	for (i = 0; i < ad.uuids_len; i++)
 		dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING,
-							&ad_uuids[i]);
+							&ad.uuids[i]);
 
 	dbus_message_iter_close_container(iter, &array);
 
@@ -197,19 +213,19 @@ static void dict_append_array(DBusMessageIter *dict, const char *key, int type,
 static gboolean service_data_exists(const GDBusPropertyTable *property,
 								void *data)
 {
-	return ad_service_uuid != NULL;
+	return ad.service.uuid != NULL;
 }
 
 static gboolean get_service_data(const GDBusPropertyTable *property,
 				DBusMessageIter *iter, void *user_data)
 {
 	DBusMessageIter dict;
-	const uint8_t *data = ad_service_data;
+	struct ad_data *data = &ad.service.data;
 
 	dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "{sv}", &dict);
 
-	dict_append_array(&dict, ad_service_uuid, DBUS_TYPE_BYTE, &data,
-							ad_service_data_len);
+	dict_append_array(&dict, ad.service.uuid, DBUS_TYPE_BYTE, &data->data,
+								data->len);
 
 	dbus_message_iter_close_container(iter, &dict);
 
@@ -219,35 +235,57 @@ static gboolean get_service_data(const GDBusPropertyTable *property,
 static gboolean manufacturer_data_exists(const GDBusPropertyTable *property,
 								void *data)
 {
-	return ad_manufacturer_id != 0;
+	return ad.manufacturer.id != 0;
 }
 
 static gboolean get_manufacturer_data(const GDBusPropertyTable *property,
 					DBusMessageIter *iter, void *user_data)
 {
 	DBusMessageIter dict;
-	const uint8_t *data = ad_manufacturer_data;
+	struct ad_data *data = &ad.manufacturer.data;
 
 	dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "{qv}", &dict);
 
-	dict_append_basic_array(&dict, DBUS_TYPE_UINT16, &ad_manufacturer_id,
-					DBUS_TYPE_BYTE, &data,
-					ad_manufacturer_data_len);
+	dict_append_basic_array(&dict, DBUS_TYPE_UINT16, &ad.manufacturer.id,
+					DBUS_TYPE_BYTE, &data->data, data->len);
 
 	dbus_message_iter_close_container(iter, &dict);
 
 	return TRUE;
 }
 
-static gboolean tx_power_exists(const GDBusPropertyTable *property, void *data)
+static gboolean includes_exists(const GDBusPropertyTable *property, void *data)
 {
-	return ad_tx_power;
+	return ad.tx_power || ad.name || ad.appearance;
 }
 
-static gboolean get_tx_power(const GDBusPropertyTable *property,
+static gboolean get_includes(const GDBusPropertyTable *property,
 				DBusMessageIter *iter, void *user_data)
 {
-	dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &ad_tx_power);
+	DBusMessageIter array;
+
+	dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "as", &array);
+
+	if (ad.tx_power) {
+		const char *str = "tx-power";
+
+		dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING, &str);
+	}
+
+	if (ad.name) {
+		const char *str = "local-name";
+
+		dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING, &str);
+	}
+
+	if (ad.appearance) {
+		const char *str = "appearance";
+
+		dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING, &str);
+	}
+
+	dbus_message_iter_close_container(iter, &array);
+
 
 	return TRUE;
 }
@@ -258,18 +296,18 @@ static const GDBusPropertyTable ad_props[] = {
 	{ "ServiceData", "a{sv}", get_service_data, NULL, service_data_exists },
 	{ "ManufacturerData", "a{qv}", get_manufacturer_data, NULL,
 						manufacturer_data_exists },
-	{ "IncludeTxPower", "b", get_tx_power, NULL, tx_power_exists },
+	{ "Includes", "as", get_includes, NULL, includes_exists },
 	{ }
 };
 
 void ad_register(DBusConnection *conn, GDBusProxy *manager, const char *type)
 {
-	if (registered == TRUE) {
+	if (ad.registered) {
 		rl_printf("Advertisement is already registered\n");
 		return;
 	}
 
-	ad_type = g_strdup(type);
+	ad.type = g_strdup(type);
 
 	if (g_dbus_register_interface(conn, AD_PATH, AD_IFACE, ad_methods,
 					NULL, ad_props, NULL, NULL) == FALSE) {
@@ -300,7 +338,7 @@ static void unregister_reply(DBusMessage *message, void *user_data)
 	dbus_error_init(&error);
 
 	if (dbus_set_error_from_message(&error, message) == FALSE) {
-		registered = FALSE;
+		ad.registered = false;
 		rl_printf("Advertising object unregistered\n");
 		if (g_dbus_unregister_interface(conn, AD_PATH,
 							AD_IFACE) == FALSE)
@@ -317,7 +355,7 @@ void ad_unregister(DBusConnection *conn, GDBusProxy *manager)
 	if (!manager)
 		ad_release(conn);
 
-	if (!registered)
+	if (!ad.registered)
 		return;
 
 	if (g_dbus_proxy_method_call(manager, "UnregisterAdvertisement",
@@ -330,34 +368,33 @@ void ad_unregister(DBusConnection *conn, GDBusProxy *manager)
 
 void ad_advertise_uuids(const char *arg)
 {
-	g_strfreev(ad_uuids);
-	ad_uuids = NULL;
-	ad_uuids_len = 0;
+	g_strfreev(ad.uuids);
+	ad.uuids = NULL;
+	ad.uuids_len = 0;
 
 	if (!arg || !strlen(arg))
 		return;
 
-	ad_uuids = g_strsplit(arg, " ", -1);
-	if (!ad_uuids) {
+	ad.uuids = g_strsplit(arg, " ", -1);
+	if (!ad.uuids) {
 		rl_printf("Failed to parse input\n");
 		return;
 	}
 
-	ad_uuids_len = g_strv_length(ad_uuids);
+	ad.uuids_len = g_strv_length(ad.uuids);
 }
 
 static void ad_clear_service(void)
 {
-	g_free(ad_service_uuid);
-	ad_service_uuid = NULL;
-	memset(ad_service_data, 0, sizeof(ad_service_data));
-	ad_service_data_len = 0;
+	g_free(ad.service.uuid);
+	memset(&ad.service, 0, sizeof(ad.service));
 }
 
 void ad_advertise_service(const char *arg)
 {
 	wordexp_t w;
 	unsigned int i;
+	struct ad_data *data;
 
 	if (wordexp(arg, &w, WRDE_NOCMD)) {
 		rl_printf("Invalid argument\n");
@@ -369,13 +406,14 @@ void ad_advertise_service(const char *arg)
 	if (w.we_wordc == 0)
 		goto done;
 
-	ad_service_uuid = g_strdup(w.we_wordv[0]);
+	ad.service.uuid = g_strdup(w.we_wordv[0]);
+	data = &ad.service.data;
 
 	for (i = 1; i < w.we_wordc; i++) {
 		long int val;
 		char *endptr = NULL;
 
-		if (i >= G_N_ELEMENTS(ad_service_data)) {
+		if (i >= G_N_ELEMENTS(data->data)) {
 			rl_printf("Too much data\n");
 			goto done;
 		}
@@ -387,8 +425,8 @@ void ad_advertise_service(const char *arg)
 			goto done;
 		}
 
-		ad_service_data[ad_service_data_len] = val;
-		ad_service_data_len++;
+		data->data[data->len] = val;
+		data->len++;
 	}
 
 done:
@@ -397,9 +435,7 @@ done:
 
 static void ad_clear_manufacturer(void)
 {
-	ad_manufacturer_id = 0;
-	memset(ad_manufacturer_data, 0, sizeof(ad_manufacturer_data));
-	ad_manufacturer_data_len = 0;
+	memset(&ad.manufacturer, 0, sizeof(ad.manufacturer));
 }
 
 void ad_advertise_manufacturer(const char *arg)
@@ -408,6 +444,7 @@ void ad_advertise_manufacturer(const char *arg)
 	unsigned int i;
 	char *endptr = NULL;
 	long int val;
+	struct ad_data *data;
 
 	if (wordexp(arg, &w, WRDE_NOCMD)) {
 		rl_printf("Invalid argument\n");
@@ -425,10 +462,11 @@ void ad_advertise_manufacturer(const char *arg)
 		goto done;
 	}
 
-	ad_manufacturer_id = val;
+	ad.manufacturer.id = val;
+	data = &ad.manufacturer.data;
 
 	for (i = 1; i < w.we_wordc; i++) {
-		if (i >= G_N_ELEMENTS(ad_service_data)) {
+		if (i >= G_N_ELEMENTS(data->data)) {
 			rl_printf("Too much data\n");
 			goto done;
 		}
@@ -440,8 +478,8 @@ void ad_advertise_manufacturer(const char *arg)
 			goto done;
 		}
 
-		ad_manufacturer_data[ad_manufacturer_data_len] = val;
-		ad_manufacturer_data_len++;
+		data->data[data->len] = val;
+		data->len++;
 	}
 
 done:
@@ -449,7 +487,17 @@ done:
 }
 
 
-void ad_advertise_tx_power(gboolean value)
+void ad_advertise_tx_power(bool value)
+{
+	ad.tx_power = value;
+}
+
+void ad_advertise_name(bool value)
+{
+	ad.name = value;
+}
+
+void ad_advertise_appearance(bool value)
 {
-	ad_tx_power = value;
+	ad.appearance = value;
 }
client/advertising.h +3 -1
index 86384656c..77fc1cca5 100644
@@ -27,4 +27,6 @@ void ad_unregister(DBusConnection *conn, GDBusProxy *manager);
 void ad_advertise_uuids(const char *arg);
 void ad_advertise_service(const char *arg);
 void ad_advertise_manufacturer(const char *arg);
-void ad_advertise_tx_power(gboolean value);
+void ad_advertise_tx_power(bool value);
+void ad_advertise_name(bool value);
+void ad_advertise_appearance(bool value);
client/main.c +46 -2

64 lines not on this page · raw message

Signature · 1 line
2.13.4
author maintainer
#6 lore raw

PATCH v8 7/14 shared/ad: Add bt_ad_add_name and bt_ad_clear_name

In reply to Luiz Augusto von Dentz

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

This adds basically functionality to add and remove name to advertising data.

---
 src/shared/ad.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/shared/ad.h |  4 ++++
 2 files changed, 68 insertions(+)
src/shared/ad.c +64 -0
index 1bf013d5a..aca4e2381 100644
@@ -31,6 +31,7 @@
 
 struct bt_ad {
 	int ref_count;
+	char *name;
 	struct queue *service_uuids;
 	struct queue *manufacturer_data;
 	struct queue *solicit_uuids;
@@ -107,6 +108,8 @@ void bt_ad_unref(struct bt_ad *ad)
 
 	queue_destroy(ad->service_data, uuid_destroy);
 
+	free(ad->name);
+
 	free(ad);
 }
 
@@ -183,6 +186,21 @@ static size_t uuid_data_length(struct queue *uuid_data)
 	return length;
 }
 
+static size_t name_length(const char *name, size_t *pos)
+{
+	size_t len;
+	
+	if (!name)
+		return 0;
+
+	len = 2 + strlen(name);
+
+	if (len > MAX_ADV_DATA_LEN - *pos)
+		len = MAX_ADV_DATA_LEN - *pos;
+
+	return len;
+}
+
 static size_t calculate_length(struct bt_ad *ad)
 {
 	size_t length = 0;
@@ -195,6 +213,8 @@ static size_t calculate_length(struct bt_ad *ad)
 
 	length += uuid_data_length(ad->service_data);
 
+	length += name_length(ad->name, &length);
+
 	return length;
 }
 
@@ -313,6 +333,27 @@ static void serialize_service_data(struct queue *service_data, uint8_t *buf,
 	}
 }
 
+static void serialize_name(const char *name, uint8_t *buf, uint8_t *pos)
+{
+	int len;
+	uint8_t type = EIR_NAME_COMPLETE;
+
+	if (!name)
+		return;
+
+	len = strlen(name);
+	if (len > MAX_ADV_DATA_LEN - (*pos + 2)) {
+		type = EIR_NAME_SHORT;
+		len = MAX_ADV_DATA_LEN - (*pos + 2);
+	}
+
+	buf[(*pos)++] = len + 1;
+	buf[(*pos)++] = type;
+
+	memcpy(buf + *pos, name, len);
+	*pos += len;
+}
+
 uint8_t *bt_ad_generate(struct bt_ad *ad, size_t *length)
 {
 	uint8_t *adv_data;
@@ -338,6 +379,8 @@ uint8_t *bt_ad_generate(struct bt_ad *ad, size_t *length)
 
 	serialize_service_data(ad->service_data, adv_data, &pos);
 
+	serialize_name(ad->name, adv_data, &pos);
+
 	return adv_data;
 }
 
@@ -656,3 +699,24 @@ void bt_ad_clear_service_data(struct bt_ad *ad)
 
 	queue_remove_all(ad->service_data, NULL, NULL, uuid_destroy);
 }
+
+bool bt_ad_add_name(struct bt_ad *ad, const char *name)
+{
+	if (!ad)
+		return false;
+
+	free(ad->name);
+
+	ad->name = strdup(name);
+
+	return true;
+}
+
+void bt_ad_clear_name(struct bt_ad *ad)
+{
+	if (!ad)
+		return;
+
+	free(ad->name);
+	ad->name = NULL;
+}
src/shared/ad.h +4 -0
index 709563d3c..161602d6d 100644
@@ -88,3 +88,7 @@ void bt_ad_foreach_service_data(struct bt_ad *ad, bt_ad_func_t func,
 bool bt_ad_remove_service_data(struct bt_ad *ad, bt_uuid_t *uuid);
 
 void bt_ad_clear_service_data(struct bt_ad *ad);
+
+bool bt_ad_add_name(struct bt_ad *ad, const char *name);
+
+void bt_ad_clear_name(struct bt_ad *ad);
Signature · 1 line
2.13.4
author maintainer
#7 lore raw

PATCH v8 8/14 shared/ad: Add bt_ad_add_appearance and bt_ad_clear_appearance

In reply to Luiz Augusto von Dentz

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

This adds basically functionality to add and remove GAP appearance to advertising data.

---
 src/shared/ad.c | 36 ++++++++++++++++++++++++++++++++++++
 src/shared/ad.h |  4 ++++
 2 files changed, 40 insertions(+)
src/shared/ad.c +36 -0
index aca4e2381..255794dc4 100644
@@ -32,6 +32,7 @@
 struct bt_ad {
 	int ref_count;
 	char *name;
+	uint16_t appearance;
 	struct queue *service_uuids;
 	struct queue *manufacturer_data;
 	struct queue *solicit_uuids;
@@ -47,6 +48,7 @@ struct bt_ad *bt_ad_new(void)
 	ad->manufacturer_data = queue_new();
 	ad->solicit_uuids = queue_new();
 	ad->service_data = queue_new();
+	ad->appearance = UINT16_MAX;
 
 	return bt_ad_ref(ad);
 }
@@ -215,6 +217,8 @@ static size_t calculate_length(struct bt_ad *ad)
 
 	length += name_length(ad->name, &length);
 
+	length += ad->appearance != UINT16_MAX ? 4 : 0;
+
 	return length;
 }
 
@@ -354,6 +358,18 @@ static void serialize_name(const char *name, uint8_t *buf, uint8_t *pos)
 	*pos += len;
 }
 
+static void serialize_appearance(uint16_t value, uint8_t *buf, uint8_t *pos)
+{
+	if (value == UINT16_MAX)
+		return;
+
+	buf[(*pos)++] = sizeof(value) + 1;
+	buf[(*pos)++] = EIR_GAP_APPEARANCE;
+
+	bt_put_le16(value, buf + (*pos));
+	*pos += 2;
+}
+
 uint8_t *bt_ad_generate(struct bt_ad *ad, size_t *length)
 {
 	uint8_t *adv_data;
@@ -381,6 +397,8 @@ uint8_t *bt_ad_generate(struct bt_ad *ad, size_t *length)
 
 	serialize_name(ad->name, adv_data, &pos);
 
+	serialize_appearance(ad->appearance, adv_data, &pos);
+
 	return adv_data;
 }
 
@@ -720,3 +738,21 @@ void bt_ad_clear_name(struct bt_ad *ad)
 	free(ad->name);
 	ad->name = NULL;
 }
+
+bool bt_ad_add_appearance(struct bt_ad *ad, uint16_t appearance)
+{
+	if (!ad)
+		return false;
+
+	ad->appearance = appearance;
+
+	return true;
+}
+
+void bt_ad_clear_appearance(struct bt_ad *ad)
+{
+	if (!ad)
+		return;
+
+	ad->appearance = UINT16_MAX;
+}
src/shared/ad.h +4 -0
index 161602d6d..f0e3b8127 100644
@@ -92,3 +92,7 @@ void bt_ad_clear_service_data(struct bt_ad *ad);
 bool bt_ad_add_name(struct bt_ad *ad, const char *name);
 
 void bt_ad_clear_name(struct bt_ad *ad);
+
+bool bt_ad_add_appearance(struct bt_ad *ad, uint16_t appearance);
+
+void bt_ad_clear_appearance(struct bt_ad *ad);
Signature · 1 line
2.13.4
author maintainer
#8 lore raw

PATCH v8 9/14 advertising: Don't depend on kernel to include the Local Name

In reply to Luiz Augusto von Dentz

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

The flag to include Local Name may not be available in older kernel so instead of depending on the kernel to add the adapter name in the scan response generate it and set it as instance scan response:

@ MGMT Command: Add Advertising (0x003e) plen 31
Instance: 1
Flags: 0x00000003
Switch into Connectable mode
Advertise as Discoverable
Duration: 0
Timeout: 0
Advertising data length: 0
Scan response length: 20
Name (complete): Vudentz's T460s #1

---
 src/advertising.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 48 insertions(+), 2 deletions(-)
src/advertising.c +48 -2
index 6d54814d5..7e7458414 100644
@@ -33,6 +33,7 @@
 #include "dbus-common.h"
 #include "error.h"
 #include "log.h"
+#include "eir.h"
 #include "src/shared/ad.h"
 #include "src/shared/mgmt.h"
 #include "src/shared/queue.h"
@@ -47,6 +48,7 @@ struct btd_adv_manager {
 	struct mgmt *mgmt;
 	uint16_t mgmt_index;
 	uint8_t max_adv_len;
+	uint8_t max_scan_rsp_len;
 	uint8_t max_ads;
 	uint32_t supported_flags;
 	unsigned int instance_bitmap;
@@ -65,6 +67,7 @@ struct btd_adv_client {
 	uint8_t type; /* Advertising type */
 	uint32_t flags;
 	struct bt_ad *data;
+	struct bt_ad *scan;
 	uint8_t instance;
 };
 
@@ -101,6 +104,7 @@ static void client_free(void *data)
 						client->instance);
 
 	bt_ad_unref(client->data);
+	bt_ad_unref(client->scan);
 
 	g_dbus_proxy_unref(client->proxy);
 
@@ -532,12 +536,34 @@ static size_t calc_max_adv_len(struct btd_adv_client *client, uint32_t flags)
 	return max;
 }
 
+static uint8_t *generate_scan_rsp(struct btd_adv_client *client,
+						uint32_t *flags, size_t *len)
+{
+	struct btd_adv_manager *manager = client->manager;
+	const char *name;
+
+	if (!(*flags & MGMT_ADV_FLAG_LOCAL_NAME)) {
+		*len = 0;
+		return NULL;
+	}
+
+	*flags &= ~MGMT_ADV_FLAG_LOCAL_NAME;
+
+	name = btd_adapter_get_name(manager->adapter);
+
+	bt_ad_add_name(client->scan, name);
+
+	return bt_ad_generate(client->scan, len);
+}
+
 static DBusMessage *refresh_advertisement(struct btd_adv_client *client)
 {
 	struct mgmt_cp_add_advertising *cp;
 	uint8_t param_len;
 	uint8_t *adv_data;
 	size_t adv_data_len;
+	uint8_t *scan_rsp;
+	size_t scan_rsp_len = -1;
 	uint32_t flags = 0;
 
 	DBG("Refreshing advertisement: %s", client->path);
@@ -557,7 +583,17 @@ static DBusMessage *refresh_advertisement(struct btd_adv_client *client)
 						"Advertising data too long.");
 	}
 
-	param_len = sizeof(struct mgmt_cp_add_advertising) + adv_data_len;
+	scan_rsp = generate_scan_rsp(client, &flags, &scan_rsp_len);
+	if (!scan_rsp && scan_rsp_len) {
+		error("Scan data couldn't be generated.");
+
+		return g_dbus_create_error(client->reg, ERROR_INTERFACE
+						".InvalidLength",
+						"Advertising data too long.");
+	}
+
+	param_len = sizeof(struct mgmt_cp_add_advertising) + adv_data_len +
+							scan_rsp_len;
 
 	cp = malloc0(param_len);
 
@@ -565,6 +601,7 @@ static DBusMessage *refresh_advertisement(struct btd_adv_client *client)
 		error("Couldn't allocate for MGMT!");
 
 		free(adv_data);
+		free(scan_rsp);
 
 		return btd_error_failed(client->reg, "Failed");
 	}
@@ -572,9 +609,12 @@ static DBusMessage *refresh_advertisement(struct btd_adv_client *client)
 	cp->flags = htobl(flags);
 	cp->instance = client->instance;
 	cp->adv_data_len = adv_data_len;
+	cp->scan_rsp_len = scan_rsp_len;
 	memcpy(cp->data, adv_data, adv_data_len);
+	memcpy(cp->data + adv_data_len, scan_rsp, scan_rsp_len);
 
 	free(adv_data);
+	free(scan_rsp);
 
 	if (!mgmt_send(client->manager->mgmt, MGMT_OP_ADD_ADVERTISING,
 				client->manager->mgmt_index, param_len, cp,
@@ -672,6 +712,10 @@ static struct btd_adv_client *client_create(struct btd_adv_manager *manager,
 	if (!client->data)
 		goto fail;
 
+	client->scan = bt_ad_new();
+	if (!client->scan)
+		goto fail;
+
 	client->manager = manager;
 
 	return client;
@@ -865,8 +909,9 @@ static void read_adv_features_callback(uint8_t status, uint16_t length,
 	}
 
 	manager->max_adv_len = feat->max_adv_data_len;
+	manager->max_scan_rsp_len = feat->max_scan_rsp_len;
 	manager->max_ads = feat->max_instances;
-	manager->supported_flags = feat->supported_flags;
+	manager->supported_flags |= feat->supported_flags;
 
 	if (manager->max_ads == 0)
 		return;
@@ -910,6 +955,7 @@ static struct btd_adv_manager *manager_create(struct btd_adapter *adapter)
 	}
 
 	manager->clients = queue_new();
+	manager->supported_flags = MGMT_ADV_FLAG_LOCAL_NAME;
 
 	return manager;
 }
Signature · 1 line
2.13.4
author maintainer
#9 lore raw

PATCH v8 10/14 advertising: Don't depend on kernel to include an appearance

In reply to Luiz Augusto von Dentz

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

The flag to include an Appearance may not be available in older kernels so instead of depending on the kernel to add it just generate it locally and add as part of advertising data:

@ MGMT Command: Add Advertising (0x003e) plen 15
Instance: 1
Flags: 0x00000003
Switch into Connectable mode
Advertise as Discoverable
Duration: 0
Timeout: 0
Advertising data length: 4
Appearance: Unknown (0x0000)
Scan response length: 0

---
 src/advertising.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
src/advertising.c +13 -2
index 7e7458414..61b17e400 100644
@@ -536,6 +536,18 @@ static size_t calc_max_adv_len(struct btd_adv_client *client, uint32_t flags)
 	return max;
 }
 
+static uint8_t *generate_adv_data(struct btd_adv_client *client,
+						uint32_t *flags, size_t *len)
+{
+	if ((*flags & MGMT_ADV_FLAG_APPEARANCE)) {
+		*flags &= ~MGMT_ADV_FLAG_APPEARANCE;
+		/* TODO: Get the appearance from the adaptor once supported. */
+		bt_ad_add_appearance(client->data, 0x0000);
+	}
+
+	return bt_ad_generate(client->data, len);
+}
+
 static uint8_t *generate_scan_rsp(struct btd_adv_client *client,
 						uint32_t *flags, size_t *len)
 {
@@ -573,8 +585,7 @@ static DBusMessage *refresh_advertisement(struct btd_adv_client *client)
 
 	flags |= client->flags;
 
-	adv_data = bt_ad_generate(client->data, &adv_data_len);
-
+	adv_data = generate_adv_data(client, &flags, &adv_data_len);
 	if (!adv_data || (adv_data_len > calc_max_adv_len(client, flags))) {
 		error("Advertising data too long or couldn't be generated.");
 
Signature · 1 line
2.13.4
author maintainer
#10 lore raw

PATCH v8 11/14 advertising: Add LocalName property

In reply to Luiz Augusto von Dentz

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

With this property applications can select their own local name to be advertised, as opposed to just including the adapter's name.

---
 doc/advertising-api.txt |  9 +++++++++
 src/advertising.c       | 29 +++++++++++++++++++++++++++--
 2 files changed, 36 insertions(+), 2 deletions(-)
doc/advertising-api.txt +9 -0
index 65f1331a9..c9bc9a148 100644
@@ -69,6 +69,15 @@ Properties	string Type
 			Possible values: as found on
 					LEAdvertisingManager.SupportedIncludes
 
+		string LocalName
+
+			Local name to be used in the advertising report. If the
+			string is too big to fit into the packet it will be
+			truncated.
+
+			If this property is available 'local-name' cannot be
+			present in the Includes.
+
 LE Advertising Manager hierarchy
 ================================
 
src/advertising.c +27 -2
index 61b17e400..e45207071 100644
@@ -61,6 +61,7 @@ struct btd_adv_client {
 	struct btd_adv_manager *manager;
 	char *owner;
 	char *path;
+	char *name;
 	GDBusClient *client;
 	GDBusProxy *proxy;
 	DBusMessage *reg;
@@ -114,6 +115,7 @@ static void client_free(void *data)
 	if (client->path)
 		g_free(client->path);
 
+	free(client->name);
 	free(client);
 }
 
@@ -450,6 +452,26 @@ static bool parse_includes(DBusMessageIter *iter,
 	return true;
 }
 
+static bool parse_local_name(DBusMessageIter *iter,
+					struct btd_adv_client *client)
+{
+	const char *name;
+
+	if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_STRING)
+		return false;
+
+	if (client->flags & MGMT_ADV_FLAG_LOCAL_NAME) {
+		error("Local name already included");
+		return false;
+	}
+
+	dbus_message_iter_get_basic(iter, &name);
+
+	client->name = strdup(name);
+
+	return true;
+}
+
 static struct adv_parser {
 	const char *name;
 	bool (*func)(DBusMessageIter *iter, struct btd_adv_client *client);
@@ -460,6 +482,7 @@ static struct adv_parser {
 	{ "ManufacturerData", parse_manufacturer_data },
 	{ "ServiceData", parse_service_data },
 	{ "Includes", parse_includes },
+	{ "LocalName", parse_local_name },
 	{ },
 };
 
@@ -554,14 +577,16 @@ static uint8_t *generate_scan_rsp(struct btd_adv_client *client,
 	struct btd_adv_manager *manager = client->manager;
 	const char *name;
 
-	if (!(*flags & MGMT_ADV_FLAG_LOCAL_NAME)) {
+	if (!(*flags & MGMT_ADV_FLAG_LOCAL_NAME) && !client->name) {
 		*len = 0;
 		return NULL;
 	}
 
 	*flags &= ~MGMT_ADV_FLAG_LOCAL_NAME;
 
-	name = btd_adapter_get_name(manager->adapter);
+	name = client->name;
+	if (!name)
+		name = btd_adapter_get_name(manager->adapter);
 
 	bt_ad_add_name(client->scan, name);
 
Signature · 1 line
2.13.4
author maintainer
#11 lore raw

PATCH v8 12/14 client: Enable set-advertise-name to set LocalName

In reply to Luiz Augusto von Dentz

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

This enables set-advertise-name to use LocalName property:

[bluetooth]# set-advertise-name blah
[bluetooth]# advertise on

@ MGMT Command: Add Advertising (0x003e) plen 17
Instance: 1
Flags: 0x00000003
Switch into Connectable mode
Advertise as Discoverable
Duration: 0
Timeout: 0
Advertising data length: 0
Scan response length: 6
Name (complete): blah

---
 client/advertising.c | 24 ++++++++++++++++++++++++
 client/advertising.h |  1 +
 client/main.c        |  4 ++--
 3 files changed, 27 insertions(+), 2 deletions(-)
client/advertising.c +24 -0
index 35e582afc..571b6e619 100644
@@ -57,6 +57,7 @@ struct manufacturer_data {
 static struct ad {
 	bool registered;
 	char *type;
+	char *local_name;
 	char **uuids;
 	size_t uuids_len;
 	struct service_data service;
@@ -290,6 +291,19 @@ static gboolean get_includes(const GDBusPropertyTable *property,
 	return TRUE;
 }
 
+static gboolean local_name_exits(const GDBusPropertyTable *property, void *data)
+{
+	return ad.local_name ? TRUE : FALSE;
+}
+
+static gboolean get_local_name(const GDBusPropertyTable *property,
+				DBusMessageIter *iter, void *user_data)
+{
+	dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &ad.local_name);
+
+	return TRUE;
+}
+
 static const GDBusPropertyTable ad_props[] = {
 	{ "Type", "s", get_type },
 	{ "ServiceUUIDs", "as", get_uuids, NULL, uuids_exists },
@@ -297,6 +311,7 @@ static const GDBusPropertyTable ad_props[] = {
 	{ "ManufacturerData", "a{qv}", get_manufacturer_data, NULL,
 						manufacturer_data_exists },
 	{ "Includes", "as", get_includes, NULL, includes_exists },
+	{ "LocalName", "s", get_local_name, NULL, local_name_exits },
 	{ }
 };
 
@@ -495,6 +510,15 @@ void ad_advertise_tx_power(bool value)
 void ad_advertise_name(bool value)
 {
 	ad.name = value;
+
+	if (!value)
+		free(ad.local_name);
+}
+
+void ad_advertise_local_name(const char *name)
+{
+	free(ad.local_name);
+	ad.local_name = strdup(name);
 }
 
 void ad_advertise_appearance(bool value)
client/advertising.h +1 -0
index 77fc1cca5..255743562 100644
@@ -30,3 +30,4 @@ void ad_advertise_manufacturer(const char *arg);
 void ad_advertise_tx_power(bool value);
 void ad_advertise_name(bool value);
 void ad_advertise_appearance(bool value);
+void ad_advertise_local_name(const char *name);
client/main.c +2 -2
index 0bc2a8896..328a5ee3a 100644
@@ -2379,7 +2379,7 @@ static void cmd_set_advertise_name(const char *arg)
 		return;
 	}
 
-	rl_printf("Invalid argument\n");
+	ad_advertise_local_name(arg);
 }
 
 static void cmd_set_advertise_appearance(const char *arg)
@@ -2446,7 +2446,7 @@ static const struct {
 	{ "set-advertise-tx-power", "<on/off>",
 			cmd_set_advertise_tx_power,
 			"Enable/disable TX power to be advertised" },
-	{ "set-advertise-name", "<on/off>", cmd_set_advertise_name,
+	{ "set-advertise-name", "<on/off/name>", cmd_set_advertise_name,
 			"Enable/disable local name to be advertised" },
 	{ "set-advertise-appearance", "<value>", cmd_set_advertise_appearance,
 			"Set custom appearance to be advertised" },
Signature · 1 line
2.13.4
author maintainer
#12 lore raw

PATCH v8 13/14 advertising: Add Appearance property

In reply to Luiz Augusto von Dentz

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

With this property applications can select their own appearance to be advertised, as opposed to just including the adapter's appearance.

---
 doc/advertising-api.txt |  6 ++++++
 src/advertising.c       | 35 +++++++++++++++++++++++++++++++----
 2 files changed, 37 insertions(+), 4 deletions(-)
doc/advertising-api.txt +6 -0
index c9bc9a148..36fe130d9 100644
@@ -78,6 +78,12 @@ Properties	string Type
 			If this property is available 'local-name' cannot be
 			present in the Includes.
 
+		uint16 Appearance
+
+			Appearance to be used in the advertising report.
+
+			Possible values: as found on GAP Service.
+
 LE Advertising Manager hierarchy
 ================================
 
src/advertising.c +31 -4
index e45207071..9eb22b9fd 100644
@@ -62,6 +62,7 @@ struct btd_adv_client {
 	char *owner;
 	char *path;
 	char *name;
+	uint16_t appearance;
 	GDBusClient *client;
 	GDBusProxy *proxy;
 	DBusMessage *reg;
@@ -472,6 +473,22 @@ static bool parse_local_name(DBusMessageIter *iter,
 	return true;
 }
 
+static bool parse_appearance(DBusMessageIter *iter,
+					struct btd_adv_client *client)
+{
+	if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_UINT16)
+		return false;
+
+	if (client->flags & MGMT_ADV_FLAG_APPEARANCE) {
+		error("Appearance already included");
+		return false;
+	}
+
+	dbus_message_iter_get_basic(iter, &client->appearance);
+
+	return true;
+}
+
 static struct adv_parser {
 	const char *name;
 	bool (*func)(DBusMessageIter *iter, struct btd_adv_client *client);
@@ -483,6 +500,7 @@ static struct adv_parser {
 	{ "ServiceData", parse_service_data },
 	{ "Includes", parse_includes },
 	{ "LocalName", parse_local_name },
+	{ "Appearance", parse_appearance },
 	{ },
 };
 
@@ -562,10 +580,18 @@ static size_t calc_max_adv_len(struct btd_adv_client *client, uint32_t flags)
 static uint8_t *generate_adv_data(struct btd_adv_client *client,
 						uint32_t *flags, size_t *len)
 {
-	if ((*flags & MGMT_ADV_FLAG_APPEARANCE)) {
-		*flags &= ~MGMT_ADV_FLAG_APPEARANCE;
-		/* TODO: Get the appearance from the adaptor once supported. */
-		bt_ad_add_appearance(client->data, 0x0000);
+	if ((*flags & MGMT_ADV_FLAG_APPEARANCE) ||
+					client->appearance != UINT16_MAX) {
+		uint16_t appearance;
+
+		appearance = client->appearance;
+		if (appearance == UINT16_MAX)
+			/* TODO: Get the appearance from the adaptor once
+			 * supported.
+			 */
+			appearance = 0x000;
+
+		bt_ad_add_appearance(client->data, appearance);
 	}
 
 	return bt_ad_generate(client->data, len);
@@ -753,6 +779,7 @@ static struct btd_adv_client *client_create(struct btd_adv_manager *manager,
 		goto fail;
 
 	client->manager = manager;
+	client->appearance = UINT16_MAX;
 
 	return client;
 
Signature · 1 line
2.13.4
author maintainer
#13 lore raw

PATCH v8 14/14 client: Enable set-advertise-appearance to set Appearance

In reply to Luiz Augusto von Dentz

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

This enables set-advertise-appearance to use Appearance property:

[bluetooth]# set-advertise-appearance 128
[bluetooth]# advertise on

@ MGMT Command: Add Advertising (0x003e) plen 15
Instance: 1
Flags: 0x00000003
Switch into Connectable mode
Advertise as Discoverable
Duration: 0
Timeout: 0
Advertising data length: 4
Appearance: Computer (0x0080)
Scan response length: 0

---
 client/advertising.c | 28 +++++++++++++++++++++++++++-
 client/advertising.h |  1 +
 client/main.c        | 11 ++++++++++-
 3 files changed, 38 insertions(+), 2 deletions(-)
client/advertising.c +27 -1
index 571b6e619..696621be3 100644
@@ -58,6 +58,7 @@ static struct ad {
 	bool registered;
 	char *type;
 	char *local_name;
+	uint16_t local_appearance;
 	char **uuids;
 	size_t uuids_len;
 	struct service_data service;
@@ -65,7 +66,9 @@ static struct ad {
 	bool tx_power;
 	bool name;
 	bool appearance;
-} ad;
+} ad = {
+	.local_appearance = UINT16_MAX,
+};
 
 static void ad_release(DBusConnection *conn)
 {
@@ -304,6 +307,20 @@ static gboolean get_local_name(const GDBusPropertyTable *property,
 	return TRUE;
 }
 
+static gboolean appearance_exits(const GDBusPropertyTable *property, void *data)
+{
+	return ad.local_appearance != UINT16_MAX ? TRUE : FALSE;
+}
+
+static gboolean get_appearance(const GDBusPropertyTable *property,
+				DBusMessageIter *iter, void *user_data)
+{
+	dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT16,
+							&ad.local_appearance);
+
+	return TRUE;
+}
+
 static const GDBusPropertyTable ad_props[] = {
 	{ "Type", "s", get_type },
 	{ "ServiceUUIDs", "as", get_uuids, NULL, uuids_exists },
@@ -312,6 +329,7 @@ static const GDBusPropertyTable ad_props[] = {
 						manufacturer_data_exists },
 	{ "Includes", "as", get_includes, NULL, includes_exists },
 	{ "LocalName", "s", get_local_name, NULL, local_name_exits },
+	{ "Appearance", "q", get_appearance, NULL, appearance_exits },
 	{ }
 };
 
@@ -524,4 +542,12 @@ void ad_advertise_local_name(const char *name)
 void ad_advertise_appearance(bool value)
 {
 	ad.appearance = value;
+
+	if (!value)
+		ad.local_appearance = UINT16_MAX;
+}
+
+void ad_advertise_local_appearance(uint16_t value)
+{
+	ad.local_appearance = value;
 }
client/advertising.h +1 -0
index 255743562..5ea57adb3 100644
@@ -31,3 +31,4 @@ void ad_advertise_tx_power(bool value);
 void ad_advertise_name(bool value);
 void ad_advertise_appearance(bool value);
 void ad_advertise_local_name(const char *name);
+void ad_advertise_local_appearance(uint16_t value);
client/main.c +10 -1
index 328a5ee3a..6b1c73f99 100644
@@ -2384,6 +2384,9 @@ static void cmd_set_advertise_name(const char *arg)
 
 static void cmd_set_advertise_appearance(const char *arg)
 {
+	long int value;
+	char *endptr = NULL;
+
 	if (arg == NULL || strlen(arg) == 0) {
 		rl_printf("Missing value argument\n");
 		return;
@@ -2399,7 +2402,13 @@ static void cmd_set_advertise_appearance(const char *arg)
 		return;
 	}
 
-	rl_printf("Invalid argument\n");
+	value = strtol(arg, &endptr, 0);
+	if (!endptr || *endptr != '\0' || value > UINT16_MAX) {
+		rl_printf("Invalid argument\n");
+		return;
+	}
+
+	ad_advertise_local_appearance(value);
 }
 
 static const struct {
Signature · 1 line
2.13.4
author maintainer Applied
#14 lore raw
In reply to Luiz Augusto von Dentz

Hi,

Quoted 52 lines · Luiz Augusto von Dentz

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

If for some reason bluetoothd crashes or is killed it may leave instances behind.

---
 src/advertising.c | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)
src/advertising.c +23 -2
index c2af3acb4..64d5a27bb 100644
@@ -144,6 +144,22 @@ static void client_destroy(void *data)
        client_free(data);
 }
 
+static void remove_advertising(struct btd_adv_manager *manager,
+                                               uint8_t instance)
+{
+       struct mgmt_cp_remove_advertising cp;
+
+       if (instance)
+               DBG("instance %u", instance);
+       else
+               DBG("all instances");
+
+       cp.instance = instance;
+
+       mgmt_send(manager->mgmt, MGMT_OP_REMOVE_ADVERTISING,
+                       manager->mgmt_index, sizeof(cp), &cp, NULL, NULL, NULL);
+}
+
 static void client_remove(void *data)
 {
        struct btd_adv_client *client = data;
@@ -791,8 +807,14 @@ static void read_adv_features_callback(uint8_t status, uint16_t length,
        if (!g_dbus_register_interface(btd_get_dbus_connection(),
                                        adapter_get_path(manager->adapter),
                                        LE_ADVERTISING_MGR_IFACE, methods,
-                                       NULL, properties, manager, NULL))
+                                       NULL, properties, manager, NULL)) {
                error("Failed to register " LE_ADVERTISING_MGR_IFACE);
+               return;
+       }
+
+       /* Reset existing instances */
+       if (feat->num_instances)
+               remove_advertising(manager, 0);
 }
 
 static struct btd_adv_manager *manager_create(struct btd_adapter *adapter)
--
Quoted 1 line

2.13.4

Applied.

Signature · 1 line
Luiz Augusto von Dentz