blob: bfbb1e93fb8435feaf2931e187300f056f339a3a [file] [log] [blame]
Neels Hofmeyr72992152020-09-19 02:36:08 +02001/* GSM subscriber details for use in SMLC */
2/*
3 * (C) 2020 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
4 *
5 * Author: Neels Hofmeyr <neels@hofmeyr.de>
6 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#include <osmocom/smlc/debug.h>
25#include <osmocom/smlc/smlc_data.h>
26#include <osmocom/smlc/smlc_subscr.h>
27
28static void smlc_subscr_free(struct smlc_subscr *smlc_subscr)
29{
30 llist_del(&smlc_subscr->entry);
31 talloc_free(smlc_subscr);
32}
33
34static int smlc_subscr_use_cb(struct osmo_use_count_entry *e, int32_t old_use_count, const char *file, int line)
35{
36 struct smlc_subscr *smlc_subscr = e->use_count->talloc_object;
37 int32_t total;
38 int level;
39
40 if (!e->use)
41 return -EINVAL;
42
43 total = osmo_use_count_total(&smlc_subscr->use_count);
44
45 if (total == 0
46 || (total == 1 && old_use_count == 0 && e->count == 1))
47 level = LOGL_INFO;
48 else
49 level = LOGL_DEBUG;
50
51 LOGPSRC(DREF, level, file, line, "%s: %s %s\n",
52 smlc_subscr_to_str_c(OTC_SELECT, smlc_subscr),
53 (e->count - old_use_count) > 0? "+" : "-", e->use);
54
55 if (e->count < 0)
56 return -ERANGE;
57
58 if (total == 0)
59 smlc_subscr_free(smlc_subscr);
60 return 0;
61}
62
63static struct smlc_subscr *smlc_subscr_alloc()
64{
65 struct smlc_subscr *smlc_subscr;
66
67 smlc_subscr = talloc_zero(g_smlc, struct smlc_subscr);
68 if (!smlc_subscr)
69 return NULL;
70
71 smlc_subscr->use_count = (struct osmo_use_count){
72 .talloc_object = smlc_subscr,
73 .use_cb = smlc_subscr_use_cb,
74 };
75
76 llist_add_tail(&smlc_subscr->entry, &g_smlc->subscribers);
77
78 return smlc_subscr;
79}
80
81struct smlc_subscr *smlc_subscr_find(const struct osmo_mobile_identity *imsi, const char *use_token)
82{
83 struct smlc_subscr *smlc_subscr;
84 if (!imsi)
85 return NULL;
86
87 llist_for_each_entry(smlc_subscr, &g_smlc->subscribers, entry) {
88 if (!osmo_mobile_identity_cmp(&smlc_subscr->imsi, imsi)) {
89 smlc_subscr_get(smlc_subscr, use_token);
90 return smlc_subscr;
91 }
92 }
93 return NULL;
94}
95
96struct smlc_subscr *smlc_subscr_find_or_create(const struct osmo_mobile_identity *imsi, const char *use_token)
97{
98 struct smlc_subscr *smlc_subscr;
99 if (!imsi)
100 return NULL;
101 smlc_subscr = smlc_subscr_find(imsi, use_token);
102 if (smlc_subscr)
103 return smlc_subscr;
104 smlc_subscr = smlc_subscr_alloc();
105 if (!smlc_subscr)
106 return NULL;
107 smlc_subscr->imsi = *imsi;
108 smlc_subscr_get(smlc_subscr, use_token);
109 return smlc_subscr;
110}
111
112int smlc_subscr_to_str_buf(char *buf, size_t buf_len, const struct smlc_subscr *smlc_subscr)
113{
114 struct osmo_strbuf sb = { .buf = buf, .len = buf_len };
115 OSMO_STRBUF_APPEND(sb, osmo_mobile_identity_to_str_buf, &smlc_subscr->imsi);
116 OSMO_STRBUF_PRINTF(sb, "[");
117 OSMO_STRBUF_APPEND(sb, osmo_use_count_to_str_buf, &smlc_subscr->use_count);
118 OSMO_STRBUF_PRINTF(sb, "]");
119 return sb.chars_needed;
120}
121
122char *smlc_subscr_to_str_c(void *ctx, const struct smlc_subscr *smlc_subscr)
123{
124 OSMO_NAME_C_IMPL(ctx, 64, "ERROR", smlc_subscr_to_str_buf, smlc_subscr)
125}