xref: /btstack/src/ble/gatt-service/scan_parameters_service_client.h (revision 503a627edab6ba8492c3d0cdd9ac598fe2b0f08a)
1 /*
2  * Copyright (C) 2021 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 /**
39  * @text The Scan Parameters Service Client allows to store its
40  * LE scan parameters on a remote device such that the remote device
41  * can utilize this information to optimize power consumption and/or
42  * reconnection latency.
43  */
44 
45 #ifndef SCAN_PARAMAETERS_SERVICE_CLIENT_H
46 #define SCAN_PARAMAETERS_SERVICE_CLIENT_H
47 
48 #include <stdint.h>
49 #include "btstack_defines.h"
50 #include "bluetooth.h"
51 #include "ble/gatt_client.h"
52 #include "btstack_linked_list.h"
53 
54 #if defined __cplusplus
55 extern "C" {
56 #endif
57 
58 typedef enum {
59     SCAN_PARAMETERS_SERVICE_CLIENT_STATE_IDLE,
60     SCAN_PARAMETERS_SERVICE_CLIENT_STATE_W2_QUERY_SERVICE,
61     SCAN_PARAMETERS_SERVICE_CLIENT_STATE_W4_SERVICE_RESULT,
62     SCAN_PARAMETERS_SERVICE_CLIENT_STATE_W2_QUERY_CHARACTERISTIC,
63     SCAN_PARAMETERS_SERVICE_CLIENT_STATE_W4_CHARACTERISTIC_RESULT,
64 #ifdef ENABLE_TESTING_SUPPORT
65     SCAN_PARAMETERS_SERVICE_CLIENT_STATE_W2_QUERY_CCC,
66     SCAN_PARAMETERS_SERVICE_CLIENT_STATE_W4_CCC,
67 #endif
68     SCAN_PARAMETERS_SERVICE_CLIENT_STATE_W2_CONFIGURE_NOTIFICATIONS,
69     SCAN_PARAMETERS_SERVICE_CLIENT_STATE_W4_NOTIFICATIONS_CONFIGURED,
70 
71     SCAN_PARAMETERS_SERVICE_CLIENT_STATE_CONNECTED
72 } scan_parameters_service_client_state_t;
73 
74 
75 typedef struct {
76     btstack_linked_item_t item;
77 
78     hci_con_handle_t con_handle;
79     uint16_t cid;
80     scan_parameters_service_client_state_t  state;
81     btstack_packet_handler_t client_handler;
82 
83     // service
84     uint16_t start_handle;
85     uint16_t end_handle;
86 
87     // characteristic
88     uint16_t scan_interval_window_value_handle;
89 
90     uint16_t scan_refresh_value_handle;
91     uint16_t scan_refresh_end_handle;
92     uint16_t scan_refresh_properties;
93 
94     bool     scan_interval_window_value_update;
95 
96     gatt_client_notification_t notification_listener;
97 } scan_parameters_service_client_t;
98 
99 /* API_START */
100 
101 /**
102  * @brief Initialize Scan Parameters Service.
103  */
104 void scan_parameters_service_client_init(void);
105 
106 /**
107  * @brief Set Scan Parameters Service. It will update all connected devices.
108  * @param scan_interval
109  * @param scan_window
110  */
111 void scan_parameters_service_client_set(uint16_t scan_interval, uint16_t scan_window);
112 
113 /**
114  * @brief Connect to Scan Parameters Service of remote device.
115  *
116  * The GATTSERVICE_SUBEVENT_SCAN_PARAMETERS_SERVICE_CONNECTED event completes the request.
117  * Its status is set to ERROR_CODE_SUCCESS if remote service and SCAN_INTERVAL_WINDOW characteristic are found.
118  * Other status codes of this event:
119  * - GATT_CLIENT_IN_WRONG_STATE: client in wrong state
120  * - ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE: service or characteristic not found
121  * - ATT errors, see bluetooth.h
122  *
123  * @param con_handle
124  * @param packet_handler
125  * @param scan_parameters_cid
126  * @return status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER if client with con_handle not found
127  */
128 uint8_t scan_parameters_service_client_connect(hci_con_handle_t con_handle,  btstack_packet_handler_t packet_handler, uint16_t * scan_parameters_cid);
129 
130 /**
131  * @brief Enable notifications
132  * @param scan_parameters_cid
133  * @return status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER if client with con_handle is not found
134  */
135 uint8_t scan_parameters_service_client_enable_notifications(uint16_t scan_parameters_cid);
136 
137 /**
138  * @brief Disconnect from Scan Parameters Service.
139  * @param scan_parameters_cid
140  * @return status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER if client with con_handle is not found
141  */
142 uint8_t scan_parameters_service_client_disconnect(uint16_t scan_parameters_cid);
143 
144 
145 /**
146  * @brief De-initialize Scan Parameters Service.
147  */
148 void scan_parameters_service_client_deinit(void);
149 
150 /* API_END */
151 
152 #if defined __cplusplus
153 }
154 #endif
155 
156 #endif
157