ASCI: Add simple implementation of Group Call Register

This is a built-in data structure to store and handle voice group calls.

The GCR will be used by VGCS/VBS call control.
 (Chg-Id: I9947403fde8212b66758104443c60aaacc8b1e7b)

The GCR will be used by VTY code.
 (Chg-Id: I5bd034a62fc8b483f550d29103c2f7587198f590)

Change-Id: Ia74a4a865f943c5fb388cd28f9406005c92e663e
Related: OS#4854
diff --git a/include/osmocom/msc/Makefile.am b/include/osmocom/msc/Makefile.am
index 3286c37..3c54dee 100644
--- a/include/osmocom/msc/Makefile.am
+++ b/include/osmocom/msc/Makefile.am
@@ -55,4 +55,5 @@
 	vlr.h \
 	vlr_sgs.h \
 	vty.h \
+	asci_gcr.h \
 	$(NULL)
diff --git a/include/osmocom/msc/asci_gcr.h b/include/osmocom/msc/asci_gcr.h
new file mode 100644
index 0000000..f2160c1
--- /dev/null
+++ b/include/osmocom/msc/asci_gcr.h
@@ -0,0 +1,55 @@
+/* Group Call Register (GCR) */
+/*
+ * (C) 2023 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
+ * All Rights Reserved
+ *
+ * SPDX-License-Identifier: AGPL-3.0+
+ *
+ * Author: Andreas Eversberg
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation; either version 3 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 Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#pragma once
+
+/* Group Call Register */
+struct gcr {
+	struct llist_head list;
+	enum trans_type trans_type;
+	char group_id[9];
+	uint16_t timeout;
+	bool mute_talker;
+	struct llist_head bss_list;
+};
+
+struct gcr_bss {
+	struct llist_head list;
+	int pc;
+	struct llist_head cell_list;
+};
+
+struct gcr_cell {
+	struct llist_head list;
+	uint16_t cell_id;
+};
+
+struct gcr_cell *gcr_add_cell(struct gcr_bss *bss, uint16_t cell_id);
+struct gcr_cell *gcr_find_cell(struct gcr_bss *bss, uint16_t cell_id);
+void gcr_rm_cell(struct gcr_bss *bss, uint16_t cell_id);
+struct gcr_bss *gcr_add_bss(struct gcr *gcr, int pc);
+struct gcr_bss *gcr_find_bss(struct gcr *gcr, int pc);
+void gcr_rm_bss(struct gcr *gcr, int pc);
+struct gcr *gcr_create(struct gsm_network *gsmnet, enum trans_type trans_type, const char *group_id);
+void gcr_destroy(struct gcr *gcr);
+struct gcr *gcr_by_group_id(struct gsm_network *gsmnet, enum trans_type trans_type, const char *group_id);
+struct gcr *gcr_by_callref(struct gsm_network *gsmnet, enum trans_type trans_type, uint32_t callref);
diff --git a/include/osmocom/msc/gsm_data.h b/include/osmocom/msc/gsm_data.h
index 2be4c35..6c0dac9 100644
--- a/include/osmocom/msc/gsm_data.h
+++ b/include/osmocom/msc/gsm_data.h
@@ -269,6 +269,11 @@
 
 	/* SMS queue config parameters */
 	struct sms_queue_config *sms_queue_cfg;
+
+	/* ASCI feature support */
+	struct {
+		struct llist_head gcr_lists;
+	} asci;
 };
 
 struct smpp_esme;
diff --git a/src/libmsc/Makefile.am b/src/libmsc/Makefile.am
index fba7b53..c4e134c 100644
--- a/src/libmsc/Makefile.am
+++ b/src/libmsc/Makefile.am
@@ -76,6 +76,7 @@
 	sgs_iface.c \
 	sgs_server.c \
 	sgs_vty.c \
+	asci_gcr.c \
 	$(NULL)
 
 if BUILD_IU
