blob: 364ef00f8fefd6e3fb71e956d748d42a089827ce [file] [log] [blame]
Pau Espin Pedrolea8dbdd2021-07-29 18:39:16 +02001/* tbf_ul_ack_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.
Pau Espin Pedrolea8dbdd2021-07-29 18:39:16 +020015 */
16
17#include <unistd.h>
18
19#include <talloc.h>
20
21#include <osmocom/core/bitvec.h>
22
23#include <tbf_ul_ack_fsm.h>
24#include <gprs_rlcmac.h>
25#include <gprs_debug.h>
26#include <gprs_ms.h>
27#include <encoding.h>
28#include <bts.h>
29#include <tbf.h>
30#include <tbf_ul.h>
31
32#include <tbf_ul_ack_fsm.h>
33
34#define X(s) (1 << (s))
35
36const struct osmo_tdef_state_timeout tbf_ul_ack_fsm_timeouts[32] = {
37 [TBF_UL_ACK_ST_NONE] = {},
38 [TBF_UL_ACK_ST_SCHED_UL_ACK] = {},
39 [TBF_UL_ACK_ST_WAIT_ACK] = {},
40};
41
42const struct value_string tbf_ul_ack_fsm_event_names[] = {
43 { TBF_UL_ACK_EV_SCHED_ACK, "SCHED_ACK" },
44 { TBF_UL_ACK_EV_CREATE_RLCMAC_MSG, "CREATE_RLCMAC_MSG" },
45 { TBF_UL_ACK_EV_RX_CTRL_ACK, "RX_CTRL_ACK" },
46 { TBF_UL_ACK_EV_POLL_TIMEOUT, "POLL_TIMEOUT" },
47 { 0, NULL }
48};
49
50static struct msgb *create_ul_ack_nack(const struct tbf_ul_ack_fsm_ctx *ctx,
51 const struct tbf_ul_ack_ev_create_rlcmac_msg_ctx *d,
52 bool final)
53{
54 struct msgb *msg;
55 int rc;
56 unsigned int rrbp = 0;
57 uint32_t new_poll_fn = 0;
58 struct gprs_rlcmac_tbf *tbf = (struct gprs_rlcmac_tbf *)ctx->tbf;
59 struct GprsMs *ms = tbf_ms(tbf);
60
61 if (final) {
62 rc = tbf_check_polling(tbf, d->fn, d->ts, &new_poll_fn, &rrbp);
63 if (rc < 0)
64 return NULL;
65 }
66
67 msg = msgb_alloc(23, "rlcmac_ul_ack");
68 if (!msg)
69 return NULL;
70 struct bitvec *ack_vec = bitvec_alloc(23, tbf);
71 if (!ack_vec) {
72 msgb_free(msg);
73 return NULL;
74 }
75 bitvec_unhex(ack_vec, DUMMY_VEC);
76 write_packet_uplink_ack(ack_vec, ctx->tbf, final, rrbp);
77 bitvec_pack(ack_vec, msgb_put(msg, 23));
78 bitvec_free(ack_vec);
79
80 /* TS 44.060 7a.2.1.1: "The contention resolution is completed on
81 * the network side when the network receives an RLC data block that
82 * comprises the TLLI value that identifies the mobile station and the
83 * TFI value associated with the TBF."
84 * However, it's handier for us to mark contention resolution success
85 * here since according to spec upon rx UL ACK is the time at which MS
86 * realizes contention resolution succeeds. */
87 if (ms_tlli(ms) != GSM_RESERVED_TMSI)
88 ul_tbf_contention_resolution_success(ctx->tbf);
89
Pau Espin Pedrola161bf42021-07-30 13:42:06 +020090 if (final) {
Pau Espin Pedrolea8dbdd2021-07-29 18:39:16 +020091 tbf_set_polling(tbf, new_poll_fn, d->ts, PDCH_ULC_POLL_UL_ACK);
Pau Espin Pedrola161bf42021-07-30 13:42:06 +020092 LOGPTBFUL(tbf, LOGL_DEBUG,
93 "Scheduled UL Acknowledgement polling on PACCH (FN=%d, TS=%d)\n",
94 new_poll_fn, d->ts);
95 }
Pau Espin Pedrolea8dbdd2021-07-29 18:39:16 +020096
97 return msg;
98}
99
100static void st_none(struct osmo_fsm_inst *fi, uint32_t event, void *data)
101{
102 switch (event) {
103 case TBF_UL_ACK_EV_SCHED_ACK:
104 tbf_ul_ack_fsm_state_chg(fi, TBF_UL_ACK_ST_SCHED_UL_ACK);
105 break;
106 default:
107 OSMO_ASSERT(0);
108 }
109}
110
111static void st_sched_ul_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
112{
113 struct tbf_ul_ack_fsm_ctx *ctx = (struct tbf_ul_ack_fsm_ctx *)fi->priv;
114 struct gprs_rlcmac_tbf *tbf = (struct gprs_rlcmac_tbf *)ctx->tbf;
115 struct tbf_ul_ack_ev_create_rlcmac_msg_ctx *data_ctx;
116 bool final;
117
118 switch (event) {
119 case TBF_UL_ACK_EV_SCHED_ACK:
120 LOGPTBFUL(tbf, LOGL_DEBUG,
121 "Sending Ack/Nack already scheduled, no need to re-schedule\n");
122 break;
123 case TBF_UL_ACK_EV_CREATE_RLCMAC_MSG:
124 data_ctx = (struct tbf_ul_ack_ev_create_rlcmac_msg_ctx *)data;
125 final = tbf_state(tbf) == TBF_ST_FINISHED;
126 data_ctx->msg = create_ul_ack_nack(ctx, data_ctx, final);
127 if (!data_ctx->msg)
128 return;
129 if (final) /* poll set */
130 tbf_ul_ack_fsm_state_chg(fi, TBF_UL_ACK_ST_WAIT_ACK);
131 else
132 tbf_ul_ack_fsm_state_chg(fi, TBF_UL_ACK_ST_NONE);
133 break;
134 default:
135 OSMO_ASSERT(0);
136 }
137}
138
139static void st_wait_ctrl_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
140{
141 struct tbf_ul_ack_fsm_ctx *ctx = (struct tbf_ul_ack_fsm_ctx *)fi->priv;
142 struct gprs_rlcmac_tbf *tbf = (struct gprs_rlcmac_tbf *)ctx->tbf;
143
144 switch (event) {
145 case TBF_UL_ACK_EV_SCHED_ACK:
146 /* ignore, we are in the middle of waiting for a response */
147 break;
148 case TBF_UL_ACK_EV_RX_CTRL_ACK:
149 tbf_ul_ack_fsm_state_chg(fi, TBF_UL_ACK_ST_NONE);
150 break;
151 case TBF_UL_ACK_EV_POLL_TIMEOUT:
152 LOGPTBF(tbf, LOGL_NOTICE,
153 "Timeout for polling PACKET CONTROL ACK for PACKET UPLINK ACK: %s\n",
154 tbf_rlcmac_diag(tbf));
155 /* Reschedule Ul Ack/NAck */
156 tbf_ul_ack_fsm_state_chg(fi, TBF_UL_ACK_ST_SCHED_UL_ACK);
157 break;
158 default:
159 OSMO_ASSERT(0);
160 }
161}
162
163static int tbf_ul_ack_fsm_timer_cb(struct osmo_fsm_inst *fi)
164{
165 switch (fi->T) {
166 default:
167 OSMO_ASSERT(0);
168 }
169 return 0;
170}
171
172static struct osmo_fsm_state tbf_ul_ack_fsm_states[] = {
173 [TBF_UL_ACK_ST_NONE] = {
174 .in_event_mask =
175 X(TBF_UL_ACK_EV_SCHED_ACK),
176 .out_state_mask =
177 X(TBF_UL_ACK_ST_SCHED_UL_ACK),
178 .name = "NONE",
179 .action = st_none,
180 },
181 [TBF_UL_ACK_ST_SCHED_UL_ACK] = {
182 .in_event_mask =
183 X(TBF_UL_ACK_EV_SCHED_ACK) |
184 X(TBF_UL_ACK_EV_CREATE_RLCMAC_MSG),
185 .out_state_mask =
186 X(TBF_UL_ACK_ST_NONE) |
187 X(TBF_UL_ACK_ST_WAIT_ACK),
188 .name = "SCHED_UL_ACK",
189 .action = st_sched_ul_ack,
190 },
191 [TBF_UL_ACK_ST_WAIT_ACK] = {
192 .in_event_mask =
193 X(TBF_UL_ACK_EV_SCHED_ACK) |
194 X(TBF_UL_ACK_EV_RX_CTRL_ACK) |
195 X(TBF_UL_ACK_EV_POLL_TIMEOUT),
196 .out_state_mask =
197 X(TBF_UL_ACK_ST_NONE) |
198 X(TBF_UL_ACK_ST_SCHED_UL_ACK),
199 .name = "WAIT_ACK",
200 .action = st_wait_ctrl_ack,
201 },
202};
203
204struct osmo_fsm tbf_ul_ack_fsm = {
205 .name = "UL_ACK_TBF",
206 .states = tbf_ul_ack_fsm_states,
207 .num_states = ARRAY_SIZE(tbf_ul_ack_fsm_states),
208 .timer_cb = tbf_ul_ack_fsm_timer_cb,
209 .log_subsys = DTBFUL,
210 .event_names = tbf_ul_ack_fsm_event_names,
211};
212
213static __attribute__((constructor)) void tbf_ul_ack_fsm_init(void)
214{
215 OSMO_ASSERT(osmo_fsm_register(&tbf_ul_ack_fsm) == 0);
216}
217
218
219struct msgb *tbf_ul_ack_create_rlcmac_msg(const struct gprs_rlcmac_tbf *tbf, uint32_t fn, uint8_t ts)
220{
221 int rc;
222 struct tbf_ul_ack_ev_create_rlcmac_msg_ctx data_ctx = {
223 .fn = fn,
224 .ts = ts,
225 .msg = NULL,
226 };
227 OSMO_ASSERT(tbf_direction(tbf) == GPRS_RLCMAC_UL_TBF);
228
229 rc = osmo_fsm_inst_dispatch(tbf_ul_ack_fi((const struct gprs_rlcmac_ul_tbf *)tbf), TBF_UL_ACK_EV_CREATE_RLCMAC_MSG, &data_ctx);
230 if (rc != 0 || !data_ctx.msg)
231 return NULL;
232 return data_ctx.msg;
233}
234
235bool tbf_ul_ack_rts(const struct gprs_rlcmac_tbf *tbf)
236{
237 struct osmo_fsm_inst *fi = tbf_ul_ack_fi((const struct gprs_rlcmac_ul_tbf *)tbf);
238 return fi->state == TBF_UL_ACK_ST_SCHED_UL_ACK;
239}
240
241/* Did we already send the Final ACK and we are waiting for its confirmation (CTRL ACK) ? */
242bool tbf_ul_ack_waiting_cnf_final_ack(const struct gprs_rlcmac_tbf* tbf)
243{
244 OSMO_ASSERT(tbf_direction(tbf) == GPRS_RLCMAC_UL_TBF);
245 struct osmo_fsm_inst *fi = tbf_ul_ack_fi((const struct gprs_rlcmac_ul_tbf *)tbf);
246 return fi->state == TBF_UL_ACK_ST_WAIT_ACK;
247}
248
249bool tbf_ul_ack_exp_ctrl_ack(const struct gprs_rlcmac_tbf *tbf, uint32_t fn, uint8_t ts)
250{
251 struct osmo_fsm_inst *fi = tbf_ul_ack_fi((const struct gprs_rlcmac_ul_tbf *)tbf);
252 return fi->state == TBF_UL_ACK_ST_WAIT_ACK;
253 /* FIXME: validate FN and TS match: && ctx->poll_fn = fn && ctx->poll_ts == ts */
254}