aboutsummaryrefslogtreecommitdiffstats
path: root/src/rlcmac/tbf_ul_ass_fsm.c
blob: 5587ddb43a5f33c172a3851f86a871a158a53612 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
/* TBF as per 3GPP TS 44.064 */
/*
 * (C) 2023 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <errno.h>
#include <talloc.h>
#include <osmocom/core/tdef.h>
#include <osmocom/core/fsm.h>
#include <osmocom/core/bitvec.h>
#include <osmocom/gsm/gsm_utils.h>
#include <osmocom/gsm/gsm0502.h>

#include <osmocom/gprs/rlcmac/types.h>
#include <osmocom/gprs/rlcmac/tbf_ul_ass_fsm.h>
#include <osmocom/gprs/rlcmac/tbf_ul.h>
#include <osmocom/gprs/rlcmac/tbf_dl.h>
#include <osmocom/gprs/rlcmac/gre.h>
#include <osmocom/gprs/rlcmac/sched.h>
#include <osmocom/gprs/rlcmac/csn1_defs.h>
#include <osmocom/gprs/rlcmac/rlcmac_enc.h>
#include <osmocom/gprs/rlcmac/rlcmac_dec.h>
#include <osmocom/gprs/rlcmac/pdch_ul_controller.h>

#define X(s) (1 << (s))

static const struct value_string tbf_ul_ass_fsm_event_names[] = {
	{ GPRS_RLCMAC_TBF_UL_ASS_EV_START,		"START" },
	{ GPRS_RLCMAC_TBF_UL_ASS_EV_START_DIRECT_2PHASE, "START_DIRECT_2PHASE" },
	{ GPRS_RLCMAC_TBF_UL_ASS_EV_START_FROM_DL_TBF,	"START_FROM_DL_TBF" },
	{ GPRS_RLCMAC_TBF_UL_ASS_EV_RX_CCCH_IMM_ASS,	"RX_CCCH_IMM_ASS" },
	{ GPRS_RLCMAC_TBF_UL_ASS_EV_TBF_STARTING_TIME,	"TBF_STARTING_TIME" },
	{ GPRS_RLCMAC_TBF_UL_ASS_EV_CREATE_RLCMAC_MSG,	"CREATE_RLCMAC_MSG" },
	{ GPRS_RLCMAC_TBF_UL_ASS_EV_RX_PKT_UL_ASS,	"RX_PKT_UL_ASS" },
	{ 0, NULL }
};

static const struct osmo_tdef_state_timeout tbf_ul_ass_fsm_timeouts[32] = {
	[GPRS_RLCMAC_TBF_UL_ASS_ST_IDLE] = { },
	[GPRS_RLCMAC_TBF_UL_ASS_ST_WAIT_CCCH_IMM_ASS] = { },
	[GPRS_RLCMAC_TBF_UL_ASS_ST_WAIT_TBF_STARTING_TIME1] = { },
	[GPRS_RLCMAC_TBF_UL_ASS_ST_SCHED_PKT_RES_REQ] = { },
	[GPRS_RLCMAC_TBF_UL_ASS_ST_WAIT_PKT_UL_ASS] = { .T = 3168 },
	[GPRS_RLCMAC_TBF_UL_ASS_ST_WAIT_TBF_STARTING_TIME2] = { },
	[GPRS_RLCMAC_TBF_UL_ASS_ST_COMPL] = { },
};

/* Transition to a state, using the T timer defined in tbf_fsm_timeouts.
 * The actual timeout value is in turn obtained from conn->T_defs.
 * Assumes local variable fi exists. */
 #define tbf_ul_ass_fsm_state_chg(fi, NEXT_STATE) \
	osmo_tdef_fsm_inst_state_chg(fi, NEXT_STATE, \
				     tbf_ul_ass_fsm_timeouts, \
				     g_rlcmac_ctx->T_defs, \
				     -1)

static struct msgb *create_pkt_resource_req(const struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *ctx,
					    struct tbf_ul_ass_ev_create_rlcmac_msg_ctx *d)
{
	struct msgb *msg;
	struct bitvec bv;
	RlcMacUplink_t ul_block;
	int rc;

	msg = msgb_alloc(GSM_MACBLOCK_LEN, "pkt_res_req");
	if (!msg)
		return NULL;

	/* Initialize a bit vector that uses allocated msgb as the data buffer. */
	bv = (struct bitvec){
		.data = msgb_put(msg, GSM_MACBLOCK_LEN),
		.data_len = GSM_MACBLOCK_LEN,
	};
	bitvec_unhex(&bv, GPRS_RLCMAC_DUMMY_VEC);

	gprs_rlcmac_enc_prepare_pkt_resource_req(&ul_block, ctx->ul_tbf, GPRS_RLCMAC_ACCESS_TYPE_2PHASE_ACC_REQ);
	rc = osmo_gprs_rlcmac_encode_uplink(&bv, &ul_block);
	if (rc < 0) {
		LOGPTBFUL(ctx->ul_tbf, LOGL_ERROR, "Encoding of Packet Resource Req failed (%d)\n", rc);
		goto free_ret;
	}

	return msg;

free_ret:
	msgb_free(msg);
	return NULL;
}

/* Generate a 8-bit CHANNEL REQUEST message as per 3GPP TS 44.018, 9.1.8 */
static uint8_t gen_chan_req(bool single_block)
{
	uint8_t rnd = (uint8_t)rand();

	if (single_block) /* 01110xxx */
		return 0x70 | (rnd & 0x07);

	/* 011110xx or 01111x0x or 01111xx0 */
	if ((rnd & 0x07) == 0x07)
		return 0x78;
	return 0x78 | (rnd & 0x07);
}

static int submit_rach_req(struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *ctx)
{
	struct osmo_gprs_rlcmac_prim *rlcmac_prim;
	ctx->rach_req_ra = gen_chan_req(ctx->ass_type == GPRS_RLCMAC_TBF_UL_ASS_TYPE_2PHASE);

	LOGPFSML(ctx->fi, LOGL_INFO, "Send RACH.req ra=0x%02x\n", ctx->rach_req_ra);
	rlcmac_prim = gprs_rlcmac_prim_alloc_l1ctl_rach8_req(ctx->rach_req_ra);
	return gprs_rlcmac_prim_call_down_cb(rlcmac_prim);
}

