Bluetooth: Audio: Update codec_cfg_get_chan_allocation

Update the function name from codec_cfg_get_chan_allocation_val
to just codec_cfg_get_chan_allocation, and add
codec_cfg_set_chan_allocation.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
diff --git a/include/zephyr/bluetooth/audio/audio.h b/include/zephyr/bluetooth/audio/audio.h
index c084c88..5bf6fad 100644
--- a/include/zephyr/bluetooth/audio/audio.h
+++ b/include/zephyr/bluetooth/audio/audio.h
@@ -637,8 +637,21 @@
  *  @retval -ENODATA if not found
  *  @retval -EBADMSG if found value has invalid size or value
  */
-int bt_audio_codec_cfg_get_chan_allocation_val(const struct bt_audio_codec_cfg *codec_cfg,
-					       enum bt_audio_location *chan_allocation);
+int bt_audio_codec_cfg_get_chan_allocation(const struct bt_audio_codec_cfg *codec_cfg,
+					   enum bt_audio_location *chan_allocation);
+
+/**
+ * @brief Set the channel allocation of a codec configuration.
+ *
+ * @param codec_cfg       The codec configuration to set data for.
+ * @param chan_allocation The channel allocation to set.
+ *
+ * @retval The data_len of @p codec_cfg on success
+ * @retval -EINVAL if arguments are invalid
+ * @retval -ENOMEM if the new value could not set or added due to memory
+ */
+int bt_audio_codec_cfg_set_chan_allocation(struct bt_audio_codec_cfg *codec_cfg,
+					   enum bt_audio_location chan_allocation);
 
 /** @brief Extract frame size in octets from BT codec config
  *
diff --git a/samples/bluetooth/hap_ha/src/bap_unicast_sr.c b/samples/bluetooth/hap_ha/src/bap_unicast_sr.c
index c185297..5ed64e8 100644
--- a/samples/bluetooth/hap_ha/src/bap_unicast_sr.c
+++ b/samples/bluetooth/hap_ha/src/bap_unicast_sr.c
@@ -89,7 +89,7 @@
 
 		printk("  Frame Duration: %d us\n",
 		       bt_audio_codec_cfg_get_frame_duration_us(codec_cfg));
-		if (bt_audio_codec_cfg_get_chan_allocation_val(codec_cfg, &chan_allocation) == 0) {
+		if (bt_audio_codec_cfg_get_chan_allocation(codec_cfg, &chan_allocation) == 0) {
 			printk("  Channel allocation: 0x%x\n", chan_allocation);
 		}
 
diff --git a/samples/bluetooth/tmap_peripheral/src/bap_unicast_sr.c b/samples/bluetooth/tmap_peripheral/src/bap_unicast_sr.c
index d05406d..ef78551 100644
--- a/samples/bluetooth/tmap_peripheral/src/bap_unicast_sr.c
+++ b/samples/bluetooth/tmap_peripheral/src/bap_unicast_sr.c
@@ -78,7 +78,7 @@
 
 		printk("  Frame Duration: %d us\n",
 		       bt_audio_codec_cfg_get_frame_duration_us(codec_cfg));
-		if (bt_audio_codec_cfg_get_chan_allocation_val(codec_cfg, &chan_allocation) == 0) {
+		if (bt_audio_codec_cfg_get_chan_allocation(codec_cfg, &chan_allocation) == 0) {
 			printk("  Channel allocation: 0x%x\n", chan_allocation);
 		}
 
diff --git a/samples/bluetooth/unicast_audio_server/src/main.c b/samples/bluetooth/unicast_audio_server/src/main.c
index fd94156..7c20ee3 100644
--- a/samples/bluetooth/unicast_audio_server/src/main.c
+++ b/samples/bluetooth/unicast_audio_server/src/main.c
@@ -149,7 +149,7 @@
 
 		printk("  Frame Duration: %d us\n",
 		       bt_audio_codec_cfg_get_frame_duration_us(codec_cfg));
-		if (bt_audio_codec_cfg_get_chan_allocation_val(codec_cfg, &chan_allocation) == 0) {
+		if (bt_audio_codec_cfg_get_chan_allocation(codec_cfg, &chan_allocation) == 0) {
 			printk("  Channel allocation: 0x%x\n", chan_allocation);
 		}
 
diff --git a/subsys/bluetooth/audio/codec.c b/subsys/bluetooth/audio/codec.c
index 59d2394..c3dbfcf 100644
--- a/subsys/bluetooth/audio/codec.c
+++ b/subsys/bluetooth/audio/codec.c
@@ -322,8 +322,8 @@
 	}
 }
 
-int bt_audio_codec_cfg_get_chan_allocation_val(const struct bt_audio_codec_cfg *codec_cfg,
-					       enum bt_audio_location *chan_allocation)
+int bt_audio_codec_cfg_get_chan_allocation(const struct bt_audio_codec_cfg *codec_cfg,
+					   enum bt_audio_location *chan_allocation)
 {
 	const uint8_t *data;
 	uint8_t data_len;
@@ -354,6 +354,23 @@
 	return 0;
 }
 
+int bt_audio_codec_cfg_set_chan_allocation(struct bt_audio_codec_cfg *codec_cfg,
+					   enum bt_audio_location chan_allocation)
+{
+	uint32_t chan_allocation_u32;
+
+	if ((chan_allocation & BT_AUDIO_LOCATION_ANY) != chan_allocation) {
+		LOG_DBG("Invalid chan_allocation value: 0x%08X", chan_allocation);
+		return -EINVAL;
+	}
+
+	chan_allocation_u32 = sys_cpu_to_le32((uint32_t)chan_allocation);
+
+	return bt_audio_codec_cfg_set_val(codec_cfg, BT_AUDIO_CODEC_CONFIG_LC3_CHAN_ALLOC,
+					  (const uint8_t *)&chan_allocation_u32,
+					  sizeof(chan_allocation_u32));
+}
+
 int bt_audio_codec_cfg_get_octets_per_frame(const struct bt_audio_codec_cfg *codec_cfg)
 {
 	const uint8_t *data;
diff --git a/tests/bluetooth/audio/codec/src/main.c b/tests/bluetooth/audio/codec/src/main.c
index 05df015..28dee9b 100644
--- a/tests/bluetooth/audio/codec/src/main.c
+++ b/tests/bluetooth/audio/codec/src/main.c
@@ -84,7 +84,7 @@
 	zassert_equal(ret, 10000u, "unexpected return value %d", ret);
 }
 
-ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_get_chan_allocation_val)
+ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_get_chan_allocation)
 {
 	const struct bt_bap_lc3_preset preset =
 		BT_BAP_LC3_UNICAST_PRESET_8_1_1(BT_AUDIO_LOCATION_FRONT_LEFT,
@@ -92,12 +92,35 @@
 	enum bt_audio_location chan_allocation = BT_AUDIO_LOCATION_FRONT_RIGHT;
 	int err;
 
-	err = bt_audio_codec_cfg_get_chan_allocation_val(&preset.codec_cfg, &chan_allocation);
+	err = bt_audio_codec_cfg_get_chan_allocation(&preset.codec_cfg, &chan_allocation);
 	zassert_false(err, "unexpected error %d", err);
 	zassert_equal(chan_allocation, BT_AUDIO_LOCATION_FRONT_LEFT, "unexpected return value %d",
 		      chan_allocation);
 }
 
+ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_set_chan_allocation)
+{
+	struct bt_bap_lc3_preset preset = BT_BAP_LC3_UNICAST_PRESET_16_2_1(
+		BT_AUDIO_LOCATION_FRONT_LEFT, BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED);
+	enum bt_audio_location chan_allocation;
+	int err;
+
+	err = bt_audio_codec_cfg_get_chan_allocation(&preset.codec_cfg, &chan_allocation);
+	zassert_equal(err, 0, "Unexpected return value %d", err);
+	zassert_equal(chan_allocation, 0x00000001, "Unexpected chan_allocation value %d",
+		      chan_allocation);
+
+	chan_allocation = BT_AUDIO_LOCATION_FRONT_RIGHT | BT_AUDIO_LOCATION_SIDE_RIGHT |
+			  BT_AUDIO_LOCATION_TOP_SIDE_RIGHT | BT_AUDIO_LOCATION_RIGHT_SURROUND;
+	err = bt_audio_codec_cfg_set_chan_allocation(&preset.codec_cfg, chan_allocation);
+	zassert_true(err > 0, "Unexpected return value %d", err);
+
+	err = bt_audio_codec_cfg_get_chan_allocation(&preset.codec_cfg, &chan_allocation);
+	zassert_equal(err, 0, "Unexpected return value %d", err);
+	zassert_equal(chan_allocation, 0x8080802, "Unexpected chan_allocation value %d",
+		      chan_allocation);
+}
+
 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_get_octets_per_frame)
 {
 	const struct bt_bap_lc3_preset preset =
diff --git a/tests/bluetooth/tester/src/btp_bap.c b/tests/bluetooth/tester/src/btp_bap.c
index 5fc38c8..dda0dfc 100644
--- a/tests/bluetooth/tester/src/btp_bap.c
+++ b/tests/bluetooth/tester/src/btp_bap.c
@@ -150,7 +150,7 @@
 
 		LOG_DBG("  Frame Duration: %d us",
 			bt_audio_codec_cfg_get_frame_duration_us(codec_cfg));
-		if (bt_audio_codec_cfg_get_chan_allocation_val(codec_cfg, &chan_allocation) == 0) {
+		if (bt_audio_codec_cfg_get_chan_allocation(codec_cfg, &chan_allocation) == 0) {
 			LOG_DBG("  Channel allocation: 0x%x", chan_allocation);
 		}
 
@@ -269,7 +269,7 @@
 
 	frame_duration_us = bt_audio_codec_cfg_get_frame_duration_us(codec_cfg);
 	chan_allocation_err =
-		bt_audio_codec_cfg_get_chan_allocation_val(codec_cfg, &chan_allocation);
+		bt_audio_codec_cfg_get_chan_allocation(codec_cfg, &chan_allocation);
 	octets_per_frame = bt_audio_codec_cfg_get_octets_per_frame(codec_cfg);
 	frames_per_sdu = bt_audio_codec_cfg_get_frame_blocks_per_sdu(codec_cfg, true);