tests: Bluetooth: bsim: Multiple central connections to peripherals

Babblesim test of Bluetooth Low Energy Central role
functionality by scanning for other devices and establishing
connection to upto 62 peripherals with a strong enough
signal.

Signed-off-by: Vinayak Kariappa Chettimada <vich@nordicsemi.no>
diff --git a/tests/bluetooth/bsim_bt/bsim_test_multiple/CMakeLists.txt b/tests/bluetooth/bsim_bt/bsim_test_multiple/CMakeLists.txt
new file mode 100644
index 0000000..85f3344
--- /dev/null
+++ b/tests/bluetooth/bsim_bt/bsim_test_multiple/CMakeLists.txt
@@ -0,0 +1,24 @@
+# SPDX-License-Identifier: Apache-2.0
+
+cmake_minimum_required(VERSION 3.13.1)
+
+if (NOT DEFINED ENV{BSIM_COMPONENTS_PATH})
+  message(FATAL_ERROR "This test requires the BabbleSim simulator. Please set  \
+          the  environment variable BSIM_COMPONENTS_PATH to point to its       \
+          components folder. More information can be found in                  \
+          https://babblesim.github.io/folder_structure_and_env.html")
+endif()
+
+find_package(Zephyr HINTS $ENV{ZEPHYR_BASE})
+project(bsim_test_multiple)
+
+target_sources(app PRIVATE
+  src/main.c
+  ${ZEPHYR_BASE}/samples/bluetooth/central_multilink/src/central_multilink.c
+  ${ZEPHYR_BASE}/samples/bluetooth/peripheral_identity/src/peripheral_identity.c
+)
+
+zephyr_include_directories(
+  $ENV{BSIM_COMPONENTS_PATH}/libUtilv1/src/
+  $ENV{BSIM_COMPONENTS_PATH}/libPhyComv1/src/
+)
diff --git a/tests/bluetooth/bsim_bt/bsim_test_multiple/prj.conf b/tests/bluetooth/bsim_bt/bsim_test_multiple/prj.conf
new file mode 100644
index 0000000..2baba54
--- /dev/null
+++ b/tests/bluetooth/bsim_bt/bsim_test_multiple/prj.conf
@@ -0,0 +1,16 @@
+CONFIG_BT=y
+CONFIG_BT_PERIPHERAL=y
+CONFIG_BT_CENTRAL=y
+CONFIG_BT_AUTO_PHY_UPDATE=n
+CONFIG_BT_PRIVACY=y
+
+CONFIG_BT_DEVICE_NAME="Multiple"
+CONFIG_BT_GAP_AUTO_UPDATE_CONN_PARAMS=n
+
+CONFIG_BT_MAX_CONN=62
+CONFIG_BT_ID_MAX=62
+
+# CONFIG_BT_GATT_CLIENT=y
+
+# CONFIG_BT_SMP=y
+# CONFIG_BT_MAX_PAIRED=62
diff --git a/tests/bluetooth/bsim_bt/bsim_test_multiple/src/main.c b/tests/bluetooth/bsim_bt/bsim_test_multiple/src/main.c
new file mode 100644
index 0000000..20b42b4
--- /dev/null
+++ b/tests/bluetooth/bsim_bt/bsim_test_multiple/src/main.c
@@ -0,0 +1,116 @@
+/* main.c - Application main entry point */
+
+/*
+ * Copyright (c) 2015-2016 Intel Corporation
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include <stddef.h>
+
+#include <sys/printk.h>
+#include <sys/util.h>
+
+#include "bs_types.h"
+#include "bs_tracing.h"
+#include "time_machine.h"
+#include "bstests.h"
+
+int init_central(void);
+int init_peripheral(void);
+
+#define FAIL(...)					\
+	do {						\
+		bst_result = Failed;			\
+		bs_trace_error_time_line(__VA_ARGS__);	\
+	} while (0)
+
+#define PASS(...)					\
+	do {						\
+		bst_result = Passed;			\
+		bs_trace_info_time(1, __VA_ARGS__);	\
+	} while (0)
+
+extern enum bst_result_t bst_result;
+
+static void test_central_main(void)
+{
+	int err;
+
+	err = init_central();
+	if (err) {
+		goto exit;
+	}
+
+	PASS("Central tests passed\n");
+	bs_trace_silent_exit(0);
+
+	return;
+
+exit:
+	FAIL("Central tests failed (%d)\n", err);
+	bs_trace_silent_exit(0);
+}
+
+static void test_peripheral_main(void)
+{
+	int err;
+
+	err = init_peripheral();
+	if (err) {
+		goto exit;
+	}
+
+	PASS("Peripheral tests passed\n");
+
+	return;
+
+exit:
+	FAIL("Peripheral tests failed (%d)\n", err);
+	bs_trace_silent_exit(0);
+}
+
+static void test_multiple_init(void)
+{
+	bst_ticker_set_next_tick_absolute(20e6);
+	bst_result = In_progress;
+}
+
+static void test_multiple_tick(bs_time_t HW_device_time)
+{
+	bst_result = Failed;
+	bs_trace_error_line("Test multiple finished.\n");
+}
+
+static const struct bst_test_instance test_def[] = {
+	{
+		.test_id = "central",
+		.test_descr = "Central Multilink",
+		.test_post_init_f = test_multiple_init,
+		.test_tick_f = test_multiple_tick,
+		.test_main_f = test_central_main
+	},
+	{
+		.test_id = "peripheral",
+		.test_descr = "Peripheral multiple identity",
+		.test_post_init_f = test_multiple_init,
+		.test_tick_f = test_multiple_tick,
+		.test_main_f = test_peripheral_main
+	},
+	BSTEST_END_MARKER
+};
+
+struct bst_test_list *test_multiple_install(struct bst_test_list *tests)
+{
+	return bst_add_tests(tests, test_def);
+}
+
+bst_test_install_t test_installers[] = {
+	test_multiple_install,
+	NULL
+};
+
+void main(void)
+{
+	bst_main();
+}
diff --git a/tests/bluetooth/bsim_bt/bsim_test_multiple/tests_scripts/multiple.sh b/tests/bluetooth/bsim_bt/bsim_test_multiple/tests_scripts/multiple.sh
new file mode 100755
index 0000000..0cea1fe
--- /dev/null
+++ b/tests/bluetooth/bsim_bt/bsim_test_multiple/tests_scripts/multiple.sh
@@ -0,0 +1,38 @@
+#!/usr/bin/env bash
+# Copyright 2018 Oticon A/S
+# SPDX-License-Identifier: Apache-2.0
+
+# Multiple connection between two devices with multiple peripheral identity
+simulation_id="multiple"
+verbosity_level=2
+process_ids=""; exit_code=0
+
+function Execute(){
+  if [ ! -f $1 ]; then
+    echo -e "  \e[91m`pwd`/`basename $1` cannot be found (did you forget to\
+ compile it?)\e[39m"
+    exit 1
+  fi
+  timeout 30 $@ & process_ids="$process_ids $!"
+}
+
+: "${BSIM_OUT_PATH:?BSIM_OUT_PATH must be defined}"
+
+#Give a default value to BOARD if it does not have one yet:
+BOARD="${BOARD:-nrf52_bsim}"
+
+cd ${BSIM_OUT_PATH}/bin
+
+Execute ./bs_${BOARD}_tests_bluetooth_bsim_bt_bsim_test_multiple_prj_conf \
+  -v=${verbosity_level} -s=${simulation_id} -d=0 -testid=central
+
+Execute ./bs_${BOARD}_tests_bluetooth_bsim_bt_bsim_test_multiple_prj_conf\
+  -v=${verbosity_level} -s=${simulation_id} -d=1 -testid=peripheral
+
+Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \
+  -D=2 -sim_length=60e6 $@
+
+for process_id in $process_ids; do
+  wait $process_id || let "exit_code=$?"
+done
+exit $exit_code #the last exit code != 0
diff --git a/tests/bluetooth/bsim_bt/compile.sh b/tests/bluetooth/bsim_bt/compile.sh
index 5a5a11f..b559e3b 100755
--- a/tests/bluetooth/bsim_bt/compile.sh
+++ b/tests/bluetooth/bsim_bt/compile.sh
@@ -57,6 +57,7 @@
   compile
 app=tests/bluetooth/bsim_bt/bsim_test_app conf_file=prj_split_low_lat.conf \
   compile
+app=tests/bluetooth/bsim_bt/bsim_test_multiple compile
 app=tests/bluetooth/bsim_bt/bsim_test_advx compile
 app=tests/bluetooth/bsim_bt/bsim_test_iso compile
 app=tests/bluetooth/bsim_bt/bsim_test_audio compile