Shuta Nakamae / nRF51822

Fork of nRF51822 by Nordic Semiconductor

Files at this revision

API Documentation at this revision

Comitter:
rgrover1
Date:
Fri May 08 15:33:58 2015 +0100
Parent:
149:f6a9caa8c565
Child:
151:e093294d98fb
Commit message:
Synchronized with git rev 4f10b571
Author: Rohit Grover
initializeSecurity() should take default arguments for security capabilities.

Changed in this revision

btle/btle_security.cpp Show annotated file Show diff for this revision Revisions of this file
btle/btle_security.h Show annotated file Show diff for this revision Revisions of this file
--- a/btle/btle_security.cpp	Fri May 08 15:33:57 2015 +0100
+++ b/btle/btle_security.cpp	Fri May 08 15:33:58 2015 +0100
@@ -24,7 +24,7 @@
 static ret_code_t dm_handler(dm_handle_t const *p_handle, dm_event_t const *p_event, ret_code_t event_result);
 
 ble_error_t
-btle_initializeSecurity()
+btle_initializeSecurity(bool enableBonding, bool requireMITM, Gap::SecurityIOCapabilities_t iocaps, const Gap::Passkey_t passkey)
 {
     /* guard against multiple initializations */
     static bool initialized = false;
@@ -36,6 +36,25 @@
         return BLE_ERROR_UNSPECIFIED;
     }
 
+    ret_code_t rc;
+    if (passkey) {
+        ble_opt_t opts;
+        opts.gap_opt.passkey.p_passkey = const_cast<uint8_t *>(passkey);
+        if ((rc = sd_ble_opt_set(BLE_GAP_OPT_PASSKEY, &opts)) != NRF_SUCCESS) {
+            switch (rc) {
+                case BLE_ERROR_INVALID_CONN_HANDLE:
+                case NRF_ERROR_INVALID_ADDR:
+                case NRF_ERROR_INVALID_PARAM:
+                default:
+                    return BLE_ERROR_INVALID_PARAM;
+                case NRF_ERROR_INVALID_STATE:
+                    return BLE_ERROR_INVALID_STATE;
+                case NRF_ERROR_BUSY:
+                    return BLE_STACK_BUSY;
+            }
+        }
+    }
+
     dm_init_param_t dm_init_param = {
         .clear_persistent_data = false /* Set to true in case the module should clear all persistent data. */
     };
@@ -47,9 +66,9 @@
         .evt_handler  = dm_handler,
         .service_type = DM_PROTOCOL_CNTXT_GATT_CLI_ID,
         .sec_param    = {
-            .bond          = 1,            /**< Perform bonding. */
-            .mitm          = 1,            /**< Man In The Middle protection required. */
-            .io_caps       = BLE_GAP_IO_CAPS_NONE, /**< IO capabilities, see @ref BLE_GAP_IO_CAPS. */
+            .bond          = enableBonding,/**< Perform bonding. */
+            .mitm          = requireMITM,  /**< Man In The Middle protection required. */
+            .io_caps       = iocaps,       /**< IO capabilities, see @ref BLE_GAP_IO_CAPS. */
             .oob           = 0,            /**< Out Of Band data available. */
             .min_key_size  = 16,           /**< Minimum encryption key size in octets between 7 and 16. If 0 then not applicable in this instance. */
             .max_key_size  = 16,           /**< Maximum encryption key size in octets between min_key_size and 16. */
@@ -61,7 +80,6 @@
         }
     };
 
-    ret_code_t rc;
     if ((rc = dm_register(&applicationInstance, &dm_param)) != NRF_SUCCESS) {
         switch (rc) {
             case NRF_ERROR_INVALID_STATE:
--- a/btle/btle_security.h	Fri May 08 15:33:57 2015 +0100
+++ b/btle/btle_security.h	Fri May 08 15:33:58 2015 +0100
@@ -25,9 +25,19 @@
  * cryptographic algorithms and protocol exchanges that allow two devices to
  * securely exchange data and privately detect each other.
  *
+ * @param[in]  enableBonding Allow for bonding.
+ * @param[in]  requireMITM   Require protection for man-in-the-middle attacks.
+ * @param[in]  iocaps        To specify IO capabilities of this peripheral,
+ *                           such as availability of a display or keyboard to
+ *                           support out-of-band exchanges of security data.
+ * @param[in]  passkey       To specify a static passkey.
+ *
  * @return BLE_ERROR_NONE on success.
  */
-ble_error_t btle_initializeSecurity();
+ble_error_t btle_initializeSecurity(bool                          enableBonding = true,
+                                    bool                          requireMITM   = true,
+                                    Gap::SecurityIOCapabilities_t iocaps        = Gap::IO_CAPS_NONE,
+                                    const Gap::Passkey_t          passkey       = NULL);
 
 /**
  * Get the security status of a link.