BLE FOTA APP

Dependencies:   BLE_API mbed

It doesn't work with the default FOTA bootloader. It use NVIC_SystemReset() to enter a bootloader.

Committer:
yihui
Date:
Fri Oct 10 03:36:28 2014 +0000
Revision:
1:a607cd9655d7
use NVIC_SystemReset() to run bootloader

Who changed what in which revision?

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