From b54c10cd15a98965db557ab7e0af58c22672a905 Mon Sep 17 00:00:00 2001
From: Matthias Ringwald <matthias@ringwald.ch>
Date: Thu, 23 May 2019 18:16:18 +0200
Subject: [PATCH] mesh: use MESH_PROXY_ events

---
 .../gatt-service/mesh_proxy_service_server.c  | 13 +++++------
 src/ble/mesh/gatt_bearer.c                    | 23 +++++++++++++------
 test/mesh/mesh.c                              | 16 ++++++-------
 3 files changed, 30 insertions(+), 22 deletions(-)

diff --git a/src/ble/gatt-service/mesh_proxy_service_server.c b/src/ble/gatt-service/mesh_proxy_service_server.c
index d6694c74b..0c2354ac3 100644
--- a/src/ble/gatt-service/mesh_proxy_service_server.c
+++ b/src/ble/gatt-service/mesh_proxy_service_server.c
@@ -75,15 +75,14 @@ static att_service_handler_t mesh_proxy_service;
 static mesh_proxy_t mesh_proxy;
 
 
-static void mesh_proxy_service_emit_link_open(hci_con_handle_t con_handle, uint8_t status){
-    uint8_t event[7] = { HCI_EVENT_MESH_META, 5, MESH_PB_TRANSPORT_LINK_OPEN, status};
+static void mesh_proxy_service_emit_connected(hci_con_handle_t con_handle){
+    uint8_t event[5] = { HCI_EVENT_MESH_META, 3, MESH_PROXY_CONNECTED};
     little_endian_store_16(event, 4, con_handle);
-    event[6] = PB_TYPE_GATT;
     mesh_proxy_service_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event));
 }
 
-static void mesh_proxy_service_emit_link_close(hci_con_handle_t con_handle, uint8_t reason){
-    uint8_t event[5] = { HCI_EVENT_MESH_META, 3, MESH_PB_TRANSPORT_LINK_CLOSED};
+static void mesh_proxy_service_emit_disconnected(hci_con_handle_t con_handle){
+    uint8_t event[5] = { HCI_EVENT_MESH_META, 3, MESH_PROXY_DISCONNECTED};
     little_endian_store_16(event, 4, con_handle);
     mesh_proxy_service_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event));
 }
@@ -140,9 +139,9 @@ static int mesh_proxy_service_write_callback(hci_con_handle_t con_handle, uint16
         instance->data_out_client_configuration_descriptor_value = little_endian_read_16(buffer, 0);
         log_info("mesh_proxy_service_write_callback: data out notify enabled %d, con handle 0x%02x", instance->data_out_client_configuration_descriptor_value, con_handle);
         if (instance->data_out_client_configuration_descriptor_value){
-            mesh_proxy_service_emit_link_open(con_handle, 0);
+            mesh_proxy_service_emit_connected(con_handle);
         } else {
-            mesh_proxy_service_emit_link_close(con_handle, 0);
+            mesh_proxy_service_emit_disconnected(con_handle);
         }
         return 0;
     }
diff --git a/src/ble/mesh/gatt_bearer.c b/src/ble/mesh/gatt_bearer.c
index 2576d3ece..d8e35b5ed 100644
--- a/src/ble/mesh/gatt_bearer.c
+++ b/src/ble/mesh/gatt_bearer.c
@@ -97,6 +97,14 @@ static void gatt_bearer_emit_can_send_now(void){
     }
 }
 
