test

Dependencies:   nrf51-sdk

Dependents:   microbit-dal

Fork of nRF51822 by Lancaster University

Committer:
LancasterUniversity
Date:
Wed Apr 06 22:38:43 2016 +0100
Revision:
615:65ea2acfc6a2
Parent:
613:cbc548e9df98
Child:
616:a8f9b022d8fd
Synchronized with git rev 60a7c0c0
Author: Rohit Grover
bring in the latest changes for BLE::init() where we allow <object, member> tuples for init callback.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
LancasterUniversity 615:65ea2acfc6a2 1 /* mbed Microcontroller Library
LancasterUniversity 615:65ea2acfc6a2 2 * Copyright (c) 2006-2013 ARM Limited
LancasterUniversity 615:65ea2acfc6a2 3 *
LancasterUniversity 615:65ea2acfc6a2 4 * Licensed under the Apache License, Version 2.0 (the "License");
LancasterUniversity 615:65ea2acfc6a2 5 * you may not use this file except in compliance with the License.
LancasterUniversity 615:65ea2acfc6a2 6 * You may obtain a copy of the License at
LancasterUniversity 615:65ea2acfc6a2 7 *
LancasterUniversity 615:65ea2acfc6a2 8 * http://www.apache.org/licenses/LICENSE-2.0
LancasterUniversity 615:65ea2acfc6a2 9 *
LancasterUniversity 615:65ea2acfc6a2 10 * Unless required by applicable law or agreed to in writing, software
LancasterUniversity 615:65ea2acfc6a2 11 * distributed under the License is distributed on an "AS IS" BASIS,
LancasterUniversity 615:65ea2acfc6a2 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
LancasterUniversity 615:65ea2acfc6a2 13 * See the License for the specific language governing permissions and
LancasterUniversity 615:65ea2acfc6a2 14 * limitations under the License.
LancasterUniversity 615:65ea2acfc6a2 15 */
LancasterUniversity 615:65ea2acfc6a2 16
LancasterUniversity 615:65ea2acfc6a2 17 #include "btle.h"
LancasterUniversity 615:65ea2acfc6a2 18 #include "pstorage.h"
LancasterUniversity 615:65ea2acfc6a2 19
LancasterUniversity 615:65ea2acfc6a2 20 #include "nRF5xGap.h"
LancasterUniversity 615:65ea2acfc6a2 21 #include "nRF5xSecurityManager.h"
LancasterUniversity 615:65ea2acfc6a2 22
LancasterUniversity 615:65ea2acfc6a2 23 #include "device_manager.h"
LancasterUniversity 615:65ea2acfc6a2 24 #include "btle_security.h"
LancasterUniversity 615:65ea2acfc6a2 25
LancasterUniversity 615:65ea2acfc6a2 26 static dm_application_instance_t applicationInstance;
LancasterUniversity 615:65ea2acfc6a2 27 static ret_code_t dm_handler(dm_handle_t const *p_handle, dm_event_t const *p_event, ret_code_t event_result);
LancasterUniversity 615:65ea2acfc6a2 28
LancasterUniversity 615:65ea2acfc6a2 29 ble_error_t
LancasterUniversity 615:65ea2acfc6a2 30 btle_initializeSecurity(bool enableBonding,
LancasterUniversity 615:65ea2acfc6a2 31 bool requireMITM,
LancasterUniversity 615:65ea2acfc6a2 32 SecurityManager::SecurityIOCapabilities_t iocaps,
LancasterUniversity 615:65ea2acfc6a2 33 const SecurityManager::Passkey_t passkey)
LancasterUniversity 615:65ea2acfc6a2 34 {
LancasterUniversity 615:65ea2acfc6a2 35 /* guard against multiple initializations */
LancasterUniversity 615:65ea2acfc6a2 36 static bool initialized = false;
LancasterUniversity 615:65ea2acfc6a2 37 if (initialized) {
LancasterUniversity 615:65ea2acfc6a2 38 return BLE_ERROR_NONE;
LancasterUniversity 615:65ea2acfc6a2 39 }
LancasterUniversity 615:65ea2acfc6a2 40
LancasterUniversity 615:65ea2acfc6a2 41 if (pstorage_init() != NRF_SUCCESS) {
LancasterUniversity 615:65ea2acfc6a2 42 return BLE_ERROR_UNSPECIFIED;
LancasterUniversity 615:65ea2acfc6a2 43 }
LancasterUniversity 615:65ea2acfc6a2 44
LancasterUniversity 615:65ea2acfc6a2 45 ret_code_t rc;
LancasterUniversity 615:65ea2acfc6a2 46 if (passkey) {
LancasterUniversity 615:65ea2acfc6a2 47 ble_opt_t opts;
LancasterUniversity 615:65ea2acfc6a2 48 opts.gap_opt.passkey.p_passkey = const_cast<uint8_t *>(passkey);
LancasterUniversity 615:65ea2acfc6a2 49 if ((rc = sd_ble_opt_set(BLE_GAP_OPT_PASSKEY, &opts)) != NRF_SUCCESS) {
LancasterUniversity 615:65ea2acfc6a2 50 switch (rc) {
LancasterUniversity 615:65ea2acfc6a2 51 case BLE_ERROR_INVALID_CONN_HANDLE:
LancasterUniversity 615:65ea2acfc6a2 52 case NRF_ERROR_INVALID_ADDR:
LancasterUniversity 615:65ea2acfc6a2 53 case NRF_ERROR_INVALID_PARAM:
LancasterUniversity 615:65ea2acfc6a2 54 default:
LancasterUniversity 615:65ea2acfc6a2 55 return BLE_ERROR_INVALID_PARAM;
LancasterUniversity 615:65ea2acfc6a2 56 case NRF_ERROR_INVALID_STATE:
LancasterUniversity 615:65ea2acfc6a2 57 return BLE_ERROR_INVALID_STATE;
LancasterUniversity 615:65ea2acfc6a2 58 case NRF_ERROR_BUSY:
LancasterUniversity 615:65ea2acfc6a2 59 return BLE_STACK_BUSY;
LancasterUniversity 615:65ea2acfc6a2 60 }
LancasterUniversity 615:65ea2acfc6a2 61 }
LancasterUniversity 615:65ea2acfc6a2 62 }
LancasterUniversity 615:65ea2acfc6a2 63
LancasterUniversity 615:65ea2acfc6a2 64 dm_init_param_t dm_init_param = {
LancasterUniversity 615:65ea2acfc6a2 65 .clear_persistent_data = false /* Set to true in case the module should clear all persistent data. */
LancasterUniversity 615:65ea2acfc6a2 66 };
LancasterUniversity 615:65ea2acfc6a2 67 if (dm_init(&dm_init_param) != NRF_SUCCESS) {
LancasterUniversity 615:65ea2acfc6a2 68 return BLE_ERROR_UNSPECIFIED;
LancasterUniversity 615:65ea2acfc6a2 69 }
LancasterUniversity 615:65ea2acfc6a2 70
LancasterUniversity 615:65ea2acfc6a2 71 const dm_application_param_t dm_param = {
LancasterUniversity 615:65ea2acfc6a2 72 .evt_handler = dm_handler,
LancasterUniversity 615:65ea2acfc6a2 73 .service_type = DM_PROTOCOL_CNTXT_GATT_CLI_ID,
LancasterUniversity 615:65ea2acfc6a2 74 .sec_param = {
LancasterUniversity 615:65ea2acfc6a2 75 .bond = enableBonding,/**< Perform bonding. */
LancasterUniversity 615:65ea2acfc6a2 76 .mitm = requireMITM, /**< Man In The Middle protection required. */
LancasterUniversity 615:65ea2acfc6a2 77 .io_caps = iocaps, /**< IO capabilities, see @ref BLE_GAP_IO_CAPS. */
LancasterUniversity 615:65ea2acfc6a2 78 .oob = 0, /**< Out Of Band data available. */
LancasterUniversity 615:65ea2acfc6a2 79 .min_key_size = 16, /**< Minimum encryption key size in octets between 7 and 16. If 0 then not applicable in this instance. */
LancasterUniversity 615:65ea2acfc6a2 80 .max_key_size = 16, /**< Maximum encryption key size in octets between min_key_size and 16. */
LancasterUniversity 615:65ea2acfc6a2 81 .kdist_periph = {
LancasterUniversity 615:65ea2acfc6a2 82 .enc = 1, /**< Long Term Key and Master Identification. */
LancasterUniversity 615:65ea2acfc6a2 83 .id = 1, /**< Identity Resolving Key and Identity Address Information. */
LancasterUniversity 615:65ea2acfc6a2 84 .sign = 1, /**< Connection Signature Resolving Key. */
LancasterUniversity 615:65ea2acfc6a2 85 }, /**< Key distribution bitmap: keys that the peripheral device will distribute. */
LancasterUniversity 615:65ea2acfc6a2 86 }
LancasterUniversity 615:65ea2acfc6a2 87 };
LancasterUniversity 615:65ea2acfc6a2 88
LancasterUniversity 615:65ea2acfc6a2 89 if ((rc = dm_register(&applicationInstance, &dm_param)) != NRF_SUCCESS) {
LancasterUniversity 615:65ea2acfc6a2 90 switch (rc) {
LancasterUniversity 615:65ea2acfc6a2 91 case NRF_ERROR_INVALID_STATE:
LancasterUniversity 615:65ea2acfc6a2 92 return BLE_ERROR_INVALID_STATE;
LancasterUniversity 615:65ea2acfc6a2 93 case NRF_ERROR_NO_MEM:
LancasterUniversity 615:65ea2acfc6a2 94 return BLE_ERROR_NO_MEM;
LancasterUniversity 615:65ea2acfc6a2 95 default:
LancasterUniversity 615:65ea2acfc6a2 96 return BLE_ERROR_UNSPECIFIED;
LancasterUniversity 615:65ea2acfc6a2 97 }
LancasterUniversity 615:65ea2acfc6a2 98 }
LancasterUniversity 615:65ea2acfc6a2 99
LancasterUniversity 615:65ea2acfc6a2 100 initialized = true;
LancasterUniversity 615:65ea2acfc6a2 101 return BLE_ERROR_NONE;
LancasterUniversity 615:65ea2acfc6a2 102 }
LancasterUniversity 615:65ea2acfc6a2 103
LancasterUniversity 615:65ea2acfc6a2 104 ble_error_t
LancasterUniversity 615:65ea2acfc6a2 105 btle_purgeAllBondingState(void)
LancasterUniversity 615:65ea2acfc6a2 106 {
LancasterUniversity 615:65ea2acfc6a2 107 ret_code_t rc;
LancasterUniversity 615:65ea2acfc6a2 108 if ((rc = dm_device_delete_all(&applicationInstance)) == NRF_SUCCESS) {
LancasterUniversity 615:65ea2acfc6a2 109 return BLE_ERROR_NONE;
LancasterUniversity 615:65ea2acfc6a2 110 }
LancasterUniversity 615:65ea2acfc6a2 111
LancasterUniversity 615:65ea2acfc6a2 112 switch (rc) {
LancasterUniversity 615:65ea2acfc6a2 113 case NRF_ERROR_INVALID_STATE:
LancasterUniversity 615:65ea2acfc6a2 114 return BLE_ERROR_INVALID_STATE;
LancasterUniversity 615:65ea2acfc6a2 115 case NRF_ERROR_NO_MEM:
LancasterUniversity 615:65ea2acfc6a2 116 return BLE_ERROR_NO_MEM;
LancasterUniversity 615:65ea2acfc6a2 117 default:
LancasterUniversity 615:65ea2acfc6a2 118 return BLE_ERROR_UNSPECIFIED;
LancasterUniversity 615:65ea2acfc6a2 119 }
LancasterUniversity 615:65ea2acfc6a2 120 }
LancasterUniversity 615:65ea2acfc6a2 121
LancasterUniversity 615:65ea2acfc6a2 122 ble_error_t
LancasterUniversity 615:65ea2acfc6a2 123 btle_getLinkSecurity(Gap::Handle_t connectionHandle, SecurityManager::LinkSecurityStatus_t *securityStatusP)
LancasterUniversity 615:65ea2acfc6a2 124 {
LancasterUniversity 615:65ea2acfc6a2 125 ret_code_t rc;
LancasterUniversity 615:65ea2acfc6a2 126 dm_handle_t dmHandle = {
LancasterUniversity 615:65ea2acfc6a2 127 .appl_id = applicationInstance,
LancasterUniversity 615:65ea2acfc6a2 128 };
LancasterUniversity 615:65ea2acfc6a2 129 if ((rc = dm_handle_get(connectionHandle, &dmHandle)) != NRF_SUCCESS) {
LancasterUniversity 615:65ea2acfc6a2 130 if (rc == NRF_ERROR_NOT_FOUND) {
LancasterUniversity 615:65ea2acfc6a2 131 return BLE_ERROR_INVALID_PARAM;
LancasterUniversity 615:65ea2acfc6a2 132 } else {
LancasterUniversity 615:65ea2acfc6a2 133 return BLE_ERROR_UNSPECIFIED;
LancasterUniversity 615:65ea2acfc6a2 134 }
LancasterUniversity 615:65ea2acfc6a2 135 }
LancasterUniversity 615:65ea2acfc6a2 136
LancasterUniversity 615:65ea2acfc6a2 137 if ((rc = dm_security_status_req(&dmHandle, reinterpret_cast<dm_security_status_t *>(securityStatusP))) != NRF_SUCCESS) {
LancasterUniversity 615:65ea2acfc6a2 138 switch (rc) {
LancasterUniversity 615:65ea2acfc6a2 139 case NRF_ERROR_INVALID_STATE:
LancasterUniversity 615:65ea2acfc6a2 140 return BLE_ERROR_INVALID_STATE;
LancasterUniversity 615:65ea2acfc6a2 141 case NRF_ERROR_NO_MEM:
LancasterUniversity 615:65ea2acfc6a2 142 return BLE_ERROR_NO_MEM;
LancasterUniversity 615:65ea2acfc6a2 143 default:
LancasterUniversity 615:65ea2acfc6a2 144 return BLE_ERROR_UNSPECIFIED;
LancasterUniversity 615:65ea2acfc6a2 145 }
LancasterUniversity 615:65ea2acfc6a2 146 }
LancasterUniversity 615:65ea2acfc6a2 147
LancasterUniversity 615:65ea2acfc6a2 148 return BLE_ERROR_NONE;
LancasterUniversity 615:65ea2acfc6a2 149 }
LancasterUniversity 615:65ea2acfc6a2 150
LancasterUniversity 615:65ea2acfc6a2 151 ret_code_t
LancasterUniversity 615:65ea2acfc6a2 152 dm_handler(dm_handle_t const *p_handle, dm_event_t const *p_event, ret_code_t event_result)
LancasterUniversity 615:65ea2acfc6a2 153 {
LancasterUniversity 615:65ea2acfc6a2 154 switch (p_event->event_id) {
LancasterUniversity 615:65ea2acfc6a2 155 case DM_EVT_SECURITY_SETUP: /* started */ {
LancasterUniversity 615:65ea2acfc6a2 156 const ble_gap_sec_params_t *peerParams = &p_event->event_param.p_gap_param->params.sec_params_request.peer_params;
LancasterUniversity 615:65ea2acfc6a2 157 nRF5xSecurityManager::getInstance().processSecuritySetupInitiatedEvent(p_event->event_param.p_gap_param->conn_handle,
LancasterUniversity 615:65ea2acfc6a2 158 peerParams->bond,
LancasterUniversity 615:65ea2acfc6a2 159 peerParams->mitm,
LancasterUniversity 615:65ea2acfc6a2 160 (SecurityManager::SecurityIOCapabilities_t)peerParams->io_caps);
LancasterUniversity 615:65ea2acfc6a2 161 break;
LancasterUniversity 615:65ea2acfc6a2 162 }
LancasterUniversity 615:65ea2acfc6a2 163 case DM_EVT_SECURITY_SETUP_COMPLETE:
LancasterUniversity 615:65ea2acfc6a2 164 nRF5xSecurityManager::getInstance().
LancasterUniversity 615:65ea2acfc6a2 165 processSecuritySetupCompletedEvent(p_event->event_param.p_gap_param->conn_handle,
LancasterUniversity 615:65ea2acfc6a2 166 (SecurityManager::SecurityCompletionStatus_t)(p_event->event_param.p_gap_param->params.auth_status.auth_status));
LancasterUniversity 615:65ea2acfc6a2 167 break;
LancasterUniversity 615:65ea2acfc6a2 168 case DM_EVT_LINK_SECURED: {
LancasterUniversity 615:65ea2acfc6a2 169 unsigned securityMode = p_event->event_param.p_gap_param->params.conn_sec_update.conn_sec.sec_mode.sm;
LancasterUniversity 615:65ea2acfc6a2 170 unsigned level = p_event->event_param.p_gap_param->params.conn_sec_update.conn_sec.sec_mode.lv;
LancasterUniversity 615:65ea2acfc6a2 171 SecurityManager::SecurityMode_t resolvedSecurityMode = SecurityManager::SECURITY_MODE_NO_ACCESS;
LancasterUniversity 615:65ea2acfc6a2 172 switch (securityMode) {
LancasterUniversity 615:65ea2acfc6a2 173 case 1:
LancasterUniversity 615:65ea2acfc6a2 174 switch (level) {
LancasterUniversity 615:65ea2acfc6a2 175 case 1:
LancasterUniversity 615:65ea2acfc6a2 176 resolvedSecurityMode = SecurityManager::SECURITY_MODE_ENCRYPTION_OPEN_LINK;
LancasterUniversity 615:65ea2acfc6a2 177 break;
LancasterUniversity 615:65ea2acfc6a2 178 case 2:
LancasterUniversity 615:65ea2acfc6a2 179 resolvedSecurityMode = SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM;
LancasterUniversity 615:65ea2acfc6a2 180 break;
LancasterUniversity 615:65ea2acfc6a2 181 case 3:
LancasterUniversity 615:65ea2acfc6a2 182 resolvedSecurityMode = SecurityManager::SECURITY_MODE_ENCRYPTION_WITH_MITM;
LancasterUniversity 615:65ea2acfc6a2 183 break;
LancasterUniversity 615:65ea2acfc6a2 184 }
LancasterUniversity 615:65ea2acfc6a2 185 break;
LancasterUniversity 615:65ea2acfc6a2 186 case 2:
LancasterUniversity 615:65ea2acfc6a2 187 switch (level) {
LancasterUniversity 615:65ea2acfc6a2 188 case 1:
LancasterUniversity 615:65ea2acfc6a2 189 resolvedSecurityMode = SecurityManager::SECURITY_MODE_SIGNED_NO_MITM;
LancasterUniversity 615:65ea2acfc6a2 190 break;
LancasterUniversity 615:65ea2acfc6a2 191 case 2:
LancasterUniversity 615:65ea2acfc6a2 192 resolvedSecurityMode = SecurityManager::SECURITY_MODE_SIGNED_WITH_MITM;
LancasterUniversity 615:65ea2acfc6a2 193 break;
LancasterUniversity 615:65ea2acfc6a2 194 }
LancasterUniversity 615:65ea2acfc6a2 195 break;
LancasterUniversity 615:65ea2acfc6a2 196 }
LancasterUniversity 615:65ea2acfc6a2 197
LancasterUniversity 615:65ea2acfc6a2 198 nRF5xSecurityManager::getInstance().processLinkSecuredEvent(p_event->event_param.p_gap_param->conn_handle, resolvedSecurityMode);
LancasterUniversity 615:65ea2acfc6a2 199 break;
LancasterUniversity 615:65ea2acfc6a2 200 }
LancasterUniversity 615:65ea2acfc6a2 201 case DM_EVT_DEVICE_CONTEXT_STORED:
LancasterUniversity 615:65ea2acfc6a2 202 nRF5xSecurityManager::getInstance().processSecurityContextStoredEvent(p_event->event_param.p_gap_param->conn_handle);
LancasterUniversity 615:65ea2acfc6a2 203 break;
LancasterUniversity 615:65ea2acfc6a2 204 default:
LancasterUniversity 615:65ea2acfc6a2 205 break;
LancasterUniversity 615:65ea2acfc6a2 206 }
LancasterUniversity 615:65ea2acfc6a2 207
LancasterUniversity 615:65ea2acfc6a2 208 return NRF_SUCCESS;
rgrover1 133:74079098b3c9 209 }