tests: Bluetooth: Add coverage for Coded PHY sync establishment

Adds test coverage for the case where the host starts the
scanner automatically upon sync creation.
This test covers there bug where we previously did not start
scanning on Coded PHY.

Signed-off-by: Rubin Gerritsen <rubin.gerritsen@nordicsemi.no>
diff --git a/tests/bsim/bluetooth/host/adv/compile.sh b/tests/bsim/bluetooth/host/adv/compile.sh
index 1729acb..c17c139 100755
--- a/tests/bsim/bluetooth/host/adv/compile.sh
+++ b/tests/bsim/bluetooth/host/adv/compile.sh
@@ -20,6 +20,7 @@
 app=tests/bsim/bluetooth/host/adv/extended conf_file=prj_scanner.conf compile
 app=tests/bsim/bluetooth/host/adv/periodic compile
 app=tests/bsim/bluetooth/host/adv/periodic conf_file=prj_long_data.conf compile
+app=tests/bsim/bluetooth/host/adv/periodic conf_file=prj_coded.conf compile
 app=tests/bsim/bluetooth/host/adv/encrypted/css_sample_data compile
 app=tests/bsim/bluetooth/host/adv/encrypted/ead_sample compile
 app=tests/bsim/bluetooth/host/adv/long_ad compile
diff --git a/tests/bsim/bluetooth/host/adv/periodic/prj_coded.conf b/tests/bsim/bluetooth/host/adv/periodic/prj_coded.conf
new file mode 100644
index 0000000..50c4c33
--- /dev/null
+++ b/tests/bsim/bluetooth/host/adv/periodic/prj_coded.conf
@@ -0,0 +1,15 @@
+CONFIG_BT=y
+CONFIG_BT_DEVICE_NAME="test_per_adv"
+CONFIG_BT_PERIPHERAL=y
+CONFIG_BT_CENTRAL=y
+CONFIG_BT_SMP=y
+CONFIG_BT_PRIVACY=y
+
+CONFIG_BT_EXT_ADV=y
+CONFIG_BT_EXT_ADV_MAX_ADV_SET=2
+CONFIG_BT_CTLR_ADVANCED_FEATURES=y
+CONFIG_BT_CTLR_ADV_AUX_SET=2
+CONFIG_BT_CTLR_ADV_DATA_BUF_MAX=2
+CONFIG_BT_CTLR_PHY_CODED=y
+CONFIG_BT_PER_ADV=y
+CONFIG_BT_PER_ADV_SYNC=y
diff --git a/tests/bsim/bluetooth/host/adv/periodic/src/per_adv_advertiser.c b/tests/bsim/bluetooth/host/adv/periodic/src/per_adv_advertiser.c
index 560b201..d6b7df6 100644
--- a/tests/bsim/bluetooth/host/adv/periodic/src/per_adv_advertiser.c
+++ b/tests/bsim/bluetooth/host/adv/periodic/src/per_adv_advertiser.c
@@ -106,6 +106,30 @@
 	printk("done.\n");
 }
 
+#if defined(CONFIG_BT_CTLR_PHY_CODED)
+static void create_per_adv_set_coded(struct bt_le_ext_adv **adv)
+{
+	int err;
+
+	printk("Creating coded PHY extended advertising set...");
+	err = bt_le_ext_adv_create(BT_LE_EXT_ADV_CODED_NCONN, NULL, adv);
+	if (err) {
+		printk("Failed to create advertising set: %d\n", err);
+		return;
+	}
+	printk("done.\n");
+
+	printk("Setting periodic advertising parameters...");
+	err = bt_le_per_adv_set_param(*adv, BT_LE_PER_ADV_DEFAULT);
+	if (err) {
+		printk("Failed to set periodic advertising parameters: %d\n",
+		       err);
+		return;
+	}
+	printk("done.\n");
+}
+#endif /* CONFIG_BT_CTLR_PHY_CODED */
+
 static void create_conn_adv_set(struct bt_le_ext_adv **adv)
 {
 	int err;
@@ -228,6 +252,31 @@
 	PASS("Periodic advertiser passed\n");
 }
 
