blob: 312d85a9e3496b5e33e483306f9b686b54dc3b80 [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
44static struct 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
80 /* We already have a timer running for this IMSI */
81 if (bts_pch_timer_get_by_imsi(bts, imsi))
Oliver Smithd3c75912021-07-09 16:37:16 +020082 return;
83
Oliver Smithd3c75912021-07-09 16:37:16 +020084 p = talloc_zero(bts, struct bts_pch_timer);
85 llist_add_tail(&p->entry, &bts->pch_timer);
Oliver Smithd3c75912021-07-09 16:37:16 +020086 p->bts = bts;
Pau Espin Pedrol13961c92021-11-08 17:38:47 +010087 OSMO_STRLCPY_ARRAY(p->imsi, imsi);
88 p->ptmsi = (mi_paging->type == GSM_MI_TYPE_TMSI) ? mi_paging->tmsi : GSM_RESERVED_TMSI;
Oliver Smithd3c75912021-07-09 16:37:16 +020089
Pau Espin Pedrol13961c92021-11-08 17:38:47 +010090 tdef = osmo_tdef_get_entry(the_pcu->T_defs, 3113);
Oliver Smithd3c75912021-07-09 16:37:16 +020091 OSMO_ASSERT(tdef);
92 osmo_timer_setup(&p->T3113, T3113_callback, p);
93 osmo_timer_schedule(&p->T3113, tdef->val, 0);
94
Pau Espin Pedrol13961c92021-11-08 17:38:47 +010095 if (log_check_level(DPCU, LOGL_DEBUG)) {
96 char str[64];
97 osmo_mobile_identity_to_str_buf(str, sizeof(str), mi_paging);
98 LOGP(DPCU, LOGL_DEBUG, "PCH paging timer started for MI=%s IMSI=%s\n", str, p->imsi);
99 }
Oliver Smithd3c75912021-07-09 16:37:16 +0200100}
101
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100102void bts_pch_timer_stop(struct gprs_rlcmac_bts *bts, const struct GprsMs *ms)
Oliver Smithd3c75912021-07-09 16:37:16 +0200103{
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100104 struct bts_pch_timer *p = NULL;
105 uint32_t tlli = ms_tlli(ms);
106 const char *imsi = ms_imsi(ms);
Oliver Smithd3c75912021-07-09 16:37:16 +0200107
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100108 /* First try matching by TMSI if available in MS */
109 if (tlli != GSM_RESERVED_TMSI)
110 p = bts_pch_timer_get_by_ptmsi(bts, tlli);
111 /* Otherwise try matching by IMSI if available in MS */
112 if (!p && imsi[0] != '\0')
113 p = bts_pch_timer_get_by_imsi(bts, imsi);
Oliver Smithd3c75912021-07-09 16:37:16 +0200114 if (p)
115 bts_pch_timer_remove(p);
116}
Oliver Smith3f794702021-08-23 14:19:21 +0200117
118void bts_pch_timer_stop_all(struct gprs_rlcmac_bts *bts)
119{
120 struct bts_pch_timer *p, *n;
121
122 llist_for_each_entry_safe(p, n, &bts->pch_timer, entry) {
123 bts_pch_timer_remove(p);
124 }
125}