Andreas Eversberg | cd605f3 | 2023-06-21 15:03:37 +0200 | [diff] [blame] | 1 | /* Group Call Register (GCR) */ |
| 2 | /* |
| 3 | * (C) 2023 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * SPDX-License-Identifier: AGPL-3.0+ |
| 7 | * |
| 8 | * Author: Andreas Eversberg |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU Affero General Public License as published by |
| 12 | * the Free Software Foundation; either version 3 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU Affero General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Affero General Public License |
| 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | */ |
| 23 | |
| 24 | #include <osmocom/msc/gsm_data.h> |
| 25 | #include <osmocom/msc/transaction.h> |
| 26 | |
| 27 | #include <osmocom/msc/asci_gcr.h> |
| 28 | |
| 29 | #define GCR_DEFAULT_TIMEOUT 60 |
| 30 | |
| 31 | static uint32_t pow10[9] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000 }; |
| 32 | |
| 33 | /* Add cell to BSS list. */ |
| 34 | struct gcr_cell *gcr_add_cell(struct gcr_bss *bss, uint16_t cell_id) |
| 35 | { |
| 36 | struct gcr_cell *c; |
| 37 | |
| 38 | c = talloc_zero(bss, struct gcr_cell); |
| 39 | if (!c) |
| 40 | return NULL; |
| 41 | c->cell_id = cell_id; |
| 42 | llist_add_tail(&c->list, &bss->cell_list); |
| 43 | |
| 44 | return c; |
| 45 | } |
| 46 | |
| 47 | /* Find cell entry in BSS list. */ |
| 48 | struct gcr_cell *gcr_find_cell(struct gcr_bss *bss, uint16_t cell_id) |
| 49 | { |
| 50 | struct gcr_cell *c; |
| 51 | |
| 52 | llist_for_each_entry(c, &bss->cell_list, list) { |
| 53 | if (c->cell_id == cell_id) |
| 54 | return c; |
| 55 | } |
| 56 | |
| 57 | return NULL; |
| 58 | } |
| 59 | |
| 60 | /* Remove cell entry from BSS list. */ |
| 61 | void gcr_rm_cell(struct gcr_bss *bss, uint16_t cell_id) |
| 62 | { |
| 63 | struct gcr_cell *c = gcr_find_cell(bss, cell_id); |
| 64 | |
| 65 | if (c) { |
| 66 | llist_del(&c->list); |
| 67 | talloc_free(c); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /* Add BSS to GCR list. */ |
| 72 | struct gcr_bss *gcr_add_bss(struct gcr *gcr, int pc) |
| 73 | { |
| 74 | struct gcr_bss *b; |
| 75 | |
| 76 | b = talloc_zero(gcr, struct gcr_bss); |
| 77 | if (!b) |
| 78 | return NULL; |
| 79 | INIT_LLIST_HEAD(&b->cell_list); |
| 80 | b->pc = pc; |
| 81 | llist_add_tail(&b->list, &gcr->bss_list); |
| 82 | |
| 83 | return b; |
| 84 | } |
| 85 | |
| 86 | /* Find BSS entry in GCR list. */ |
| 87 | struct gcr_bss *gcr_find_bss(struct gcr *gcr, int pc) |
| 88 | { |
| 89 | struct gcr_bss *b; |
| 90 | |
| 91 | llist_for_each_entry(b, &gcr->bss_list, list) { |
| 92 | if (b->pc == pc) |
| 93 | return b; |
| 94 | } |
| 95 | |
| 96 | return NULL; |
| 97 | } |
| 98 | |
| 99 | /* Remove BSS entry from GCR list. */ |
| 100 | void gcr_rm_bss(struct gcr *gcr, int pc) |
| 101 | { |
| 102 | struct gcr_bss *b = gcr_find_bss(gcr, pc); |
| 103 | |
| 104 | if (b) { |
| 105 | /* All cell definitons will be removed, as they are attached to BSS. */ |
| 106 | llist_del(&b->list); |
| 107 | talloc_free(b); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /* Create a new (empty) GCR list. */ |
| 112 | struct gcr *gcr_create(struct gsm_network *gsmnet, enum trans_type trans_type, const char *group_id) |
| 113 | { |
| 114 | struct gcr *gcr; |
| 115 | |
| 116 | gcr = talloc_zero(gsmnet, struct gcr); |
| 117 | if (!gcr) |
| 118 | return NULL; |
| 119 | |
| 120 | INIT_LLIST_HEAD(&gcr->bss_list); |
| 121 | gcr->trans_type = trans_type; |
| 122 | gcr->timeout = GCR_DEFAULT_TIMEOUT; |
| 123 | gcr->mute_talker = true; |
| 124 | osmo_strlcpy(gcr->group_id, group_id, sizeof(gcr->group_id)); |
| 125 | llist_add_tail(&gcr->list, &gsmnet->asci.gcr_lists); |
| 126 | |
| 127 | return gcr; |
| 128 | } |
| 129 | |
| 130 | /* Destroy a GCR list. */ |
| 131 | void gcr_destroy(struct gcr *gcr) |
| 132 | { |
| 133 | /* All BSS definitons will be removed, as they are attached to GCR. */ |
| 134 | llist_del(&gcr->list); |
| 135 | talloc_free(gcr); |
| 136 | } |
| 137 | |
| 138 | /* Find GCR list by group ID. */ |
| 139 | struct gcr *gcr_by_group_id(struct gsm_network *gsmnet, enum trans_type trans_type, const char *group_id) |
| 140 | { |
| 141 | struct gcr *gcr; |
| 142 | |
| 143 | llist_for_each_entry(gcr, &gsmnet->asci.gcr_lists, list) { |
| 144 | if (gcr->trans_type == trans_type && !strcmp(gcr->group_id, group_id)) |
| 145 | return gcr; |
| 146 | } |
| 147 | |
| 148 | return NULL; |
| 149 | } |
| 150 | |
| 151 | /* Find GCR list by callref. */ |
| 152 | struct gcr *gcr_by_callref(struct gsm_network *gsmnet, enum trans_type trans_type, uint32_t callref) |
| 153 | { |
| 154 | struct gcr *most_specific_gcr = NULL, *gcr; |
| 155 | int a, b; |
| 156 | size_t most_specific_len = 0, l; |
| 157 | |
| 158 | llist_for_each_entry(gcr, &gsmnet->asci.gcr_lists, list) { |
| 159 | /* Compare only the digits in Group ID with the digits in callref. |
| 160 | * callref is an integer. Only the remainder, based on Group ID length, is checked. */ |
| 161 | l = strlen(gcr->group_id); |
| 162 | a = atoi(gcr->group_id); |
| 163 | OSMO_ASSERT(l < ARRAY_SIZE(pow10)); |
| 164 | b = callref % pow10[l]; |
| 165 | if (gcr->trans_type == trans_type && a == b) { |
| 166 | /* Get most specific GROUP ID, no matter what order they are stored. */ |
| 167 | if (l > most_specific_len) { |
| 168 | most_specific_gcr = gcr; |
| 169 | most_specific_len = l; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return most_specific_gcr; |
| 175 | } |