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