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:54 2015 +0100
Revision:
123:e19f9f7874f9
Parent:
122:df81fcbfa150
Child:
124:664d4257a9f8
Synchronized with git rev e5ba1d85
Author: Rohit Grover
Call pstorage_init() from btle_init()

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rohit Grover 22:c6ee8136847e 1 /* mbed Microcontroller Library
Rohit Grover 22:c6ee8136847e 2 * Copyright (c) 2006-2013 ARM Limited
Rohit Grover 22:c6ee8136847e 3 *
Rohit Grover 22:c6ee8136847e 4 * Licensed under the Apache License, Version 2.0 (the "License");
Rohit Grover 22:c6ee8136847e 5 * you may not use this file except in compliance with the License.
Rohit Grover 22:c6ee8136847e 6 * You may obtain a copy of the License at
Rohit Grover 22:c6ee8136847e 7 *
Rohit Grover 22:c6ee8136847e 8 * http://www.apache.org/licenses/LICENSE-2.0
Rohit Grover 22:c6ee8136847e 9 *
Rohit Grover 22:c6ee8136847e 10 * Unless required by applicable law or agreed to in writing, software
Rohit Grover 22:c6ee8136847e 11 * distributed under the License is distributed on an "AS IS" BASIS,
Rohit Grover 22:c6ee8136847e 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Rohit Grover 22:c6ee8136847e 13 * See the License for the specific language governing permissions and
Rohit Grover 22:c6ee8136847e 14 * limitations under the License.
Rohit Grover 22:c6ee8136847e 15 */
Rohit Grover 52:120bd37b9d0d 16
Rohit Grover 22:c6ee8136847e 17 #include "common/common.h"
Rohit Grover 37:c29c330d942c 18 #include "nordic_common.h"
Rohit Grover 22:c6ee8136847e 19
Rohit Grover 22:c6ee8136847e 20 #include "btle.h"
Rohit Grover 22:c6ee8136847e 21
Rohit Grover 22:c6ee8136847e 22 #include "ble_stack_handler_types.h"
Rohit Grover 22:c6ee8136847e 23 #include "ble_flash.h"
Rohit Grover 22:c6ee8136847e 24 #include "ble_conn_params.h"
Rohit Grover 22:c6ee8136847e 25
Rohit Grover 22:c6ee8136847e 26 #include "btle_gap.h"
Rohit Grover 22:c6ee8136847e 27 #include "btle_advertising.h"
Rohit Grover 22:c6ee8136847e 28 #include "custom/custom_helper.h"
Rohit Grover 22:c6ee8136847e 29
Rohit Grover 22:c6ee8136847e 30 #include "softdevice_handler.h"
Rohit Grover 22:c6ee8136847e 31 #include "pstorage.h"
Rohit Grover 22:c6ee8136847e 32
Rohit Grover 52:120bd37b9d0d 33 #include "GapEvents.h"
Rohit Grover 22:c6ee8136847e 34 #include "nRF51Gap.h"
Rohit Grover 22:c6ee8136847e 35 #include "nRF51GattServer.h"
Rohit Grover 22:c6ee8136847e 36
Rohit Grover 56:a1071b629aa3 37 #include "ble_hci.h"
Rohit Grover 56:a1071b629aa3 38
Rohit Grover 56:a1071b629aa3 39 extern "C" void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name);
Rohit Grover 56:a1071b629aa3 40 void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name);
Rohit Grover 22:c6ee8136847e 41
Rohit Grover 22:c6ee8136847e 42 static void btle_handler(ble_evt_t *p_ble_evt);
Rohit Grover 22:c6ee8136847e 43
Rohit Grover 22:c6ee8136847e 44 static void sys_evt_dispatch(uint32_t sys_evt)
Rohit Grover 22:c6ee8136847e 45 {
Rohit Grover 22:c6ee8136847e 46 pstorage_sys_event_handler(sys_evt);
Rohit Grover 22:c6ee8136847e 47 }
Rohit Grover 22:c6ee8136847e 48
Rohit Grover 22:c6ee8136847e 49 error_t btle_init(void)
Rohit Grover 22:c6ee8136847e 50 {
rgrover1 94:2bac1b3c5cfc 51 #if defined(TARGET_DELTA_DFCM_NNN40) || defined(TARGET_HRM1017)
rgrover1 112:737b08b3b995 52 SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_RC_250_PPM_4000MS_CALIBRATION, NULL);
ytsuboi 61:d0158c65d0d7 53 #else
rgrover1 112:737b08b3b995 54 SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, NULL);
ytsuboi 61:d0158c65d0d7 55 #endif
Rohit Grover 56:a1071b629aa3 56
Rohit Grover 56:a1071b629aa3 57 // Enable BLE stack
Rohit Grover 56:a1071b629aa3 58 /**
Rohit Grover 56:a1071b629aa3 59 * Using this call, the application can select whether to include the
Rohit Grover 56:a1071b629aa3 60 * Service Changed characteristic in the GATT Server. The default in all
Rohit Grover 56:a1071b629aa3 61 * previous releases has been to include the Service Changed characteristic,
Rohit Grover 56:a1071b629aa3 62 * but this affects how GATT clients behave. Specifically, it requires
Rohit Grover 56:a1071b629aa3 63 * clients to subscribe to this attribute and not to cache attribute handles
Rohit Grover 56:a1071b629aa3 64 * between connections unless the devices are bonded. If the application
Rohit Grover 56:a1071b629aa3 65 * does not need to change the structure of the GATT server attributes at
Rohit Grover 56:a1071b629aa3 66 * runtime this adds unnecessary complexity to the interaction with peer
Rohit Grover 56:a1071b629aa3 67 * clients. If the SoftDevice is enabled with the Service Changed
Rohit Grover 56:a1071b629aa3 68 * Characteristics turned off, then clients are allowed to cache attribute
Rohit Grover 56:a1071b629aa3 69 * handles making applications simpler on both sides.
Rohit Grover 56:a1071b629aa3 70 */
Rohit Grover 68:936d81c963fe 71 static const bool IS_SRVC_CHANGED_CHARACT_PRESENT = true;
Rohit Grover 56:a1071b629aa3 72 ble_enable_params_t enableParams = {
Rohit Grover 56:a1071b629aa3 73 .gatts_enable_params = {
Rohit Grover 56:a1071b629aa3 74 .service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT
Rohit Grover 56:a1071b629aa3 75 }
Rohit Grover 56:a1071b629aa3 76 };
Rohit Grover 56:a1071b629aa3 77 if (sd_ble_enable(&enableParams) != NRF_SUCCESS) {
Rohit Grover 56:a1071b629aa3 78 return ERROR_INVALID_PARAM;
Rohit Grover 56:a1071b629aa3 79 }
Rohit Grover 56:a1071b629aa3 80
Rohit Grover 56:a1071b629aa3 81 ble_gap_addr_t addr;
Rohit Grover 56:a1071b629aa3 82 if (sd_ble_gap_address_get(&addr) != NRF_SUCCESS) {
Rohit Grover 56:a1071b629aa3 83 return ERROR_INVALID_PARAM;
Rohit Grover 56:a1071b629aa3 84 }
Rohit Grover 56:a1071b629aa3 85 if (sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &addr) != NRF_SUCCESS) {
Rohit Grover 56:a1071b629aa3 86 return ERROR_INVALID_PARAM;
Rohit Grover 56:a1071b629aa3 87 }
Rohit Grover 22:c6ee8136847e 88
Rohit Grover 22:c6ee8136847e 89 ASSERT_STATUS( softdevice_ble_evt_handler_set(btle_handler));
Rohit Grover 22:c6ee8136847e 90 ASSERT_STATUS( softdevice_sys_evt_handler_set(sys_evt_dispatch));
Rohit Grover 22:c6ee8136847e 91
rgrover1 123:e19f9f7874f9 92 pstorage_init();
rgrover1 123:e19f9f7874f9 93
Rohit Grover 22:c6ee8136847e 94 btle_gap_init();
Rohit Grover 22:c6ee8136847e 95
Rohit Grover 22:c6ee8136847e 96 return ERROR_NONE;
Rohit Grover 22:c6ee8136847e 97 }
Rohit Grover 22:c6ee8136847e 98
Rohit Grover 22:c6ee8136847e 99 static void btle_handler(ble_evt_t *p_ble_evt)
Rohit Grover 22:c6ee8136847e 100 {
Rohit Grover 22:c6ee8136847e 101 /* Library service handlers */
Rohit Grover 56:a1071b629aa3 102 #if SDK_CONN_PARAMS_MODULE_ENABLE
Rohit Grover 22:c6ee8136847e 103 ble_conn_params_on_ble_evt(p_ble_evt);
Rohit Grover 56:a1071b629aa3 104 #endif
Rohit Grover 22:c6ee8136847e 105
Rohit Grover 22:c6ee8136847e 106 /* Custom event handler */
Rohit Grover 22:c6ee8136847e 107 switch (p_ble_evt->header.evt_id) {
Rohit Grover 41:6e66cd970659 108 case BLE_GAP_EVT_CONNECTED: {
Rohit Grover 41:6e66cd970659 109 Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle;
Rohit Grover 41:6e66cd970659 110 nRF51Gap::getInstance().setConnectionHandle(handle);
Rohit Grover 56:a1071b629aa3 111 const Gap::ConnectionParams_t *params = reinterpret_cast<Gap::ConnectionParams_t *>(&(p_ble_evt->evt.gap_evt.params.connected.conn_params));
rgrover1 77:9886b2865631 112 const ble_gap_addr_t *peer = &p_ble_evt->evt.gap_evt.params.connected.peer_addr;
rgrover1 112:737b08b3b995 113 const ble_gap_addr_t *own = &p_ble_evt->evt.gap_evt.params.connected.own_addr;
rgrover1 112:737b08b3b995 114 nRF51Gap::getInstance().processConnectionEvent(handle,
rgrover1 112:737b08b3b995 115 static_cast<Gap::addr_type_t>(peer->addr_type), peer->addr,
rgrover1 112:737b08b3b995 116 static_cast<Gap::addr_type_t>(own->addr_type), own->addr,
rgrover1 112:737b08b3b995 117 params);
Rohit Grover 22:c6ee8136847e 118 break;
Rohit Grover 41:6e66cd970659 119 }
Rohit Grover 22:c6ee8136847e 120
Rohit Grover 41:6e66cd970659 121 case BLE_GAP_EVT_DISCONNECTED: {
Rohit Grover 41:6e66cd970659 122 Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle;
Rohit Grover 22:c6ee8136847e 123 // Since we are not in a connection and have not started advertising,
Rohit Grover 22:c6ee8136847e 124 // store bonds
Rohit Grover 22:c6ee8136847e 125 nRF51Gap::getInstance().setConnectionHandle (BLE_CONN_HANDLE_INVALID);
Rohit Grover 56:a1071b629aa3 126
Rohit Grover 65:98215c4f3a25 127 Gap::DisconnectionReason_t reason;
Rohit Grover 65:98215c4f3a25 128 switch (p_ble_evt->evt.gap_evt.params.disconnected.reason) {
Rohit Grover 65:98215c4f3a25 129 case BLE_HCI_LOCAL_HOST_TERMINATED_CONNECTION:
Rohit Grover 65:98215c4f3a25 130 reason = Gap::LOCAL_HOST_TERMINATED_CONNECTION;
Rohit Grover 65:98215c4f3a25 131 break;
Rohit Grover 65:98215c4f3a25 132 case BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION:
Rohit Grover 65:98215c4f3a25 133 reason = Gap::REMOTE_USER_TERMINATED_CONNECTION;
Rohit Grover 65:98215c4f3a25 134 break;
Rohit Grover 65:98215c4f3a25 135 case BLE_HCI_CONN_INTERVAL_UNACCEPTABLE:
Rohit Grover 65:98215c4f3a25 136 reason = Gap::CONN_INTERVAL_UNACCEPTABLE;
Rohit Grover 65:98215c4f3a25 137 break;
Rohit Grover 65:98215c4f3a25 138 default:
Rohit Grover 65:98215c4f3a25 139 /* Please refer to the underlying transport library for an
Rohit Grover 65:98215c4f3a25 140 * interpretion of this reason's value. */
Rohit Grover 65:98215c4f3a25 141 reason = static_cast<Gap::DisconnectionReason_t>(p_ble_evt->evt.gap_evt.params.disconnected.reason);
Rohit Grover 65:98215c4f3a25 142 break;
Rohit Grover 56:a1071b629aa3 143 }
Rohit Grover 65:98215c4f3a25 144 nRF51Gap::getInstance().processDisconnectionEvent(handle, reason);
Rohit Grover 22:c6ee8136847e 145 break;
Rohit Grover 41:6e66cd970659 146 }
Rohit Grover 22:c6ee8136847e 147
Rohit Grover 22:c6ee8136847e 148 case BLE_GAP_EVT_SEC_PARAMS_REQUEST: {
Rohit Grover 22:c6ee8136847e 149 ble_gap_sec_params_t sec_params = {0};
Rohit Grover 22:c6ee8136847e 150
rgrover1 112:737b08b3b995 151 sec_params.bond = 1; /**< Perform bonding. */
rgrover1 112:737b08b3b995 152 sec_params.mitm = CFG_BLE_SEC_PARAM_MITM;
rgrover1 112:737b08b3b995 153 sec_params.io_caps = CFG_BLE_SEC_PARAM_IO_CAPABILITIES;
rgrover1 112:737b08b3b995 154 sec_params.oob = CFG_BLE_SEC_PARAM_OOB;
rgrover1 112:737b08b3b995 155 sec_params.min_key_size = CFG_BLE_SEC_PARAM_MIN_KEY_SIZE;
rgrover1 112:737b08b3b995 156 sec_params.max_key_size = CFG_BLE_SEC_PARAM_MAX_KEY_SIZE;
Rohit Grover 22:c6ee8136847e 157
rgrover1 112:737b08b3b995 158 ble_gap_sec_keyset_t sec_keyset = {0};
rgrover1 112:737b08b3b995 159
rgrover1 112:737b08b3b995 160 ASSERT_STATUS_RET_VOID(sd_ble_gap_sec_params_reply(nRF51Gap::getInstance().getConnectionHandle(), BLE_GAP_SEC_STATUS_SUCCESS, &sec_params, &sec_keyset));
Rohit Grover 22:c6ee8136847e 161 }
Rohit Grover 22:c6ee8136847e 162 break;
Rohit Grover 22:c6ee8136847e 163
Rohit Grover 22:c6ee8136847e 164 case BLE_GAP_EVT_TIMEOUT:
rgrover1 112:737b08b3b995 165 if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISING) {
Rohit Grover 41:6e66cd970659 166 nRF51Gap::getInstance().processEvent(GapEvents::GAP_EVENT_TIMEOUT);
Rohit Grover 22:c6ee8136847e 167 }
Rohit Grover 22:c6ee8136847e 168 break;
Rohit Grover 22:c6ee8136847e 169
Rohit Grover 22:c6ee8136847e 170 case BLE_GATTC_EVT_TIMEOUT:
Rohit Grover 22:c6ee8136847e 171 case BLE_GATTS_EVT_TIMEOUT:
Rohit Grover 22:c6ee8136847e 172 // Disconnect on GATT Server and Client timeout events.
Rohit Grover 22:c6ee8136847e 173 // ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle,
Rohit Grover 22:c6ee8136847e 174 // BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION));
Rohit Grover 22:c6ee8136847e 175 break;
Rohit Grover 22:c6ee8136847e 176
Rohit Grover 22:c6ee8136847e 177 default:
Rohit Grover 22:c6ee8136847e 178 break;
Rohit Grover 22:c6ee8136847e 179 }
Rohit Grover 22:c6ee8136847e 180
Rohit Grover 22:c6ee8136847e 181 nRF51GattServer::getInstance().hwCallback(p_ble_evt);
Rohit Grover 22:c6ee8136847e 182 }
Rohit Grover 22:c6ee8136847e 183
Rohit Grover 22:c6ee8136847e 184 /**************************************************************************/
Rohit Grover 22:c6ee8136847e 185 /*!
Rohit Grover 22:c6ee8136847e 186 @brief Callback when an error occurs inside the SoftDevice
Rohit Grover 22:c6ee8136847e 187
Rohit Grover 22:c6ee8136847e 188 @param[in] line_num
Rohit Grover 22:c6ee8136847e 189 @param[in] p-file_name
Rohit Grover 22:c6ee8136847e 190
Rohit Grover 22:c6ee8136847e 191 @returns
Rohit Grover 22:c6ee8136847e 192 */
Rohit Grover 22:c6ee8136847e 193 /**************************************************************************/
Rohit Grover 22:c6ee8136847e 194 void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name)
Rohit Grover 22:c6ee8136847e 195 {
Rohit Grover 22:c6ee8136847e 196 ASSERT(false, (void) 0);
Rohit Grover 22:c6ee8136847e 197 }
Rohit Grover 22:c6ee8136847e 198
Rohit Grover 22:c6ee8136847e 199 /**************************************************************************/
Rohit Grover 22:c6ee8136847e 200 /*!
Rohit Grover 22:c6ee8136847e 201 @brief Handler for general errors above the SoftDevice layer.
Rohit Grover 22:c6ee8136847e 202 Typically we can' recover from this so we do a reset.
Rohit Grover 22:c6ee8136847e 203
Rohit Grover 22:c6ee8136847e 204 @param[in] error_code
Rohit Grover 22:c6ee8136847e 205 @param[in] line_num
Rohit Grover 22:c6ee8136847e 206 @param[in] p-file_name
Rohit Grover 22:c6ee8136847e 207
Rohit Grover 22:c6ee8136847e 208 @returns
Rohit Grover 22:c6ee8136847e 209 */
Rohit Grover 22:c6ee8136847e 210 /**************************************************************************/
Rohit Grover 22:c6ee8136847e 211 void app_error_handler(uint32_t error_code,
Rohit Grover 22:c6ee8136847e 212 uint32_t line_num,
Rohit Grover 22:c6ee8136847e 213 const uint8_t *p_file_name)
Rohit Grover 22:c6ee8136847e 214 {
Rohit Grover 22:c6ee8136847e 215 ASSERT_STATUS_RET_VOID( error_code );
Rohit Grover 22:c6ee8136847e 216 NVIC_SystemReset();
rgrover1 77:9886b2865631 217 }