xref: /btstack/src/mesh/pb_gatt.c (revision d58a1b5f11ada8ddf896c41fff5a35e7f140c37e)
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                     memcpy(sar_buffer.reassembly_buffer, packet+pos, pdu_segment_len);
114                     reassembly_offset = pdu_segment_len;
115                     return;
116                 case MESH_MSG_SAR_FIELD_CONTINUE:
117                     memcpy(sar_buffer.reassembly_buffer + reassembly_offset, packet+pos, pdu_segment_len);
118                     reassembly_offset += pdu_segment_len;
119                     return;
120                 case MESH_MSG_SAR_FIELD_LAST_SEGMENT:
121                     memcpy(sar_buffer.reassembly_buffer + reassembly_offset, packet+pos, pdu_segment_len);
122                     reassembly_offset += pdu_segment_len;
123                     // send to provisioning device
124                     pb_gatt_packet_handler(PROVISIONING_DATA_PACKET, 0, sar_buffer.reassembly_buffer, reassembly_offset);
125                     reassembly_offset = 0;
126                     break;
127                 case MESH_MSG_SAR_FIELD_COMPLETE_MSG:
128                     // send to provisioning device
129                     pb_gatt_packet_handler(PROVISIONING_DATA_PACKET, 0, packet+pos, pdu_segment_len);
130                     break;
131             }
132             break;
133 
134         case HCI_EVENT_PACKET:
135             switch (hci_event_packet_get_type(packet)) {
136                 case HCI_EVENT_MESH_META:
137                     switch (hci_event_mesh_meta_get_subevent_code(packet)){
138                         case MESH_SUBEVENT_PB_TRANSPORT_LINK_OPEN:
139                         case MESH_SUBEVENT_PB_TRANSPORT_LINK_CLOSED:
140                             // Forward link open/close
141                             pb_gatt_mtu = ATT_DEFAULT_MTU;
142                             pb_gatt_packet_handler(HCI_EVENT_PACKET, 0, packet, size);
143                             break;
144                         case MESH_SUBEVENT_CAN_SEND_NOW:
145                             con_handle = little_endian_read_16(packet, 3);
146                             if (con_handle == HCI_CON_HANDLE_INVALID) return;
147 
148                             sar_buffer.segmentation_buffer[0] = (segmentation_state << 6) | MESH_MSG_TYPE_PROVISIONING_PDU;
149                             pdu_segment_len = btstack_min(proxy_pdu_size - segmentation_offset, pb_gatt_mtu - 1);
150                             memcpy(&sar_buffer.segmentation_buffer[1], &proxy_pdu[segmentation_offset], pdu_segment_len);
151                             segmentation_offset += pdu_segment_len;
152 
153                             mesh_provisioning_service_server_send_proxy_pdu(con_handle, sar_buffer.segmentation_buffer, pdu_segment_len + 1);
154 
155                             switch (segmentation_state){
156                                 case MESH_MSG_SAR_FIELD_COMPLETE_MSG:
157                                 case MESH_MSG_SAR_FIELD_LAST_SEGMENT:
158                                     pb_gatt_emit_pdu_sent(0);
159                                     break;
160                                 case MESH_MSG_SAR_FIELD_CONTINUE:
161                                 case MESH_MSG_SAR_FIELD_FIRST_SEGMENT:
162                                     if ((proxy_pdu_size - segmentation_offset) > (pb_gatt_mtu - 1)){
163                                         segmentation_state = MESH_MSG_SAR_FIELD_CONTINUE;
164                                     } else {
165                                         segmentation_state = MESH_MSG_SAR_FIELD_LAST_SEGMENT;
166                                     }
167                                     mesh_provisioning_service_server_request_can_send_now(con_handle);
168                                     break;
169                             }
170                             break;
171                         default:
172                             break;
173                     }
174             }
175             break;
176         default:
177             break;
178     }
179 }
180 
181 /**
182  * Setup mesh provisioning service
183  * @param device_uuid
184  */
185 void pb_gatt_init(void){
186     // setup mesh provisioning service
187     mesh_provisioning_service_server_init();
188     mesh_provisioning_service_server_register_packet_handler(packet_handler);
189 }
190 
191 /**
192  * Register listener for Provisioning PDUs
193  */
194 void pb_gatt_register_packet_handler(btstack_packet_handler_t _packet_handler){
195     pb_gatt_packet_handler = _packet_handler;
196 }
197 
198 /**
199  * Send pdu
200  * @param con_handle
201  * @param pdu
202  * @param pdu_size
203  */
204 void pb_gatt_send_pdu(uint16_t con_handle, const uint8_t * pdu, uint16_t size){
205     if (!pdu || size <= 0) return;
206     if (con_handle == HCI_CON_HANDLE_INVALID) return;
207     // store pdu, request to send
208     proxy_pdu = pdu;
209     proxy_pdu_size = size;
210     segmentation_offset = 0;
211 
212     // check if segmentation is necessary
213     if (proxy_pdu_size > (pb_gatt_mtu - 1)){
214         segmentation_state = MESH_MSG_SAR_FIELD_FIRST_SEGMENT;
215     } else {
216         segmentation_state = MESH_MSG_SAR_FIELD_COMPLETE_MSG;
217     }
218     mesh_provisioning_service_server_request_can_send_now(con_handle);
219 }
220 /**
221  * Close Link
222  * @param con_handle
223  * @param reason 0 = success, 1 = timeout, 2 = fail
224  */
225 void pb_gatt_close_link(hci_con_handle_t con_handle, uint8_t reason){
226     UNUSED(con_handle);
227     UNUSED(reason);
228 }
229 
230 /**
231  * Setup Link with unprovisioned device
232  * @param device_uuid
233  * @returns con_handle or HCI_CON_HANDLE_INVALID
234  */
235 uint16_t pb_gatt_create_link(const uint8_t * device_uuid){
236     UNUSED(device_uuid);
237     return HCI_CON_HANDLE_INVALID;
238 }
239