Skip to contentSkip to conversation

core: Add plugin-support for Manufacturer Specific Data EIR

PATCH Superseded Alfonso Acosta 2 participants 7 messages Oct 14, 2014 – Oct 20, 2014 1 message not in the archive Bluetooth drivers

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover core: Add plugin-support for Manufacturer Specific Data EIR

v4:

* Support for multiple MSD fields in the same EIR data block.
* Moved parsing to a separate function: eir_parse_msd()

v3:

* Corrections suggested by Johan on the handling of eir.h
* Parser sanity check on the size of the MSD payload.
* Minor typo correction on commit message.

Alfonso Acosta (2):
core: Add Manufacturer Specific Data EIR field
core: Add subscription API for Manufacturer Specific Data

 src/adapter.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 src/adapter.h | 10 ++++++++++
 src/eir.c     | 22 ++++++++++++++++++++++
 src/eir.h     |  8 ++++++++
 4 files changed, 84 insertions(+)
Signature · 1 line
1.9.1
#1 lore raw

PATCH v4 1/2 core: Add Manufacturer Specific Data EIR field

Add data structure and parsing support.

---
 src/eir.c | 22 ++++++++++++++++++++++
 src/eir.h |  8 ++++++++
 2 files changed, 30 insertions(+)
src/eir.c +22 -0
index d22ad91..2ea8731 100644
@@ -53,6 +53,8 @@ void eir_data_free(struct eir_data *eir)
 	eir->hash = NULL;
 	g_free(eir->randomizer);
 	eir->randomizer = NULL;
+	g_slist_free_full(eir->msd_list, g_free);
+	eir->msd_list = NULL;
 }
 
 static void eir_parse_uuid16(struct eir_data *eir, const void *data,
@@ -137,6 +139,22 @@ static char *name2utf8(const uint8_t *name, uint8_t len)
 	return g_strdup(utf8_name);
 }
 
+static void eir_parse_msd(struct eir_data *eir, const uint8_t *data,
+								uint8_t len)
+{
+	struct eir_msd *msd;
+
+	if (len < 2 || len > 2 + sizeof(msd->data))
+		return;
+
+	msd = g_malloc(sizeof(*msd));
+	msd->company = get_le16(data);
+	msd->data_len = len - 2;
+	memcpy(&msd->data, data + 2, msd->data_len);
+
+	eir->msd_list = g_slist_append(eir->msd_list, msd);
+}
+
 void eir_parse(struct eir_data *eir, const uint8_t *eir_data, uint8_t eir_len)
 {
 	uint16_t len = 0;
@@ -240,6 +258,10 @@ void eir_parse(struct eir_data *eir, const uint8_t *eir_data, uint8_t eir_len)
 			eir->did_product = data[4] | (data[5] << 8);
 			eir->did_version = data[6] | (data[7] << 8);
 			break;
+
+		case EIR_MANUFACTURER_DATA:
+			eir_parse_msd(eir, data, data_len);
+			break;
 		}
 
 		eir_data += field_len + 1;
src/eir.h +8 -0
index e486fa2..1d5d3b3 100644
@@ -37,6 +37,7 @@
 #define EIR_SSP_RANDOMIZER          0x0F  /* SSP Randomizer */
 #define EIR_DEVICE_ID               0x10  /* device ID */
 #define EIR_GAP_APPEARANCE          0x19  /* GAP appearance */
+#define EIR_MANUFACTURER_DATA       0xFF  /* Manufacturer Specific Data */
 
 /* Flags Descriptions */
 #define EIR_LIM_DISC                0x01 /* LE Limited Discoverable Mode */
@@ -47,6 +48,12 @@
 #define EIR_SIM_HOST                0x10 /* Simultaneous LE and BR/EDR to Same
 					    Device Capable (Host) */
 