static int handle_imm_ass(struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *ctx, const struct tbf_ul_ass_ev_rx_ccch_imm_ass_ctx *d)
{
	/* TS 44.018 10.5.2.16 IA Rest Octets */
	switch (d->iaro->UnionType) {
	case 1: /* d->iaro->u.lh.* (IA_RestOctetsLH_t) */
		switch (d->iaro->u.lh.lh0x.UnionType) {
		case 0: /* d->iaro->u.ll.lh0x.EGPRS_PktUlAss.* (IA_EGPRS_PktUlAss_t) */
			return -ENOTSUP; /* TODO */
		}
	case 3: /* d->iaro->u.hh.* (IA_RestOctetsHH_t) */
		switch (d->iaro->u.hh.UnionType) {
		case 0: /* d->iaro->u.hh.u.UplinkDownlinkAssignment.* (IA_PacketAssignment_UL_DL_t) */
			switch (d->iaro->u.hh.u.UplinkDownlinkAssignment.UnionType) {
			case 0: /* d->iaro->u.hh.u.UplinkDownlinkAssignment.ul_dl.Packet_Uplink_ImmAssignment.* (Packet_Uplink_ImmAssignment_t) */
				switch (d->iaro->u.hh.u.UplinkDownlinkAssignment.ul_dl.Packet_Uplink_ImmAssignment.UnionType) {
				case 0: /* d->iaro->u.hh.u.UplinkDownlinkAssignment.ul_dl.Packet_Uplink_ImmAssignment.Access.SingleBlockAllocation.* (GPRS_SingleBlockAllocation_t) */
					/* TODO: 2phase access support: Schedule transmit of PKT_RES_REQ on FN=(GPRS_SingleBlockAllocation_t).TBF_STARTING_TIME */
					if (ctx->ass_type == GPRS_RLCMAC_TBF_UL_ASS_TYPE_1PHASE) {
						LOGPFSML(ctx->fi, LOGL_INFO, "MS requested 1-Phase-Access, but Network forces 2-Phase-Access\n");
						ctx->ass_type = GPRS_RLCMAC_TBF_UL_ASS_TYPE_2PHASE;
					}
					ctx->sba = true;
					ctx->tbf_starting_time_exists = true;
					ctx->tbf_starting_time = TBF_StartingTime_to_fn(&d->iaro->u.hh.u.UplinkDownlinkAssignment.ul_dl.Packet_Uplink_ImmAssignment.Access.SingleBlockAllocation.TBF_STARTING_TIME,
											d->fn);
					ctx->phase1_alloc.ts[d->ts_nr].allocated = true;
					ctx->phase1_alloc.num_ts = 1;
					LOGPFSML(ctx->fi, LOGL_INFO, "ImmAss SingleBlock (2phase access) cur_tn=%u cur_fn=%u start_fn=%u\n", d->ts_nr, d->fn, ctx->tbf_starting_time);
					return 0;
				case 1: /* d->iaro->u.hh.u.UplinkDownlinkAssignment.ul_dl.Packet_Uplink_ImmAssignment.Access.DynamicOrFixedAllocation.* (GPRS_DynamicOrFixedAllocation_t) */
					ctx->phase1_alloc.ul_tfi = d->iaro->u.hh.u.UplinkDownlinkAssignment.ul_dl.Packet_Uplink_ImmAssignment.Access.DynamicOrFixedAllocation.TFI_ASSIGNMENT;
					ctx->ul_tbf->tx_cs = d->iaro->u.hh.u.UplinkDownlinkAssignment.ul_dl.Packet_Uplink_ImmAssignment.Access.DynamicOrFixedAllocation.CHANNEL_CODING_COMMAND + 1;
					ctx->tbf_starting_time_exists = d->iaro->u.hh.u.UplinkDownlinkAssignment.ul_dl.Packet_Uplink_ImmAssignment.Access.DynamicOrFixedAllocation.Exist_TBF_STARTING_TIME;
					if (ctx->tbf_starting_time_exists)
						ctx->tbf_starting_time = TBF_StartingTime_to_fn(&d->iaro->u.hh.u.UplinkDownlinkAssignment.ul_dl.Packet_Uplink_ImmAssignment.Access.DynamicOrFixedAllocation.TBF_STARTING_TIME,
												d->fn);
					LOGPFSML(ctx->fi, LOGL_INFO, "ImmAss TFI=%u initCS=%s cur_tn=%u cur_fn=%u start_fn=%u\n",
						 ctx->phase1_alloc.ul_tfi, gprs_rlcmac_mcs_name(ctx->ul_tbf->tx_cs), d->ts_nr, d->fn, ctx->tbf_starting_time);
					switch (d->iaro->u.hh.u.UplinkDownlinkAssignment.ul_dl.Packet_Uplink_ImmAssignment.Access.DynamicOrFixedAllocation.UnionType) {
					case 0: /* d->iaro->u.hh.u.UplinkDownlinkAssignment.ul_dl.Packet_Uplink_ImmAssignment.Access.DynamicOrFixedAllocation.Allocation.DynamicAllocation (DynamicAllocation_t) */
						ctx->phase1_alloc.ts[d->ts_nr].allocated = true;
						ctx->phase1_alloc.ts[d->ts_nr].usf = d->iaro->u.hh.u.UplinkDownlinkAssignment.ul_dl.Packet_Uplink_ImmAssignment.Access.DynamicOrFixedAllocation.Allocation.DynamicAllocation.USF;
						ctx->phase1_alloc.num_ts = 1;
						LOGPFSML(ctx->fi, LOGL_INFO, "ImmAss DynamicAlloc (1phase access) ts_nr=%u usf=%u\n", d->ts_nr, ctx->phase1_alloc.ts[d->ts_nr].usf);
						return 0;
					case 1: /* d->iaro->u.hh.u.UplinkDownlinkAssignment.ul_dl.Packet_Uplink_ImmAssignment.Access.DynamicOrFixedAllocation.Allocation.FixedAllocationDummy (guint8) */
						return -ENOTSUP;
					}
					break;
				}
				break;
			}
			break;
		}
		break;
	}

	OSMO_ASSERT(0);
	return -EFAULT;
}

