Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nrf52 nrf51 unified integration copy #2261

Merged
merged 109 commits into from
Jul 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
109 commits
Select commit Hold shift + click to select a range
9d66dca
Add NRF52 and NRF52_TARGETS.
pan- Jun 14, 2016
9d85bfd
Implementation of nrf52 target (draft).
pan- Jun 14, 2016
aa19a97
RTX port for nrf52 based targets.
pan- Jun 14, 2016
a3b9c78
Update detection code for NRF52.
pan- Jun 15, 2016
70ffeb6
Add softdevice S132 binary for nrf52 targets.
pan- Jun 15, 2016
e5b7579
Merge pull request #3 from pan-/nrf52_port
anangl Jun 15, 2016
f2c4b77
Factorization of NRF51 and NRF52 targets.
pan- Jun 15, 2016
a1fb8d2
Merge pull request #4 from pan-/nordic_sdk_integration
anangl Jun 15, 2016
216fa9d
Introduce nordic SDK v11, cleanup path and targets.
pan- Jun 15, 2016
6afe131
Merge pull request #5 from pan-/nordic_sdk_integration
anangl Jun 15, 2016
2eaa468
Added missing SDK drivers, added SOFTDEVICE_PRESENT macros in targets…
anangl Jun 15, 2016
96a1759
Updated device definition file for NRF52_DK target.
anangl Jun 15, 2016
df6bd2f
Changed the drivers HAL implementations to the ones using NRF5 SDK, m…
anangl Jun 15, 2016
082fd40
Added LF clock starting code to SysInit.
anangl Jun 15, 2016
52b9563
Added modifications in SDK drivers needed for mbed HAL implementation.
anangl Jun 16, 2016
4fcffd4
fix pwm hal implemenation for nRF51.
nvlsianpu Jun 16, 2016
441a665
fix implemet pwm_write
nvlsianpu Jun 16, 2016
c8dc17a
Import radio notification from the nordic SDK (required y BLE
pan- Jun 17, 2016
7ad0af3
Enable BLE feature on NRF51 and nrf52.
pan- Jun 17, 2016
1b826f2
Namespace softdevice headers to avoid conflict wih other headers.
pan- Jun 17, 2016
38671de
Remove unused BLE services.
pan- Jun 17, 2016
10c803e
Add LF clock initialization in startup sequence.
pan- Jun 17, 2016
96b2604
Remove obsolete softdevices.
pan- Jun 17, 2016
3fe77a5
Fix known BUG in nrf_svc.h when compiled by GCC.
pan- Jun 17, 2016
d1b902a
Update NRF51 link and startup sequence.
pan- Jun 17, 2016
daf7ef3
Import assert fix from mbed v3.
pan- Jun 17, 2016
05fea6e
Corection in macros for NRF5x devices
nvlsianpu Jun 21, 2016
089143e
Merge pull request #7 from pan-/ble_integration
nvlsianpu Jun 21, 2016
22b5c78
Fixing test builds for devices with softdevices.
bridadan Jun 14, 2016
6c7d15d
Re-enabled SPI after adjusting HAL implementation to slightly differe…
anangl Jun 22, 2016
601b96d
Restored alternative method of RTOS tick generation for NRF51, which …
anangl Jun 22, 2016
f880312
Merge branch 'nordic_sdk_integration' of https://github.com/ARMmbed/m…
anangl Jun 23, 2016
bb2111a
Merge pull request #8 from pan-/backport_tests_fix
anangl Jun 23, 2016
8249a7f
Merge branch 'nordic_sdk_integration' of https://github.com/ARMmbed/m…
anangl Jun 23, 2016
e259029
Merge pull request #6 from anangl/nordic_sdk_integration
nvlsianpu Jun 23, 2016
3bdbccb
Corrected serial_api implementation, so 'serial_putc' no longer requi…
anangl Jun 23, 2016
ba863f4
Enabled I2C_ASYNCH.
anangl Jun 24, 2016
1357fb0
Polished ticker related code.
anangl Jun 23, 2016
a664be0
Fix AnalogInput fo nRF51.
nvlsianpu Jun 27, 2016
5ba9392
Merge branch 'nordic_sdk_integration' of https://github.com/nvlsianpu…
nvlsianpu Jun 27, 2016
f21ca34
Merge pull request #9 from anangl/nordic_sdk_integration
nvlsianpu Jun 28, 2016
1c262e3
remove unwanted initialization of adc_value
nvlsianpu Jun 28, 2016
8de419c
Merge pull request #10 from nvlsianpu/nordic_sdk_integration
anangl Jun 28, 2016
79ee724
Merge pull request #3 from ARMmbed/nordic_sdk_integration
anangl Jun 28, 2016
3ff34ef
Merge branch 'nordic_sdk_integration' into hal_improvements
anangl Jun 28, 2016
1688054
Merge pull request #4 from anangl/hal_improvements
anangl Jun 28, 2016
eef471b
AnalogIn api implenetation for nRF52
nvlsianpu Jun 28, 2016
63bfe7d
Merge branch 'nordic_sdk_integration' of https://github.com/nvlsianpu…
nvlsianpu Jun 28, 2016
44bef41
AnalogIn api implenetation for nRF52 - supplement in target descriptor
nvlsianpu Jun 29, 2016
3bde75a
Merge pull request #11 from anangl/nordic_sdk_integration
nvlsianpu Jun 29, 2016
f19efb6
AnalogIn for nRF51 Fixes
nvlsianpu Jun 29, 2016
9d137c8
AnalogIn for nRF52 add u16 readout API realization. Polish rest of th…
nvlsianpu Jun 29, 2016
adf15c8
CR of AnalogIn
nvlsianpu Jun 30, 2016
02bc98a
Merge branch 'nordic_sdk_integration' of https://github.com/ARMmbed/m…
nvlsianpu Jun 30, 2016
f02d431
Added multi-instance and slave mode support to spi_api.
anangl Jun 29, 2016
fce8a30
Merge branch 'nordic_sdk_integration' of https://github.com/ARMmbed/m…
anangl Jul 1, 2016
e5a2bfc
Merge pull request #13 from anangl/hal_improvements
anangl Jul 1, 2016
bf70d5a
Enabled lp_ticker after adjusting its implementation to the new API.
anangl Jul 1, 2016
0249ee1
add missing dates in some nordic license disclamer
nvlsianpu Jul 1, 2016
7e62fc3
Merge branch 'nordic_sdk_integration' of https://github.com/ARMmbed/m…
nvlsianpu Jul 1, 2016
f7d53d1
New Nordic license clause
nvlsianpu Jul 1, 2016
fb7b463
Merge pull request #15 from nvlsianpu/nordic_sdk_integration
nvlsianpu Jul 1, 2016
75ba6b7
Polished tickers implementation - moved interrupt clearing code.
anangl Jul 4, 2016
6a31d1a
Merge pull request #14 from anangl/hal_improvements
anangl Jul 4, 2016
0d9dc1e
Merge pull request #16 from ARMmbed/nordic_sdk_integration
pan- Jul 4, 2016
a8c27bd
startup file for nRF52 basing on nRF51 and nordic SDK v11.
nvlsianpu Jul 5, 2016
bd8c335
add scater files forARM CC and IAR for nRF52
nvlsianpu Jul 6, 2016
17c7250
os_tick for iar
nvlsianpu Jul 7, 2016
07ce12f
nrf52 PwmOut in progress.
nvlsianpu Jul 1, 2016
7798651
nRF52 PwmOut implementation debug
nvlsianpu Jul 8, 2016
9f33787
PWM for nRF52 full debuged.
nvlsianpu Jul 12, 2016
3519122
cCR changes for nRF52 PwmOut implementation.
nvlsianpu Jul 12, 2016
3c93283
CR changes for PWm nRF52 part 2.
nvlsianpu Jul 12, 2016
5d5ea9a
CR changes for PWM of nRF52 part 3.
nvlsianpu Jul 12, 2016
eaa9d3b
Merge pull request #19 from nvlsianpu/add_nrf52_pwm_rebase
nvlsianpu Jul 12, 2016
9745eff
Merge commit 'ce830296d0297a8da543c24134bf859710fd7698' into epr_inte…
pan- Jul 13, 2016
3c8c82b
make posible IAR compilation
nvlsianpu Jul 13, 2016
f281ecd
Comand IAR toolchain to provide intel-hex as a build product.
nvlsianpu Jul 14, 2016
7861cef
Fix: IAR build for NRF5x produce proper combined intel-hex file.
nvlsianpu Jul 14, 2016
5d7c5cf
Corect nRF51 IAR scatre files in order to be compatible with softdevi…
nvlsianpu Jul 14, 2016
7eaef66
rename scater file for nRF52 ARM_5
nvlsianpu Jul 14, 2016
d433b55
changes from CR of IAR fixes for nRF5x port
nvlsianpu Jul 15, 2016
62543f1
remove unrichable return statment
nvlsianpu Jul 15, 2016
d998437
lf clock config prove of concept
nvlsianpu Jul 19, 2016
be46875
startup configuraton of LF oscilator over mbed configuration system.
nvlsianpu Jul 19, 2016
fa5937f
Add configuration of LF clk source in targer.json for targets MCU_NRF…
nvlsianpu Jul 19, 2016
3368fa8
Merge pull request #22 from nvlsianpu/anpu_arm_cc_sandbox
pan- Jul 19, 2016
6d214eb
ARM cc has difficulties to link binary with undefined symbols, even if
pan- Jul 19, 2016
ea0ae68
Update memory layout for nrf51 targets.
pan- Jul 19, 2016
0bb2d33
Merge pull request #24 from nvlsianpu/nrf5x_lf_clk_config
pan- Jul 20, 2016
847d0f6
Remove app_timer and dependencies instead of adding a dependency to RTX.
pan- Jul 20, 2016
c3ddc9a
Merge pull request #25 from pan-/armcc_support
pan- Jul 20, 2016
406ac8f
Makes room for old NRF51 implementation.
pan- Jul 20, 2016
a81b8b1
Set correct compiler support for NRF51 and NRF52 based targets.
pan- Jul 20, 2016
478a7fd
Remove files from unused targets.
pan- Jul 21, 2016
6399ad1
Merge pull request #26 from pan-/nrf51_compatibility
pan- Jul 21, 2016
f10cf7f
Merge remote-tracking branch 'mbed-nordic/epr_integration' into nrf52…
pan- Jul 21, 2016
a9a0657
Reintegration of TARGET_MCU_NRF51822 from master.
pan- Jul 21, 2016
e0a5ca1
Bring new ble implementation from mbed-os/workshop-nordic.
pan- Jul 21, 2016
50ce4fd
Initialize ticker prior to read, this is required by the wait_api mod…
pan- Jul 22, 2016
900acc6
Fixed RTC1_IRQHandler linking problem.
anangl Jul 22, 2016
42d9898
Fixed RTC1_IRQHandler in a bit more elegant way.
anangl Jul 22, 2016
d59608a
Refactored serial_api implementation, improved asynchronous routines.
anangl Jul 22, 2016
1f53752
Merge pull request #2 from anangl/nrf52_nrf51_unified_integration
pan- Jul 22, 2016
32afc59
Removed no longer used UART-related SDK files.
anangl Jul 22, 2016
5206add
Fix intermittent lockup in echo test
c1728p9 Jul 23, 2016
a8a7df0
Corrected serial_api - added waiting for TX completion in 'serial_put…
anangl Jul 25, 2016
91bd396
Merge branch 'nrf52_nrf51_unified_integration' of https://github.com/…
anangl Jul 25, 2016
c05272c
Merge pull request #3 from anangl/refactored_serial_api
pan- Jul 25, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
344 changes: 344 additions & 0 deletions features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF5/CHANGELOG.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
This module contains softdevice which comes with The Nordic Softdevice License Agreement,
a BSD-like licence for binary distributions, offered by Nordic for use in mbed. Some
other files come from the mbed SDK, and are licensed under Apache-2.0. Unless
specifically indicated otherwise in a file, files are licensed under the
Apache 2.0 license, as can be found in: apache-2.0.txt. The Nordic Semiconductor Softdevice
License Agreement can be found in softdevice_nrf51822_licence_agreement.txt.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright (c) 2015 ARM Limited

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* S110/S120/S130 License Agreement
*
* Copyright (c) 2015, Nordic Semiconductor ASA, All rights reserved.
*
* Redistribution. Redistribution and use in binary form, without modification,
* are permitted provided that the following conditions are met:
*
* • Redistributions must reproduce the above copyright notice and the following
* disclaimer in the documentation and/or other materials provided with the
* distribution.
* • Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* • No reverse engineering, decompilation, or disassembly of this software is
* permitted.
*
* DISCLAIMER.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* /
36 changes: 36 additions & 0 deletions features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF5/module.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "ble-nrf51822",
"version": "2.7.1",
"description": "Nordic stack and drivers for the mbed BLE API.",
"keywords": [
"Bluetooth",
"BLE",
"mbed",
"mbed-official"
],
"author": "Rohit Grover",
"repository": {
"url": "git@github.com:ARMmbed/ble-nRF51822.git",
"type": "git"
},
"homepage": "https://developer.mbed.org/teams/Nordic-Semiconductor/",
"licenses": [
{
"url": "https://spdx.org/licenses/Apache-2.0",
"type": "Apache-2.0"
},
{
"type": "LicenseRef-softdevice_nrf51822_licence_agreement.txt"
}
],
"dependencies": {
"ble": "^2.6.0",
"nrf51-sdk": "^2.4.0"
},
"extraIncludes": [
"source/btle",
"source/btle/custom",
"source/common"
],
"targetDependencies": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* S110/S120/S130 License Agreement
*
* Copyright (c) 2015, Nordic Semiconductor ASA, All rights reserved.
*
* Redistribution. Redistribution and use in binary form, without modification,
* are permitted provided that the following conditions are met:
*
* • Redistributions must reproduce the above copyright notice and the following
* disclaimer in the documentation and/or other materials provided with the
* distribution.
* • Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* • No reverse engineering, decompilation, or disassembly of this software is
* permitted.
*
* DISCLAIMER.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* /
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "common/common.h"
#include "nordic_common.h"

#include "btle.h"
#include "btle_clock.h"

#include "ble_flash.h"
#include "ble_conn_params.h"

#include "btle_gap.h"
#include "btle_advertising.h"
#include "custom/custom_helper.h"

#include "ble/GapEvents.h"
#include "nRF5xn.h"

extern "C" {
#include "pstorage.h"
#include "device_manager.h"
#include "softdevice_handler.h"
#include "ble_stack_handler_types.h"
}

#include "nrf_ble_hci.h"
#include "btle_discovery.h"

#include "nRF5xGattClient.h"
#include "nRF5xServiceDiscovery.h"
#include "nRF5xCharacteristicDescriptorDiscoverer.h"

bool isEventsSignaled = false;

extern "C" void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name);
void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name);

