Maiko Matsumoto / Mbed 2 deprecated BLE_WallbotBLE_Challenge_byYUTAKA3

Dependencies:   mbed

Fork of BLE_WallbotBLE_Challenge_byYUTAKA by Yutaka Yoshida

Committer:
jksoft
Date:
Wed Nov 12 02:40:34 2014 +0000
Revision:
0:76dfa9657d9d
????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:76dfa9657d9d 1 /* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
jksoft 0:76dfa9657d9d 2 *
jksoft 0:76dfa9657d9d 3 * The information contained herein is property of Nordic Semiconductor ASA.
jksoft 0:76dfa9657d9d 4 * Terms and conditions of usage are described in detail in NORDIC
jksoft 0:76dfa9657d9d 5 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
jksoft 0:76dfa9657d9d 6 *
jksoft 0:76dfa9657d9d 7 * Licensees are granted free, non-transferable use of the information. NO
jksoft 0:76dfa9657d9d 8 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
jksoft 0:76dfa9657d9d 9 * the file.
jksoft 0:76dfa9657d9d 10 */
jksoft 0:76dfa9657d9d 11
jksoft 0:76dfa9657d9d 12
jksoft 0:76dfa9657d9d 13 /**@file
jksoft 0:76dfa9657d9d 14 *
jksoft 0:76dfa9657d9d 15 * @defgroup ble_sdk_lib_db_discovery Database Discovery
jksoft 0:76dfa9657d9d 16 * @{
jksoft 0:76dfa9657d9d 17 * @ingroup nrf51_sdk_api
jksoft 0:76dfa9657d9d 18 * @brief Database discovery module.
jksoft 0:76dfa9657d9d 19 *
jksoft 0:76dfa9657d9d 20 * @details This module contains the APIs and types exposed by the DB Discovery module. These APIs
jksoft 0:76dfa9657d9d 21 * and types can be used by the application to perform discovery of a service and its
jksoft 0:76dfa9657d9d 22 * characteristics at the peer server. This module can also be used to discover the
jksoft 0:76dfa9657d9d 23 * desired services in multiple remote devices.
jksoft 0:76dfa9657d9d 24 * A typical use of this library is described in the figure below.
jksoft 0:76dfa9657d9d 25 * @image html db_discovery.jpg
jksoft 0:76dfa9657d9d 26 *
jksoft 0:76dfa9657d9d 27 * @warning The maximum number of characteristics per service that can be discovered by this module
jksoft 0:76dfa9657d9d 28 * is indicated by the value of @ref BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV. If the peer
jksoft 0:76dfa9657d9d 29 * has more than the supported number of characteristics, then the first found will be
jksoft 0:76dfa9657d9d 30 * discovered and any further characteristics will be ignored. No descriptors other
jksoft 0:76dfa9657d9d 31 * than Client Characteristic Configuration Descriptors will be searched for at the peer.
jksoft 0:76dfa9657d9d 32 *
jksoft 0:76dfa9657d9d 33 * @note Presently only one instance of a Primary Service can be discovered by this module. If
jksoft 0:76dfa9657d9d 34 * there are multiple instances of the service at the peer, only the first instance
jksoft 0:76dfa9657d9d 35 * of it at the peer is fetched and returned to the application.
jksoft 0:76dfa9657d9d 36 *
jksoft 0:76dfa9657d9d 37 * @note The application must propagate BLE stack events to this module by calling
jksoft 0:76dfa9657d9d 38 * ble_db_discovery_on_ble_evt().
jksoft 0:76dfa9657d9d 39 *
jksoft 0:76dfa9657d9d 40 */
jksoft 0:76dfa9657d9d 41
jksoft 0:76dfa9657d9d 42 #ifndef BLE_DB_DISCOVERY_H__
jksoft 0:76dfa9657d9d 43 #define BLE_DB_DISCOVERY_H__
jksoft 0:76dfa9657d9d 44
jksoft 0:76dfa9657d9d 45 #include <stdint.h>
jksoft 0:76dfa9657d9d 46 #include "ble_gattc.h"
jksoft 0:76dfa9657d9d 47 #include "ble.h"
jksoft 0:76dfa9657d9d 48 #include "nrf_error.h"
jksoft 0:76dfa9657d9d 49 #include "ble_srv_common.h"
jksoft 0:76dfa9657d9d 50
jksoft 0:76dfa9657d9d 51 /**
jksoft 0:76dfa9657d9d 52 * @defgroup db_disc_defines Defines
jksoft 0:76dfa9657d9d 53 * @{
jksoft 0:76dfa9657d9d 54 */
jksoft 0:76dfa9657d9d 55
jksoft 0:76dfa9657d9d 56 #define BLE_DB_DISCOVERY_MAX_SRV 2 /**< 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). */
jksoft 0:76dfa9657d9d 57 #define BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV 3 /**< Maximum number of characteristics per service supported by this module. */
jksoft 0:76dfa9657d9d 58
jksoft 0:76dfa9657d9d 59 /** @} */
jksoft 0:76dfa9657d9d 60
jksoft 0:76dfa9657d9d 61 /**
jksoft 0:76dfa9657d9d 62 * @defgroup db_disc_enums Enumerations
jksoft 0:76dfa9657d9d 63 * @{
jksoft 0:76dfa9657d9d 64 */
jksoft 0:76dfa9657d9d 65
jksoft 0:76dfa9657d9d 66 /**@brief Type of the DB Discovery event.
jksoft 0:76dfa9657d9d 67 */
jksoft 0:76dfa9657d9d 68 typedef enum
jksoft 0:76dfa9657d9d 69 {
jksoft 0:76dfa9657d9d 70 BLE_DB_DISCOVERY_COMPLETE, /**< Event indicating that the GATT Database discovery is complete. */
jksoft 0:76dfa9657d9d 71 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.*/
jksoft 0:76dfa9657d9d 72 BLE_DB_DISCOVERY_SRV_NOT_FOUND /**< Event indicating that the service was not found at the peer.*/
jksoft 0:76dfa9657d9d 73 } ble_db_discovery_evt_type_t;
jksoft 0:76dfa9657d9d 74
jksoft 0:76dfa9657d9d 75 /** @} */
jksoft 0:76dfa9657d9d 76
jksoft 0:76dfa9657d9d 77 /**
jksoft 0:76dfa9657d9d 78 * @defgroup db_disc_structs Structures
jksoft 0:76dfa9657d9d 79 * @{
jksoft 0:76dfa9657d9d 80 */
jksoft 0:76dfa9657d9d 81
jksoft 0:76dfa9657d9d 82 /**@brief Structure for holding the characteristic and the handle of its CCCD found during the
jksoft 0:76dfa9657d9d 83 * discovery process.
jksoft 0:76dfa9657d9d 84 */
jksoft 0:76dfa9657d9d 85 typedef struct
jksoft 0:76dfa9657d9d 86 {
jksoft 0:76dfa9657d9d 87 ble_gattc_char_t characteristic; /**< Structure containing information about the characteristic. */
jksoft 0:76dfa9657d9d 88 uint16_t cccd_handle; /**< CCCD Handle value for this characteristic. This will be set to BLE_GATT_HANDLE_INVALID if a CCCD is not present at the server. */
jksoft 0:76dfa9657d9d 89 } ble_db_discovery_char_t;
jksoft 0:76dfa9657d9d 90
jksoft 0:76dfa9657d9d 91 /**@brief Structure for holding information about the service and the characteristics found during
jksoft 0:76dfa9657d9d 92 * the discovery process.
jksoft 0:76dfa9657d9d 93 */
jksoft 0:76dfa9657d9d 94 typedef struct
jksoft 0:76dfa9657d9d 95 {
jksoft 0:76dfa9657d9d 96 ble_uuid_t srv_uuid; /**< UUID of the service. */
jksoft 0:76dfa9657d9d 97 uint8_t char_count; /**< Number of characteristics present in the service. */
jksoft 0:76dfa9657d9d 98 ble_db_discovery_char_t charateristics[BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV]; /**< Array of information related to the characteristics present in the service. */
jksoft 0:76dfa9657d9d 99 ble_gattc_handle_range_t handle_range; /**< Service Handle Range. */
jksoft 0:76dfa9657d9d 100 } ble_db_discovery_srv_t;
jksoft 0:76dfa9657d9d 101
jksoft 0:76dfa9657d9d 102 /**@brief Structure for holding the information related to the GATT database at the server.
jksoft 0:76dfa9657d9d 103 *
jksoft 0:76dfa9657d9d 104 * @details This module identifies a remote database. Use one instance of this structure per
jksoft 0:76dfa9657d9d 105 * connection.
jksoft 0:76dfa9657d9d 106 *
jksoft 0:76dfa9657d9d 107 * @warning This structure must be zero-initialized.
jksoft 0:76dfa9657d9d 108 */
jksoft 0:76dfa9657d9d 109 typedef struct
jksoft 0:76dfa9657d9d 110 {
jksoft 0:76dfa9657d9d 111 ble_db_discovery_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.*/
jksoft 0:76dfa9657d9d 112 uint16_t conn_handle; /**< Connection handle as provided by the SoftDevice. */
jksoft 0:76dfa9657d9d 113 uint8_t srv_count; /**< Number of services at the peers GATT database.*/
jksoft 0:76dfa9657d9d 114 uint8_t curr_char_ind; /**< Index of the current characteristic being discovered. This is intended for internal use during service discovery.*/
jksoft 0:76dfa9657d9d 115 uint8_t curr_srv_ind; /**< Index of the current service being discovered. This is intended for internal use during service discovery.*/
jksoft 0:76dfa9657d9d 116 bool discovery_in_progress; /**< Variable to indicate if there is a service discovery in progress. */
jksoft 0:76dfa9657d9d 117 } ble_db_discovery_t;
jksoft 0:76dfa9657d9d 118
jksoft 0:76dfa9657d9d 119
jksoft 0:76dfa9657d9d 120 /**@brief Structure containing the event from the DB discovery module to the application.
jksoft 0:76dfa9657d9d 121 */
jksoft 0:76dfa9657d9d 122 typedef struct
jksoft 0:76dfa9657d9d 123 {
jksoft 0:76dfa9657d9d 124 ble_db_discovery_evt_type_t evt_type; /**< Type of event. */
jksoft 0:76dfa9657d9d 125 uint16_t conn_handle; /**< Handle of the connection for which this event has occurred. */
jksoft 0:76dfa9657d9d 126 union
jksoft 0:76dfa9657d9d 127 {
jksoft 0:76dfa9657d9d 128 ble_db_discovery_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.*/
jksoft 0:76dfa9657d9d 129 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. */
jksoft 0:76dfa9657d9d 130 } params;
jksoft 0:76dfa9657d9d 131 } ble_db_discovery_evt_t;
jksoft 0:76dfa9657d9d 132
jksoft 0:76dfa9657d9d 133 /** @} */
jksoft 0:76dfa9657d9d 134
jksoft 0:76dfa9657d9d 135 /**
jksoft 0:76dfa9657d9d 136 * @defgroup db_disc_types Types
jksoft 0:76dfa9657d9d 137 * @{
jksoft 0:76dfa9657d9d 138 */
jksoft 0:76dfa9657d9d 139
jksoft 0:76dfa9657d9d 140 /**@brief DB Discovery event handler type. */
jksoft 0:76dfa9657d9d 141 typedef void (* ble_db_discovery_evt_handler_t)(ble_db_discovery_evt_t * p_evt);
jksoft 0:76dfa9657d9d 142
jksoft 0:76dfa9657d9d 143 /** @} */
jksoft 0:76dfa9657d9d 144
jksoft 0:76dfa9657d9d 145 /**
jksoft 0:76dfa9657d9d 146 * @addtogroup db_disc_structs
jksoft 0:76dfa9657d9d 147 * @{
jksoft 0:76dfa9657d9d 148 */
jksoft 0:76dfa9657d9d 149
jksoft 0:76dfa9657d9d 150 /** @} */
jksoft 0:76dfa9657d9d 151
jksoft 0:76dfa9657d9d 152 /**
jksoft 0:76dfa9657d9d 153 * @defgroup db_disc_functions Functions
jksoft 0:76dfa9657d9d 154 * @{
jksoft 0:76dfa9657d9d 155 */
jksoft 0:76dfa9657d9d 156
jksoft 0:76dfa9657d9d 157 /**@brief Function for initializing the DB Discovery module.
jksoft 0:76dfa9657d9d 158 *
jksoft 0:76dfa9657d9d 159 * @retval NRF_SUCCESS on successful initialization.
jksoft 0:76dfa9657d9d 160 */
jksoft 0:76dfa9657d9d 161 uint32_t ble_db_discovery_init(void);
jksoft 0:76dfa9657d9d 162
jksoft 0:76dfa9657d9d 163
jksoft 0:76dfa9657d9d 164 /**@brief Function for closing the DB Discovery module.
jksoft 0:76dfa9657d9d 165 *
jksoft 0:76dfa9657d9d 166 * @details This function will clear up any internal variables and states maintained by the
jksoft 0:76dfa9657d9d 167 * module. To re-use the module after calling this function, the function @ref
jksoft 0:76dfa9657d9d 168 * ble_db_discovery_init must be called again.
jksoft 0:76dfa9657d9d 169 *
jksoft 0:76dfa9657d9d 170 * @retval NRF_SUCCESS Operation success.
jksoft 0:76dfa9657d9d 171 */
jksoft 0:76dfa9657d9d 172 uint32_t ble_db_discovery_close(void);
jksoft 0:76dfa9657d9d 173
jksoft 0:76dfa9657d9d 174
jksoft 0:76dfa9657d9d 175 /**@brief Function for registering with the DB Discovery module.
jksoft 0:76dfa9657d9d 176 *
jksoft 0:76dfa9657d9d 177 * @details The application can use this function to inform which service it is interested in
jksoft 0:76dfa9657d9d 178 * discovering at the server.
jksoft 0:76dfa9657d9d 179 *
jksoft 0:76dfa9657d9d 180 * @param[in] p_uuid Pointer to the UUID of the service to be discovered at the server.
jksoft 0:76dfa9657d9d 181 * @param[in] evt_handler Event handler to be called by the DB discovery module when any event
jksoft 0:76dfa9657d9d 182 * related to discovery of the registered service occurs.
jksoft 0:76dfa9657d9d 183 *
jksoft 0:76dfa9657d9d 184 * @note The total number of services that can be discovered by this module is @ref
jksoft 0:76dfa9657d9d 185 * BLE_DB_DISCOVERY_MAX_SRV. This effectively means that the maximum number of
jksoft 0:76dfa9657d9d 186 * registrations possible is equal to the @ref BLE_DB_DISCOVERY_MAX_SRV.
jksoft 0:76dfa9657d9d 187 *
jksoft 0:76dfa9657d9d 188 * @retval NRF_SUCCESS Operation success.
jksoft 0:76dfa9657d9d 189 * @retval NRF_ERROR_NULL When a NULL pointer is passed as input.
jksoft 0:76dfa9657d9d 190 * @retval NRF_ERROR_INVALID_STATE If this function is called without calling the
jksoft 0:76dfa9657d9d 191 * @ref ble_db_discovery_init.
jksoft 0:76dfa9657d9d 192 * @retval NRF_ERROR_NOT_SUPPORTED The maximum number of registrations allowed by this module
jksoft 0:76dfa9657d9d 193 * has been reached.
jksoft 0:76dfa9657d9d 194 */
jksoft 0:76dfa9657d9d 195 uint32_t ble_db_discovery_evt_register(const ble_uuid_t * const p_uuid,
jksoft 0:76dfa9657d9d 196 const ble_db_discovery_evt_handler_t evt_handler);
jksoft 0:76dfa9657d9d 197
jksoft 0:76dfa9657d9d 198
jksoft 0:76dfa9657d9d 199 /**@brief Function for starting the discovery of the GATT database at the server.
jksoft 0:76dfa9657d9d 200 *
jksoft 0:76dfa9657d9d 201 * @warning p_db_discovery structure must be zero-initialized.
jksoft 0:76dfa9657d9d 202 *
jksoft 0:76dfa9657d9d 203 * @param[out] p_db_discovery Pointer to the DB Discovery structure.
jksoft 0:76dfa9657d9d 204 * @param[in] conn_handle The handle of the connection for which the discovery should be
jksoft 0:76dfa9657d9d 205 * started.
jksoft 0:76dfa9657d9d 206 *
jksoft 0:76dfa9657d9d 207 * @retval NRF_SUCCESS Operation success.
jksoft 0:76dfa9657d9d 208 * @retval NRF_ERROR_NULL When a NULL pointer is passed as input.
jksoft 0:76dfa9657d9d 209 * @retval NRF_ERROR_INVALID_STATE If this function is called without calling the
jksoft 0:76dfa9657d9d 210 * @ref ble_db_discovery_init, or without calling
jksoft 0:76dfa9657d9d 211 * @ref ble_db_discovery_evt_register.
jksoft 0:76dfa9657d9d 212 * @retval NRF_ERROR_BUSY If a discovery is already in progress for the current
jksoft 0:76dfa9657d9d 213 * connection.
jksoft 0:76dfa9657d9d 214 *
jksoft 0:76dfa9657d9d 215 * @return This API propagates the error code returned by the
jksoft 0:76dfa9657d9d 216 * SoftDevice API @ref sd_ble_gattc_primary_services_discover.
jksoft 0:76dfa9657d9d 217 */
jksoft 0:76dfa9657d9d 218 uint32_t ble_db_discovery_start(ble_db_discovery_t * const p_db_discovery,
jksoft 0:76dfa9657d9d 219 uint16_t conn_handle);
jksoft 0:76dfa9657d9d 220
jksoft 0:76dfa9657d9d 221
jksoft 0:76dfa9657d9d 222 /**@brief Function for handling the Application's BLE Stack events.
jksoft 0:76dfa9657d9d 223 *
jksoft 0:76dfa9657d9d 224 * @param[in,out] p_db_discovery Pointer to the DB Discovery structure.
jksoft 0:76dfa9657d9d 225 * @param[in] p_ble_evt Pointer to the BLE event received.
jksoft 0:76dfa9657d9d 226 */
jksoft 0:76dfa9657d9d 227 void ble_db_discovery_on_ble_evt(ble_db_discovery_t * const p_db_discovery,
jksoft 0:76dfa9657d9d 228 const ble_evt_t * const p_ble_evt);
jksoft 0:76dfa9657d9d 229
jksoft 0:76dfa9657d9d 230 /** @} */
jksoft 0:76dfa9657d9d 231
jksoft 0:76dfa9657d9d 232 #endif // BLE_DB_DISCOVERY_H__
jksoft 0:76dfa9657d9d 233
jksoft 0:76dfa9657d9d 234 /** @} */