xref: /btstack/src/mesh/gatt_bearer.c (revision d58a1b5f11ada8ddf896c41fff5a35e7f140c37e)
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 // 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     memcpy(&sar_buffer.segmentation_buffer[1], &proxy_pdu[segmentation_offset], pdu_segment_len);
133     segmentation_offset += pdu_segment_len;
134     mesh_proxy_service_server_send_proxy_pdu(con_handle, sar_buffer.segmentation_buffer, pdu_segment_len + 1);
135 
136     switch (segmentation_state){
137         case MESH_MSG_SAR_FIELD_COMPLETE_MSG:
138         case MESH_MSG_SAR_FIELD_LAST_SEGMENT:
139             // gatt_bearer_emit_pdu_sent(0);
140             outgoing_ready = 0;
141             gatt_bearer_emit_message_sent(msg_type);
142             break;
143         case MESH_MSG_SAR_FIELD_CONTINUE:
144         case MESH_MSG_SAR_FIELD_FIRST_SEGMENT:
145             if ((proxy_pdu_size - segmentation_offset) > (gatt_bearer_mtu - 1 - 3)){
146                 segmentation_state = MESH_MSG_SAR_FIELD_CONTINUE;
147             } else {
148                 segmentation_state = MESH_MSG_SAR_FIELD_LAST_SEGMENT;
149             }
150             mesh_proxy_service_server_request_can_send_now(con_handle);
151             break;
152         default:
153             break;
154     }
155 }
156 
157 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
158     UNUSED(channel);
159     UNUSED(size);
160     mesh_msg_sar_field_t msg_sar_field;
161 
162     uint16_t pdu_segment_len;
163     uint16_t pos;
164     hci_con_handle_t con_handle;
165     int send_to_mesh_network;
166 
167     switch (packet_type) {
168         case MESH_PROXY_DATA_PACKET:
169             pos = 0;
170             // on provisioning PDU call packet handler with PROVISIONG_DATA type
171             msg_sar_field = packet[pos] >> 6;
172             msg_type = packet[pos] & 0x3F;
173             pos++;
174 
175             switch (msg_type){
176                 case MESH_MSG_TYPE_NETWORK_PDU:
177                 case MESH_MSG_TYPE_BEACON:
178                 case MESH_MSG_TYPE_PROXY_CONFIGURATION:
179                     if (!client_callbacks[msg_type]){
180                         log_error("client callback not defined");
181                         return;
182                     }
183                     break;
184                 default:
185                     log_info("gatt bearer: message type %d not supported", msg_type);
186                     return;
187             }
188             pdu_segment_len = size - pos;
189 
190             if (sizeof(sar_buffer.reassembly_buffer) - reassembly_offset < pdu_segment_len) {
191                 log_error("sar buffer too small left %d, new to store %d", MESH_PROV_MAX_PROXY_PDU - reassembly_offset, pdu_segment_len);
192                 break;
193             }
194 
195             // update mtu if incoming packet is larger than default
196             if (size > (ATT_DEFAULT_MTU - 1)){
197                 log_info("Remote uses larger MTU, enable long PDUs");
198                 gatt_bearer_mtu = att_server_get_mtu(channel);
199             }
200 
201             switch (msg_sar_field){
202                 case MESH_MSG_SAR_FIELD_COMPLETE_MSG:
203                 case MESH_MSG_SAR_FIELD_FIRST_SEGMENT:
204                     memset(sar_buffer.reassembly_buffer, 0, sizeof(sar_buffer.reassembly_buffer));
205                     if (sizeof(sar_buffer.reassembly_buffer) < pdu_segment_len) return;
206                     memcpy(sar_buffer.reassembly_buffer, packet+pos, pdu_segment_len);
207                     reassembly_offset = pdu_segment_len;
208                     break;
209                 case MESH_MSG_SAR_FIELD_CONTINUE:
210                     if ((sizeof(sar_buffer.reassembly_buffer) - reassembly_offset) < pdu_segment_len) return;
211                     memcpy(sar_buffer.reassembly_buffer + reassembly_offset, packet+pos, pdu_segment_len);
212                     reassembly_offset += pdu_segment_len;
213                     return;
214                 case MESH_MSG_SAR_FIELD_LAST_SEGMENT:
215                     if ((sizeof(sar_buffer.reassembly_buffer) - reassembly_offset) < pdu_segment_len) return;
216                     memcpy(sar_buffer.reassembly_buffer + reassembly_offset, packet+pos, pdu_segment_len);
217                     reassembly_offset += pdu_segment_len;
218                     break;
219                 default:
220                     break;
221             }
222 
223             send_to_mesh_network = (msg_sar_field == MESH_MSG_SAR_FIELD_COMPLETE_MSG) || (msg_sar_field == MESH_MSG_SAR_FIELD_LAST_SEGMENT);
224 
225             if (!send_to_mesh_network) break;
226             switch (msg_type){
227                 case MESH_MSG_TYPE_NETWORK_PDU:
228                 case MESH_MSG_TYPE_BEACON:
229                 case MESH_MSG_TYPE_PROXY_CONFIGURATION:
230                     if ((*client_callbacks[msg_type])){
231                         (*client_callbacks[msg_type])(MESH_PROXY_DATA_PACKET, 0, sar_buffer.reassembly_buffer, reassembly_offset);
232                     }
233                     reassembly_offset = 0;
234                     break;
235                 default:
236                     log_info("gatt bearer: message type %d not supported", msg_type);
237                     return;
238             }
239 
240             break;
241 
242         case HCI_EVENT_PACKET:
243             switch (hci_event_packet_get_type(packet)) {
244                 case HCI_EVENT_MESH_META:
245                     switch (hci_event_mesh_meta_get_subevent_code(packet)){
246                         case MESH_SUBEVENT_PROXY_CONNECTED:
247                             gatt_bearer_mtu = ATT_DEFAULT_MTU;
248                             gatt_bearer_con_handle = mesh_subevent_proxy_connected_get_con_handle(packet);
249                             gatt_bearer_emit_event_for_all(packet, size);
250                             break;
251                         case MESH_SUBEVENT_PROXY_DISCONNECTED:
252                             gatt_bearer_con_handle = HCI_CON_HANDLE_INVALID;
253                             gatt_bearer_emit_event_for_all(packet, size);
254                             break;
255                         case MESH_SUBEVENT_CAN_SEND_NOW:
256                             con_handle = little_endian_read_16(packet, 3);
257                             if (con_handle == HCI_CON_HANDLE_INVALID) return;
258 
259                             if (!outgoing_ready){
260                                 gatt_bearer_emit_can_send_now();
261                                 return;
262                             }
263                             gatt_bearer_start_sending(con_handle);
264                             return;
265                         default:
266                             break;
267                     }
268             }
269             break;
270         default:
271             break;
272     }
273 }
274 
275 void gatt_bearer_init(void){
276     mesh_proxy_service_server_init();
277     mesh_proxy_service_server_register_packet_handler(packet_handler);
278 }
279 
280 void gatt_bearer_register_for_network_pdu(btstack_packet_handler_t _packet_handler){
281     client_callbacks[MESH_MSG_TYPE_NETWORK_PDU] = _packet_handler;
282 }
283 void gatt_bearer_register_for_beacon(btstack_packet_handler_t _packet_handler){
284     client_callbacks[MESH_MSG_TYPE_BEACON] = _packet_handler;
285 }
286 void gatt_bearer_register_for_mesh_proxy_configuration(btstack_packet_handler_t _packet_handler){
287     client_callbacks[MESH_MSG_TYPE_PROXY_CONFIGURATION] = _packet_handler;
288 }
289 
290 void gatt_bearer_request_can_send_now_for_network_pdu(void){
291     gatt_bearer_request(MESH_MSG_TYPE_NETWORK_PDU);
292 }
293 void gatt_bearer_request_can_send_now_for_beacon(void){
294     gatt_bearer_request(MESH_MSG_TYPE_BEACON);
295 }
296 void gatt_bearer_request_can_send_now_for_mesh_proxy_configuration(void){
297     gatt_bearer_request(MESH_MSG_TYPE_PROXY_CONFIGURATION);
298 }
299 
300 static void gatt_bearer_send_pdu(uint16_t con_handle, const uint8_t * pdu, uint16_t size){
301     if (!pdu || size <= 0) return;
302     if (con_handle == HCI_CON_HANDLE_INVALID) return;
303     // store pdu, request to send
304     proxy_pdu = pdu;
305     proxy_pdu_size = size;
306     segmentation_offset = 0;
307 
308     // check if segmentation is necessary
309     if (proxy_pdu_size > (gatt_bearer_mtu - 1 - 3)){
310         segmentation_state = MESH_MSG_SAR_FIELD_FIRST_SEGMENT;
311     } else {
312         segmentation_state = MESH_MSG_SAR_FIELD_COMPLETE_MSG;
313     }
314     outgoing_ready = 1;
315     gatt_bearer_start_sending(con_handle);
316 }
317 
318 void gatt_bearer_send_network_pdu(const uint8_t * data, uint16_t data_len){
319     msg_type = MESH_MSG_TYPE_NETWORK_PDU;
320     gatt_bearer_send_pdu(gatt_bearer_con_handle, data, data_len);
321 }
322 
323 void gatt_bearer_send_beacon(const uint8_t * data, uint16_t data_len){
324     msg_type = MESH_MSG_TYPE_BEACON;
325     gatt_bearer_send_pdu(gatt_bearer_con_handle, data, data_len);
326 }
327 
328 void gatt_bearer_send_mesh_proxy_configuration(const uint8_t * data, uint16_t data_len){
329     msg_type = MESH_MSG_TYPE_PROXY_CONFIGURATION;
330     gatt_bearer_send_pdu(gatt_bearer_con_handle, data, data_len);
331 }
332 
333