+struct eir_msd {
+	uint16_t company;
+	uint8_t data[HCI_MAX_EIR_LENGTH];
+	uint8_t data_len;
+};
+
 struct eir_data {
 	GSList *services;
 	unsigned int flags;
@@ -62,6 +69,7 @@ struct eir_data {
 	uint16_t did_product;
 	uint16_t did_version;
 	uint16_t did_source;
+	GSList *msd_list;
 };
 
 void eir_data_free(struct eir_data *eir);
Signature · 1 line
1.9.1
#2 lore raw

Re: [PATCH v4 1/2] core: Add Manufacturer Specific Data EIR field

Hi Alfonso,

Quoted 5 lines · Alfonso Acosta

Add data structure and parsing support.

---
 src/eir.c | 22 ++++++++++++++++++++++
 src/eir.h |  8 ++++++++
 2 files changed, 30 insertions(+)

This patch doesn't compile:

In file included from android/bluetooth.c:45:0:
./src/eir.h:53:15: error: ‘HCI_MAX_EIR_LENGTH’ undeclared here (not in a function)

  uint8_t data[HCI_MAX_EIR_LENGTH];
               ^

make[1]: *** [android/bluetooth.o] Error 1

Johan

#3 lore raw

Re: [PATCH v4 1/2] core: Add Manufacturer Specific Data EIR field

Hi Johan,

Quoted 8 lines

This patch doesn't compile:

In file included from android/bluetooth.c:45:0:
./src/eir.h:53:15: error: ‘HCI_MAX_EIR_LENGTH’ undeclared here (not in a function)

  uint8_t data[HCI_MAX_EIR_LENGTH];
               ^

make[1]: *** [android/bluetooth.o] Error 1

I wasn't enabling android and this problem didn't surface otherwise. v5 fixes this.

Signature · 5 lines
Alfonso Acosta

Embedded Systems Engineer at Spotify
Birger Jarlsgatan 61, Stockholm, Sweden
http://www.spotify.com
#4 lore raw

PATCH v4 2/2 core: Add subscription API for Manufacturer Specific Data

In reply to Alfonso Acosta

This patch allows plugins to be notified whenever an adapter receives Manufacturer Specific Data in the advertising reports from a device.

This can happen when new device is discovered or when we autoconnect to it.

---
 src/adapter.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 src/adapter.h | 10 ++++++++++
 2 files changed, 54 insertions(+)
src/adapter.c +44 -0
index 92ee1a0..f23db62 100644
@@ -206,6 +206,7 @@ struct btd_adapter {
 	gboolean initialized;
 
 	GSList *pin_callbacks;
+	GSList *msd_callbacks;
 
 	GSList *drivers;
 	GSList *profiles;
@@ -4549,6 +4550,9 @@ static void adapter_remove(struct btd_adapter *adapter)
 
 	g_slist_free(adapter->pin_callbacks);
 	adapter->pin_callbacks = NULL;
+
+	g_slist_free(adapter->msd_callbacks);
+	adapter->msd_callbacks = NULL;
 }
 
 const char *adapter_get_path(struct btd_adapter *adapter)
@@ -4647,6 +4651,28 @@ static void confirm_name(struct btd_adapter *adapter, const bdaddr_t *bdaddr,
 						confirm_name_timeout, adapter);
 }
 
