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