xref: /btstack/src/mesh/gatt_bearer.c (revision 4662af4a88ca1e7d6d85dcfaea44271a9a60ea26)
177ba3d3fSMatthias Ringwald /*
277ba3d3fSMatthias Ringwald  * Copyright (C) 2017 BlueKitchen GmbH
377ba3d3fSMatthias Ringwald  *
477ba3d3fSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
577ba3d3fSMatthias Ringwald  * modification, are permitted provided that the following conditions
677ba3d3fSMatthias Ringwald  * are met:
777ba3d3fSMatthias Ringwald  *
877ba3d3fSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
977ba3d3fSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
1077ba3d3fSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
1177ba3d3fSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
1277ba3d3fSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
1377ba3d3fSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
1477ba3d3fSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
1577ba3d3fSMatthias Ringwald  *    from this software without specific prior written permission.
1677ba3d3fSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
1777ba3d3fSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
1877ba3d3fSMatthias Ringwald  *    monetary gain.
1977ba3d3fSMatthias Ringwald  *
2077ba3d3fSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
2177ba3d3fSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2277ba3d3fSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2377ba3d3fSMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
2477ba3d3fSMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2577ba3d3fSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2677ba3d3fSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
2777ba3d3fSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2877ba3d3fSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2977ba3d3fSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
3077ba3d3fSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3177ba3d3fSMatthias Ringwald  * SUCH DAMAGE.
3277ba3d3fSMatthias Ringwald  *
3377ba3d3fSMatthias Ringwald  * Please inquire about commercial licensing options at
3477ba3d3fSMatthias Ringwald  * [email protected]
3577ba3d3fSMatthias Ringwald  *
3677ba3d3fSMatthias Ringwald  */
3777ba3d3fSMatthias Ringwald 
3877ba3d3fSMatthias Ringwald #define __BTSTACK_FILE__ "gatt_bearer.c"
3977ba3d3fSMatthias Ringwald 
4077ba3d3fSMatthias Ringwald #include <string.h>
4177ba3d3fSMatthias Ringwald 
4277ba3d3fSMatthias Ringwald #include "ble/gatt-service/mesh_proxy_service_server.h"
4377ba3d3fSMatthias Ringwald #include "ble/att_server.h"
4477ba3d3fSMatthias Ringwald #include "mesh/gatt_bearer.h"
4577ba3d3fSMatthias Ringwald #include "ble/core.h"
4677ba3d3fSMatthias Ringwald #include "bluetooth.h"
4777ba3d3fSMatthias Ringwald #include "bluetooth_data_types.h"
4877ba3d3fSMatthias Ringwald #include "bluetooth_gatt.h"
4977ba3d3fSMatthias Ringwald #include "btstack_debug.h"
5077ba3d3fSMatthias Ringwald #include "btstack_util.h"
5177ba3d3fSMatthias Ringwald #include "btstack_run_loop.h"
5277ba3d3fSMatthias Ringwald #include "btstack_event.h"
5377ba3d3fSMatthias Ringwald #include "gap.h"
5477ba3d3fSMatthias Ringwald #include "btstack_event.h"
5577ba3d3fSMatthias Ringwald #include "provisioning.h"
5677ba3d3fSMatthias Ringwald 
5777ba3d3fSMatthias Ringwald #define NUM_TYPES 3
5877ba3d3fSMatthias Ringwald 
5977ba3d3fSMatthias Ringwald static btstack_packet_handler_t client_callbacks[NUM_TYPES];
6077ba3d3fSMatthias Ringwald static int request_can_send_now[NUM_TYPES];
6177ba3d3fSMatthias Ringwald static int last_sender;
6277ba3d3fSMatthias Ringwald 
6377ba3d3fSMatthias Ringwald // share buffer for reassembly and segmentation - protocol is half-duplex
6477ba3d3fSMatthias Ringwald static union {
6577ba3d3fSMatthias Ringwald     uint8_t  reassembly_buffer[MESH_PROV_MAX_PROXY_PDU];
6677ba3d3fSMatthias Ringwald     uint8_t  segmentation_buffer[MESH_PROV_MAX_PROXY_PDU];
6777ba3d3fSMatthias Ringwald } sar_buffer;
6877ba3d3fSMatthias Ringwald 
6977ba3d3fSMatthias Ringwald static const uint8_t * proxy_pdu;
7077ba3d3fSMatthias Ringwald static uint16_t proxy_pdu_size;
7177ba3d3fSMatthias Ringwald static uint8_t outgoing_ready;
7277ba3d3fSMatthias Ringwald static uint16_t reassembly_offset;
7377ba3d3fSMatthias Ringwald static uint16_t segmentation_offset;
7477ba3d3fSMatthias Ringwald static mesh_msg_sar_field_t segmentation_state;
7577ba3d3fSMatthias Ringwald static mesh_msg_type_t msg_type;
7677ba3d3fSMatthias Ringwald static uint16_t gatt_bearer_mtu;
7777ba3d3fSMatthias Ringwald static hci_con_handle_t gatt_bearer_con_handle;
7877ba3d3fSMatthias Ringwald 
7977ba3d3fSMatthias Ringwald static const uint8_t adv_data_with_network_id_template[] = {
8077ba3d3fSMatthias Ringwald     // Flags general discoverable, BR/EDR not supported
8177ba3d3fSMatthias Ringwald     0x02, BLUETOOTH_DATA_TYPE_FLAGS, 0x06,
8277ba3d3fSMatthias Ringwald     // 16-bit Service UUIDs
8377ba3d3fSMatthias Ringwald     0x03, BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS, ORG_BLUETOOTH_SERVICE_MESH_PROXY & 0xff, ORG_BLUETOOTH_SERVICE_MESH_PROXY >> 8,
8477ba3d3fSMatthias Ringwald     // Service Data
8577ba3d3fSMatthias Ringwald     0x0C, BLUETOOTH_DATA_TYPE_SERVICE_DATA, ORG_BLUETOOTH_SERVICE_MESH_PROXY & 0xff, ORG_BLUETOOTH_SERVICE_MESH_PROXY >> 8,
8677ba3d3fSMatthias Ringwald           // MESH_IDENTIFICATION_NETWORK_ID_TYPE
8777ba3d3fSMatthias Ringwald           MESH_IDENTIFICATION_NETWORK_ID_TYPE
8877ba3d3fSMatthias Ringwald };
8977ba3d3fSMatthias Ringwald 
9077ba3d3fSMatthias Ringwald // round-robin
9177ba3d3fSMatthias Ringwald static void gatt_bearer_emit_can_send_now(void){
9277ba3d3fSMatthias Ringwald     // if (gatt_active) return;
9377ba3d3fSMatthias Ringwald     int countdown = NUM_TYPES;
9477ba3d3fSMatthias Ringwald     while (countdown--) {
9577ba3d3fSMatthias Ringwald         last_sender++;
9677ba3d3fSMatthias Ringwald         if (last_sender == NUM_TYPES) {
9777ba3d3fSMatthias Ringwald             last_sender = 0;
9877ba3d3fSMatthias Ringwald         }
9977ba3d3fSMatthias Ringwald         if (request_can_send_now[last_sender]){
10077ba3d3fSMatthias Ringwald             request_can_send_now[last_sender] = 0;
10177ba3d3fSMatthias Ringwald             // emit can send now
10277ba3d3fSMatthias Ringwald             uint8_t event[5];
10377ba3d3fSMatthias Ringwald             int pos = 0;
10477ba3d3fSMatthias Ringwald             event[pos++] = HCI_EVENT_MESH_META;
10577ba3d3fSMatthias Ringwald             event[pos++] = 1;
10677ba3d3fSMatthias Ringwald             event[pos++] = MESH_SUBEVENT_CAN_SEND_NOW;
10777ba3d3fSMatthias Ringwald             little_endian_store_16(event, pos, gatt_bearer_con_handle);
10877ba3d3fSMatthias Ringwald             pos += 2;
10977ba3d3fSMatthias Ringwald             (*client_callbacks[last_sender])(HCI_EVENT_PACKET, 0, &event[0], pos);
11077ba3d3fSMatthias Ringwald             return;
11177ba3d3fSMatthias Ringwald         }
11277ba3d3fSMatthias Ringwald     }
11377ba3d3fSMatthias Ringwald }
11477ba3d3fSMatthias Ringwald 
11577ba3d3fSMatthias Ringwald static void gatt_bearer_emit_message_sent(mesh_msg_type_t type_id){
11677ba3d3fSMatthias Ringwald     uint8_t event[5];
11777ba3d3fSMatthias Ringwald     int pos = 0;
11877ba3d3fSMatthias Ringwald     event[pos++] = HCI_EVENT_MESH_META;
11977ba3d3fSMatthias Ringwald     event[pos++] = 1;
12077ba3d3fSMatthias Ringwald     event[pos++] = MESH_SUBEVENT_MESSAGE_SENT;
12177ba3d3fSMatthias Ringwald     little_endian_store_16(event, pos, gatt_bearer_con_handle);
12277ba3d3fSMatthias Ringwald     pos += 2;
12377ba3d3fSMatthias Ringwald     (*client_callbacks[type_id])(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
12477ba3d3fSMatthias Ringwald }
12577ba3d3fSMatthias Ringwald 
12677ba3d3fSMatthias Ringwald static void gatt_bearer_emit_event_for_all(uint8_t * packet, uint16_t size){
12777ba3d3fSMatthias Ringwald     unsigned int i;
12877ba3d3fSMatthias Ringwald     for (i=0; i < NUM_TYPES; i++){
12977ba3d3fSMatthias Ringwald         if ( client_callbacks[i] == NULL) continue;
13077ba3d3fSMatthias Ringwald         (*client_callbacks[last_sender])(HCI_EVENT_PACKET, 0, packet, size);
13177ba3d3fSMatthias Ringwald     }
13277ba3d3fSMatthias Ringwald }
13377ba3d3fSMatthias Ringwald 
13477ba3d3fSMatthias Ringwald static void gatt_bearer_request(mesh_msg_type_t type_id){
13577ba3d3fSMatthias Ringwald     request_can_send_now[type_id] = 1;
13677ba3d3fSMatthias Ringwald     mesh_proxy_service_server_request_can_send_now(gatt_bearer_con_handle);
13777ba3d3fSMatthias Ringwald }
13877ba3d3fSMatthias Ringwald 
13977ba3d3fSMatthias Ringwald 
14077ba3d3fSMatthias Ringwald static void gatt_bearer_start_sending(hci_con_handle_t con_handle){
14177ba3d3fSMatthias Ringwald     uint16_t pdu_segment_len = btstack_min(proxy_pdu_size - segmentation_offset, gatt_bearer_mtu - 1 - 3);
14277ba3d3fSMatthias Ringwald     sar_buffer.segmentation_buffer[0] = (segmentation_state << 6) | msg_type;
14377ba3d3fSMatthias Ringwald     memcpy(&sar_buffer.segmentation_buffer[1], &proxy_pdu[segmentation_offset], pdu_segment_len);
14477ba3d3fSMatthias Ringwald     segmentation_offset += pdu_segment_len;
14577ba3d3fSMatthias Ringwald     mesh_proxy_service_server_send_proxy_pdu(con_handle, sar_buffer.segmentation_buffer, pdu_segment_len + 1);
14677ba3d3fSMatthias Ringwald 
14777ba3d3fSMatthias Ringwald     switch (segmentation_state){
14877ba3d3fSMatthias Ringwald         case MESH_MSG_SAR_FIELD_COMPLETE_MSG:
14977ba3d3fSMatthias Ringwald         case MESH_MSG_SAR_FIELD_LAST_SEGMENT:
15077ba3d3fSMatthias Ringwald             // gatt_bearer_emit_pdu_sent(0);
15177ba3d3fSMatthias Ringwald             outgoing_ready = 0;
15277ba3d3fSMatthias Ringwald             gatt_bearer_emit_message_sent(msg_type);
15377ba3d3fSMatthias Ringwald             break;
15477ba3d3fSMatthias Ringwald         case MESH_MSG_SAR_FIELD_CONTINUE:
15577ba3d3fSMatthias Ringwald         case MESH_MSG_SAR_FIELD_FIRST_SEGMENT:
15677ba3d3fSMatthias Ringwald             if ((proxy_pdu_size - segmentation_offset) > (gatt_bearer_mtu - 1 - 3)){
15777ba3d3fSMatthias Ringwald                 segmentation_state = MESH_MSG_SAR_FIELD_CONTINUE;
15877ba3d3fSMatthias Ringwald             } else {
15977ba3d3fSMatthias Ringwald                 segmentation_state = MESH_MSG_SAR_FIELD_LAST_SEGMENT;
16077ba3d3fSMatthias Ringwald             }
16177ba3d3fSMatthias Ringwald             mesh_proxy_service_server_request_can_send_now(con_handle);
16277ba3d3fSMatthias Ringwald             break;
16377ba3d3fSMatthias Ringwald         default:
16477ba3d3fSMatthias Ringwald             break;
16577ba3d3fSMatthias Ringwald     }
16677ba3d3fSMatthias Ringwald }
16777ba3d3fSMatthias Ringwald 
16877ba3d3fSMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
16977ba3d3fSMatthias Ringwald     UNUSED(channel);
17077ba3d3fSMatthias Ringwald     UNUSED(size);
17177ba3d3fSMatthias Ringwald     mesh_msg_sar_field_t msg_sar_field;
17277ba3d3fSMatthias Ringwald 
17377ba3d3fSMatthias Ringwald     int pdu_segment_len;
17477ba3d3fSMatthias Ringwald     int pos;
17577ba3d3fSMatthias Ringwald     hci_con_handle_t con_handle;
17677ba3d3fSMatthias Ringwald     int send_to_mesh_network;
17777ba3d3fSMatthias Ringwald 
17877ba3d3fSMatthias Ringwald     switch (packet_type) {
17977ba3d3fSMatthias Ringwald         case MESH_PROXY_DATA_PACKET:
18077ba3d3fSMatthias Ringwald             pos = 0;
18177ba3d3fSMatthias Ringwald             // on provisioning PDU call packet handler with PROVISIONG_DATA type
18277ba3d3fSMatthias Ringwald             msg_sar_field = packet[pos] >> 6;
18377ba3d3fSMatthias Ringwald             msg_type = packet[pos] & 0x3F;
18477ba3d3fSMatthias Ringwald             pos++;
18577ba3d3fSMatthias Ringwald 
18677ba3d3fSMatthias Ringwald             switch (msg_type){
18777ba3d3fSMatthias Ringwald                 case MESH_MSG_TYPE_NETWORK_PDU:
18877ba3d3fSMatthias Ringwald                 case MESH_MSG_TYPE_BEACON:
18977ba3d3fSMatthias Ringwald                 case MESH_MSG_TYPE_PROXY_CONFIGURATION:
19077ba3d3fSMatthias Ringwald                     if (!client_callbacks[msg_type]){
19177ba3d3fSMatthias Ringwald                         log_error("client callback not defined");
19277ba3d3fSMatthias Ringwald                         return;
19377ba3d3fSMatthias Ringwald                     }
19477ba3d3fSMatthias Ringwald                     break;
19577ba3d3fSMatthias Ringwald                 default:
19677ba3d3fSMatthias Ringwald                     log_info("gatt bearer: message type %d not supported", msg_type);
19777ba3d3fSMatthias Ringwald                     return;
19877ba3d3fSMatthias Ringwald             }
19977ba3d3fSMatthias Ringwald             pdu_segment_len = size - pos;
20077ba3d3fSMatthias Ringwald 
20177ba3d3fSMatthias Ringwald             if (sizeof(sar_buffer.reassembly_buffer) - reassembly_offset < pdu_segment_len) {
20277ba3d3fSMatthias Ringwald                 log_error("sar buffer too small left %d, new to store %d", MESH_PROV_MAX_PROXY_PDU - reassembly_offset, pdu_segment_len);
20377ba3d3fSMatthias Ringwald                 break;
20477ba3d3fSMatthias Ringwald             }
20577ba3d3fSMatthias Ringwald 
20677ba3d3fSMatthias Ringwald             // update mtu if incoming packet is larger than default
20777ba3d3fSMatthias Ringwald             if (size > (ATT_DEFAULT_MTU - 1)){
20877ba3d3fSMatthias Ringwald                 log_info("Remote uses larger MTU, enable long PDUs");
20977ba3d3fSMatthias Ringwald                 gatt_bearer_mtu = att_server_get_mtu(channel);
21077ba3d3fSMatthias Ringwald             }
21177ba3d3fSMatthias Ringwald 
21277ba3d3fSMatthias Ringwald             switch (msg_sar_field){
21377ba3d3fSMatthias Ringwald                 case MESH_MSG_SAR_FIELD_COMPLETE_MSG:
21477ba3d3fSMatthias Ringwald                 case MESH_MSG_SAR_FIELD_FIRST_SEGMENT:
21577ba3d3fSMatthias Ringwald                     memset(sar_buffer.reassembly_buffer, 0, sizeof(sar_buffer.reassembly_buffer));
21677ba3d3fSMatthias Ringwald                     if (sizeof(sar_buffer.reassembly_buffer) < pdu_segment_len) return;
21777ba3d3fSMatthias Ringwald                     memcpy(sar_buffer.reassembly_buffer, packet+pos, pdu_segment_len);
21877ba3d3fSMatthias Ringwald                     reassembly_offset = pdu_segment_len;
21977ba3d3fSMatthias Ringwald                     break;
22077ba3d3fSMatthias Ringwald                 case MESH_MSG_SAR_FIELD_CONTINUE:
22177ba3d3fSMatthias Ringwald                     if ((sizeof(sar_buffer.reassembly_buffer) - reassembly_offset) < pdu_segment_len) return;
22277ba3d3fSMatthias Ringwald                     memcpy(sar_buffer.reassembly_buffer + reassembly_offset, packet+pos, pdu_segment_len);
22377ba3d3fSMatthias Ringwald                     reassembly_offset += pdu_segment_len;
22477ba3d3fSMatthias Ringwald                     return;
22577ba3d3fSMatthias Ringwald                 case MESH_MSG_SAR_FIELD_LAST_SEGMENT:
22677ba3d3fSMatthias Ringwald                     if ((sizeof(sar_buffer.reassembly_buffer) - reassembly_offset) < pdu_segment_len) return;
22777ba3d3fSMatthias Ringwald                     memcpy(sar_buffer.reassembly_buffer + reassembly_offset, packet+pos, pdu_segment_len);
22877ba3d3fSMatthias Ringwald                     reassembly_offset += pdu_segment_len;
22977ba3d3fSMatthias Ringwald                     break;
23077ba3d3fSMatthias Ringwald                 default:
23177ba3d3fSMatthias Ringwald                     break;
23277ba3d3fSMatthias Ringwald             }
23377ba3d3fSMatthias Ringwald 
23477ba3d3fSMatthias Ringwald             send_to_mesh_network = (msg_sar_field == MESH_MSG_SAR_FIELD_COMPLETE_MSG) || (msg_sar_field == MESH_MSG_SAR_FIELD_LAST_SEGMENT);
23577ba3d3fSMatthias Ringwald 
23677ba3d3fSMatthias Ringwald             if (!send_to_mesh_network) break;
23777ba3d3fSMatthias Ringwald             switch (msg_type){
23877ba3d3fSMatthias Ringwald                 case MESH_MSG_TYPE_NETWORK_PDU:
23977ba3d3fSMatthias Ringwald                 case MESH_MSG_TYPE_BEACON:
24077ba3d3fSMatthias Ringwald                 case MESH_MSG_TYPE_PROXY_CONFIGURATION:
24177ba3d3fSMatthias Ringwald                     if ((*client_callbacks[msg_type])){
24277ba3d3fSMatthias Ringwald                         (*client_callbacks[msg_type])(MESH_PROXY_DATA_PACKET, 0, sar_buffer.reassembly_buffer, reassembly_offset);
24377ba3d3fSMatthias Ringwald                     }
24477ba3d3fSMatthias Ringwald                     reassembly_offset = 0;
24577ba3d3fSMatthias Ringwald                     break;
24677ba3d3fSMatthias Ringwald                 default:
24777ba3d3fSMatthias Ringwald                     log_info("gatt bearer: message type %d not supported", msg_type);
24877ba3d3fSMatthias Ringwald                     return;
24977ba3d3fSMatthias Ringwald             }
25077ba3d3fSMatthias Ringwald 
25177ba3d3fSMatthias Ringwald             break;
25277ba3d3fSMatthias Ringwald 
25377ba3d3fSMatthias Ringwald         case HCI_EVENT_PACKET:
25477ba3d3fSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
25577ba3d3fSMatthias Ringwald                 case HCI_EVENT_MESH_META:
25677ba3d3fSMatthias Ringwald                     switch (hci_event_mesh_meta_get_subevent_code(packet)){
25777ba3d3fSMatthias Ringwald                         case MESH_SUBEVENT_PROXY_CONNECTED:
25877ba3d3fSMatthias Ringwald                             gatt_bearer_mtu = ATT_DEFAULT_MTU;
25977ba3d3fSMatthias Ringwald                             gatt_bearer_con_handle = mesh_subevent_proxy_connected_get_con_handle(packet);
26077ba3d3fSMatthias Ringwald                             gatt_bearer_emit_event_for_all(packet, size);
26177ba3d3fSMatthias Ringwald                             break;
26277ba3d3fSMatthias Ringwald                         case MESH_SUBEVENT_PROXY_DISCONNECTED:
26377ba3d3fSMatthias Ringwald                             gatt_bearer_con_handle = HCI_CON_HANDLE_INVALID;
26477ba3d3fSMatthias Ringwald                             gatt_bearer_emit_event_for_all(packet, size);
26577ba3d3fSMatthias Ringwald                             break;
26677ba3d3fSMatthias Ringwald                         case MESH_SUBEVENT_CAN_SEND_NOW:
26777ba3d3fSMatthias Ringwald                             con_handle = little_endian_read_16(packet, 3);
26877ba3d3fSMatthias Ringwald                             if (con_handle == HCI_CON_HANDLE_INVALID) return;
26977ba3d3fSMatthias Ringwald 
27077ba3d3fSMatthias Ringwald                             if (!outgoing_ready){
27177ba3d3fSMatthias Ringwald                                 gatt_bearer_emit_can_send_now();
27277ba3d3fSMatthias Ringwald                                 return;
27377ba3d3fSMatthias Ringwald                             }
27477ba3d3fSMatthias Ringwald                             gatt_bearer_start_sending(con_handle);
27577ba3d3fSMatthias Ringwald                             return;
27677ba3d3fSMatthias Ringwald                         default:
27777ba3d3fSMatthias Ringwald                             break;
27877ba3d3fSMatthias Ringwald                     }
27977ba3d3fSMatthias Ringwald             }
28077ba3d3fSMatthias Ringwald             break;
28177ba3d3fSMatthias Ringwald         default:
28277ba3d3fSMatthias Ringwald             break;
28377ba3d3fSMatthias Ringwald     }
28477ba3d3fSMatthias Ringwald }
28577ba3d3fSMatthias Ringwald 
28677ba3d3fSMatthias Ringwald void gatt_bearer_init(void){
28777ba3d3fSMatthias Ringwald     mesh_proxy_service_server_init();
28877ba3d3fSMatthias Ringwald     mesh_proxy_service_server_register_packet_handler(packet_handler);
28977ba3d3fSMatthias Ringwald }
29077ba3d3fSMatthias Ringwald 
291*4662af4aSMatthias Ringwald void gatt_bearer_register_for_network_pdu(btstack_packet_handler_t _packet_handler){
29277ba3d3fSMatthias Ringwald     client_callbacks[MESH_MSG_TYPE_NETWORK_PDU] = _packet_handler;
29377ba3d3fSMatthias Ringwald }
294*4662af4aSMatthias Ringwald void gatt_bearer_register_for_beacon(btstack_packet_handler_t _packet_handler){
29577ba3d3fSMatthias Ringwald     client_callbacks[MESH_MSG_TYPE_BEACON] = _packet_handler;
29677ba3d3fSMatthias Ringwald }
29777ba3d3fSMatthias Ringwald void gatt_bearer_register_for_mesh_proxy_configuration(btstack_packet_handler_t _packet_handler){
29877ba3d3fSMatthias Ringwald     client_callbacks[MESH_MSG_TYPE_PROXY_CONFIGURATION] = _packet_handler;
29977ba3d3fSMatthias Ringwald }
30077ba3d3fSMatthias Ringwald 
301*4662af4aSMatthias Ringwald void gatt_bearer_request_can_send_now_for_network_pdu(void){
30277ba3d3fSMatthias Ringwald     gatt_bearer_request(MESH_MSG_TYPE_NETWORK_PDU);
30377ba3d3fSMatthias Ringwald }
304*4662af4aSMatthias Ringwald void gatt_bearer_request_can_send_now_for_beacon(void){
30577ba3d3fSMatthias Ringwald     gatt_bearer_request(MESH_MSG_TYPE_BEACON);
30677ba3d3fSMatthias Ringwald }
30777ba3d3fSMatthias Ringwald void gatt_bearer_request_can_send_now_for_mesh_proxy_configuration(void){
30877ba3d3fSMatthias Ringwald     gatt_bearer_request(MESH_MSG_TYPE_PROXY_CONFIGURATION);
30977ba3d3fSMatthias Ringwald }
31077ba3d3fSMatthias Ringwald 
31177ba3d3fSMatthias Ringwald static void gatt_bearer_send_pdu(uint16_t con_handle, const uint8_t * pdu, uint16_t size){
31277ba3d3fSMatthias Ringwald     if (!pdu || size <= 0) return;
31377ba3d3fSMatthias Ringwald     if (con_handle == HCI_CON_HANDLE_INVALID) return;
31477ba3d3fSMatthias Ringwald     // store pdu, request to send
31577ba3d3fSMatthias Ringwald     proxy_pdu = pdu;
31677ba3d3fSMatthias Ringwald     proxy_pdu_size = size;
31777ba3d3fSMatthias Ringwald     segmentation_offset = 0;
31877ba3d3fSMatthias Ringwald 
31977ba3d3fSMatthias Ringwald     // check if segmentation is necessary
32077ba3d3fSMatthias Ringwald     if (proxy_pdu_size > (gatt_bearer_mtu - 1 - 3)){
32177ba3d3fSMatthias Ringwald         segmentation_state = MESH_MSG_SAR_FIELD_FIRST_SEGMENT;
32277ba3d3fSMatthias Ringwald     } else {
32377ba3d3fSMatthias Ringwald         segmentation_state = MESH_MSG_SAR_FIELD_COMPLETE_MSG;
32477ba3d3fSMatthias Ringwald     }
32577ba3d3fSMatthias Ringwald     outgoing_ready = 1;
32677ba3d3fSMatthias Ringwald     gatt_bearer_start_sending(con_handle);
32777ba3d3fSMatthias Ringwald }
32877ba3d3fSMatthias Ringwald 
329*4662af4aSMatthias Ringwald void gatt_bearer_send_network_pdu(const uint8_t * data, uint16_t data_len){
33077ba3d3fSMatthias Ringwald     msg_type = MESH_MSG_TYPE_NETWORK_PDU;
33177ba3d3fSMatthias Ringwald     gatt_bearer_send_pdu(gatt_bearer_con_handle, data, data_len);
33277ba3d3fSMatthias Ringwald }
33377ba3d3fSMatthias Ringwald 
334*4662af4aSMatthias Ringwald void gatt_bearer_send_beacon(const uint8_t * data, uint16_t data_len){
33577ba3d3fSMatthias Ringwald     msg_type = MESH_MSG_TYPE_BEACON;
33677ba3d3fSMatthias Ringwald     gatt_bearer_send_pdu(gatt_bearer_con_handle, data, data_len);
33777ba3d3fSMatthias Ringwald }
33877ba3d3fSMatthias Ringwald 
33977ba3d3fSMatthias Ringwald void gatt_bearer_send_mesh_proxy_configuration(const uint8_t * data, uint16_t data_len){
34077ba3d3fSMatthias Ringwald     msg_type = MESH_MSG_TYPE_PROXY_CONFIGURATION;
34177ba3d3fSMatthias Ringwald     gatt_bearer_send_pdu(gatt_bearer_con_handle, data, data_len);
34277ba3d3fSMatthias Ringwald }
34377ba3d3fSMatthias Ringwald 
34477ba3d3fSMatthias Ringwald uint8_t gatt_bearer_setup_advertising_with_network_id(uint8_t * buffer, uint8_t * network_id){
34577ba3d3fSMatthias Ringwald     memcpy(&buffer[0], adv_data_with_network_id_template, 12);
34677ba3d3fSMatthias Ringwald     memcpy(&buffer[12], network_id, 8);
34777ba3d3fSMatthias Ringwald     return 20;
34877ba3d3fSMatthias Ringwald }
349