Add initial CSD support with external MNCC

Implement and use CSD bearer service logic (with similar audio codec code):
* csd_filter (codec_filter)
* csd_bs (sdp_audio_codec)
* csd_bs_list (sdp_audio_codecs)

Related: OS#4394
Change-Id: Ide8b8321e0401dcbe35da2ec9cee0abca821d99a
diff --git a/include/osmocom/msc/Makefile.am b/include/osmocom/msc/Makefile.am
index 948021a..3286c37 100644
--- a/include/osmocom/msc/Makefile.am
+++ b/include/osmocom/msc/Makefile.am
@@ -3,6 +3,8 @@
 	cell_id_list.h \
 	codec_filter.h \
 	codec_mapping.h \
+	csd_bs.h \
+	csd_filter.h \
 	db.h \
 	debug.h \
 	e_link.h \
diff --git a/include/osmocom/msc/csd_bs.h b/include/osmocom/msc/csd_bs.h
new file mode 100644
index 0000000..eee8692
--- /dev/null
+++ b/include/osmocom/msc/csd_bs.h
@@ -0,0 +1,54 @@
+/* 3GPP TS 122.002 Bearer Services */
+#pragma once
+
+#include <osmocom/gsm/mncc.h>
+#include <osmocom/gsm/protocol/gsm_08_08.h>
+
+enum csd_bs {
+	CSD_BS_NONE,
+
+	/* 3.1.1.1.2 */
+	CSD_BS_21_T_V110_0k3,
+	CSD_BS_22_T_V110_1k2,
+	CSD_BS_24_T_V110_2k4,
+	CSD_BS_25_T_V110_4k8,
+	CSD_BS_26_T_V110_9k6,
+
+	/* 3.1.1.2.2 */
+	CSD_BS_21_NT_V110_0k3,
+	CSD_BS_22_NT_V110_1k2,
+	CSD_BS_24_NT_V110_2k4,
+	CSD_BS_25_NT_V110_4k8,
+	CSD_BS_26_NT_V110_9k6,
+
+	/* 3.1.2.1.2 */
+	CSD_BS_31_T_V110_1k2,
+	CSD_BS_32_T_V110_2k4,
+	CSD_BS_33_T_V110_4k8,
+	CSD_BS_34_T_V110_9k6,
+
+	CSD_BS_MAX,
+};
+
+struct csd_bs_list {
+	unsigned int count;
+	enum csd_bs bs[CSD_BS_MAX];
+};
+
+void csd_bs_list_add_bs(struct csd_bs_list *list, enum csd_bs bs);
+int csd_bs_list_to_bearer_cap(struct gsm_mncc_bearer_cap *cap, const struct csd_bs_list *list);
+void csd_bs_list_from_bearer_cap(struct csd_bs_list *list, const struct gsm_mncc_bearer_cap *cap);
+
+int csd_bs_to_str_buf(char *buf, size_t buflen, enum csd_bs bs);
+char *csd_bs_to_str_c(void *ctx, enum csd_bs bs);
+const char *csd_bs_to_str(enum csd_bs bs);
+
+int csd_bs_list_to_str_buf(char *buf, size_t buflen, const struct csd_bs_list *list);
+char *csd_bs_list_to_str_c(void *ctx, const struct csd_bs_list *list);
+const char *csd_bs_list_to_str(const struct csd_bs_list *list);
+
+void csd_bs_list_add_bs(struct csd_bs_list *list, enum csd_bs bs);
+void csd_bs_list_remove(struct csd_bs_list *list, enum csd_bs bs);
+void csd_bs_list_intersection(struct csd_bs_list *dest, const struct csd_bs_list *other);
+
+int csd_bs_list_to_gsm0808_channel_type(struct gsm0808_channel_type *ct, const struct csd_bs_list *list);
diff --git a/include/osmocom/msc/csd_filter.h b/include/osmocom/msc/csd_filter.h
new file mode 100644
index 0000000..51ffff7
--- /dev/null
+++ b/include/osmocom/msc/csd_filter.h
@@ -0,0 +1,53 @@
+/* Filter/overlay data rates for CSD, across MS, RAN and CN limitations */
+/*
+ * (C) 2023 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
+ * All Rights Reserved
+ *
+ * Author: Oliver Smith
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+#pragma once
+
+#include <osmocom/gsm/gsm_utils.h>
+#include <osmocom/gsm/mncc.h>
+#include <osmocom/mgcp_client/mgcp_client.h>
+
+#include <osmocom/msc/csd_bs.h>
+#include <osmocom/msc/sdp_msg.h>
+
+/* Combine various data rate selections to obtain a resulting set allowed by
+ * all of them. Members reflect the different entities/stages that select data
+ * rates in CSD. Call csd_filter_run() and obtain the resulting set in
+ * csd_filter.result. */
+struct csd_filter {
+	/* The fixed set available on the RAN type, per definition. */
+	struct csd_bs_list ran;
+	/* The services advertised by the MS Bearer Capabilities */
+	struct csd_bs_list ms;
+	/* If known, the set the current RAN cell allows / has available. This
+	 * may not be available if the BSC does not issue this information
+	 * early enough. Should be ignored if empty. */
+	struct csd_bs_list bss;
+
+	/* After a channel was assigned, this reflects the chosen BS. */
+	enum csd_bs assignment;
+};
+
+void csd_filter_set_ran(struct csd_filter *filter, enum osmo_rat_type ran_type);
+int csd_filter_run(struct csd_filter *filter, struct sdp_msg *result, const struct sdp_msg *remote);
+
+int csd_filter_to_str_buf(char *buf, size_t buflen, const struct csd_filter *filter,
+			    const struct sdp_msg *result, const struct sdp_msg *remote);
+char *csd_filter_to_str_c(void *ctx, const struct csd_filter *filter, const struct sdp_msg *result, const struct sdp_msg *remote);
+const char *csd_filter_to_str(const struct csd_filter *filter, const struct sdp_msg *result, const struct sdp_msg *remote);
diff --git a/include/osmocom/msc/sdp_msg.h b/include/osmocom/msc/sdp_msg.h
index 1b905b7..e62f22b 100644
--- a/include/osmocom/msc/sdp_msg.h
+++ b/include/osmocom/msc/sdp_msg.h
@@ -4,6 +4,8 @@
 #include <osmocom/core/utils.h>
 #include <osmocom/core/sockaddr_str.h>
 
