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 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__ "ublox_spp_service_server.c" 39 40 /** 41 * Implementation of the ublox SPP-like profile 42 * 43 * To use with your application, add '#import <ublox_spp_service.gatt' to your .gatt file 44 * and call all functions below. All strings and blobs need to stay valid after calling the functions. 45 */ 46 47 #include "btstack_defines.h" 48 #include "btstack_event.h" 49 #include "btstack_debug.h" 50 #include "btstack_util.h" 51 #include "bluetooth_gatt.h" 52 #include "hci.h" 53 #include "ble/att_db.h" 54 #include "ble/att_server.h" 55 #include "ble/gatt-service/ublox_spp_service_server.h" 56 57 #define UBLOX_SPP_MAX_CREDITS 16 58 #define UBLOX_SPP_CREDITS_THRESHOLD 8 59 60 // 61 static const uint8_t ublox_spp_profile_uuid128[] = { 0x24, 0x56, 0xE1, 0xB9, 0x26, 0xE2, 0x8F, 0x83, 0xE7, 0x44, 0xF3, 0x4F, 0x01, 0xE9, 0xD7, 0x01 }; 62 static const uint8_t ublox_spp_fifo_uuid128[] = { 0x24, 0x56, 0xE1, 0xB9, 0x26, 0xE2, 0x8F, 0x83, 0xE7, 0x44, 0xF3, 0x4F, 0x01, 0xE9, 0xD7, 0x03 }; 63 static const uint8_t ublox_spp_credits_uuid128[] = { 0x24, 0x56, 0xE1, 0xB9, 0x26, 0xE2, 0x8F, 0x83, 0xE7, 0x44, 0xF3, 0x4F, 0x01, 0xE9, 0xD7, 0x04 }; 64 65 typedef struct { 66 hci_con_handle_t con_handle; 67 68 // characteristic: FIFO 69 uint16_t fifo_value_handle; 70 uint8_t data[20]; 71 72 // characteristic descriptor: Client Characteristic Configuration 73 uint16_t fifo_client_configuration_descriptor_handle; 74 uint16_t fifo_client_configuration_descriptor_value; // none, notify or indicate; 75 btstack_context_callback_registration_t fifo_callback; 76 77 // characteristic: Flow control/credits 78 uint16_t credits_value_handle; 79 uint16_t incoming_credits; 80 uint16_t outgoing_credits; 81 uint8_t delta_credits; 82 83 // characteristic descriptor: Client Characteristic Configuration 84 uint16_t credits_client_configuration_descriptor_handle; 85 uint16_t credits_client_configuration_descriptor_value; 86 87 btstack_context_callback_registration_t credits_callback; 88 89 void (*client_data_callback)(hci_con_handle_t con_handle, const uint8_t * data, uint16_t size); 90 void (*client_credits_callback)(hci_con_handle_t con_handle, uint16_t credits); 91 92 // flow control 93 btstack_context_callback_registration_t * request; 94 } ublox_spp_service_t; 95 96 static att_service_handler_t ublox_spp_service; 97 static ublox_spp_service_t ublox_spp; 98 99 static int ublox_spp_service_flow_control_enabled(ublox_spp_service_t * instance){ 100 return instance->credits_client_configuration_descriptor_value; 101 } 102 103 static ublox_spp_service_t * ublox_get_instance_for_con_handle(hci_con_handle_t con_handle){ 104 ublox_spp_service_t * instance = &ublox_spp; 105 if (con_handle == HCI_CON_HANDLE_INVALID) return NULL; 106 instance->con_handle = con_handle; 107 return instance; 108 } 109 110 static inline void ublox_spp_service_init_credits(ublox_spp_service_t * instance){ 111 instance->incoming_credits = 0; 112 instance->outgoing_credits = 0; 113 instance->delta_credits = UBLOX_SPP_MAX_CREDITS; 114 } 115 116 static uint16_t ublox_spp_service_read_callback(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){ 117 UNUSED(offset); 118 UNUSED(buffer_size); 119 ublox_spp_service_t * instance = ublox_get_instance_for_con_handle(con_handle); 120 if (!instance) return 0; 121 122 if (attribute_handle == instance->fifo_client_configuration_descriptor_handle){ 123 if (buffer){ 124 little_endian_store_16(buffer, 0, instance->fifo_client_configuration_descriptor_value); 125 } 126 return 2; 127 } 128 129 if (attribute_handle == instance->credits_client_configuration_descriptor_handle){ 130 if (buffer){ 131 little_endian_store_16(buffer, 0, instance->credits_client_configuration_descriptor_value); 132 } 133 return 2; 134 } 135 return 0; 136 } 137 138 139 static int ublox_spp_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){ 140 UNUSED(transaction_mode); 141 UNUSED(offset); 142 ublox_spp_service_t * instance = ublox_get_instance_for_con_handle(con_handle); 143 if (!instance) return 0; 144 145 if (attribute_handle == instance->fifo_value_handle){ 146 instance->client_data_callback(con_handle, &buffer[0], buffer_size); 147 if (!ublox_spp_service_flow_control_enabled(instance)) return 0; 148 if (!instance->incoming_credits) return 0; 149 instance->incoming_credits--; 150 if (instance->incoming_credits < UBLOX_SPP_CREDITS_THRESHOLD){ 151 att_server_request_to_send_notification(&instance->credits_callback, instance->con_handle); 152 } 153 } 154 155 if (attribute_handle == instance->fifo_client_configuration_descriptor_handle){ 156 if (buffer_size < 2){ 157 return ATT_ERROR_INVALID_OFFSET; 158 } 159 instance->fifo_client_configuration_descriptor_value = little_endian_read_16(buffer, 0); 160 instance->client_data_callback(con_handle, NULL, 0); 161 } 162 163 if (attribute_handle == instance->credits_value_handle){ 164 if (!ublox_spp_service_flow_control_enabled(instance)) return 0; 165 int8_t credits = (int8_t)buffer[0]; 166 if (credits <= 0) return 0; 167 instance->outgoing_credits += credits; 168 if (instance->request){ 169 btstack_context_callback_registration_t * request = instance->request; 170 instance->request = NULL; 171 att_server_request_to_send_notification(request, instance->con_handle); 172 } 173 } 174 175 if (attribute_handle == instance->credits_client_configuration_descriptor_handle){ 176 if (buffer_size < 2){ 177 return ATT_ERROR_INVALID_OFFSET; 178 } 179 instance->credits_client_configuration_descriptor_value = little_endian_read_16(buffer, 0); 180 181 ublox_spp_service_init_credits(instance); 182 if (instance->credits_client_configuration_descriptor_value){ 183 att_server_request_to_send_notification(&instance->credits_callback, instance->con_handle); 184 } 185 } 186 187 return 0; 188 } 189 190 static void ublox_spp_credits_callback(void * context){ 191 ublox_spp_service_t * instance = (ublox_spp_service_t *) context; 192 if (!instance) return; 193 194 instance->delta_credits = UBLOX_SPP_MAX_CREDITS - instance->incoming_credits; 195 if (instance->delta_credits){ 196 instance->incoming_credits = UBLOX_SPP_MAX_CREDITS; 197 att_server_notify(instance->con_handle, instance->credits_value_handle, &instance->delta_credits, 1); 198 } 199 } 200 /** 201 * @brief Init ublox SPP Service Server with ATT DB 202 * @param callback for tx data from peer 203 */ 204 void ublox_spp_service_server_init(void (*client_data_callback)(hci_con_handle_t con_handle, const uint8_t * data, uint16_t size), 205 void (*client_credits_callback)(hci_con_handle_t con_handle, uint16_t credits)){ 206 ublox_spp_service_t * instance = &ublox_spp; 207 instance->client_data_callback = client_data_callback; 208 instance->client_credits_callback = client_credits_callback; 209 210 instance->credits_callback.callback = ublox_spp_credits_callback; 211 instance->credits_callback.context = instance; 212 ublox_spp_service_init_credits(instance); 213 214 215 // get service handle range 216 uint16_t start_handle = 0; 217 uint16_t end_handle = 0xfff; 218 int service_found = gatt_server_get_get_handle_range_for_service_with_uuid128(ublox_spp_profile_uuid128, &start_handle, &end_handle); 219 if (!service_found) return; 220 221 // get characteristic value handle and client configuration handle 222 // FIFO 223 instance->fifo_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid128(start_handle, end_handle, ublox_spp_fifo_uuid128); 224 instance->fifo_client_configuration_descriptor_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid128(start_handle, end_handle, ublox_spp_fifo_uuid128); 225 // Credits 226 instance->credits_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid128(start_handle, end_handle, ublox_spp_credits_uuid128); 227 instance->credits_client_configuration_descriptor_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid128(start_handle, end_handle, ublox_spp_credits_uuid128); 228 229 log_info("FIFO value handle 0x%02x", instance->fifo_value_handle); 230 log_info("FIFO CCC value handle 0x%02x", instance->fifo_client_configuration_descriptor_handle); 231 log_info("Credits value handle 0x%02x", instance->credits_value_handle); 232 log_info("Credits CCC value handle 0x%02x", instance->credits_client_configuration_descriptor_handle); 233 234 // register service with ATT Server 235 ublox_spp_service.start_handle = start_handle; 236 ublox_spp_service.end_handle = end_handle; 237 ublox_spp_service.read_callback = &ublox_spp_service_read_callback; 238 ublox_spp_service.write_callback = &ublox_spp_service_write_callback; 239 att_server_register_service_handler(&ublox_spp_service); 240 } 241 242 /** 243 * @brief Queue send request. When called, one packet can be send via ublox_spp_service_send below 244 * @param request 245 * @param con_handle 246 */ 247 void ublox_spp_service_server_request_can_send_now(btstack_context_callback_registration_t * request, hci_con_handle_t con_handle){ 248 ublox_spp_service_t * instance = &ublox_spp; 249 if (!ublox_spp_service_flow_control_enabled(instance) || instance->outgoing_credits) { 250 att_server_request_to_send_notification(request, con_handle); 251 return; 252 } 253 instance->request = request; 254 } 255 256 /** 257 * @brief Send data 258 * @param con_handle 259 * @param data 260 * @param size 261 */ 262 int ublox_spp_service_server_send(hci_con_handle_t con_handle, const uint8_t * data, uint16_t size){ 263 ublox_spp_service_t * instance = &ublox_spp; 264 if (ublox_spp_service_flow_control_enabled(instance)){ 265 if (instance->outgoing_credits > 0){ 266 instance->outgoing_credits--; 267 } 268 } 269 return att_server_notify(con_handle, instance->fifo_value_handle, &data[0], size); 270 } 271 272