Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1 | /* 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. |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include <osmocom/msc/cell_id_list.h> |
| 22 | |
| 23 | int cell_id_list_add_cell(void *talloc_ctx, struct llist_head *list, const struct gsm0808_cell_id *cid) |
| 24 | { |
| 25 | struct cell_id_list_entry *e = cell_id_list_find(list, cid, 0, true); |
| 26 | |
| 27 | if (e) |
| 28 | return 0; |
| 29 | |
| 30 | e = talloc(talloc_ctx, struct cell_id_list_entry); |
| 31 | OSMO_ASSERT(e); |
| 32 | *e = (struct cell_id_list_entry){ |
| 33 | .cell_id = *cid, |
| 34 | }; |
| 35 | llist_add_tail(&e->entry, list); |
| 36 | return 1; |
| 37 | } |
| 38 | |
| 39 | int cell_id_list_add_list(void *talloc_ctx, struct llist_head *list, const struct gsm0808_cell_id_list2 *cil) |
| 40 | { |
| 41 | struct gsm0808_cell_id one_id; |
| 42 | int i; |
| 43 | int added = 0; |
| 44 | for (i = 0; i < cil->id_list_len; i++) { |
| 45 | one_id = (struct gsm0808_cell_id){ |
| 46 | .id_discr = cil->id_discr, |
| 47 | .id = cil->id_list[i], |
| 48 | }; |
| 49 | added += cell_id_list_add_cell(talloc_ctx, list, &one_id); |
| 50 | } |
| 51 | return added; |
| 52 | } |
| 53 | |
| 54 | void cell_id_list_del_entry(struct cell_id_list_entry *e) |
| 55 | { |
| 56 | llist_del(&e->entry); |
| 57 | talloc_free(e); |
| 58 | } |
| 59 | |
| 60 | struct cell_id_list_entry *cell_id_list_find(struct llist_head *list, |
| 61 | const struct gsm0808_cell_id *id, |
| 62 | unsigned int match_nr, |
| 63 | bool exact_match) |
| 64 | { |
| 65 | struct cell_id_list_entry *e; |
| 66 | llist_for_each_entry(e, list, entry) { |
| 67 | if (gsm0808_cell_ids_match(id, &e->cell_id, exact_match)) { |
| 68 | if (match_nr) |
| 69 | match_nr--; |
| 70 | else |
| 71 | return e; |
| 72 | } |
| 73 | } |
| 74 | return NULL; |
| 75 | } |