Nordic stack and drivers for the mbed BLE API

Fork of nRF51822 by Nordic Semiconductor

Committer:
Vincent Coubard
Date:
Wed Sep 14 14:39:43 2016 +0100
Revision:
640:c90ae1400bf2
Sync with bdab10dc0f90748b6989c8b577771bb403ca6bd8 from ARMmbed/mbed-os.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Vincent Coubard 640:c90ae1400bf2 1 /* mbed Microcontroller Library
Vincent Coubard 640:c90ae1400bf2 2 * Copyright (c) 2006-2013 ARM Limited
Vincent Coubard 640:c90ae1400bf2 3 *
Vincent Coubard 640:c90ae1400bf2 4 * Licensed under the Apache License, Version 2.0 (the "License");
Vincent Coubard 640:c90ae1400bf2 5 * you may not use this file except in compliance with the License.
Vincent Coubard 640:c90ae1400bf2 6 * You may obtain a copy of the License at
Vincent Coubard 640:c90ae1400bf2 7 *
Vincent Coubard 640:c90ae1400bf2 8 * http://www.apache.org/licenses/LICENSE-2.0
Vincent Coubard 640:c90ae1400bf2 9 *
Vincent Coubard 640:c90ae1400bf2 10 * Unless required by applicable law or agreed to in writing, software
Vincent Coubard 640:c90ae1400bf2 11 * distributed under the License is distributed on an "AS IS" BASIS,
Vincent Coubard 640:c90ae1400bf2 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Vincent Coubard 640:c90ae1400bf2 13 * See the License for the specific language governing permissions and
Vincent Coubard 640:c90ae1400bf2 14 * limitations under the License.
Vincent Coubard 640:c90ae1400bf2 15 */
Vincent Coubard 640:c90ae1400bf2 16
Vincent Coubard 640:c90ae1400bf2 17 #include "btle.h"
Vincent Coubard 640:c90ae1400bf2 18
Vincent Coubard 640:c90ae1400bf2 19 #include "nRF5xn.h"
Vincent Coubard 640:c90ae1400bf2 20
Vincent Coubard 640:c90ae1400bf2 21 extern "C" {
Vincent Coubard 640:c90ae1400bf2 22 #include "pstorage.h"
Vincent Coubard 640:c90ae1400bf2 23 #include "device_manager.h"
Vincent Coubard 640:c90ae1400bf2 24 #include "id_manager.h"
Vincent Coubard 640:c90ae1400bf2 25 }
Vincent Coubard 640:c90ae1400bf2 26
Vincent Coubard 640:c90ae1400bf2 27 #include "btle_security.h"
Vincent Coubard 640:c90ae1400bf2 28
Vincent Coubard 640:c90ae1400bf2 29 static dm_application_instance_t applicationInstance;
Vincent Coubard 640:c90ae1400bf2 30 static bool initialized = false;
Vincent Coubard 640:c90ae1400bf2 31 static ret_code_t dm_handler(dm_handle_t const *p_handle, dm_event_t const *p_event, ret_code_t event_result);
Vincent Coubard 640:c90ae1400bf2 32
Vincent Coubard 640:c90ae1400bf2 33 // default security parameters
Vincent Coubard 640:c90ae1400bf2 34 static ble_gap_sec_params_t securityParameters = {
Vincent Coubard 640:c90ae1400bf2 35 .bond = true, /**< Perform bonding. */
Vincent Coubard 640:c90ae1400bf2 36 .mitm = true, /**< Man In The Middle protection required. */
Vincent Coubard 640:c90ae1400bf2 37 .io_caps = SecurityManager::IO_CAPS_NONE, /**< IO capabilities, see @ref BLE_GAP_IO_CAPS. */
Vincent Coubard 640:c90ae1400bf2 38 .oob = 0, /**< Out Of Band data available. */
Vincent Coubard 640:c90ae1400bf2 39 .min_key_size = 16, /**< Minimum encryption key size in octets between 7 and 16. If 0 then not applicable in this instance. */
Vincent Coubard 640:c90ae1400bf2 40 .max_key_size = 16, /**< Maximum encryption key size in octets between min_key_size and 16. */
Vincent Coubard 640:c90ae1400bf2 41 .kdist_periph = {
Vincent Coubard 640:c90ae1400bf2 42 .enc = 1, /**< Long Term Key and Master Identification. */
Vincent Coubard 640:c90ae1400bf2 43 .id = 1, /**< Identity Resolving Key and Identity Address Information. */
Vincent Coubard 640:c90ae1400bf2 44 .sign = 1, /**< Connection Signature Resolving Key. */
Vincent Coubard 640:c90ae1400bf2 45 }, /**< Key distribution bitmap: keys that the peripheral device will distribute. */
Vincent Coubard 640:c90ae1400bf2 46 };
Vincent Coubard 640:c90ae1400bf2 47
Vincent Coubard 640:c90ae1400bf2 48 bool
Vincent Coubard 640:c90ae1400bf2 49 btle_hasInitializedSecurity(void)
Vincent Coubard 640:c90ae1400bf2 50 {
Vincent Coubard 640:c90ae1400bf2 51 return initialized;
Vincent Coubard 640:c90ae1400bf2 52 }
Vincent Coubard 640:c90ae1400bf2 53
Vincent Coubard 640:c90ae1400bf2 54 ble_error_t
Vincent Coubard 640:c90ae1400bf2 55 btle_initializeSecurity(bool enableBonding,
Vincent Coubard 640:c90ae1400bf2 56 bool requireMITM,
Vincent Coubard 640:c90ae1400bf2 57 SecurityManager::SecurityIOCapabilities_t iocaps,
Vincent Coubard 640:c90ae1400bf2 58 const SecurityManager::Passkey_t passkey)
Vincent Coubard 640:c90ae1400bf2 59 {
Vincent Coubard 640:c90ae1400bf2 60 /* guard against multiple initializations */
Vincent Coubard 640:c90ae1400bf2 61 if (initialized) {
Vincent Coubard 640:c90ae1400bf2 62 return BLE_ERROR_NONE;
Vincent Coubard 640:c90ae1400bf2 63 }
Vincent Coubard 640:c90ae1400bf2 64
Vincent Coubard 640:c90ae1400bf2 65 if (pstorage_init() != NRF_SUCCESS) {
Vincent Coubard 640:c90ae1400bf2 66 return BLE_ERROR_UNSPECIFIED;
Vincent Coubard 640:c90ae1400bf2 67 }
Vincent Coubard 640:c90ae1400bf2 68
Vincent Coubard 640:c90ae1400bf2 69 ret_code_t rc;
Vincent Coubard 640:c90ae1400bf2 70 if (passkey) {
Vincent Coubard 640:c90ae1400bf2 71 ble_opt_t opts;
Vincent Coubard 640:c90ae1400bf2 72 opts.gap_opt.passkey.p_passkey = const_cast<uint8_t *>(passkey);
Vincent Coubard 640:c90ae1400bf2 73 if ((rc = sd_ble_opt_set(BLE_GAP_OPT_PASSKEY, &opts)) != NRF_SUCCESS) {
Vincent Coubard 640:c90ae1400bf2 74 switch (rc) {
Vincent Coubard 640:c90ae1400bf2 75 case BLE_ERROR_INVALID_CONN_HANDLE:
Vincent Coubard 640:c90ae1400bf2 76 case NRF_ERROR_INVALID_ADDR:
Vincent Coubard 640:c90ae1400bf2 77 case NRF_ERROR_INVALID_PARAM:
Vincent Coubard 640:c90ae1400bf2 78 default:
Vincent Coubard 640:c90ae1400bf2 79 return BLE_ERROR_INVALID_PARAM;
Vincent Coubard 640:c90ae1400bf2 80 case NRF_ERROR_INVALID_STATE:
Vincent Coubard 640:c90ae1400bf2 81 return BLE_ERROR_INVALID_STATE;
Vincent Coubard 640:c90ae1400bf2 82 case NRF_ERROR_BUSY:
Vincent Coubard 640:c90ae1400bf2 83 return BLE_STACK_BUSY;
Vincent Coubard 640:c90ae1400bf2 84 }
Vincent Coubard 640:c90ae1400bf2 85 }
Vincent Coubard 640:c90ae1400bf2 86 }
Vincent Coubard 640:c90ae1400bf2 87
Vincent Coubard 640:c90ae1400bf2 88 dm_init_param_t dm_init_param = {
Vincent Coubard 640:c90ae1400bf2 89 .clear_persistent_data = false /* Set to true in case the module should clear all persistent data. */
Vincent Coubard 640:c90ae1400bf2 90 };
Vincent Coubard 640:c90ae1400bf2 91 if (dm_init(&dm_init_param) != NRF_SUCCESS) {
Vincent Coubard 640:c90ae1400bf2 92 return BLE_ERROR_UNSPECIFIED;
Vincent Coubard 640:c90ae1400bf2 93 }
Vincent Coubard 640:c90ae1400bf2 94
Vincent Coubard 640:c90ae1400bf2 95 // update default security parameters with function call parameters
Vincent Coubard 640:c90ae1400bf2 96 securityParameters.bond = enableBonding;
Vincent Coubard 640:c90ae1400bf2 97 securityParameters.mitm = requireMITM;
Vincent Coubard 640:c90ae1400bf2 98 securityParameters.io_caps = iocaps;
Vincent Coubard 640:c90ae1400bf2 99
Vincent Coubard 640:c90ae1400bf2 100 const dm_application_param_t dm_param = {
Vincent Coubard 640:c90ae1400bf2 101 .evt_handler = dm_handler,
Vincent Coubard 640:c90ae1400bf2 102 .service_type = DM_PROTOCOL_CNTXT_GATT_CLI_ID,
Vincent Coubard 640:c90ae1400bf2 103 .sec_param = securityParameters
Vincent Coubard 640:c90ae1400bf2 104 };
Vincent Coubard 640:c90ae1400bf2 105
Vincent Coubard 640:c90ae1400bf2 106 if ((rc = dm_register(&applicationInstance, &dm_param)) != NRF_SUCCESS) {
Vincent Coubard 640:c90ae1400bf2 107 switch (rc) {
Vincent Coubard 640:c90ae1400bf2 108 case NRF_ERROR_INVALID_STATE:
Vincent Coubard 640:c90ae1400bf2 109 return BLE_ERROR_INVALID_STATE;
Vincent Coubard 640:c90ae1400bf2 110 case NRF_ERROR_NO_MEM:
Vincent Coubard 640:c90ae1400bf2 111 return BLE_ERROR_NO_MEM;
Vincent Coubard 640:c90ae1400bf2 112 default:
Vincent Coubard 640:c90ae1400bf2 113 return BLE_ERROR_UNSPECIFIED;
Vincent Coubard 640:c90ae1400bf2 114 }
Vincent Coubard 640:c90ae1400bf2 115 }
Vincent Coubard 640:c90ae1400bf2 116
Vincent Coubard 640:c90ae1400bf2 117 initialized = true;
Vincent Coubard 640:c90ae1400bf2 118 return BLE_ERROR_NONE;
Vincent Coubard 640:c90ae1400bf2 119 }
Vincent Coubard 640:c90ae1400bf2 120
Vincent Coubard 640:c90ae1400bf2 121 ble_error_t
Vincent Coubard 640:c90ae1400bf2 122 btle_purgeAllBondingState(void)
Vincent Coubard 640:c90ae1400bf2 123 {
Vincent Coubard 640:c90ae1400bf2 124 ret_code_t rc;
Vincent Coubard 640:c90ae1400bf2 125 if ((rc = dm_device_delete_all(&applicationInstance)) == NRF_SUCCESS) {
Vincent Coubard 640:c90ae1400bf2 126 return BLE_ERROR_NONE;
Vincent Coubard 640:c90ae1400bf2 127 }
Vincent Coubard 640:c90ae1400bf2 128
Vincent Coubard 640:c90ae1400bf2 129 switch (rc) {
Vincent Coubard 640:c90ae1400bf2 130 case NRF_ERROR_INVALID_STATE:
Vincent Coubard 640:c90ae1400bf2 131 return BLE_ERROR_INVALID_STATE;
Vincent Coubard 640:c90ae1400bf2 132 case NRF_ERROR_NO_MEM:
Vincent Coubard 640:c90ae1400bf2 133 return BLE_ERROR_NO_MEM;
Vincent Coubard 640:c90ae1400bf2 134 default:
Vincent Coubard 640:c90ae1400bf2 135 return BLE_ERROR_UNSPECIFIED;
Vincent Coubard 640:c90ae1400bf2 136 }
Vincent Coubard 640:c90ae1400bf2 137 }
Vincent Coubard 640:c90ae1400bf2 138
Vincent Coubard 640:c90ae1400bf2 139 ble_error_t
Vincent Coubard 640:c90ae1400bf2 140 btle_getLinkSecurity(Gap::Handle_t connectionHandle, SecurityManager::LinkSecurityStatus_t *securityStatusP)
Vincent Coubard 640:c90ae1400bf2 141 {
Vincent Coubard 640:c90ae1400bf2 142 ret_code_t rc;
Vincent Coubard 640:c90ae1400bf2 143 dm_handle_t dmHandle = {
Vincent Coubard 640:c90ae1400bf2 144 .appl_id = applicationInstance,
Vincent Coubard 640:c90ae1400bf2 145 };
Vincent Coubard 640:c90ae1400bf2 146 if ((rc = dm_handle_get(connectionHandle, &dmHandle)) != NRF_SUCCESS) {
Vincent Coubard 640:c90ae1400bf2 147 if (rc == NRF_ERROR_NOT_FOUND) {
Vincent Coubard 640:c90ae1400bf2 148 return BLE_ERROR_INVALID_PARAM;
Vincent Coubard 640:c90ae1400bf2 149 } else {
Vincent Coubard 640:c90ae1400bf2 150 return BLE_ERROR_UNSPECIFIED;
Vincent Coubard 640:c90ae1400bf2 151 }
Vincent Coubard 640:c90ae1400bf2 152 }
Vincent Coubard 640:c90ae1400bf2 153
Vincent Coubard 640:c90ae1400bf2 154 if ((rc = dm_security_status_req(&dmHandle, reinterpret_cast<dm_security_status_t *>(securityStatusP))) != NRF_SUCCESS) {
Vincent Coubard 640:c90ae1400bf2 155 switch (rc) {
Vincent Coubard 640:c90ae1400bf2 156 case NRF_ERROR_INVALID_STATE:
Vincent Coubard 640:c90ae1400bf2 157 return BLE_ERROR_INVALID_STATE;
Vincent Coubard 640:c90ae1400bf2 158 case NRF_ERROR_NO_MEM:
Vincent Coubard 640:c90ae1400bf2 159 return BLE_ERROR_NO_MEM;
Vincent Coubard 640:c90ae1400bf2 160 default:
Vincent Coubard 640:c90ae1400bf2 161 return BLE_ERROR_UNSPECIFIED;
Vincent Coubard 640:c90ae1400bf2 162 }
Vincent Coubard 640:c90ae1400bf2 163 }
Vincent Coubard 640:c90ae1400bf2 164
Vincent Coubard 640:c90ae1400bf2 165 return BLE_ERROR_NONE;
Vincent Coubard 640:c90ae1400bf2 166 }
Vincent Coubard 640:c90ae1400bf2 167
Vincent Coubard 640:c90ae1400bf2 168 ble_error_t
Vincent Coubard 640:c90ae1400bf2 169 btle_setLinkSecurity(Gap::Handle_t connectionHandle, SecurityManager::SecurityMode_t securityMode)
Vincent Coubard 640:c90ae1400bf2 170 {
Vincent Coubard 640:c90ae1400bf2 171 // use default and updated parameters as starting point
Vincent Coubard 640:c90ae1400bf2 172 // and modify structure based on security mode.
Vincent Coubard 640:c90ae1400bf2 173 ble_gap_sec_params_t params = securityParameters;
Vincent Coubard 640:c90ae1400bf2 174
Vincent Coubard 640:c90ae1400bf2 175 switch (securityMode) {
Vincent Coubard 640:c90ae1400bf2 176 case SecurityManager::SECURITY_MODE_ENCRYPTION_OPEN_LINK:
Vincent Coubard 640:c90ae1400bf2 177 /**< Require no protection, open link. */
Vincent Coubard 640:c90ae1400bf2 178 securityParameters.bond = false;
Vincent Coubard 640:c90ae1400bf2 179 securityParameters.mitm = false;
Vincent Coubard 640:c90ae1400bf2 180 break;
Vincent Coubard 640:c90ae1400bf2 181
Vincent Coubard 640:c90ae1400bf2 182 case SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM:
Vincent Coubard 640:c90ae1400bf2 183 /**< Require encryption, but no MITM protection. */
Vincent Coubard 640:c90ae1400bf2 184 securityParameters.bond = true;
Vincent Coubard 640:c90ae1400bf2 185 securityParameters.mitm = false;
Vincent Coubard 640:c90ae1400bf2 186 break;
Vincent Coubard 640:c90ae1400bf2 187
Vincent Coubard 640:c90ae1400bf2 188 // not yet implemented security modes
Vincent Coubard 640:c90ae1400bf2 189 case SecurityManager::SECURITY_MODE_NO_ACCESS:
Vincent Coubard 640:c90ae1400bf2 190 case SecurityManager::SECURITY_MODE_ENCRYPTION_WITH_MITM:
Vincent Coubard 640:c90ae1400bf2 191 /**< Require encryption and MITM protection. */
Vincent Coubard 640:c90ae1400bf2 192 case SecurityManager::SECURITY_MODE_SIGNED_NO_MITM:
Vincent Coubard 640:c90ae1400bf2 193 /**< Require signing or encryption, but no MITM protection. */
Vincent Coubard 640:c90ae1400bf2 194 case SecurityManager::SECURITY_MODE_SIGNED_WITH_MITM:
Vincent Coubard 640:c90ae1400bf2 195 /**< Require signing or encryption, and MITM protection. */
Vincent Coubard 640:c90ae1400bf2 196 default:
Vincent Coubard 640:c90ae1400bf2 197 return BLE_ERROR_NOT_IMPLEMENTED;
Vincent Coubard 640:c90ae1400bf2 198 }
Vincent Coubard 640:c90ae1400bf2 199
Vincent Coubard 640:c90ae1400bf2 200 // update security settings for given connection
Vincent Coubard 640:c90ae1400bf2 201 uint32_t result = sd_ble_gap_authenticate(connectionHandle, &params);
Vincent Coubard 640:c90ae1400bf2 202
Vincent Coubard 640:c90ae1400bf2 203 if (result == NRF_SUCCESS) {
Vincent Coubard 640:c90ae1400bf2 204 return BLE_ERROR_NONE;
Vincent Coubard 640:c90ae1400bf2 205 } else {
Vincent Coubard 640:c90ae1400bf2 206 return BLE_ERROR_UNSPECIFIED;
Vincent Coubard 640:c90ae1400bf2 207 }
Vincent Coubard 640:c90ae1400bf2 208 }
Vincent Coubard 640:c90ae1400bf2 209
Vincent Coubard 640:c90ae1400bf2 210 ret_code_t
Vincent Coubard 640:c90ae1400bf2 211 dm_handler(dm_handle_t const *p_handle, dm_event_t const *p_event, ret_code_t event_result)
Vincent Coubard 640:c90ae1400bf2 212 {
Vincent Coubard 640:c90ae1400bf2 213 nRF5xn &ble = nRF5xn::Instance(BLE::DEFAULT_INSTANCE);
Vincent Coubard 640:c90ae1400bf2 214 nRF5xSecurityManager &securityManager = (nRF5xSecurityManager &) ble.getSecurityManager();
Vincent Coubard 640:c90ae1400bf2 215
Vincent Coubard 640:c90ae1400bf2 216 switch (p_event->event_id) {
Vincent Coubard 640:c90ae1400bf2 217 case DM_EVT_SECURITY_SETUP: /* started */ {
Vincent Coubard 640:c90ae1400bf2 218 const ble_gap_sec_params_t *peerParams = &p_event->event_param.p_gap_param->params.sec_params_request.peer_params;
Vincent Coubard 640:c90ae1400bf2 219 securityManager.processSecuritySetupInitiatedEvent(p_event->event_param.p_gap_param->conn_handle,
Vincent Coubard 640:c90ae1400bf2 220 peerParams->bond,
Vincent Coubard 640:c90ae1400bf2 221 peerParams->mitm,
Vincent Coubard 640:c90ae1400bf2 222 (SecurityManager::SecurityIOCapabilities_t)peerParams->io_caps);
Vincent Coubard 640:c90ae1400bf2 223 break;
Vincent Coubard 640:c90ae1400bf2 224 }
Vincent Coubard 640:c90ae1400bf2 225 case DM_EVT_SECURITY_SETUP_COMPLETE:
Vincent Coubard 640:c90ae1400bf2 226 securityManager.
Vincent Coubard 640:c90ae1400bf2 227 processSecuritySetupCompletedEvent(p_event->event_param.p_gap_param->conn_handle,
Vincent Coubard 640:c90ae1400bf2 228 (SecurityManager::SecurityCompletionStatus_t)(p_event->event_param.p_gap_param->params.auth_status.auth_status));
Vincent Coubard 640:c90ae1400bf2 229 break;
Vincent Coubard 640:c90ae1400bf2 230 case DM_EVT_LINK_SECURED: {
Vincent Coubard 640:c90ae1400bf2 231 unsigned securityMode = p_event->event_param.p_gap_param->params.conn_sec_update.conn_sec.sec_mode.sm;
Vincent Coubard 640:c90ae1400bf2 232 unsigned level = p_event->event_param.p_gap_param->params.conn_sec_update.conn_sec.sec_mode.lv;
Vincent Coubard 640:c90ae1400bf2 233 SecurityManager::SecurityMode_t resolvedSecurityMode = SecurityManager::SECURITY_MODE_NO_ACCESS;
Vincent Coubard 640:c90ae1400bf2 234 switch (securityMode) {
Vincent Coubard 640:c90ae1400bf2 235 case 1:
Vincent Coubard 640:c90ae1400bf2 236 switch (level) {
Vincent Coubard 640:c90ae1400bf2 237 case 1:
Vincent Coubard 640:c90ae1400bf2 238 resolvedSecurityMode = SecurityManager::SECURITY_MODE_ENCRYPTION_OPEN_LINK;
Vincent Coubard 640:c90ae1400bf2 239 break;
Vincent Coubard 640:c90ae1400bf2 240 case 2:
Vincent Coubard 640:c90ae1400bf2 241 resolvedSecurityMode = SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM;
Vincent Coubard 640:c90ae1400bf2 242 break;
Vincent Coubard 640:c90ae1400bf2 243 case 3:
Vincent Coubard 640:c90ae1400bf2 244 resolvedSecurityMode = SecurityManager::SECURITY_MODE_ENCRYPTION_WITH_MITM;
Vincent Coubard 640:c90ae1400bf2 245 break;
Vincent Coubard 640:c90ae1400bf2 246 }
Vincent Coubard 640:c90ae1400bf2 247 break;
Vincent Coubard 640:c90ae1400bf2 248 case 2:
Vincent Coubard 640:c90ae1400bf2 249 switch (level) {
Vincent Coubard 640:c90ae1400bf2 250 case 1:
Vincent Coubard 640:c90ae1400bf2 251 resolvedSecurityMode = SecurityManager::SECURITY_MODE_SIGNED_NO_MITM;
Vincent Coubard 640:c90ae1400bf2 252 break;
Vincent Coubard 640:c90ae1400bf2 253 case 2:
Vincent Coubard 640:c90ae1400bf2 254 resolvedSecurityMode = SecurityManager::SECURITY_MODE_SIGNED_WITH_MITM;
Vincent Coubard 640:c90ae1400bf2 255 break;
Vincent Coubard 640:c90ae1400bf2 256 }
Vincent Coubard 640:c90ae1400bf2 257 break;
Vincent Coubard 640:c90ae1400bf2 258 }
Vincent Coubard 640:c90ae1400bf2 259
Vincent Coubard 640:c90ae1400bf2 260 securityManager.processLinkSecuredEvent(p_event->event_param.p_gap_param->conn_handle, resolvedSecurityMode);
Vincent Coubard 640:c90ae1400bf2 261 break;
Vincent Coubard 640:c90ae1400bf2 262 }
Vincent Coubard 640:c90ae1400bf2 263 case DM_EVT_DEVICE_CONTEXT_STORED:
Vincent Coubard 640:c90ae1400bf2 264 securityManager.processSecurityContextStoredEvent(p_event->event_param.p_gap_param->conn_handle);
Vincent Coubard 640:c90ae1400bf2 265 break;
Vincent Coubard 640:c90ae1400bf2 266 default:
Vincent Coubard 640:c90ae1400bf2 267 break;
Vincent Coubard 640:c90ae1400bf2 268 }
Vincent Coubard 640:c90ae1400bf2 269
Vincent Coubard 640:c90ae1400bf2 270 return NRF_SUCCESS;
Vincent Coubard 640:c90ae1400bf2 271 }
Vincent Coubard 640:c90ae1400bf2 272
Vincent Coubard 640:c90ae1400bf2 273 ble_error_t
Vincent Coubard 640:c90ae1400bf2 274 btle_createWhitelistFromBondTable(ble_gap_whitelist_t *p_whitelist)
Vincent Coubard 640:c90ae1400bf2 275 {
Vincent Coubard 640:c90ae1400bf2 276 if (!btle_hasInitializedSecurity()) {
Vincent Coubard 640:c90ae1400bf2 277 return BLE_ERROR_INITIALIZATION_INCOMPLETE;
Vincent Coubard 640:c90ae1400bf2 278 }
Vincent Coubard 640:c90ae1400bf2 279 ret_code_t err = dm_whitelist_create(&applicationInstance, p_whitelist);
Vincent Coubard 640:c90ae1400bf2 280 if (err == NRF_SUCCESS) {
Vincent Coubard 640:c90ae1400bf2 281 return BLE_ERROR_NONE;
Vincent Coubard 640:c90ae1400bf2 282 } else if (err == NRF_ERROR_NULL) {
Vincent Coubard 640:c90ae1400bf2 283 return BLE_ERROR_PARAM_OUT_OF_RANGE;
Vincent Coubard 640:c90ae1400bf2 284 } else {
Vincent Coubard 640:c90ae1400bf2 285 return BLE_ERROR_INVALID_STATE;
Vincent Coubard 640:c90ae1400bf2 286 }
Vincent Coubard 640:c90ae1400bf2 287 }
Vincent Coubard 640:c90ae1400bf2 288
Vincent Coubard 640:c90ae1400bf2 289
Vincent Coubard 640:c90ae1400bf2 290 bool
Vincent Coubard 640:c90ae1400bf2 291 btle_matchAddressAndIrk(ble_gap_addr_t const * p_addr, ble_gap_irk_t const * p_irk)
Vincent Coubard 640:c90ae1400bf2 292 {
Vincent Coubard 640:c90ae1400bf2 293 /*
Vincent Coubard 640:c90ae1400bf2 294 * Use a helper function from the Nordic SDK to test whether the BLE
Vincent Coubard 640:c90ae1400bf2 295 * address can be generated using the IRK.
Vincent Coubard 640:c90ae1400bf2 296 */
Vincent Coubard 640:c90ae1400bf2 297 return im_address_resolve(p_addr, p_irk);
Vincent Coubard 640:c90ae1400bf2 298 }
Vincent Coubard 640:c90ae1400bf2 299
Vincent Coubard 640:c90ae1400bf2 300 void
Vincent Coubard 640:c90ae1400bf2 301 btle_generateResolvableAddress(const ble_gap_irk_t &irk, ble_gap_addr_t &address)
Vincent Coubard 640:c90ae1400bf2 302 {
Vincent Coubard 640:c90ae1400bf2 303 /* Set type to resolvable */
Vincent Coubard 640:c90ae1400bf2 304 address.addr_type = BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE;
Vincent Coubard 640:c90ae1400bf2 305
Vincent Coubard 640:c90ae1400bf2 306 /*
Vincent Coubard 640:c90ae1400bf2 307 * Assign a random number to the most significant 3 bytes
Vincent Coubard 640:c90ae1400bf2 308 * of the address.
Vincent Coubard 640:c90ae1400bf2 309 */
Vincent Coubard 640:c90ae1400bf2 310 address.addr[BLE_GAP_ADDR_LEN - 3] = 0x8E;
Vincent Coubard 640:c90ae1400bf2 311 address.addr[BLE_GAP_ADDR_LEN - 2] = 0x4F;
Vincent Coubard 640:c90ae1400bf2 312 address.addr[BLE_GAP_ADDR_LEN - 1] = 0x7C;
Vincent Coubard 640:c90ae1400bf2 313
Vincent Coubard 640:c90ae1400bf2 314 /* Calculate the hash and store it in the top half of the address */
Vincent Coubard 640:c90ae1400bf2 315 ah(irk.irk, &address.addr[BLE_GAP_ADDR_LEN - 3], address.addr);
Vincent Coubard 640:c90ae1400bf2 316 }