diff --git a/src/libmsc/asci_gcr.c b/src/libmsc/asci_gcr.c
new file mode 100644
index 0000000..2208151
--- /dev/null
+++ b/src/libmsc/asci_gcr.c
@@ -0,0 +1,175 @@
+/* Group Call Register (GCR) */
+/*
+ * (C) 2023 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
+ * All Rights Reserved
+ *
+ * SPDX-License-Identifier: AGPL-3.0+
+ *
+ * Author: Andreas Eversberg
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation; either version 3 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 Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <osmocom/msc/gsm_data.h>
+#include <osmocom/msc/transaction.h>
+
+#include <osmocom/msc/asci_gcr.h>
+
+#define GCR_DEFAULT_TIMEOUT 60
+
+static uint32_t pow10[9] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000 };
+
+/* Add cell to BSS list. */
+struct gcr_cell *gcr_add_cell(struct gcr_bss *bss, uint16_t cell_id)
+{
+	struct gcr_cell *c;
+
+	c = talloc_zero(bss, struct gcr_cell);
+	if (!c)
+		return NULL;
+	c->cell_id = cell_id;
+	llist_add_tail(&c->list, &bss->cell_list);
+
+	return c;
+}
+
+/* Find cell entry in BSS list. */
+struct gcr_cell *gcr_find_cell(struct gcr_bss *bss, uint16_t cell_id)
+{
+	struct gcr_cell *c;
+
+	llist_for_each_entry(c, &bss->cell_list, list) {
+		if (c->cell_id == cell_id)
+			return c;
+	}
+
+	return NULL;
+}
+
+/* Remove cell entry from BSS list. */
+void gcr_rm_cell(struct gcr_bss *bss, uint16_t cell_id)
+{
+	struct gcr_cell *c = gcr_find_cell(bss, cell_id);
+
+	if (c) {
+		llist_del(&c->list);
+		talloc_free(c);
+	}
+}
+
+/* Add BSS to GCR list. */
+struct gcr_bss *gcr_add_bss(struct gcr *gcr, int pc)
+{
+	struct gcr_bss *b;
+
+	b = talloc_zero(gcr, struct gcr_bss);
+	if (!b)
+		return NULL;
+	INIT_LLIST_HEAD(&b->cell_list);
+	b->pc = pc;
+	llist_add_tail(&b->list, &gcr->bss_list);
+
+	return b;
+}
+
+/* Find BSS entry in GCR list. */
+struct gcr_bss *gcr_find_bss(struct gcr *gcr, int pc)
+{
+	struct gcr_bss *b;
+
+	llist_for_each_entry(b, &gcr->bss_list, list) {
+		if (b->pc == pc)
+			return b;
+	}
+
+	return NULL;
+}
+
+/* Remove BSS entry from GCR list. */
+void gcr_rm_bss(struct gcr *gcr, int pc)
+{
+	struct gcr_bss *b = gcr_find_bss(gcr, pc);
+
+	if (b) {
+		/* All cell definitons will be removed, as they are attached to BSS. */
+		llist_del(&b->list);
+		talloc_free(b);
+	}
+}
+
+/* Create a new (empty) GCR list. */
+struct gcr *gcr_create(struct gsm_network *gsmnet, enum trans_type trans_type, const char *group_id)
+{
+	struct gcr *gcr;
+
+	gcr = talloc_zero(gsmnet, struct gcr);
+	if (!gcr)
+		return NULL;
+
+	INIT_LLIST_HEAD(&gcr->bss_list);
+	gcr->trans_type = trans_type;
+	gcr->timeout = GCR_DEFAULT_TIMEOUT;
+	gcr->mute_talker = true;
+	osmo_strlcpy(gcr->group_id, group_id, sizeof(gcr->group_id));
+	llist_add_tail(&gcr->list, &gsmnet->asci.gcr_lists);
+
+	return gcr;
+}
+
+/* Destroy a GCR list. */
+void gcr_destroy(struct gcr *gcr)
+{
+	/* All BSS definitons will be removed, as they are attached to GCR. */
+	llist_del(&gcr->list);
+	talloc_free(gcr);
+}
+
+/* Find GCR list by group ID. */
+struct gcr *gcr_by_group_id(struct gsm_network *gsmnet, enum trans_type trans_type, const char *group_id)
+{
+	struct gcr *gcr;
+
+	llist_for_each_entry(gcr, &gsmnet->asci.gcr_lists, list) {
+		if (gcr->trans_type == trans_type && !strcmp(gcr->group_id, group_id))
+			return gcr;
+	}
+
+	return NULL;
+}
+
+/* Find GCR list by callref. */
+struct gcr *gcr_by_callref(struct gsm_network *gsmnet, enum trans_type trans_type, uint32_t callref)
+{
+	struct gcr *most_specific_gcr = NULL, *gcr;
+	int a, b;
+	size_t most_specific_len = 0, l;
+
+	llist_for_each_entry(gcr, &gsmnet->asci.gcr_lists, list) {
+		/* Compare only the digits in Group ID with the digits in callref.
+		 * callref is an integer. Only the remainder, based on Group ID length, is checked. */
+		l = strlen(gcr->group_id);
+		a = atoi(gcr->group_id);
+		OSMO_ASSERT(l < ARRAY_SIZE(pow10));
+		b = callref % pow10[l];
+		if (gcr->trans_type == trans_type && a == b) {
+			/* Get most specific GROUP ID, no matter what order they are stored. */
+			if (l > most_specific_len) {
+				most_specific_gcr = gcr;
+				most_specific_len = l;
+			}
+		}
+	}
+
+	return most_specific_gcr;
+}
diff --git a/src/libmsc/msc_net_init.c b/src/libmsc/msc_net_init.c
index 45500b2..f63acf8 100644
--- a/src/libmsc/msc_net_init.c
+++ b/src/libmsc/msc_net_init.c
@@ -75,6 +75,7 @@
 	INIT_LLIST_HEAD(&net->trans_list);
 	INIT_LLIST_HEAD(&net->upqueue);
 	INIT_LLIST_HEAD(&net->neighbor_ident_list);
+	INIT_LLIST_HEAD(&net->asci.gcr_lists);
 
 	/* init statistics */
 	net->msc_ctrs = rate_ctr_group_alloc(net, &msc_ctrg_desc, 0);