xref: /btstack/src/ble/gatt-service/battery_service_client.h (revision 25d5427a345779b159b63c0d8b197d2dee40cf37)
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 Battery Service Client
40  *
41  */
42 
43 #ifndef BATTERY_SERVICE_CLIENT_H
44 #define BATTERY_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 
52 #if defined __cplusplus
53 extern "C" {
54 #endif
55 
56 /**
57  * @text The Battery Service Client connects to the Battery Services of a remote device
58  * and queries its battery level values. Level updates are either received via notifications
59  * (if supported by the remote Battery Service), or by manual polling.
60  */
61 
62 #ifndef MAX_NUM_BATTERY_SERVICES
63 #define MAX_NUM_BATTERY_SERVICES 3
64 #endif
65 
66 #if MAX_NUM_BATTERY_SERVICES > 8
67     #error "Maximum number of Battery Services exceeded"
68 #endif
69 
70 
71 typedef enum {
72     BATTERY_SERVICE_CLIENT_STATE_IDLE,
73     BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_SERVICE,
74     BATTERY_SERVICE_CLIENT_STATE_W4_SERVICE_RESULT,
75     BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_CHARACTERISTICS,
76     BATTERY_SERVICE_CLIENT_STATE_W4_CHARACTERISTIC_RESULT,
77 
78 #ifdef ENABLE_TESTING_SUPPORT
79     BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_CHARACTERISTIC_DESCRIPTORS,
80     BATTERY_SERVICE_CLIENT_STATE_W4_CHARACTERISTIC_DESCRIPTORS_RESULT,
81 #endif
82 
83     BATTERY_SERVICE_CLIENT_STATE_W2_REGISTER_NOTIFICATION,
84     BATTERY_SERVICE_CLIENT_STATE_W4_NOTIFICATION_REGISTERED,
85     BATTERY_SERVICE_CLIENT_STATE_CONNECTED,
86     BATTERY_SERVICE_CLIENT_STATE_W4_CHARACTERISTIC_VALUE_READ,
87 
88 #ifdef ENABLE_TESTING_SUPPORT
89     BATTERY_SERVICE_CLIENT_W2_READ_CHARACTERISTIC_CONFIGURATION,
90     BATTERY_SERVICE_CLIENT_W4_CHARACTERISTIC_CONFIGURATION_RESULT,
91 #endif
92 } battery_service_client_state_t;
93 
94 
95 typedef struct {
96     // service
97     uint16_t start_handle;
98     uint16_t end_handle;
99 
100     // characteristic
101     uint16_t properties;
102     uint16_t value_handle;
103 
104 #ifdef ENABLE_TESTING_SUPPORT
105     uint16_t ccc_handle;
106 #endif
107 
108     gatt_client_notification_t notification_listener;
109 } battery_service_t;
110 
111 
112 typedef struct {
113     btstack_linked_item_t item;
114 
115     hci_con_handle_t  con_handle;
116     uint16_t          cid;
117     battery_service_client_state_t  state;
118     btstack_packet_handler_t client_handler;
119 
120     uint32_t poll_interval_ms;
121 
122     uint8_t num_instances;
123     battery_service_t services[MAX_NUM_BATTERY_SERVICES];
124 
125     // used for discovering characteristics and polling
126     uint8_t service_index;
127     uint8_t poll_bitmap;
128     uint8_t need_poll_bitmap;
129     uint8_t polled_service_index;
130     btstack_timer_source_t poll_timer;
131 } battery_service_client_t;
132 
133 /* API_START */
134 
135 
136 /**
137  * @brief Initialize Battery Service.
138  */
139 void battery_service_client_init(void);
140 
141 /**
142  * @brief Connect to Battery Services of remote device. The client will try to register for notifications.
143  * If notifications are not supported by remote Battery Service, the client will poll battery level
144  * If poll_interval_ms is 0, polling is disabled, and only notifications will be received.
145  * In either case, the battery level is received via GATTSERVICE_SUBEVENT_BATTERY_SERVICE_LEVEL event.
146  * The battery level is reported as percentage, i.e. 100 = full and it is valid if the ATT status is equal to ATT_ERROR_SUCCESS,
147  * see ATT errors (see bluetooth.h) for other values.
148  *
149  * For manual polling, see battery_service_client_read_battery_level below.
150  *
151  * Event GATTSERVICE_SUBEVENT_BATTERY_SERVICE_CONNECTED is emitted with status ERROR_CODE_SUCCESS on success, otherwise
152  * GATT_CLIENT_IN_WRONG_STATE, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE if no battery service is found, or ATT errors (see bluetooth.h).
153  * This event event also returns number of battery instances found on remote server, as well as poll bitmap that indicates which indexes
154  * of services require polling, i.e. they do not support notification on battery level change,
155  *
156  * @param con_handle
157  * @param packet_handler
158  * @param poll_interval_ms or 0 to disable polling
159  * @param battery_service_cid
160  * @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
161  */
162 uint8_t battery_service_client_connect(hci_con_handle_t con_handle, btstack_packet_handler_t packet_handler, uint32_t poll_interval_ms, uint16_t * battery_service_cid);
163 
164 /**
165  * @brief Read battery level for service with given index. Event GATTSERVICE_SUBEVENT_BATTERY_SERVICE_LEVEL is
166  * received with battery level (unit is in percentage, i.e. 100 = full). The battery level is valid if the ATTT status
167  * is equal to ATT_ERROR_SUCCESS, see ATT errors (see bluetooth.h) for other values.
168  * @param battery_service_cid
169  * @param service_index
170  * @return status
171  */
172 uint8_t battery_service_client_read_battery_level(uint16_t battery_service_cid, uint8_t service_index);
173 
174 /**
175  * @brief Disconnect from Battery Service.
176  * @param battery_service_cid
177  * @return status
178  */
179 uint8_t battery_service_client_disconnect(uint16_t battery_service_cid);
180 
181 /**
182  * @brief De-initialize Battery Service.
183  */
184 void battery_service_client_deinit(void);
185 
186 /* API_END */
187 
188 #if defined __cplusplus
189 }
190 #endif
191 
192 #endif
193