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__ "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 62 typedef struct { 63 hci_con_handle_t con_handle; 64 65 // characteristic: FIFO 66 uint16_t fifo_value_handle; 67 uint8_t data[20]; 68 69 // characteristic descriptor: Client Characteristic Configuration 70 uint16_t fifo_client_configuration_descriptor_handle; 71 uint16_t fifo_client_configuration_descriptor_value; // none, notify or indicate; 72 btstack_context_callback_registration_t fifo_callback; 73 74 // characteristic: Flow control/credits 75 uint16_t credits_value_handle; 76 uint16_t incoming_credits; 77 uint16_t outgoing_credits; 78 uint8_t delta_credits; 79 80 // characteristic descriptor: Client Characteristic Configuration 81 uint16_t credits_client_configuration_descriptor_handle; 82 uint16_t credits_client_configuration_descriptor_value; 83 84 btstack_context_callback_registration_t credits_callback; 85 86 btstack_packet_handler_t client_packet_handler; 87 88 // flow control 89 btstack_context_callback_registration_t * request; 90 } ublox_spp_service_t; 91 92 static att_service_handler_t ublox_spp_service; 93 static ublox_spp_service_t ublox_spp; 94 95 static void ublox_spp_service_emit_state(ublox_spp_service_t * instance, bool enabled){ 96 uint8_t event[5]; 97 uint8_t pos = 0; 98 event[pos++] = HCI_EVENT_GATTSERVICE_META; 99 event[pos++] = sizeof(event) - 2; 100 event[pos++] = enabled ? GATTSERVICE_SUBEVENT_SPP_SERVICE_CONNECTED : GATTSERVICE_SUBEVENT_SPP_SERVICE_DISCONNECTED; 101 little_endian_store_16(event,pos, instance->con_handle); 102 pos += 2; 103 (*instance->client_packet_handler)(HCI_EVENT_PACKET, 0, event, pos); 104 } 105 106 static int ublox_spp_service_flow_control_enabled(ublox_spp_service_t * instance){ 107 return instance->credits_client_configuration_descriptor_value; 108 } 109 110 static ublox_spp_service_t * ublox_get_instance_for_con_handle(hci_con_handle_t con_handle){ 111 ublox_spp_service_t * instance = &ublox_spp; 112 if (con_handle == HCI_CON_HANDLE_INVALID) return NULL; 113 instance->con_handle = con_handle; 114 return instance; 115 } 116 117 static inline void ublox_spp_service_init_credits(ublox_spp_service_t * instance){ 118 instance->incoming_credits = 0; 119 instance->outgoing_credits = 0; 120 instance->delta_credits = UBLOX_SPP_MAX_CREDITS; 121 } 122 123 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){ 124 UNUSED(offset); 125 UNUSED(buffer_size); 126 ublox_spp_service_t * instance = ublox_get_instance_for_con_handle(con_handle); 127 if (!instance) return 0; 128 129 if (attribute_handle == instance->fifo_client_configuration_descriptor_handle){ 130 if (buffer != NULL){ 131 little_endian_store_16(buffer, 0, instance->fifo_client_configuration_descriptor_value); 132 } 133 return 2; 134 } 135 136 if (attribute_handle == instance->credits_client_configuration_descriptor_handle){ 137 if (buffer != NULL){ 138 little_endian_store_16(buffer, 0, instance->credits_client_configuration_descriptor_value); 139 } 140 return 2; 141 } 142 return 0; 143 } 144 145 146 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){ 147 UNUSED(transaction_mode); 148 UNUSED(offset); 149 ublox_spp_service_t * instance = ublox_get_instance_for_con_handle(con_handle); 150 if (!instance) return 0; 151 152 if (attribute_handle == instance->fifo_value_handle){ 153 instance->client_packet_handler(RFCOMM_DATA_PACKET, (uint16_t) con_handle, &buffer[0], buffer_size); 154 if (!ublox_spp_service_flow_control_enabled(instance)) return 0; 155 if (!instance->incoming_credits) return 0; 156 instance->incoming_credits--; 157 if (instance->incoming_credits < UBLOX_SPP_CREDITS_THRESHOLD){ 158 att_server_request_to_send_notification(&instance->credits_callback, instance->con_handle); 159 } 160 } 161 162 if (attribute_handle == instance->fifo_client_configuration_descriptor_handle){ 163 if (buffer_size < 2u){ 164 return ATT_ERROR_INVALID_OFFSET; 165 } 166 instance->fifo_client_configuration_descriptor_value = little_endian_read_16(buffer, 0); 167 log_info("ublox spp service FIFO control: %d", instance->fifo_client_configuration_descriptor_value); 168 ublox_spp_service_emit_state(instance, instance->fifo_client_configuration_descriptor_value != 0); 169 } 170 171 if (attribute_handle == instance->credits_value_handle){ 172 if (!ublox_spp_service_flow_control_enabled(instance)) return 0; 173 int8_t credits = (int8_t)buffer[0]; 174 if (credits <= 0) return 0; 175 instance->outgoing_credits += credits; 176 log_info("received outgoing credits, total %d", instance->outgoing_credits); 177 // Provide credits 178 att_server_request_to_send_notification(&instance->credits_callback, instance->con_handle); 179 180 // handle user request 181 if (instance->request){ 182 btstack_context_callback_registration_t * request = instance->request; 183 instance->request = NULL; 184 att_server_request_to_send_notification(request, instance->con_handle); 185 } 186 } 187 188 if (attribute_handle == instance->credits_client_configuration_descriptor_handle){ 189 if (buffer_size < 2u){ 190 return ATT_ERROR_INVALID_OFFSET; 191 } 192 instance->credits_client_configuration_descriptor_value = little_endian_read_16(buffer, 0); 193 log_info("ublox spp service Credits control: %d", instance->credits_client_configuration_descriptor_value); 194 ublox_spp_service_init_credits(instance); 195 } 196 197 return 0; 198 } 199 200 static void ublox_spp_credits_callback(void * context){ 201 ublox_spp_service_t * instance = (ublox_spp_service_t *) context; 202 if (!instance) return; 203 204 instance->delta_credits = UBLOX_SPP_MAX_CREDITS - instance->incoming_credits; 205 if (instance->delta_credits){ 206 instance->incoming_credits = UBLOX_SPP_MAX_CREDITS; 207 att_server_notify(instance->con_handle, instance->credits_value_handle, &instance->delta_credits, 1); 208 } 209 } 210 /** 211 * @brief Init ublox SPP Service Server with ATT DB 212 * @param callback for tx data from peer 213 */ 214 void ublox_spp_service_server_init(btstack_packet_handler_t packet_handler){ 215 216 static const uint8_t ublox_spp_profile_uuid128[] = { 0x24, 0x56, 0xE1, 0xB9, 0x26, 0xE2, 0x8F, 0x83, 0xE7, 0x44, 0xF3, 0x4F, 0x01, 0xE9, 0xD7, 0x01 }; 217 static const uint8_t ublox_spp_fifo_uuid128[] = { 0x24, 0x56, 0xE1, 0xB9, 0x26, 0xE2, 0x8F, 0x83, 0xE7, 0x44, 0xF3, 0x4F, 0x01, 0xE9, 0xD7, 0x03 }; 218 static const uint8_t ublox_spp_credits_uuid128[] = { 0x24, 0x56, 0xE1, 0xB9, 0x26, 0xE2, 0x8F, 0x83, 0xE7, 0x44, 0xF3, 0x4F, 0x01, 0xE9, 0xD7, 0x04 }; 219 220 ublox_spp_service_t * instance = &ublox_spp; 221 instance->client_packet_handler = packet_handler; 222 223 instance->credits_callback.callback = ublox_spp_credits_callback; 224 instance->credits_callback.context = instance; 225 ublox_spp_service_init_credits(instance); 226 227 228 // get service handle range 229 uint16_t start_handle = 0; 230 uint16_t end_handle = 0xffff; 231 int service_found = gatt_server_get_handle_range_for_service_with_uuid128(ublox_spp_profile_uuid128, &start_handle, &end_handle); 232 btstack_assert(service_found != 0); 233 UNUSED(service_found); 234 235 // get characteristic value handle and client configuration handle 236 // FIFO 237 instance->fifo_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid128(start_handle, end_handle, ublox_spp_fifo_uuid128); 238 instance->fifo_client_configuration_descriptor_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid128(start_handle, end_handle, ublox_spp_fifo_uuid128); 239 // Credits 240 instance->credits_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid128(start_handle, end_handle, ublox_spp_credits_uuid128); 241 instance->credits_client_configuration_descriptor_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid128(start_handle, end_handle, ublox_spp_credits_uuid128); 242 243 log_info("FIFO value handle 0x%02x", instance->fifo_value_handle); 244 log_info("FIFO CCC value handle 0x%02x", instance->fifo_client_configuration_descriptor_handle); 245 log_info("Credits value handle 0x%02x", instance->credits_value_handle); 246 log_info("Credits CCC value handle 0x%02x", instance->credits_client_configuration_descriptor_handle); 247 248 // register service with ATT Server 249 ublox_spp_service.start_handle = start_handle; 250 ublox_spp_service.end_handle = end_handle; 251 ublox_spp_service.read_callback = &ublox_spp_service_read_callback; 252 ublox_spp_service.write_callback = &ublox_spp_service_write_callback; 253 att_server_register_service_handler(&ublox_spp_service); 254 } 255 256 /** 257 * @brief Queue send request. When called, one packet can be send via ublox_spp_service_send below 258 * @param request 259 * @param con_handle 260 */ 261 void ublox_spp_service_server_request_can_send_now(btstack_context_callback_registration_t * request, hci_con_handle_t con_handle){ 262 ublox_spp_service_t * instance = &ublox_spp; 263 if (!ublox_spp_service_flow_control_enabled(instance) || instance->outgoing_credits) { 264 att_server_request_to_send_notification(request, con_handle); 265 return; 266 } 267 instance->request = request; 268 } 269 270 /** 271 * @brief Send data 272 * @param con_handle 273 * @param data 274 * @param size 275 */ 276 int ublox_spp_service_server_send(hci_con_handle_t con_handle, const uint8_t * data, uint16_t size){ 277 ublox_spp_service_t * instance = &ublox_spp; 278 if (ublox_spp_service_flow_control_enabled(instance)){ 279 if (instance->outgoing_credits > 0u){ 280 instance->outgoing_credits--; 281 } 282 } 283 return att_server_notify(con_handle, instance->fifo_value_handle, &data[0], size); 284 } 285 286