nordic

Fork of nRF51822 by Nordic Semiconductor

Committer:
rgrover1
Date:
Fri Jun 19 15:55:15 2015 +0100
Revision:
195:c99fc3160091
Parent:
194:29ed6ba3e53f
Child:
248:df37e7bb3f71
Synchronized with git rev 5f5027e2
Author: Rohit Grover
separate out gatt-client functionality into btle_gattc.cpp
presently have event handler and lauchServiceDiscovery()

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"
rgrover1 195:c99fc3160091 27 #include "btle_gattc.h"
Rohit Grover 22:c6ee8136847e 28 #include "btle_advertising.h"
Rohit Grover 22:c6ee8136847e 29 #include "custom/custom_helper.h"
Rohit Grover 22:c6ee8136847e 30
Rohit Grover 22:c6ee8136847e 31 #include "softdevice_handler.h"
Rohit Grover 22:c6ee8136847e 32 #include "pstorage.h"
Rohit Grover 22:c6ee8136847e 33
Rohit Grover 52:120bd37b9d0d 34 #include "GapEvents.h"
Rohit Grover 22:c6ee8136847e 35 #include "nRF51Gap.h"
Rohit Grover 22:c6ee8136847e 36 #include "nRF51GattServer.h"
rgrover1 125:664d4257a9f8 37 #include "device_manager.h"
Rohit Grover 22:c6ee8136847e 38
Rohit Grover 56:a1071b629aa3 39 #include "ble_hci.h"
Rohit Grover 56:a1071b629aa3 40
Rohit Grover 56:a1071b629aa3 41 extern "C" void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name);
Rohit Grover 56:a1071b629aa3 42 void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name);
Rohit Grover 22:c6ee8136847e 43
Rohit Grover 22:c6ee8136847e 44 static void btle_handler(ble_evt_t *p_ble_evt);
Rohit Grover 22:c6ee8136847e 45
Rohit Grover 22:c6ee8136847e 46 static void sys_evt_dispatch(uint32_t sys_evt)
Rohit Grover 22:c6ee8136847e 47 {
Rohit Grover 22:c6ee8136847e 48 pstorage_sys_event_handler(sys_evt);
Rohit Grover 22:c6ee8136847e 49 }
Rohit Grover 22:c6ee8136847e 50
Rohit Grover 22:c6ee8136847e 51 error_t btle_init(void)
Rohit Grover 22:c6ee8136847e 52 {
rgrover1 95:2bac1b3c5cfc 53 #if defined(TARGET_DELTA_DFCM_NNN40) || defined(TARGET_HRM1017)
rgrover1 113:737b08b3b995 54 SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_RC_250_PPM_4000MS_CALIBRATION, NULL);
ytsuboi 61:d0158c65d0d7 55 #else
rgrover1 113:737b08b3b995 56 SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, NULL);
ytsuboi 61:d0158c65d0d7 57 #endif
Rohit Grover 56:a1071b629aa3 58
Rohit Grover 56:a1071b629aa3 59 // Enable BLE stack
Rohit Grover 56:a1071b629aa3 60 /**
Rohit Grover 56:a1071b629aa3 61 * Using this call, the application can select whether to include the
Rohit Grover 56:a1071b629aa3 62 * Service Changed characteristic in the GATT Server. The default in all
Rohit Grover 56:a1071b629aa3 63 * previous releases has been to include the Service Changed characteristic,
Rohit Grover 56:a1071b629aa3 64 * but this affects how GATT clients behave. Specifically, it requires
Rohit Grover 56:a1071b629aa3 65 * clients to subscribe to this attribute and not to cache attribute handles
Rohit Grover 56:a1071b629aa3 66 * between connections unless the devices are bonded. If the application
Rohit Grover 56:a1071b629aa3 67 * does not need to change the structure of the GATT server attributes at
Rohit Grover 56:a1071b629aa3 68 * runtime this adds unnecessary complexity to the interaction with peer
Rohit Grover 56:a1071b629aa3 69 * clients. If the SoftDevice is enabled with the Service Changed
Rohit Grover 56:a1071b629aa3 70 * Characteristics turned off, then clients are allowed to cache attribute
Rohit Grover 56:a1071b629aa3 71 * handles making applications simpler on both sides.
Rohit Grover 56:a1071b629aa3 72 */
Rohit Grover 68:936d81c963fe 73 static const bool IS_SRVC_CHANGED_CHARACT_PRESENT = true;
Rohit Grover 56:a1071b629aa3 74 ble_enable_params_t enableParams = {
Rohit Grover 56:a1071b629aa3 75 .gatts_enable_params = {
Rohit Grover 56:a1071b629aa3 76 .service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT
Rohit Grover 56:a1071b629aa3 77 }
Rohit Grover 56:a1071b629aa3 78 };
Rohit Grover 56:a1071b629aa3 79 if (sd_ble_enable(&enableParams) != NRF_SUCCESS) {
Rohit Grover 56:a1071b629aa3 80 return ERROR_INVALID_PARAM;
Rohit Grover 56:a1071b629aa3 81 }
Rohit Grover 56:a1071b629aa3 82
Rohit Grover 56:a1071b629aa3 83 ble_gap_addr_t addr;
Rohit Grover 56:a1071b629aa3 84 if (sd_ble_gap_address_get(&addr) != NRF_SUCCESS) {
Rohit Grover 56:a1071b629aa3 85 return ERROR_INVALID_PARAM;
Rohit Grover 56:a1071b629aa3 86 }
Rohit Grover 56:a1071b629aa3 87 if (sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &addr) != NRF_SUCCESS) {
Rohit Grover 56:a1071b629aa3 88 return ERROR_INVALID_PARAM;
Rohit Grover 56:a1071b629aa3 89 }
Rohit Grover 22:c6ee8136847e 90
Rohit Grover 22:c6ee8136847e 91 ASSERT_STATUS( softdevice_ble_evt_handler_set(btle_handler));
Rohit Grover 22:c6ee8136847e 92 ASSERT_STATUS( softdevice_sys_evt_handler_set(sys_evt_dispatch));
Rohit Grover 22:c6ee8136847e 93
rgrover1 128:bd1e1fe607e0 94 btle_gap_init();
rgrover1 128:bd1e1fe607e0 95
rgrover1 128:bd1e1fe607e0 96 return ERROR_NONE;
rgrover1 128:bd1e1fe607e0 97 }
rgrover1 128:bd1e1fe607e0 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
rgrover1 126:35e4f65364bc 106 dm_ble_evt_handler(p_ble_evt);
rgrover1 126:35e4f65364bc 107
rgrover1 195:c99fc3160091 108 bleGattcEventHandler(p_ble_evt);
rgrover1 195:c99fc3160091 109
Rohit Grover 22:c6ee8136847e 110 /* Custom event handler */
Rohit Grover 22:c6ee8136847e 111 switch (p_ble_evt->header.evt_id) {
Rohit Grover 41:6e66cd970659 112 case BLE_GAP_EVT_CONNECTED: {
Rohit Grover 41:6e66cd970659 113 Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle;
Rohit Grover 41:6e66cd970659 114 nRF51Gap::getInstance().setConnectionHandle(handle);
Rohit Grover 56:a1071b629aa3 115 const Gap::ConnectionParams_t *params = reinterpret_cast<Gap::ConnectionParams_t *>(&(p_ble_evt->evt.gap_evt.params.connected.conn_params));
rgrover1 77:9886b2865631 116 const ble_gap_addr_t *peer = &p_ble_evt->evt.gap_evt.params.connected.peer_addr;
rgrover1 113:737b08b3b995 117 const ble_gap_addr_t *own = &p_ble_evt->evt.gap_evt.params.connected.own_addr;
rgrover1 113:737b08b3b995 118 nRF51Gap::getInstance().processConnectionEvent(handle,
rgrover1 180:10e4c13360d9 119 static_cast<Gap::AddressType_t>(peer->addr_type), peer->addr,
rgrover1 180:10e4c13360d9 120 static_cast<Gap::AddressType_t>(own->addr_type), own->addr,
rgrover1 113:737b08b3b995 121 params);
Rohit Grover 22:c6ee8136847e 122 break;
Rohit Grover 41:6e66cd970659 123 }
Rohit Grover 22:c6ee8136847e 124
Rohit Grover 41:6e66cd970659 125 case BLE_GAP_EVT_DISCONNECTED: {
Rohit Grover 41:6e66cd970659 126 Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle;
Rohit Grover 22:c6ee8136847e 127 // Since we are not in a connection and have not started advertising,
Rohit Grover 22:c6ee8136847e 128 // store bonds
Rohit Grover 22:c6ee8136847e 129 nRF51Gap::getInstance().setConnectionHandle (BLE_CONN_HANDLE_INVALID);
Rohit Grover 56:a1071b629aa3 130
Rohit Grover 65:98215c4f3a25 131 Gap::DisconnectionReason_t reason;
Rohit Grover 65:98215c4f3a25 132 switch (p_ble_evt->evt.gap_evt.params.disconnected.reason) {
Rohit Grover 65:98215c4f3a25 133 case BLE_HCI_LOCAL_HOST_TERMINATED_CONNECTION:
Rohit Grover 65:98215c4f3a25 134 reason = Gap::LOCAL_HOST_TERMINATED_CONNECTION;
Rohit Grover 65:98215c4f3a25 135 break;
Rohit Grover 65:98215c4f3a25 136 case BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION:
Rohit Grover 65:98215c4f3a25 137 reason = Gap::REMOTE_USER_TERMINATED_CONNECTION;
Rohit Grover 65:98215c4f3a25 138 break;
Rohit Grover 65:98215c4f3a25 139 case BLE_HCI_CONN_INTERVAL_UNACCEPTABLE:
Rohit Grover 65:98215c4f3a25 140 reason = Gap::CONN_INTERVAL_UNACCEPTABLE;
Rohit Grover 65:98215c4f3a25 141 break;
Rohit Grover 65:98215c4f3a25 142 default:
Rohit Grover 65:98215c4f3a25 143 /* Please refer to the underlying transport library for an
Rohit Grover 65:98215c4f3a25 144 * interpretion of this reason's value. */
Rohit Grover 65:98215c4f3a25 145 reason = static_cast<Gap::DisconnectionReason_t>(p_ble_evt->evt.gap_evt.params.disconnected.reason);
Rohit Grover 65:98215c4f3a25 146 break;
Rohit Grover 56:a1071b629aa3 147 }
Rohit Grover 65:98215c4f3a25 148 nRF51Gap::getInstance().processDisconnectionEvent(handle, reason);
Rohit Grover 22:c6ee8136847e 149 break;
Rohit Grover 41:6e66cd970659 150 }
Rohit Grover 22:c6ee8136847e 151
rgrover1 162:6712855a0107 152 case BLE_GAP_EVT_PASSKEY_DISPLAY:
rgrover1 162:6712855a0107 153 nRF51Gap::getInstance().processPasskeyDisplayEvent(p_ble_evt->evt.gap_evt.conn_handle, p_ble_evt->evt.gap_evt.params.passkey_display.passkey);
rgrover1 162:6712855a0107 154 break;
rgrover1 162:6712855a0107 155
Rohit Grover 22:c6ee8136847e 156 case BLE_GAP_EVT_TIMEOUT:
rgrover1 113:737b08b3b995 157 if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISING) {
Rohit Grover 41:6e66cd970659 158 nRF51Gap::getInstance().processEvent(GapEvents::GAP_EVENT_TIMEOUT);
Rohit Grover 22:c6ee8136847e 159 }
Rohit Grover 22:c6ee8136847e 160 break;
Rohit Grover 22:c6ee8136847e 161
Rohit Grover 22:c6ee8136847e 162 case BLE_GATTC_EVT_TIMEOUT:
Rohit Grover 22:c6ee8136847e 163 case BLE_GATTS_EVT_TIMEOUT:
Rohit Grover 22:c6ee8136847e 164 // Disconnect on GATT Server and Client timeout events.
Rohit Grover 22:c6ee8136847e 165 // ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle,
Rohit Grover 22:c6ee8136847e 166 // BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION));
Rohit Grover 22:c6ee8136847e 167 break;
Rohit Grover 22:c6ee8136847e 168
rgrover1 166:67fcad70785a 169 case BLE_GAP_EVT_ADV_REPORT: {
rgrover1 166:67fcad70785a 170 const ble_gap_evt_adv_report_t *advReport = &p_ble_evt->evt.gap_evt.params.adv_report;
rgrover1 168:66ee4f876396 171 nRF51Gap::getInstance().processAdvertisementReport(advReport->peer_addr.addr,
rgrover1 168:66ee4f876396 172 advReport->rssi,
rgrover1 168:66ee4f876396 173 advReport->scan_rsp,
rgrover1 168:66ee4f876396 174 static_cast<Gap::AdvertisementType_t>(advReport->type),
rgrover1 168:66ee4f876396 175 advReport->dlen,
rgrover1 168:66ee4f876396 176 advReport->data);
rgrover1 168:66ee4f876396 177 break;
rgrover1 166:67fcad70785a 178 }
rgrover1 166:67fcad70785a 179
Rohit Grover 22:c6ee8136847e 180 default:
Rohit Grover 22:c6ee8136847e 181 break;
Rohit Grover 22:c6ee8136847e 182 }
Rohit Grover 22:c6ee8136847e 183
Rohit Grover 22:c6ee8136847e 184 nRF51GattServer::getInstance().hwCallback(p_ble_evt);
Rohit Grover 22:c6ee8136847e 185 }
Rohit Grover 22:c6ee8136847e 186
rgrover1 161:7d04579fdb2a 187 /*! @brief Callback when an error occurs inside the SoftDevice */
Rohit Grover 22:c6ee8136847e 188 void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name)
Rohit Grover 22:c6ee8136847e 189 {
Rohit Grover 22:c6ee8136847e 190 ASSERT(false, (void) 0);
Rohit Grover 22:c6ee8136847e 191 }
Rohit Grover 22:c6ee8136847e 192
Rohit Grover 22:c6ee8136847e 193 /*!
Rohit Grover 22:c6ee8136847e 194 @brief Handler for general errors above the SoftDevice layer.
Rohit Grover 22:c6ee8136847e 195 Typically we can' recover from this so we do a reset.
Rohit Grover 22:c6ee8136847e 196 */
rgrover1 161:7d04579fdb2a 197 void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name)
Rohit Grover 22:c6ee8136847e 198 {
Rohit Grover 22:c6ee8136847e 199 ASSERT_STATUS_RET_VOID( error_code );
Rohit Grover 22:c6ee8136847e 200 NVIC_SystemReset();
rgrover1 77:9886b2865631 201 }