xref: /btstack/src/mesh/gatt_bearer.c (revision d567aeb32398f2a708611c446bcd9bad85a152cd)
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
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH 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 
382d4000d1SMatthias Ringwald #define BTSTACK_FILE__ "gatt_bearer.c"
3977ba3d3fSMatthias Ringwald 
4077ba3d3fSMatthias Ringwald #include <string.h>
4177ba3d3fSMatthias Ringwald 
4277ba3d3fSMatthias Ringwald #include "ble/att_server.h"
4377ba3d3fSMatthias Ringwald #include "ble/core.h"
4477ba3d3fSMatthias Ringwald #include "bluetooth.h"
4577ba3d3fSMatthias Ringwald #include "bluetooth_data_types.h"
4677ba3d3fSMatthias Ringwald #include "bluetooth_gatt.h"
4777ba3d3fSMatthias Ringwald #include "btstack_debug.h"
48*d567aeb3SMatthias Ringwald #include "btstack_event.h"
49*d567aeb3SMatthias Ringwald #include "btstack_event.h"
5077ba3d3fSMatthias Ringwald #include "btstack_run_loop.h"
51*d567aeb3SMatthias Ringwald #include "btstack_util.h"
5277ba3d3fSMatthias Ringwald #include "gap.h"
53*d567aeb3SMatthias Ringwald #include "mesh/gatt-service/mesh_proxy_service_server.h"
54*d567aeb3SMatthias Ringwald #include "mesh/gatt_bearer.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 // round-robin
gatt_bearer_emit_can_send_now(void)8077ba3d3fSMatthias Ringwald static void gatt_bearer_emit_can_send_now(void){
8177ba3d3fSMatthias Ringwald     // if (gatt_active) return;
8277ba3d3fSMatthias Ringwald     int countdown = NUM_TYPES;
8377ba3d3fSMatthias Ringwald     while (countdown--) {
8477ba3d3fSMatthias Ringwald         last_sender++;
8577ba3d3fSMatthias Ringwald         if (last_sender == NUM_TYPES) {
8677ba3d3fSMatthias Ringwald             last_sender = 0;
8777ba3d3fSMatthias Ringwald         }
8877ba3d3fSMatthias Ringwald         if (request_can_send_now[last_sender]){
8977ba3d3fSMatthias Ringwald             request_can_send_now[last_sender] = 0;
9077ba3d3fSMatthias Ringwald             // emit can send now
9177ba3d3fSMatthias Ringwald             uint8_t event[5];
9277ba3d3fSMatthias Ringwald             int pos = 0;
9377ba3d3fSMatthias Ringwald             event[pos++] = HCI_EVENT_MESH_META;
9477ba3d3fSMatthias Ringwald             event[pos++] = 1;
9577ba3d3fSMatthias Ringwald             event[pos++] = MESH_SUBEVENT_CAN_SEND_NOW;
9677ba3d3fSMatthias Ringwald             little_endian_store_16(event, pos, gatt_bearer_con_handle);
9777ba3d3fSMatthias Ringwald             pos += 2;
9877ba3d3fSMatthias Ringwald             (*client_callbacks[last_sender])(HCI_EVENT_PACKET, 0, &event[0], pos);
9977ba3d3fSMatthias Ringwald             return;
10077ba3d3fSMatthias Ringwald         }
10177ba3d3fSMatthias Ringwald     }
10277ba3d3fSMatthias Ringwald }
10377ba3d3fSMatthias Ringwald 
gatt_bearer_emit_message_sent(mesh_msg_type_t type_id)10477ba3d3fSMatthias Ringwald static void gatt_bearer_emit_message_sent(mesh_msg_type_t type_id){
10577ba3d3fSMatthias Ringwald     uint8_t event[5];
10677ba3d3fSMatthias Ringwald     int pos = 0;
10777ba3d3fSMatthias Ringwald     event[pos++] = HCI_EVENT_MESH_META;
10877ba3d3fSMatthias Ringwald     event[pos++] = 1;
10977ba3d3fSMatthias Ringwald     event[pos++] = MESH_SUBEVENT_MESSAGE_SENT;
11077ba3d3fSMatthias Ringwald     little_endian_store_16(event, pos, gatt_bearer_con_handle);
11177ba3d3fSMatthias Ringwald     pos += 2;
11277ba3d3fSMatthias Ringwald     (*client_callbacks[type_id])(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
11377ba3d3fSMatthias Ringwald }
11477ba3d3fSMatthias Ringwald 
gatt_bearer_emit_event_for_all(uint8_t * packet,uint16_t size)11577ba3d3fSMatthias Ringwald static void gatt_bearer_emit_event_for_all(uint8_t * packet, uint16_t size){
11677ba3d3fSMatthias Ringwald     unsigned int i;
11777ba3d3fSMatthias Ringwald     for (i=0; i < NUM_TYPES; i++){
11877ba3d3fSMatthias Ringwald         if ( client_callbacks[i] == NULL) continue;
11977ba3d3fSMatthias Ringwald         (*client_callbacks[last_sender])(HCI_EVENT_PACKET, 0, packet, size);
12077ba3d3fSMatthias Ringwald     }
12177ba3d3fSMatthias Ringwald }
12277ba3d3fSMatthias Ringwald 
gatt_bearer_request(mesh_msg_type_t type_id)12377ba3d3fSMatthias Ringwald static void gatt_bearer_request(mesh_msg_type_t type_id){
12477ba3d3fSMatthias Ringwald     request_can_send_now[type_id] = 1;
12577ba3d3fSMatthias Ringwald     mesh_proxy_service_server_request_can_send_now(gatt_bearer_con_handle);
12677ba3d3fSMatthias Ringwald }
12777ba3d3fSMatthias Ringwald 
12877ba3d3fSMatthias Ringwald 
gatt_bearer_start_sending(hci_con_handle_t con_handle)12977ba3d3fSMatthias Ringwald static void gatt_bearer_start_sending(hci_con_handle_t con_handle){
13077ba3d3fSMatthias Ringwald     uint16_t pdu_segment_len = btstack_min(proxy_pdu_size - segmentation_offset, gatt_bearer_mtu - 1 - 3);
13177ba3d3fSMatthias Ringwald     sar_buffer.segmentation_buffer[0] = (segmentation_state << 6) | msg_type;
1326535961aSMatthias Ringwald     (void)memcpy(&sar_buffer.segmentation_buffer[1],
1336535961aSMatthias Ringwald                  &proxy_pdu[segmentation_offset], pdu_segment_len);
13477ba3d3fSMatthias Ringwald     segmentation_offset += pdu_segment_len;
13577ba3d3fSMatthias Ringwald     mesh_proxy_service_server_send_proxy_pdu(con_handle, sar_buffer.segmentation_buffer, pdu_segment_len + 1);
13677ba3d3fSMatthias Ringwald 
13777ba3d3fSMatthias Ringwald     switch (segmentation_state){
13877ba3d3fSMatthias Ringwald         case MESH_MSG_SAR_FIELD_COMPLETE_MSG:
13977ba3d3fSMatthias Ringwald         case MESH_MSG_SAR_FIELD_LAST_SEGMENT:
14077ba3d3fSMatthias Ringwald             // gatt_bearer_emit_pdu_sent(0);
14177ba3d3fSMatthias Ringwald             outgoing_ready = 0;
14277ba3d3fSMatthias Ringwald             gatt_bearer_emit_message_sent(msg_type);
14377ba3d3fSMatthias Ringwald             break;
14477ba3d3fSMatthias Ringwald         case MESH_MSG_SAR_FIELD_CONTINUE:
14577ba3d3fSMatthias Ringwald         case MESH_MSG_SAR_FIELD_FIRST_SEGMENT:
14677ba3d3fSMatthias Ringwald             if ((proxy_pdu_size - segmentation_offset) > (gatt_bearer_mtu - 1 - 3)){
14777ba3d3fSMatthias Ringwald                 segmentation_state = MESH_MSG_SAR_FIELD_CONTINUE;
14877ba3d3fSMatthias Ringwald             } else {
14977ba3d3fSMatthias Ringwald                 segmentation_state = MESH_MSG_SAR_FIELD_LAST_SEGMENT;
15077ba3d3fSMatthias Ringwald             }
15177ba3d3fSMatthias Ringwald             mesh_proxy_service_server_request_can_send_now(con_handle);
15277ba3d3fSMatthias Ringwald             break;
15377ba3d3fSMatthias Ringwald         default:
15477ba3d3fSMatthias Ringwald             break;
15577ba3d3fSMatthias Ringwald     }
15677ba3d3fSMatthias Ringwald }
15777ba3d3fSMatthias Ringwald 
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)15877ba3d3fSMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
15977ba3d3fSMatthias Ringwald     UNUSED(channel);
16077ba3d3fSMatthias Ringwald     UNUSED(size);
16177ba3d3fSMatthias Ringwald     mesh_msg_sar_field_t msg_sar_field;
16277ba3d3fSMatthias Ringwald 
1632983fbcbSMatthias Ringwald     uint16_t pdu_segment_len;
1642983fbcbSMatthias Ringwald     uint16_t pos;
16577ba3d3fSMatthias Ringwald     hci_con_handle_t con_handle;
16677ba3d3fSMatthias Ringwald     int send_to_mesh_network;
16777ba3d3fSMatthias Ringwald 
16877ba3d3fSMatthias Ringwald     switch (packet_type) {
16977ba3d3fSMatthias Ringwald         case MESH_PROXY_DATA_PACKET:
17077ba3d3fSMatthias Ringwald             pos = 0;
17177ba3d3fSMatthias Ringwald             // on provisioning PDU call packet handler with PROVISIONG_DATA type
17277ba3d3fSMatthias Ringwald             msg_sar_field = packet[pos] >> 6;
17377ba3d3fSMatthias Ringwald             msg_type = packet[pos] & 0x3F;
17477ba3d3fSMatthias Ringwald             pos++;
17577ba3d3fSMatthias Ringwald 
17677ba3d3fSMatthias Ringwald             switch (msg_type){
17777ba3d3fSMatthias Ringwald                 case MESH_MSG_TYPE_NETWORK_PDU:
17877ba3d3fSMatthias Ringwald                 case MESH_MSG_TYPE_BEACON:
17977ba3d3fSMatthias Ringwald                 case MESH_MSG_TYPE_PROXY_CONFIGURATION:
18077ba3d3fSMatthias Ringwald                     if (!client_callbacks[msg_type]){
18177ba3d3fSMatthias Ringwald                         log_error("client callback not defined");
18277ba3d3fSMatthias Ringwald                         return;
18377ba3d3fSMatthias Ringwald                     }
18477ba3d3fSMatthias Ringwald                     break;
18577ba3d3fSMatthias Ringwald                 default:
18677ba3d3fSMatthias Ringwald                     log_info("gatt bearer: message type %d not supported", msg_type);
18777ba3d3fSMatthias Ringwald                     return;
18877ba3d3fSMatthias Ringwald             }
18977ba3d3fSMatthias Ringwald             pdu_segment_len = size - pos;
19077ba3d3fSMatthias Ringwald 
19177ba3d3fSMatthias Ringwald             if (sizeof(sar_buffer.reassembly_buffer) - reassembly_offset < pdu_segment_len) {
19277ba3d3fSMatthias Ringwald                 log_error("sar buffer too small left %d, new to store %d", MESH_PROV_MAX_PROXY_PDU - reassembly_offset, pdu_segment_len);
19377ba3d3fSMatthias Ringwald                 break;
19477ba3d3fSMatthias Ringwald             }
19577ba3d3fSMatthias Ringwald 
19677ba3d3fSMatthias Ringwald             // update mtu if incoming packet is larger than default
19777ba3d3fSMatthias Ringwald             if (size > (ATT_DEFAULT_MTU - 1)){
19877ba3d3fSMatthias Ringwald                 log_info("Remote uses larger MTU, enable long PDUs");
19977ba3d3fSMatthias Ringwald                 gatt_bearer_mtu = att_server_get_mtu(channel);
20077ba3d3fSMatthias Ringwald             }
20177ba3d3fSMatthias Ringwald 
20277ba3d3fSMatthias Ringwald             switch (msg_sar_field){
20377ba3d3fSMatthias Ringwald                 case MESH_MSG_SAR_FIELD_COMPLETE_MSG:
20477ba3d3fSMatthias Ringwald                 case MESH_MSG_SAR_FIELD_FIRST_SEGMENT:
20577ba3d3fSMatthias Ringwald                     memset(sar_buffer.reassembly_buffer, 0, sizeof(sar_buffer.reassembly_buffer));
20677ba3d3fSMatthias Ringwald                     if (sizeof(sar_buffer.reassembly_buffer) < pdu_segment_len) return;
2076535961aSMatthias Ringwald                     (void)memcpy(sar_buffer.reassembly_buffer, packet + pos,
2086535961aSMatthias Ringwald                                  pdu_segment_len);
20977ba3d3fSMatthias Ringwald                     reassembly_offset = pdu_segment_len;
21077ba3d3fSMatthias Ringwald                     break;
21177ba3d3fSMatthias Ringwald                 case MESH_MSG_SAR_FIELD_CONTINUE:
21277ba3d3fSMatthias Ringwald                     if ((sizeof(sar_buffer.reassembly_buffer) - reassembly_offset) < pdu_segment_len) return;
2136535961aSMatthias Ringwald                     (void)memcpy(sar_buffer.reassembly_buffer + reassembly_offset,
2146535961aSMatthias Ringwald                                  packet + pos, pdu_segment_len);
21577ba3d3fSMatthias Ringwald                     reassembly_offset += pdu_segment_len;
21677ba3d3fSMatthias Ringwald                     return;
21777ba3d3fSMatthias Ringwald                 case MESH_MSG_SAR_FIELD_LAST_SEGMENT:
21877ba3d3fSMatthias Ringwald                     if ((sizeof(sar_buffer.reassembly_buffer) - reassembly_offset) < pdu_segment_len) return;
2196535961aSMatthias Ringwald                     (void)memcpy(sar_buffer.reassembly_buffer + reassembly_offset,
2206535961aSMatthias Ringwald                                  packet + pos, pdu_segment_len);
22177ba3d3fSMatthias Ringwald                     reassembly_offset += pdu_segment_len;
22277ba3d3fSMatthias Ringwald                     break;
22377ba3d3fSMatthias Ringwald                 default:
22477ba3d3fSMatthias Ringwald                     break;
22577ba3d3fSMatthias Ringwald             }
22677ba3d3fSMatthias Ringwald 
22777ba3d3fSMatthias Ringwald             send_to_mesh_network = (msg_sar_field == MESH_MSG_SAR_FIELD_COMPLETE_MSG) || (msg_sar_field == MESH_MSG_SAR_FIELD_LAST_SEGMENT);
22877ba3d3fSMatthias Ringwald 
22977ba3d3fSMatthias Ringwald             if (!send_to_mesh_network) break;
23077ba3d3fSMatthias Ringwald             switch (msg_type){
23177ba3d3fSMatthias Ringwald                 case MESH_MSG_TYPE_NETWORK_PDU:
23277ba3d3fSMatthias Ringwald                 case MESH_MSG_TYPE_BEACON:
23377ba3d3fSMatthias Ringwald                 case MESH_MSG_TYPE_PROXY_CONFIGURATION:
23477ba3d3fSMatthias Ringwald                     if ((*client_callbacks[msg_type])){
23577ba3d3fSMatthias Ringwald                         (*client_callbacks[msg_type])(MESH_PROXY_DATA_PACKET, 0, sar_buffer.reassembly_buffer, reassembly_offset);
23677ba3d3fSMatthias Ringwald                     }
23777ba3d3fSMatthias Ringwald                     reassembly_offset = 0;
23877ba3d3fSMatthias Ringwald                     break;
23977ba3d3fSMatthias Ringwald                 default:
24077ba3d3fSMatthias Ringwald                     log_info("gatt bearer: message type %d not supported", msg_type);
24177ba3d3fSMatthias Ringwald                     return;
24277ba3d3fSMatthias Ringwald             }
24377ba3d3fSMatthias Ringwald 
24477ba3d3fSMatthias Ringwald             break;
24577ba3d3fSMatthias Ringwald 
24677ba3d3fSMatthias Ringwald         case HCI_EVENT_PACKET:
24777ba3d3fSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
24877ba3d3fSMatthias Ringwald                 case HCI_EVENT_MESH_META:
24977ba3d3fSMatthias Ringwald                     switch (hci_event_mesh_meta_get_subevent_code(packet)){
25077ba3d3fSMatthias Ringwald                         case MESH_SUBEVENT_PROXY_CONNECTED:
25177ba3d3fSMatthias Ringwald                             gatt_bearer_mtu = ATT_DEFAULT_MTU;
25277ba3d3fSMatthias Ringwald                             gatt_bearer_con_handle = mesh_subevent_proxy_connected_get_con_handle(packet);
25377ba3d3fSMatthias Ringwald                             gatt_bearer_emit_event_for_all(packet, size);
25477ba3d3fSMatthias Ringwald                             break;
25577ba3d3fSMatthias Ringwald                         case MESH_SUBEVENT_PROXY_DISCONNECTED:
25677ba3d3fSMatthias Ringwald                             gatt_bearer_con_handle = HCI_CON_HANDLE_INVALID;
25777ba3d3fSMatthias Ringwald                             gatt_bearer_emit_event_for_all(packet, size);
25877ba3d3fSMatthias Ringwald                             break;
25977ba3d3fSMatthias Ringwald                         case MESH_SUBEVENT_CAN_SEND_NOW:
26077ba3d3fSMatthias Ringwald                             con_handle = little_endian_read_16(packet, 3);
26177ba3d3fSMatthias Ringwald                             if (con_handle == HCI_CON_HANDLE_INVALID) return;
26277ba3d3fSMatthias Ringwald 
26377ba3d3fSMatthias Ringwald                             if (!outgoing_ready){
26477ba3d3fSMatthias Ringwald                                 gatt_bearer_emit_can_send_now();
26577ba3d3fSMatthias Ringwald                                 return;
26677ba3d3fSMatthias Ringwald                             }
26777ba3d3fSMatthias Ringwald                             gatt_bearer_start_sending(con_handle);
26877ba3d3fSMatthias Ringwald                             return;
26977ba3d3fSMatthias Ringwald                         default:
27077ba3d3fSMatthias Ringwald                             break;
27177ba3d3fSMatthias Ringwald                     }
2727bbeb3adSMilanka Ringwald                     break;
2737bbeb3adSMilanka Ringwald 
2747bbeb3adSMilanka Ringwald                 default:
2757bbeb3adSMilanka Ringwald                     break;
27677ba3d3fSMatthias Ringwald             }
27777ba3d3fSMatthias Ringwald             break;
27877ba3d3fSMatthias Ringwald         default:
27977ba3d3fSMatthias Ringwald             break;
28077ba3d3fSMatthias Ringwald     }
28177ba3d3fSMatthias Ringwald }
28277ba3d3fSMatthias Ringwald 
gatt_bearer_init(void)28377ba3d3fSMatthias Ringwald void gatt_bearer_init(void){
28477ba3d3fSMatthias Ringwald     mesh_proxy_service_server_init();
28577ba3d3fSMatthias Ringwald     mesh_proxy_service_server_register_packet_handler(packet_handler);
28677ba3d3fSMatthias Ringwald }
28777ba3d3fSMatthias Ringwald 
gatt_bearer_register_for_network_pdu(btstack_packet_handler_t _packet_handler)2884662af4aSMatthias Ringwald void gatt_bearer_register_for_network_pdu(btstack_packet_handler_t _packet_handler){
28977ba3d3fSMatthias Ringwald     client_callbacks[MESH_MSG_TYPE_NETWORK_PDU] = _packet_handler;
29077ba3d3fSMatthias Ringwald }
gatt_bearer_register_for_beacon(btstack_packet_handler_t _packet_handler)2914662af4aSMatthias Ringwald void gatt_bearer_register_for_beacon(btstack_packet_handler_t _packet_handler){
29277ba3d3fSMatthias Ringwald     client_callbacks[MESH_MSG_TYPE_BEACON] = _packet_handler;
29377ba3d3fSMatthias Ringwald }
gatt_bearer_register_for_mesh_proxy_configuration(btstack_packet_handler_t _packet_handler)29477ba3d3fSMatthias Ringwald void gatt_bearer_register_for_mesh_proxy_configuration(btstack_packet_handler_t _packet_handler){
29577ba3d3fSMatthias Ringwald     client_callbacks[MESH_MSG_TYPE_PROXY_CONFIGURATION] = _packet_handler;
29677ba3d3fSMatthias Ringwald }
29777ba3d3fSMatthias Ringwald 
gatt_bearer_request_can_send_now_for_network_pdu(void)2984662af4aSMatthias Ringwald void gatt_bearer_request_can_send_now_for_network_pdu(void){
29977ba3d3fSMatthias Ringwald     gatt_bearer_request(MESH_MSG_TYPE_NETWORK_PDU);
30077ba3d3fSMatthias Ringwald }
gatt_bearer_request_can_send_now_for_beacon(void)3014662af4aSMatthias Ringwald void gatt_bearer_request_can_send_now_for_beacon(void){
30277ba3d3fSMatthias Ringwald     gatt_bearer_request(MESH_MSG_TYPE_BEACON);
30377ba3d3fSMatthias Ringwald }
gatt_bearer_request_can_send_now_for_mesh_proxy_configuration(void)30477ba3d3fSMatthias Ringwald void gatt_bearer_request_can_send_now_for_mesh_proxy_configuration(void){
30577ba3d3fSMatthias Ringwald     gatt_bearer_request(MESH_MSG_TYPE_PROXY_CONFIGURATION);
30677ba3d3fSMatthias Ringwald }
30777ba3d3fSMatthias Ringwald 
gatt_bearer_send_pdu(uint16_t con_handle,const uint8_t * pdu,uint16_t size)30877ba3d3fSMatthias Ringwald static void gatt_bearer_send_pdu(uint16_t con_handle, const uint8_t * pdu, uint16_t size){
30977ba3d3fSMatthias Ringwald     if (!pdu || size <= 0) return;
31077ba3d3fSMatthias Ringwald     if (con_handle == HCI_CON_HANDLE_INVALID) return;
31177ba3d3fSMatthias Ringwald     // store pdu, request to send
31277ba3d3fSMatthias Ringwald     proxy_pdu = pdu;
31377ba3d3fSMatthias Ringwald     proxy_pdu_size = size;
31477ba3d3fSMatthias Ringwald     segmentation_offset = 0;
31577ba3d3fSMatthias Ringwald 
31677ba3d3fSMatthias Ringwald     // check if segmentation is necessary
31777ba3d3fSMatthias Ringwald     if (proxy_pdu_size > (gatt_bearer_mtu - 1 - 3)){
31877ba3d3fSMatthias Ringwald         segmentation_state = MESH_MSG_SAR_FIELD_FIRST_SEGMENT;
31977ba3d3fSMatthias Ringwald     } else {
32077ba3d3fSMatthias Ringwald         segmentation_state = MESH_MSG_SAR_FIELD_COMPLETE_MSG;
32177ba3d3fSMatthias Ringwald     }
32277ba3d3fSMatthias Ringwald     outgoing_ready = 1;
32377ba3d3fSMatthias Ringwald     gatt_bearer_start_sending(con_handle);
32477ba3d3fSMatthias Ringwald }
32577ba3d3fSMatthias Ringwald 
gatt_bearer_send_network_pdu(const uint8_t * data,uint16_t data_len)3264662af4aSMatthias Ringwald void gatt_bearer_send_network_pdu(const uint8_t * data, uint16_t data_len){
32777ba3d3fSMatthias Ringwald     msg_type = MESH_MSG_TYPE_NETWORK_PDU;
32877ba3d3fSMatthias Ringwald     gatt_bearer_send_pdu(gatt_bearer_con_handle, data, data_len);
32977ba3d3fSMatthias Ringwald }
33077ba3d3fSMatthias Ringwald 
gatt_bearer_send_beacon(const uint8_t * data,uint16_t data_len)3314662af4aSMatthias Ringwald void gatt_bearer_send_beacon(const uint8_t * data, uint16_t data_len){
33277ba3d3fSMatthias Ringwald     msg_type = MESH_MSG_TYPE_BEACON;
33377ba3d3fSMatthias Ringwald     gatt_bearer_send_pdu(gatt_bearer_con_handle, data, data_len);
33477ba3d3fSMatthias Ringwald }
33577ba3d3fSMatthias Ringwald 
gatt_bearer_send_mesh_proxy_configuration(const uint8_t * data,uint16_t data_len)33677ba3d3fSMatthias Ringwald void gatt_bearer_send_mesh_proxy_configuration(const uint8_t * data, uint16_t data_len){
33777ba3d3fSMatthias Ringwald     msg_type = MESH_MSG_TYPE_PROXY_CONFIGURATION;
33877ba3d3fSMatthias Ringwald     gatt_bearer_send_pdu(gatt_bearer_con_handle, data, data_len);
33977ba3d3fSMatthias Ringwald }
34077ba3d3fSMatthias Ringwald 
341