static int handle_pkt_ul_ass(struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *ctx, const struct tbf_ul_ass_ev_rx_pkt_ul_ass_ctx *d)
{

	const Packet_Uplink_Assignment_t *ulass = &d->dl_block->u.Packet_Uplink_Assignment;
	uint8_t tn;
	const Timeslot_Allocation_t *ts_alloc;
	const Timeslot_Allocation_Power_Ctrl_Param_t *ts_alloc_pwr_ctl;

	switch (ulass->UnionType) {
	case 0: /* ulass->u.PUA_GPRS_Struct.* (PUA_GPRS_t) */
		ctx->ul_tbf->tx_cs = ulass->u.PUA_GPRS_Struct.CHANNEL_CODING_COMMAND + 1;
		switch (ulass->u.PUA_GPRS_Struct.UnionType) {
		case 1: /* Dynamic Allocation (Dynamic_Allocation_t) */
			if (ulass->u.PUA_GPRS_Struct.u.Dynamic_Allocation.Exist_UPLINK_TFI_ASSIGNMENT)
				ctx->phase2_alloc.ul_tfi = ulass->u.PUA_GPRS_Struct.u.Dynamic_Allocation.UPLINK_TFI_ASSIGNMENT;
			/* TODO: P0, PR_MODE, USF_GRANULARITY, RLC_DATA_BLOCKS_GRANTED */
			ctx->tbf_starting_time_exists = ulass->u.PUA_GPRS_Struct.u.Dynamic_Allocation.Exist_TBF_Starting_Time;
			if (ctx->tbf_starting_time_exists)
				ctx->tbf_starting_time = TBF_Starting_Frame_Number_to_fn(&ulass->u.PUA_GPRS_Struct.u.Dynamic_Allocation.TBF_Starting_Time,
											 d->fn);
			switch (ulass->u.PUA_GPRS_Struct.u.Dynamic_Allocation.UnionType) {
			case 0: /* Timeslot_Allocation_t */
				ts_alloc = &ulass->u.PUA_GPRS_Struct.u.Dynamic_Allocation.u.Timeslot_Allocation[0];
				ctx->phase2_alloc.num_ts = 0;
				for (tn = 0; tn < 8; tn++) {
					ctx->phase2_alloc.ts[tn].allocated = ts_alloc[tn].Exist;
					if (ts_alloc[tn].Exist) {
						ctx->phase2_alloc.num_ts++;
						ctx->phase2_alloc.ts[tn].usf = ts_alloc[tn].USF_TN;
					}
				}
				break;
			case 1: /* Timeslot_Allocation_Power_Ctrl_Param_t */
				/* TODO: ALPHA, GAMMA */
				ts_alloc_pwr_ctl = &ulass->u.PUA_GPRS_Struct.u.Dynamic_Allocation.u.Timeslot_Allocation_Power_Ctrl_Param;
				ctx->phase2_alloc.num_ts = 0;
				for (tn = 0; tn < 8; tn++) {
					ctx->phase2_alloc.ts[tn].allocated = ts_alloc_pwr_ctl->Slot[tn].Exist;
					if (ts_alloc_pwr_ctl->Slot[tn].Exist) {
						ctx->phase2_alloc.num_ts++;
						ctx->phase2_alloc.ts[tn].usf = ts_alloc_pwr_ctl->Slot[tn].USF_TN;
					}
				}
				break;
			}
			break;
		case 2: /* Single Block Allocation */
			LOGPFSML(ctx->fi, LOGL_NOTICE, "Rx Pkt Ul Ass GPRS Single Block Allocation not supported!\n");
			return -ENOTSUP;
		case 0: /* Fixed Allocation */
			LOGPFSML(ctx->fi, LOGL_NOTICE, "Rx Pkt Ul Ass GPRS Fixed Allocation not supported!\n");
			return -ENOTSUP;
		}
		return 0;
	case 1: /* ulass->u.PUA_EGPRS_Struct.* (PUA_EGPRS_t) */
		LOGPFSML(ctx->fi, LOGL_NOTICE, "Rx Pkt Ul Ass EGPRS not supported!\n");
		return -ENOTSUP;
	}

	OSMO_ASSERT(0);
	return -EFAULT;
}

static void st_idle_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
{
	struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *ctx = (struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *)fi->priv;

	/* Reset state: */
	ctx->dl_tbf = NULL;
	memset(&ctx->phase1_alloc, 0, sizeof(ctx->phase1_alloc));
	memset(&ctx->phase2_alloc, 0, sizeof(ctx->phase2_alloc));
}

static void st_idle(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
	struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *ctx = (struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *)fi->priv;
	int rc;

	switch (event) {
	case GPRS_RLCMAC_TBF_UL_ASS_EV_START:
		/* Inform the main TBF state about the assignment starting: */
		osmo_fsm_inst_dispatch(ctx->ul_tbf->state_fsm.fi, GPRS_RLCMAC_TBF_UL_EV_UL_ASS_START, NULL);
		ctx->ass_type = *(enum gprs_rlcmac_tbf_ul_ass_type *)data;
		rc = submit_rach_req(ctx);
		if (rc < 0) {
			osmo_fsm_inst_dispatch(ctx->ul_tbf->state_fsm.fi, GPRS_RLCMAC_TBF_UL_EV_UL_ASS_REJ, NULL);
			break;
		}
		tbf_ul_ass_fsm_state_chg(fi, GPRS_RLCMAC_TBF_UL_ASS_ST_WAIT_CCCH_IMM_ASS);
		break;
	case GPRS_RLCMAC_TBF_UL_ASS_EV_START_DIRECT_2PHASE:
		osmo_fsm_inst_dispatch(ctx->ul_tbf->state_fsm.fi, GPRS_RLCMAC_TBF_UL_EV_UL_ASS_START, NULL);
		ctx->ass_type = GPRS_RLCMAC_TBF_UL_ASS_TYPE_2PHASE;
		tbf_ul_ass_fsm_state_chg(fi, GPRS_RLCMAC_TBF_UL_ASS_ST_SCHED_PKT_RES_REQ);
		break;
	case GPRS_RLCMAC_TBF_UL_ASS_EV_START_FROM_DL_TBF:
		osmo_fsm_inst_dispatch(ctx->ul_tbf->state_fsm.fi, GPRS_RLCMAC_TBF_UL_EV_UL_ASS_START, NULL);
		ctx->ass_type = GPRS_RLCMAC_TBF_UL_ASS_TYPE_2PHASE;
		tbf_ul_ass_fsm_state_chg(fi, GPRS_RLCMAC_TBF_UL_ASS_ST_WAIT_PKT_UL_ASS);
		break;
	default:
		OSMO_ASSERT(0);
	}
}