+#if defined(CONFIG_BT_CTLR_PHY_CODED)
+static void main_per_adv_advertiser_coded(void)
+{
+	struct bt_le_ext_adv *per_adv;
+
+	common_init();
+
+	create_per_adv_set_coded(&per_adv);
+
+	start_per_adv_set(per_adv);
+	start_ext_adv_set(per_adv);
+
+	/* Advertise for a bit */
+	k_sleep(K_SECONDS(10));
+
+	stop_per_adv_set(per_adv);
+	stop_ext_adv_set(per_adv);
+
+	delete_adv_set(per_adv);
+	per_adv = NULL;
+
+	PASS("Periodic advertiser coded PHY passed\n");
+}
+#endif /* CONFIG_BT_CTLR_PHY_CODED */
+
 static void main_per_adv_conn_advertiser(void)
 {
 	struct bt_le_ext_adv *conn_adv;
@@ -329,6 +378,16 @@
 		.test_tick_f = test_tick,
 		.test_main_f = main_per_adv_advertiser
 	},
+#if defined(CONFIG_BT_CTLR_PHY_CODED)
+	{
+		.test_id = "per_adv_advertiser_coded_phy",
+		.test_descr = "Basic periodic advertising test on Coded PHY. "
+			      "Advertiser and periodic advertiser uses Coded PHY",
+		.test_post_init_f = test_init,
+		.test_tick_f = test_tick,
+		.test_main_f = main_per_adv_advertiser_coded
+	},
+#endif /* CONFIG_BT_CTLR_PHY_CODED */
 	{
 		.test_id = "per_adv_conn_advertiser",
 		.test_descr = "Periodic advertising test with concurrent ACL "
diff --git a/tests/bsim/bluetooth/host/adv/periodic/src/per_adv_syncer.c b/tests/bsim/bluetooth/host/adv/periodic/src/per_adv_syncer.c
index df126e3..9c0ad74 100644
--- a/tests/bsim/bluetooth/host/adv/periodic/src/per_adv_syncer.c
+++ b/tests/bsim/bluetooth/host/adv/periodic/src/per_adv_syncer.c
@@ -190,7 +190,10 @@
 	int err;
 
 	printk("Start scanning...");
-	err = bt_le_scan_start(BT_LE_SCAN_ACTIVE, NULL);
+
+	err = bt_le_scan_start(IS_ENABLED(CONFIG_BT_CTLR_PHY_CODED) ?
+			       BT_LE_SCAN_CODED_ACTIVE : BT_LE_SCAN_ACTIVE,
+			       NULL);
 	if (err) {
 		FAIL("Failed to start scan: %d\n", err);
 		return;
diff --git a/tests/bsim/bluetooth/host/adv/periodic/tests_scripts/per_adv_app_not_scanning_coded.sh b/tests/bsim/bluetooth/host/adv/periodic/tests_scripts/per_adv_app_not_scanning_coded.sh
new file mode 100755
index 0000000..005bd6b
--- /dev/null
+++ b/tests/bsim/bluetooth/host/adv/periodic/tests_scripts/per_adv_app_not_scanning_coded.sh
@@ -0,0 +1,27 @@
+#!/usr/bin/env bash
+# Copyright (c) 2024 Nordic Semiconductor ASA
+# SPDX-License-Identifier: Apache-2.0
+
+# Periodic advertising sync test where the host starts scanning
+# automatically because the application didn't start it.
+# The advertiser is using Coded PHY as primary PHY.
+
+source ${ZEPHYR_BASE}/tests/bsim/sh_common.source
+
+simulation_id="per_adv_app_not_scanning_coded"
+verbosity_level=2
+
+cd ${BSIM_OUT_PATH}/bin
+
+Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_host_adv_periodic_prj_coded_conf \
+  -v=${verbosity_level} -s=${simulation_id} -d=0 -RealEncryption=0 \
+  -testid=per_adv_advertiser_coded_phy -rs=23
+
+Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_host_adv_periodic_prj_coded_conf \
+  -v=${verbosity_level} -s=${simulation_id} -d=1 -RealEncryption=0 \
+  -testid=per_adv_syncer_app_not_scanning -rs=6
+
+Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \
+  -D=2 -sim_length=20e6 $@
+
+wait_for_background_jobs