+#include <osmocom/msc/csd_bs.h>
+
 extern const struct value_string sdp_msg_payload_type_names[];
 static inline const char *sdp_msg_payload_type_name(unsigned int payload_type)
 { return get_value_string(sdp_msg_payload_type_names, payload_type); }
@@ -36,6 +38,7 @@
 	unsigned int ptime;
 	enum sdp_mode_e mode;
 	struct sdp_audio_codecs audio_codecs;
+	struct csd_bs_list bearer_services;
 };
 
 #define foreach_sdp_audio_codec(/* struct sdp_audio_codec* */ CODEC, \
@@ -80,3 +83,5 @@
 int sdp_msg_to_str_buf(char *buf, size_t buflen, const struct sdp_msg *sdp);
 char *sdp_msg_to_str_c(void *ctx, const struct sdp_msg *sdp);
 const char *sdp_msg_to_str(const struct sdp_msg *sdp);
+
+void sdp_audio_codecs_set_csd(struct sdp_audio_codecs *ac);
diff --git a/include/osmocom/msc/transaction.h b/include/osmocom/msc/transaction.h
index 36aab78..d2a463d 100644
--- a/include/osmocom/msc/transaction.h
+++ b/include/osmocom/msc/transaction.h
@@ -9,6 +9,7 @@
 #include <osmocom/msc/msc_a.h>
 #include <osmocom/msc/debug.h>
 #include <osmocom/msc/codec_filter.h>
+#include <osmocom/msc/csd_filter.h>
 #include <osmocom/gsm/gsm0411_smc.h>
 #include <osmocom/gsm/gsm0411_smr.h>
 
@@ -107,8 +108,9 @@
 			struct osmo_lcls *lcls;
 			/* SDP as last received from the remote call leg. */
 			struct sdp_msg remote;
-			/* Track codec choices from BSS and remote call leg */
+			/* Track codec/CSD choices from BSS and remote call leg */
 			struct codec_filter codecs;
+			struct csd_filter csd;
 			/* Resulting choice from codecs/bearer services and the
 			 * local RTP address to be sent to the remote call leg. */
 			struct sdp_msg local;