static void st_wait_ccch_imm_ass(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
	struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *ctx = (struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *)fi->priv;
	const struct tbf_ul_ass_ev_rx_ccch_imm_ass_ctx *ev_rx_ccch_imm_ass_ctx;

	switch (event) {
	case GPRS_RLCMAC_TBF_UL_ASS_EV_RX_CCCH_IMM_ASS:
		ev_rx_ccch_imm_ass_ctx = data;
		if (handle_imm_ass(ctx, ev_rx_ccch_imm_ass_ctx) < 0)
			return;
		if (ctx->tbf_starting_time_exists &&
		    fn_cmp(ctx->tbf_starting_time, ev_rx_ccch_imm_ass_ctx->fn) > 0) {
			tbf_ul_ass_fsm_state_chg(fi, GPRS_RLCMAC_TBF_UL_ASS_ST_WAIT_TBF_STARTING_TIME1);
			return;
		}
		if (ctx->ass_type == GPRS_RLCMAC_TBF_UL_ASS_TYPE_1PHASE)
			tbf_ul_ass_fsm_state_chg(fi, GPRS_RLCMAC_TBF_UL_ASS_ST_COMPL);
		else
			tbf_ul_ass_fsm_state_chg(fi, GPRS_RLCMAC_TBF_UL_ASS_ST_SCHED_PKT_RES_REQ);
		break;
	default:
		OSMO_ASSERT(0);
	}
}

static void st_wait_tbf_starting_time1_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
{
	struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *ctx = (struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *)fi->priv;

	/* Configure lower layers to submit an RTS tick starting at tbf_starting_time
	 * and scheduler will send event  GPRS_RLCMAC_TBF_UL_ASS_EV_TBF_STARTING_TIME to us. */
	gprs_rlcmac_ul_tbf_submit_configure_req(ctx->ul_tbf, &ctx->phase1_alloc,
						ctx->tbf_starting_time_exists, ctx->tbf_starting_time);
}

static void st_wait_tbf_starting_time1(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
	struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *ctx = (struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *)fi->priv;
	const struct tbf_ul_ass_ev_rx_ccch_imm_ass_ctx *ev_rx_ccch_imm_ass_ctx;

	switch (event) {
	case GPRS_RLCMAC_TBF_UL_ASS_EV_RX_CCCH_IMM_ASS:
		ev_rx_ccch_imm_ass_ctx = data;
		if (handle_imm_ass(ctx, ev_rx_ccch_imm_ass_ctx) < 0)
			return;
		if (ctx->tbf_starting_time_exists &&
		    fn_cmp(ctx->tbf_starting_time, ev_rx_ccch_imm_ass_ctx->fn) > 0) {
			tbf_ul_ass_fsm_state_chg(fi, GPRS_RLCMAC_TBF_UL_ASS_ST_WAIT_TBF_STARTING_TIME1);
			return;
		}
		if (ctx->ass_type == GPRS_RLCMAC_TBF_UL_ASS_TYPE_1PHASE)
			tbf_ul_ass_fsm_state_chg(fi, GPRS_RLCMAC_TBF_UL_ASS_ST_COMPL);
		else
			tbf_ul_ass_fsm_state_chg(fi, GPRS_RLCMAC_TBF_UL_ASS_ST_SCHED_PKT_RES_REQ);
		break;
	case GPRS_RLCMAC_TBF_UL_ASS_EV_TBF_STARTING_TIME:
		if (ctx->ass_type == GPRS_RLCMAC_TBF_UL_ASS_TYPE_1PHASE)
			tbf_ul_ass_fsm_state_chg(fi, GPRS_RLCMAC_TBF_UL_ASS_ST_COMPL);
		else
			tbf_ul_ass_fsm_state_chg(fi, GPRS_RLCMAC_TBF_UL_ASS_ST_SCHED_PKT_RES_REQ);
		break;
	default:
		OSMO_ASSERT(0);
	}
}

static void st_sched_pkt_res_req(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
	struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *ctx = (struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *)fi->priv;
	struct tbf_ul_ass_ev_create_rlcmac_msg_ctx *data_ctx;
	switch (event) {
	case GPRS_RLCMAC_TBF_UL_ASS_EV_CREATE_RLCMAC_MSG:
		data_ctx = (struct tbf_ul_ass_ev_create_rlcmac_msg_ctx *)data;
		data_ctx->msg = create_pkt_resource_req(ctx, data_ctx);
		if (!data_ctx->msg)
			return;
		ctx->sba = false; /* Reset state */
		tbf_ul_ass_fsm_state_chg(fi, GPRS_RLCMAC_TBF_UL_ASS_ST_WAIT_PKT_UL_ASS);
		break;
	default:
		OSMO_ASSERT(0);
	}
}

