static void ble_stack_init(void) { ret_code_t err_code; // Initialize the SoftDevice handler module. nrf_clock_lf_cfg_t clock_lf_cfg = NRF_CLOCK_LFCLKSRC; SOFTDEVICE_HANDLER_INIT(&clock_lf_cfg, NULL); // Fetch the start address of the application RAM. uint32_t ram_start = 0; err_code = softdevice_app_ram_start_get(&ram_start); APP_ERROR_CHECK(err_code); // Overwrite some of the default configurations for the BLE stack. ble_cfg_t ble_cfg; memset(&ble_cfg, 0, sizeof(ble_cfg)); ble_cfg.common_cfg.vs_uuid_cfg.vs_uuid_count = 1; err_code = sd_ble_cfg_set(BLE_COMMON_CFG_VS_UUID, &ble_cfg, ram_start); APP_ERROR_CHECK(err_code); // Configure the maximum number of connections. memset(&ble_cfg, 0, sizeof(ble_cfg)); ble_cfg.gap_cfg.role_count_cfg.periph_role_count = PERIPHERAL_LINK_COUNT; ble_cfg.gap_cfg.role_count_cfg.central_role_count = CENTRAL_LINK_COUNT; ble_cfg.gap_cfg.role_count_cfg.central_sec_count = BLE_GAP_ROLE_COUNT_CENTRAL_SEC_DEFAULT; err_code = sd_ble_cfg_set(BLE_GAP_CFG_ROLE_COUNT, &ble_cfg, ram_start); APP_ERROR_CHECK(err_code); memset(&ble_cfg, 0, sizeof(ble_cfg)); ble_cfg.conn_cfg.params.gap_conn_cfg.conn_count = PERIPHERAL_LINK_COUNT + CENTRAL_LINK_COUNT; ble_cfg.conn_cfg.params.gap_conn_cfg.event_length = BLE_GAP_EVENT_LENGTH_DEFAULT; ble_cfg.conn_cfg.conn_cfg_tag = APP_CONN_CFG_TAG; err_code = sd_ble_cfg_set(BLE_CONN_CFG_GAP, &ble_cfg, ram_start); APP_ERROR_CHECK(err_code); // Configure the maximum ATT MTU. memset(&ble_cfg, 0x00, sizeof(ble_cfg)); ble_cfg.conn_cfg.conn_cfg_tag = APP_CONN_CFG_TAG; ble_cfg.conn_cfg.params.gatt_conn_cfg.att_mtu = NRF_BLE_GATT_MAX_MTU_SIZE; err_code = sd_ble_cfg_set(BLE_CONN_CFG_GATT, &ble_cfg, ram_start); APP_ERROR_CHECK(err_code); ble_cfg.gatts_cfg.attr_tab_size.attr_tab_size = 4000; err_code = sd_ble_cfg_set(BLE_GATTS_CFG_ATTR_TAB_SIZE, &ble_cfg, ram_start); APP_ERROR_CHECK(err_code); // Enable BLE stack. err_code = softdevice_enable(&ram_start); APP_ERROR_CHECK(err_code); // Register with the SoftDevice handler module for BLE events. err_code = softdevice_ble_evt_handler_set(ble_evt_dispatch); APP_ERROR_CHECK(err_code); // Register with the SoftDevice handler module for System events. err_code = softdevice_sys_evt_handler_set(sys_evt_dispatch); APP_ERROR_CHECK(err_code); (void) sd_ble_gap_tx_power_set(8); }