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