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_proxy_service_server.c" 39 40 #include "mesh/gatt-service/mesh_proxy_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_defines.h" 48 #include "btstack_event.h" 49 #include "btstack_util.h" 50 #include "hci.h" 51 52 #include "mesh/provisioning.h" 53 54 typedef struct { 55 hci_con_handle_t con_handle; 56 57 uint16_t data_in_client_value_handle; 58 uint8_t data_in_proxy_pdu[MESH_PROV_MAX_PROXY_PDU]; 59 60 // Mesh Provisioning Data Out 61 uint16_t data_out_client_value_handle; 62 uint8_t data_out_proxy_pdu[MESH_PROV_MAX_PROXY_PDU]; 63 uint16_t data_out_proxy_pdu_size; 64 65 // Mesh Provisioning Data Out Notification 66 uint16_t data_out_client_configuration_descriptor_handle; 67 uint16_t data_out_client_configuration_descriptor_value; 68 btstack_context_callback_registration_t data_out_notify_callback; 69 70 btstack_context_callback_registration_t pdu_response_callback; 71 } mesh_proxy_t; 72 73 static btstack_packet_handler_t mesh_proxy_service_packet_handler; 74 static att_service_handler_t mesh_proxy_service; 75 static mesh_proxy_t mesh_proxy; 76 77 78 static void mesh_proxy_service_emit_connected(hci_con_handle_t con_handle){ 79 uint8_t event[5] = { HCI_EVENT_MESH_META, 3, MESH_SUBEVENT_PROXY_CONNECTED}; 80 little_endian_store_16(event, 4, con_handle); 81 mesh_proxy_service_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event)); 82 } 83 84 static void mesh_proxy_service_emit_disconnected(hci_con_handle_t con_handle){ 85 uint8_t event[5] = { HCI_EVENT_MESH_META, 3, MESH_SUBEVENT_PROXY_DISCONNECTED}; 86 little_endian_store_16(event, 4, con_handle); 87 mesh_proxy_service_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event)); 88 } 89 90 static mesh_proxy_t * mesh_proxy_service_get_instance_for_con_handle(hci_con_handle_t con_handle){ 91 mesh_proxy_t * instance = &mesh_proxy; 92 if (con_handle == HCI_CON_HANDLE_INVALID) return NULL; 93 instance->con_handle = con_handle; 94 return instance; 95 } 96 97 static uint16_t mesh_proxy_service_read_callback(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){ 98 UNUSED(con_handle); 99 UNUSED(attribute_handle); 100 UNUSED(offset); 101 UNUSED(buffer_size); 102 103 mesh_proxy_t * instance = mesh_proxy_service_get_instance_for_con_handle(con_handle); 104 if (!instance){ 105 log_error("mesh_proxy_service_read_callback: instance is null"); 106 return 0; 107 } 108 if (attribute_handle == instance->data_out_client_configuration_descriptor_handle){ 109 if (buffer && buffer_size >= 2){ 110 little_endian_store_16(buffer, 0, instance->data_out_client_configuration_descriptor_value); 111 } 112 return 2; 113 } 114 log_info("mesh_proxy_service_read_callback: not handled read on handle 0x%02x", attribute_handle); 115 return 0; 116 } 117 118 static int mesh_proxy_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){ 119 UNUSED(transaction_mode); 120 UNUSED(offset); 121 UNUSED(buffer_size); 122 123 mesh_proxy_t * instance = mesh_proxy_service_get_instance_for_con_handle(con_handle); 124 if (!instance){ 125 log_error("mesh_proxy_service_write_callback: instance is null"); 126 return 0; 127 } 128 129 if (attribute_handle == instance->data_in_client_value_handle){ 130 if (!mesh_proxy_service_packet_handler) return 0; 131 (*mesh_proxy_service_packet_handler)(MESH_PROXY_DATA_PACKET, con_handle, buffer, buffer_size); 132 return 0; 133 } 134 135 if (attribute_handle == instance->data_out_client_configuration_descriptor_handle){ 136 if (buffer_size < 2){ 137 return ATT_ERROR_INVALID_OFFSET; 138 } 139 instance->data_out_client_configuration_descriptor_value = little_endian_read_16(buffer, 0); 140 log_info("mesh_proxy_service_write_callback: data out notify enabled %d, con handle 0x%02x", instance->data_out_client_configuration_descriptor_value, con_handle); 141 if (instance->data_out_client_configuration_descriptor_value){ 142 mesh_proxy_service_emit_connected(con_handle); 143 } else { 144 mesh_proxy_service_emit_disconnected(con_handle); 145 } 146 return 0; 147 } 148 log_info("mesh_proxy_service_write_callback: not handled write on handle 0x%02x, buffer size %d", attribute_handle, buffer_size); 149 return 0; 150 } 151 152 void mesh_proxy_service_server_init(void){ 153 mesh_proxy_t * instance = &mesh_proxy; 154 if (!instance){ 155 log_error("mesh_proxy_service_server_init: instance is null"); 156 return; 157 } 158 159 // get service handle range 160 uint16_t start_handle = 0; 161 uint16_t end_handle = 0xffff; 162 int service_found = gatt_server_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_MESH_PROXY, &start_handle, &end_handle); 163 btstack_assert(service_found != 0); 164 UNUSED(service_found); 165 166 instance->data_in_client_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_MESH_PROXY_DATA_IN); 167 instance->data_out_client_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_MESH_PROXY_DATA_OUT); 168 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_PROXY_DATA_OUT); 169 170 log_info("DataIn value handle 0x%02x", instance->data_in_client_value_handle); 171 log_info("DataOut value handle 0x%02x", instance->data_out_client_value_handle); 172 log_info("DataOut CC value handle 0x%02x", instance->data_out_client_configuration_descriptor_handle); 173 174 mesh_proxy_service.start_handle = start_handle; 175 mesh_proxy_service.end_handle = end_handle; 176 mesh_proxy_service.read_callback = &mesh_proxy_service_read_callback; 177 mesh_proxy_service.write_callback = &mesh_proxy_service_write_callback; 178 179 att_server_register_service_handler(&mesh_proxy_service); 180 } 181 182 void mesh_proxy_service_server_send_proxy_pdu(uint16_t con_handle, const uint8_t * proxy_pdu, uint16_t proxy_pdu_size){ 183 mesh_proxy_t * instance = mesh_proxy_service_get_instance_for_con_handle(con_handle); 184 if (!instance){ 185 log_error("mesh_proxy_service_server_data_out_can_send_now: instance is null"); 186 return; 187 } 188 att_server_notify(instance->con_handle, instance->data_out_client_value_handle, proxy_pdu, proxy_pdu_size); 189 } 190 191 static void mesh_proxy_service_can_send_now(void * context){ 192 hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context; 193 // notify client 194 mesh_proxy_t * instance = mesh_proxy_service_get_instance_for_con_handle(con_handle); 195 if (!instance){ 196 log_error("no instance for handle 0x%02x", con_handle); 197 return; 198 } 199 200 if (!mesh_proxy_service_packet_handler) return; 201 uint8_t buffer[5]; 202 buffer[0] = HCI_EVENT_MESH_META; 203 buffer[1] = 3; 204 buffer[2] = MESH_SUBEVENT_CAN_SEND_NOW; 205 little_endian_store_16(buffer, 3, (uint16_t) con_handle); 206 (*mesh_proxy_service_packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer)); 207 } 208 209 void mesh_proxy_service_server_request_can_send_now(hci_con_handle_t con_handle){ 210 mesh_proxy_t * instance = mesh_proxy_service_get_instance_for_con_handle(con_handle); 211 if (!instance){ 212 log_error("mesh_proxy_service_server_request_can_send_now: instance is null, 0x%2x", con_handle); 213 return; 214 } 215 instance->pdu_response_callback.callback = &mesh_proxy_service_can_send_now; 216 instance->pdu_response_callback.context = (void*) (uintptr_t) con_handle; 217 att_server_register_can_send_now_callback(&instance->pdu_response_callback, con_handle); 218 } 219 220 void mesh_proxy_service_server_register_packet_handler(btstack_packet_handler_t callback){ 221 mesh_proxy_service_packet_handler = callback; 222 } 223