xref: /btstack/src/ble/gatt-service/ublox_spp_service_server.c (revision 98451c7b102094e15e0a72ca2f7098d91aed2017)
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 
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     void (*client_data_callback)(hci_con_handle_t con_handle, const uint8_t * data, uint16_t size);
87     void (*client_credits_callback)(hci_con_handle_t con_handle, uint16_t credits);
88 
89     // flow control
90     btstack_context_callback_registration_t * request;
91 } ublox_spp_service_t;
92 
93 static att_service_handler_t  ublox_spp_service;
94 static ublox_spp_service_t    ublox_spp;
95 
96 static int ublox_spp_service_flow_control_enabled(ublox_spp_service_t * instance){
97     return instance->credits_client_configuration_descriptor_value;
98 }
99 
100 static ublox_spp_service_t * ublox_get_instance_for_con_handle(hci_con_handle_t con_handle){
101     ublox_spp_service_t * instance = &ublox_spp;
102     if (con_handle == HCI_CON_HANDLE_INVALID) return NULL;
103     instance->con_handle = con_handle;
104     return instance;
105 }
106 
107 static inline void ublox_spp_service_init_credits(ublox_spp_service_t * instance){
108     instance->incoming_credits = 0;
109     instance->outgoing_credits = 0;
110     instance->delta_credits = UBLOX_SPP_MAX_CREDITS;
111 }
112 
113 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){
114     UNUSED(offset);
115     UNUSED(buffer_size);
116     ublox_spp_service_t * instance = ublox_get_instance_for_con_handle(con_handle);
117     if (!instance) return 0;
118 
119     if (attribute_handle == instance->fifo_client_configuration_descriptor_handle){
120         if (buffer != NULL){
121             little_endian_store_16(buffer, 0, instance->fifo_client_configuration_descriptor_value);
122         }
123         return 2;
124     }
125 
126     if (attribute_handle == instance->credits_client_configuration_descriptor_handle){
127         if (buffer != NULL){
128             little_endian_store_16(buffer, 0, instance->credits_client_configuration_descriptor_value);
129         }
130         return 2;
131     }
132     return 0;
133 }
134 
135 
136 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){
137     UNUSED(transaction_mode);
138     UNUSED(offset);
139     ublox_spp_service_t * instance = ublox_get_instance_for_con_handle(con_handle);
140     if (!instance) return 0;
141 
142     if (attribute_handle == instance->fifo_value_handle){
143         instance->client_data_callback(con_handle, &buffer[0], buffer_size);
144         if (!ublox_spp_service_flow_control_enabled(instance)) return 0;
145         if (!instance->incoming_credits) return 0;
146         instance->incoming_credits--;
147         if (instance->incoming_credits < UBLOX_SPP_CREDITS_THRESHOLD){
148             att_server_request_to_send_notification(&instance->credits_callback, instance->con_handle);
149         }
150     }
151 
152     if (attribute_handle == instance->fifo_client_configuration_descriptor_handle){
153         if (buffer_size < 2u){
154             return ATT_ERROR_INVALID_OFFSET;
155         }
156         instance->fifo_client_configuration_descriptor_value = little_endian_read_16(buffer, 0);
157         log_info("ublox spp service FIFO control: %d", instance->fifo_client_configuration_descriptor_value);
158         instance->client_data_callback(con_handle, NULL, 0);
159     }
160 
161     if (attribute_handle == instance->credits_value_handle){
162         if (!ublox_spp_service_flow_control_enabled(instance)) return 0;
163         int8_t credits = (int8_t)buffer[0];
164         if (credits <= 0) return 0;
165         instance->outgoing_credits += credits;
166         log_info("received outgoing credits, total %d", instance->outgoing_credits);
167         // Provide credits
168         att_server_request_to_send_notification(&instance->credits_callback, instance->con_handle);
169 
170         // handle user request
171         if (instance->request){
172             btstack_context_callback_registration_t * request = instance->request;
173             instance->request = NULL;
174             att_server_request_to_send_notification(request, instance->con_handle);
175         }
176     }
177 
178     if (attribute_handle == instance->credits_client_configuration_descriptor_handle){
179         if (buffer_size < 2u){
180             return ATT_ERROR_INVALID_OFFSET;
181         }
182         instance->credits_client_configuration_descriptor_value = little_endian_read_16(buffer, 0);
183         log_info("ublox spp service Credits control: %d", instance->credits_client_configuration_descriptor_value);
184         ublox_spp_service_init_credits(instance);
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 
207     static const uint8_t ublox_spp_profile_uuid128[] = { 0x24, 0x56, 0xE1, 0xB9, 0x26, 0xE2, 0x8F, 0x83, 0xE7, 0x44, 0xF3, 0x4F, 0x01, 0xE9, 0xD7, 0x01 };
208     static const uint8_t ublox_spp_fifo_uuid128[]    = { 0x24, 0x56, 0xE1, 0xB9, 0x26, 0xE2, 0x8F, 0x83, 0xE7, 0x44, 0xF3, 0x4F, 0x01, 0xE9, 0xD7, 0x03 };
209     static const uint8_t ublox_spp_credits_uuid128[] = { 0x24, 0x56, 0xE1, 0xB9, 0x26, 0xE2, 0x8F, 0x83, 0xE7, 0x44, 0xF3, 0x4F, 0x01, 0xE9, 0xD7, 0x04 };
210 
211     ublox_spp_service_t * instance = &ublox_spp;
212     instance->client_data_callback = client_data_callback;
213     instance->client_credits_callback = client_credits_callback;
214 
215     instance->credits_callback.callback = ublox_spp_credits_callback;
216     instance->credits_callback.context = instance;
217     ublox_spp_service_init_credits(instance);
218 
219 
220     // get service handle range
221     uint16_t start_handle = 0;
222     uint16_t end_handle   = 0xfff;
223     int service_found = gatt_server_get_get_handle_range_for_service_with_uuid128(ublox_spp_profile_uuid128, &start_handle, &end_handle);
224 	btstack_assert(service_found != 0);
225 	UNUSED(service_found);
226 
227     // get characteristic value handle and client configuration handle
228     // FIFO
229     instance->fifo_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid128(start_handle, end_handle, ublox_spp_fifo_uuid128);
230     instance->fifo_client_configuration_descriptor_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid128(start_handle, end_handle, ublox_spp_fifo_uuid128);
231     // Credits
232     instance->credits_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid128(start_handle, end_handle, ublox_spp_credits_uuid128);
233     instance->credits_client_configuration_descriptor_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid128(start_handle, end_handle, ublox_spp_credits_uuid128);
234 
235     log_info("FIFO        value handle 0x%02x", instance->fifo_value_handle);
236     log_info("FIFO CCC    value handle 0x%02x", instance->fifo_client_configuration_descriptor_handle);
237     log_info("Credits     value handle 0x%02x", instance->credits_value_handle);
238     log_info("Credits CCC value handle 0x%02x", instance->credits_client_configuration_descriptor_handle);
239 
240     // register service with ATT Server
241     ublox_spp_service.start_handle   = start_handle;
242     ublox_spp_service.end_handle     = end_handle;
243     ublox_spp_service.read_callback  = &ublox_spp_service_read_callback;
244     ublox_spp_service.write_callback = &ublox_spp_service_write_callback;
245     att_server_register_service_handler(&ublox_spp_service);
246 }
247 
248 /**
249  * @brief Queue send request. When called, one packet can be send via ublox_spp_service_send below
250  * @param request
251  * @param con_handle
252  */
253 void ublox_spp_service_server_request_can_send_now(btstack_context_callback_registration_t * request, hci_con_handle_t con_handle){
254     ublox_spp_service_t * instance = &ublox_spp;
255     if (!ublox_spp_service_flow_control_enabled(instance) || instance->outgoing_credits) {
256         att_server_request_to_send_notification(request, con_handle);
257         return;
258     }
259     instance->request = request;
260 }
261 
262 /**
263  * @brief Send data
264  * @param con_handle
265  * @param data
266  * @param size
267  */
268 int ublox_spp_service_server_send(hci_con_handle_t con_handle, const uint8_t * data, uint16_t size){
269     ublox_spp_service_t * instance = &ublox_spp;
270     if (ublox_spp_service_flow_control_enabled(instance)){
271         if (instance->outgoing_credits > 0u){
272             instance->outgoing_credits--;
273         }
274     }
275     return att_server_notify(con_handle, instance->fifo_value_handle, &data[0], size);
276 }
277 
278