xref: /btstack/src/mesh/gatt_bearer.c (revision 01122b73eef352aeca7441c8b29c15365f0d0177)
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 MATTHIAS
24  * RINGWALD 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/gatt-service/mesh_proxy_service_server.h"
43 #include "ble/att_server.h"
44 #include "mesh/gatt_bearer.h"
45 #include "ble/core.h"
46 #include "bluetooth.h"
47 #include "bluetooth_data_types.h"
48 #include "bluetooth_gatt.h"
49 #include "btstack_debug.h"
50 #include "btstack_util.h"
51 #include "btstack_run_loop.h"
52 #include "btstack_event.h"
53 #include "gap.h"
54 #include "btstack_event.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 static const uint8_t adv_data_with_network_id_template[] = {
80     // Flags general discoverable, BR/EDR not supported
81     0x02, BLUETOOTH_DATA_TYPE_FLAGS, 0x06,
82     // 16-bit Service UUIDs
83     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     // Service Data
85     0x0C, BLUETOOTH_DATA_TYPE_SERVICE_DATA, ORG_BLUETOOTH_SERVICE_MESH_PROXY & 0xff, ORG_BLUETOOTH_SERVICE_MESH_PROXY >> 8,
86           // MESH_IDENTIFICATION_NETWORK_ID_TYPE
87           MESH_IDENTIFICATION_NETWORK_ID_TYPE
88 };
89 
90 // round-robin
91 static void gatt_bearer_emit_can_send_now(void){
92     // if (gatt_active) return;
93     int countdown = NUM_TYPES;
94     while (countdown--) {
95         last_sender++;
96         if (last_sender == NUM_TYPES) {
97             last_sender = 0;
98         }
99         if (request_can_send_now[last_sender]){
100             request_can_send_now[last_sender] = 0;
101             // emit can send now
102             uint8_t event[5];
103             int pos = 0;
104             event[pos++] = HCI_EVENT_MESH_META;
105             event[pos++] = 1;
106             event[pos++] = MESH_SUBEVENT_CAN_SEND_NOW;
107             little_endian_store_16(event, pos, gatt_bearer_con_handle);
108             pos += 2;
109             (*client_callbacks[last_sender])(HCI_EVENT_PACKET, 0, &event[0], pos);
110             return;
111         }
112     }
113 }
114 
115 static void gatt_bearer_emit_message_sent(mesh_msg_type_t type_id){
116     uint8_t event[5];
117     int pos = 0;
118     event[pos++] = HCI_EVENT_MESH_META;
119     event[pos++] = 1;
120     event[pos++] = MESH_SUBEVENT_MESSAGE_SENT;
121     little_endian_store_16(event, pos, gatt_bearer_con_handle);
122     pos += 2;
123     (*client_callbacks[type_id])(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
124 }
125 
126 static void gatt_bearer_emit_event_for_all(uint8_t * packet, uint16_t size){
127     unsigned int i;
128     for (i=0; i < NUM_TYPES; i++){
129         if ( client_callbacks[i] == NULL) continue;
130         (*client_callbacks[last_sender])(HCI_EVENT_PACKET, 0, packet, size);
131     }
132 }
133 
134 static void gatt_bearer_request(mesh_msg_type_t type_id){
135     request_can_send_now[type_id] = 1;
136     mesh_proxy_service_server_request_can_send_now(gatt_bearer_con_handle);
137 }
138 
139 
140 static void gatt_bearer_start_sending(hci_con_handle_t con_handle){
141     uint16_t pdu_segment_len = btstack_min(proxy_pdu_size - segmentation_offset, gatt_bearer_mtu - 1 - 3);
142     sar_buffer.segmentation_buffer[0] = (segmentation_state << 6) | msg_type;
143     memcpy(&sar_buffer.segmentation_buffer[1], &proxy_pdu[segmentation_offset], pdu_segment_len);
144     segmentation_offset += pdu_segment_len;
145     mesh_proxy_service_server_send_proxy_pdu(con_handle, sar_buffer.segmentation_buffer, pdu_segment_len + 1);
146 
147     switch (segmentation_state){
148         case MESH_MSG_SAR_FIELD_COMPLETE_MSG:
149         case MESH_MSG_SAR_FIELD_LAST_SEGMENT:
150             // gatt_bearer_emit_pdu_sent(0);
151             outgoing_ready = 0;
152             gatt_bearer_emit_message_sent(msg_type);
153             break;
154         case MESH_MSG_SAR_FIELD_CONTINUE:
155         case MESH_MSG_SAR_FIELD_FIRST_SEGMENT:
156             if ((proxy_pdu_size - segmentation_offset) > (gatt_bearer_mtu - 1 - 3)){
157                 segmentation_state = MESH_MSG_SAR_FIELD_CONTINUE;
158             } else {
159                 segmentation_state = MESH_MSG_SAR_FIELD_LAST_SEGMENT;
160             }
161             mesh_proxy_service_server_request_can_send_now(con_handle);
162             break;
163         default:
164             break;
165     }
166 }
167 
168 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
169     UNUSED(channel);
170     UNUSED(size);
171     mesh_msg_sar_field_t msg_sar_field;
172 
173     int pdu_segment_len;
174     int pos;
175     hci_con_handle_t con_handle;
176     int send_to_mesh_network;
177 
178     switch (packet_type) {
179         case MESH_PROXY_DATA_PACKET:
180             pos = 0;
181             // on provisioning PDU call packet handler with PROVISIONG_DATA type
182             msg_sar_field = packet[pos] >> 6;
183             msg_type = packet[pos] & 0x3F;
184             pos++;
185 
186             switch (msg_type){
187                 case MESH_MSG_TYPE_NETWORK_PDU:
188                 case MESH_MSG_TYPE_BEACON:
189                 case MESH_MSG_TYPE_PROXY_CONFIGURATION:
190                     if (!client_callbacks[msg_type]){
191                         log_error("client callback not defined");
192                         return;
193                     }
194                     break;
195                 default:
196                     log_info("gatt bearer: message type %d not supported", msg_type);
197                     return;
198             }
199             pdu_segment_len = size - pos;
200 
201             if (sizeof(sar_buffer.reassembly_buffer) - reassembly_offset < pdu_segment_len) {
202                 log_error("sar buffer too small left %d, new to store %d", MESH_PROV_MAX_PROXY_PDU - reassembly_offset, pdu_segment_len);
203                 break;
204             }
205 
206             // update mtu if incoming packet is larger than default
207             if (size > (ATT_DEFAULT_MTU - 1)){
208                 log_info("Remote uses larger MTU, enable long PDUs");
209                 gatt_bearer_mtu = att_server_get_mtu(channel);
210             }
211 
212             switch (msg_sar_field){
213                 case MESH_MSG_SAR_FIELD_COMPLETE_MSG:
214                 case MESH_MSG_SAR_FIELD_FIRST_SEGMENT:
215                     memset(sar_buffer.reassembly_buffer, 0, sizeof(sar_buffer.reassembly_buffer));
216                     if (sizeof(sar_buffer.reassembly_buffer) < pdu_segment_len) return;
217                     memcpy(sar_buffer.reassembly_buffer, packet+pos, pdu_segment_len);
218                     reassembly_offset = pdu_segment_len;
219                     break;
220                 case MESH_MSG_SAR_FIELD_CONTINUE:
221                     if ((sizeof(sar_buffer.reassembly_buffer) - reassembly_offset) < pdu_segment_len) return;
222                     memcpy(sar_buffer.reassembly_buffer + reassembly_offset, packet+pos, pdu_segment_len);
223                     reassembly_offset += pdu_segment_len;
224                     return;
225                 case MESH_MSG_SAR_FIELD_LAST_SEGMENT:
226                     if ((sizeof(sar_buffer.reassembly_buffer) - reassembly_offset) < pdu_segment_len) return;
227                     memcpy(sar_buffer.reassembly_buffer + reassembly_offset, packet+pos, pdu_segment_len);
228                     reassembly_offset += pdu_segment_len;
229                     break;
230                 default:
231                     break;
232             }
233 
234             send_to_mesh_network = (msg_sar_field == MESH_MSG_SAR_FIELD_COMPLETE_MSG) || (msg_sar_field == MESH_MSG_SAR_FIELD_LAST_SEGMENT);
235 
236             if (!send_to_mesh_network) break;
237             switch (msg_type){
238                 case MESH_MSG_TYPE_NETWORK_PDU:
239                 case MESH_MSG_TYPE_BEACON:
240                 case MESH_MSG_TYPE_PROXY_CONFIGURATION:
241                     if ((*client_callbacks[msg_type])){
242                         (*client_callbacks[msg_type])(MESH_PROXY_DATA_PACKET, 0, sar_buffer.reassembly_buffer, reassembly_offset);
243                     }
244                     reassembly_offset = 0;
245                     break;
246                 default:
247                     log_info("gatt bearer: message type %d not supported", msg_type);
248                     return;
249             }
250 
251             break;
252 
253         case HCI_EVENT_PACKET:
254             switch (hci_event_packet_get_type(packet)) {
255                 case HCI_EVENT_MESH_META:
256                     switch (hci_event_mesh_meta_get_subevent_code(packet)){
257                         case MESH_SUBEVENT_PROXY_CONNECTED:
258                             gatt_bearer_mtu = ATT_DEFAULT_MTU;
259                             gatt_bearer_con_handle = mesh_subevent_proxy_connected_get_con_handle(packet);
260                             gatt_bearer_emit_event_for_all(packet, size);
261                             break;
262                         case MESH_SUBEVENT_PROXY_DISCONNECTED:
263                             gatt_bearer_con_handle = HCI_CON_HANDLE_INVALID;
264                             gatt_bearer_emit_event_for_all(packet, size);
265                             break;
266                         case MESH_SUBEVENT_CAN_SEND_NOW:
267                             con_handle = little_endian_read_16(packet, 3);
268                             if (con_handle == HCI_CON_HANDLE_INVALID) return;
269 
270                             if (!outgoing_ready){
271                                 gatt_bearer_emit_can_send_now();
272                                 return;
273                             }
274                             gatt_bearer_start_sending(con_handle);
275                             return;
276                         default:
277                             break;
278                     }
279             }
280             break;
281         default:
282             break;
283     }
284 }
285 
286 void gatt_bearer_init(void){
287     mesh_proxy_service_server_init();
288     mesh_proxy_service_server_register_packet_handler(packet_handler);
289 }
290 
291 void gatt_bearer_register_for_network_pdu(btstack_packet_handler_t _packet_handler){
292     client_callbacks[MESH_MSG_TYPE_NETWORK_PDU] = _packet_handler;
293 }
294 void gatt_bearer_register_for_beacon(btstack_packet_handler_t _packet_handler){
295     client_callbacks[MESH_MSG_TYPE_BEACON] = _packet_handler;
296 }
297 void gatt_bearer_register_for_mesh_proxy_configuration(btstack_packet_handler_t _packet_handler){
298     client_callbacks[MESH_MSG_TYPE_PROXY_CONFIGURATION] = _packet_handler;
299 }
300 
301 void gatt_bearer_request_can_send_now_for_network_pdu(void){
302     gatt_bearer_request(MESH_MSG_TYPE_NETWORK_PDU);
303 }
304 void gatt_bearer_request_can_send_now_for_beacon(void){
305     gatt_bearer_request(MESH_MSG_TYPE_BEACON);
306 }
307 void gatt_bearer_request_can_send_now_for_mesh_proxy_configuration(void){
308     gatt_bearer_request(MESH_MSG_TYPE_PROXY_CONFIGURATION);
309 }
310 
311 static void gatt_bearer_send_pdu(uint16_t con_handle, const uint8_t * pdu, uint16_t size){
312     if (!pdu || size <= 0) return;
313     if (con_handle == HCI_CON_HANDLE_INVALID) return;
314     // store pdu, request to send
315     proxy_pdu = pdu;
316     proxy_pdu_size = size;
317     segmentation_offset = 0;
318 
319     // check if segmentation is necessary
320     if (proxy_pdu_size > (gatt_bearer_mtu - 1 - 3)){
321         segmentation_state = MESH_MSG_SAR_FIELD_FIRST_SEGMENT;
322     } else {
323         segmentation_state = MESH_MSG_SAR_FIELD_COMPLETE_MSG;
324     }
325     outgoing_ready = 1;
326     gatt_bearer_start_sending(con_handle);
327 }
328 
329 void gatt_bearer_send_network_pdu(const uint8_t * data, uint16_t data_len){
330     msg_type = MESH_MSG_TYPE_NETWORK_PDU;
331     gatt_bearer_send_pdu(gatt_bearer_con_handle, data, data_len);
332 }
333 
334 void gatt_bearer_send_beacon(const uint8_t * data, uint16_t data_len){
335     msg_type = MESH_MSG_TYPE_BEACON;
336     gatt_bearer_send_pdu(gatt_bearer_con_handle, data, data_len);
337 }
338 
339 void gatt_bearer_send_mesh_proxy_configuration(const uint8_t * data, uint16_t data_len){
340     msg_type = MESH_MSG_TYPE_PROXY_CONFIGURATION;
341     gatt_bearer_send_pdu(gatt_bearer_con_handle, data, data_len);
342 }
343 
344 uint8_t gatt_bearer_setup_advertising_with_network_id(uint8_t * buffer, uint8_t * network_id){
345     memcpy(&buffer[0], adv_data_with_network_id_template, 12);
346     memcpy(&buffer[12], network_id, 8);
347     return 20;
348 }
349