aboutsummaryrefslogtreecommitdiffstats
path: root/src/libmsc/asci_gcr.c
blob: 2208151366958e93fc0457d7c56d18f7261a0ff1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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;
}