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