static void st_wait_pkt_ul_ass(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
	struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *ctx = (struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *)fi->priv;
	struct tbf_ul_ass_ev_rx_pkt_ul_ass_ctx *d;
	int rc;

	switch (event) {
	case GPRS_RLCMAC_TBF_UL_ASS_EV_RX_PKT_UL_ASS:
		d = data;
		rc = handle_pkt_ul_ass(ctx, d);
		if (rc < 0)
			LOGPFSML(fi, LOGL_ERROR, "Rx Pkt Ul Ass: failed to parse!\n");
		// TODO: what to do if Pkt_ul_ass is "reject"? need to check spec, depending on cause.
		/* If RRBP contains valid data, schedule a response (PKT CONTROL ACK or PKT RESOURCE REQ). */
		if (d->dl_block->SP) {
			uint32_t poll_fn = rrbp2fn(d->fn, d->dl_block->RRBP);
			uint32_t next_blk = fn_next_block(fn_next_block(poll_fn));
			gprs_rlcmac_pdch_ulc_reserve(g_rlcmac_ctx->sched.ulc[d->ts_nr], poll_fn,
						GPRS_RLCMAC_PDCH_ULC_POLL_UL_ASS,
						ul_tbf_as_tbf(ctx->ul_tbf));
			/* We need to wait at least until sending the PKT CTRL
			 * ACK (in the old CTRL TS) before completing the
			 * assignment and using the new TS assignment. */
			if (!ctx->tbf_starting_time_exists && fn_cmp(ctx->tbf_starting_time, next_blk) < 0) {
				ctx->tbf_starting_time_exists = true;
				ctx->tbf_starting_time = next_blk;
			}
		}

		if (ctx->tbf_starting_time_exists &&
		    fn_cmp(ctx->tbf_starting_time, d->fn) > 0) {
			tbf_ul_ass_fsm_state_chg(fi, GPRS_RLCMAC_TBF_UL_ASS_ST_WAIT_TBF_STARTING_TIME2);
		} else {
			tbf_ul_ass_fsm_state_chg(fi, GPRS_RLCMAC_TBF_UL_ASS_ST_COMPL);
		}
		break;
	default:
		OSMO_ASSERT(0);
	}
}

static void st_wait_tbf_starting_time2_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
{
	struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *ctx = (struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *)fi->priv;

	/* Configure lower layers to submit an RTS tick starting at tbf_starting_time
	 * and scheduler will send event GPRS_RLCMAC_TBF_UL_ASS_EV_TBF_STARTING_TIME to us. */
	gprs_rlcmac_ul_tbf_submit_configure_req(ctx->ul_tbf, &ctx->phase2_alloc,
						ctx->tbf_starting_time_exists, ctx->tbf_starting_time);
}

static void st_wait_tbf_starting_time2(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
	struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *ctx = (struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *)fi->priv;
	struct tbf_ul_ass_ev_rx_pkt_ul_ass_ctx *d;
	struct tbf_ul_ass_ev_create_rlcmac_msg_ctx *data_ctx;
	int rc;

	switch (event) {
	case GPRS_RLCMAC_TBF_UL_ASS_EV_RX_PKT_UL_ASS:
		d = data;
		rc = handle_pkt_ul_ass(ctx, d);
		if (rc < 0)
			LOGPFSML(fi, LOGL_ERROR, "Rx Pkt Ul Ass: failed to parse!\n");
		// TODO: what to do if Pkt_ul_ass is "reject"? need to check spec, depending on cause.
		/* If RRBP contains valid data, schedule a response (PKT CONTROL ACK or PKT RESOURCE REQ). */
		if (d->dl_block->SP) {
			uint32_t poll_fn = rrbp2fn(d->fn, d->dl_block->RRBP);
			uint32_t next_blk = fn_next_block(poll_fn);
			/* FIXME: double fn_next_block() here is to delay
			* release of old TS late enough so that the PKT CTRL ACK
			* is transmitted. This is wrong since we basically lose
			* the first TS in the new TBF, but otherwise lower layers
			* may free the lchan while last burst of the last block is
			* still not transmitted... IMHO lower layers need to be
			* fixed to delay closing the lchan until all the
			* blocks/bursts enqueued are transmitted... */
			next_blk = fn_next_block(next_blk);
			gprs_rlcmac_pdch_ulc_reserve(g_rlcmac_ctx->sched.ulc[d->ts_nr], poll_fn,
						GPRS_RLCMAC_PDCH_ULC_POLL_UL_ASS,
						ul_tbf_as_tbf(ctx->ul_tbf));
			/* We need to wait at least until sending the PKT CTRL
			 * ACK (in the old CTRL TS) before completing the
			 * assignment and using the new TS assignment. */
			if (!ctx->tbf_starting_time_exists && fn_cmp(ctx->tbf_starting_time, next_blk) < 0) {
				ctx->tbf_starting_time_exists = true;
				ctx->tbf_starting_time = next_blk;
			}
		}

		if (ctx->tbf_starting_time_exists &&
		    fn_cmp(ctx->tbf_starting_time, d->fn) > 0) {
			tbf_ul_ass_fsm_state_chg(fi, GPRS_RLCMAC_TBF_UL_ASS_ST_WAIT_TBF_STARTING_TIME2);
		} else {
			tbf_ul_ass_fsm_state_chg(fi, GPRS_RLCMAC_TBF_UL_ASS_ST_COMPL);
		}
		break;
	case GPRS_RLCMAC_TBF_UL_ASS_EV_TBF_STARTING_TIME:
		tbf_ul_ass_fsm_state_chg(fi, GPRS_RLCMAC_TBF_UL_ASS_ST_COMPL);
		break;
	case GPRS_RLCMAC_TBF_UL_ASS_EV_CREATE_RLCMAC_MSG:
		data_ctx = (struct tbf_ul_ass_ev_create_rlcmac_msg_ctx *)data;
		LOGPFSML(fi, LOGL_INFO, "TS=%u FN=%u Tx Pkt Ctrl Ack\n", data_ctx->ts, data_ctx->fn);
		data_ctx->msg = gprs_rlcmac_gre_create_pkt_ctrl_ack(ul_tbf_as_tbf(ctx->ul_tbf)->gre);
		if (!data_ctx->msg)
			return;
		break;
	default:
		OSMO_ASSERT(0);
	}
}

