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 87 #ifdef ENABLE_TESTING_SUPPORT 88 BATTERY_SERVICE_CLIENT_W2_READ_CHARACTERISTIC_CONFIGURATION, 89 BATTERY_SERVICE_CLIENT_W4_CHARACTERISTIC_CONFIGURATION_RESULT, 90 #endif 91 } battery_service_client_state_t; 92 93 94 typedef struct { 95 // service 96 uint16_t start_handle; 97 uint16_t end_handle; 98 99 // characteristic 100 uint16_t properties; 101 uint16_t value_handle; 102 103 #ifdef ENABLE_TESTING_SUPPORT 104 uint16_t ccc_handle; 105 #endif 106 107 gatt_client_notification_t notification_listener; 108 } battery_service_t; 109 110 111 typedef struct { 112 btstack_linked_item_t item; 113 114 hci_con_handle_t con_handle; 115 uint16_t cid; 116 battery_service_client_state_t state; 117 btstack_packet_handler_t client_handler; 118 119 uint32_t poll_interval_ms; 120 121 uint8_t num_instances; 122 battery_service_t services[MAX_NUM_BATTERY_SERVICES]; 123 124 // used for discovering characteristics and polling 125 uint8_t service_index; 126 uint8_t poll_bitmap; 127 uint8_t need_poll_bitmap; 128 uint8_t polled_service_index; 129 btstack_timer_source_t poll_timer; 130 } battery_service_client_t; 131 132 /* API_START */ 133 134 135 /** 136 * @brief Initialize Battery Service. 137 */ 138 void battery_service_client_init(void); 139 140 /** 141 * @brief Connect to Battery Services of remote device. The client will try to register for notifications. 142 * If notifications are not supported by remote Battery Service, the client will poll battery level 143 * If poll_interval_ms is 0, polling is disabled, and only notifications will be received. 144 * In either case, the battery level is received via GATTSERVICE_SUBEVENT_BATTERY_SERVICE_LEVEL event. 145 * 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, 146 * see ATT errors (see bluetooth.h) for other values. 147 * 148 * For manual polling, see battery_service_client_read_battery_level below. 149 * 150 * Event GATTSERVICE_SUBEVENT_BATTERY_SERVICE_CONNECTED is emitted with status ERROR_CODE_SUCCESS on success, otherwise 151 * GATT_CLIENT_IN_WRONG_STATE, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE if no battery service is found, or ATT errors (see bluetooth.h). 152 * This event event also returns number of battery instances found on remote server, as well as poll bitmap that indicates which indexes 153 * of services require polling, i.e. they do not support notification on battery level change, 154 * 155 * @param con_handle 156 * @param packet_handler 157 * @param poll_interval_ms or 0 to disable polling 158 * @param battery_service_cid 159 * @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 160 */ 161 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); 162 163 /** 164 * @brief Read battery level for service with given index. Event GATTSERVICE_SUBEVENT_BATTERY_SERVICE_LEVEL is 165 * received with battery level (unit is in percentage, i.e. 100 = full). The battery level is valid if the ATTT status 166 * is equal to ATT_ERROR_SUCCESS, see ATT errors (see bluetooth.h) for other values. 167 * @param battery_service_cid 168 * @param service_index 169 * @return status 170 */ 171 uint8_t battery_service_client_read_battery_level(uint16_t battery_service_cid, uint8_t service_index); 172 173 /** 174 * @brief Disconnect from Battery Service. 175 * @param battery_service_cid 176 * @return status 177 */ 178 uint8_t battery_service_client_disconnect(uint16_t battery_service_cid); 179 180 /** 181 * @brief De-initialize Battery Service. 182 */ 183 void battery_service_client_deinit(void); 184 185 /* API_END */ 186 187 #if defined __cplusplus 188 } 189 #endif 190 191 #endif 192