from bbc microbit library

Fork of nrf51-sdk by Lancaster University

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers device_manager.h Source File

device_manager.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) Nordic Semiconductor ASA
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without modification,
00006  * are permitted provided that the following conditions are met:
00007  *
00008  *   1. Redistributions of source code must retain the above copyright notice, this
00009  *   list of conditions and the following disclaimer.
00010  *
00011  *   2. Redistributions in binary form must reproduce the above copyright notice, this
00012  *   list of conditions and the following disclaimer in the documentation and/or
00013  *   other materials provided with the distribution.
00014  *
00015  *   3. Neither the name of Nordic Semiconductor ASA nor the names of other
00016  *   contributors to this software may be used to endorse or promote products
00017  *   derived from this software without specific prior written permission.
00018  *
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00021  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00022  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00023  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00024  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00025  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00026  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00027  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030  *
00031  */
00032 
00033 /**
00034  * @file device_manager.h
00035  *
00036  * @defgroup device_manager Device Manager
00037  * @ingroup ble_sdk_lib
00038  * @{
00039  * @brief Device Manager Application Interface Abstraction.
00040  *
00041  * @details The Device Manager module manages Active and Bonded Peers. Management of peer includes
00042  *          book keeping of contextual information like the Security Keys, GATT
00043  *          configuration and any application specific information.
00044  *
00045  *          Active Peers are devices which are connected, and may or may not be bonded.
00046  *          Bonded Peers are devices which are bonded, and may or may not be Active (Connected).
00047  *          Active Bonded Peer refers to a device which is connected and bonded.
00048  *
00049  *          Paired Devices refers to peer devices that are connected and have necessary context
00050  *          establishment/exchange for the current connection session. On disconnect,
00051  *          all contextual information is flushed. For example, SMP Information Exchanged during
00052  *          pairing and GATT Configuration is not retained on disconnection.
00053  *
00054  *          Note that this module allows management of contextual information but 
00055  *          does not provide an interface for connection management. Therefore, entering connectible
00056  *          mode, connection establishment, or disconnection of a link with peer is not in scope
00057  *          of this module.
00058  *
00059  *          For bonded peers, the contextual information is required to be retained on disconnection
00060  *          and power cycling. Persistent storage of contextual information is handled by the
00061  *          module. This module categorizes the contextual information into 3 categories:
00062  *             - <b>Bonding Information</b>
00063  *               Bond information is the information exchanged between local and peer device to
00064  *               establish a bond. It also includes peer identification information,
00065  *               like the peer address or the IRK or both. From here on this category of information
00066  *               is referred to as Device Context.
00067  *             - <b>Service/Protocol Information</b>
00068  *               Service/Protocol information is the information retained for the peer to save on one-time
00069  *               procedures like the GATT Service Discovery procedures and Service Configurations.
00070  *               It allows devices to resume data exchange on subsequent reconnection without having
00071  *               to perform initial set-up procedures each time. From here on this category is
00072  *               referred to as Service Context.
00073  *             - <b>Application Information</b>
00074  *               Application information is the context that the application would like to associate with
00075  *               each of the bonded device. For example, if the application chooses to rank its peers
00076  *               in order to manage them better, the rank information could be treated as
00077  *               Application Information. This storage space is provided to save the application from
00078  *               maintaining a mapping table with each Device Instance and Application Information.
00079  *               However, if the application have no use for this, it is possible to not
00080  *               use or employ this at compile time. From here on this category of information is
00081  *               referred to as Application Context.
00082  */
00083 
00084 
00085 #ifndef DEVICE_MANAGER_H__
00086 #define DEVICE_MANAGER_H__
00087 
00088 #include <stdint.h>
00089 #include <stdbool.h>
00090 #include "sdk_common.h"
00091 #include "ble.h"
00092 #include "ble_gap.h"
00093 #include "device_manager_cnfg.h "
00094 
00095 /**
00096  * @defgroup dm_service_cntext_types Service/Protocol Types
00097  *
00098  * @brief Describes the possible types of Service/Protocol Contexts for a bonded/peer device.
00099  *
00100  * @details Possible Service/Protocol context per peer device. The Device Manager provides the
00101  *          functionality of persistently storing the Service/Protocol context and can automatically
00102  *          load them when needed.
00103  *          For example system attributes for a GATT Server. Based on the nature of the application, 
00104  *          not all service types may be needed. The application can specify
00105  *          only the service/protocol context it wants to use at the time of registration.
00106  * @{
00107  */
00108 #define DM_PROTOCOL_CNTXT_NONE         0x00  /**< No Service Context, this implies the application does not want to associate any service/protocol context with the peer device */
00109 #define DM_PROTOCOL_CNTXT_GATT_SRVR_ID 0x01  /**< GATT Server Service Context, this implies the application does associate GATT Server with the peer device and this information will be loaded when needed for a bonded device */
00110 #define DM_PROTOCOL_CNTXT_GATT_CLI_ID  0x02  /**< GATT Client Service Context, this implies the application does associate GATT Client with the peer device and this information will be loaded when needed for a bonded device */
00111 #define DM_PROTOCOL_CNTXT_ALL                                                                     \
00112         (DM_PROTOCOL_CNTXT_GATT_SRVR_ID | DM_PROTOCOL_CNTXT_GATT_CLI_ID) /**< All Service/Protocol Context, this implies that the application wants to associate all Service/Protocol Information with the bonded device. This is configurable based on system requirements. If the application has only one type of service, this define could be altered to reflect the same.  */
00113 /** @} */
00114 
00115 
00116 /**
00117  * @defgroup dm_events Device Manager Events
00118  *
00119  * @brief This section describes the device manager events that are notified to the application.
00120  *
00121  * @details The Device Manager notifies the application of various asynchronous events using the
00122  *          asynchronous event notification callback. All events has been categorized into:
00123  *          a. General.
00124  *          b. Link Status.
00125  *          c. Context Management.
00126  *
00127  *          In the callback, these events are notified along with handle that uniquely identifies:
00128  *          application instance, active instance (if applicable), device instance
00129  *          bonding instance, (if applicable) and service instance.
00130  *          Not all events are pertaining to an active connection, for example a context deletion event could occur even if the peer
00131  *          is not connected. Also, general category of events may not be pertaining to any specific peer.
00132  *          See also \ref dm_event_cb_t and \ref dm_register.
00133  * @{
00134  */
00135 /**
00136  * @defgroup general_events General Events
00137  *
00138  * @brief General or miscellaneous events.
00139  *
00140  * @details This category of events are general events not pertaining to a peer or context.
00141  *
00142  * @{
00143  */
00144 #define DM_EVT_RFU   0x00 /**< Reserved for future use, is never notified. */
00145 #define DM_EVT_ERROR 0x01 /**< Device Manager Event Error. */
00146 /** @} */
00147 
00148 /**
00149  * @defgroup link_status_events Link Status Events
00150  *
00151  * @brief Link Status Events.
00152  *
00153  * @details This category of events notify the application of the link status. Event result associated
00154  *          with the event is provided along with the event in the callback to provide more details of
00155  *          whether a procedure succeeded or failed and assist the application in decision making of
00156  *          how to proceed. For example if a DM_DEVICE_CONNECT_IND is indicated with NRF_SUCCESS
00157  *          result, the application may want to proceed with discovering and association with
00158  *          service of the peer. However, if indicated with a failure result, the application may
00159  *          want to take an alternate action such as reattempting to connect or go into a
00160  *          sleep mode.
00161  *
00162  * @{
00163  */
00164 #define DM_EVT_CONNECTION              0x11 /**< Indicates that link with the peer is established. */
00165 #define DM_EVT_DISCONNECTION           0x12 /**< Indicates that link with peer is torn down. */
00166 #define DM_EVT_SECURITY_SETUP          0x13 /**< Security procedure for link started indication */
00167 #define DM_EVT_SECURITY_SETUP_COMPLETE 0x14 /**< Security procedure for link completion indication. */
00168 #define DM_EVT_LINK_SECURED            0x15 /**< Indicates that link with the peer is secured. For bonded devices, subsequent reconnections with bonded peer will result only in this event when the link is secured and setup procedures will not occur unless the bonding information is either lost or deleted on either or both sides.  */
00169 #define DM_EVT_SECURITY_SETUP_REFRESH  0x16 /**< Indicates that the security on the link was re-established. */
00170 /** @} */
00171 
00172 /**
00173  * @defgroup context_mgmt_events Context Management Events
00174  *
00175  * @brief Context Management Events.
00176  *
00177  * @details These events notify the application of the status of context loading and storing.
00178  *
00179  * @{
00180  */
00181 #define DM_EVT_DEVICE_CONTEXT_LOADED   0x21 /**< Indicates that device context for a peer is loaded. */
00182 #define DM_EVT_DEVICE_CONTEXT_STORED   0x22 /**< Indicates that device context is stored persistently. */
00183 #define DM_EVT_DEVICE_CONTEXT_DELETED  0x23 /**< Indicates that device context is deleted. */
00184 #define DM_EVT_SERVICE_CONTEXT_LOADED  0x31 /**< Indicates that service context for a peer is loaded. */
00185 #define DM_EVT_SERVICE_CONTEXT_STORED  0x32 /**< Indicates that service context is stored persistently. */
00186 #define DM_EVT_SERVICE_CONTEXT_DELETED 0x33 /**< Indicates that service context is deleted. */
00187 #define DM_EVT_APPL_CONTEXT_LOADED     0x41 /**< Indicates that application context for a peer is loaded. */
00188 #define DM_EVT_APPL_CONTEXT_STORED     0x42 /**< Indicates that application context is stored persistently. */
00189 #define DM_EVT_APPL_CONTEXT_DELETED    0x43 /**< Indicates that application context is deleted. */
00190 /** @} */
00191 /** @} */
00192 
00193 #define DM_INVALID_ID 0xFF /**< Invalid instance idenitifer. */
00194 
00195 /**
00196  * @defgroup dm_data_structure Device Manager Data Types
00197  *
00198  * @brief This section describes all the data types exposed by the module to the application.
00199  * @{
00200  */
00201 
00202 /**
00203  * @brief Application Instance.
00204  *
00205  * @details Application instance uniquely identifies an application. The identifier is allocated by
00206  *          the device manager when application registers with the module. The application is
00207  *          expected to identify itself with this instance identifier when initiating subsequent
00208  *          requests. Application should use the utility API \ref dm_application_instance_set in
00209  *          order to set its application instance in dm_handle_t needed for all subsequent APIs.
00210  *          See also \ref dm_register.
00211  */
00212 typedef uint8_t dm_application_instance_t;
00213 
00214 /**
00215  * @brief Connection Instance.
00216  *
00217  * @details Identifies connection instance for an active device. This instance is allocated by the 
00218  *          device manager when a connection is established and is notified with DM_EVT_CONNECTION
00219  *          with the event result NRF_SUCCESS.
00220  */
00221 typedef uint8_t dm_connection_instance_t;
00222 
00223 /**
00224  * @brief Device Instance.
00225  *
00226  * @details Uniquely identifies a bonded peer device. The peer device may or may not be connected.
00227  *          In case of the central: The bonded device instance to identify the peer is allocated when bonding procedure is initiated by the central using dm_security_setup_req.
00228  *          In case of the peripheral: When the bonding procedure is successful, the DM_EVT_SECURITY_SETUP_COMPLETE event with success event result, is received.
00229  *          In case the module cannot add more bonded devices, no instance is allocated, this is indicated by an appropriate error code for the API/event as the case may be. Application can choose to disconnect the link.
00230  */
00231 typedef uint8_t dm_device_instance_t;
00232 
00233 /**
00234  * @brief Service Instance.
00235  *
00236  * @details Uniquely identifies a peer device. The peer device may or may not be connected. This
00237  *          instance is allocated by the device manager when a device is bonded and is notified
00238  *          when security procedures have been initiated.
00239  *          Security Procedures initiation is notified with DM_SECURITY_SETUP_IND with
00240  *          success event result. In case the event result indicates that the module cannot add more
00241  *          bonded devices, no instance is allocated. Application can chose to disconnect the link.
00242  */
00243 typedef uint8_t dm_service_instance_t;
00244 
00245 /**
00246  * @brief Service/Protocol Type Identifier.
00247  *
00248  * @details Uniquely identifies a service or a protocol type. Service/Protocol Type identification
00249  *          is needed as each service/protocol can have its own contextual data.
00250  *          This allows the peer to access more than one service at a time. \ref dm_service_cntext_types describes the
00251  *          list of services/protocols supported.
00252  */
00253 typedef uint8_t service_type_t;
00254 
00255 /**@brief Device Manager Master identification and encryption information. */
00256 typedef struct dm_enc_key
00257 {
00258     ble_gap_enc_info_t  enc_info;  /**< GAP encryption information. */
00259     ble_gap_master_id_t master_id; /**< Master identification. */
00260 } dm_enc_key_t;
00261 
00262 /** @brief Device Manager identity and address information. */
00263 typedef struct dm_id_key
00264 {
00265   ble_gap_irk_t  id_info;      /**< Identity information. */
00266   ble_gap_addr_t id_addr_info; /**< Identity address information. */
00267 } dm_id_key_t;
00268 
00269 /** @brief Device Manager signing information. */
00270 typedef struct dm_sign_key
00271 {
00272     ble_gap_sign_info_t sign_key; /**< GAP signing information. */
00273 } dm_sign_key_t;
00274 
00275 /** @brief Security keys. */
00276 typedef struct dm_sec_keyset
00277 {
00278     union 
00279     {
00280         dm_enc_key_t       * p_enc_key;  /**< Pointer to Device Manager encryption information structure. */
00281     } enc_key;
00282     dm_id_key_t   * p_id_key;            /**< Identity key, or NULL. */
00283     dm_sign_key_t * p_sign_key;          /**< Signing key, or NULL. */
00284 } dm_sec_keys_t;
00285 
00286 /** @brief Device Manager security key set. */
00287 typedef struct
00288 {
00289   dm_sec_keys_t keys_periph;  /**< Keys distributed by the device in the Peripheral role. */
00290   dm_sec_keys_t keys_central; /**< Keys distributed by the device in the Central role. */
00291 } dm_sec_keyset_t;
00292 
00293 /**
00294  * @brief Device Handle used for unique identification of each peer.
00295  *
00296  * @details This data type is used to uniquely identify each peer device. A peer device could be
00297  *          active and/or bonded. Therefore an instance for active and bonded is provided.
00298  *          However, the application is expected to treat this is an opaque structure and use this for
00299  *          all API interactions once stored on appropriate events.
00300  *          See \ref dm_events.
00301  */
00302 typedef struct device_handle
00303 {
00304     dm_application_instance_t    appl_id;       /**< Identifies the application instances for the device that is being managed. */
00305     dm_connection_instance_t     connection_id; /**< Identifies the active connection instance. */
00306     dm_device_instance_t         device_id;     /**< Identifies peer instance in the data base. */
00307     dm_service_instance_t        service_id;    /**< Service instance identifier. */
00308 } dm_handle_t;
00309 
00310 /**
00311  * @brief Definition of Data Context.
00312  *
00313  * @details Defines contextual data format, it consists of context data length and pointer to data.
00314  */
00315 typedef struct
00316 {
00317     uint32_t  flags;  /**< Additional flags identifying data. */
00318     uint32_t  len;    /**< Length of data. */
00319     uint8_t * p_data; /**< Pointer to contextual data, a copy is made of the data. */
00320 } dm_context_t;
00321 
00322 
00323 /**
00324  * @brief Device Context.
00325  *
00326  * @details Defines "device context" type for a device managed by device manager.
00327  */
00328 typedef dm_context_t dm_device_context_t;
00329 
00330 /**
00331  * @brief Service Context.
00332  *
00333  * @details Service context data for a service identified by the 'service_type' field.
00334  */
00335 typedef struct
00336 {
00337     service_type_t service_type; /**< Identifies the service/protocol to which the context data is related. */
00338     dm_context_t   context_data; /**< Contains length and pointer to context data */
00339 } dm_service_context_t;
00340 
00341 /**
00342  * @brief Application context.
00343  *
00344  * @details The application context can be used by the application to map any application level
00345  *          information that is to be mapped with a particular peer.
00346  *          For bonded peers, this information will be stored by the bond manager persistently.
00347  *          Note that the device manager treats this information as an
00348  *          opaque block of bytes.
00349  *          Necessary APIs to get and set this context for a peer have been provided.
00350  */
00351 typedef dm_context_t dm_application_context_t;
00352 
00353 /**
00354  * @brief Event parameters.
00355  *
00356  * @details Defines event parameters for each of the events notified by the module.
00357  */
00358 typedef union
00359 {
00360     ble_gap_evt_t            * p_gap_param;       /**< All events that are triggered in device manager as a result of GAP events, like connection, disconnection and security procedures are accompanied with GAP parameters. */
00361     dm_application_context_t * p_app_context;     /**< All events that are associated with application context procedures of store, load, and deletion have this as event parameter. */
00362     dm_service_context_t     * p_service_context; /**< All events that are associated with service context procedures of store, load and deletion have this as event parameter. */
00363     dm_device_context_t      * p_device_context;  /**< All events that are associated with device context procedures of store, load and deletion have this as event parameter. */
00364 } dm_event_param_t;
00365 
00366 /**
00367  * @brief Asynchronous events details notified to the application by the module.
00368  *
00369  * @details Defines event type along with event parameters notified to the application by the
00370  *          module.
00371  */
00372 typedef struct
00373 {
00374     uint8_t          event_id;       /**< Identifies the event. See \ref dm_events for details on event types and their significance. */
00375     dm_event_param_t event_param;    /**< Event parameters. Can be NULL if the event does not have any parameters. */
00376     uint16_t         event_paramlen; /**< Length of the event parameters, is zero if the event does not have any parameters. */
00377 } dm_event_t;
00378 
00379 /**
00380  * @brief Event notification callback registered by application with the module.
00381  *
00382  * @details Event notification callback registered by application with the module when registering
00383  *          the module using \ref dm_register API.
00384  *
00385  * @param[in] p_handle   Identifies the peer for which the event is being notified.
00386  * @param[in] p_event    Identifies the event, any associated parameters and parameter length.
00387  *                       See \ref dm_events for details on event types and their significance.
00388  * @param[in,out] event_result   Provide additional information on the event. 
00389  *                      In addition to SDK error codes there is also a return value
00390  *                      indicating if maximum number of connections has been reached when connecting or bonding.
00391  *
00392  * @retval NRF_SUCCESS on success, or a failure to indicate if it could handle the event
00393  *         successfully. There is no action taken in case application returns a failure.
00394  */
00395 typedef ret_code_t (*dm_event_cb_t)(dm_handle_t const * p_handle,
00396                                     dm_event_t const  * p_event,
00397                                     ret_code_t        event_result);
00398 
00399 /**
00400  * @brief Initialization Parameters.
00401  *
00402  * @details Indicates the application parameters. Currently this only encompasses clearing
00403  *          all persistent data.
00404  */
00405 typedef struct
00406 {
00407     bool clear_persistent_data; /**< Set to true in case the module should clear all persistent data. */
00408 } dm_init_param_t;
00409 
00410 /**
00411  * @brief Application Registration Parameters.
00412  *
00413  * @details Parameters needed by the module when registering with it.
00414  */
00415 typedef struct
00416 {
00417     dm_event_cb_t        evt_handler;  /**< Event Handler to be registered. It will receive asynchronous notification from the module, see \ref dm_events for asynchronous events. */
00418     uint8_t              service_type; /**< Bit mask identifying services that the application intends to support for all peers. */
00419     ble_gap_sec_params_t sec_param;    /**< Security parameters to be used for the application. */
00420 } dm_application_param_t;
00421 
00422 /**
00423  * @brief Defines possible security status/states.
00424  *
00425  * @details Defines possible security status/states of a link when requested by application using
00426  *          the \ref dm_security_status_req.
00427  */
00428 typedef enum
00429 {
00430     NOT_ENCRYPTED,          /**< The link is not secured. */
00431     ENCRYPTION_IN_PROGRESS, /**< Link security is being established.*/
00432     ENCRYPTED               /**< The link is secure.*/
00433 } dm_security_status_t;
00434 /** @} */
00435 
00436 /**
00437  * @defgroup dm_api Device Module APIs
00438  *
00439  * @brief This section describes APIs exposed by the module.
00440  *
00441  * @details This section describes APIs exposed by the module. The APIs have been categorized to provide
00442  *          better and specific look up for developers. Categories are:
00443  *          - Set up APIs.
00444  *          - Context Management APIs.
00445  *          - Utility APIs.
00446  *
00447  *          MSCs describe usage of these APIs.  
00448  *          See @ref dm_msc.
00449  * @{
00450  */
00451 /**
00452  * @defgroup dm_setup_api Device Module Set-up APIs
00453  *
00454  * @brief Initialization & registration APIs that are pre-requisite for all other module procedures.
00455  * @details This section describes the Module Initialization and Registration APIs needed to be set up by
00456  *          the application before device manager can start managing devices and device contexts
00457  *          for the application.
00458  *
00459  * @{
00460  */
00461 
00462 /**
00463  * @brief Module Initialization Routine.
00464  *
00465  * @details Function for initializing the module. Must called before any other APIs of the module are used.
00466  *
00467  * @param[in] p_init_param Initialization parameters.
00468  *
00469  * @retval NRF_SUCCESS On success, else an error code indicating reason for failure.
00470  *
00471  * @note It is mandatory that pstorage is initialized before initializing this module.
00472  */
00473 ret_code_t dm_init(dm_init_param_t const * p_init_param);
00474 
00475 /**
00476  * @brief Function for registering the application.
00477  *
00478  * @details This routine is used by the application to register for asynchronous events with the
00479  *          device manager. During registration the application also indicates the services that it
00480  *          intends to support on this instance. It is possible to register multiple times with the
00481  *          device manager. At least one instance shall be registered with the device manager after
00482  *          the module has been initialized.
00483  *          Maximum number of application instances device manager can support is determined
00484  *          by DM_MAX_APPLICATIONS.
00485  *
00486  *          All applications must be registered before initiating or accepting connections from the peer.     
00487  *
00488  * @param[in]  p_appl_param    Application parameters.
00489  * @param[out] p_appl_instance Application Instance Identifier in case registration is successful.
00490  *
00491  * @retval NRF_SUCCESS             On success, else an error code indicating reason for failure.
00492  * @retval NRF_ERROR_INVALID_STATE If the API is called without module initialization.
00493  * @retval NRF_ERROR_NO_MEM        If module cannot support more applications.
00494  *
00495  * @note Currently only one application instance is supported by the module.
00496  */
00497 ret_code_t dm_register(dm_application_instance_t    * p_appl_instance,
00498                        dm_application_param_t const * p_appl_param);
00499 
00500 /**
00501  * @brief Function for handling BLE events.
00502  *
00503  * @details BLE Event Handler for the module. This routine should be called from BLE stack event
00504  *          dispatcher for the module to work as expected.
00505  *
00506  * @param[in] p_ble_evt BLE stack event being dispatched to the function.
00507  *
00508  */
00509 void dm_ble_evt_handler(ble_evt_t * p_ble_evt);
00510 
00511 /** @} */
00512 
00513 
00514 /**
00515  * @defgroup dm_security_api APIs to set up or read status of security on a link.
00516  *
00517  * @brief This section describes APIs to set up Security. These APIs require that the peer is
00518  *        connected before the procedures can be requested.
00519  *
00520  * @details This group allows application to request security procedures
00521  *          or get the status of the security on a link.
00522  * @{
00523  */
00524 /**
00525  * @brief Function for requesting setting up security on a link.
00526  *
00527  * @details This API initiates security procedures with a peer device.
00528  *          @note For the GAP Central role, in case peer is not bonded, request to bond/pair is
00529  *          initiated. If it is bonded, the link is re-encrypted using the existing bond information.
00530  *          For the GAP peripheral role, a Slave security request is sent.
00531  * @details If a pairing procedure is initiated successfully, application is notified of
00532  *          @ref DM_EVT_SECURITY_SETUP_COMPLETE. A result indicating success or failure is notified along with the event.
00533  *          In case the link is re-encrypted using existing bond information, @ref DM_EVT_LINK_SECURED is
00534  *          notified to the application.
00535  *
00536  * @param[in] p_handle Identifies the link on which security is desired.
00537  *
00538  * @retval NRF_SUCCESS             On success, else an error code indicating reason for failure.
00539  * @retval NRF_ERROR_INVALID_STATE If the API is called without module initialization and/or
00540  *                                 application registration.
00541  * @retval NRF_ERROR_NULL          If p_handle is NULL.
00542  * @retval NRF_ERROR_INVALID_ADDR  If the peer is not identified by the handle provided by the application
00543  *                                 or if the peer is not connected when this procedure is requested.
00544  */
00545 ret_code_t dm_security_setup_req(dm_handle_t * p_handle);
00546 
00547 /**
00548  * @brief Function for reading the status of the security on a link.
00549  *
00550  * @details This API allows application to query status of security on a link.
00551  *
00552  * @param[in]  p_handle  Identifies the link on which security is desired.
00553  * @param[out] p_status  Pointer where security status is provided to the application.
00554  *                       See \ref dm_security_status_t for possible statuses that can be expected.
00555  *
00556  * @retval NRF_SUCCESS             Or appropriate error code indicating reason for failure.
00557  * @retval NRF_ERROR_INVALID_STATE If the API is called without module initialization and/or
00558  *                                 application registration.
00559  * @retval NRF_ERROR_NULL          If p_handle or p_status is NULL.
00560  * @retval NRF_ERROR_INVALID_ADDR  If peer is not identified by the handle provided by the application
00561  *                                 or if peer is not connected when this procedure is requested.
00562  */
00563 ret_code_t dm_security_status_req(dm_handle_t const * p_handle, dm_security_status_t * p_status);
00564 
00565 /**
00566  * @brief Function for creating the whitelist.
00567  *
00568  * @details This API allows application to create whitelist based on bonded peer devices in module
00569  *          data base.
00570  *
00571  * @param[in]  p_handle       Identifies the application requesting whitelist creation.
00572  * @param[in,out] p_whitelist Pointer where created whitelist is provided to the application.
00573  *
00574  * @note 'addr_count' and 'irk_count' fields of the structure should be populated with the maximum
00575  *       number of devices that the application wishes to request in the whitelist. 
00576  *       If the number of bonded devices is less than requested, the fields are updated with that number of devices.
00577  *       If the number of devices are more than requested, the module will populate the list
00578  *       with devices in the order the bond was established with the peer devices. Also, if this routine is
00579  *       called when a connection exists with one or more peer devices,
00580  *       those connected devices are not added to the whitelist.
00581  *
00582  * @retval NRF_SUCCESS             On success, else an error code indicating reason for failure.
00583  * @retval NRF_ERROR_INVALID_STATE If the API is called without module initialization and/or
00584  *                                 application registration.
00585  * @retval NRF_ERROR_NULL          If p_handle or p_whitelist is NULL.
00586  */
00587 ret_code_t dm_whitelist_create(dm_application_instance_t const * p_handle,
00588                                ble_gap_whitelist_t             * p_whitelist);
00589 
00590 /** @} */
00591 
00592 
00593 /**
00594  * @defgroup dm_cntxt_mgmt_api Context Management APIs
00595  *
00596  * @brief Utility APIs offered by the device manager to get information about the peer if and
00597  *        when needed.
00598  *
00599  * @details This group of API allow the application to access information that is not required to be
00600  *          maintained by the application but may be needed. Hence it is possible to get the
00601  *          information from the module instead of mapping all the information with a device
00602  *          context.
00603  * @{
00604  */
00605 
00606 ret_code_t dm_device_add(dm_handle_t               * p_handle,
00607                          dm_device_context_t const * p_context);
00608 
00609 /**
00610  * @brief Function for deleting a peer device context and all related information from the database.
00611  *
00612  * @details Delete peer device context and all related information from database. If
00613  *          this API returns NRF_SUCCESS, DM_EVT_DEVICE_CONTEXT_DELETED event is notified to the
00614  *          application. Event result notified along with the event indicates success or failure
00615  *          of this procedure.
00616  *
00617  * @param[in] p_handle Identifies the peer device to be deleted.
00618  *
00619  * @retval NRF_SUCCESS             on success, else an error code indicating reason for failure.
00620  * @retval NRF_ERROR_INVALID_STATE In the API is called without module initialization and/or
00621  *                                 application registration.
00622  * @retval NRF_ERROR_NULL          If p_handle is NULL.
00623  * @retval NRF_ERROR_INVALID_ADDR  If peer is not identified the handle provided by the application.
00624  *
00625  * @note Deleting device context results in deleting service and application context for the
00626  *       bonded device. The respective events DM_EVT_SERVICE_CONTEXT_DELETED and
00627  *       DM_EVT_APPL_CONTEXT_DELETED are not notified to the application.
00628  */
00629 ret_code_t dm_device_delete(dm_handle_t const * p_handle);
00630 
00631 /**
00632  * @brief Function for deleting all peer device context and all related information from the database.
00633  *
00634  * @details Delete peer device context and all related information from database. If
00635  *          this API returns NRF_SUCCESS, DM_EVT_DEVICE_CONTEXT_DELETED event is notified to the
00636  *          application for each device that is deleted from the data base. Event result
00637  *          notified along with the event indicates success or failure of this procedure.
00638  *
00639  * @param[in] p_handle Identifies application instance that is requesting
00640  *                     the deletion of all bonded devices.
00641  *
00642  * @retval NRF_SUCCESS             On success, else an error code indicating reason for failure.
00643  * @retval NRF_ERROR_INVALID_STATE If the API is called without module initialization and/or
00644  *                                 application registration.
00645  * @retval NRF_ERROR_NULL          If p_handle is NULL.
00646  * @retval NRF_ERROR_INVALID_ADDR  If peer is not identified the handle provided by the application.
00647  *
00648  * @note Deleting device context results in deleting both service and application context for the
00649  *       bonded device. The respective events DM_EVT_SERVICE_CONTEXT_DELETED and
00650  *       DM_EVT_APPL_CONTEXT_DELETED are not notified to the application.
00651  */
00652 ret_code_t dm_device_delete_all(dm_application_instance_t const * p_handle);
00653 
00654 /**
00655  * @brief Function for setting Service Context for a peer device identified by 'p_handle' parameter.
00656  *
00657  * @details This API allows application to Set Service Context for a peer device identified by the
00658  *          'p_handle' parameter. This API is useful when the Service Context cannot be requested
00659  *          from the SoftDevice, but needs to be assembled by the application or an another module.
00660  *          (or when service context is exchanged in an out of band way.)
00661  *          This API could also be used to trigger a storing of service context into persistent
00662  *          memory. If this is desired, a NULL pointer could be passed to the p_context.
00663  *
00664  * @param[in] p_handle  Identifies peer device for which the procedure is requested.
00665  * @param[in] p_context Service context being set. The context information includes length of
00666  *                      data and pointer to the contextual data being set. The memory pointed to by
00667  *                      the pointer to data is assumed to be resident when API is being called and
00668  *                      can be freed or reused once the set procedure is complete. Set procedure
00669  *                      completion is indicated by the event \ref DM_EVT_SERVICE_CONTEXT_STORED.
00670  *                      The Event result is notified along with the event and indicates success or failure of
00671  *                      this procedure.
00672  *
00673  * @retval NRF_SUCCESS             On success, else an error code indicating reason for failure.
00674  * @retval NRF_ERROR_INVALID_STATE If the API is called without module initialization and/or
00675  *                                 application registration.
00676  * @retval NRF_ERROR_NULL          If p_handle is NULL.
00677  * @retval NRF_ERROR_INVALID_ADDR  If the peer is not identified by the handle provided by the application.
00678  */
00679 ret_code_t dm_service_context_set(dm_handle_t const          * p_handle,
00680                                   dm_service_context_t const * p_context);
00681 
00682 /**
00683  * @brief Function for getting Service Context for a peer device identified by 'p_handle' parameter.
00684  *
00685  * @details Get Service Context for a peer device identified by the 'p_handle' parameter. If
00686  *          this API returns NRF_SUCCESS, DM_EVT_SERVICE_CONTEXT_LOADED event is notified to the
00687  *          application. The event result is notified along with the event indicates success or failure
00688  *          of this procedure.
00689  *
00690  * @param[in] p_handle  Identifies peer device for which procedure is requested.
00691  * @param[in] p_context Application context being requested. The context information includes length
00692  *                      of the data and a pointer to the data. Note that requesting a 'get'
00693  *                      of application does not need to provide memory, the pointer to data will be
00694  *                      pointing to service data and hence no data movement is involved.
00695  *
00696  * @retval NRF_SUCCESS             On success, else an error code indicating reason for failure.
00697  * @retval NRF_ERROR_INVALID_STATE In case API is called without module initialization and/or
00698  *                                 application registration.
00699  * @retval NRF_ERROR_NULL          If p_handle is NULL.
00700  * @retval NRF_ERROR_INVALID_ADDR  If the peer is not identified by the handle provided by the application.
00701  */
00702 ret_code_t dm_service_context_get(dm_handle_t const    * p_handle,
00703                                   dm_service_context_t * p_context);
00704 
00705 /**
00706  * @brief Function for deleting a Service Context for a peer device identified by the 'p_handle' parameter.
00707  *
00708  * @details This API allows application to delete a Service Context identified for a peer device
00709  *          identified by the 'p_handle' parameter. If this API returns NRF_SUCCESS,
00710  *          DM_EVT_SERVICE_CONTEXT_DELETED event is notified to the application. 
00711  *          Event result is notified along with the event and indicates success or failure of this
00712  *          procedure.
00713  *
00714  * @param[in] p_handle Identifies peer device for which procedure is requested.
00715  *
00716  * @retval NRF_SUCCESS             On success, else an error code indicating reason for failure.
00717  * @retval NRF_ERROR_INVALID_STATE If the API is called without module initialization and/or
00718  *                                 application registration.
00719  * @retval NRF_ERROR_NULL          If p_handle is NULL.
00720  * @retval NRF_ERROR_INVALID_ADDR  If the peer is not identified by the handle provided by the application.
00721  */
00722 ret_code_t dm_service_context_delete(dm_handle_t const * p_handle);
00723 
00724 /**
00725  * @brief Function for setting Application Context for a peer device identified by the 'p_handle' parameter.
00726  *
00727  * @details This application allows the setting of the application context for the peer device identified by
00728  *          the 'p_handle'. Application context is stored persistently by the module and can be
00729  *          requested by the application at any time using the \ref dm_application_context_get
00730  *          API. Note that this procedure is permitted only for bonded devices. If the
00731  *          device is not bonded, application context cannot be set. However, it is not mandatory
00732  *          that the bonded device is connected when requesting this procedure.
00733  *
00734  * @param[in] p_handle  Identifies peer device for which procedure is requested.
00735  *
00736  * @param[in] p_context Application context being set. The context information includes length of the
00737  *                      data and pointer to the contextual data being set. The memory pointed to by
00738  *                      the data pointer is assumed to be resident when API is being called and
00739  *                      can be freed or reused once the set procedure is complete. Set procedure
00740  *                      completion is notified by the event \ref DM_EVT_APPL_CONTEXT_STORED.
00741  *                      The event result is notified along with the event and indicates success or
00742  *                      failure of this procedure.
00743  *
00744  * @retval NRF_SUCCESS             On success, else an error code indicating reason for failure.
00745  * @retval NRF_ERROR_INVALID_STATE If the API is called without module initialization and/or
00746  *                                 application registration.
00747  * @retval NRF_ERROR_NULL          If p_handle and/or p_context is NULL.
00748  * @retval NRF_ERROR_INVALID_ADDR  If peer is not identified the handle provided by the application.
00749  *
00750  * @note The API returns FEATURE_NOT_ENABLED in case DEVICE_MANAGER_APP_CONTEXT_SIZE is set to zero.
00751  */
00752 ret_code_t dm_application_context_set(dm_handle_t const              * p_handle,
00753                                       dm_application_context_t const * p_context);
00754 
00755 /**
00756  * @brief Function for getting Application Context for a peer device identified by the 'p_handle' parameter.
00757  *
00758  * @details Get Application Context for a peer device identified by the 'p_handle' parameter. If
00759  *          this API returns NRF_SUCCESS, DM_EVT_APPL_CONTEXT_LOADED event is notified to the
00760  *          application. Event result notified along with the event indicates success or failure
00761  *          of this procedure.
00762  *
00763  * @param[in] p_handle  Identifies peer device for which procedure is requested.
00764  * @param[in] p_context Application context being requested. The context information includes
00765  *                      length of data and pointer to the contextual data is provided.
00766  *
00767  * @retval NRF_SUCCESS             On success, else an error code indicating reason for failure.
00768  * @retval NRF_ERROR_INVALID_STATE If the API is called without module initialization and/or
00769  *                                 application registration.
00770  * @retval NRF_ERROR_NULL          If p_handle and/or p_context is NULL.
00771  * @retval NRF_ERROR_INVALID_ADDR  If the peer is not identified by the handle provided by the application.
00772  * @retval DM_NO_APP_CONTEXT       If no application context was set that can be fetched.
00773  *
00774  * @note The API returns FEATURE_NOT_ENABLED in case DEVICE_MANAGER_APP_CONTEXT_SIZE is set to
00775  *       zero.
00776  */
00777 ret_code_t dm_application_context_get(dm_handle_t const        * p_handle,
00778                                       dm_application_context_t * p_context);
00779 
00780 /**
00781  * @brief Function for deleting Application Context for a peer device identified by the 'p_handle' parameter.
00782  *
00783  * @details Delete Application Context for a peer device identified by the 'p_handle' parameter. If
00784  *          this API returns NRF_SUCCESS, DM_EVT_APPL_CONTEXT_DELETED event is notified to the
00785  *          application. The event result notified along with the event and indicates success or failure
00786  *          of this procedure.
00787  *
00788  * @param[in] p_handle Identifies peer device for which procedure is requested.
00789  *
00790  * @retval NRF_SUCCESS             On success, else an error code indicating reason for failure.
00791  * @retval NRF_ERROR_INVALID_STATE If the API is called without module initialization and/or
00792  *                                 application registration.
00793  * @retval NRF_ERROR_NULL          If the p_handle is NULL.
00794  * @retval NRF_ERROR_INVALID_ADDR  If peer is not identified the handle provided by the application.
00795  * @retval DM_NO_APP_CONTEXT       If no application context was set that can be deleted.
00796  *
00797  * @note The API returns FEATURE_NOT_ENABLED if the DEVICE_MANAGER_APP_CONTEXT_SIZE is set to zero.
00798  */
00799 ret_code_t dm_application_context_delete(dm_handle_t const * p_handle);
00800 
00801 /** @} */
00802 
00803 
00804 /**
00805  * @defgroup utility_api Utility APIs
00806  * @{
00807  * @brief This section describes the utility APIs offered by the module.
00808  *
00809  * @details APIs defined in this section are utility or assisting/helper APIs.
00810  */
00811 /**
00812  * @brief Function for Setting/Copying Application instance to Device Manager handle.
00813  *
00814  * @param[in]  p_appl_instance Application instance to be set.
00815  * @param[out] p_handle        Device Manager handle for which the instance is to be copied.
00816  *
00817  * @retval NRF_SUCCESS             On success, else an error code indicating reason for failure.
00818  * @retval NRF_ERROR_INVALID_STATE If the API is called without module initialization and/or
00819  *                                 application registration.
00820  * @retval NRF_ERROR_NULL          If p_handle and/or p_addr is NULL.
00821  */
00822 ret_code_t dm_application_instance_set(dm_application_instance_t const * p_appl_instance,
00823                                        dm_handle_t                     * p_handle);
00824 
00825 /**
00826  * @brief Function for getting a peer's device address.
00827  *
00828  * @param[in]  p_handle Identifies the peer device whose address is requested. Can not be NULL.
00829  * @param[out] p_addr   Pointer where address is to be copied. Can not be NULL.
00830  *
00831  * @retval NRF_SUCCESS             On success, else an error code indicating reason for failure.
00832  * @retval NRF_ERROR_INVALID_STATE If the API is called without module initialization and/or
00833  *                                 application registration.
00834  * @retval NRF_ERROR_NULL          If p_handle and/or p_addr is NULL.
00835  * @retval NRF_ERROR_NOT_FOUND     If the peer could not be identified.
00836  */
00837 ret_code_t dm_peer_addr_get(dm_handle_t const * p_handle,
00838                             ble_gap_addr_t    * p_addr);
00839 
00840 /**
00841  * @brief Function for setting/updating a peer's device address.
00842  *
00843  * @param[in]  p_handle Identifies the peer device whose address is requested to be set/updated.
00844  * @param[out] p_addr   Address to be set/updated.
00845  *
00846  * @retval NRF_SUCCESS             On success, else an error code indicating reason for failure.
00847  * @retval NRF_ERROR_INVALID_STATE If the API is called without module initialization and/or
00848  *                                 application registration.
00849  * @retval NRF_ERROR_NULL          If p_handle and/or p_addr is NULL.
00850  * @retval NRF_ERROR_INVALID_ADDR  If the peer is not identified by the handle provided by the application.
00851  * @retval NRF_ERROR_INVALID_PARAM If this procedure is requested while connected to the peer or if the address
00852  *                                 type was set to BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE.
00853  *
00854  * @note Setting or updating a peer's device address is permitted 
00855  *       only for a peer that is bonded and disconnected.
00856  * @note Updated address is reflected only after DM_EVT_DEVICE_CONTEXT_STORED is notified to the
00857  *       application for this bonded device instance. In order to avoid abnormal behaviour, it is
00858  *       recommended to not invite/initiate connections on the updated address unless this event
00859  *       has been notified.
00860  */
00861 ret_code_t dm_peer_addr_set(dm_handle_t const    * p_handle,
00862                             ble_gap_addr_t const * p_addr);
00863 
00864 /**
00865  * @brief Function for initializing Device Manager handle.
00866  *
00867  * @param[in] p_handle Device Manager handle to be initialized.
00868  *
00869  * @retval NRF_SUCCESS    On success.
00870  * @retval NRF_ERROR_NULL If p_handle is NULL.
00871  *
00872  * @note This routine is permitted before initialization of the module.
00873  */
00874 ret_code_t dm_handle_initialize(dm_handle_t * p_handle);
00875 
00876 /**
00877  * @brief Function for getting distributed keys for a device.
00878  *
00879  * @param[in]  p_handle   Device Manager handle identifying the peer.
00880  * @param[out] p_key_dist Pointer to distributed keys.
00881  *
00882  * @retval NRF_SUCCESS             On success, else an error code indicating reason for failure.
00883  * @retval NRF_ERROR_INVALID_STATE If the API is called without module initialization and/or
00884  *                                 application registration.
00885  * @retval NRF_ERROR_NULL          If the p_handle and/or p_key_dist pointer is NULL.
00886  * @retval NRF_ERROR_INVALID_ADDR  If the peer is not identified by the handle provided by the application.
00887  */
00888 ret_code_t dm_distributed_keys_get(dm_handle_t const * p_handle,
00889                                    dm_sec_keyset_t   * p_key_dist);
00890 
00891 /**
00892  * @brief Function for getting the corresponding dm_handle_t based on the connection handle.
00893  *
00894  * @param[in]     conn_handle Connection handle as provided by the SoftDevice.
00895  * @param[in,out] p_handle    Pointer to the p_handle containg the application instance for the 
00896  *                            registered application. If the application instance is valid then 
00897  *                            the p_handle will be filled with requested data.
00898  *
00899  * @retval NRF_SUCCESS          On success, else an error code indicating reason for failure.
00900  * @retval NRF_ERROR_NULL       If the p_handle pointer is NULL.
00901  * @retval NRF_ERROR_NOT_FOUND  If no p_handle is found for the provided connection handle.
00902  */
00903 ret_code_t dm_handle_get(uint16_t conn_handle, dm_handle_t * p_handle);
00904 
00905 /** @} */
00906 /** @} */
00907 /** @} */
00908 #endif // DEVICE_MANAGER_H__
00909