static void st_compl_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
{
	struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *ctx = (struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *)fi->priv;

	/* Update TBF with allocated content: */
	if (ctx->ass_type == GPRS_RLCMAC_TBF_UL_ASS_TYPE_1PHASE)
		memcpy(&ctx->ul_tbf->cur_alloc, &ctx->phase1_alloc, sizeof(ctx->phase1_alloc));
	else
		memcpy(&ctx->ul_tbf->cur_alloc, &ctx->phase2_alloc, sizeof(ctx->phase2_alloc));
	/* Inform the main TBF state about the assignment completed: */
	osmo_fsm_inst_dispatch(ctx->ul_tbf->state_fsm.fi, GPRS_RLCMAC_TBF_UL_EV_UL_ASS_COMPL, NULL);
	/* Go back to IDLE state. */
	tbf_ul_ass_fsm_state_chg(fi, GPRS_RLCMAC_TBF_UL_ASS_ST_IDLE);
}

static struct osmo_fsm_state tbf_ul_ass_fsm_states[] = {
	[GPRS_RLCMAC_TBF_UL_ASS_ST_IDLE] = {
		.in_event_mask =
			X(GPRS_RLCMAC_TBF_UL_ASS_EV_START) |
			X(GPRS_RLCMAC_TBF_UL_ASS_EV_START_DIRECT_2PHASE) |
			X(GPRS_RLCMAC_TBF_UL_ASS_EV_START_FROM_DL_TBF),
		.out_state_mask =
			X(GPRS_RLCMAC_TBF_UL_ASS_ST_WAIT_CCCH_IMM_ASS) |
			X(GPRS_RLCMAC_TBF_UL_ASS_ST_SCHED_PKT_RES_REQ) |
			X(GPRS_RLCMAC_TBF_UL_ASS_ST_WAIT_PKT_UL_ASS),
		.name = "IDLE",
		.onenter = st_idle_on_enter,
		.action = st_idle,
	},
	[GPRS_RLCMAC_TBF_UL_ASS_ST_WAIT_CCCH_IMM_ASS] = {
		.in_event_mask =
			X(GPRS_RLCMAC_TBF_UL_ASS_EV_RX_CCCH_IMM_ASS),
		.out_state_mask =
			X(GPRS_RLCMAC_TBF_UL_ASS_ST_WAIT_TBF_STARTING_TIME1) |
			X(GPRS_RLCMAC_TBF_UL_ASS_ST_SCHED_PKT_RES_REQ) |
			X(GPRS_RLCMAC_TBF_UL_ASS_ST_COMPL),
		.name = "WAIT_CCCH_IMM_ASS",
		.action = st_wait_ccch_imm_ass,
	},
	[GPRS_RLCMAC_TBF_UL_ASS_ST_WAIT_TBF_STARTING_TIME1] = {
		.in_event_mask =
			X(GPRS_RLCMAC_TBF_UL_ASS_EV_RX_CCCH_IMM_ASS) |
			X(GPRS_RLCMAC_TBF_UL_ASS_EV_TBF_STARTING_TIME),
		.out_state_mask =
			X(GPRS_RLCMAC_TBF_UL_ASS_ST_WAIT_TBF_STARTING_TIME1) |
			X(GPRS_RLCMAC_TBF_UL_ASS_ST_SCHED_PKT_RES_REQ) |
			X(GPRS_RLCMAC_TBF_UL_ASS_ST_COMPL),
		.name = "WAIT_TBF_STARTING_TIME1",
		.onenter = st_wait_tbf_starting_time1_on_enter,
		.action = st_wait_tbf_starting_time1,
	},
	[GPRS_RLCMAC_TBF_UL_ASS_ST_SCHED_PKT_RES_REQ] = {
		.in_event_mask =
			X(GPRS_RLCMAC_TBF_UL_ASS_EV_CREATE_RLCMAC_MSG),
		.out_state_mask =
			X(GPRS_RLCMAC_TBF_UL_ASS_ST_WAIT_PKT_UL_ASS),
		.name = "SCHED_PKT_RES_REQ",
		.action = st_sched_pkt_res_req,
	},
	[GPRS_RLCMAC_TBF_UL_ASS_ST_WAIT_PKT_UL_ASS] = {
		.in_event_mask =
			X(GPRS_RLCMAC_TBF_UL_ASS_EV_RX_PKT_UL_ASS),
		.out_state_mask =
			X(GPRS_RLCMAC_TBF_UL_ASS_ST_SCHED_PKT_RES_REQ) |
			X(GPRS_RLCMAC_TBF_UL_ASS_ST_WAIT_TBF_STARTING_TIME2) |
			X(GPRS_RLCMAC_TBF_UL_ASS_ST_COMPL),
		.name = "WAIT_PKT_UL_ASS",
		.action = st_wait_pkt_ul_ass,
	},
	[GPRS_RLCMAC_TBF_UL_ASS_ST_WAIT_TBF_STARTING_TIME2] = {
		.in_event_mask =
			X(GPRS_RLCMAC_TBF_UL_ASS_EV_RX_PKT_UL_ASS) |
			X(GPRS_RLCMAC_TBF_UL_ASS_EV_TBF_STARTING_TIME) |
			X(GPRS_RLCMAC_TBF_UL_ASS_EV_CREATE_RLCMAC_MSG),
		.out_state_mask =
			X(GPRS_RLCMAC_TBF_UL_ASS_ST_SCHED_PKT_RES_REQ) |
			X(GPRS_RLCMAC_TBF_UL_ASS_ST_WAIT_TBF_STARTING_TIME2) |
			X(GPRS_RLCMAC_TBF_UL_ASS_ST_COMPL),
		.name = "WAIT_TBF_STARTING_TIME2",
		.onenter = st_wait_tbf_starting_time2_on_enter,
		.action = st_wait_tbf_starting_time2,
	},
	[GPRS_RLCMAC_TBF_UL_ASS_ST_COMPL] = {
		.in_event_mask = 0,
		.out_state_mask =
			X(GPRS_RLCMAC_TBF_UL_ASS_ST_IDLE),
		.name = "COMPLETED",
		.onenter = st_compl_on_enter,
	},
};


