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