xref: /btstack/src/ble/gatt-service/ublox_spp_service_server.c (revision 80e33422a96c028b3a9c308fc4b9b874712dafb4)
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         log_info("ublox spp service FIFO control: %d", instance->fifo_client_configuration_descriptor_value);
161         instance->client_data_callback(con_handle, NULL, 0);
162     }
163 
164     if (attribute_handle == instance->credits_value_handle){
165         if (!ublox_spp_service_flow_control_enabled(instance)) return 0;
166         int8_t credits = (int8_t)buffer[0];
167         if (credits <= 0) return 0;
168         instance->outgoing_credits += credits;
169         log_info("received outgoing credits, total %d", instance->outgoing_credits);
170         // Provide credits
171         att_server_request_to_send_notification(&instance->credits_callback, instance->con_handle);
172 
173         // handle user request
174         if (instance->request){
175             btstack_context_callback_registration_t * request = instance->request;
176             instance->request = NULL;
177             att_server_request_to_send_notification(request, instance->con_handle);
178         }
179     }
180 
181     if (attribute_handle == instance->credits_client_configuration_descriptor_handle){
182         if (buffer_size < 2){
183             return ATT_ERROR_INVALID_OFFSET;
184         }
185         instance->credits_client_configuration_descriptor_value = little_endian_read_16(buffer, 0);
186         log_info("ublox spp service Credits control: %d", instance->credits_client_configuration_descriptor_value);
187         ublox_spp_service_init_credits(instance);
188     }
189 
190     return 0;
191 }
192 
193 static void ublox_spp_credits_callback(void * context){
194     ublox_spp_service_t * instance = (ublox_spp_service_t *) context;
195     if (!instance) return;
196 
197     instance->delta_credits = UBLOX_SPP_MAX_CREDITS - instance->incoming_credits;
198     if (instance->delta_credits){
199         instance->incoming_credits = UBLOX_SPP_MAX_CREDITS;
200         att_server_notify(instance->con_handle, instance->credits_value_handle, &instance->delta_credits, 1);
201     }
202 }
203 /**
204  * @brief Init ublox SPP Service Server with ATT DB
205  * @param callback for tx data from peer
206  */
207 void ublox_spp_service_server_init(void (*client_data_callback)(hci_con_handle_t con_handle, const uint8_t * data, uint16_t size),
208                                    void (*client_credits_callback)(hci_con_handle_t con_handle, uint16_t credits)){
209     ublox_spp_service_t * instance = &ublox_spp;
210     instance->client_data_callback = client_data_callback;
211     instance->client_credits_callback = client_credits_callback;
212 
213     instance->credits_callback.callback = ublox_spp_credits_callback;
214     instance->credits_callback.context = instance;
215     ublox_spp_service_init_credits(instance);
216 
217 
218     // get service handle range
219     uint16_t start_handle = 0;
220     uint16_t end_handle   = 0xfff;
221     int service_found = gatt_server_get_get_handle_range_for_service_with_uuid128(ublox_spp_profile_uuid128, &start_handle, &end_handle);
222     if (!service_found) return;
223 
224     // get characteristic value handle and client configuration handle
225     // FIFO
226     instance->fifo_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid128(start_handle, end_handle, ublox_spp_fifo_uuid128);
227     instance->fifo_client_configuration_descriptor_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid128(start_handle, end_handle, ublox_spp_fifo_uuid128);
228     // Credits
229     instance->credits_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid128(start_handle, end_handle, ublox_spp_credits_uuid128);
230     instance->credits_client_configuration_descriptor_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid128(start_handle, end_handle, ublox_spp_credits_uuid128);
231 
232     log_info("FIFO        value handle 0x%02x", instance->fifo_value_handle);
233     log_info("FIFO CCC    value handle 0x%02x", instance->fifo_client_configuration_descriptor_handle);
234     log_info("Credits     value handle 0x%02x", instance->credits_value_handle);
235     log_info("Credits CCC value handle 0x%02x", instance->credits_client_configuration_descriptor_handle);
236 
237     // register service with ATT Server
238     ublox_spp_service.start_handle   = start_handle;
239     ublox_spp_service.end_handle     = end_handle;
240     ublox_spp_service.read_callback  = &ublox_spp_service_read_callback;
241     ublox_spp_service.write_callback = &ublox_spp_service_write_callback;
242     att_server_register_service_handler(&ublox_spp_service);
243 }
244 
245 /**
246  * @brief Queue send request. When called, one packet can be send via ublox_spp_service_send below
247  * @param request
248  * @param con_handle
249  */
250 void ublox_spp_service_server_request_can_send_now(btstack_context_callback_registration_t * request, hci_con_handle_t con_handle){
251     ublox_spp_service_t * instance = &ublox_spp;
252     if (!ublox_spp_service_flow_control_enabled(instance) || instance->outgoing_credits) {
253         att_server_request_to_send_notification(request, con_handle);
254         return;
255     }
256     instance->request = request;
257 }
258 
259 /**
260  * @brief Send data
261  * @param con_handle
262  * @param data
263  * @param size
264  */
265 int ublox_spp_service_server_send(hci_con_handle_t con_handle, const uint8_t * data, uint16_t size){
266     ublox_spp_service_t * instance = &ublox_spp;
267     if (ublox_spp_service_flow_control_enabled(instance)){
268         if (instance->outgoing_credits > 0){
269             instance->outgoing_credits--;
270         }
271     }
272     return att_server_notify(con_handle, instance->fifo_value_handle, &data[0], size);
273 }
274 
275