static int tbf_ul_ass_fsm_timer_cb(struct osmo_fsm_inst *fi)
{
	struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *ctx = (struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *)fi->priv;
	switch (fi->T) {
	case 3168:
		/* If the UL TBF assignment was started from DL TBF it is not
		really possible reattempting because we haven't yet any phase1
		allocation. Hence simply destroy the TBF and let next DL TBF DL
		ACK/NACK re-request an UL TBF assignment: */
		if (ctx->dl_tbf) {
			LOGPFSML(ctx->fi, LOGL_NOTICE,
				 "UL TBF establishment failure (T3168 timeout attempts=%u, ass from DL TBF)\n",
				 ctx->pkt_res_req_proc_attempts);
			gprs_rlcmac_ul_tbf_free(ctx->ul_tbf);
			return 0;
		}
		/* TS 44.060 7.1.3.3: "the mobile station shall then reinitiate the packet access
		 * procedure unless the packet access procedure has already been attempted four
		 * times. In that case, TBF failure has occurred and an RLC/MAC error should be
		 * reported to the higher layer for each of the TBFs for which resources were
		 * requested". */
		ctx->pkt_res_req_proc_attempts++;
		LOGPFSML(ctx->fi, LOGL_INFO, "T3168 timeout attempts=%u\n", ctx->pkt_res_req_proc_attempts);
		OSMO_ASSERT(fi->state == GPRS_RLCMAC_TBF_UL_ASS_ST_WAIT_PKT_UL_ASS);
		if (ctx->pkt_res_req_proc_attempts == 4) {
			LOGPFSML(ctx->fi, LOGL_NOTICE, "TBF establishment failure (T3168 timeout attempts=%u)\n",
				 ctx->pkt_res_req_proc_attempts);
			gprs_rlcmac_ul_tbf_free(ctx->ul_tbf);
			return 0;
		}
		tbf_ul_ass_fsm_state_chg(fi, GPRS_RLCMAC_TBF_UL_ASS_ST_SCHED_PKT_RES_REQ);
		break;
	default:
		OSMO_ASSERT(0);
	}
	return 0;
}

static struct osmo_fsm tbf_ul_ass_fsm = {
	.name = "UL_TBF_ASS",
	.states = tbf_ul_ass_fsm_states,
	.num_states = ARRAY_SIZE(tbf_ul_ass_fsm_states),
	.timer_cb = tbf_ul_ass_fsm_timer_cb,
	.log_subsys = DLGLOBAL, /* updated dynamically through gprs_rlcmac_tbf_ul_ass_fsm_set_log_cat() */
	.event_names = tbf_ul_ass_fsm_event_names,
};

int gprs_rlcmac_tbf_ul_ass_fsm_init(void)
{
	return osmo_fsm_register(&tbf_ul_ass_fsm);
}

void gprs_rlcmac_tbf_ul_ass_fsm_set_log_cat(int logcat)
{
	tbf_ul_ass_fsm.log_subsys = logcat;
}

int gprs_rlcmac_tbf_ul_ass_fsm_constructor(struct gprs_rlcmac_ul_tbf *ul_tbf)
{
	struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *ctx = &ul_tbf->ul_ass_fsm;
	ctx->ul_tbf = ul_tbf;
	ctx->fi = osmo_fsm_inst_alloc(&tbf_ul_ass_fsm, ul_tbf, ctx, LOGL_INFO, NULL);
	if (!ctx->fi)
		return -ENODATA;

	return 0;
}

void gprs_rlcmac_tbf_ul_ass_fsm_destructor(struct gprs_rlcmac_ul_tbf *ul_tbf)
{
	struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *ctx = &ul_tbf->ul_ass_fsm;
	osmo_fsm_inst_free(ctx->fi);
	ctx->fi = NULL;
}

int gprs_rlcmac_tbf_ul_ass_start(struct gprs_rlcmac_ul_tbf *ul_tbf, enum gprs_rlcmac_tbf_ul_ass_type type)
{
	int rc;
	rc = osmo_fsm_inst_dispatch(ul_tbf->ul_ass_fsm.fi,
				    GPRS_RLCMAC_TBF_UL_ASS_EV_START,
				    &type);
	return rc;
}

/* A releasing TBF being polled is used to fill in 1phase access internally and
* switch the FSM to trigger the 2hpase directly (tx Pkt Res Req) */
int gprs_rlcmac_tbf_ul_ass_start_from_releasing_ul_tbf(struct gprs_rlcmac_ul_tbf *ul_tbf, struct gprs_rlcmac_ul_tbf *old_ul_tbf)
{
	int rc;
	memcpy(&ul_tbf->ul_ass_fsm.phase1_alloc, &old_ul_tbf->cur_alloc,
	       sizeof(ul_tbf->ul_ass_fsm.phase1_alloc));
	rc = osmo_fsm_inst_dispatch(ul_tbf->ul_ass_fsm.fi,
				    GPRS_RLCMAC_TBF_UL_ASS_EV_START_DIRECT_2PHASE,
				    NULL);
	return rc;
}

/* A DL-TBF requested a UL TBF over DL ACK/NACK, wait to receive Pkt Ul Ass for
 * it, aka switch the FSM to trigger the 2hpase directly (tx Pkt Res Req) */
int gprs_rlcmac_tbf_ul_ass_start_from_dl_tbf_ack_nack(struct gprs_rlcmac_ul_tbf *ul_tbf, const struct gprs_rlcmac_dl_tbf *dl_tbf, uint8_t tn)
{
	int rc;
	ul_tbf->ul_ass_fsm.dl_tbf = dl_tbf;

	/* The TS where the PKT UL ASS is to be received is the one where the DL
	 * ACK/NACK was sent in the DL TBF (control TS): */
	ul_tbf->ul_ass_fsm.phase1_alloc.num_ts = 1;
	ul_tbf->ul_ass_fsm.phase1_alloc.ts[tn].allocated = true;
	ul_tbf->ul_ass_fsm.phase1_alloc.ts[tn].usf = 0xff;

	rc = osmo_fsm_inst_dispatch(ul_tbf->ul_ass_fsm.fi,
				    GPRS_RLCMAC_TBF_UL_ASS_EV_START_FROM_DL_TBF,
				    NULL);
	return rc;
}