+static void gatt_bearer_emit_event_for_all(uint8_t * packet, uint16_t size){
+    unsigned int i;
+    for (i=0; i < NUM_TYPES; i++){
+        if ( client_callbacks[i] == NULL) continue;
+        (*client_callbacks[last_sender])(HCI_EVENT_PACKET, 0, packet, size);
+    }
+}
+
 static void gatt_bearer_request(mesh_msg_type_t type_id){
     request_can_send_now[type_id] = 1;
     mesh_proxy_service_server_request_can_send_now(gatt_bearer_con_handle);
@@ -219,14 +227,15 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe
             switch (hci_event_packet_get_type(packet)) {
                 case HCI_EVENT_MESH_META:
                     switch (hci_event_mesh_meta_get_subevent_code(packet)){
-                        case MESH_PB_TRANSPORT_LINK_OPEN:
-                        case MESH_PB_TRANSPORT_LINK_CLOSED:
+                        case MESH_PROXY_DISCONNECTED:
                             gatt_bearer_mtu = ATT_DEFAULT_MTU;
-                            gatt_bearer_con_handle  = mesh_pb_transport_link_open_event_get_pb_transport_cid(packet);
-                            if ((*client_callbacks[MESH_MSG_TYPE_NETWORK_PDU])){
-                                (*client_callbacks[MESH_MSG_TYPE_NETWORK_PDU])(HCI_EVENT_PACKET, 0, packet, size);
-                            }
-                            break; 
+                            gatt_bearer_con_handle = mesh_proxy_connected_event_get_con_handle(packet);
+                            gatt_bearer_emit_event_for_all(packet, size);
+                            break;
+                        case MESH_PROXY_CONNECTED:
+                            gatt_bearer_con_handle = HCI_CON_HANDLE_INVALID;
+                            gatt_bearer_emit_event_for_all(packet, size);
+                            break;
                         case MESH_SUBEVENT_CAN_SEND_NOW:
                             con_handle = little_endian_read_16(packet, 3); 
                             if (con_handle == HCI_CON_HANDLE_INVALID) return;
diff --git a/test/mesh/mesh.c b/test/mesh/mesh.c
index 2c7b11b5a..31b824568 100644
--- a/test/mesh/mesh.c
+++ b/test/mesh/mesh.c
@@ -441,7 +441,7 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
     }
 }
 
-static void mesh_message_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
+static void mesh_provisioning_message_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
     if (packet_type != HCI_EVENT_PACKET) return;
     mesh_provisioning_data_t provisioning_data;
     mesh_network_key_t * network_key;
@@ -2673,7 +2673,7 @@ static void virtual_address_complete(void * arg){
 static void key_derived(void * arg){
 }
 
-static void packet_handler_for_mesh_beacon(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
+static void mesh_proxy_packet_handler_beacon(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
     printf("packet_handler_for_mesh_beacon\n");
     
     switch (packet_type){
@@ -2701,7 +2701,7 @@ static void packet_handler_for_mesh_beacon(uint8_t packet_type, uint16_t channel
     }
 }
 
-static void packet_handler_for_mesh_network_pdu(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
+static void mesh_proxy_packet_handler_network_pdu(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
     printf("packet_handler_for_mesh_network_pdu\n");
     
     switch (packet_type){
@@ -2714,8 +2714,8 @@ static void packet_handler_for_mesh_network_pdu(uint8_t packet_type, uint16_t ch
             switch (hci_event_packet_get_type(packet)){
                 case HCI_EVENT_MESH_META:
                     switch (hci_event_mesh_meta_get_subevent_code(packet)){
-                        case MESH_PB_TRANSPORT_LINK_OPEN:
-                            printf("mesh_proxy_server: MESH_PB_TRANSPORT_LINK_OPEN\n");
+                        case MESH_PROXY_CONNECTED:
+                            printf("mesh_proxy_server: MESH_PROXY_CONNECTED\n");
                             printf("+ Setup Secure Network Beacon\n");
                             mesh_secure_network_beacon[0] = BEACON_TYPE_SECURE_NETWORK;
                             mesh_secure_network_beacon[1] = mesh_flags;
@@ -2880,8 +2880,8 @@ int btstack_main(void)
 
     // Setup GATT bearer
     gatt_bearer_init();
-    gatt_bearer_register_for_mesh_network_pdu(&packet_handler_for_mesh_network_pdu);
-    gatt_bearer_register_for_mesh_beacon(&packet_handler_for_mesh_beacon);
+    gatt_bearer_register_for_mesh_network_pdu(&mesh_proxy_packet_handler_network_pdu);
+    gatt_bearer_register_for_mesh_beacon(&mesh_proxy_packet_handler_beacon);
 
     gatt_bearer_register_for_mesh_proxy_configuration(&packet_handler_for_mesh_proxy_configuration);
     mesh_network_set_proxy_message_handler(proxy_configuration_message_handler);
@@ -2894,7 +2894,7 @@ int btstack_main(void)
 
     // Provisioning in device role
     provisioning_device_init(device_uuid);
-    provisioning_device_register_packet_handler(&mesh_message_handler);
+    provisioning_device_register_packet_handler(&mesh_provisioning_message_handler);
 
     // Network layer
     mesh_network_init();