test code 123

Dependencies:   mbed

Fork of LinkNode-Test by Qi Yao

Committer:
youkee
Date:
Thu Sep 01 05:14:03 2016 +0000
Revision:
0:1ad0e04b1bc5
change internal time from 1s to 200ms

Who changed what in which revision?

UserRevisionLine numberNew contents of line
youkee 0:1ad0e04b1bc5 1 /* mbed Microcontroller Library
youkee 0:1ad0e04b1bc5 2 * Copyright (c) 2006-2013 ARM Limited
youkee 0:1ad0e04b1bc5 3 *
youkee 0:1ad0e04b1bc5 4 * Licensed under the Apache License, Version 2.0 (the "License");
youkee 0:1ad0e04b1bc5 5 * you may not use this file except in compliance with the License.
youkee 0:1ad0e04b1bc5 6 * You may obtain a copy of the License at
youkee 0:1ad0e04b1bc5 7 *
youkee 0:1ad0e04b1bc5 8 * http://www.apache.org/licenses/LICENSE-2.0
youkee 0:1ad0e04b1bc5 9 *
youkee 0:1ad0e04b1bc5 10 * Unless required by applicable law or agreed to in writing, software
youkee 0:1ad0e04b1bc5 11 * distributed under the License is distributed on an "AS IS" BASIS,
youkee 0:1ad0e04b1bc5 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
youkee 0:1ad0e04b1bc5 13 * See the License for the specific language governing permissions and
youkee 0:1ad0e04b1bc5 14 * limitations under the License.
youkee 0:1ad0e04b1bc5 15 */
youkee 0:1ad0e04b1bc5 16
youkee 0:1ad0e04b1bc5 17 #include "common/common.h"
youkee 0:1ad0e04b1bc5 18 #include "nordic_common.h"
youkee 0:1ad0e04b1bc5 19
youkee 0:1ad0e04b1bc5 20 #include "btle.h"
youkee 0:1ad0e04b1bc5 21
youkee 0:1ad0e04b1bc5 22 #include "ble_flash.h"
youkee 0:1ad0e04b1bc5 23 #include "ble_conn_params.h"
youkee 0:1ad0e04b1bc5 24
youkee 0:1ad0e04b1bc5 25 #include "btle_gap.h"
youkee 0:1ad0e04b1bc5 26 #include "btle_advertising.h"
youkee 0:1ad0e04b1bc5 27 #include "custom/custom_helper.h"
youkee 0:1ad0e04b1bc5 28
youkee 0:1ad0e04b1bc5 29 #include "ble/GapEvents.h"
youkee 0:1ad0e04b1bc5 30 #include "nRF5xGap.h"
youkee 0:1ad0e04b1bc5 31 #include "nRF5xGattServer.h"
youkee 0:1ad0e04b1bc5 32 #include "nRF5xSecurityManager.h"
youkee 0:1ad0e04b1bc5 33
youkee 0:1ad0e04b1bc5 34 extern "C" {
youkee 0:1ad0e04b1bc5 35 #include "pstorage.h"
youkee 0:1ad0e04b1bc5 36 #include "device_manager.h"
youkee 0:1ad0e04b1bc5 37 #include "softdevice_handler.h"
youkee 0:1ad0e04b1bc5 38 #include "ble_stack_handler_types.h"
youkee 0:1ad0e04b1bc5 39 }
youkee 0:1ad0e04b1bc5 40
youkee 0:1ad0e04b1bc5 41 #include "ble_hci.h"
youkee 0:1ad0e04b1bc5 42 #include "btle_discovery.h"
youkee 0:1ad0e04b1bc5 43
youkee 0:1ad0e04b1bc5 44 extern "C" void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name);
youkee 0:1ad0e04b1bc5 45 void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name);
youkee 0:1ad0e04b1bc5 46
youkee 0:1ad0e04b1bc5 47 static void btle_handler(ble_evt_t *p_ble_evt);
youkee 0:1ad0e04b1bc5 48
youkee 0:1ad0e04b1bc5 49 static void sys_evt_dispatch(uint32_t sys_evt)
youkee 0:1ad0e04b1bc5 50 {
youkee 0:1ad0e04b1bc5 51 pstorage_sys_event_handler(sys_evt);
youkee 0:1ad0e04b1bc5 52 }
youkee 0:1ad0e04b1bc5 53
youkee 0:1ad0e04b1bc5 54 /**
youkee 0:1ad0e04b1bc5 55 * This function is called in interrupt context to handle BLE events; i.e. pull
youkee 0:1ad0e04b1bc5 56 * system and user events out of the pending events-queue of the BLE stack. The
youkee 0:1ad0e04b1bc5 57 * BLE stack signals the availability of events by the triggering the SWI2
youkee 0:1ad0e04b1bc5 58 * interrupt, which forwards the handling to this function.
youkee 0:1ad0e04b1bc5 59 *
youkee 0:1ad0e04b1bc5 60 * The event processing loop is implemented in intern_softdevice_events_execute().
youkee 0:1ad0e04b1bc5 61 *
youkee 0:1ad0e04b1bc5 62 * In mbed OS, a callback for intern_softdevice_events_execute() is posted
youkee 0:1ad0e04b1bc5 63 * to the scheduler, which then executes in thread mode. In mbed-classic,
youkee 0:1ad0e04b1bc5 64 * event processing happens right-away in interrupt context (which is more
youkee 0:1ad0e04b1bc5 65 * risk-prone). In either case, the logic of event processing is identical.
youkee 0:1ad0e04b1bc5 66 */
youkee 0:1ad0e04b1bc5 67 static uint32_t eventHandler()
youkee 0:1ad0e04b1bc5 68 {
youkee 0:1ad0e04b1bc5 69 #ifdef YOTTA_CFG_MBED_OS
youkee 0:1ad0e04b1bc5 70 minar::Scheduler::postCallback(intern_softdevice_events_execute);
youkee 0:1ad0e04b1bc5 71 #else
youkee 0:1ad0e04b1bc5 72 intern_softdevice_events_execute();
youkee 0:1ad0e04b1bc5 73 #endif
youkee 0:1ad0e04b1bc5 74
youkee 0:1ad0e04b1bc5 75 return NRF_SUCCESS;
youkee 0:1ad0e04b1bc5 76 }
youkee 0:1ad0e04b1bc5 77
youkee 0:1ad0e04b1bc5 78 error_t btle_init(void)
youkee 0:1ad0e04b1bc5 79 {
youkee 0:1ad0e04b1bc5 80 nrf_clock_lfclksrc_t clockSource;
youkee 0:1ad0e04b1bc5 81 if (NRF_CLOCK->LFCLKSRC & (CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos)) {
youkee 0:1ad0e04b1bc5 82 clockSource = NRF_CLOCK_LFCLKSRC_XTAL_20_PPM;
youkee 0:1ad0e04b1bc5 83 } else {
youkee 0:1ad0e04b1bc5 84 clockSource = NRF_CLOCK_LFCLKSRC_RC_250_PPM_4000MS_CALIBRATION;
youkee 0:1ad0e04b1bc5 85 }
youkee 0:1ad0e04b1bc5 86 SOFTDEVICE_HANDLER_INIT(clockSource, eventHandler);
youkee 0:1ad0e04b1bc5 87
youkee 0:1ad0e04b1bc5 88 // Enable BLE stack
youkee 0:1ad0e04b1bc5 89 /**
youkee 0:1ad0e04b1bc5 90 * Using this call, the application can select whether to include the
youkee 0:1ad0e04b1bc5 91 * Service Changed characteristic in the GATT Server. The default in all
youkee 0:1ad0e04b1bc5 92 * previous releases has been to include the Service Changed characteristic,
youkee 0:1ad0e04b1bc5 93 * but this affects how GATT clients behave. Specifically, it requires
youkee 0:1ad0e04b1bc5 94 * clients to subscribe to this attribute and not to cache attribute handles
youkee 0:1ad0e04b1bc5 95 * between connections unless the devices are bonded. If the application
youkee 0:1ad0e04b1bc5 96 * does not need to change the structure of the GATT server attributes at
youkee 0:1ad0e04b1bc5 97 * runtime this adds unnecessary complexity to the interaction with peer
youkee 0:1ad0e04b1bc5 98 * clients. If the SoftDevice is enabled with the Service Changed
youkee 0:1ad0e04b1bc5 99 * Characteristics turned off, then clients are allowed to cache attribute
youkee 0:1ad0e04b1bc5 100 * handles making applications simpler on both sides.
youkee 0:1ad0e04b1bc5 101 */
youkee 0:1ad0e04b1bc5 102 static const bool IS_SRVC_CHANGED_CHARACT_PRESENT = true;
youkee 0:1ad0e04b1bc5 103 ble_enable_params_t enableParams = {
youkee 0:1ad0e04b1bc5 104 .gatts_enable_params = {
youkee 0:1ad0e04b1bc5 105 .service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT
youkee 0:1ad0e04b1bc5 106 }
youkee 0:1ad0e04b1bc5 107 };
youkee 0:1ad0e04b1bc5 108 if (sd_ble_enable(&enableParams) != NRF_SUCCESS) {
youkee 0:1ad0e04b1bc5 109 return ERROR_INVALID_PARAM;
youkee 0:1ad0e04b1bc5 110 }
youkee 0:1ad0e04b1bc5 111
youkee 0:1ad0e04b1bc5 112 ble_gap_addr_t addr;
youkee 0:1ad0e04b1bc5 113 if (sd_ble_gap_address_get(&addr) != NRF_SUCCESS) {
youkee 0:1ad0e04b1bc5 114 return ERROR_INVALID_PARAM;
youkee 0:1ad0e04b1bc5 115 }
youkee 0:1ad0e04b1bc5 116 if (sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &addr) != NRF_SUCCESS) {
youkee 0:1ad0e04b1bc5 117 return ERROR_INVALID_PARAM;
youkee 0:1ad0e04b1bc5 118 }
youkee 0:1ad0e04b1bc5 119
youkee 0:1ad0e04b1bc5 120 ASSERT_STATUS( softdevice_ble_evt_handler_set(btle_handler));
youkee 0:1ad0e04b1bc5 121 ASSERT_STATUS( softdevice_sys_evt_handler_set(sys_evt_dispatch));
youkee 0:1ad0e04b1bc5 122
youkee 0:1ad0e04b1bc5 123 btle_gap_init();
youkee 0:1ad0e04b1bc5 124
youkee 0:1ad0e04b1bc5 125 return ERROR_NONE;
youkee 0:1ad0e04b1bc5 126 }
youkee 0:1ad0e04b1bc5 127
youkee 0:1ad0e04b1bc5 128 static void btle_handler(ble_evt_t *p_ble_evt)
youkee 0:1ad0e04b1bc5 129 {
youkee 0:1ad0e04b1bc5 130 /* Library service handlers */
youkee 0:1ad0e04b1bc5 131 #if SDK_CONN_PARAMS_MODULE_ENABLE
youkee 0:1ad0e04b1bc5 132 ble_conn_params_on_ble_evt(p_ble_evt);
youkee 0:1ad0e04b1bc5 133 #endif
youkee 0:1ad0e04b1bc5 134
youkee 0:1ad0e04b1bc5 135 dm_ble_evt_handler(p_ble_evt);
youkee 0:1ad0e04b1bc5 136
youkee 0:1ad0e04b1bc5 137 #if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
youkee 0:1ad0e04b1bc5 138 bleGattcEventHandler(p_ble_evt);
youkee 0:1ad0e04b1bc5 139 #endif
youkee 0:1ad0e04b1bc5 140
youkee 0:1ad0e04b1bc5 141 /* Custom event handler */
youkee 0:1ad0e04b1bc5 142 switch (p_ble_evt->header.evt_id) {
youkee 0:1ad0e04b1bc5 143 case BLE_GAP_EVT_CONNECTED: {
youkee 0:1ad0e04b1bc5 144 Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle;
youkee 0:1ad0e04b1bc5 145 #if defined(TARGET_MCU_NRF51_16K_S110) || defined(TARGET_MCU_NRF51_32K_S110)
youkee 0:1ad0e04b1bc5 146 /* Only peripheral role is supported by S110 */
youkee 0:1ad0e04b1bc5 147 Gap::Role_t role = Gap::PERIPHERAL;
youkee 0:1ad0e04b1bc5 148 #else
youkee 0:1ad0e04b1bc5 149 Gap::Role_t role = static_cast<Gap::Role_t>(p_ble_evt->evt.gap_evt.params.connected.role);
youkee 0:1ad0e04b1bc5 150 #endif
youkee 0:1ad0e04b1bc5 151 nRF5xGap::getInstance().setConnectionHandle(handle);
youkee 0:1ad0e04b1bc5 152 const Gap::ConnectionParams_t *params = reinterpret_cast<Gap::ConnectionParams_t *>(&(p_ble_evt->evt.gap_evt.params.connected.conn_params));
youkee 0:1ad0e04b1bc5 153 const ble_gap_addr_t *peer = &p_ble_evt->evt.gap_evt.params.connected.peer_addr;
youkee 0:1ad0e04b1bc5 154 const ble_gap_addr_t *own = &p_ble_evt->evt.gap_evt.params.connected.own_addr;
youkee 0:1ad0e04b1bc5 155 nRF5xGap::getInstance().processConnectionEvent(handle,
youkee 0:1ad0e04b1bc5 156 role,
youkee 0:1ad0e04b1bc5 157 static_cast<Gap::AddressType_t>(peer->addr_type), peer->addr,
youkee 0:1ad0e04b1bc5 158 static_cast<Gap::AddressType_t>(own->addr_type), own->addr,
youkee 0:1ad0e04b1bc5 159 params);
youkee 0:1ad0e04b1bc5 160 break;
youkee 0:1ad0e04b1bc5 161 }
youkee 0:1ad0e04b1bc5 162
youkee 0:1ad0e04b1bc5 163 case BLE_GAP_EVT_DISCONNECTED: {
youkee 0:1ad0e04b1bc5 164 Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle;
youkee 0:1ad0e04b1bc5 165 // Since we are not in a connection and have not started advertising,
youkee 0:1ad0e04b1bc5 166 // store bonds
youkee 0:1ad0e04b1bc5 167 nRF5xGap::getInstance().setConnectionHandle (BLE_CONN_HANDLE_INVALID);
youkee 0:1ad0e04b1bc5 168
youkee 0:1ad0e04b1bc5 169 Gap::DisconnectionReason_t reason;
youkee 0:1ad0e04b1bc5 170 switch (p_ble_evt->evt.gap_evt.params.disconnected.reason) {
youkee 0:1ad0e04b1bc5 171 case BLE_HCI_LOCAL_HOST_TERMINATED_CONNECTION:
youkee 0:1ad0e04b1bc5 172 reason = Gap::LOCAL_HOST_TERMINATED_CONNECTION;
youkee 0:1ad0e04b1bc5 173 break;
youkee 0:1ad0e04b1bc5 174 case BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION:
youkee 0:1ad0e04b1bc5 175 reason = Gap::REMOTE_USER_TERMINATED_CONNECTION;
youkee 0:1ad0e04b1bc5 176 break;
youkee 0:1ad0e04b1bc5 177 case BLE_HCI_CONN_INTERVAL_UNACCEPTABLE:
youkee 0:1ad0e04b1bc5 178 reason = Gap::CONN_INTERVAL_UNACCEPTABLE;
youkee 0:1ad0e04b1bc5 179 break;
youkee 0:1ad0e04b1bc5 180 default:
youkee 0:1ad0e04b1bc5 181 /* Please refer to the underlying transport library for an
youkee 0:1ad0e04b1bc5 182 * interpretion of this reason's value. */
youkee 0:1ad0e04b1bc5 183 reason = static_cast<Gap::DisconnectionReason_t>(p_ble_evt->evt.gap_evt.params.disconnected.reason);
youkee 0:1ad0e04b1bc5 184 break;
youkee 0:1ad0e04b1bc5 185 }
youkee 0:1ad0e04b1bc5 186 nRF5xGap::getInstance().processDisconnectionEvent(handle, reason);
youkee 0:1ad0e04b1bc5 187 break;
youkee 0:1ad0e04b1bc5 188 }
youkee 0:1ad0e04b1bc5 189
youkee 0:1ad0e04b1bc5 190 case BLE_GAP_EVT_PASSKEY_DISPLAY:
youkee 0:1ad0e04b1bc5 191 nRF5xSecurityManager::getInstance().processPasskeyDisplayEvent(p_ble_evt->evt.gap_evt.conn_handle, p_ble_evt->evt.gap_evt.params.passkey_display.passkey);
youkee 0:1ad0e04b1bc5 192 break;
youkee 0:1ad0e04b1bc5 193
youkee 0:1ad0e04b1bc5 194 case BLE_GAP_EVT_TIMEOUT:
youkee 0:1ad0e04b1bc5 195 nRF5xGap::getInstance().processTimeoutEvent(static_cast<Gap::TimeoutSource_t>(p_ble_evt->evt.gap_evt.params.timeout.src));
youkee 0:1ad0e04b1bc5 196 break;
youkee 0:1ad0e04b1bc5 197
youkee 0:1ad0e04b1bc5 198 case BLE_GATTC_EVT_TIMEOUT:
youkee 0:1ad0e04b1bc5 199 case BLE_GATTS_EVT_TIMEOUT:
youkee 0:1ad0e04b1bc5 200 // Disconnect on GATT Server and Client timeout events.
youkee 0:1ad0e04b1bc5 201 // ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle,
youkee 0:1ad0e04b1bc5 202 // BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION));
youkee 0:1ad0e04b1bc5 203 break;
youkee 0:1ad0e04b1bc5 204
youkee 0:1ad0e04b1bc5 205 case BLE_GAP_EVT_ADV_REPORT: {
youkee 0:1ad0e04b1bc5 206 const ble_gap_evt_adv_report_t *advReport = &p_ble_evt->evt.gap_evt.params.adv_report;
youkee 0:1ad0e04b1bc5 207 nRF5xGap::getInstance().processAdvertisementReport(advReport->peer_addr.addr,
youkee 0:1ad0e04b1bc5 208 advReport->rssi,
youkee 0:1ad0e04b1bc5 209 advReport->scan_rsp,
youkee 0:1ad0e04b1bc5 210 static_cast<GapAdvertisingParams::AdvertisingType_t>(advReport->type),
youkee 0:1ad0e04b1bc5 211 advReport->dlen,
youkee 0:1ad0e04b1bc5 212 advReport->data);
youkee 0:1ad0e04b1bc5 213 break;
youkee 0:1ad0e04b1bc5 214 }
youkee 0:1ad0e04b1bc5 215
youkee 0:1ad0e04b1bc5 216 default:
youkee 0:1ad0e04b1bc5 217 break;
youkee 0:1ad0e04b1bc5 218 }
youkee 0:1ad0e04b1bc5 219
youkee 0:1ad0e04b1bc5 220 nRF5xGattServer::getInstance().hwCallback(p_ble_evt);
youkee 0:1ad0e04b1bc5 221 }
youkee 0:1ad0e04b1bc5 222
youkee 0:1ad0e04b1bc5 223 /*! @brief Callback when an error occurs inside the SoftDevice */
youkee 0:1ad0e04b1bc5 224 void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name)
youkee 0:1ad0e04b1bc5 225 {
youkee 0:1ad0e04b1bc5 226 ASSERT(false, (void) 0);
youkee 0:1ad0e04b1bc5 227 }
youkee 0:1ad0e04b1bc5 228
youkee 0:1ad0e04b1bc5 229 /*!
youkee 0:1ad0e04b1bc5 230 @brief Handler for general errors above the SoftDevice layer.
youkee 0:1ad0e04b1bc5 231 Typically we can' recover from this so we do a reset.
youkee 0:1ad0e04b1bc5 232 */
youkee 0:1ad0e04b1bc5 233 void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name)
youkee 0:1ad0e04b1bc5 234 {
youkee 0:1ad0e04b1bc5 235 ASSERT_STATUS_RET_VOID( error_code );
youkee 0:1ad0e04b1bc5 236 NVIC_SystemReset();
youkee 0:1ad0e04b1bc5 237 }