Mouse code for the MacroRat
mbed-dev/targets/TARGET_NORDIC/TARGET_NRF5/sdk/ble/ble_db_discovery/ble_db_discovery.h@46:b156ef445742, 2017-06-03 (annotated)
- Committer:
- sahilmgandhi
- Date:
- Sat Jun 03 00:22:44 2017 +0000
- Revision:
- 46:b156ef445742
- Parent:
- 18:6a4db94011d3
Final code for internal battlebot competition.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sahilmgandhi | 18:6a4db94011d3 | 1 | /* |
sahilmgandhi | 18:6a4db94011d3 | 2 | * Copyright (c) 2013 Nordic Semiconductor ASA |
sahilmgandhi | 18:6a4db94011d3 | 3 | * All rights reserved. |
sahilmgandhi | 18:6a4db94011d3 | 4 | * |
sahilmgandhi | 18:6a4db94011d3 | 5 | * Redistribution and use in source and binary forms, with or without modification, |
sahilmgandhi | 18:6a4db94011d3 | 6 | * are permitted provided that the following conditions are met: |
sahilmgandhi | 18:6a4db94011d3 | 7 | * |
sahilmgandhi | 18:6a4db94011d3 | 8 | * 1. Redistributions of source code must retain the above copyright notice, this list |
sahilmgandhi | 18:6a4db94011d3 | 9 | * of conditions and the following disclaimer. |
sahilmgandhi | 18:6a4db94011d3 | 10 | * |
sahilmgandhi | 18:6a4db94011d3 | 11 | * 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA |
sahilmgandhi | 18:6a4db94011d3 | 12 | * integrated circuit in a product or a software update for such product, must reproduce |
sahilmgandhi | 18:6a4db94011d3 | 13 | * the above copyright notice, this list of conditions and the following disclaimer in |
sahilmgandhi | 18:6a4db94011d3 | 14 | * the documentation and/or other materials provided with the distribution. |
sahilmgandhi | 18:6a4db94011d3 | 15 | * |
sahilmgandhi | 18:6a4db94011d3 | 16 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be |
sahilmgandhi | 18:6a4db94011d3 | 17 | * used to endorse or promote products derived from this software without specific prior |
sahilmgandhi | 18:6a4db94011d3 | 18 | * written permission. |
sahilmgandhi | 18:6a4db94011d3 | 19 | * |
sahilmgandhi | 18:6a4db94011d3 | 20 | * 4. This software, with or without modification, must only be used with a |
sahilmgandhi | 18:6a4db94011d3 | 21 | * Nordic Semiconductor ASA integrated circuit. |
sahilmgandhi | 18:6a4db94011d3 | 22 | * |
sahilmgandhi | 18:6a4db94011d3 | 23 | * 5. Any software provided in binary or object form under this license must not be reverse |
sahilmgandhi | 18:6a4db94011d3 | 24 | * engineered, decompiled, modified and/or disassembled. |
sahilmgandhi | 18:6a4db94011d3 | 25 | * |
sahilmgandhi | 18:6a4db94011d3 | 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
sahilmgandhi | 18:6a4db94011d3 | 27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
sahilmgandhi | 18:6a4db94011d3 | 28 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
sahilmgandhi | 18:6a4db94011d3 | 29 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
sahilmgandhi | 18:6a4db94011d3 | 30 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
sahilmgandhi | 18:6a4db94011d3 | 31 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
sahilmgandhi | 18:6a4db94011d3 | 32 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
sahilmgandhi | 18:6a4db94011d3 | 33 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
sahilmgandhi | 18:6a4db94011d3 | 34 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
sahilmgandhi | 18:6a4db94011d3 | 35 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
sahilmgandhi | 18:6a4db94011d3 | 36 | * |
sahilmgandhi | 18:6a4db94011d3 | 37 | */ |
sahilmgandhi | 18:6a4db94011d3 | 38 | |
sahilmgandhi | 18:6a4db94011d3 | 39 | |
sahilmgandhi | 18:6a4db94011d3 | 40 | |
sahilmgandhi | 18:6a4db94011d3 | 41 | /**@file |
sahilmgandhi | 18:6a4db94011d3 | 42 | * |
sahilmgandhi | 18:6a4db94011d3 | 43 | * @defgroup ble_sdk_lib_db_discovery Database Discovery |
sahilmgandhi | 18:6a4db94011d3 | 44 | * @{ |
sahilmgandhi | 18:6a4db94011d3 | 45 | * @ingroup ble_sdk_lib |
sahilmgandhi | 18:6a4db94011d3 | 46 | * @brief Database discovery module. |
sahilmgandhi | 18:6a4db94011d3 | 47 | * |
sahilmgandhi | 18:6a4db94011d3 | 48 | * @details This module contains the APIs and types exposed by the DB Discovery module. These APIs |
sahilmgandhi | 18:6a4db94011d3 | 49 | * and types can be used by the application to perform discovery of a service and its |
sahilmgandhi | 18:6a4db94011d3 | 50 | * characteristics at the peer server. This module can also be used to discover the |
sahilmgandhi | 18:6a4db94011d3 | 51 | * desired services in multiple remote devices. |
sahilmgandhi | 18:6a4db94011d3 | 52 | * |
sahilmgandhi | 18:6a4db94011d3 | 53 | * @warning The maximum number of characteristics per service that can be discovered by this module |
sahilmgandhi | 18:6a4db94011d3 | 54 | * is determined by the number of characteristics in the service structure defined in |
sahilmgandhi | 18:6a4db94011d3 | 55 | * db_disc_config.h. If the peer has more than the supported number of characteristics, then |
sahilmgandhi | 18:6a4db94011d3 | 56 | * the first found will be discovered and any further characteristics will be ignored. No |
sahilmgandhi | 18:6a4db94011d3 | 57 | * descriptors other than Client Characteristic Configuration Descriptors will be searched |
sahilmgandhi | 18:6a4db94011d3 | 58 | * for at the peer. |
sahilmgandhi | 18:6a4db94011d3 | 59 | * |
sahilmgandhi | 18:6a4db94011d3 | 60 | * @note Presently only one instance of a Primary Service can be discovered by this module. If |
sahilmgandhi | 18:6a4db94011d3 | 61 | * there are multiple instances of the service at the peer, only the first instance |
sahilmgandhi | 18:6a4db94011d3 | 62 | * of it at the peer is fetched and returned to the application. |
sahilmgandhi | 18:6a4db94011d3 | 63 | * |
sahilmgandhi | 18:6a4db94011d3 | 64 | * @note The application must propagate BLE stack events to this module by calling |
sahilmgandhi | 18:6a4db94011d3 | 65 | * ble_db_discovery_on_ble_evt(). |
sahilmgandhi | 18:6a4db94011d3 | 66 | * |
sahilmgandhi | 18:6a4db94011d3 | 67 | */ |
sahilmgandhi | 18:6a4db94011d3 | 68 | |
sahilmgandhi | 18:6a4db94011d3 | 69 | #ifndef BLE_DB_DISCOVERY_H__ |
sahilmgandhi | 18:6a4db94011d3 | 70 | #define BLE_DB_DISCOVERY_H__ |
sahilmgandhi | 18:6a4db94011d3 | 71 | |
sahilmgandhi | 18:6a4db94011d3 | 72 | #include <stdint.h> |
sahilmgandhi | 18:6a4db94011d3 | 73 | #include "nrf_ble_gattc.h" |
sahilmgandhi | 18:6a4db94011d3 | 74 | #include "nrf_ble.h" |
sahilmgandhi | 18:6a4db94011d3 | 75 | #include "nrf_error.h" |
sahilmgandhi | 18:6a4db94011d3 | 76 | #include "ble_srv_common.h" |
sahilmgandhi | 18:6a4db94011d3 | 77 | #include "ble_gatt_db.h" |
sahilmgandhi | 18:6a4db94011d3 | 78 | |
sahilmgandhi | 18:6a4db94011d3 | 79 | |
sahilmgandhi | 18:6a4db94011d3 | 80 | #define BLE_DB_DISCOVERY_MAX_SRV 6 /**< Maximum number of services supported by this module. This also indicates the maximum number of users allowed to be registered to this module. (one user per service). */ |
sahilmgandhi | 18:6a4db94011d3 | 81 | |
sahilmgandhi | 18:6a4db94011d3 | 82 | |
sahilmgandhi | 18:6a4db94011d3 | 83 | /**@brief Type of the DB Discovery event. |
sahilmgandhi | 18:6a4db94011d3 | 84 | */ |
sahilmgandhi | 18:6a4db94011d3 | 85 | typedef enum |
sahilmgandhi | 18:6a4db94011d3 | 86 | { |
sahilmgandhi | 18:6a4db94011d3 | 87 | BLE_DB_DISCOVERY_COMPLETE, /**< Event indicating that the GATT Database discovery is complete. */ |
sahilmgandhi | 18:6a4db94011d3 | 88 | BLE_DB_DISCOVERY_ERROR, /**< Event indicating that an internal error has occurred in the DB Discovery module. This could typically be because of the SoftDevice API returning an error code during the DB discover.*/ |
sahilmgandhi | 18:6a4db94011d3 | 89 | BLE_DB_DISCOVERY_SRV_NOT_FOUND, /**< Event indicating that the service was not found at the peer.*/ |
sahilmgandhi | 18:6a4db94011d3 | 90 | BLE_DB_DISCOVERY_AVAILABLE /**< Event indicating that the DB discovery module is available.*/ |
sahilmgandhi | 18:6a4db94011d3 | 91 | } ble_db_discovery_evt_type_t; |
sahilmgandhi | 18:6a4db94011d3 | 92 | |
sahilmgandhi | 18:6a4db94011d3 | 93 | |
sahilmgandhi | 18:6a4db94011d3 | 94 | |
sahilmgandhi | 18:6a4db94011d3 | 95 | /**@brief Structure for holding the information related to the GATT database at the server. |
sahilmgandhi | 18:6a4db94011d3 | 96 | * |
sahilmgandhi | 18:6a4db94011d3 | 97 | * @details This module identifies a remote database. Use one instance of this structure per |
sahilmgandhi | 18:6a4db94011d3 | 98 | * connection. |
sahilmgandhi | 18:6a4db94011d3 | 99 | * |
sahilmgandhi | 18:6a4db94011d3 | 100 | * @warning This structure must be zero-initialized. |
sahilmgandhi | 18:6a4db94011d3 | 101 | */ |
sahilmgandhi | 18:6a4db94011d3 | 102 | typedef struct |
sahilmgandhi | 18:6a4db94011d3 | 103 | { |
sahilmgandhi | 18:6a4db94011d3 | 104 | ble_gatt_db_srv_t services[BLE_DB_DISCOVERY_MAX_SRV]; /**< Information related to the current service being discovered. This is intended for internal use during service discovery.*/ |
sahilmgandhi | 18:6a4db94011d3 | 105 | uint8_t srv_count; /**< Number of services at the peers GATT database.*/ |
sahilmgandhi | 18:6a4db94011d3 | 106 | uint8_t curr_char_ind; /**< Index of the current characteristic being discovered. This is intended for internal use during service discovery.*/ |
sahilmgandhi | 18:6a4db94011d3 | 107 | uint8_t curr_srv_ind; /**< Index of the current service being discovered. This is intended for internal use during service discovery.*/ |
sahilmgandhi | 18:6a4db94011d3 | 108 | bool discovery_in_progress; /**< Variable to indicate if there is a service discovery in progress. */ |
sahilmgandhi | 18:6a4db94011d3 | 109 | uint8_t discoveries_count; /**< Number of service discoveries made, both successful and unsuccessful. */ |
sahilmgandhi | 18:6a4db94011d3 | 110 | uint16_t conn_handle; /**< Connection handle on which the discovery is started*/ |
sahilmgandhi | 18:6a4db94011d3 | 111 | } ble_db_discovery_t; |
sahilmgandhi | 18:6a4db94011d3 | 112 | |
sahilmgandhi | 18:6a4db94011d3 | 113 | |
sahilmgandhi | 18:6a4db94011d3 | 114 | /**@brief Structure containing the event from the DB discovery module to the application. |
sahilmgandhi | 18:6a4db94011d3 | 115 | */ |
sahilmgandhi | 18:6a4db94011d3 | 116 | typedef struct |
sahilmgandhi | 18:6a4db94011d3 | 117 | { |
sahilmgandhi | 18:6a4db94011d3 | 118 | ble_db_discovery_evt_type_t evt_type; /**< Type of event. */ |
sahilmgandhi | 18:6a4db94011d3 | 119 | uint16_t conn_handle; /**< Handle of the connection for which this event has occurred. */ |
sahilmgandhi | 18:6a4db94011d3 | 120 | union |
sahilmgandhi | 18:6a4db94011d3 | 121 | { |
sahilmgandhi | 18:6a4db94011d3 | 122 | ble_gatt_db_srv_t discovered_db; /**< Structure containing the information about the GATT Database at the server. This will be filled when the event type is @ref BLE_DB_DISCOVERY_COMPLETE.*/ |
sahilmgandhi | 18:6a4db94011d3 | 123 | uint32_t err_code; /**< nRF Error code indicating the type of error which occurred in the DB Discovery module. This will be filled when the event type is @ref BLE_DB_DISCOVERY_ERROR. */ |
sahilmgandhi | 18:6a4db94011d3 | 124 | } params; |
sahilmgandhi | 18:6a4db94011d3 | 125 | } ble_db_discovery_evt_t; |
sahilmgandhi | 18:6a4db94011d3 | 126 | |
sahilmgandhi | 18:6a4db94011d3 | 127 | |
sahilmgandhi | 18:6a4db94011d3 | 128 | /**@brief DB Discovery event handler type. */ |
sahilmgandhi | 18:6a4db94011d3 | 129 | typedef void (* ble_db_discovery_evt_handler_t)(ble_db_discovery_evt_t * p_evt); |
sahilmgandhi | 18:6a4db94011d3 | 130 | |
sahilmgandhi | 18:6a4db94011d3 | 131 | |
sahilmgandhi | 18:6a4db94011d3 | 132 | /**@brief Function for initializing the DB Discovery module. |
sahilmgandhi | 18:6a4db94011d3 | 133 | * |
sahilmgandhi | 18:6a4db94011d3 | 134 | * @param[in] evt_handler Event handler to be called by the DB discovery module when any event |
sahilmgandhi | 18:6a4db94011d3 | 135 | * related to discovery of the registered service occurs. |
sahilmgandhi | 18:6a4db94011d3 | 136 | * |
sahilmgandhi | 18:6a4db94011d3 | 137 | * @retval NRF_SUCCESS On successful initialization. |
sahilmgandhi | 18:6a4db94011d3 | 138 | * @retval NRF_ERROR_NULL If the handler was NULL. |
sahilmgandhi | 18:6a4db94011d3 | 139 | */ |
sahilmgandhi | 18:6a4db94011d3 | 140 | uint32_t ble_db_discovery_init(ble_db_discovery_evt_handler_t evt_handler); |
sahilmgandhi | 18:6a4db94011d3 | 141 | |
sahilmgandhi | 18:6a4db94011d3 | 142 | |
sahilmgandhi | 18:6a4db94011d3 | 143 | /**@brief Function for closing the DB Discovery module. |
sahilmgandhi | 18:6a4db94011d3 | 144 | * |
sahilmgandhi | 18:6a4db94011d3 | 145 | * @details This function will clear up any internal variables and states maintained by the |
sahilmgandhi | 18:6a4db94011d3 | 146 | * module. To re-use the module after calling this function, the function @ref |
sahilmgandhi | 18:6a4db94011d3 | 147 | * ble_db_discovery_init must be called again. |
sahilmgandhi | 18:6a4db94011d3 | 148 | * |
sahilmgandhi | 18:6a4db94011d3 | 149 | * @retval NRF_SUCCESS Operation success. |
sahilmgandhi | 18:6a4db94011d3 | 150 | */ |
sahilmgandhi | 18:6a4db94011d3 | 151 | uint32_t ble_db_discovery_close(void); |
sahilmgandhi | 18:6a4db94011d3 | 152 | |
sahilmgandhi | 18:6a4db94011d3 | 153 | |
sahilmgandhi | 18:6a4db94011d3 | 154 | /**@brief Function for registering with the DB Discovery module. |
sahilmgandhi | 18:6a4db94011d3 | 155 | * |
sahilmgandhi | 18:6a4db94011d3 | 156 | * @details The application can use this function to inform which service it is interested in |
sahilmgandhi | 18:6a4db94011d3 | 157 | * discovering at the server. |
sahilmgandhi | 18:6a4db94011d3 | 158 | * |
sahilmgandhi | 18:6a4db94011d3 | 159 | * @param[in] p_uuid Pointer to the UUID of the service to be discovered at the server. |
sahilmgandhi | 18:6a4db94011d3 | 160 | * |
sahilmgandhi | 18:6a4db94011d3 | 161 | * @note The total number of services that can be discovered by this module is @ref |
sahilmgandhi | 18:6a4db94011d3 | 162 | * BLE_DB_DISCOVERY_MAX_SRV. This effectively means that the maximum number of |
sahilmgandhi | 18:6a4db94011d3 | 163 | * registrations possible is equal to the @ref BLE_DB_DISCOVERY_MAX_SRV. |
sahilmgandhi | 18:6a4db94011d3 | 164 | * |
sahilmgandhi | 18:6a4db94011d3 | 165 | * @retval NRF_SUCCESS Operation success. |
sahilmgandhi | 18:6a4db94011d3 | 166 | * @retval NRF_ERROR_NULL When a NULL pointer is passed as input. |
sahilmgandhi | 18:6a4db94011d3 | 167 | * @retval NRF_ERROR_INVALID_STATE If this function is called without calling the |
sahilmgandhi | 18:6a4db94011d3 | 168 | * @ref ble_db_discovery_init. |
sahilmgandhi | 18:6a4db94011d3 | 169 | * @retval NRF_ERROR_NO_MEM The maximum number of registrations allowed by this module |
sahilmgandhi | 18:6a4db94011d3 | 170 | * has been reached. |
sahilmgandhi | 18:6a4db94011d3 | 171 | */ |
sahilmgandhi | 18:6a4db94011d3 | 172 | uint32_t ble_db_discovery_evt_register(const ble_uuid_t * const p_uuid); |
sahilmgandhi | 18:6a4db94011d3 | 173 | |
sahilmgandhi | 18:6a4db94011d3 | 174 | |
sahilmgandhi | 18:6a4db94011d3 | 175 | /**@brief Function for starting the discovery of the GATT database at the server. |
sahilmgandhi | 18:6a4db94011d3 | 176 | * |
sahilmgandhi | 18:6a4db94011d3 | 177 | * @warning p_db_discovery structure must be zero-initialized. |
sahilmgandhi | 18:6a4db94011d3 | 178 | * |
sahilmgandhi | 18:6a4db94011d3 | 179 | * @param[out] p_db_discovery Pointer to the DB Discovery structure. |
sahilmgandhi | 18:6a4db94011d3 | 180 | * @param[in] conn_handle The handle of the connection for which the discovery should be |
sahilmgandhi | 18:6a4db94011d3 | 181 | * started. |
sahilmgandhi | 18:6a4db94011d3 | 182 | * |
sahilmgandhi | 18:6a4db94011d3 | 183 | * @retval NRF_SUCCESS Operation success. |
sahilmgandhi | 18:6a4db94011d3 | 184 | * @retval NRF_ERROR_NULL When a NULL pointer is passed as input. |
sahilmgandhi | 18:6a4db94011d3 | 185 | * @retval NRF_ERROR_INVALID_STATE If this function is called without calling the |
sahilmgandhi | 18:6a4db94011d3 | 186 | * @ref ble_db_discovery_init, or without calling |
sahilmgandhi | 18:6a4db94011d3 | 187 | * @ref ble_db_discovery_evt_register. |
sahilmgandhi | 18:6a4db94011d3 | 188 | * @retval NRF_ERROR_BUSY If a discovery is already in progress for the current |
sahilmgandhi | 18:6a4db94011d3 | 189 | * connection. |
sahilmgandhi | 18:6a4db94011d3 | 190 | * |
sahilmgandhi | 18:6a4db94011d3 | 191 | * @return This API propagates the error code returned by the |
sahilmgandhi | 18:6a4db94011d3 | 192 | * SoftDevice API @ref sd_ble_gattc_primary_services_discover. |
sahilmgandhi | 18:6a4db94011d3 | 193 | */ |
sahilmgandhi | 18:6a4db94011d3 | 194 | uint32_t ble_db_discovery_start(ble_db_discovery_t * const p_db_discovery, |
sahilmgandhi | 18:6a4db94011d3 | 195 | uint16_t conn_handle); |
sahilmgandhi | 18:6a4db94011d3 | 196 | |
sahilmgandhi | 18:6a4db94011d3 | 197 | |
sahilmgandhi | 18:6a4db94011d3 | 198 | /**@brief Function for handling the Application's BLE Stack events. |
sahilmgandhi | 18:6a4db94011d3 | 199 | * |
sahilmgandhi | 18:6a4db94011d3 | 200 | * @param[in,out] p_db_discovery Pointer to the DB Discovery structure. |
sahilmgandhi | 18:6a4db94011d3 | 201 | * @param[in] p_ble_evt Pointer to the BLE event received. |
sahilmgandhi | 18:6a4db94011d3 | 202 | */ |
sahilmgandhi | 18:6a4db94011d3 | 203 | void ble_db_discovery_on_ble_evt(ble_db_discovery_t * const p_db_discovery, |
sahilmgandhi | 18:6a4db94011d3 | 204 | const ble_evt_t * const p_ble_evt); |
sahilmgandhi | 18:6a4db94011d3 | 205 | |
sahilmgandhi | 18:6a4db94011d3 | 206 | #endif // BLE_DB_DISCOVERY_H__ |
sahilmgandhi | 18:6a4db94011d3 | 207 | |
sahilmgandhi | 18:6a4db94011d3 | 208 | /** @} */ |