blob: 5799d906f02d18e5d311a081f8aa232cbbff499f [file] [log] [blame]
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +02001/* tbf_fsm.c
2 *
3 * Copyright (C) 2021 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
4 * Author: Pau Espin Pedrol <pespin@sysmocom.de>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#include <unistd.h>
22
23#include <talloc.h>
24
25#include <tbf_fsm.h>
26#include <gprs_rlcmac.h>
27#include <gprs_debug.h>
28#include <gprs_ms.h>
29#include <encoding.h>
30#include <bts.h>
31
32#define X(s) (1 << (s))
33
34const struct osmo_tdef_state_timeout tbf_fsm_timeouts[32] = {
35 [TBF_ST_NULL] = {},
36 [TBF_ST_ASSIGN] = { },
37 [TBF_ST_FLOW] = { },
38 [TBF_ST_FINISHED] = {},
39 [TBF_ST_WAIT_RELEASE] = {},
40 [TBF_ST_RELEASING] = {},
41};
42
43const struct value_string tbf_fsm_event_names[] = {
Pau Espin Pedrol33e80072021-07-22 19:20:50 +020044 { TBF_EV_ASSIGN_ADD_CCCH, "ASSIGN_ADD_CCCH" },
45 { TBF_EV_ASSIGN_ADD_PACCH, "ASSIGN_ADD_PACCH" },
46 { TBF_EV_ASSIGN_DEL_CCCH, "ASSIGN_DEL_CCCH" },
Pau Espin Pedrol720e19e2021-07-22 19:56:37 +020047 { TBF_EV_ASSIGN_ACK_PACCH, "ASSIGN_ACK_PACCH" },
48 { TBF_EV_ASSIGN_READY_CCCH, "ASSIGN_READY_CCCH" },
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +020049 { 0, NULL }
50};
51
Pau Espin Pedrol33e80072021-07-22 19:20:50 +020052static void mod_ass_type(struct tbf_fsm_ctx *ctx, uint8_t t, bool set)
53{
54 const char *ch = "UNKNOWN";
55 bool prev_set = ctx->state_flags & (1 << t);
56
57 switch (t) {
58 case GPRS_RLCMAC_FLAG_CCCH:
59 ch = "CCCH";
60 break;
61 case GPRS_RLCMAC_FLAG_PACCH:
62 ch = "PACCH";
63 break;
64 default:
65 LOGPTBF(ctx->tbf, LOGL_ERROR,
66 "attempted to %sset unexpected ass. type %d - FIXME!\n",
67 set ? "" : "un", t);
68 return;
69 }
70
71 if (set && prev_set) {
72 LOGPTBF(ctx->tbf, LOGL_ERROR,
73 "attempted to set ass. type %s which is already set.\n", ch);
74 } else if (!set && !prev_set) {
75 return;
76 }
77
78 LOGPTBF(ctx->tbf, LOGL_INFO, "%sset ass. type %s [prev CCCH:%u, PACCH:%u]\n",
79 set ? "" : "un", ch,
80 !!(ctx->state_flags & (1 << GPRS_RLCMAC_FLAG_CCCH)),
81 !!(ctx->state_flags & (1 << GPRS_RLCMAC_FLAG_PACCH)));
82
83 if (set) {
84 ctx->state_flags |= (1 << t);
85 } else {
86 ctx->state_flags &= GPRS_RLCMAC_FLAG_TO_MASK; /* keep to flags */
87 ctx->state_flags &= ~(1 << t);
88 }
89}
90
91
92static void st_null(struct osmo_fsm_inst *fi, uint32_t event, void *data)
93{
94 struct tbf_fsm_ctx *ctx = (struct tbf_fsm_ctx *)fi->priv;
95 switch (event) {
96 case TBF_EV_ASSIGN_ADD_CCCH:
97 mod_ass_type(ctx, GPRS_RLCMAC_FLAG_CCCH, true);
98 tbf_fsm_state_chg(fi, tbf_direction(ctx->tbf) == GPRS_RLCMAC_DL_TBF ?
99 TBF_ST_ASSIGN : TBF_ST_FLOW);
100 break;
101 case TBF_EV_ASSIGN_ADD_PACCH:
102 mod_ass_type(ctx, GPRS_RLCMAC_FLAG_PACCH, true);
103 tbf_fsm_state_chg(fi, TBF_ST_ASSIGN);
104 break;
105 default:
106 OSMO_ASSERT(0);
107 }
108}
109
110static void st_assign(struct osmo_fsm_inst *fi, uint32_t event, void *data)
111{
112 struct tbf_fsm_ctx *ctx = (struct tbf_fsm_ctx *)fi->priv;
113 switch (event) {
114 case TBF_EV_ASSIGN_ADD_CCCH:
115 mod_ass_type(ctx, GPRS_RLCMAC_FLAG_CCCH, true);
116 break;
117 case TBF_EV_ASSIGN_ADD_PACCH:
118 mod_ass_type(ctx, GPRS_RLCMAC_FLAG_PACCH, true);
119 break;
Pau Espin Pedrol720e19e2021-07-22 19:56:37 +0200120 case TBF_EV_ASSIGN_ACK_PACCH:
121 if (ctx->state_flags & (1 << GPRS_RLCMAC_FLAG_CCCH)) {
122 /* We now know that the PACCH really existed */
123 LOGPTBF(ctx->tbf, LOGL_INFO,
124 "The TBF has been confirmed on the PACCH, "
125 "changed type from CCCH to PACCH\n");
126 mod_ass_type(ctx, GPRS_RLCMAC_FLAG_CCCH, false);
127 mod_ass_type(ctx, GPRS_RLCMAC_FLAG_PACCH, true);
128 }
129 tbf_fsm_state_chg(fi, TBF_ST_FLOW);
130 break;
131 case TBF_EV_ASSIGN_READY_CCCH:
132 /* change state to FLOW, so scheduler will start transmission */
133 tbf_fsm_state_chg(fi, TBF_ST_FLOW);
134 break;
Pau Espin Pedrol33e80072021-07-22 19:20:50 +0200135 default:
136 OSMO_ASSERT(0);
137 }
138}
139
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +0200140static void tbf_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
141{
142 /* TODO: needed ?
143 * struct tbf_fsm_ctx *ctx = (struct tbf_fsm_ctx *)fi->priv;
144 */
145}
146
147static int tbf_fsm_timer_cb(struct osmo_fsm_inst *fi)
148{
149 switch (fi->T) {
150 default:
151 break;
152 }
153 return 0;
154}
155
156static struct osmo_fsm_state tbf_fsm_states[] = {
157 [TBF_ST_NULL] = {
158 .in_event_mask =
Pau Espin Pedrol33e80072021-07-22 19:20:50 +0200159 X(TBF_EV_ASSIGN_ADD_CCCH) |
160 X(TBF_EV_ASSIGN_ADD_PACCH),
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +0200161 .out_state_mask =
162 X(TBF_ST_ASSIGN) |
163 X(TBF_ST_FLOW) |
164 X(TBF_ST_RELEASING),
165 .name = "NULL",
Pau Espin Pedrol33e80072021-07-22 19:20:50 +0200166 .action = st_null,
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +0200167 },
168 [TBF_ST_ASSIGN] = {
169 .in_event_mask =
Pau Espin Pedrol33e80072021-07-22 19:20:50 +0200170 X(TBF_EV_ASSIGN_ADD_CCCH) |
Pau Espin Pedrol720e19e2021-07-22 19:56:37 +0200171 X(TBF_EV_ASSIGN_ADD_PACCH) |
172 X(TBF_EV_ASSIGN_ACK_PACCH) |
173 X(TBF_EV_ASSIGN_READY_CCCH),
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +0200174 .out_state_mask =
175 X(TBF_ST_FLOW) |
176 X(TBF_ST_FINISHED) |
177 X(TBF_ST_RELEASING),
178 .name = "ASSIGN",
Pau Espin Pedrol33e80072021-07-22 19:20:50 +0200179 .action = st_assign,
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +0200180 },
181 [TBF_ST_FLOW] = {
182 .in_event_mask =
183 0,
184 .out_state_mask =
185 X(TBF_ST_FINISHED) |
186 X(TBF_ST_WAIT_RELEASE) |
187 X(TBF_ST_RELEASING),
188 .name = "FLOW",
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +0200189 },
190 [TBF_ST_FINISHED] = {
191 .in_event_mask =
192 0,
193 .out_state_mask =
194 X(TBF_ST_WAIT_RELEASE),
195 .name = "FINISHED",
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +0200196 },
197 [TBF_ST_WAIT_RELEASE] = {
198 .in_event_mask =
199 0,
200 .out_state_mask =
201 X(TBF_ST_RELEASING),
202 .name = "WAIT_RELEASE",
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +0200203 },
204 [TBF_ST_RELEASING] = {
205 .in_event_mask =
206 0,
207 .out_state_mask =
208 0,
209 .name = "RELEASING",
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +0200210 },
211};
212
Pau Espin Pedrol33e80072021-07-22 19:20:50 +0200213void tbf_fsm_allstate_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
214{
215 struct tbf_fsm_ctx *ctx = (struct tbf_fsm_ctx *)fi->priv;
216 switch (event) {
217 case TBF_EV_ASSIGN_DEL_CCCH:
218 mod_ass_type(ctx, GPRS_RLCMAC_FLAG_CCCH, false);
219 break;
220 default:
221 OSMO_ASSERT(0);
222 }
223}
224
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +0200225struct osmo_fsm tbf_fsm = {
226 .name = "TBF",
227 .states = tbf_fsm_states,
228 .num_states = ARRAY_SIZE(tbf_fsm_states),
229 .timer_cb = tbf_fsm_timer_cb,
230 .cleanup = tbf_fsm_cleanup,
231 .log_subsys = DTBF,
232 .event_names = tbf_fsm_event_names,
Pau Espin Pedrol33e80072021-07-22 19:20:50 +0200233 .allstate_action = tbf_fsm_allstate_action,
234 .allstate_event_mask = X(TBF_EV_ASSIGN_DEL_CCCH),
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +0200235};
236
237static __attribute__((constructor)) void tbf_fsm_init(void)
238{
239 OSMO_ASSERT(osmo_fsm_register(&tbf_fsm) == 0);
240}