xref: /btstack/src/ble/gatt-service/scan_parameters_service_client.h (revision 98451c7b102094e15e0a72ca2f7098d91aed2017)
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     SCAN_PARAMETERS_SERVICE_CLIENT_STATE_CONNECTED
58 } scan_parameters_service_client_state_t;
59 
60 
61 typedef struct {
62     btstack_linked_item_t item;
63 
64     hci_con_handle_t con_handle;
65     uint16_t cid;
66     scan_parameters_service_client_state_t  state;
67     btstack_packet_handler_t client_handler;
68 
69     // service
70     uint16_t start_handle;
71     uint16_t end_handle;
72 
73     // characteristic
74     uint16_t scan_interval_window_value_handle;
75     bool     scan_interval_window_value_update;
76 } scan_parameters_service_client_t;
77 
78 /* API_START */
79 
80 /**
81  * @brief Initialize Scan Parameters Service.
82  */
83 void scan_parameters_service_client_init(void);
84 
85 /**
86  * @brief Connect to Scan Parameters Service of remote device.
87  *
88  * The GATTSERVICE_SUBEVENT_SCAN_PARAMETERS_SERVICE_CONNECTED event completes the request.
89  * Its status is set to ERROR_CODE_SUCCESS if remote service and SCAN_INTERVAL_WINDOW characteristic are found.
90  * Other status codes of this event:
91  * - GATT_CLIENT_IN_WRONG_STATE: client in wrong state
92  * - ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE: service or characteristic not found
93  * - ATT errors, see bluetooth.h
94  *
95  * @param con_handle
96  * @param packet_handler
97  * @param scan_parameters_cid
98  * @return status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER if client with con_handle not found
99  */
100 uint8_t scan_parameters_service_client_connect(hci_con_handle_t con_handle,  btstack_packet_handler_t packet_handler, uint16_t * scan_parameters_cid);
101 
102 /**
103  * @brief Set Scan Parameters Service. It will update all connected devices.
104  * @param scan_interval
105  * @param scan_window
106  */
107 void scan_parameters_service_client_set(uint16_t scan_interval, uint16_t scan_window);
108 
109 /**
110  * @brief Disconnect from Scan Parameters Service.
111  * @param scan_parameters_cid
112  * @return status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER if client with con_handle is not found
113  */
114 uint8_t scan_parameters_service_client_disconnect(uint16_t scan_parameters_cid);
115 
116 
117 /**
118  * @brief De-initialize Scan Parameters Service.
119  */
120 void scan_parameters_service_client_deinit(void);
121 
122 /* API_END */
123 
124 #if defined __cplusplus
125 }
126 #endif
127 
128 #endif
129