Nordic stack and drivers for the mbed BLE API

Dependents:   idd_hw5_bleFanProto

Fork of nRF51822 by Nordic Semiconductor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ble_db_discovery.h Source File

ble_db_discovery.h

Go to the documentation of this file.
00001 /* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
00002  *
00003  * The information contained herein is property of Nordic Semiconductor ASA.
00004  * Terms and conditions of usage are described in detail in NORDIC
00005  * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
00006  *
00007  * Licensees are granted free, non-transferable use of the information. NO
00008  * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
00009  * the file.
00010  */
00011 
00012 
00013 /**@file
00014  *
00015  * @defgroup ble_sdk_lib_db_discovery Database Discovery
00016  * @{
00017  * @ingroup  nrf51_sdk_api
00018  * @brief    Database discovery module.
00019  *
00020  * @details  This module contains the APIs and types exposed by the DB Discovery module. These APIs
00021  *           and types can be used by the application to perform discovery of a service and its
00022  *           characteristics at the peer server. This module can also be used to discover the 
00023  *           desired services in multiple remote devices.
00024  *           A typical use of this library is described in the figure below.
00025  *           @image html db_discovery.jpg
00026  *
00027  * @warning  The maximum number of characteristics per service that can be discovered by this module
00028  *           is indicated by the value of @ref BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV. If the peer
00029  *           has more than the supported number of characteristics, then the first found will be
00030  *           discovered and any further characteristics will be ignored. No descriptors other
00031  *           than Client Characteristic Configuration Descriptors will be searched for at the peer.
00032  *
00033  * @note     Presently only one instance of a Primary Service can be discovered by this module. If
00034  *           there are multiple instances of the service at the peer, only the first instance
00035  *           of it at the peer is fetched and returned to the application.
00036  *
00037  * @note     The application must propagate BLE stack events to this module by calling
00038  *           ble_db_discovery_on_ble_evt().
00039  *
00040  */
00041 
00042 #ifndef BLE_DB_DISCOVERY_H__
00043 #define BLE_DB_DISCOVERY_H__
00044 
00045 #include <stdint.h>
00046 #include "ble_gattc.h"
00047 #include "ble.h"
00048 #include "nrf_error.h"
00049 #include "ble_srv_common.h "
00050 
00051 /**
00052  * @defgroup db_disc_defines Defines
00053  * @{
00054  */
00055 
00056 #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). */
00057 #define BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV 3  /**< Maximum number of characteristics per service supported by this module. */
00058 
00059 /** @} */
00060 
00061 /**
00062  * @defgroup db_disc_enums Enumerations
00063  * @{
00064  */
00065 
00066 /**@brief   Type of the DB Discovery event.
00067  */
00068 typedef enum
00069 {
00070     BLE_DB_DISCOVERY_COMPLETE,      /**< Event indicating that the GATT Database discovery is complete. */
00071     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.*/
00072     BLE_DB_DISCOVERY_SRV_NOT_FOUND  /**< Event indicating that the service was not found at the peer.*/
00073 } ble_db_discovery_evt_type_t;
00074 
00075 /** @} */
00076 
00077 /**
00078  * @defgroup db_disc_structs Structures
00079  * @{
00080  */
00081 
00082 /**@brief Structure for holding the characteristic and the handle of its CCCD found during the
00083  *        discovery process.
00084  */
00085 typedef struct
00086 {
00087     ble_gattc_char_t characteristic;  /**< Structure containing information about the characteristic. */
00088     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. */
00089 } ble_db_discovery_char_t;
00090 
00091 /**@brief Structure for holding information about the service and the characteristics found during
00092  *        the discovery process.
00093  */
00094 typedef struct
00095 {
00096     ble_uuid_t               srv_uuid;                                           /**< UUID of the service. */    
00097     uint8_t                  char_count;                                         /**< Number of characteristics present in the service. */
00098     ble_db_discovery_char_t  charateristics[BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV];  /**< Array of information related to the characteristics present in the service. */
00099     ble_gattc_handle_range_t handle_range;                                       /**< Service Handle Range. */
00100 } ble_db_discovery_srv_t;
00101 
00102 /**@brief   Structure for holding the information related to the GATT database at the server.
00103  *
00104  * @details This module identifies a remote database. Use one instance of this structure per 
00105  *          connection.
00106  *
00107  * @warning This structure must be zero-initialized.
00108  */
00109 typedef struct
00110 {
00111     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.*/
00112     uint16_t               conn_handle;                         /**< Connection handle as provided by the SoftDevice. */
00113     uint8_t                srv_count;                           /**< Number of services at the peers GATT database.*/
00114     uint8_t                curr_char_ind;                       /**< Index of the current characteristic being discovered. This is intended for internal use during service discovery.*/
00115     uint8_t                curr_srv_ind;                        /**< Index of the current service being discovered. This is intended for internal use during service discovery.*/
00116     bool                   discovery_in_progress;               /**< Variable to indicate if there is a service discovery in progress. */
00117 } ble_db_discovery_t;
00118 
00119 
00120 /**@brief   Structure containing the event from the DB discovery module to the application.
00121  */
00122 typedef struct
00123 {
00124     ble_db_discovery_evt_type_t evt_type;     /**< Type of event. */
00125     uint16_t                    conn_handle;  /**< Handle of the connection for which this event has occurred. */
00126     union
00127     {
00128         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.*/
00129         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. */
00130     } params;
00131 } ble_db_discovery_evt_t;
00132 
00133 /** @} */
00134 
00135 /**
00136  * @defgroup db_disc_types Types
00137  * @{
00138  */
00139 
00140 /**@brief   DB Discovery event handler type. */
00141 typedef void (* ble_db_discovery_evt_handler_t)(ble_db_discovery_evt_t * p_evt);
00142 
00143 /** @} */
00144 
00145 /**
00146  * @addtogroup db_disc_structs
00147  * @{
00148  */
00149 
00150 /** @} */
00151 
00152 /**
00153  * @defgroup db_disc_functions Functions
00154  * @{
00155  */
00156 
00157 /**@brief     Function for initializing the DB Discovery module.
00158  *
00159  * @retval    NRF_SUCCESS on successful initialization.
00160  */
00161 uint32_t ble_db_discovery_init(void);
00162 
00163 
00164 /**@brief Function for closing the DB Discovery module.
00165  *
00166  * @details This function will clear up any internal variables and states maintained by the
00167  *          module. To re-use the module after calling this function, the function @ref
00168  *          ble_db_discovery_init must be called again.
00169  *
00170  * @retval  NRF_SUCCESS                 Operation success.
00171  */
00172 uint32_t ble_db_discovery_close(void);
00173 
00174 
00175 /**@brief Function for registering with the DB Discovery module.
00176  *
00177  * @details   The application can use this function to inform which service it is interested in 
00178  *            discovering at the server.
00179  *
00180  * @param[in] p_uuid             Pointer to the UUID of the service to be discovered at the server.
00181  * @param[in] evt_handler        Event handler to be called by the DB discovery module when any event 
00182  *                               related to discovery of the registered service occurs.
00183  *
00184  * @note      The total number of services that can be discovered by this module is @ref 
00185  *            BLE_DB_DISCOVERY_MAX_SRV. This effectively means that the maximum number of 
00186  *            registrations possible is equal to the @ref BLE_DB_DISCOVERY_MAX_SRV. 
00187  *
00188  * @retval    NRF_SUCCESS               Operation success.
00189  * @retval    NRF_ERROR_NULL            When a NULL pointer is passed as input.
00190  * @retval    NRF_ERROR_INVALID_STATE   If this function is called without calling the 
00191  *                                      @ref ble_db_discovery_init.
00192  * @retval    NRF_ERROR_NOT_SUPPORTED   The maximum number of registrations allowed by this module 
00193  *                                      has been reached.
00194  */
00195 uint32_t ble_db_discovery_evt_register(const ble_uuid_t * const             p_uuid,
00196                                        const ble_db_discovery_evt_handler_t evt_handler);
00197 
00198                                        
00199 /**@brief Function for starting the discovery of the GATT database at the server.
00200  *
00201  * @warning p_db_discovery structure must be zero-initialized.
00202  *
00203  * @param[out] p_db_discovery    Pointer to the DB Discovery structure.
00204  * @param[in]  conn_handle       The handle of the connection for which the discovery should be 
00205  *                               started.
00206  *
00207  * @retval    NRF_SUCCESS               Operation success.
00208  * @retval    NRF_ERROR_NULL            When a NULL pointer is passed as input.
00209  * @retval    NRF_ERROR_INVALID_STATE   If this function is called without calling the 
00210  *                                      @ref ble_db_discovery_init, or without calling 
00211  *                                      @ref ble_db_discovery_evt_register.
00212  * @retval    NRF_ERROR_BUSY            If a discovery is already in progress for the current 
00213  *                                      connection.
00214  *
00215  * @return                              This API propagates the error code returned by the 
00216  *                                      SoftDevice API @ref sd_ble_gattc_primary_services_discover.
00217  */
00218 uint32_t ble_db_discovery_start(ble_db_discovery_t * const p_db_discovery,
00219                                 uint16_t                   conn_handle);
00220 
00221                                 
00222 /**@brief Function for handling the Application's BLE Stack events.
00223  *
00224  * @param[in,out] p_db_discovery Pointer to the DB Discovery structure.
00225  * @param[in]     p_ble_evt      Pointer to the BLE event received.
00226  */
00227 void ble_db_discovery_on_ble_evt(ble_db_discovery_t * const p_db_discovery,
00228                                  const ble_evt_t * const    p_ble_evt);
00229 
00230 /** @} */
00231 
00232 #endif // BLE_DB_DISCOVERY_H__
00233 
00234 /** @} */