1 /*
2 * Copyright (C) 2018 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 BLUEKITCHEN
24 * GMBH 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__ "mesh_provisioning_service_server.c"
39
40 #include "mesh/gatt-service/mesh_provisioning_service_server.h"
41
42 #include "ble/att_db.h"
43 #include "ble/att_server.h"
44 #include "bluetooth.h"
45 #include "bluetooth_gatt.h"
46 #include "btstack_debug.h"
47 #include "btstack_event.h"
48 #include "btstack_defines.h"
49 #include "btstack_event.h"
50 #include "btstack_util.h"
51 #include "hci.h"
52
53 #include "mesh/provisioning.h"
54
55 typedef struct {
56 hci_con_handle_t con_handle;
57
58 uint16_t data_in_client_value_handle;
59 uint8_t data_in_proxy_pdu[MESH_PROV_MAX_PROXY_PDU];
60 uint8_t link_open;
61
62 // Mesh Provisioning Data Out
63 uint16_t data_out_client_value_handle;
64 uint8_t data_out_proxy_pdu[MESH_PROV_MAX_PROXY_PDU];
65 uint16_t data_out_proxy_pdu_size;
66
67 // Mesh Provisioning Data Out Notification
68 uint16_t data_out_client_configuration_descriptor_handle;
69 uint16_t data_out_client_configuration_descriptor_value;
70 btstack_context_callback_registration_t data_out_notify_callback;
71
72 btstack_context_callback_registration_t pdu_response_callback;
73
74 } mesh_provisioning_t;
75
76 static btstack_packet_handler_t mesh_provisioning_service_packet_handler;
77 static att_service_handler_t mesh_provisioning_service;
78 static mesh_provisioning_t mesh_provisioning;
79
80
mesh_provisioning_service_emit_link_open(hci_con_handle_t con_handle,uint8_t status)81 static void mesh_provisioning_service_emit_link_open(hci_con_handle_t con_handle, uint8_t status){
82 uint8_t event[7] = { HCI_EVENT_MESH_META, 5, MESH_SUBEVENT_PB_TRANSPORT_LINK_OPEN, status};
83 little_endian_store_16(event, 4, con_handle);
84 event[6] = MESH_PB_TYPE_GATT;
85 mesh_provisioning_service_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event));
86 }
87
mesh_provisioning_service_emit_link_close(hci_con_handle_t con_handle,uint8_t reason)88 static void mesh_provisioning_service_emit_link_close(hci_con_handle_t con_handle, uint8_t reason){
89 uint8_t event[6] = { HCI_EVENT_MESH_META, 3, MESH_SUBEVENT_PB_TRANSPORT_LINK_CLOSED};
90 little_endian_store_16(event, 3, con_handle);
91 event[5] = reason;
92 mesh_provisioning_service_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event));
93 }
94
mesh_provisioning_service_get_instance_for_con_handle(hci_con_handle_t con_handle)95 static mesh_provisioning_t * mesh_provisioning_service_get_instance_for_con_handle(hci_con_handle_t con_handle){
96 mesh_provisioning_t * instance = &mesh_provisioning;
97 log_debug("mesh_provisioning_service_get_instance_for_con_handle, handle %x (instance %x)", con_handle, instance->con_handle);
98 if (instance->con_handle != HCI_CON_HANDLE_INVALID && instance->con_handle != con_handle) return NULL;
99 instance->con_handle = con_handle;
100 return instance;
101 }
102
mesh_provisioning_service_read_callback(hci_con_handle_t con_handle,uint16_t attribute_handle,uint16_t offset,uint8_t * buffer,uint16_t buffer_size)103 static uint16_t mesh_provisioning_service_read_callback(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
104 UNUSED(con_handle);
105 UNUSED(attribute_handle);
106 UNUSED(offset);
107 UNUSED(buffer_size);
108
109 mesh_provisioning_t * instance = mesh_provisioning_service_get_instance_for_con_handle(con_handle);
110 if (!instance){
111 log_error("mesh_provisioning_service_read_callback: instance is null");
112 return 0;
113 }
114 if (attribute_handle == instance->data_out_client_configuration_descriptor_handle){
115 if (buffer && buffer_size >= 2){
116 little_endian_store_16(buffer, 0, instance->data_out_client_configuration_descriptor_value);
117 }
118 return 2;
119 }
120 log_info("mesh_provisioning_service_read_callback: not handled read on handle 0x%02x", attribute_handle);
121 return 0;
122 }
123
mesh_provisioning_service_write_callback(hci_con_handle_t con_handle,uint16_t attribute_handle,uint16_t transaction_mode,uint16_t offset,uint8_t * buffer,uint16_t buffer_size)124 static int mesh_provisioning_service_write_callback(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size){
125 UNUSED(transaction_mode);
126 UNUSED(offset);
127 UNUSED(buffer_size);
128
129 mesh_provisioning_t * instance = mesh_provisioning_service_get_instance_for_con_handle(con_handle);
130 if (!instance){
131 log_error("mesh_provisioning_service_write_callback: instance is null");
132 return 0;
133 }
134
135 if (attribute_handle == instance->data_in_client_value_handle){
136 if (!mesh_provisioning_service_packet_handler) return 0;
137 mesh_msg_type_t msg_type = buffer[0] & 0x3f;
138 switch (msg_type){
139 case MESH_MSG_TYPE_PROXY_CONFIGURATION:
140 log_info("data_in_client_value_handle not handled, msg_type %d", msg_type);
141 break;
142 case MESH_MSG_TYPE_PROVISIONING_PDU:
143 (*mesh_provisioning_service_packet_handler)(PROVISIONING_DATA_PACKET, con_handle, buffer, buffer_size);
144 break;
145 default:
146 log_info("data_in_client_value_handle not written, wrong msg_type %d", msg_type);
147 return ATT_ERROR_REQUEST_NOT_SUPPORTED;
148 }
149 return 0;
150 }
151
152 if (attribute_handle == instance->data_out_client_configuration_descriptor_handle){
153 if (buffer_size < 2){
154 return ATT_ERROR_INVALID_OFFSET;
155 }
156 uint16_t enable_data_out_notify = little_endian_read_16(buffer, 0);
157 if (enable_data_out_notify){
158 if (instance->data_out_client_configuration_descriptor_value) return 0;
159 instance->data_out_client_configuration_descriptor_value = enable_data_out_notify;
160 instance->link_open = 1;
161 mesh_provisioning_service_emit_link_open(con_handle, 0);
162 } else {
163 if (!instance->data_out_client_configuration_descriptor_value) return 0;
164 instance->data_out_client_configuration_descriptor_value = enable_data_out_notify;
165 instance->link_open = 1;
166 mesh_provisioning_service_emit_link_open(con_handle, 0);
167 }
168 log_info("mesh_provisioning_service_write_callback: enable_data_out_notify %d, con handle 0x%02x", enable_data_out_notify, con_handle);
169 return 0;
170 }
171 log_info("mesh_provisioning_service_write_callback: not handled write on handle 0x%02x, buffer size %d", attribute_handle, buffer_size);
172 return 0;
173 }
174
mesh_provisioning_service_server_packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)175 static void mesh_provisioning_service_server_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
176 UNUSED(channel);
177 UNUSED(size);
178 if (packet_type != HCI_EVENT_PACKET) return;
179 if (hci_event_packet_get_type(packet) != HCI_EVENT_DISCONNECTION_COMPLETE) return;
180 hci_con_handle_t con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
181 mesh_provisioning_t * instance = mesh_provisioning_service_get_instance_for_con_handle(con_handle);
182 if (!instance) return;
183 if (instance->link_open){
184 // emit close, free instance
185 instance->link_open = 0;
186 instance->con_handle = HCI_CON_HANDLE_INVALID;
187 instance->data_out_client_configuration_descriptor_value = 0;
188 mesh_provisioning_service_emit_link_close(con_handle, 0);
189 }
190 }
191
mesh_provisioning_service_server_init(void)192 void mesh_provisioning_service_server_init(void){
193 mesh_provisioning_t * instance = &mesh_provisioning;
194 instance->con_handle = HCI_CON_HANDLE_INVALID;
195
196 // get service handle range
197 uint16_t start_handle = 0;
198 uint16_t end_handle = 0xffff;
199 int service_found = gatt_server_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_MESH_PROVISIONING, &start_handle, &end_handle);
200 btstack_assert(service_found != 0);
201 UNUSED(service_found);
202
203 instance->data_in_client_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_MESH_PROVISIONING_DATA_IN);
204 instance->data_out_client_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_MESH_PROVISIONING_DATA_OUT);
205 instance->data_out_client_configuration_descriptor_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_MESH_PROVISIONING_DATA_OUT);
206
207 log_info("DataIn value handle 0x%02x", instance->data_in_client_value_handle);
208 log_info("DataOut value handle 0x%02x", instance->data_out_client_value_handle);
209 log_info("DataOut CC value handle 0x%02x", instance->data_out_client_configuration_descriptor_handle);
210
211 mesh_provisioning_service.start_handle = start_handle;
212 mesh_provisioning_service.end_handle = end_handle;
213 mesh_provisioning_service.read_callback = &mesh_provisioning_service_read_callback;
214 mesh_provisioning_service.write_callback = &mesh_provisioning_service_write_callback;
215 mesh_provisioning_service.packet_handler = &mesh_provisioning_service_server_packet_handler;
216
217 att_server_register_service_handler(&mesh_provisioning_service);
218 }
219
mesh_provisioning_service_server_send_proxy_pdu(uint16_t con_handle,const uint8_t * proxy_pdu,uint16_t proxy_pdu_size)220 void mesh_provisioning_service_server_send_proxy_pdu(uint16_t con_handle, const uint8_t * proxy_pdu, uint16_t proxy_pdu_size){
221 mesh_provisioning_t * instance = mesh_provisioning_service_get_instance_for_con_handle(con_handle);
222 if (!instance){
223 log_error("mesh_provisioning_service_server_data_out_can_send_now: instance is null");
224 return;
225 }
226 att_server_notify(instance->con_handle, instance->data_out_client_value_handle, proxy_pdu, proxy_pdu_size);
227 }
228
mesh_provisioning_service_can_send_now(void * context)229 static void mesh_provisioning_service_can_send_now(void * context){
230 hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
231 // notify client
232 mesh_provisioning_t * instance = mesh_provisioning_service_get_instance_for_con_handle(con_handle);
233 if (!instance){
234 log_error("no instance for handle 0x%02x", con_handle);
235 return;
236 }
237
238 if (!mesh_provisioning_service_packet_handler) return;
239 uint8_t buffer[5];
240 buffer[0] = HCI_EVENT_MESH_META;
241 buffer[1] = 3;
242 buffer[2] = MESH_SUBEVENT_CAN_SEND_NOW;
243 little_endian_store_16(buffer, 3, (uint16_t) con_handle);
244 (*mesh_provisioning_service_packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
245 }
246
mesh_provisioning_service_server_request_can_send_now(hci_con_handle_t con_handle)247 void mesh_provisioning_service_server_request_can_send_now(hci_con_handle_t con_handle){
248 mesh_provisioning_t * instance = mesh_provisioning_service_get_instance_for_con_handle(con_handle);
249 if (!instance){
250 log_error("mesh_provisioning_service_server_request_can_send_now: instance is null, 0x%2x", con_handle);
251 return;
252 }
253 instance->pdu_response_callback.callback = &mesh_provisioning_service_can_send_now;
254 instance->pdu_response_callback.context = (void*) (uintptr_t) con_handle;
255 att_server_register_can_send_now_callback(&instance->pdu_response_callback, con_handle);
256 }
257
mesh_provisioning_service_server_register_packet_handler(btstack_packet_handler_t callback)258 void mesh_provisioning_service_server_register_packet_handler(btstack_packet_handler_t callback){
259 mesh_provisioning_service_packet_handler = callback;
260 }
261