aa

Dependents:   Peripheral_1_serial_copy Peripheral_1_serial 151006_1st_Scenario_normal

Fork of nRF51822 by Nordic Semiconductor

Committer:
rgrover1
Date:
Fri Jun 19 15:55:15 2015 +0100
Revision:
193:29ed6ba3e53f
Parent:
192:3bc89bec110f
Child:
194:c99fc3160091
Synchronized with git rev ba73fee2
Author: Rohit Grover
intermediate checkin while attempting characteristic discovery

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