xref: /btstack/src/mesh/gatt_bearer.c (revision 77ba3d3f9fd2c90e975cda31e3c706784e95d43a)
1*77ba3d3fSMatthias Ringwald /*
2*77ba3d3fSMatthias Ringwald  * Copyright (C) 2017 BlueKitchen GmbH
3*77ba3d3fSMatthias Ringwald  *
4*77ba3d3fSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5*77ba3d3fSMatthias Ringwald  * modification, are permitted provided that the following conditions
6*77ba3d3fSMatthias Ringwald  * are met:
7*77ba3d3fSMatthias Ringwald  *
8*77ba3d3fSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9*77ba3d3fSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10*77ba3d3fSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11*77ba3d3fSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12*77ba3d3fSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13*77ba3d3fSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14*77ba3d3fSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15*77ba3d3fSMatthias Ringwald  *    from this software without specific prior written permission.
16*77ba3d3fSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17*77ba3d3fSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18*77ba3d3fSMatthias Ringwald  *    monetary gain.
19*77ba3d3fSMatthias Ringwald  *
20*77ba3d3fSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21*77ba3d3fSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*77ba3d3fSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*77ba3d3fSMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24*77ba3d3fSMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25*77ba3d3fSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26*77ba3d3fSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27*77ba3d3fSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28*77ba3d3fSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29*77ba3d3fSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30*77ba3d3fSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*77ba3d3fSMatthias Ringwald  * SUCH DAMAGE.
32*77ba3d3fSMatthias Ringwald  *
33*77ba3d3fSMatthias Ringwald  * Please inquire about commercial licensing options at
34*77ba3d3fSMatthias Ringwald  * [email protected]
35*77ba3d3fSMatthias Ringwald  *
36*77ba3d3fSMatthias Ringwald  */
37*77ba3d3fSMatthias Ringwald 
38*77ba3d3fSMatthias Ringwald #define __BTSTACK_FILE__ "gatt_bearer.c"
39*77ba3d3fSMatthias Ringwald 
40*77ba3d3fSMatthias Ringwald #include <string.h>
41*77ba3d3fSMatthias Ringwald 
42*77ba3d3fSMatthias Ringwald #include "ble/gatt-service/mesh_proxy_service_server.h"
43*77ba3d3fSMatthias Ringwald #include "ble/att_server.h"
44*77ba3d3fSMatthias Ringwald #include "mesh/gatt_bearer.h"
45*77ba3d3fSMatthias Ringwald #include "ble/core.h"
46*77ba3d3fSMatthias Ringwald #include "bluetooth.h"
47*77ba3d3fSMatthias Ringwald #include "bluetooth_data_types.h"
48*77ba3d3fSMatthias Ringwald #include "bluetooth_gatt.h"
49*77ba3d3fSMatthias Ringwald #include "btstack_debug.h"
50*77ba3d3fSMatthias Ringwald #include "btstack_util.h"
51*77ba3d3fSMatthias Ringwald #include "btstack_run_loop.h"
52*77ba3d3fSMatthias Ringwald #include "btstack_event.h"
53*77ba3d3fSMatthias Ringwald #include "gap.h"
54*77ba3d3fSMatthias Ringwald #include "btstack_event.h"
55*77ba3d3fSMatthias Ringwald #include "provisioning.h"
56*77ba3d3fSMatthias Ringwald 
57*77ba3d3fSMatthias Ringwald #define NUM_TYPES 3
58*77ba3d3fSMatthias Ringwald 
59*77ba3d3fSMatthias Ringwald static btstack_packet_handler_t client_callbacks[NUM_TYPES];
60*77ba3d3fSMatthias Ringwald static int request_can_send_now[NUM_TYPES];
61*77ba3d3fSMatthias Ringwald static int last_sender;
62*77ba3d3fSMatthias Ringwald 
63*77ba3d3fSMatthias Ringwald // share buffer for reassembly and segmentation - protocol is half-duplex
64*77ba3d3fSMatthias Ringwald static union {
65*77ba3d3fSMatthias Ringwald     uint8_t  reassembly_buffer[MESH_PROV_MAX_PROXY_PDU];
66*77ba3d3fSMatthias Ringwald     uint8_t  segmentation_buffer[MESH_PROV_MAX_PROXY_PDU];
67*77ba3d3fSMatthias Ringwald } sar_buffer;
68*77ba3d3fSMatthias Ringwald 
69*77ba3d3fSMatthias Ringwald static const uint8_t * proxy_pdu;
70*77ba3d3fSMatthias Ringwald static uint16_t proxy_pdu_size;
71*77ba3d3fSMatthias Ringwald static uint8_t outgoing_ready;
72*77ba3d3fSMatthias Ringwald static uint16_t reassembly_offset;
73*77ba3d3fSMatthias Ringwald static uint16_t segmentation_offset;
74*77ba3d3fSMatthias Ringwald static mesh_msg_sar_field_t segmentation_state;
75*77ba3d3fSMatthias Ringwald static mesh_msg_type_t msg_type;
76*77ba3d3fSMatthias Ringwald static uint16_t gatt_bearer_mtu;
77*77ba3d3fSMatthias Ringwald static hci_con_handle_t gatt_bearer_con_handle;
78*77ba3d3fSMatthias Ringwald 
79*77ba3d3fSMatthias Ringwald static const uint8_t adv_data_with_network_id_template[] = {
80*77ba3d3fSMatthias Ringwald     // Flags general discoverable, BR/EDR not supported
81*77ba3d3fSMatthias Ringwald     0x02, BLUETOOTH_DATA_TYPE_FLAGS, 0x06,
82*77ba3d3fSMatthias Ringwald     // 16-bit Service UUIDs
83*77ba3d3fSMatthias 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,
84*77ba3d3fSMatthias Ringwald     // Service Data
85*77ba3d3fSMatthias Ringwald     0x0C, BLUETOOTH_DATA_TYPE_SERVICE_DATA, ORG_BLUETOOTH_SERVICE_MESH_PROXY & 0xff, ORG_BLUETOOTH_SERVICE_MESH_PROXY >> 8,
86*77ba3d3fSMatthias Ringwald           // MESH_IDENTIFICATION_NETWORK_ID_TYPE
87*77ba3d3fSMatthias Ringwald           MESH_IDENTIFICATION_NETWORK_ID_TYPE
88*77ba3d3fSMatthias Ringwald };
89*77ba3d3fSMatthias Ringwald 
90*77ba3d3fSMatthias Ringwald // round-robin
91*77ba3d3fSMatthias Ringwald static void gatt_bearer_emit_can_send_now(void){
92*77ba3d3fSMatthias Ringwald     // if (gatt_active) return;
93*77ba3d3fSMatthias Ringwald     int countdown = NUM_TYPES;
94*77ba3d3fSMatthias Ringwald     while (countdown--) {
95*77ba3d3fSMatthias Ringwald         last_sender++;
96*77ba3d3fSMatthias Ringwald         if (last_sender == NUM_TYPES) {
97*77ba3d3fSMatthias Ringwald             last_sender = 0;
98*77ba3d3fSMatthias Ringwald         }
99*77ba3d3fSMatthias Ringwald         if (request_can_send_now[last_sender]){
100*77ba3d3fSMatthias Ringwald             request_can_send_now[last_sender] = 0;
101*77ba3d3fSMatthias Ringwald             // emit can send now
102*77ba3d3fSMatthias Ringwald             uint8_t event[5];
103*77ba3d3fSMatthias Ringwald             int pos = 0;
104*77ba3d3fSMatthias Ringwald             event[pos++] = HCI_EVENT_MESH_META;
105*77ba3d3fSMatthias Ringwald             event[pos++] = 1;
106*77ba3d3fSMatthias Ringwald             event[pos++] = MESH_SUBEVENT_CAN_SEND_NOW;
107*77ba3d3fSMatthias Ringwald             little_endian_store_16(event, pos, gatt_bearer_con_handle);
108*77ba3d3fSMatthias Ringwald             pos += 2;
109*77ba3d3fSMatthias Ringwald             (*client_callbacks[last_sender])(HCI_EVENT_PACKET, 0, &event[0], pos);
110*77ba3d3fSMatthias Ringwald             return;
111*77ba3d3fSMatthias Ringwald         }
112*77ba3d3fSMatthias Ringwald     }
113*77ba3d3fSMatthias Ringwald }
114*77ba3d3fSMatthias Ringwald 
115*77ba3d3fSMatthias Ringwald static void gatt_bearer_emit_message_sent(mesh_msg_type_t type_id){
116*77ba3d3fSMatthias Ringwald     uint8_t event[5];
117*77ba3d3fSMatthias Ringwald     int pos = 0;
118*77ba3d3fSMatthias Ringwald     event[pos++] = HCI_EVENT_MESH_META;
119*77ba3d3fSMatthias Ringwald     event[pos++] = 1;
120*77ba3d3fSMatthias Ringwald     event[pos++] = MESH_SUBEVENT_MESSAGE_SENT;
121*77ba3d3fSMatthias Ringwald     little_endian_store_16(event, pos, gatt_bearer_con_handle);
122*77ba3d3fSMatthias Ringwald     pos += 2;
123*77ba3d3fSMatthias Ringwald     (*client_callbacks[type_id])(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
124*77ba3d3fSMatthias Ringwald }
125*77ba3d3fSMatthias Ringwald 
126*77ba3d3fSMatthias Ringwald static void gatt_bearer_emit_event_for_all(uint8_t * packet, uint16_t size){
127*77ba3d3fSMatthias Ringwald     unsigned int i;
128*77ba3d3fSMatthias Ringwald     for (i=0; i < NUM_TYPES; i++){
129*77ba3d3fSMatthias Ringwald         if ( client_callbacks[i] == NULL) continue;
130*77ba3d3fSMatthias Ringwald         (*client_callbacks[last_sender])(HCI_EVENT_PACKET, 0, packet, size);
131*77ba3d3fSMatthias Ringwald     }
132*77ba3d3fSMatthias Ringwald }
133*77ba3d3fSMatthias Ringwald 
134*77ba3d3fSMatthias Ringwald static void gatt_bearer_request(mesh_msg_type_t type_id){
135*77ba3d3fSMatthias Ringwald     request_can_send_now[type_id] = 1;
136*77ba3d3fSMatthias Ringwald     mesh_proxy_service_server_request_can_send_now(gatt_bearer_con_handle);
137*77ba3d3fSMatthias Ringwald }
138*77ba3d3fSMatthias Ringwald 
139*77ba3d3fSMatthias Ringwald 
140*77ba3d3fSMatthias Ringwald static void gatt_bearer_start_sending(hci_con_handle_t con_handle){
141*77ba3d3fSMatthias Ringwald     uint16_t pdu_segment_len = btstack_min(proxy_pdu_size - segmentation_offset, gatt_bearer_mtu - 1 - 3);
142*77ba3d3fSMatthias Ringwald     sar_buffer.segmentation_buffer[0] = (segmentation_state << 6) | msg_type;
143*77ba3d3fSMatthias Ringwald     memcpy(&sar_buffer.segmentation_buffer[1], &proxy_pdu[segmentation_offset], pdu_segment_len);
144*77ba3d3fSMatthias Ringwald     segmentation_offset += pdu_segment_len;
145*77ba3d3fSMatthias Ringwald     mesh_proxy_service_server_send_proxy_pdu(con_handle, sar_buffer.segmentation_buffer, pdu_segment_len + 1);
146*77ba3d3fSMatthias Ringwald 
147*77ba3d3fSMatthias Ringwald     switch (segmentation_state){
148*77ba3d3fSMatthias Ringwald         case MESH_MSG_SAR_FIELD_COMPLETE_MSG:
149*77ba3d3fSMatthias Ringwald         case MESH_MSG_SAR_FIELD_LAST_SEGMENT:
150*77ba3d3fSMatthias Ringwald             // gatt_bearer_emit_pdu_sent(0);
151*77ba3d3fSMatthias Ringwald             outgoing_ready = 0;
152*77ba3d3fSMatthias Ringwald             gatt_bearer_emit_message_sent(msg_type);
153*77ba3d3fSMatthias Ringwald             break;
154*77ba3d3fSMatthias Ringwald         case MESH_MSG_SAR_FIELD_CONTINUE:
155*77ba3d3fSMatthias Ringwald         case MESH_MSG_SAR_FIELD_FIRST_SEGMENT:
156*77ba3d3fSMatthias Ringwald             if ((proxy_pdu_size - segmentation_offset) > (gatt_bearer_mtu - 1 - 3)){
157*77ba3d3fSMatthias Ringwald                 segmentation_state = MESH_MSG_SAR_FIELD_CONTINUE;
158*77ba3d3fSMatthias Ringwald             } else {
159*77ba3d3fSMatthias Ringwald                 segmentation_state = MESH_MSG_SAR_FIELD_LAST_SEGMENT;
160*77ba3d3fSMatthias Ringwald             }
161*77ba3d3fSMatthias Ringwald             mesh_proxy_service_server_request_can_send_now(con_handle);
162*77ba3d3fSMatthias Ringwald             break;
163*77ba3d3fSMatthias Ringwald         default:
164*77ba3d3fSMatthias Ringwald             break;
165*77ba3d3fSMatthias Ringwald     }
166*77ba3d3fSMatthias Ringwald }
167*77ba3d3fSMatthias Ringwald 
168*77ba3d3fSMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
169*77ba3d3fSMatthias Ringwald     UNUSED(channel);
170*77ba3d3fSMatthias Ringwald     UNUSED(size);
171*77ba3d3fSMatthias Ringwald     mesh_msg_sar_field_t msg_sar_field;
172*77ba3d3fSMatthias Ringwald 
173*77ba3d3fSMatthias Ringwald     int pdu_segment_len;
174*77ba3d3fSMatthias Ringwald     int pos;
175*77ba3d3fSMatthias Ringwald     hci_con_handle_t con_handle;
176*77ba3d3fSMatthias Ringwald     int send_to_mesh_network;
177*77ba3d3fSMatthias Ringwald 
178*77ba3d3fSMatthias Ringwald     switch (packet_type) {
179*77ba3d3fSMatthias Ringwald         case MESH_PROXY_DATA_PACKET:
180*77ba3d3fSMatthias Ringwald             pos = 0;
181*77ba3d3fSMatthias Ringwald             // on provisioning PDU call packet handler with PROVISIONG_DATA type
182*77ba3d3fSMatthias Ringwald             msg_sar_field = packet[pos] >> 6;
183*77ba3d3fSMatthias Ringwald             msg_type = packet[pos] & 0x3F;
184*77ba3d3fSMatthias Ringwald             pos++;
185*77ba3d3fSMatthias Ringwald 
186*77ba3d3fSMatthias Ringwald             switch (msg_type){
187*77ba3d3fSMatthias Ringwald                 case MESH_MSG_TYPE_NETWORK_PDU:
188*77ba3d3fSMatthias Ringwald                 case MESH_MSG_TYPE_BEACON:
189*77ba3d3fSMatthias Ringwald                 case MESH_MSG_TYPE_PROXY_CONFIGURATION:
190*77ba3d3fSMatthias Ringwald                     if (!client_callbacks[msg_type]){
191*77ba3d3fSMatthias Ringwald                         log_error("client callback not defined");
192*77ba3d3fSMatthias Ringwald                         return;
193*77ba3d3fSMatthias Ringwald                     }
194*77ba3d3fSMatthias Ringwald                     break;
195*77ba3d3fSMatthias Ringwald                 default:
196*77ba3d3fSMatthias Ringwald                     log_info("gatt bearer: message type %d not supported", msg_type);
197*77ba3d3fSMatthias Ringwald                     return;
198*77ba3d3fSMatthias Ringwald             }
199*77ba3d3fSMatthias Ringwald             pdu_segment_len = size - pos;
200*77ba3d3fSMatthias Ringwald 
201*77ba3d3fSMatthias Ringwald             if (sizeof(sar_buffer.reassembly_buffer) - reassembly_offset < pdu_segment_len) {
202*77ba3d3fSMatthias Ringwald                 log_error("sar buffer too small left %d, new to store %d", MESH_PROV_MAX_PROXY_PDU - reassembly_offset, pdu_segment_len);
203*77ba3d3fSMatthias Ringwald                 break;
204*77ba3d3fSMatthias Ringwald             }
205*77ba3d3fSMatthias Ringwald 
206*77ba3d3fSMatthias Ringwald             // update mtu if incoming packet is larger than default
207*77ba3d3fSMatthias Ringwald             if (size > (ATT_DEFAULT_MTU - 1)){
208*77ba3d3fSMatthias Ringwald                 log_info("Remote uses larger MTU, enable long PDUs");
209*77ba3d3fSMatthias Ringwald                 gatt_bearer_mtu = att_server_get_mtu(channel);
210*77ba3d3fSMatthias Ringwald             }
211*77ba3d3fSMatthias Ringwald 
212*77ba3d3fSMatthias Ringwald             switch (msg_sar_field){
213*77ba3d3fSMatthias Ringwald                 case MESH_MSG_SAR_FIELD_COMPLETE_MSG:
214*77ba3d3fSMatthias Ringwald                 case MESH_MSG_SAR_FIELD_FIRST_SEGMENT:
215*77ba3d3fSMatthias Ringwald                     memset(sar_buffer.reassembly_buffer, 0, sizeof(sar_buffer.reassembly_buffer));
216*77ba3d3fSMatthias Ringwald                     if (sizeof(sar_buffer.reassembly_buffer) < pdu_segment_len) return;
217*77ba3d3fSMatthias Ringwald                     memcpy(sar_buffer.reassembly_buffer, packet+pos, pdu_segment_len);
218*77ba3d3fSMatthias Ringwald                     reassembly_offset = pdu_segment_len;
219*77ba3d3fSMatthias Ringwald                     break;
220*77ba3d3fSMatthias Ringwald                 case MESH_MSG_SAR_FIELD_CONTINUE:
221*77ba3d3fSMatthias Ringwald                     if ((sizeof(sar_buffer.reassembly_buffer) - reassembly_offset) < pdu_segment_len) return;
222*77ba3d3fSMatthias Ringwald                     memcpy(sar_buffer.reassembly_buffer + reassembly_offset, packet+pos, pdu_segment_len);
223*77ba3d3fSMatthias Ringwald                     reassembly_offset += pdu_segment_len;
224*77ba3d3fSMatthias Ringwald                     return;
225*77ba3d3fSMatthias Ringwald                 case MESH_MSG_SAR_FIELD_LAST_SEGMENT:
226*77ba3d3fSMatthias Ringwald                     if ((sizeof(sar_buffer.reassembly_buffer) - reassembly_offset) < pdu_segment_len) return;
227*77ba3d3fSMatthias Ringwald                     memcpy(sar_buffer.reassembly_buffer + reassembly_offset, packet+pos, pdu_segment_len);
228*77ba3d3fSMatthias Ringwald                     reassembly_offset += pdu_segment_len;
229*77ba3d3fSMatthias Ringwald                     break;
230*77ba3d3fSMatthias Ringwald                 default:
231*77ba3d3fSMatthias Ringwald                     break;
232*77ba3d3fSMatthias Ringwald             }
233*77ba3d3fSMatthias Ringwald 
234*77ba3d3fSMatthias Ringwald             send_to_mesh_network = (msg_sar_field == MESH_MSG_SAR_FIELD_COMPLETE_MSG) || (msg_sar_field == MESH_MSG_SAR_FIELD_LAST_SEGMENT);
235*77ba3d3fSMatthias Ringwald 
236*77ba3d3fSMatthias Ringwald             if (!send_to_mesh_network) break;
237*77ba3d3fSMatthias Ringwald             switch (msg_type){
238*77ba3d3fSMatthias Ringwald                 case MESH_MSG_TYPE_NETWORK_PDU:
239*77ba3d3fSMatthias Ringwald                 case MESH_MSG_TYPE_BEACON:
240*77ba3d3fSMatthias Ringwald                 case MESH_MSG_TYPE_PROXY_CONFIGURATION:
241*77ba3d3fSMatthias Ringwald                     if ((*client_callbacks[msg_type])){
242*77ba3d3fSMatthias Ringwald                         (*client_callbacks[msg_type])(MESH_PROXY_DATA_PACKET, 0, sar_buffer.reassembly_buffer, reassembly_offset);
243*77ba3d3fSMatthias Ringwald                     }
244*77ba3d3fSMatthias Ringwald                     reassembly_offset = 0;
245*77ba3d3fSMatthias Ringwald                     break;
246*77ba3d3fSMatthias Ringwald                 default:
247*77ba3d3fSMatthias Ringwald                     log_info("gatt bearer: message type %d not supported", msg_type);
248*77ba3d3fSMatthias Ringwald                     return;
249*77ba3d3fSMatthias Ringwald             }
250*77ba3d3fSMatthias Ringwald 
251*77ba3d3fSMatthias Ringwald             break;
252*77ba3d3fSMatthias Ringwald 
253*77ba3d3fSMatthias Ringwald         case HCI_EVENT_PACKET:
254*77ba3d3fSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
255*77ba3d3fSMatthias Ringwald                 case HCI_EVENT_MESH_META:
256*77ba3d3fSMatthias Ringwald                     switch (hci_event_mesh_meta_get_subevent_code(packet)){
257*77ba3d3fSMatthias Ringwald                         case MESH_SUBEVENT_PROXY_CONNECTED:
258*77ba3d3fSMatthias Ringwald                             gatt_bearer_mtu = ATT_DEFAULT_MTU;
259*77ba3d3fSMatthias Ringwald                             gatt_bearer_con_handle = mesh_subevent_proxy_connected_get_con_handle(packet);
260*77ba3d3fSMatthias Ringwald                             gatt_bearer_emit_event_for_all(packet, size);
261*77ba3d3fSMatthias Ringwald                             break;
262*77ba3d3fSMatthias Ringwald                         case MESH_SUBEVENT_PROXY_DISCONNECTED:
263*77ba3d3fSMatthias Ringwald                             gatt_bearer_con_handle = HCI_CON_HANDLE_INVALID;
264*77ba3d3fSMatthias Ringwald                             gatt_bearer_emit_event_for_all(packet, size);
265*77ba3d3fSMatthias Ringwald                             break;
266*77ba3d3fSMatthias Ringwald                         case MESH_SUBEVENT_CAN_SEND_NOW:
267*77ba3d3fSMatthias Ringwald                             con_handle = little_endian_read_16(packet, 3);
268*77ba3d3fSMatthias Ringwald                             if (con_handle == HCI_CON_HANDLE_INVALID) return;
269*77ba3d3fSMatthias Ringwald 
270*77ba3d3fSMatthias Ringwald                             if (!outgoing_ready){
271*77ba3d3fSMatthias Ringwald                                 gatt_bearer_emit_can_send_now();
272*77ba3d3fSMatthias Ringwald                                 return;
273*77ba3d3fSMatthias Ringwald                             }
274*77ba3d3fSMatthias Ringwald                             gatt_bearer_start_sending(con_handle);
275*77ba3d3fSMatthias Ringwald                             return;
276*77ba3d3fSMatthias Ringwald                         default:
277*77ba3d3fSMatthias Ringwald                             break;
278*77ba3d3fSMatthias Ringwald                     }
279*77ba3d3fSMatthias Ringwald             }
280*77ba3d3fSMatthias Ringwald             break;
281*77ba3d3fSMatthias Ringwald         default:
282*77ba3d3fSMatthias Ringwald             break;
283*77ba3d3fSMatthias Ringwald     }
284*77ba3d3fSMatthias Ringwald }
285*77ba3d3fSMatthias Ringwald 
286*77ba3d3fSMatthias Ringwald void gatt_bearer_init(void){
287*77ba3d3fSMatthias Ringwald     mesh_proxy_service_server_init();
288*77ba3d3fSMatthias Ringwald     mesh_proxy_service_server_register_packet_handler(packet_handler);
289*77ba3d3fSMatthias Ringwald }
290*77ba3d3fSMatthias Ringwald 
291*77ba3d3fSMatthias Ringwald void gatt_bearer_register_for_mesh_network_pdu(btstack_packet_handler_t _packet_handler){
292*77ba3d3fSMatthias Ringwald     client_callbacks[MESH_MSG_TYPE_NETWORK_PDU] = _packet_handler;
293*77ba3d3fSMatthias Ringwald }
294*77ba3d3fSMatthias Ringwald void gatt_bearer_register_for_mesh_beacon(btstack_packet_handler_t _packet_handler){
295*77ba3d3fSMatthias Ringwald     client_callbacks[MESH_MSG_TYPE_BEACON] = _packet_handler;
296*77ba3d3fSMatthias Ringwald }
297*77ba3d3fSMatthias Ringwald void gatt_bearer_register_for_mesh_proxy_configuration(btstack_packet_handler_t _packet_handler){
298*77ba3d3fSMatthias Ringwald     client_callbacks[MESH_MSG_TYPE_PROXY_CONFIGURATION] = _packet_handler;
299*77ba3d3fSMatthias Ringwald }
300*77ba3d3fSMatthias Ringwald 
301*77ba3d3fSMatthias Ringwald void gatt_bearer_request_can_send_now_for_mesh_network_pdu(void){
302*77ba3d3fSMatthias Ringwald     gatt_bearer_request(MESH_MSG_TYPE_NETWORK_PDU);
303*77ba3d3fSMatthias Ringwald }
304*77ba3d3fSMatthias Ringwald void gatt_bearer_request_can_send_now_for_mesh_beacon(void){
305*77ba3d3fSMatthias Ringwald     gatt_bearer_request(MESH_MSG_TYPE_BEACON);
306*77ba3d3fSMatthias Ringwald }
307*77ba3d3fSMatthias Ringwald void gatt_bearer_request_can_send_now_for_mesh_proxy_configuration(void){
308*77ba3d3fSMatthias Ringwald     gatt_bearer_request(MESH_MSG_TYPE_PROXY_CONFIGURATION);
309*77ba3d3fSMatthias Ringwald }
310*77ba3d3fSMatthias Ringwald 
311*77ba3d3fSMatthias Ringwald static void gatt_bearer_send_pdu(uint16_t con_handle, const uint8_t * pdu, uint16_t size){
312*77ba3d3fSMatthias Ringwald     if (!pdu || size <= 0) return;
313*77ba3d3fSMatthias Ringwald     if (con_handle == HCI_CON_HANDLE_INVALID) return;
314*77ba3d3fSMatthias Ringwald     // store pdu, request to send
315*77ba3d3fSMatthias Ringwald     proxy_pdu = pdu;
316*77ba3d3fSMatthias Ringwald     proxy_pdu_size = size;
317*77ba3d3fSMatthias Ringwald     segmentation_offset = 0;
318*77ba3d3fSMatthias Ringwald 
319*77ba3d3fSMatthias Ringwald     // check if segmentation is necessary
320*77ba3d3fSMatthias Ringwald     if (proxy_pdu_size > (gatt_bearer_mtu - 1 - 3)){
321*77ba3d3fSMatthias Ringwald         segmentation_state = MESH_MSG_SAR_FIELD_FIRST_SEGMENT;
322*77ba3d3fSMatthias Ringwald     } else {
323*77ba3d3fSMatthias Ringwald         segmentation_state = MESH_MSG_SAR_FIELD_COMPLETE_MSG;
324*77ba3d3fSMatthias Ringwald     }
325*77ba3d3fSMatthias Ringwald     outgoing_ready = 1;
326*77ba3d3fSMatthias Ringwald     gatt_bearer_start_sending(con_handle);
327*77ba3d3fSMatthias Ringwald }
328*77ba3d3fSMatthias Ringwald 
329*77ba3d3fSMatthias Ringwald void gatt_bearer_send_mesh_network_pdu(const uint8_t * data, uint16_t data_len){
330*77ba3d3fSMatthias Ringwald     msg_type = MESH_MSG_TYPE_NETWORK_PDU;
331*77ba3d3fSMatthias Ringwald     gatt_bearer_send_pdu(gatt_bearer_con_handle, data, data_len);
332*77ba3d3fSMatthias Ringwald }
333*77ba3d3fSMatthias Ringwald 
334*77ba3d3fSMatthias Ringwald void gatt_bearer_send_mesh_beacon(const uint8_t * data, uint16_t data_len){
335*77ba3d3fSMatthias Ringwald     msg_type = MESH_MSG_TYPE_BEACON;
336*77ba3d3fSMatthias Ringwald     gatt_bearer_send_pdu(gatt_bearer_con_handle, data, data_len);
337*77ba3d3fSMatthias Ringwald }
338*77ba3d3fSMatthias Ringwald 
339*77ba3d3fSMatthias Ringwald void gatt_bearer_send_mesh_proxy_configuration(const uint8_t * data, uint16_t data_len){
340*77ba3d3fSMatthias Ringwald     msg_type = MESH_MSG_TYPE_PROXY_CONFIGURATION;
341*77ba3d3fSMatthias Ringwald     gatt_bearer_send_pdu(gatt_bearer_con_handle, data, data_len);
342*77ba3d3fSMatthias Ringwald }
343*77ba3d3fSMatthias Ringwald 
344*77ba3d3fSMatthias Ringwald uint8_t gatt_bearer_setup_advertising_with_network_id(uint8_t * buffer, uint8_t * network_id){
345*77ba3d3fSMatthias Ringwald     memcpy(&buffer[0], adv_data_with_network_id_template, 12);
346*77ba3d3fSMatthias Ringwald     memcpy(&buffer[12], network_id, 8);
347*77ba3d3fSMatthias Ringwald     return 20;
348*77ba3d3fSMatthias Ringwald }
349