blob: ca7a6d43b0acc27705c846b3d4f1fdf901069720 [file] [log] [blame]
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001/* Manage a list of struct gsm0808_cell_id */
2/*
3 * (C) 2019 by sysmocom - s.m.f.c. GmbH <info@sysmocom.de>
4 * All Rights Reserved
5 *
6 * Author: Neels Hofmeyr
7 *
8 * SPDX-License-Identifier: GPL-2.0+
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 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 General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
24
25#include <osmocom/msc/cell_id_list.h>
26
27int cell_id_list_add_cell(void *talloc_ctx, struct llist_head *list, const struct gsm0808_cell_id *cid)
28{
29 struct cell_id_list_entry *e = cell_id_list_find(list, cid, 0, true);
30
31 if (e)
32 return 0;
33
34 e = talloc(talloc_ctx, struct cell_id_list_entry);
35 OSMO_ASSERT(e);
36 *e = (struct cell_id_list_entry){
37 .cell_id = *cid,
38 };
39 llist_add_tail(&e->entry, list);
40 return 1;
41}
42
43int cell_id_list_add_list(void *talloc_ctx, struct llist_head *list, const struct gsm0808_cell_id_list2 *cil)
44{
45 struct gsm0808_cell_id one_id;
46 int i;
47 int added = 0;
48 for (i = 0; i < cil->id_list_len; i++) {
49 one_id = (struct gsm0808_cell_id){
50 .id_discr = cil->id_discr,
51 .id = cil->id_list[i],
52 };
53 added += cell_id_list_add_cell(talloc_ctx, list, &one_id);
54 }
55 return added;
56}
57
58void cell_id_list_del_entry(struct cell_id_list_entry *e)
59{
60 llist_del(&e->entry);
61 talloc_free(e);
62}
63
64struct cell_id_list_entry *cell_id_list_find(struct llist_head *list,
65 const struct gsm0808_cell_id *id,
66 unsigned int match_nr,
67 bool exact_match)
68{
69 struct cell_id_list_entry *e;
70 llist_for_each_entry(e, list, entry) {
71 if (gsm0808_cell_ids_match(id, &e->cell_id, exact_match)) {
72 if (match_nr)
73 match_nr--;
74 else
75 return e;
76 }
77 }
78 return NULL;
79}