xref: /btstack/src/ble/gatt-service/battery_service_client.h (revision 6bdecec7ba8f55d805af8d13b9f45c3a7f6d5810)
1*6bdecec7SMatthias Ringwald /*
2*6bdecec7SMatthias Ringwald  * Copyright (C) 2021 BlueKitchen GmbH
3*6bdecec7SMatthias Ringwald  *
4*6bdecec7SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5*6bdecec7SMatthias Ringwald  * modification, are permitted provided that the following conditions
6*6bdecec7SMatthias Ringwald  * are met:
7*6bdecec7SMatthias Ringwald  *
8*6bdecec7SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9*6bdecec7SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10*6bdecec7SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11*6bdecec7SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12*6bdecec7SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13*6bdecec7SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14*6bdecec7SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15*6bdecec7SMatthias Ringwald  *    from this software without specific prior written permission.
16*6bdecec7SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17*6bdecec7SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18*6bdecec7SMatthias Ringwald  *    monetary gain.
19*6bdecec7SMatthias Ringwald  *
20*6bdecec7SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21*6bdecec7SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*6bdecec7SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*6bdecec7SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24*6bdecec7SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25*6bdecec7SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26*6bdecec7SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27*6bdecec7SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28*6bdecec7SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29*6bdecec7SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30*6bdecec7SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*6bdecec7SMatthias Ringwald  * SUCH DAMAGE.
32*6bdecec7SMatthias Ringwald  *
33*6bdecec7SMatthias Ringwald  * Please inquire about commercial licensing options at
34*6bdecec7SMatthias Ringwald  * [email protected]
35*6bdecec7SMatthias Ringwald  *
36*6bdecec7SMatthias Ringwald  */
37*6bdecec7SMatthias Ringwald 
38*6bdecec7SMatthias Ringwald #ifndef BATTERY_SERVICE_CLIENT_H
39*6bdecec7SMatthias Ringwald #define BATTERY_SERVICE_CLIENT_H
40*6bdecec7SMatthias Ringwald 
41*6bdecec7SMatthias Ringwald #include <stdint.h>
42*6bdecec7SMatthias Ringwald #include "btstack_defines.h"
43*6bdecec7SMatthias Ringwald #include "bluetooth.h"
44*6bdecec7SMatthias Ringwald #include "btstack_linked_list.h"
45*6bdecec7SMatthias Ringwald #include "ble/gatt_client.h"
46*6bdecec7SMatthias Ringwald 
47*6bdecec7SMatthias Ringwald 
48*6bdecec7SMatthias Ringwald #if defined __cplusplus
49*6bdecec7SMatthias Ringwald extern "C" {
50*6bdecec7SMatthias Ringwald #endif
51*6bdecec7SMatthias Ringwald 
52*6bdecec7SMatthias Ringwald #ifndef MAX_NUM_BATTERY_SERVICES
53*6bdecec7SMatthias Ringwald #define MAX_NUM_BATTERY_SERVICES 3
54*6bdecec7SMatthias Ringwald #endif
55*6bdecec7SMatthias Ringwald 
56*6bdecec7SMatthias Ringwald #if MAX_NUM_BATTERY_SERVICES > 8
57*6bdecec7SMatthias Ringwald     #error "Maximum number of Battery Services exceeded"
58*6bdecec7SMatthias Ringwald #endif
59*6bdecec7SMatthias Ringwald 
60*6bdecec7SMatthias Ringwald 
61*6bdecec7SMatthias Ringwald typedef enum {
62*6bdecec7SMatthias Ringwald     BATTERY_SERVICE_CLIENT_STATE_IDLE,
63*6bdecec7SMatthias Ringwald     BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_SERVICE,
64*6bdecec7SMatthias Ringwald     BATTERY_SERVICE_CLIENT_STATE_W4_SERVICE_RESULT,
65*6bdecec7SMatthias Ringwald     BATTERY_SERVICE_CLIENT_STATE_W4_CHARACTERISTIC_RESULT,
66*6bdecec7SMatthias Ringwald     BATTERY_SERVICE_CLIENT_STATE_W4_REGISTER_NOTIFICATION,
67*6bdecec7SMatthias Ringwald     BATTERY_SERVICE_CLIENT_STATE_CONNECTED
68*6bdecec7SMatthias Ringwald } battery_service_client_state_t;
69*6bdecec7SMatthias Ringwald 
70*6bdecec7SMatthias Ringwald 
71*6bdecec7SMatthias Ringwald typedef struct {
72*6bdecec7SMatthias Ringwald     // service
73*6bdecec7SMatthias Ringwald     uint16_t start_handle;
74*6bdecec7SMatthias Ringwald     uint16_t end_handle;
75*6bdecec7SMatthias Ringwald 
76*6bdecec7SMatthias Ringwald     // characteristic
77*6bdecec7SMatthias Ringwald     uint16_t properties;
78*6bdecec7SMatthias Ringwald     uint16_t value_handle;
79*6bdecec7SMatthias Ringwald 
80*6bdecec7SMatthias Ringwald     gatt_client_notification_t notification_listener;
81*6bdecec7SMatthias Ringwald } battery_service_t;
82*6bdecec7SMatthias Ringwald 
83*6bdecec7SMatthias Ringwald 
84*6bdecec7SMatthias Ringwald typedef struct {
85*6bdecec7SMatthias Ringwald     btstack_linked_item_t item;
86*6bdecec7SMatthias Ringwald 
87*6bdecec7SMatthias Ringwald     hci_con_handle_t  con_handle;
88*6bdecec7SMatthias Ringwald     uint16_t          cid;
89*6bdecec7SMatthias Ringwald     battery_service_client_state_t  state;
90*6bdecec7SMatthias Ringwald     btstack_packet_handler_t client_handler;
91*6bdecec7SMatthias Ringwald 
92*6bdecec7SMatthias Ringwald     uint32_t poll_interval_ms;
93*6bdecec7SMatthias Ringwald 
94*6bdecec7SMatthias Ringwald     uint8_t num_instances;
95*6bdecec7SMatthias Ringwald     battery_service_t services[MAX_NUM_BATTERY_SERVICES];
96*6bdecec7SMatthias Ringwald 
97*6bdecec7SMatthias Ringwald     // used for discovering characteristics and polling
98*6bdecec7SMatthias Ringwald     uint8_t battery_service_index;
99*6bdecec7SMatthias Ringwald     uint8_t poll_bitmap;
100*6bdecec7SMatthias Ringwald     uint8_t need_poll_bitmap;
101*6bdecec7SMatthias Ringwald     uint8_t polled_service_index;
102*6bdecec7SMatthias Ringwald     btstack_timer_source_t poll_timer;
103*6bdecec7SMatthias Ringwald } battery_service_client_t;
104*6bdecec7SMatthias Ringwald 
105*6bdecec7SMatthias Ringwald /* API_START */
106*6bdecec7SMatthias Ringwald 
107*6bdecec7SMatthias Ringwald 
108*6bdecec7SMatthias Ringwald /**
109*6bdecec7SMatthias Ringwald  * @brief Initialize Battery Service.
110*6bdecec7SMatthias Ringwald  */
111*6bdecec7SMatthias Ringwald void battery_service_client_init(void);
112*6bdecec7SMatthias Ringwald 
113*6bdecec7SMatthias Ringwald /**
114*6bdecec7SMatthias Ringwald  * @brief Connect to Battery Services of remote device. The client will try to register for notifications.
115*6bdecec7SMatthias Ringwald  * If notifications are not supported by remote Battery Service, the client will poll battery level
116*6bdecec7SMatthias Ringwald  * If poll_interval_ms is 0, polling is disabled, and only notifications will be received.
117*6bdecec7SMatthias Ringwald  * In either case, the battery level is received via GATTSERVICE_SUBEVENT_BATTERY_SERVICE_LEVEL event.
118*6bdecec7SMatthias Ringwald  * 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*6bdecec7SMatthias Ringwald  * see ATT errors (see bluetooth.h) for other values.
120*6bdecec7SMatthias Ringwald  *
121*6bdecec7SMatthias Ringwald  * For manual polling, see battery_service_client_read_battery_level below.
122*6bdecec7SMatthias Ringwald  *
123*6bdecec7SMatthias Ringwald  * Event GATTSERVICE_SUBEVENT_BATTERY_SERVICE_CONNECTED is emitted with status ERROR_CODE_SUCCESS on success, otherwise
124*6bdecec7SMatthias Ringwald  * 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*6bdecec7SMatthias Ringwald  * This event event also returns number of battery instances found on remote server, as well as poll bitmap that indicates which indexes
126*6bdecec7SMatthias Ringwald  * of services require polling, i.e. they do not support notification on battery level change,
127*6bdecec7SMatthias Ringwald  *
128*6bdecec7SMatthias Ringwald  * @param con_handle
129*6bdecec7SMatthias Ringwald  * @param packet_handler
130*6bdecec7SMatthias Ringwald  * @param poll_interval_ms or 0 to disable polling
131*6bdecec7SMatthias Ringwald  * @param battery_service_cid
132*6bdecec7SMatthias Ringwald  * @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*6bdecec7SMatthias Ringwald  */
134*6bdecec7SMatthias Ringwald 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*6bdecec7SMatthias Ringwald 
136*6bdecec7SMatthias Ringwald /**
137*6bdecec7SMatthias Ringwald  * @brief Read battery level for service with given index. Event GATTSERVICE_SUBEVENT_BATTERY_SERVICE_LEVEL is
138*6bdecec7SMatthias Ringwald  * received with battery level (unit is in percentage, i.e. 100 = full). The battery level is valid if the ATTT status
139*6bdecec7SMatthias Ringwald  * is equal to ATT_ERROR_SUCCESS, see ATT errors (see bluetooth.h) for other values.
140*6bdecec7SMatthias Ringwald  * @param battery_service_cid
141*6bdecec7SMatthias Ringwald  * @param service_index
142*6bdecec7SMatthias Ringwald  * @return status
143*6bdecec7SMatthias Ringwald  */
144*6bdecec7SMatthias Ringwald uint8_t battery_service_client_read_battery_level(uint16_t battery_service_cid, uint8_t service_index);
145*6bdecec7SMatthias Ringwald 
146*6bdecec7SMatthias Ringwald /**
147*6bdecec7SMatthias Ringwald  * @brief Disconnect from Battery Service.
148*6bdecec7SMatthias Ringwald  * @param battery_service_cid
149*6bdecec7SMatthias Ringwald  * @return status
150*6bdecec7SMatthias Ringwald  */
151*6bdecec7SMatthias Ringwald uint8_t battery_service_client_disconnect(uint16_t battery_service_cid);
152*6bdecec7SMatthias Ringwald 
153*6bdecec7SMatthias Ringwald /**
154*6bdecec7SMatthias Ringwald  * @brief De-initialize Battery Service.
155*6bdecec7SMatthias Ringwald  */
156*6bdecec7SMatthias Ringwald void battery_service_client_deinit(void);
157*6bdecec7SMatthias Ringwald 
158*6bdecec7SMatthias Ringwald /* API_END */
159*6bdecec7SMatthias Ringwald 
160*6bdecec7SMatthias Ringwald #if defined __cplusplus
161*6bdecec7SMatthias Ringwald }
162*6bdecec7SMatthias Ringwald #endif
163*6bdecec7SMatthias Ringwald 
164*6bdecec7SMatthias Ringwald #endif
165