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