From d6d9d66972c4c8f3bf119a0f1753a2e938ac70f6 Mon Sep 17 00:00:00 2001
From: Hauke Petersen <hauke.petersen@fu-berlin.de>
Date: Fri, 7 Dec 2018 18:05:00 +0100
Subject: [PATCH] examples/nimble_gatt: use bluetil_ad

---
 examples/nimble_gatt/Makefile |  3 +++
 examples/nimble_gatt/main.c   | 29 ++++++++---------------------
 2 files changed, 11 insertions(+), 21 deletions(-)

diff --git a/examples/nimble_gatt/Makefile b/examples/nimble_gatt/Makefile
index ecf821f78b..e256343e6a 100644
--- a/examples/nimble_gatt/Makefile
+++ b/examples/nimble_gatt/Makefile
@@ -13,6 +13,9 @@ RIOTBASE ?= $(CURDIR)/../..
 # Include NimBLE
 USEPKG += nimble
 
+# We also use the AD part of the BLE helper module
+USEMODULE += bluetil_ad
+
 # Comment this out to disable code in RIOT that does safety checking
 # which is not needed in a production environment but helps in the
 # development process:
diff --git a/examples/nimble_gatt/main.c b/examples/nimble_gatt/main.c
index df55a279ca..c9a7ffdc3b 100644
--- a/examples/nimble_gatt/main.c
+++ b/examples/nimble_gatt/main.c
@@ -23,39 +23,26 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "net/bluetil/ad.h"
+
 #include "host/ble_hs.h"
 #include "host/util/util.h"
 #include "host/ble_gatt.h"
 #include "services/gap/ble_svc_gap.h"
 #include "services/gatt/ble_svc_gatt.h"
 
-static const char device_name[] = "NimBLE on RIOT";
+static const char *device_name = "NimBLE on RIOT";
 static uint8_t own_addr_type;
 
-
 static void start_advertise(void);
 
-static void put_ad(uint8_t ad_type, uint8_t ad_len, const void *ad, uint8_t *buf,
-                   uint8_t *len)
-{
-    buf[(*len)++] = ad_len + 1;
-    buf[(*len)++] = ad_type;
-
-    memcpy(&buf[*len], ad, ad_len);
-
-    *len += ad_len;
-}
-
 static void update_ad(void)
 {
-    uint8_t ad[BLE_HS_ADV_MAX_SZ];
-    uint8_t ad_len = 0;
-    uint8_t ad_flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP;
-
-    put_ad(BLE_HS_ADV_TYPE_FLAGS, 1, &ad_flags, ad, &ad_len);
-    put_ad(BLE_HS_ADV_TYPE_COMP_NAME, sizeof(device_name), device_name, ad, &ad_len);
-
-    ble_gap_adv_set_data(ad, ad_len);
+    uint8_t buf[BLE_HS_ADV_MAX_SZ];
+    bluetil_ad_t ad;
+    bluetil_ad_init_with_flags(&ad, buf, sizeof(buf), BLUETIL_AD_FLAGS_DEFAULT);
+    bluetil_ad_add_name(&ad, device_name);
+    ble_gap_adv_set_data(ad.buf, ad.pos);
 }
 
 static int gap_event_cb(struct ble_gap_event *event, void *arg)
-- 
GitLab