static void btle_handler(ble_evt_t *p_ble_evt);

#if 0
#define CENTRAL_LINK_COUNT (YOTTA_CFG_NORDIC_BLE_CENTRAL_LINKS) /**<number of central links used by the application. When changing this number remember to adjust the RAM settings */
/** If value for YOTTA_CFG_NORDIC_BLE_PERIPHERAL_LINKS was used, ram settings are adjusted by the yotta target module. */
#define PERIPHERAL_LINK_COUNT (YOTTA_CFG_NORDIC_BLE_PERIPHERAL_LINKS) /**<number of peripheral links used by the application. When changing this number remember to adjust the RAM settings*/
/** If value for YOTTA_CFG_NORDIC_BLE_CENTRAL_LINKS was used, ram settings are adjusted by the yotta target module. */
#define GATTS_ATTR_TAB_SIZE (YOTTA_CFG_NORDIC_BLE_GATTS_ATTR_TAB_SIZE) /**< GATTS attribite table size. */
/** If value for YOTTA_CFG_NORDIC_BLE_GATTS_ATTR_TAB_SIZE was used, ram settings are adjusted by the yotta target module. */
#else
#define CENTRAL_LINK_COUNT 3 /**<number of central links used by the application. When changing this number remember to adjust the RAM settings */
/** If value for YOTTA_CFG_NORDIC_BLE_PERIPHERAL_LINKS was used, ram settings are adjusted by the yotta target module. */
#define PERIPHERAL_LINK_COUNT 1 /**<number of peripheral links used by the application. When changing this number remember to adjust the RAM settings*/
/** If value for YOTTA_CFG_NORDIC_BLE_CENTRAL_LINKS was used, ram settings are adjusted by the yotta target module. */
#define GATTS_ATTR_TAB_SIZE 0x600 /**< GATTS attribite table size. */
/** If value for YOTTA_CFG_NORDIC_BLE_GATTS_ATTR_TAB_SIZE was used, ram settings are adjusted by the yotta target module. */

