xref: /btstack/src/ble/gatt-service/link_loss_service_client.h (revision 25d5427a345779b159b63c0d8b197d2dee40cf37)
1 /*
2  * Copyright (C) 2024 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 Link Loss Service Client
40  *
41  */
42 
43 #ifndef LINK_LOSS_SERVICE_CLIENT_H
44 #define LINK_LOSS_SERVICE_CLIENT_H
45 
46 #include <stdint.h>
47 #include "btstack_defines.h"
48 #include "bluetooth.h"
49 #include "btstack_linked_list.h"
50 #include "ble/gatt_client.h"
51 #include "ble/gatt_service_client.h"
52 #include "ble/gatt-service/link_loss_service_util.h"
53 
54 #if defined __cplusplus
55 extern "C" {
56 #endif
57 
58 
59 /**
60  * @text The Link Loss Service Client
61  */
62 typedef enum {
63     LINK_LOSS_SERVICE_CLIENT_STATE_IDLE = 0,
64     LINK_LOSS_SERVICE_CLIENT_STATE_W4_CONNECTION,
65     LINK_LOSS_SERVICE_CLIENT_STATE_READY,
66     LINK_LOSS_SERVICE_CLIENT_STATE_W2_READ_CHARACTERISTIC_VALUE,
67     LINK_LOSS_SERVICE_CLIENT_STATE_W4_READ_CHARACTERISTIC_VALUE_RESULT,
68     LINK_LOSS_SERVICE_CLIENT_STATE_W2_WRITE_CHARACTERISTIC_VALUE,
69     LINK_LOSS_SERVICE_CLIENT_STATE_W4_WRITE_CHARACTERISTIC_VALUE_RESULT
70 } link_loss_service_client_state_t;
71 
72 typedef struct {
73     btstack_linked_item_t item;
74 
75     gatt_service_client_connection_t basic_connection;
76     btstack_packet_handler_t packet_handler;
77 
78     link_loss_service_client_state_t state;
79 
80     // Used for read characteristic queries
81     uint8_t characteristic_index;
82 
83     // Used to store parameters for write characteristic
84     uint8_t write_buffer[1];
85 } lls_client_connection_t;
86 
87 /* API_START */
88 
89 
90 /**
91  * @brief Initialize Link Loss Service.
92  */
93 void link_loss_service_client_init(void);
94 
95  /**
96  * @brief Connect to a Link Loss Service instance of remote device. The client will automatically register for notifications.
97  * The mute state is received via GATTSERVICE_SUBEVENT_LLS_CLIENT_MUTE event.
98  * The mute state can be 0 - off, 1 - on, 2 - disabeled and it is valid if the ATT status is equal to ATT_ERROR_SUCCESS,
99  * see ATT errors (see bluetooth.h) for other values.
100  *
101  * Event GATTSERVICE_SUBEVENT_LLS_CLIENT_CONNECTED is emitted with status ERROR_CODE_SUCCESS on success, otherwise
102  * GATT_CLIENT_IN_WRONG_STATE, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE if no audio input control service is found, or ATT errors (see bluetooth.h).
103  *
104  * @param con_handle
105  * @param packet_handler
106  * @param llc_connection
107  * @param llc_characteristics_storage           storage for characteristics
108  * @param llc_characteristics_num               == LINK_LOSS_SERVICE_CLIENT_NUM_CHARACTERISTICS
109  * @return status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_COMMAND_DISALLOWED if there is already a client associated with con_handle, or BTSTACK_MEMORY_ALLOC_FAILED
110  */
111 uint8_t link_loss_service_client_connect(
112          hci_con_handle_t con_handle,
113          btstack_packet_handler_t packet_handler,
114          lls_client_connection_t * llc_connection,
115          gatt_service_client_characteristic_t * llc_characteristics_storage, uint8_t llc_characteristics_num,
116          uint16_t * lls_cid
117 );
118 
119 uint8_t link_loss_service_client_read_alert_level(uint16_t lls_cid);
120 uint8_t link_loss_service_client_write_alert_level(uint16_t lls_cid, lls_alert_level_t alert_level);
121 
122 
123 /**
124  * @brief Disconnect.
125  * @param lls_cid
126  * @return status
127  */
128 uint8_t link_loss_service_client_disconnect(uint16_t lls_cid);
129 
130 /**
131  * @brief De-initialize Link Loss Service.
132  */
133 void link_loss_service_client_deinit(void);
134 
135 /* API_END */
136 
137 #if defined __cplusplus
138 }
139 #endif
140 
141 #endif
142