xref: /btstack/src/ble/gatt-service/scan_parameters_service_server.c (revision 753e0cc26ad54a8c3ba16bac2237b5da037201eb)
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__ "scan_parameters_service_server.c"
39 
40 /**
41  * Implementation of the Nordic SPP-like profile
42  *
43  * To use with your application, add `#import <scan_parameters_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 <stdint.h>
48 #include <string.h>
49 
50 #ifdef ENABLE_TESTING_SUPPORT
51 #include <stdio.h>
52 #endif
53 
54 #include "btstack_defines.h"
55 #include "ble/att_db.h"
56 #include "ble/att_server.h"
57 #include "btstack_util.h"
58 #include "bluetooth_gatt.h"
59 #include "btstack_debug.h"
60 
61 #include "ble/gatt-service/scan_parameters_service_server.h"
62 
63 static btstack_packet_handler_t                 scan_parameters_packet_handler;
64 static btstack_context_callback_registration_t  scan_parameters_callback;
65 static att_service_handler_t                    scan_parameters_service;
66 
67 static uint16_t scan_interval_window_value_handle;
68 static uint16_t scan_interval_window_value_handle_client_configuration;
69 
70 
71 static uint16_t scan_refresh_value_client_configuration;
72 static hci_con_handle_t scan_refresh_value_client_configuration_connection;
73 
74 static uint16_t scan_refresh_value_handle;
75 static uint16_t scan_refresh_value_handle_client_configuration;
76 
77 static void scan_parameters_service_emit_state(hci_con_handle_t con_handle, uint16_t max_scan_interval, uint16_t min_scan_window){
78     if (scan_parameters_packet_handler == NULL) return;
79 
80     uint8_t  event[9];
81     uint16_t pos = 0;
82     event[pos++] = HCI_EVENT_GATTSERVICE_META;
83     event[pos++] = sizeof(event) - 2;
84     event[pos++] = GATTSERVICE_SUBEVENT_SCAN_PARAMETERS_SERVICE_SCAN_INTERVAL_UPDATE;
85     little_endian_store_16(event, pos, (uint16_t) con_handle);
86     pos += 2;
87     little_endian_store_16(event, pos, max_scan_interval);
88     pos += 2;
89     little_endian_store_16(event, pos, min_scan_window);
90     pos += 2;
91 
92     (*scan_parameters_packet_handler)(HCI_EVENT_GATTSERVICE_META, 0, event, pos);
93 }
94 
95 static uint16_t scan_parameters_service_read_callback(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
96     UNUSED(con_handle);
97     UNUSED(attribute_handle);
98     UNUSED(offset);
99     UNUSED(buffer);
100     UNUSED(buffer_size);
101     if (attribute_handle == scan_refresh_value_handle_client_configuration){
102         if (buffer != NULL){
103             little_endian_store_16(buffer, 0, scan_refresh_value_client_configuration);
104         }
105         return 2;
106     }
107     return 0;
108 }
109 
110 static int scan_parameters_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){
111     UNUSED(offset);
112     UNUSED(buffer_size);
113 
114     if (transaction_mode != ATT_TRANSACTION_MODE_NONE){
115         return 0;
116     }
117 
118     if (attribute_handle == scan_interval_window_value_handle){
119         if (buffer_size == 4){
120             uint16_t max_scan_interval = little_endian_read_16(buffer, 0);
121             uint16_t min_scan_window   = little_endian_read_16(buffer, 2);
122             scan_parameters_service_emit_state(con_handle, max_scan_interval, min_scan_window);
123             return ATT_ERROR_SUCCESS;
124         } else {
125             return ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH;
126         }
127     }
128 
129     if (attribute_handle == scan_refresh_value_handle_client_configuration){
130         if (buffer_size == 2){
131             scan_refresh_value_client_configuration = little_endian_read_16(buffer, 0);
132             scan_refresh_value_client_configuration_connection = con_handle;
133             return ATT_ERROR_SUCCESS;
134         } else {
135             return ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH;
136         }
137     }
138 
139     return ATT_ERROR_UNLIKELY_ERROR;
140 }
141 
142 static void scan_parameters_service_refresh_can_send_now(void * context){
143     UNUSED(context);
144     const uint8_t value = 0;
145     att_server_notify(scan_refresh_value_client_configuration_connection, scan_refresh_value_handle, &value, 1);
146 }
147 
148 /**
149  * @brief Init Nordic SPP Service Server with ATT DB
150  * @param callback for tx data from peer
151  */
152 void scan_parameters_service_server_init(btstack_packet_handler_t packet_handler){
153     scan_parameters_packet_handler = packet_handler;
154 
155     // get service handle range
156     uint16_t start_handle = 0;
157     uint16_t end_handle   = 0xffff;
158     int service_found = gatt_server_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_SCAN_PARAMETERS, &start_handle, &end_handle);
159     btstack_assert(service_found != 0);
160     UNUSED(service_found);
161 
162     // get characteristic value handle and client configuration handle
163     scan_interval_window_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_SCAN_INTERVAL_WINDOW);
164     scan_interval_window_value_handle_client_configuration = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_SCAN_INTERVAL_WINDOW);
165 
166     scan_refresh_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_SCAN_REFRESH);
167     scan_refresh_value_handle_client_configuration = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_SCAN_REFRESH);
168 
169     // register service with ATT Server
170     scan_parameters_service.start_handle   = start_handle;
171     scan_parameters_service.end_handle     = end_handle;
172     scan_parameters_service.read_callback  = &scan_parameters_service_read_callback;
173     scan_parameters_service.write_callback = &scan_parameters_service_write_callback;
174     att_server_register_service_handler(&scan_parameters_service);
175 }
176 
177 
178 void scan_parameters_service_server_request_scan_parameters(void){
179     if (scan_refresh_value_client_configuration != 0){
180         scan_parameters_callback.callback = &scan_parameters_service_refresh_can_send_now;
181         scan_parameters_callback.context  = NULL;
182         att_server_register_can_send_now_callback(&scan_parameters_callback, scan_refresh_value_client_configuration_connection);
183     }
184 }
185