#endif

static void sys_evt_dispatch(uint32_t sys_evt)
{
pstorage_sys_event_handler(sys_evt);
}

/**
* This function is called in interrupt context to handle BLE events; i.e. pull
* system and user events out of the pending events-queue of the BLE stack. The
* BLE stack signals the availability of events by the triggering the SWI2
* interrupt, which forwards the handling to this function.
*
* The event processing loop is implemented in intern_softdevice_events_execute().
*
* This function will signal to the user code by calling signalEventsToProcess
* that their is events to process and BLE::processEvents should be called.
*/
static uint32_t signalEvent()
{
if(isEventsSignaled == false) {
isEventsSignaled = true;
nRF5xn::Instance(BLE::DEFAULT_INSTANCE).signalEventsToProcess(BLE::DEFAULT_INSTANCE);
}
return NRF_SUCCESS;
}

error_t btle_init(void)
{
nrf_clock_lf_cfg_t clockConfiguration;

// Configure the LF clock according to values provided by btle_clock.h.
// It is input from the chain of the yotta configuration system.
clockConfiguration.source = LFCLK_CONF_SOURCE;
clockConfiguration.xtal_accuracy = LFCLK_CONF_ACCURACY;
clockConfiguration.rc_ctiv = LFCLK_CONF_RC_CTIV;
clockConfiguration.rc_temp_ctiv = LFCLK_CONF_RC_TEMP_CTIV;

SOFTDEVICE_HANDLER_INIT(&clockConfiguration, signalEvent);

// Enable BLE stack
/**
* Using this call, the application can select whether to include the
* Service Changed characteristic in the GATT Server. The default in all
* previous releases has been to include the Service Changed characteristic,
* but this affects how GATT clients behave. Specifically, it requires
* clients to subscribe to this attribute and not to cache attribute handles
* between connections unless the devices are bonded. If the application
* does not need to change the structure of the GATT server attributes at
* runtime this adds unnecessary complexity to the interaction with peer
* clients. If the SoftDevice is enabled with the Service Changed
* Characteristics turned off, then clients are allowed to cache attribute
* handles making applications simpler on both sides.
*/
static const bool IS_SRVC_CHANGED_CHARACT_PRESENT = true;

ble_enable_params_t ble_enable_params;
uint32_t err_code = softdevice_enable_get_default_config(CENTRAL_LINK_COUNT,
PERIPHERAL_LINK_COUNT,
&ble_enable_params);

ble_enable_params.gatts_enable_params.attr_tab_size = GATTS_ATTR_TAB_SIZE;
ble_enable_params.gatts_enable_params.service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT;
ble_enable_params.common_enable_params.vs_uuid_count = UUID_TABLE_MAX_ENTRIES;

if(err_code != NRF_SUCCESS) {
return ERROR_INVALID_PARAM;
}

if (softdevice_enable(&ble_enable_params) != NRF_SUCCESS) {
return ERROR_INVALID_PARAM;
}

ble_gap_addr_t addr;
if (sd_ble_gap_address_get(&addr) != NRF_SUCCESS) {
return ERROR_INVALID_PARAM;
}
if (sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &addr) != NRF_SUCCESS) {
return ERROR_INVALID_PARAM;
}

