xref: /btstack/src/ble/gatt-service/ublox_spp_service_server.c (revision 67b65e8f108eb716cb65c6192e677b0f719b3e70)
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 
ublox_spp_service_emit_state(ublox_spp_service_t * instance,bool enabled)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 
ublox_spp_service_flow_control_enabled(ublox_spp_service_t * instance)106 static int ublox_spp_service_flow_control_enabled(ublox_spp_service_t * instance){
107     return instance->credits_client_configuration_descriptor_value;
108 }
109 
ublox_get_instance_for_con_handle(hci_con_handle_t con_handle)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 
ublox_spp_service_init_credits(ublox_spp_service_t * instance)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 
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)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 
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)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(offset);
148 
149     if (transaction_mode != ATT_TRANSACTION_MODE_NONE){
150         return 0;
151     }
152 
153     ublox_spp_service_t * instance = ublox_get_instance_for_con_handle(con_handle);
154     if (instance == NULL){
155         return 0;
156     }
157 
158     if (attribute_handle == instance->fifo_value_handle){
159         instance->client_packet_handler(RFCOMM_DATA_PACKET, (uint16_t) con_handle, &buffer[0], buffer_size);
160         if (!ublox_spp_service_flow_control_enabled(instance)) return 0;
161         if (!instance->incoming_credits) return 0;
162         instance->incoming_credits--;
163         if (instance->incoming_credits < UBLOX_SPP_CREDITS_THRESHOLD){
164             att_server_request_to_send_notification(&instance->credits_callback, instance->con_handle);
165         }
166     }
167 
168     if (attribute_handle == instance->fifo_client_configuration_descriptor_handle){
169         if (buffer_size < 2u){
170             return ATT_ERROR_INVALID_OFFSET;
171         }
172         instance->fifo_client_configuration_descriptor_value = little_endian_read_16(buffer, 0);
173         log_info("ublox spp service FIFO control: %d", instance->fifo_client_configuration_descriptor_value);
174         ublox_spp_service_emit_state(instance,  instance->fifo_client_configuration_descriptor_value != 0);
175     }
176 
177     if (attribute_handle == instance->credits_value_handle){
178         if (!ublox_spp_service_flow_control_enabled(instance)) return 0;
179         int8_t credits = (int8_t)buffer[0];
180         if (credits <= 0) return 0;
181         instance->outgoing_credits += credits;
182         log_info("received outgoing credits, total %d", instance->outgoing_credits);
183         // Provide credits
184         att_server_request_to_send_notification(&instance->credits_callback, instance->con_handle);
185 
186         // handle user request
187         if (instance->request){
188             btstack_context_callback_registration_t * request = instance->request;
189             instance->request = NULL;
190             att_server_request_to_send_notification(request, instance->con_handle);
191         }
192     }
193 
194     if (attribute_handle == instance->credits_client_configuration_descriptor_handle){
195         if (buffer_size < 2u){
196             return ATT_ERROR_INVALID_OFFSET;
197         }
198         instance->credits_client_configuration_descriptor_value = little_endian_read_16(buffer, 0);
199         log_info("ublox spp service Credits control: %d", instance->credits_client_configuration_descriptor_value);
200         ublox_spp_service_init_credits(instance);
201     }
202 
203     return 0;
204 }
205 
ublox_spp_credits_callback(void * context)206 static void ublox_spp_credits_callback(void * context){
207     ublox_spp_service_t * instance = (ublox_spp_service_t *) context;
208     if (!instance) return;
209 
210     instance->delta_credits = UBLOX_SPP_MAX_CREDITS - instance->incoming_credits;
211     if (instance->delta_credits){
212         instance->incoming_credits = UBLOX_SPP_MAX_CREDITS;
213         att_server_notify(instance->con_handle, instance->credits_value_handle, &instance->delta_credits, 1);
214     }
215 }
216 /**
217  * @brief Init ublox SPP Service Server with ATT DB
218  * @param callback for tx data from peer
219  */
ublox_spp_service_server_init(btstack_packet_handler_t packet_handler)220 void ublox_spp_service_server_init(btstack_packet_handler_t packet_handler){
221 
222     static const uint8_t ublox_spp_profile_uuid128[] = { 0x24, 0x56, 0xE1, 0xB9, 0x26, 0xE2, 0x8F, 0x83, 0xE7, 0x44, 0xF3, 0x4F, 0x01, 0xE9, 0xD7, 0x01 };
223     static const uint8_t ublox_spp_fifo_uuid128[]    = { 0x24, 0x56, 0xE1, 0xB9, 0x26, 0xE2, 0x8F, 0x83, 0xE7, 0x44, 0xF3, 0x4F, 0x01, 0xE9, 0xD7, 0x03 };
224     static const uint8_t ublox_spp_credits_uuid128[] = { 0x24, 0x56, 0xE1, 0xB9, 0x26, 0xE2, 0x8F, 0x83, 0xE7, 0x44, 0xF3, 0x4F, 0x01, 0xE9, 0xD7, 0x04 };
225 
226     ublox_spp_service_t * instance = &ublox_spp;
227     instance->client_packet_handler = packet_handler;
228 
229     instance->credits_callback.callback = ublox_spp_credits_callback;
230     instance->credits_callback.context = instance;
231     ublox_spp_service_init_credits(instance);
232 
233 
234     // get service handle range
235     uint16_t start_handle = 0;
236     uint16_t end_handle   = 0xffff;
237     int service_found = gatt_server_get_handle_range_for_service_with_uuid128(ublox_spp_profile_uuid128, &start_handle, &end_handle);
238 	btstack_assert(service_found != 0);
239 	UNUSED(service_found);
240 
241     // get characteristic value handle and client configuration handle
242     // FIFO
243     instance->fifo_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid128(start_handle, end_handle, ublox_spp_fifo_uuid128);
244     instance->fifo_client_configuration_descriptor_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid128(start_handle, end_handle, ublox_spp_fifo_uuid128);
245     // Credits
246     instance->credits_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid128(start_handle, end_handle, ublox_spp_credits_uuid128);
247     instance->credits_client_configuration_descriptor_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid128(start_handle, end_handle, ublox_spp_credits_uuid128);
248 
249     log_info("FIFO        value handle 0x%02x", instance->fifo_value_handle);
250     log_info("FIFO CCC    value handle 0x%02x", instance->fifo_client_configuration_descriptor_handle);
251     log_info("Credits     value handle 0x%02x", instance->credits_value_handle);
252     log_info("Credits CCC value handle 0x%02x", instance->credits_client_configuration_descriptor_handle);
253 
254     // register service with ATT Server
255     ublox_spp_service.start_handle   = start_handle;
256     ublox_spp_service.end_handle     = end_handle;
257     ublox_spp_service.read_callback  = &ublox_spp_service_read_callback;
258     ublox_spp_service.write_callback = &ublox_spp_service_write_callback;
259     att_server_register_service_handler(&ublox_spp_service);
260 }
261 
262 /**
263  * @brief Queue send request. When called, one packet can be send via ublox_spp_service_send below
264  * @param request
265  * @param con_handle
266  */
ublox_spp_service_server_request_can_send_now(btstack_context_callback_registration_t * request,hci_con_handle_t con_handle)267 void ublox_spp_service_server_request_can_send_now(btstack_context_callback_registration_t * request, hci_con_handle_t con_handle){
268     ublox_spp_service_t * instance = &ublox_spp;
269     if (!ublox_spp_service_flow_control_enabled(instance) || instance->outgoing_credits) {
270         att_server_request_to_send_notification(request, con_handle);
271         return;
272     }
273     instance->request = request;
274 }
275 
276 /**
277  * @brief Send data
278  * @param con_handle
279  * @param data
280  * @param size
281  */
ublox_spp_service_server_send(hci_con_handle_t con_handle,const uint8_t * data,uint16_t size)282 int ublox_spp_service_server_send(hci_con_handle_t con_handle, const uint8_t * data, uint16_t size){
283     ublox_spp_service_t * instance = &ublox_spp;
284     if (ublox_spp_service_flow_control_enabled(instance)){
285         if (instance->outgoing_credits > 0u){
286             instance->outgoing_credits--;
287         }
288     }
289     return att_server_notify(con_handle, instance->fifo_value_handle, &data[0], size);
290 }
291 
292