bool gprs_rlcmac_tbf_ul_ass_pending(struct gprs_rlcmac_ul_tbf *ul_tbf)
{
	return ul_tbf->ul_ass_fsm.fi->state != GPRS_RLCMAC_TBF_UL_ASS_ST_IDLE;
}

bool gprs_rlcmac_tbf_ul_ass_match_rach_req(struct gprs_rlcmac_ul_tbf *ul_tbf, uint8_t ra)
{
	return ul_tbf->ul_ass_fsm.fi->state == GPRS_RLCMAC_TBF_UL_ASS_ST_WAIT_CCCH_IMM_ASS &&
		ul_tbf->ul_ass_fsm.rach_req_ra == ra;
}

bool gprs_rlcmac_tbf_ul_ass_waiting_tbf_starting_time(const struct gprs_rlcmac_ul_tbf *ul_tbf)
{
	return ul_tbf->ul_ass_fsm.fi->state == GPRS_RLCMAC_TBF_UL_ASS_ST_WAIT_TBF_STARTING_TIME1 ||
	       ul_tbf->ul_ass_fsm.fi->state == GPRS_RLCMAC_TBF_UL_ASS_ST_WAIT_TBF_STARTING_TIME2;
}

/* The scheduled ticks the new FN, which may trigger changes internally if TBF Starting Time is reached */
void gprs_rlcmac_tbf_ul_ass_fn_tick(const struct gprs_rlcmac_ul_tbf *ul_tbf, uint32_t fn, uint8_t ts_nr)
{
	int res;

	OSMO_ASSERT(ul_tbf->ul_ass_fsm.tbf_starting_time_exists);
	switch (ul_tbf->ul_ass_fsm.fi->state) {
	case GPRS_RLCMAC_TBF_UL_ASS_ST_WAIT_TBF_STARTING_TIME1:
		OSMO_ASSERT(ul_tbf->ul_ass_fsm.phase1_alloc.num_ts > 0);
		if (!ul_tbf->ul_ass_fsm.phase1_alloc.ts[ts_nr].allocated)
			return;
		break;
	case GPRS_RLCMAC_TBF_UL_ASS_ST_WAIT_TBF_STARTING_TIME2:
		OSMO_ASSERT(ul_tbf->ul_ass_fsm.phase2_alloc.num_ts > 0);
		if (!ul_tbf->ul_ass_fsm.phase2_alloc.ts[ts_nr].allocated)
			return;
		break;
	default:
		OSMO_ASSERT(0);
	}
	res = fn_cmp(fn, ul_tbf->ul_ass_fsm.tbf_starting_time);
	if (res < 0) {/* fn BEFORE tbf_starting_time */
		LOGPTBFUL(ul_tbf, LOGL_DEBUG, "TS=%" PRIu8 " FN=%u Waiting for tbf_starting_time=%u\n",
			  ts_nr, fn, ul_tbf->ul_ass_fsm.tbf_starting_time);
		return;
	}
	if (res > 0) /* fn AFTER tbf_starting time */
		LOGPTBFUL(ul_tbf, LOGL_ERROR, "TS=%" PRIu8 " FN=%u Received late tick for tbf_starting_time=%u!\n",
			  ts_nr, fn, ul_tbf->ul_ass_fsm.tbf_starting_time);
	/* fn == tbf_starting time */
	LOGPTBFUL(ul_tbf, LOGL_INFO, "TS=%" PRIu8 " FN=%u TBF_STARTING_TIME reached\n", fn, ts_nr);
	osmo_fsm_inst_dispatch(ul_tbf->ul_ass_fsm.fi, GPRS_RLCMAC_TBF_UL_ASS_EV_TBF_STARTING_TIME, NULL);
}

enum gprs_rlcmac_tbf_ul_ass_fsm_states gprs_rlcmac_tbf_ul_ass_state(const struct gprs_rlcmac_ul_tbf *ul_tbf)
{
	const struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *ctx = &ul_tbf->ul_ass_fsm;
	return ctx->fi->state;
}

bool gprs_rlcmac_tbf_ul_ass_rts(const struct gprs_rlcmac_ul_tbf *ul_tbf, const struct gprs_rlcmac_rts_block_ind *bi)
{
	const struct gprs_rlcmac_tbf_ul_ass_fsm_ctx *ctx = &ul_tbf->ul_ass_fsm;

	switch (ctx->fi->state) {
	case GPRS_RLCMAC_TBF_UL_ASS_ST_SCHED_PKT_RES_REQ:
		if (!ctx->phase1_alloc.ts[bi->ts].allocated)
			return false;
		if (ctx->sba)
			return (!ctx->tbf_starting_time_exists || ctx->tbf_starting_time == bi->fn);
		/* No SBA, check assigned USF: */
		return ctx->phase1_alloc.ts[bi->ts].usf == bi->usf;
	default:
		return false;
	};
}

struct msgb *gprs_rlcmac_tbf_ul_ass_create_rlcmac_msg(const struct gprs_rlcmac_ul_tbf *ul_tbf,
						      const struct gprs_rlcmac_rts_block_ind *bi)
{
	int rc;
	struct tbf_ul_ass_ev_create_rlcmac_msg_ctx data_ctx = {
		.ts = bi->ts,
		.fn = bi->fn,
		.msg = NULL,
	};

	rc = osmo_fsm_inst_dispatch(ul_tbf->ul_ass_fsm.fi,
				    GPRS_RLCMAC_TBF_UL_ASS_EV_CREATE_RLCMAC_MSG,
				    &data_ctx);
	if (rc != 0 || !data_ctx.msg)
		return NULL;
	return data_ctx.msg;
}