xref: /btstack/src/mesh/pb_gatt.c (revision 7cdc89a533ca236b2c2564b759993b788bae89d3)
1 /*
2  * Copyright (C) 2014 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__ "pb_gatt.c"
39 
40 #include "mesh/pb_gatt.h"
41 
42 #include <stdlib.h>
43 #include <string.h>
44 
45 #include "ble/att_server.h"
46 #include "ble/gatt-service/mesh_provisioning_service_server.h"
47 
48 #include "btstack_debug.h"
49 #include "btstack_event.h"
50 #include "btstack_util.h"
51 
52 #include "mesh/provisioning.h"
53 
54 /************** PB GATT Mesh Provisioning ********************/
55 
56 // share buffer for reassembly and segmentation - protocol is half-duplex
57 static union {
58     uint8_t  reassembly_buffer[MESH_PROV_MAX_PROXY_PDU];
59     uint8_t  segmentation_buffer[MESH_PROV_MAX_PROXY_PDU];
60 } sar_buffer;
61 
62 static const uint8_t * proxy_pdu;
63 static uint16_t proxy_pdu_size;
64 
65 static uint16_t reassembly_offset;
66 static uint16_t segmentation_offset;
67 static mesh_msg_sar_field_t segmentation_state;
68 static uint16_t pb_gatt_mtu;
69 
70 static btstack_packet_handler_t pb_gatt_packet_handler;
71 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
72 
73 static void pb_gatt_emit_pdu_sent(uint8_t status){
74     uint8_t event[] = {HCI_EVENT_MESH_META, 2, MESH_SUBEVENT_PB_TRANSPORT_PDU_SENT, status};
75     pb_gatt_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event));
76 }
77 
78 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
79     UNUSED(channel);
80     UNUSED(size);
81     mesh_msg_sar_field_t msg_sar_field;
82     mesh_msg_type_t msg_type;
83     uint16_t pdu_segment_len;
84     uint16_t pos;
85     hci_con_handle_t con_handle;
86 
87     switch (packet_type) {
88         case PROVISIONING_DATA_PACKET:
89             pos = 0;
90             // on provisioning PDU call packet handler with PROVISIONG_DATA type
91             msg_sar_field = packet[pos] >> 6;
92             msg_type = packet[pos] & 0x3F;
93             pos++;
94             if (msg_type != MESH_MSG_TYPE_PROVISIONING_PDU) return;
95             if (!pb_gatt_packet_handler) return;
96 
97             pdu_segment_len = size - pos;
98 
99             if (sizeof(sar_buffer.reassembly_buffer) - reassembly_offset < pdu_segment_len) {
100                 log_error("sar buffer too small left %d, new to store %d", MESH_PROV_MAX_PROXY_PDU - reassembly_offset, pdu_segment_len);
101                 break;
102             }
103 
104             // update mtu if incoming packet is larger than default
105             if (size > (ATT_DEFAULT_MTU - 1)){
106                 log_info("Remote uses larger MTU, enable long PDUs");
107                 pb_gatt_mtu = att_server_get_mtu(channel);
108             }
109 
110             switch (msg_sar_field){
111                 case MESH_MSG_SAR_FIELD_FIRST_SEGMENT:
112                     memset(sar_buffer.reassembly_buffer, 0, sizeof(sar_buffer.reassembly_buffer));
113                     (void)memcpy(sar_buffer.reassembly_buffer, packet + pos,
114                                  pdu_segment_len);
115                     reassembly_offset = pdu_segment_len;
116                     return;
117                 case MESH_MSG_SAR_FIELD_CONTINUE:
118                     (void)memcpy(sar_buffer.reassembly_buffer + reassembly_offset,
119                                  packet + pos, pdu_segment_len);
120                     reassembly_offset += pdu_segment_len;
121                     return;
122                 case MESH_MSG_SAR_FIELD_LAST_SEGMENT:
123                     (void)memcpy(sar_buffer.reassembly_buffer + reassembly_offset,
124                                  packet + pos, pdu_segment_len);
125                     reassembly_offset += pdu_segment_len;
126                     // send to provisioning device
127                     pb_gatt_packet_handler(PROVISIONING_DATA_PACKET, 0, sar_buffer.reassembly_buffer, reassembly_offset);
128                     reassembly_offset = 0;
129                     break;
130                 case MESH_MSG_SAR_FIELD_COMPLETE_MSG:
131                     // send to provisioning device
132                     pb_gatt_packet_handler(PROVISIONING_DATA_PACKET, 0, packet+pos, pdu_segment_len);
133                     break;
134             }
135             break;
136 
137         case HCI_EVENT_PACKET:
138             switch (hci_event_packet_get_type(packet)) {
139                 case HCI_EVENT_MESH_META:
140                     switch (hci_event_mesh_meta_get_subevent_code(packet)){
141                         case MESH_SUBEVENT_PB_TRANSPORT_LINK_OPEN:
142                         case MESH_SUBEVENT_PB_TRANSPORT_LINK_CLOSED:
143                             // Forward link open/close
144                             pb_gatt_mtu = ATT_DEFAULT_MTU;
145                             pb_gatt_packet_handler(HCI_EVENT_PACKET, 0, packet, size);
146                             break;
147                         case MESH_SUBEVENT_CAN_SEND_NOW:
148                             con_handle = little_endian_read_16(packet, 3);
149                             if (con_handle == HCI_CON_HANDLE_INVALID) return;
150 
151                             sar_buffer.segmentation_buffer[0] = (segmentation_state << 6) | MESH_MSG_TYPE_PROVISIONING_PDU;
152                             pdu_segment_len = btstack_min(proxy_pdu_size - segmentation_offset, pb_gatt_mtu - 1);
153                             (void)memcpy(&sar_buffer.segmentation_buffer[1],
154                                          &proxy_pdu[segmentation_offset],
155                                          pdu_segment_len);
156                             segmentation_offset += pdu_segment_len;
157 
158                             mesh_provisioning_service_server_send_proxy_pdu(con_handle, sar_buffer.segmentation_buffer, pdu_segment_len + 1);
159 
160                             switch (segmentation_state){
161                                 case MESH_MSG_SAR_FIELD_COMPLETE_MSG:
162                                 case MESH_MSG_SAR_FIELD_LAST_SEGMENT:
163                                     pb_gatt_emit_pdu_sent(0);
164                                     break;
165                                 case MESH_MSG_SAR_FIELD_CONTINUE:
166                                 case MESH_MSG_SAR_FIELD_FIRST_SEGMENT:
167                                     if ((proxy_pdu_size - segmentation_offset) > (pb_gatt_mtu - 1)){
168                                         segmentation_state = MESH_MSG_SAR_FIELD_CONTINUE;
169                                     } else {
170                                         segmentation_state = MESH_MSG_SAR_FIELD_LAST_SEGMENT;
171                                     }
172                                     mesh_provisioning_service_server_request_can_send_now(con_handle);
173                                     break;
174                             }
175                             break;
176                         default:
177                             break;
178                     }
179             }
180             break;
181         default:
182             break;
183     }
184 }
185 
186 /**
187  * Setup mesh provisioning service
188  * @param device_uuid
189  */
190 void pb_gatt_init(void){
191     // setup mesh provisioning service
192     mesh_provisioning_service_server_init();
193     mesh_provisioning_service_server_register_packet_handler(packet_handler);
194 }
195 
196 /**
197  * Register listener for Provisioning PDUs
198  */
199 void pb_gatt_register_packet_handler(btstack_packet_handler_t _packet_handler){
200     pb_gatt_packet_handler = _packet_handler;
201 }
202 
203 /**
204  * Send pdu
205  * @param con_handle
206  * @param pdu
207  * @param pdu_size
208  */
209 void pb_gatt_send_pdu(uint16_t con_handle, const uint8_t * pdu, uint16_t size){
210     if (!pdu || size <= 0) return;
211     if (con_handle == HCI_CON_HANDLE_INVALID) return;
212     // store pdu, request to send
213     proxy_pdu = pdu;
214     proxy_pdu_size = size;
215     segmentation_offset = 0;
216 
217     // check if segmentation is necessary
218     if (proxy_pdu_size > (pb_gatt_mtu - 1)){
219         segmentation_state = MESH_MSG_SAR_FIELD_FIRST_SEGMENT;
220     } else {
221         segmentation_state = MESH_MSG_SAR_FIELD_COMPLETE_MSG;
222     }
223     mesh_provisioning_service_server_request_can_send_now(con_handle);
224 }
225 /**
226  * Close Link
227  * @param con_handle
228  * @param reason 0 = success, 1 = timeout, 2 = fail
229  */
230 void pb_gatt_close_link(hci_con_handle_t con_handle, uint8_t reason){
231     UNUSED(con_handle);
232     UNUSED(reason);
233 }
234 
235 /**
236  * Setup Link with unprovisioned device
237  * @param device_uuid
238  * @returns con_handle or HCI_CON_HANDLE_INVALID
239  */
240 uint16_t pb_gatt_create_link(const uint8_t * device_uuid){
241     UNUSED(device_uuid);
242     return HCI_CON_HANDLE_INVALID;
243 }
244