nordic

Fork of nRF51822 by Nordic Semiconductor

Committer:
rgrover1
Date:
Fri May 08 15:33:57 2015 +0100
Revision:
145:9d8fce4b4d5a
Parent:
144:9d73e7f9f2bf
Child:
148:457a129dfa5e
Synchronized with git rev 989c6290
Author: Rohit Grover
guard against repeated calls to ble_initializeSecurity()

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 134:74079098b3c9 1 /* mbed Microcontroller Library
rgrover1 134:74079098b3c9 2 * Copyright (c) 2006-2013 ARM Limited
rgrover1 134:74079098b3c9 3 *
rgrover1 134:74079098b3c9 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 134:74079098b3c9 5 * you may not use this file except in compliance with the License.
rgrover1 134:74079098b3c9 6 * You may obtain a copy of the License at
rgrover1 134:74079098b3c9 7 *
rgrover1 134:74079098b3c9 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 134:74079098b3c9 9 *
rgrover1 134:74079098b3c9 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 134:74079098b3c9 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 134:74079098b3c9 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 134:74079098b3c9 13 * See the License for the specific language governing permissions and
rgrover1 134:74079098b3c9 14 * limitations under the License.
rgrover1 134:74079098b3c9 15 */
rgrover1 134:74079098b3c9 16
rgrover1 134:74079098b3c9 17 #include "btle.h"
rgrover1 134:74079098b3c9 18 #include "pstorage.h"
rgrover1 134:74079098b3c9 19 #include "nRF51Gap.h"
rgrover1 134:74079098b3c9 20 #include "device_manager.h"
rgrover1 135:df7e7964a9c3 21 #include "btle_security.h"
rgrover1 134:74079098b3c9 22
rgrover1 136:fb674d7364bf 23 static uint8_t applicationInstance;
rgrover1 134:74079098b3c9 24 static ret_code_t dm_handler(dm_handle_t const *p_handle, dm_event_t const *p_event, ret_code_t event_result);
rgrover1 134:74079098b3c9 25
rgrover1 134:74079098b3c9 26 ble_error_t
rgrover1 134:74079098b3c9 27 btle_initializeSecurity()
rgrover1 134:74079098b3c9 28 {
rgrover1 145:9d8fce4b4d5a 29 /* guard against multiple initializations */
rgrover1 145:9d8fce4b4d5a 30 static bool initialized = false;
rgrover1 145:9d8fce4b4d5a 31 if (initialized) {
rgrover1 145:9d8fce4b4d5a 32 return BLE_ERROR_NONE;
rgrover1 145:9d8fce4b4d5a 33 }
rgrover1 145:9d8fce4b4d5a 34
rgrover1 134:74079098b3c9 35 if (pstorage_init() != NRF_SUCCESS) {
rgrover1 134:74079098b3c9 36 return BLE_ERROR_UNSPECIFIED;
rgrover1 134:74079098b3c9 37 }
rgrover1 134:74079098b3c9 38
rgrover1 134:74079098b3c9 39 dm_init_param_t dm_init_param = {
rgrover1 134:74079098b3c9 40 .clear_persistent_data = false /* Set to true in case the module should clear all persistent data. */
rgrover1 134:74079098b3c9 41 };
rgrover1 134:74079098b3c9 42 if (dm_init(&dm_init_param) != NRF_SUCCESS) {
rgrover1 134:74079098b3c9 43 return BLE_ERROR_UNSPECIFIED;
rgrover1 134:74079098b3c9 44 }
rgrover1 134:74079098b3c9 45
rgrover1 134:74079098b3c9 46 const dm_application_param_t dm_param = {
rgrover1 134:74079098b3c9 47 .evt_handler = dm_handler,
rgrover1 134:74079098b3c9 48 .service_type = DM_PROTOCOL_CNTXT_GATT_CLI_ID,
rgrover1 134:74079098b3c9 49 .sec_param = {
rgrover1 134:74079098b3c9 50 .bond = 1, /**< Perform bonding. */
rgrover1 134:74079098b3c9 51 .mitm = 1, /**< Man In The Middle protection required. */
rgrover1 134:74079098b3c9 52 .io_caps = BLE_GAP_IO_CAPS_NONE, /**< IO capabilities, see @ref BLE_GAP_IO_CAPS. */
rgrover1 134:74079098b3c9 53 .oob = 0, /**< Out Of Band data available. */
rgrover1 134:74079098b3c9 54 .min_key_size = 16, /**< Minimum encryption key size in octets between 7 and 16. If 0 then not applicable in this instance. */
rgrover1 134:74079098b3c9 55 .max_key_size = 16, /**< Maximum encryption key size in octets between min_key_size and 16. */
rgrover1 134:74079098b3c9 56 .kdist_periph = {
rgrover1 134:74079098b3c9 57 .enc = 1, /**< Long Term Key and Master Identification. */
rgrover1 134:74079098b3c9 58 .id = 1, /**< Identity Resolving Key and Identity Address Information. */
rgrover1 134:74079098b3c9 59 .sign = 1, /**< Connection Signature Resolving Key. */
rgrover1 134:74079098b3c9 60 }, /**< Key distribution bitmap: keys that the peripheral device will distribute. */
rgrover1 134:74079098b3c9 61 }
rgrover1 134:74079098b3c9 62 };
rgrover1 134:74079098b3c9 63
rgrover1 134:74079098b3c9 64 ret_code_t rc;
rgrover1 144:9d73e7f9f2bf 65 if ((rc = dm_register(&applicationInstance, &dm_param)) != NRF_SUCCESS) {
rgrover1 144:9d73e7f9f2bf 66 switch (rc) {
rgrover1 144:9d73e7f9f2bf 67 case NRF_ERROR_INVALID_STATE:
rgrover1 144:9d73e7f9f2bf 68 return BLE_ERROR_INVALID_STATE;
rgrover1 144:9d73e7f9f2bf 69 case NRF_ERROR_NO_MEM:
rgrover1 144:9d73e7f9f2bf 70 return BLE_ERROR_NO_MEM;
rgrover1 144:9d73e7f9f2bf 71 default:
rgrover1 144:9d73e7f9f2bf 72 return BLE_ERROR_UNSPECIFIED;
rgrover1 144:9d73e7f9f2bf 73 }
rgrover1 134:74079098b3c9 74 }
rgrover1 134:74079098b3c9 75
rgrover1 145:9d8fce4b4d5a 76 initialized = true;
rgrover1 144:9d73e7f9f2bf 77 return BLE_ERROR_NONE;
rgrover1 137:9bb49953de6a 78 }
rgrover1 137:9bb49953de6a 79
rgrover1 137:9bb49953de6a 80 ble_error_t
rgrover1 141:3a5282e3f30c 81 btle_purgeAllBondingState(void)
rgrover1 137:9bb49953de6a 82 {
rgrover1 137:9bb49953de6a 83 ret_code_t rc;
rgrover1 138:aafab7b0a8bd 84 if ((rc = dm_device_delete_all(&applicationInstance)) == NRF_SUCCESS) {
rgrover1 138:aafab7b0a8bd 85 return BLE_ERROR_NONE;
rgrover1 138:aafab7b0a8bd 86 }
rgrover1 137:9bb49953de6a 87
rgrover1 138:aafab7b0a8bd 88 switch (rc) {
rgrover1 138:aafab7b0a8bd 89 case NRF_ERROR_INVALID_STATE:
rgrover1 138:aafab7b0a8bd 90 return BLE_ERROR_INVALID_STATE;
rgrover1 138:aafab7b0a8bd 91 case NRF_ERROR_NO_MEM:
rgrover1 138:aafab7b0a8bd 92 return BLE_ERROR_NO_MEM;
rgrover1 138:aafab7b0a8bd 93 default:
rgrover1 138:aafab7b0a8bd 94 return BLE_ERROR_UNSPECIFIED;
rgrover1 138:aafab7b0a8bd 95 }
rgrover1 134:74079098b3c9 96 }
rgrover1 134:74079098b3c9 97
rgrover1 139:750eca573e18 98 ble_error_t
rgrover1 139:750eca573e18 99 btle_getLinkSecurity(Gap::Handle_t connectionHandle, Gap::LinkSecurityStatus_t *securityStatusP)
rgrover1 139:750eca573e18 100 {
rgrover1 142:586e146a3903 101 ret_code_t rc;
rgrover1 139:750eca573e18 102 dm_handle_t dmHandle;
rgrover1 139:750eca573e18 103 if ((rc = dm_handle_get(connectionHandle, &dmHandle)) != NRF_SUCCESS) {
rgrover1 143:b6fe43a5c18f 104 printf("dm_handle_get failed with %u\r\n", rc);
rgrover1 139:750eca573e18 105 if (rc == NRF_ERROR_NOT_FOUND) {
rgrover1 139:750eca573e18 106 return BLE_ERROR_INVALID_PARAM;
rgrover1 139:750eca573e18 107 } else {
rgrover1 139:750eca573e18 108 return BLE_ERROR_UNSPECIFIED;
rgrover1 139:750eca573e18 109 }
rgrover1 139:750eca573e18 110 }
rgrover1 139:750eca573e18 111
rgrover1 139:750eca573e18 112 if ((rc = dm_security_status_req(&dmHandle, reinterpret_cast<dm_security_status_t *>(securityStatusP))) != NRF_SUCCESS) {
rgrover1 143:b6fe43a5c18f 113 printf("security status request failed\r\n");
rgrover1 139:750eca573e18 114 switch (rc) {
rgrover1 139:750eca573e18 115 case NRF_ERROR_INVALID_STATE:
rgrover1 139:750eca573e18 116 return BLE_ERROR_INVALID_STATE;
rgrover1 139:750eca573e18 117 case NRF_ERROR_NO_MEM:
rgrover1 139:750eca573e18 118 return BLE_ERROR_NO_MEM;
rgrover1 139:750eca573e18 119 default:
rgrover1 139:750eca573e18 120 return BLE_ERROR_UNSPECIFIED;
rgrover1 139:750eca573e18 121 }
rgrover1 139:750eca573e18 122 }
rgrover1 139:750eca573e18 123
rgrover1 139:750eca573e18 124 return BLE_ERROR_NONE;
rgrover1 139:750eca573e18 125 }
rgrover1 139:750eca573e18 126
rgrover1 134:74079098b3c9 127 ret_code_t
rgrover1 134:74079098b3c9 128 dm_handler(dm_handle_t const *p_handle, dm_event_t const *p_event, ret_code_t event_result)
rgrover1 134:74079098b3c9 129 {
rgrover1 134:74079098b3c9 130 switch (p_event->event_id) {
rgrover1 134:74079098b3c9 131 case DM_EVT_SECURITY_SETUP: /* started */
rgrover1 134:74079098b3c9 132 nRF51Gap::getInstance().processSecuritySetupStartedEvent(p_event->event_param.p_gap_param->conn_handle);
rgrover1 134:74079098b3c9 133 break;
rgrover1 134:74079098b3c9 134 case DM_EVT_SECURITY_SETUP_COMPLETE:
rgrover1 134:74079098b3c9 135 nRF51Gap::getInstance().processSecuritySetupCompletedEvent(p_event->event_param.p_gap_param->conn_handle);
rgrover1 134:74079098b3c9 136 break;
rgrover1 134:74079098b3c9 137 case DM_EVT_LINK_SECURED:
rgrover1 134:74079098b3c9 138 nRF51Gap::getInstance().processLinkSecuredEvent(p_event->event_param.p_gap_param->conn_handle);
rgrover1 134:74079098b3c9 139 break;
rgrover1 134:74079098b3c9 140 case DM_EVT_DEVICE_CONTEXT_STORED:
rgrover1 134:74079098b3c9 141 nRF51Gap::getInstance().processSecurityContextStoredEvent(p_event->event_param.p_gap_param->conn_handle);
rgrover1 134:74079098b3c9 142 break;
rgrover1 134:74079098b3c9 143 default:
rgrover1 134:74079098b3c9 144 break;
rgrover1 134:74079098b3c9 145 }
rgrover1 134:74079098b3c9 146
rgrover1 134:74079098b3c9 147 return NRF_SUCCESS;
rgrover1 134:74079098b3c9 148 }