ASSERT_STATUS( softdevice_ble_evt_handler_set(btle_handler));
ASSERT_STATUS( softdevice_sys_evt_handler_set(sys_evt_dispatch));

return btle_gap_init();
}

static void btle_handler(ble_evt_t *p_ble_evt)
{
/* Library service handlers */
#if SDK_CONN_PARAMS_MODULE_ENABLE
ble_conn_params_on_ble_evt(p_ble_evt);
#endif

dm_ble_evt_handler(p_ble_evt);

#if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
bleGattcEventHandler(p_ble_evt);
#endif

nRF5xn &ble = nRF5xn::Instance(BLE::DEFAULT_INSTANCE);
nRF5xGap &gap = (nRF5xGap &) ble.getGap();
nRF5xGattServer &gattServer = (nRF5xGattServer &) ble.getGattServer();
nRF5xSecurityManager &securityManager = (nRF5xSecurityManager &) ble.getSecurityManager();

/* Custom event handler */
switch (p_ble_evt->header.evt_id) {
case BLE_GAP_EVT_CONNECTED: {
Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle;
#if defined(TARGET_MCU_NRF51_16K_S110) || defined(TARGET_MCU_NRF51_32K_S110)
/* Only peripheral role is supported by S110 */
Gap::Role_t role = Gap::PERIPHERAL;
#else
Gap::Role_t role = static_cast<Gap::Role_t>(p_ble_evt->evt.gap_evt.params.connected.role);
#endif
gap.setConnectionHandle(handle);
const Gap::ConnectionParams_t *params = reinterpret_cast<Gap::ConnectionParams_t *>(&(p_ble_evt->evt.gap_evt.params.connected.conn_params));
const ble_gap_addr_t *peer = &p_ble_evt->evt.gap_evt.params.connected.peer_addr;
const ble_gap_addr_t *own = &p_ble_evt->evt.gap_evt.params.connected.own_addr;
gap.processConnectionEvent(handle,
role,
static_cast<BLEProtocol::AddressType_t>(peer->addr_type), peer->addr,
static_cast<BLEProtocol::AddressType_t>(own->addr_type), own->addr,
params);
break;
}

case BLE_GAP_EVT_DISCONNECTED: {
Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle;
// Since we are not in a connection and have not started advertising,
// store bonds
gap.setConnectionHandle (BLE_CONN_HANDLE_INVALID);

Gap::DisconnectionReason_t reason;
switch (p_ble_evt->evt.gap_evt.params.disconnected.reason) {
case BLE_HCI_LOCAL_HOST_TERMINATED_CONNECTION:
reason = Gap::LOCAL_HOST_TERMINATED_CONNECTION;
break;
case BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION:
reason = Gap::REMOTE_USER_TERMINATED_CONNECTION;
break;
case BLE_HCI_CONN_INTERVAL_UNACCEPTABLE:
reason = Gap::CONN_INTERVAL_UNACCEPTABLE;
break;
default:
/* Please refer to the underlying transport library for an
* interpretion of this reason's value. */
reason = static_cast<Gap::DisconnectionReason_t>(p_ble_evt->evt.gap_evt.params.disconnected.reason);
break;
}

#if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
// Close all pending discoveries for this connection
nRF5xGattClient& gattClient = ble.getGattClient();
gattClient.characteristicDescriptorDiscoverer().terminate(handle, BLE_ERROR_INVALID_STATE);
gattClient.discovery().terminate(handle);
#endif

gap.processDisconnectionEvent(handle, reason);
break;
}

case BLE_GAP_EVT_PASSKEY_DISPLAY:
securityManager.processPasskeyDisplayEvent(p_ble_evt->evt.gap_evt.conn_handle, p_ble_evt->evt.gap_evt.params.passkey_display.passkey);
break;

case BLE_GAP_EVT_TIMEOUT:
gap.processTimeoutEvent(static_cast<Gap::TimeoutSource_t>(p_ble_evt->evt.gap_evt.params.timeout.src));
break;

case BLE_GATTC_EVT_TIMEOUT:
case BLE_GATTS_EVT_TIMEOUT:
// Disconnect on GATT Server and Client timeout events.
// ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle,
// BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION));
break;

case BLE_GAP_EVT_ADV_REPORT: {
const ble_gap_evt_adv_report_t *advReport = &p_ble_evt->evt.gap_evt.params.adv_report;
gap.processAdvertisementReport(advReport->peer_addr.addr,
advReport->rssi,
advReport->scan_rsp,
static_cast<GapAdvertisingParams::AdvertisingType_t>(advReport->type),
advReport->dlen,
advReport->data);
break;
}

default:
break;
}

gattServer.hwCallback(p_ble_evt);
}

/*! @brief Callback when an error occurs inside the SoftDevice */
void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name)
{
ASSERT(false, (void) 0);
}

/*!
@brief Handler for general errors above the SoftDevice layer.
Typically we can' recover from this so we do a reset.
*/
void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name)
{
ASSERT_STATUS_RET_VOID( error_code );
NVIC_SystemReset();
}
Loading