From ff1c8a81b3074fff77c771c0b8775406dba5360d Mon Sep 17 00:00:00 2001
From: Matthias Ringwald <matthias@ringwald.ch>
Date: Mon, 3 Jun 2019 15:59:46 +0200
Subject: [PATCH] mesh: extract gatt_bearer_setup_advertising_with_network_id

---
 src/ble/mesh/gatt_bearer.c | 18 ++++++++++++++++++
 src/ble/mesh/gatt_bearer.h |  8 ++++++++
 test/mesh/mesh.c           | 10 +---------
 3 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/src/ble/mesh/gatt_bearer.c b/src/ble/mesh/gatt_bearer.c
index 9785f053e..3b20a0d3d 100644
--- a/src/ble/mesh/gatt_bearer.c
+++ b/src/ble/mesh/gatt_bearer.c
@@ -45,6 +45,7 @@
 #include "ble/core.h"
 #include "bluetooth.h"
 #include "bluetooth_data_types.h"
+#include "bluetooth_gatt.h"
 #include "btstack_debug.h"
 #include "btstack_util.h"
 #include "btstack_run_loop.h"
@@ -75,6 +76,17 @@ static mesh_msg_type_t msg_type;
 static uint16_t gatt_bearer_mtu;
 static hci_con_handle_t gatt_bearer_con_handle;
 
+static const uint8_t adv_data_with_network_id_template[] = {
+    // Flags general discoverable, BR/EDR not supported
+    0x02, BLUETOOTH_DATA_TYPE_FLAGS, 0x06, 
+    // 16-bit Service UUIDs
+    0x03, BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS, ORG_BLUETOOTH_SERVICE_MESH_PROXY & 0xff, ORG_BLUETOOTH_SERVICE_MESH_PROXY >> 8,
+    // Service Data
+    0x0C, BLUETOOTH_DATA_TYPE_SERVICE_DATA, ORG_BLUETOOTH_SERVICE_MESH_PROXY & 0xff, ORG_BLUETOOTH_SERVICE_MESH_PROXY >> 8, 
+          // MESH_IDENTIFICATION_NETWORK_ID_TYPE
+          MESH_IDENTIFICATION_NETWORK_ID_TYPE 
+};
+
 // round-robin
 static void gatt_bearer_emit_can_send_now(void){
     // if (gatt_active) return;
@@ -328,3 +340,9 @@ void gatt_bearer_send_mesh_proxy_configuration(const uint8_t * data, uint16_t da
     msg_type = MESH_MSG_TYPE_PROXY_CONFIGURATION;
     gatt_bearer_send_pdu(gatt_bearer_con_handle, data, data_len);
 }
+
+uint8_t gatt_bearer_setup_advertising_with_network_id(uint8_t * buffer, uint8_t * network_id){
+    memcpy(&buffer[0], adv_data_with_network_id_template, 12);
+    memcpy(&buffer[12], network_id, 8);
+    return 20;
+}
diff --git a/src/ble/mesh/gatt_bearer.h b/src/ble/mesh/gatt_bearer.h
index 665048279..db6caad08 100644
--- a/src/ble/mesh/gatt_bearer.h
+++ b/src/ble/mesh/gatt_bearer.h
@@ -74,6 +74,14 @@ void gatt_bearer_send_mesh_network_pdu(const uint8_t * network_pdu, uint16_t siz
 void gatt_bearer_send_mesh_beacon(const uint8_t * beacon_update, uint16_t size); 
 void gatt_bearer_send_mesh_proxy_configuration(const uint8_t * proxy_configuration, uint16_t size); 
 
+/* Utility functions */
+/**
+ * Setup Proxy Advertisement for given network id
+ * @param buffer (20 bytes)
+ * @param network_id (8 bytes)
+ */
+uint8_t gatt_bearer_setup_advertising_with_network_id(uint8_t * buffer, uint8_t * network_id);
+
 #if defined __cplusplus
 }
 #endif
diff --git a/test/mesh/mesh.c b/test/mesh/mesh.c
index 45032351c..0698f6f05 100644
--- a/test/mesh/mesh.c
+++ b/test/mesh/mesh.c
@@ -212,17 +212,9 @@ static void mesh_proxy_handle_get_random(void * arg){
 }
 #endif
 
-
 #ifdef USE_ADVERTISING_WITH_NETWORK_ID
 static void setup_advertising_with_network_id(void){
-
-    // dynamically store network ID into adv data
-    memcpy(&adv_data_with_network_id[12], network_id, sizeof(network_id));
-
-    // store in advertisement item
-    memset(&connectable_advertisement_item, 0, sizeof(connectable_advertisement_item));
-    connectable_advertisement_item.adv_length = adv_data_with_network_id_len;
-    memcpy(connectable_advertisement_item.adv_data, (uint8_t*) adv_data_with_network_id,  adv_data_with_network_id_len);
+    connectable_advertisement_item.adv_length = gatt_bearer_setup_advertising_with_network_id(connectable_advertisement_item.adv_data, network_id);
 
     // setup advertisements
     bd_addr_t null_addr;