aboutsummaryrefslogtreecommitdiffstats
path: root/src/gprs_rlcmac.cpp
blob: 3353462ec88a1879fd6252a119bc21895b50949f (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
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
/* gprs_rlcmac.cpp
 *
 * Copyright (C) 2012 Ivan Klyuchnikov
 * Copyright (C) 2012 Andreas Eversberg <jolly@eversberg.eu>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
 
#include <gprs_bssgp_pcu.h>
#include <pcu_l1_if.h>
#include <gprs_rlcmac.h>
#include <gsmL1prim.h>

LLIST_HEAD(gprs_rlcmac_tbfs);
void *rlcmac_tall_ctx;
LLIST_HEAD(block_queue);

/* FIXME: spread ressources on multiple TRX */
int tfi_alloc(uint8_t *_trx, uint8_t *_ts)
{
	struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts;
	struct gprs_rlcmac_pdch *pdch;
	uint8_t trx, ts, tfi;

	for (trx = 0; trx < 8; trx++) {
		for (ts = 0; ts < 8; ts++) {
			pdch = &bts->trx[trx].pdch[ts];
			if (!pdch->enable)
				continue;
			break;
		}
		if (ts < 8)
			break;
	}
	if (trx == 8) {
		LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH available.\n");
		return -EINVAL;
	}


	LOGP(DRLCMAC, LOGL_DEBUG, "Searching for first unallocated TFI: "
		"TRX=%d TS=%d\n", trx, ts);
	for (tfi = 0; tfi < 32; tfi++) {
		if (!pdch->tbf[tfi])
			break;
	}
	
	if (tfi < 32) {
		LOGP(DRLCMAC, LOGL_DEBUG, " Found TFI=%d.\n", tfi);
		*_trx = trx;
		*_ts = ts;
		return tfi;
	}
	LOGP(DRLCMAC, LOGL_NOTICE, "No TFI available.\n");

	return -1;
}

int find_free_usf(uint8_t trx, uint8_t ts)
{
	struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts;
	struct gprs_rlcmac_pdch *pdch;
	struct gprs_rlcmac_tbf *tbf;
	uint8_t usf_map = 0;
	uint8_t tfi, usf;

	if (trx >= 8 || ts >= 8)
		return -EINVAL;
	pdch = &bts->trx[trx].pdch[ts];

	/* make map of used USF */
	for (tfi = 0; tfi < 32; tfi++) {
		tbf = pdch->tbf[tfi];
		if (!tbf)
			continue;
		if (tbf->direction != GPRS_RLCMAC_UL_TBF)
			continue;
		usf_map |= (1 << tbf->dir.ul.usf);
	}

	/* look for USF, don't use USF=7 */
	for (usf = 0; usf < 7; usf++) {
		if (!(usf_map & (1 << usf))) {
			LOGP(DRLCMAC, LOGL_DEBUG, " Found USF=%d.\n", usf);
			return usf;
		}
	}
	LOGP(DRLCMAC, LOGL_NOTICE, "No USF available.\n");

	return -1;
}

/* lookup TBF Entity (by TFI) */
#warning FIXME: use pdch instance by trx and ts, because tfi is local
struct gprs_rlcmac_tbf *tbf_by_tfi(uint8_t tfi)
{
	struct gprs_rlcmac_tbf *tbf;

	llist_for_each_entry(tbf, &gprs_rlcmac_tbfs, list) {
		if (tbf->tfi == tfi)
			return tbf;
	}
	return NULL;
}

struct gprs_rlcmac_tbf *tbf_by_tlli(uint32_t tlli)
{
	struct gprs_rlcmac_tbf *tbf;
	llist_for_each_entry(tbf, &gprs_rlcmac_tbfs, list) {
		if ((tbf->tlli == tlli)&&(tbf->direction == GPRS_RLCMAC_UL_TBF))
			return tbf;
	}
	return NULL;
}

struct gprs_rlcmac_tbf *tbf_alloc(uint8_t tfi, uint8_t trx, uint8_t ts)
{
	struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts;
	struct gprs_rlcmac_pdch *pdch;
	struct gprs_rlcmac_tbf *tbf;

	LOGP(DRLCMAC, LOGL_INFO, "********** TBF starts here **********\n");
	LOGP(DRLCMAC, LOGL_INFO, "Allocating TBF with TFI=%d.\n", tfi);

	if (trx >= 8 || ts >= 8 || tfi >= 32)
		return NULL;
	pdch = &bts->trx[trx].pdch[ts];

	tbf = talloc_zero(rlcmac_tall_ctx, struct gprs_rlcmac_tbf);
	if (!tbf)
		return NULL;

	tbf->tfi = tfi;
	tbf->trx = trx;
	tbf->ts = ts;
	tbf->arfcn = bts->trx[trx].arfcn;
	tbf->tsc = bts->trx[trx].pdch[ts].tsc;
	tbf->ws = 64;
	tbf->sns = 128;
	llist_add(&tbf->list, &gprs_rlcmac_tbfs);
	pdch->tbf[tfi] = tbf;

	return tbf;
}

void tbf_free(struct gprs_rlcmac_tbf *tbf)
{
	struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts;
	struct gprs_rlcmac_pdch *pdch;

	LOGP(DRLCMAC, LOGL_INFO, "Free TBF with TFI=%d.\n", tbf->tfi);
	LOGP(DRLCMAC, LOGL_INFO, "********** TBF ends here **********\n");
	tbf_timer_stop(tbf);
	pdch = &bts->trx[tbf->trx].pdch[tbf->ts];
	pdch->tbf[tbf->tfi] = NULL;
	llist_del(&tbf->list);
	talloc_free(tbf);
}


static void tbf_timer_cb(void *_tbf)
{
	struct gprs_rlcmac_tbf *tbf = (struct gprs_rlcmac_tbf *)_tbf;

	LOGP(DRLCMAC, LOGL_DEBUG, "TBF timer %u expired.\n", tbf->T);

	tbf->num_T_exp++;

	switch (tbf->T) {
	case 3169:
		LOGP(DRLCMAC, LOGL_DEBUG, "TBF will be freed due to timeout\n");
		/* free TBF */
		tbf_free(tbf);
		break;
	case 3103:
		if (++tbf->dir.ul.n3103 == N3103_MAX) {
			LOGP(DRLCMAC, LOGL_DEBUG, "Final ACK will be resent "
				"due to timeout\n");
			/* timeout for polling with final ack message, */
			/* trigger sending at next RTS */
			tbf->dir.ul.substate = GPRS_RLCMAC_UL_SEND_ACK;
		} else {
			LOGP(DRLCMAC, LOGL_DEBUG, "Too many timeouts on final "
				"ACK, starting T3169\n");
			/* restart T3169, so we can be sure that after expiry,
			 * the mobile has timed out and wiil not transmit
			 * anymore */
			tbf->state = GPRS_RLCMAC_RELEASING;
		        tbf_timer_start(tbf, 3169, T3169);
		}
		break;
	default:
		LOGP(DRLCMAC, LOGL_ERROR, "Timer expired in unknown mode: %u\n",
			tbf->T);
	}
}

void tbf_timer_start(struct gprs_rlcmac_tbf *tbf, unsigned int T,
				unsigned int seconds)
{
	if (!osmo_timer_pending(&tbf->timer))
		LOGP(DRLCMAC, LOGL_DEBUG, "Starting TBF timer %u.\n", T);
	else
		LOGP(DRLCMAC, LOGL_DEBUG, "Restarting TBF timer %u while old "
			"timer %u pending \n", T, tbf->T);

	tbf->T = T;
	tbf->num_T_exp = 0;

	/* Tunning timers can be safely re-scheduled. */
	tbf->timer.data = tbf;
	tbf->timer.cb = &tbf_timer_cb;

	osmo_timer_schedule(&tbf->timer, seconds, 0);
}

void tbf_timer_stop(struct gprs_rlcmac_tbf *tbf)
{
	if (osmo_timer_pending(&tbf->timer)) {
		LOGP(DRLCMAC, LOGL_DEBUG, "Stopping TBF timer %u.\n", tbf->T);
		osmo_timer_del(&tbf->timer);
	}
}

static void tbf_gsm_timer_cb(void *_tbf)
{
	struct gprs_rlcmac_tbf *tbf = (struct gprs_rlcmac_tbf *)_tbf;

	tbf->num_fT_exp++;

	switch (tbf->fT) {
	case 0:
		// This is timer for delay RLC/MAC data sending after Downlink Immediate Assignment on CCCH.
		gprs_rlcmac_segment_llc_pdu(tbf);
		LOGP(DRLCMAC, LOGL_NOTICE, "TBF: [DOWNLINK] END TFI: %u TLLI: 0x%08x \n", tbf->tfi, tbf->tlli);
		tbf_free(tbf);
		break;
	default:
		LOGP(DRLCMAC, LOGL_NOTICE, "Timer expired in unknown mode: %u \n", tbf->fT);
	}
}

static void tbf_gsm_timer_start(struct gprs_rlcmac_tbf *tbf, unsigned int fT,
				int frames)
{
	if (osmo_gsm_timer_pending(&tbf->gsm_timer))
		LOGP(DRLCMAC, LOGL_NOTICE, "Starting TBF timer %u while old timer %u pending \n", fT, tbf->fT);
	tbf->fT = fT;
	tbf->num_fT_exp = 0;

	/* FIXME: we should do this only once ? */
	tbf->gsm_timer.data = tbf;
	tbf->gsm_timer.cb = &tbf_gsm_timer_cb;

	osmo_gsm_timer_schedule(&tbf->gsm_timer, frames);
}

void gprs_rlcmac_enqueue_block(bitvec *block, int len)
{
	struct msgb *msg = msgb_alloc(len, "rlcmac_dl");
	bitvec_pack(block, msgb_put(msg, len));
	msgb_enqueue(&block_queue, msg);
}

void  write_packet_downlink_assignment(bitvec * dest, uint8_t tfi, uint32_t tlli, uint16_t arfcn, uint8_t tn, uint8_t ta, uint8_t tsc)
{
	// TODO We should use our implementation of encode RLC/MAC Control messages.
	unsigned wp = 0;
	int i;
	bitvec_write_field(dest, wp,0x1,2);  // Payload Type
	bitvec_write_field(dest, wp,0x0,2);  // Uplink block with TDMA framenumber
	bitvec_write_field(dest, wp,0x1,1);  // Suppl/Polling Bit
	bitvec_write_field(dest, wp,0x1,3);  // Uplink state flag
	bitvec_write_field(dest, wp,0x2,6);  // MESSAGE TYPE
	bitvec_write_field(dest, wp,0x0,2);  // Page Mode

	bitvec_write_field(dest, wp,0x0,1); // switch PERSIST_LEVEL: off
	bitvec_write_field(dest, wp,0x0,1); // switch TFI : on
	bitvec_write_field(dest, wp,0x0,1); // switch UPLINK TFI : on
	bitvec_write_field(dest, wp,tfi-1,5); // TFI

	bitvec_write_field(dest, wp,0x0,1); // Message escape
	bitvec_write_field(dest, wp,0x0,2); // Medium Access Method: Dynamic Allocation
	bitvec_write_field(dest, wp,0x0,1); // RLC acknowledged mode

	bitvec_write_field(dest, wp,0x0,1); // the network establishes no new downlink TBF for the mobile station
	bitvec_write_field(dest, wp,0x80 >> tn,8); // timeslot(s)

	bitvec_write_field(dest, wp,0x1,1); // switch TIMING_ADVANCE_VALUE = on
	bitvec_write_field(dest, wp,ta,6); // TIMING_ADVANCE_VALUE
	bitvec_write_field(dest, wp,0x0,1); // switch TIMING_ADVANCE_INDEX = off

	bitvec_write_field(dest, wp,0x0,1); // switch POWER CONTROL = off
	bitvec_write_field(dest, wp,0x1,1); // Frequency Parameters information elements = present

	bitvec_write_field(dest, wp,tsc,3); // Training Sequence Code (TSC) = 2
	bitvec_write_field(dest, wp,0x0,2); // ARFCN = present
	bitvec_write_field(dest, wp,arfcn,10); // ARFCN

	bitvec_write_field(dest, wp,0x1,1); // switch TFI   : on
	bitvec_write_field(dest, wp,tfi,5);// TFI

	bitvec_write_field(dest, wp,0x1,1); // Power Control Parameters IE = present
	bitvec_write_field(dest, wp,0x0,4); // ALPHA power control parameter
	for (i = 0; i < 8; i++)
		bitvec_write_field(dest, wp,(tn == i),1); // switch GAMMA_TN[i] = on or off
	bitvec_write_field(dest, wp,0x0,5); // GAMMA_TN[tn]

	bitvec_write_field(dest, wp,0x0,1); // TBF Starting TIME IE not present
	bitvec_write_field(dest, wp,0x0,1); // Measurement Mapping struct not present
	bitvec_write_field(dest, wp,0x0,1);
}

void  write_packet_uplink_assignment(bitvec * dest, uint8_t tfi, uint32_t tlli)
{
	// TODO We should use our implementation of encode RLC/MAC Control messages.
	unsigned wp = 0;
	bitvec_write_field(dest, wp,0x1,2);  // Payload Type
	bitvec_write_field(dest, wp,0x0,2);  // Uplink block with TDMA framenumber
	bitvec_write_field(dest, wp,0x1,1);  // Suppl/Polling Bit
	bitvec_write_field(dest, wp,0x1,3);  // Uplink state flag


	bitvec_write_field(dest, wp,0xa,6);  // MESSAGE TYPE

	bitvec_write_field(dest, wp,0x0,2);  // Page Mode

	bitvec_write_field(dest, wp,0x0,1); // switch PERSIST_LEVEL: off
	bitvec_write_field(dest, wp,0x2,2); // switch TLLI   : on
	bitvec_write_field(dest, wp,tlli,32); // TLLI

	bitvec_write_field(dest, wp,0x0,1); // Message escape
	bitvec_write_field(dest, wp,0x0,2); // CHANNEL_CODING_COMMAND
	bitvec_write_field(dest, wp,0x0,1); // TLLI_BLOCK_CHANNEL_CODING 

	bitvec_write_field(dest, wp,0x1,1); // switch TIMING_ADVANCE_VALUE = on
	bitvec_write_field(dest, wp,0x0,6); // TIMING_ADVANCE_VALUE
	bitvec_write_field(dest, wp,0x0,1); // switch TIMING_ADVANCE_INDEX = off
	
	bitvec_write_field(dest, wp,0x0,1); // Frequency Parameters = off

	bitvec_write_field(dest, wp,0x1,2); // Dynamic Allocation = off
	
	bitvec_write_field(dest, wp,0x0,1); // Dynamic Allocation
	bitvec_write_field(dest, wp,0x0,1); // P0 = off
	
	bitvec_write_field(dest, wp,0x1,0); // USF_GRANULARITY
	bitvec_write_field(dest, wp,0x1,1); // switch TFI   : on
	bitvec_write_field(dest, wp,tfi,5);// TFI

	bitvec_write_field(dest, wp,0x0,1); //
	bitvec_write_field(dest, wp,0x0,1); // TBF Starting Time = off
	bitvec_write_field(dest, wp,0x0,1); // Timeslot Allocation
	
	bitvec_write_field(dest, wp,0x0,5); // USF_TN 0 - 4
	bitvec_write_field(dest, wp,0x1,1); // USF_TN 5
	bitvec_write_field(dest, wp,0x1,3); // USF_TN 5
	bitvec_write_field(dest, wp,0x0,2); // USF_TN 6 - 7
//	bitvec_write_field(dest, wp,0x0,1); // Measurement Mapping struct not present
}


// GSM 04.08 9.1.18 Immediate assignment
int write_immediate_assignment(bitvec * dest, uint8_t downlink, uint8_t ra, uint32_t fn,
								uint8_t ta, uint16_t arfcn, uint8_t ts, uint8_t tsc, uint8_t tfi = 0, uint8_t usf = 0, uint32_t tlli = 0)
{
	unsigned wp = 0;

	bitvec_write_field(dest, wp,0x0,4);  // Skip Indicator
	bitvec_write_field(dest, wp,0x6,4);  // Protocol Discriminator
	bitvec_write_field(dest, wp,0x3F,8); // Immediate Assignment Message Type

	// 10.5.2.25b Dedicated mode or TBF
	bitvec_write_field(dest, wp,0x0,1);      // spare
	bitvec_write_field(dest, wp,0x0,1);      // TMA : Two-message assignment: No meaning
	bitvec_write_field(dest, wp,downlink,1); // Downlink : Downlink assignment to mobile in packet idle mode
	bitvec_write_field(dest, wp,0x1,1);      // T/D : TBF or dedicated mode: this message assigns a Temporary Block Flow (TBF).

	bitvec_write_field(dest, wp,0x0,4); // Page Mode

	// GSM 04.08 10.5.2.25a Packet Channel Description
	bitvec_write_field(dest, wp,0x1,5);                               // Channel type
	bitvec_write_field(dest, wp,ts,3);     // TN
	bitvec_write_field(dest, wp,tsc,3);    // TSC
	bitvec_write_field(dest, wp,0x0,3);                               // non-hopping RF channel configuraion
	bitvec_write_field(dest, wp,arfcn,10); // ARFCN

	//10.5.2.30 Request Reference
	bitvec_write_field(dest, wp,ra,8);                    // RA
	bitvec_write_field(dest, wp,(fn / (26 * 51)) % 32,5); // T1'
	bitvec_write_field(dest, wp,fn % 51,6);               // T3
	bitvec_write_field(dest, wp,fn % 26,5);               // T2

	// 10.5.2.40 Timing Advance
	bitvec_write_field(dest, wp,0x0,2); // spare
	bitvec_write_field(dest, wp,ta,6);  // Timing Advance value

	// No mobile allocation in non-hopping systems.
	// A zero-length LV.  Just write L=0.
	bitvec_write_field(dest, wp,0,8);

	if (downlink)
	{
		// GSM 04.08 10.5.2.16 IA Rest Octets
		bitvec_write_field(dest, wp, 3, 2);   // "HH"
		bitvec_write_field(dest, wp, 1, 2);   // "01" Packet Downlink Assignment
		bitvec_write_field(dest, wp,tlli,32); // TLLI
		bitvec_write_field(dest, wp,0x1,1);   // switch TFI   : on
		bitvec_write_field(dest, wp,tfi,5);   // TFI
		bitvec_write_field(dest, wp,0x0,1);   // RLC acknowledged mode
		bitvec_write_field(dest, wp,0x0,1);   // ALPHA = present
		bitvec_write_field(dest, wp,0x0,5);   // GAMMA power control parameter
		bitvec_write_field(dest, wp,0x0,1);   // Polling Bit
		bitvec_write_field(dest, wp,0x1,1);   // TA_VALID ???
		bitvec_write_field(dest, wp,0x1,1);   // switch TIMING_ADVANCE_INDEX = on
		bitvec_write_field(dest, wp,0x0,4);   // TIMING_ADVANCE_INDEX
		bitvec_write_field(dest, wp,0x0,1);   // TBF Starting TIME present
		bitvec_write_field(dest, wp,0x0,1);   // P0 not present
		bitvec_write_field(dest, wp,0x1,1);   // P0 not present
		bitvec_write_field(dest, wp,0xb,4);
	}
	else
	{
		// GMS 04.08 10.5.2.37b 10.5.2.16
		bitvec_write_field(dest, wp, 3, 2);    // "HH"
		bitvec_write_field(dest, wp, 0, 2);    // "0" Packet Uplink Assignment
		bitvec_write_field(dest, wp, 1, 1);    // Block Allocation : Not Single Block Allocation
		bitvec_write_field(dest, wp, tfi, 5);  // TFI_ASSIGNMENT Temporary Flow Identity
		bitvec_write_field(dest, wp, 0, 1);    // POLLING
		bitvec_write_field(dest, wp, 0, 1);    // ALLOCATION_TYPE: dynamic
		bitvec_write_field(dest, wp, usf, 3);    // USF
		bitvec_write_field(dest, wp, 0, 1);    // USF_GRANULARITY
		bitvec_write_field(dest, wp, 0 , 1);   // "0" power control: Not Present
		bitvec_write_field(dest, wp, 0, 2);    // CHANNEL_CODING_COMMAND 
		bitvec_write_field(dest, wp, 0, 1);    // TLLI_BLOCK_CHANNEL_CODING
		bitvec_write_field(dest, wp, 1 , 1);   // "1" Alpha : Present
		bitvec_write_field(dest, wp, 0, 4);    // Alpha
		bitvec_write_field(dest, wp, 0, 5);    // Gamma
		bitvec_write_field(dest, wp, 0, 1);    // TIMING_ADVANCE_INDEX_FLAG
		bitvec_write_field(dest, wp, 0, 1);    // TBF_STARTING_TIME_FLAG
	}

	if (wp%8)
		return wp/8+1;
	else
		return wp/8;
}


void write_ia_rest_octets_downlink_assignment(bitvec * dest, uint8_t tfi, uint32_t tlli)
{
	// GSM 04.08 10.5.2.16
	unsigned wp = 0;
	bitvec_write_field(dest, wp, 3, 2);    // "HH"
	bitvec_write_field(dest, wp, 1, 2);    // "01" Packet Downlink Assignment
	bitvec_write_field(dest, wp,tlli,32); // TLLI
	bitvec_write_field(dest, wp,0x1,1);   // switch TFI   : on
	bitvec_write_field(dest, wp,tfi,5);   // TFI
	bitvec_write_field(dest, wp,0x0,1);   // RLC acknowledged mode
	bitvec_write_field(dest, wp,0x0,1);   // ALPHA = present
	bitvec_write_field(dest, wp,0x0,5);   // GAMMA power control parameter
	bitvec_write_field(dest, wp,0x0,1);   // Polling Bit
	bitvec_write_field(dest, wp,0x1,1);   // TA_VALID ???
	bitvec_write_field(dest, wp,0x1,1);   // switch TIMING_ADVANCE_INDEX = on
	bitvec_write_field(dest, wp,0x0,4);   // TIMING_ADVANCE_INDEX
	bitvec_write_field(dest, wp,0x0,1);   // TBF Starting TIME present
	bitvec_write_field(dest, wp,0x0,1);   // P0 not present
	bitvec_write_field(dest, wp,0x1,1);   // P0 not present
	bitvec_write_field(dest, wp,0xb,4);
}

#if 0
void gprs_rlcmac_tx_ul_ack(uint8_t tfi, uint32_t tlli, RlcMacUplinkDataBlock_t * ul_data_block)
{
	bitvec *packet_uplink_ack_vec = bitvec_alloc(23);
	bitvec_unhex(packet_uplink_ack_vec, "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
	write_packet_uplink_ack(packet_uplink_ack_vec, tfi, tlli, ul_data_block->CV, ul_data_block->BSN);
	LOGP(DRLCMAC, LOGL_NOTICE, "TX: [PCU -> BTS] TFI: %u TLLI: 0x%08x Packet Uplink Ack\n", tfi, tlli);
	RlcMacDownlink_t * packet_uplink_ack = (RlcMacDownlink_t *)malloc(sizeof(RlcMacDownlink_t));
	LOGP(DRLCMAC, LOGL_NOTICE, "+++++++++++++++++++++++++ TX : Packet Uplink Ack +++++++++++++++++++++++++\n");
	decode_gsm_rlcmac_downlink(packet_uplink_ack_vec, packet_uplink_ack);
	LOGPC(DRLCMAC, LOGL_NOTICE, "\n");
	LOGP(DRLCMAC, LOGL_NOTICE, "------------------------- TX : Packet Uplink Ack -------------------------\n");
	free(packet_uplink_ack);
	gprs_rlcmac_enqueue_block(packet_uplink_ack_vec, 23);
	bitvec_free(packet_uplink_ack_vec);
}

void gprs_rlcmac_data_block_parse(gprs_rlcmac_tbf* tbf, RlcMacUplinkDataBlock_t * ul_data_block)
{
	// 1. Count the number of octets in header and number of LLC PDU in uplink data block.
	unsigned data_block_hdr_len = 3; // uplink data block header length: 3 mandatory octets
	unsigned llc_pdu_num = 0; // number of LLC PDU in data block

	
	if (ul_data_block->E_1 == 0) // Extension octet follows immediately
	{
		unsigned i = -1;
		do
		{
			i++;
			data_block_hdr_len += 1;
			llc_pdu_num++;
			
			// Singular case, TS 44.060 10.4.14
			if (ul_data_block->LENGTH_INDICATOR[i] == 0)
			{
				break;
			}
			
			// New LLC PDU starts after the current LLC PDU and continues until
			// the end of the RLC information field, no more extension octets.
			if ((ul_data_block->M[i] == 1)&&(ul_data_block->E[i] == 1))
			{
				llc_pdu_num++;
			}
		} while(ul_data_block->E[i] == 0); // there is another extension octet, which delimits the new LLC PDU
	}
	else
	{
		llc_pdu_num++;
	}
	if(ul_data_block->TI == 1) // TLLI field is present
	{
		tbf->tlli = ul_data_block->TLLI;
		data_block_hdr_len += 4; // TLLI length : 4 octets
		if (ul_data_block->PI == 1) // PFI is present if TI field indicates presence of TLLI
		{
			data_block_hdr_len += 1; // PFI length : 1 octet
		}
	}
	
	// 2. Extract all LLC PDU from uplink data block and send them to SGSN.
	unsigned llc_pdu_len = 0;
	unsigned data_octet_num = 0;

	for (unsigned num = 0; num < llc_pdu_num; num ++)
	{
		if (ul_data_block->E_1 == 0) // Extension octet follows immediately
		{
			// Singular case, TS 44.060 10.4.14
			if (ul_data_block->LENGTH_INDICATOR[num] == 0)
			{
				llc_pdu_len = UL_RLC_DATA_BLOCK_LEN - data_block_hdr_len;
			}
			else
			{
				llc_pdu_len = ul_data_block->LENGTH_INDICATOR[num];
			}
		}
		else
		{
			llc_pdu_len = UL_RLC_DATA_BLOCK_LEN - data_block_hdr_len;
		}
		
		for (unsigned i = tbf->llc_index; i < tbf->llc_index + llc_pdu_len; i++)
		{
			tbf->llc_frame[i] = ul_data_block->RLC_DATA[data_octet_num];
			data_octet_num++;
		}
		tbf->llc_index += llc_pdu_len;
		
		if (ul_data_block->E_1 == 0) // Extension octet follows immediately
		{
			// New LLC PDU starts after the current LLC PDU 
			if (ul_data_block->M[num] == 1)
			{
				gprs_rlcmac_tx_ul_ud(tbf);
				tbf->llc_index = 0;
				// New LLC PDU continues until the end of the RLC information field, no more extension octets.
				if ((ul_data_block->E[num] == 1))
				{
					llc_pdu_len = UL_RLC_DATA_BLOCK_LEN - data_block_hdr_len - data_octet_num;
					for (unsigned i = tbf->llc_index; i < tbf->llc_index + llc_pdu_len; i++)
					{
						tbf->llc_frame[i] = ul_data_block->RLC_DATA[data_octet_num];
						data_octet_num++;
					}
					tbf->llc_index += llc_pdu_len;
					num++;
				}
			}
		}
	}
}

/* Received Uplink RLC data block. */
int gprs_rlcmac_rcv_data_block(bitvec *rlc_block)
{
	struct gprs_rlcmac_tbf *tbf;

	LOGP(DRLCMAC, LOGL_NOTICE, "RX: [PCU <- BTS] Uplink Data Block\n");
	RlcMacUplinkDataBlock_t * ul_data_block = (RlcMacUplinkDataBlock_t *)malloc(sizeof(RlcMacUplinkDataBlock_t));
	LOGP(DRLCMAC, LOGL_NOTICE, "+++++++++++++++++++++++++ RX : Uplink Data Block +++++++++++++++++++++++++\n");
	decode_gsm_rlcmac_uplink_data(rlc_block, ul_data_block);
	LOGP(DRLCMAC, LOGL_NOTICE, "------------------------- RX : Uplink Data Block -------------------------\n");
	tbf = tbf_by_tfi(ul_data_block->TFI);
	if (!tbf) {
		return 0;
	}

	if (ul_data_block->TI == 1)
	{
		tbf->tlli = ul_data_block->TLLI;
	}

	switch (tbf->state) {
	case GPRS_RLCMAC_WAIT_DATA_SEQ_START: 
		if (ul_data_block->BSN == 0) {
			tbf->llc_index = 0;
			gprs_rlcmac_data_block_parse(tbf, ul_data_block);
			gprs_rlcmac_tx_ul_ack(tbf->tfi, tbf->tlli, ul_data_block);
			if (ul_data_block->CV == 0) {
				// Recieved last Data Block in this sequence.
				tbf->state = GPRS_RLCMAC_WAIT_NEXT_DATA_SEQ;
				gprs_rlcmac_tx_ul_ud(tbf);
			} else {
				tbf->bsn = ul_data_block->BSN;
				tbf->state = GPRS_RLCMAC_WAIT_NEXT_DATA_BLOCK;
			}
		}
		break;
	case GPRS_RLCMAC_WAIT_NEXT_DATA_BLOCK:
		if (tbf->bsn == (ul_data_block->BSN - 1)) {
			gprs_rlcmac_data_block_parse(tbf, ul_data_block);
			gprs_rlcmac_tx_ul_ack(tbf->tfi, tbf->tlli, ul_data_block);
			if (ul_data_block->CV == 0) {
				// Recieved last Data Block in this sequence.
				tbf->state = GPRS_RLCMAC_WAIT_NEXT_DATA_SEQ;
				gprs_rlcmac_tx_ul_ud(tbf);
			} else {
				tbf->bsn = ul_data_block->BSN;
				tbf->state = GPRS_RLCMAC_WAIT_NEXT_DATA_BLOCK;
			}
		} else {
			// Recieved Data Block with unexpected BSN.
			// We should try to find nesessary Data Block. 
			tbf->state = GPRS_RLCMAC_WAIT_NEXT_DATA_BLOCK;
		}
		break;
	case GPRS_RLCMAC_WAIT_NEXT_DATA_SEQ:
		// Now we just ignore all Data Blocks and wait next Uplink TBF
		break;
	}

	free(ul_data_block);
	return 1;
}
#endif

/* Received Uplink RLC control block. */
int gprs_rlcmac_rcv_control_block(bitvec *rlc_block, uint32_t fn)
{
	uint8_t tfi = 0;
	uint32_t tlli = 0;
	struct gprs_rlcmac_tbf *tbf;
//	struct gprs_rlcmac_tbf *ul_tbf;

	RlcMacUplink_t * ul_control_block = (RlcMacUplink_t *)malloc(sizeof(RlcMacUplink_t));
	LOGP(DRLCMAC, LOGL_NOTICE, "+++++++++++++++++++++++++ RX : Uplink Control Block +++++++++++++++++++++++++\n");
	decode_gsm_rlcmac_uplink(rlc_block, ul_control_block);
	LOGPC(DRLCMAC, LOGL_NOTICE, "\n");
	LOGP(DRLCMAC, LOGL_NOTICE, "------------------------- RX : Uplink Control Block -------------------------\n");
	switch (ul_control_block->u.MESSAGE_TYPE) {
	case MT_PACKET_CONTROL_ACK:
		tlli = ul_control_block->u.Packet_Control_Acknowledgement.TLLI;
		tbf = tbf_by_tlli(tlli);
		if (!tbf) {
			LOGP(DRLCMAC, LOGL_NOTICE, "PACKET CONTROL ACK with "
				"unknown TLLI=0x%x\n", tlli);
			return 0;
		}
		if (tbf->poll_fn != fn) {
			LOGP(DRLCMAC, LOGL_NOTICE, "PACKET CONTROL ACK for "
				"TFI=%d received at FN=%d, but was requested "
				"for FN=%d\n", tbf->tfi, fn, tbf->poll_fn);
		} else {
			LOGP(DRLCMAC, LOGL_NOTICE, "PACKET CONTROL ACK received on FN=%d as expected\n", fn);
		}
		LOGP(DRLCMAC, LOGL_NOTICE, "RX: [PCU <- BTS] TFI: %u TLLI: 0x%08x Packet Control Ack\n", tbf->tfi, tbf->tlli);
		LOGP(DRLCMAC, LOGL_NOTICE, "TBF: [UPLINK] END TFI: %u TLLI: 0x%08x \n", tbf->tfi, tbf->tlli);
		tbf_free(tbf);
		break;
	case MT_PACKET_DOWNLINK_ACK_NACK:
		tfi = ul_control_block->u.Packet_Downlink_Ack_Nack.DOWNLINK_TFI;
		tbf = tbf_by_tfi(tfi);
		if (!tbf) {
			return 0;
		}
		LOGP(DRLCMAC, LOGL_NOTICE, "RX: [PCU <- BTS] TFI: %u TLLI: 0x%08x Packet Downlink Ack/Nack\n", tbf->tfi, tbf->tlli);
		tlli = tbf->tlli;
		LOGP(DRLCMAC, LOGL_NOTICE, "TBF: [DOWNLINK] END TFI: %u TLLI: 0x%08x \n", tbf->tfi, tbf->tlli);
		tbf_free(tbf);
		break;
	}
	free(ul_control_block);
	return 1;
}

int gprs_rlcmac_rcv_block(uint8_t *data, uint8_t len, uint32_t fn)
{
	unsigned payload = data[0] >> 6;
	bitvec *block;
	int rc = 0;

	switch (payload) {
	case GPRS_RLCMAC_DATA_BLOCK:
		rc = gprs_rlcmac_rcv_data_block_acknowledged(data, len);
		break;
	case GPRS_RLCMAC_CONTROL_BLOCK:
		block = bitvec_alloc(len);
		if (!block)
			return -ENOMEM;
		bitvec_unpack(block, data);
		rc = gprs_rlcmac_rcv_control_block(block, fn);
		bitvec_free(block);
		break;
	case GPRS_RLCMAC_CONTROL_BLOCK_OPT:
		LOGP(DRLCMAC, LOGL_NOTICE, "GPRS_RLCMAC_CONTROL_BLOCK_OPT block payload is not supported.\n");
	default:
		LOGP(DRLCMAC, LOGL_NOTICE, "Unknown RLCMAC block payload.\n");
		rc = -EINVAL;
	}

	return rc;
}

#if 0
int select_pdch(uint8_t *_trx, uint8_t *_ts)
{
	struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts;
	uint8_t trx, ts;

	for (trx = 0; trx < 8; trx++) {
		for (ts = 0; ts < 8; ts++) {
			if (bts->trx[trx].pdch[ts].enable) {
				*_trx = trx;
				*_ts = ts;
				return 0;
			}
		}
	}

	return -EBUSY;
}
#endif

int gprs_rlcmac_rcv_rach(uint8_t ra, uint32_t Fn, int16_t qta)
{
	struct gprs_rlcmac_tbf *tbf;
	uint8_t trx, ts;
	int tfi, usf; /* must be signed */

	// Create new TBF
	tfi = tfi_alloc(&trx, &ts);
	if (tfi < 0) {
		LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH ressource\n");
		/* FIXME: send reject */
		return -EBUSY;
	}
	usf = find_free_usf(trx, ts);
	if (usf < 0) {
		LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH ressource for USF\n");
		/* FIXME: send reject */
		return -EBUSY;
	}
	tbf = tbf_alloc(tfi, trx, ts);
	if (qta < 0)
		qta = 0;
	if (qta > 252)
		qta = 252;
	tbf->ta = qta >> 2;
	tbf->direction = GPRS_RLCMAC_UL_TBF;
	tbf->dir.ul.usf = usf;
	tbf->state = GPRS_RLCMAC_FLOW;
	tbf_timer_start(tbf, 3169, T3169);
	LOGP(DRLCMAC, LOGL_NOTICE, "TBF: [UPLINK] START TFI: %u\n", tbf->tfi);
	LOGP(DRLCMAC, LOGL_NOTICE, "RX: [PCU <- BTS] TFI: %u RACH qbit-ta=%d ra=%d, Fn=%d (%d,%d,%d)\n", tbf->tfi, qta, ra, Fn, (Fn / (26 * 51)) % 32, Fn % 51, Fn % 26);
	LOGP(DRLCMAC, LOGL_NOTICE, "TX: [PCU -> BTS] TFI: %u Packet Immidiate Assignment\n", tbf->tfi);
	bitvec *immediate_assignment = bitvec_alloc(23);
	bitvec_unhex(immediate_assignment, "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
	int len = write_immediate_assignment(immediate_assignment, 0, ra, Fn, tbf->ta, tbf->arfcn, tbf->ts, tbf->tsc, tbf->tfi, usf);
	pcu_l1if_tx_agch(immediate_assignment, len);
	bitvec_free(immediate_assignment);

	return 0;
}

// Send RLC data to OpenBTS.
void gprs_rlcmac_tx_dl_data_block(uint32_t tlli, uint8_t tfi, uint8_t *pdu, int start_index, int end_index, uint8_t bsn, uint8_t fbi)
{
	int spare_len = 0;
	bitvec *data_block_vector = bitvec_alloc(BLOCK_LEN);
	bitvec_unhex(data_block_vector, "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
	RlcMacDownlinkDataBlock_t * data_block = (RlcMacDownlinkDataBlock_t *)malloc(sizeof(RlcMacDownlinkDataBlock_t));
	data_block->PAYLOAD_TYPE = 0;
	data_block->RRBP = 0;
	data_block->SP = 1;
	data_block->USF = 1;
	data_block->PR = 0;
	data_block->TFI = tfi;
	data_block->FBI = fbi;
	data_block->BSN = bsn;
	
	// Last RLC data block of current LLC PDU
	if (fbi == 1)
	{
		data_block->E_1 = 0;
		data_block->M[0] = 0;
		data_block->E[0] = 1;
		// Singular case, TS 44.060 10.4.14
		if ((end_index - start_index) == (BLOCK_LEN - 3))
		{
			data_block->FBI = 0;
			data_block->LENGTH_INDICATOR[0] = 0;
			spare_len =  0;
			end_index--;
		}
		else
		 {
			data_block->LENGTH_INDICATOR[0] = end_index-start_index;
			spare_len = BLOCK_LEN - 4 - data_block->LENGTH_INDICATOR[0];
		}
	}
	else
	{
		data_block->E_1 = 1; 
	}

	int data_oct_num = 0;  
	int i = 0;
	// Pack LLC PDU into RLC data field 
	for(i = start_index; i < end_index; i++) {
		data_block->RLC_DATA[data_oct_num] = pdu[i];
		data_oct_num++;
	}
	// Fill spare bits
	for(i = data_oct_num; i < data_oct_num + spare_len; i++) {
		data_block->RLC_DATA[i] = 0x2b;
	}
	LOGP(DRLCMAC, LOGL_NOTICE, "TX: [PCU -> BTS] Downlink Data Block\n");
	LOGP(DRLCMAC, LOGL_NOTICE, "+++++++++++++++++++++++++ TX : Downlink Data Block +++++++++++++++++++++++++\n");
	encode_gsm_rlcmac_downlink_data(data_block_vector, data_block);
	LOGP(DRLCMAC, LOGL_NOTICE, "------------------------- TX : Downlink Data Block -------------------------\n");
	free(data_block);
	gprs_rlcmac_enqueue_block(data_block_vector, BLOCK_LEN);
	bitvec_free(data_block_vector);
	
	// Singular case, TS 44.060 10.4.14
	if ((fbi == 1)&&((end_index + 1 - start_index) == (BLOCK_LEN - 3)))
	{
		gprs_rlcmac_tx_dl_data_block(tlli, tfi, pdu, end_index, end_index+1, bsn+1, fbi);
	}
}

int gprs_rlcmac_segment_llc_pdu(struct gprs_rlcmac_tbf *tbf)
{
	int fbi = 0;
	int bsn = 0;
	int num_blocks = 0; // number of RLC data blocks necessary for LLC PDU transmission 


	// LLC PDU fits into one RLC data block with optional LI field.
	if (tbf->llc_index < BLOCK_LEN - 4)
	{
		fbi = 1;
		gprs_rlcmac_tx_dl_data_block(tbf->tlli, tbf->tfi, tbf->llc_frame, 0, tbf->llc_index, bsn, fbi);
	}
	// Necessary several RLC data blocks for transmit LLC PDU.
	else
	{
		// length of RLC data field in block (no optional octets)
		int block_data_len = BLOCK_LEN - 3; 
		
		// number of blocks with 20 octets length RLC data field
		num_blocks = tbf->llc_index/block_data_len; 
		
		// rest of LLC PDU, which doesn't fit into data blocks with 20 octets RLC data field
		int rest_len = tbf->llc_index%BLOCK_DATA_LEN; 
		if (rest_len > 0)
		{
			// add one block for transmission rest of LLC PDU
			num_blocks++;
		}

		int start_index = 0;
		int end_index = 0;

		// Transmit all RLC data blocks of current LLC PDU to MS
		for (bsn = 0; bsn < num_blocks; bsn++)
		{
			if (bsn == num_blocks-1)
			{
				if (rest_len > 0)
				{
					block_data_len = rest_len;
				}
				fbi = 1;
			}
			end_index = start_index + block_data_len;
			gprs_rlcmac_tx_dl_data_block(tbf->tlli, tbf->tfi, tbf->llc_frame, start_index, end_index, bsn, fbi);
			start_index += block_data_len;
		}
	}

	return 0;
}

/* Send Uplink unit-data to SGSN. */
void gprs_rlcmac_tx_ul_ud(gprs_rlcmac_tbf *tbf)
{
	const uint8_t qos_profile = QOS_PROFILE;
	struct msgb *llc_pdu;
	unsigned msg_len = NS_HDR_LEN + BSSGP_HDR_LEN + tbf->llc_index;

	LOGP(DBSSGP, LOGL_NOTICE, "TX: [PCU -> SGSN ] TFI: %u TLLI: 0x%08x DataLen: %u", tbf->tfi, tbf->tlli, tbf->llc_index);
	//LOGP(DBSSGP, LOGL_NOTICE, " Data = ");
	//for (unsigned i = 0; i < tbf->llc_index; i++)
	//	LOGPC(DBSSGP, LOGL_NOTICE, "%02x ", tbf->llc_frame[i]);
	
	bctx->cell_id = CELL_ID;
	bctx->nsei = NSEI;
	bctx->ra_id.mnc = MNC;
	bctx->ra_id.mcc = MCC;
	bctx->ra_id.lac = PCU_LAC;
	bctx->ra_id.rac = PCU_RAC;
	bctx->bvci = BVCI;

	llc_pdu = msgb_alloc_headroom(msg_len, msg_len,"llc_pdu");
	msgb_tvlv_push(llc_pdu, BSSGP_IE_LLC_PDU, sizeof(uint8_t)*tbf->llc_index, tbf->llc_frame);
	bssgp_tx_ul_ud(bctx, tbf->tlli, &qos_profile, llc_pdu);
}

void gprs_rlcmac_downlink_assignment(gprs_rlcmac_tbf *tbf)
{
	LOGP(DRLCMAC, LOGL_NOTICE, "TX: [PCU -> BTS] TFI: %u TLLI: 0x%08x Immidiate Assignment (CCCH)\n", tbf->tfi, tbf->tlli);
	bitvec *immediate_assignment = bitvec_alloc(23);
	bitvec_unhex(immediate_assignment, "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
	int len = write_immediate_assignment(immediate_assignment, 1, 125, get_current_fn(), tbf->ta, tbf->arfcn, tbf->ts, tbf->tsc, tbf->tfi, 0, tbf->tlli);
	pcu_l1if_tx_agch(immediate_assignment, len);
	bitvec_free(immediate_assignment);
	tbf_gsm_timer_start(tbf, 0, 120);
}

void gprs_rlcmac_packet_downlink_assignment(gprs_rlcmac_tbf *tbf)
{
	LOGP(DRLCMAC, LOGL_NOTICE, "TX: [PCU -> BTS] TFI: %u TLLI: 0x%08x Packet DL Assignment\n", tbf->tfi, tbf->tlli);
	bitvec *packet_downlink_assignment_vec = bitvec_alloc(23);
	bitvec_unhex(packet_downlink_assignment_vec, "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
	write_packet_downlink_assignment(packet_downlink_assignment_vec, tbf->tfi, tbf->tlli, tbf->arfcn, tbf->ts, tbf->ta, tbf->tsc);
	RlcMacDownlink_t * packet_downlink_assignment = (RlcMacDownlink_t *)malloc(sizeof(RlcMacDownlink_t));
	LOGP(DRLCMAC, LOGL_NOTICE, "+++++++++++++++++++++++++ TX : Packet Downlink Assignment +++++++++++++++++++++++++\n");
	decode_gsm_rlcmac_downlink(packet_downlink_assignment_vec, packet_downlink_assignment);
	LOGPC(DRLCMAC, LOGL_NOTICE, "\n");
	LOGP(DRLCMAC, LOGL_NOTICE, "------------------------- TX : Packet Downlink Assignment -------------------------\n");
	free(packet_downlink_assignment);
	gprs_rlcmac_enqueue_block(packet_downlink_assignment_vec, 23);
	bitvec_free(packet_downlink_assignment_vec);
	tbf_gsm_timer_start(tbf, 0, 120);
}