+void adapter_msd_notify(struct btd_adapter *adapter, struct btd_device *dev,
+						GSList *msd_list)
+{
+	GSList *cb_l, *cb_next;
+	GSList *msd_l, *msd_next;
+
+	for (cb_l = adapter->msd_callbacks; cb_l != NULL; cb_l = cb_next) {
+		btd_msd_cb_t cb = cb_l->data;
+
+		cb_next = g_slist_next(cb_l);
+
+		for (msd_l = msd_list; msd_l != NULL; msd_l = msd_next) {
+			const struct eir_msd *msd = msd_l->data;
+
+			msd_next = g_slist_next(msd_l);
+
+			cb(adapter, dev, msd->company, msd->data,
+								msd->data_len);
+		}
+	}
+}
+
 static void update_found_devices(struct btd_adapter *adapter,
 					const bdaddr_t *bdaddr,
 					uint8_t bdaddr_type, int8_t rssi,
@@ -4738,6 +4764,9 @@ static void update_found_devices(struct btd_adapter *adapter,
 
 	device_add_eir_uuids(dev, eir_data.services);
 
+	if (eir_data.msd_list)
+		adapter_msd_notify(adapter, dev, eir_data.msd_list);
+
 	eir_data_free(&eir_data);
 
 	/*
@@ -5173,6 +5202,18 @@ void btd_adapter_unregister_pin_cb(struct btd_adapter *adapter,
 	adapter->pin_callbacks = g_slist_remove(adapter->pin_callbacks, cb);
 }
 
+void btd_adapter_unregister_msd_cb(struct btd_adapter *adapter,
+							btd_msd_cb_t cb)
+{
+	adapter->msd_callbacks = g_slist_remove(adapter->msd_callbacks, cb);
+}
+
+void btd_adapter_register_msd_cb(struct btd_adapter *adapter,
+							btd_msd_cb_t cb)
+{
+	adapter->msd_callbacks = g_slist_prepend(adapter->msd_callbacks, cb);
+}
+
 int btd_adapter_set_fast_connectable(struct btd_adapter *adapter,
 							gboolean enable)
 {
@@ -6663,6 +6704,9 @@ static void connected_callback(uint16_t index, uint16_t length,
 		btd_device_device_set_name(device, eir_data.name);
 	}
 
+	if (eir_data.msd_list)
+		adapter_msd_notify(adapter, device, eir_data.msd_list);
+
 	eir_data_free(&eir_data);
 }
 
src/adapter.h +10 -0
index 6801fee..8f4098a 100644
@@ -138,6 +138,16 @@ struct btd_adapter_pin_cb_iter *btd_adapter_pin_cb_iter_new(
 void btd_adapter_pin_cb_iter_free(struct btd_adapter_pin_cb_iter *iter);
 bool btd_adapter_pin_cb_iter_end(struct btd_adapter_pin_cb_iter *iter);
 
+typedef void (*btd_msd_cb_t) (struct btd_adapter *adapter,
+							struct btd_device *dev,
+							uint16_t company,
+							const uint8_t *data,
+							uint8_t data_len);
+void btd_adapter_register_msd_cb(struct btd_adapter *adapter,
+							btd_msd_cb_t cb);
+void btd_adapter_unregister_msd_cb(struct btd_adapter *adapter,
+							btd_msd_cb_t cb);
+
 /* If TRUE, enables fast connectabe, i.e. reduces page scan interval and changes
  * type. If FALSE, disables fast connectable, i.e. sets page scan interval and
  * type to default values. Valid for both connectable and discoverable modes. */
Signature · 1 line
1.9.1
#5 lore raw

Re: [PATCH v4 2/2] core: Add subscription API for Manufacturer Specific Data

Hi Alfonso,

Quoted 9 lines · Alfonso Acosta

This patch allows plugins to be notified whenever an adapter receives Manufacturer Specific Data in the advertising reports from a device.

This can happen when new device is discovered or when we autoconnect to it.

---
 src/adapter.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 src/adapter.h | 10 ++++++++++
 2 files changed, 54 insertions(+)

This patch also doesn't compile (the error from the first patch might have gone away, but then there's this new one):

src/adapter.c:4654:6: error: no previous declaration for ‘adapter_msd_notify’ [-Werror=missing-declarations]
void adapter_msd_notify(struct btd_adapter *adapter, struct btd_device *dev,

      ^

cc1: all warnings being treated as errors
make[1]: *** [src/bluetoothd-adapter.o] Error 1

Johan

A message that is not in the archive (sent off-list or not yet received): CAHF=Y4q0ZRbGAUMtMi5vA13hQFn8JCmu+7B1rLdRxDSO6jxiCg@mail.gmail.com

#7 lore raw

Re: [PATCH v4 2/2] core: Add subscription API for Manufacturer Specific Data

Hi Johan,

Quoted 9 lines

This patch also doesn't compile (the error from the first patch might have gone away, but then there's this new one):

src/adapter.c:4654:6: error: no previous declaration for ‘adapter_msd_notify’ [-Werror=missing-declarations]
void adapter_msd_notify(struct btd_adapter *adapter, struct btd_device *dev,

      ^

cc1: all warnings being treated as errors
make[1]: *** [src/bluetoothd-adapter.o] Error 1

v5 corrects this problem.

I didn't know that -Wall is only enabled when passing --enable-maintainer-mode to configure (which I wasn't doing).

Signature · 5 lines
Alfonso Acosta

Embedded Systems Engineer at Spotify
Birger Jarlsgatan 61, Stockholm, Sweden
http://www.spotify.com