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