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 38*2d4000d1SMatthias 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 // round-robin 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 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 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 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 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; 13277ba3d3fSMatthias Ringwald memcpy(&sar_buffer.segmentation_buffer[1], &proxy_pdu[segmentation_offset], pdu_segment_len); 13377ba3d3fSMatthias Ringwald segmentation_offset += pdu_segment_len; 13477ba3d3fSMatthias Ringwald mesh_proxy_service_server_send_proxy_pdu(con_handle, sar_buffer.segmentation_buffer, pdu_segment_len + 1); 13577ba3d3fSMatthias Ringwald 13677ba3d3fSMatthias Ringwald switch (segmentation_state){ 13777ba3d3fSMatthias Ringwald case MESH_MSG_SAR_FIELD_COMPLETE_MSG: 13877ba3d3fSMatthias Ringwald case MESH_MSG_SAR_FIELD_LAST_SEGMENT: 13977ba3d3fSMatthias Ringwald // gatt_bearer_emit_pdu_sent(0); 14077ba3d3fSMatthias Ringwald outgoing_ready = 0; 14177ba3d3fSMatthias Ringwald gatt_bearer_emit_message_sent(msg_type); 14277ba3d3fSMatthias Ringwald break; 14377ba3d3fSMatthias Ringwald case MESH_MSG_SAR_FIELD_CONTINUE: 14477ba3d3fSMatthias Ringwald case MESH_MSG_SAR_FIELD_FIRST_SEGMENT: 14577ba3d3fSMatthias Ringwald if ((proxy_pdu_size - segmentation_offset) > (gatt_bearer_mtu - 1 - 3)){ 14677ba3d3fSMatthias Ringwald segmentation_state = MESH_MSG_SAR_FIELD_CONTINUE; 14777ba3d3fSMatthias Ringwald } else { 14877ba3d3fSMatthias Ringwald segmentation_state = MESH_MSG_SAR_FIELD_LAST_SEGMENT; 14977ba3d3fSMatthias Ringwald } 15077ba3d3fSMatthias Ringwald mesh_proxy_service_server_request_can_send_now(con_handle); 15177ba3d3fSMatthias Ringwald break; 15277ba3d3fSMatthias Ringwald default: 15377ba3d3fSMatthias Ringwald break; 15477ba3d3fSMatthias Ringwald } 15577ba3d3fSMatthias Ringwald } 15677ba3d3fSMatthias Ringwald 15777ba3d3fSMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 15877ba3d3fSMatthias Ringwald UNUSED(channel); 15977ba3d3fSMatthias Ringwald UNUSED(size); 16077ba3d3fSMatthias Ringwald mesh_msg_sar_field_t msg_sar_field; 16177ba3d3fSMatthias Ringwald 1622983fbcbSMatthias Ringwald uint16_t pdu_segment_len; 1632983fbcbSMatthias Ringwald uint16_t pos; 16477ba3d3fSMatthias Ringwald hci_con_handle_t con_handle; 16577ba3d3fSMatthias Ringwald int send_to_mesh_network; 16677ba3d3fSMatthias Ringwald 16777ba3d3fSMatthias Ringwald switch (packet_type) { 16877ba3d3fSMatthias Ringwald case MESH_PROXY_DATA_PACKET: 16977ba3d3fSMatthias Ringwald pos = 0; 17077ba3d3fSMatthias Ringwald // on provisioning PDU call packet handler with PROVISIONG_DATA type 17177ba3d3fSMatthias Ringwald msg_sar_field = packet[pos] >> 6; 17277ba3d3fSMatthias Ringwald msg_type = packet[pos] & 0x3F; 17377ba3d3fSMatthias Ringwald pos++; 17477ba3d3fSMatthias Ringwald 17577ba3d3fSMatthias Ringwald switch (msg_type){ 17677ba3d3fSMatthias Ringwald case MESH_MSG_TYPE_NETWORK_PDU: 17777ba3d3fSMatthias Ringwald case MESH_MSG_TYPE_BEACON: 17877ba3d3fSMatthias Ringwald case MESH_MSG_TYPE_PROXY_CONFIGURATION: 17977ba3d3fSMatthias Ringwald if (!client_callbacks[msg_type]){ 18077ba3d3fSMatthias Ringwald log_error("client callback not defined"); 18177ba3d3fSMatthias Ringwald return; 18277ba3d3fSMatthias Ringwald } 18377ba3d3fSMatthias Ringwald break; 18477ba3d3fSMatthias Ringwald default: 18577ba3d3fSMatthias Ringwald log_info("gatt bearer: message type %d not supported", msg_type); 18677ba3d3fSMatthias Ringwald return; 18777ba3d3fSMatthias Ringwald } 18877ba3d3fSMatthias Ringwald pdu_segment_len = size - pos; 18977ba3d3fSMatthias Ringwald 19077ba3d3fSMatthias Ringwald if (sizeof(sar_buffer.reassembly_buffer) - reassembly_offset < pdu_segment_len) { 19177ba3d3fSMatthias Ringwald log_error("sar buffer too small left %d, new to store %d", MESH_PROV_MAX_PROXY_PDU - reassembly_offset, pdu_segment_len); 19277ba3d3fSMatthias Ringwald break; 19377ba3d3fSMatthias Ringwald } 19477ba3d3fSMatthias Ringwald 19577ba3d3fSMatthias Ringwald // update mtu if incoming packet is larger than default 19677ba3d3fSMatthias Ringwald if (size > (ATT_DEFAULT_MTU - 1)){ 19777ba3d3fSMatthias Ringwald log_info("Remote uses larger MTU, enable long PDUs"); 19877ba3d3fSMatthias Ringwald gatt_bearer_mtu = att_server_get_mtu(channel); 19977ba3d3fSMatthias Ringwald } 20077ba3d3fSMatthias Ringwald 20177ba3d3fSMatthias Ringwald switch (msg_sar_field){ 20277ba3d3fSMatthias Ringwald case MESH_MSG_SAR_FIELD_COMPLETE_MSG: 20377ba3d3fSMatthias Ringwald case MESH_MSG_SAR_FIELD_FIRST_SEGMENT: 20477ba3d3fSMatthias Ringwald memset(sar_buffer.reassembly_buffer, 0, sizeof(sar_buffer.reassembly_buffer)); 20577ba3d3fSMatthias Ringwald if (sizeof(sar_buffer.reassembly_buffer) < pdu_segment_len) return; 20677ba3d3fSMatthias Ringwald memcpy(sar_buffer.reassembly_buffer, packet+pos, pdu_segment_len); 20777ba3d3fSMatthias Ringwald reassembly_offset = pdu_segment_len; 20877ba3d3fSMatthias Ringwald break; 20977ba3d3fSMatthias Ringwald case MESH_MSG_SAR_FIELD_CONTINUE: 21077ba3d3fSMatthias Ringwald if ((sizeof(sar_buffer.reassembly_buffer) - reassembly_offset) < pdu_segment_len) return; 21177ba3d3fSMatthias Ringwald memcpy(sar_buffer.reassembly_buffer + reassembly_offset, packet+pos, pdu_segment_len); 21277ba3d3fSMatthias Ringwald reassembly_offset += pdu_segment_len; 21377ba3d3fSMatthias Ringwald return; 21477ba3d3fSMatthias Ringwald case MESH_MSG_SAR_FIELD_LAST_SEGMENT: 21577ba3d3fSMatthias Ringwald if ((sizeof(sar_buffer.reassembly_buffer) - reassembly_offset) < pdu_segment_len) return; 21677ba3d3fSMatthias Ringwald memcpy(sar_buffer.reassembly_buffer + reassembly_offset, packet+pos, pdu_segment_len); 21777ba3d3fSMatthias Ringwald reassembly_offset += pdu_segment_len; 21877ba3d3fSMatthias Ringwald break; 21977ba3d3fSMatthias Ringwald default: 22077ba3d3fSMatthias Ringwald break; 22177ba3d3fSMatthias Ringwald } 22277ba3d3fSMatthias Ringwald 22377ba3d3fSMatthias Ringwald send_to_mesh_network = (msg_sar_field == MESH_MSG_SAR_FIELD_COMPLETE_MSG) || (msg_sar_field == MESH_MSG_SAR_FIELD_LAST_SEGMENT); 22477ba3d3fSMatthias Ringwald 22577ba3d3fSMatthias Ringwald if (!send_to_mesh_network) break; 22677ba3d3fSMatthias Ringwald switch (msg_type){ 22777ba3d3fSMatthias Ringwald case MESH_MSG_TYPE_NETWORK_PDU: 22877ba3d3fSMatthias Ringwald case MESH_MSG_TYPE_BEACON: 22977ba3d3fSMatthias Ringwald case MESH_MSG_TYPE_PROXY_CONFIGURATION: 23077ba3d3fSMatthias Ringwald if ((*client_callbacks[msg_type])){ 23177ba3d3fSMatthias Ringwald (*client_callbacks[msg_type])(MESH_PROXY_DATA_PACKET, 0, sar_buffer.reassembly_buffer, reassembly_offset); 23277ba3d3fSMatthias Ringwald } 23377ba3d3fSMatthias Ringwald reassembly_offset = 0; 23477ba3d3fSMatthias Ringwald break; 23577ba3d3fSMatthias Ringwald default: 23677ba3d3fSMatthias Ringwald log_info("gatt bearer: message type %d not supported", msg_type); 23777ba3d3fSMatthias Ringwald return; 23877ba3d3fSMatthias Ringwald } 23977ba3d3fSMatthias Ringwald 24077ba3d3fSMatthias Ringwald break; 24177ba3d3fSMatthias Ringwald 24277ba3d3fSMatthias Ringwald case HCI_EVENT_PACKET: 24377ba3d3fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 24477ba3d3fSMatthias Ringwald case HCI_EVENT_MESH_META: 24577ba3d3fSMatthias Ringwald switch (hci_event_mesh_meta_get_subevent_code(packet)){ 24677ba3d3fSMatthias Ringwald case MESH_SUBEVENT_PROXY_CONNECTED: 24777ba3d3fSMatthias Ringwald gatt_bearer_mtu = ATT_DEFAULT_MTU; 24877ba3d3fSMatthias Ringwald gatt_bearer_con_handle = mesh_subevent_proxy_connected_get_con_handle(packet); 24977ba3d3fSMatthias Ringwald gatt_bearer_emit_event_for_all(packet, size); 25077ba3d3fSMatthias Ringwald break; 25177ba3d3fSMatthias Ringwald case MESH_SUBEVENT_PROXY_DISCONNECTED: 25277ba3d3fSMatthias Ringwald gatt_bearer_con_handle = HCI_CON_HANDLE_INVALID; 25377ba3d3fSMatthias Ringwald gatt_bearer_emit_event_for_all(packet, size); 25477ba3d3fSMatthias Ringwald break; 25577ba3d3fSMatthias Ringwald case MESH_SUBEVENT_CAN_SEND_NOW: 25677ba3d3fSMatthias Ringwald con_handle = little_endian_read_16(packet, 3); 25777ba3d3fSMatthias Ringwald if (con_handle == HCI_CON_HANDLE_INVALID) return; 25877ba3d3fSMatthias Ringwald 25977ba3d3fSMatthias Ringwald if (!outgoing_ready){ 26077ba3d3fSMatthias Ringwald gatt_bearer_emit_can_send_now(); 26177ba3d3fSMatthias Ringwald return; 26277ba3d3fSMatthias Ringwald } 26377ba3d3fSMatthias Ringwald gatt_bearer_start_sending(con_handle); 26477ba3d3fSMatthias Ringwald return; 26577ba3d3fSMatthias Ringwald default: 26677ba3d3fSMatthias Ringwald break; 26777ba3d3fSMatthias Ringwald } 26877ba3d3fSMatthias Ringwald } 26977ba3d3fSMatthias Ringwald break; 27077ba3d3fSMatthias Ringwald default: 27177ba3d3fSMatthias Ringwald break; 27277ba3d3fSMatthias Ringwald } 27377ba3d3fSMatthias Ringwald } 27477ba3d3fSMatthias Ringwald 27577ba3d3fSMatthias Ringwald void gatt_bearer_init(void){ 27677ba3d3fSMatthias Ringwald mesh_proxy_service_server_init(); 27777ba3d3fSMatthias Ringwald mesh_proxy_service_server_register_packet_handler(packet_handler); 27877ba3d3fSMatthias Ringwald } 27977ba3d3fSMatthias Ringwald 2804662af4aSMatthias Ringwald void gatt_bearer_register_for_network_pdu(btstack_packet_handler_t _packet_handler){ 28177ba3d3fSMatthias Ringwald client_callbacks[MESH_MSG_TYPE_NETWORK_PDU] = _packet_handler; 28277ba3d3fSMatthias Ringwald } 2834662af4aSMatthias Ringwald void gatt_bearer_register_for_beacon(btstack_packet_handler_t _packet_handler){ 28477ba3d3fSMatthias Ringwald client_callbacks[MESH_MSG_TYPE_BEACON] = _packet_handler; 28577ba3d3fSMatthias Ringwald } 28677ba3d3fSMatthias Ringwald void gatt_bearer_register_for_mesh_proxy_configuration(btstack_packet_handler_t _packet_handler){ 28777ba3d3fSMatthias Ringwald client_callbacks[MESH_MSG_TYPE_PROXY_CONFIGURATION] = _packet_handler; 28877ba3d3fSMatthias Ringwald } 28977ba3d3fSMatthias Ringwald 2904662af4aSMatthias Ringwald void gatt_bearer_request_can_send_now_for_network_pdu(void){ 29177ba3d3fSMatthias Ringwald gatt_bearer_request(MESH_MSG_TYPE_NETWORK_PDU); 29277ba3d3fSMatthias Ringwald } 2934662af4aSMatthias Ringwald void gatt_bearer_request_can_send_now_for_beacon(void){ 29477ba3d3fSMatthias Ringwald gatt_bearer_request(MESH_MSG_TYPE_BEACON); 29577ba3d3fSMatthias Ringwald } 29677ba3d3fSMatthias Ringwald void gatt_bearer_request_can_send_now_for_mesh_proxy_configuration(void){ 29777ba3d3fSMatthias Ringwald gatt_bearer_request(MESH_MSG_TYPE_PROXY_CONFIGURATION); 29877ba3d3fSMatthias Ringwald } 29977ba3d3fSMatthias Ringwald 30077ba3d3fSMatthias Ringwald static void gatt_bearer_send_pdu(uint16_t con_handle, const uint8_t * pdu, uint16_t size){ 30177ba3d3fSMatthias Ringwald if (!pdu || size <= 0) return; 30277ba3d3fSMatthias Ringwald if (con_handle == HCI_CON_HANDLE_INVALID) return; 30377ba3d3fSMatthias Ringwald // store pdu, request to send 30477ba3d3fSMatthias Ringwald proxy_pdu = pdu; 30577ba3d3fSMatthias Ringwald proxy_pdu_size = size; 30677ba3d3fSMatthias Ringwald segmentation_offset = 0; 30777ba3d3fSMatthias Ringwald 30877ba3d3fSMatthias Ringwald // check if segmentation is necessary 30977ba3d3fSMatthias Ringwald if (proxy_pdu_size > (gatt_bearer_mtu - 1 - 3)){ 31077ba3d3fSMatthias Ringwald segmentation_state = MESH_MSG_SAR_FIELD_FIRST_SEGMENT; 31177ba3d3fSMatthias Ringwald } else { 31277ba3d3fSMatthias Ringwald segmentation_state = MESH_MSG_SAR_FIELD_COMPLETE_MSG; 31377ba3d3fSMatthias Ringwald } 31477ba3d3fSMatthias Ringwald outgoing_ready = 1; 31577ba3d3fSMatthias Ringwald gatt_bearer_start_sending(con_handle); 31677ba3d3fSMatthias Ringwald } 31777ba3d3fSMatthias Ringwald 3184662af4aSMatthias Ringwald void gatt_bearer_send_network_pdu(const uint8_t * data, uint16_t data_len){ 31977ba3d3fSMatthias Ringwald msg_type = MESH_MSG_TYPE_NETWORK_PDU; 32077ba3d3fSMatthias Ringwald gatt_bearer_send_pdu(gatt_bearer_con_handle, data, data_len); 32177ba3d3fSMatthias Ringwald } 32277ba3d3fSMatthias Ringwald 3234662af4aSMatthias Ringwald void gatt_bearer_send_beacon(const uint8_t * data, uint16_t data_len){ 32477ba3d3fSMatthias Ringwald msg_type = MESH_MSG_TYPE_BEACON; 32577ba3d3fSMatthias Ringwald gatt_bearer_send_pdu(gatt_bearer_con_handle, data, data_len); 32677ba3d3fSMatthias Ringwald } 32777ba3d3fSMatthias Ringwald 32877ba3d3fSMatthias Ringwald void gatt_bearer_send_mesh_proxy_configuration(const uint8_t * data, uint16_t data_len){ 32977ba3d3fSMatthias Ringwald msg_type = MESH_MSG_TYPE_PROXY_CONFIGURATION; 33077ba3d3fSMatthias Ringwald gatt_bearer_send_pdu(gatt_bearer_con_handle, data, data_len); 33177ba3d3fSMatthias Ringwald } 33277ba3d3fSMatthias Ringwald 333