aa

Dependents:   Peripheral_1_serial_copy Peripheral_1_serial 151006_1st_Scenario_normal

Fork of nRF51822 by Nordic Semiconductor

Committer:
rgrover1
Date:
Fri May 08 15:33:56 2015 +0100
Revision:
137:aafab7b0a8bd
Parent:
136:9bb49953de6a
Child:
138:750eca573e18
Synchronized with git rev 9e7dab43
Author: Rohit Grover
implement deleteAllBondedDevices()

Who changed what in which revision?

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