blob: d721155154ca226879e0882374d48dd0fbbef31f [file] [log] [blame]
Oliver Smithd3c75912021-07-09 16:37:16 +02001/*
2 * Copyright (C) 2021 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
3 * Author: Oliver Smith
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19#include <string.h>
20
21#include <osmocom/core/logging.h>
22#include <osmocom/core/talloc.h>
23#include <osmocom/core/tdef.h>
24#include <osmocom/core/utils.h>
25
26#include <gprs_debug.h>
27#include <gprs_pcu.h>
28#include <bts_pch_timer.h>
Pau Espin Pedrol13961c92021-11-08 17:38:47 +010029#include <gprs_ms.h>
Oliver Smithd3c75912021-07-09 16:37:16 +020030
Pau Espin Pedrol13961c92021-11-08 17:38:47 +010031static struct bts_pch_timer *bts_pch_timer_get_by_ptmsi(struct gprs_rlcmac_bts *bts, uint32_t ptmsi)
32{
33 struct bts_pch_timer *p;
34 OSMO_ASSERT(ptmsi != GSM_RESERVED_TMSI);
35
36 llist_for_each_entry(p, &bts->pch_timer, entry) {
37 if (p->ptmsi != GSM_RESERVED_TMSI && p->ptmsi == ptmsi)
38 return p;
39 }
40
41 return NULL;
42}
43
Pau Espin Pedrolef8a7302021-11-08 19:19:25 +010044struct bts_pch_timer *bts_pch_timer_get_by_imsi(struct gprs_rlcmac_bts *bts, const char *imsi)
Oliver Smithd3c75912021-07-09 16:37:16 +020045{
46 struct bts_pch_timer *p;
47
48 llist_for_each_entry(p, &bts->pch_timer, entry) {
49 if (strcmp(p->imsi, imsi) == 0)
50 return p;
51 }
52
53 return NULL;
54}
55
56static void bts_pch_timer_remove(struct bts_pch_timer *p)
57{
58 osmo_timer_del(&p->T3113);
59 llist_del(&p->entry);
60
61 LOGP(DPCU, LOGL_DEBUG, "PCH paging timer stopped for IMSI=%s\n", p->imsi);
62 talloc_free(p);
63}
64
65static void T3113_callback(void *data)
66{
67 struct bts_pch_timer *p = data;
68
69 LOGP(DPCU, LOGL_INFO, "PCH paging timeout for IMSI=%s\n", p->imsi);
70 bts_do_rate_ctr_inc(p->bts, CTR_PCH_REQUESTS_TIMEDOUT);
71 bts_pch_timer_remove(p);
72}
73
Pau Espin Pedrol13961c92021-11-08 17:38:47 +010074void bts_pch_timer_start(struct gprs_rlcmac_bts *bts, const struct osmo_mobile_identity *mi_paging,
75 const char *imsi)
Oliver Smithd3c75912021-07-09 16:37:16 +020076{
Pau Espin Pedrol13961c92021-11-08 17:38:47 +010077 struct bts_pch_timer *p;
78 struct osmo_tdef *tdef;
79
Oliver Smithd3c75912021-07-09 16:37:16 +020080 p = talloc_zero(bts, struct bts_pch_timer);
81 llist_add_tail(&p->entry, &bts->pch_timer);
Oliver Smithd3c75912021-07-09 16:37:16 +020082 p->bts = bts;
Pau Espin Pedrol13961c92021-11-08 17:38:47 +010083 OSMO_STRLCPY_ARRAY(p->imsi, imsi);
84 p->ptmsi = (mi_paging->type == GSM_MI_TYPE_TMSI) ? mi_paging->tmsi : GSM_RESERVED_TMSI;
Oliver Smithd3c75912021-07-09 16:37:16 +020085
Pau Espin Pedrol13961c92021-11-08 17:38:47 +010086 tdef = osmo_tdef_get_entry(the_pcu->T_defs, 3113);
Oliver Smithd3c75912021-07-09 16:37:16 +020087 OSMO_ASSERT(tdef);
88 osmo_timer_setup(&p->T3113, T3113_callback, p);
89 osmo_timer_schedule(&p->T3113, tdef->val, 0);
90
Pau Espin Pedrol13961c92021-11-08 17:38:47 +010091 if (log_check_level(DPCU, LOGL_DEBUG)) {
92 char str[64];
93 osmo_mobile_identity_to_str_buf(str, sizeof(str), mi_paging);
94 LOGP(DPCU, LOGL_DEBUG, "PCH paging timer started for MI=%s IMSI=%s\n", str, p->imsi);
95 }
Oliver Smithd3c75912021-07-09 16:37:16 +020096}
97
Pau Espin Pedrol13961c92021-11-08 17:38:47 +010098void bts_pch_timer_stop(struct gprs_rlcmac_bts *bts, const struct GprsMs *ms)
Oliver Smithd3c75912021-07-09 16:37:16 +020099{
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100100 struct bts_pch_timer *p = NULL;
101 uint32_t tlli = ms_tlli(ms);
102 const char *imsi = ms_imsi(ms);
Oliver Smithd3c75912021-07-09 16:37:16 +0200103
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100104 /* First try matching by TMSI if available in MS */
105 if (tlli != GSM_RESERVED_TMSI)
106 p = bts_pch_timer_get_by_ptmsi(bts, tlli);
107 /* Otherwise try matching by IMSI if available in MS */
108 if (!p && imsi[0] != '\0')
109 p = bts_pch_timer_get_by_imsi(bts, imsi);
Oliver Smithd3c75912021-07-09 16:37:16 +0200110 if (p)
111 bts_pch_timer_remove(p);
112}
Oliver Smith3f794702021-08-23 14:19:21 +0200113
114void bts_pch_timer_stop_all(struct gprs_rlcmac_bts *bts)
115{
116 struct bts_pch_timer *p, *n;
117
118 llist_for_each_entry_safe(p, n, &bts->pch_timer, entry) {
119 bts_pch_timer_remove(p);
120 }
121}