ms: Support new and old TLLIs

According to the specification (GSM 04.08/24.008, 4.7.1.5) after a
new P-TMSI has been assigned, the old P-TMSI must be kept basically
until it has been used by both sides. Since the TLLI will be derived
from the P-TMSI, the old TLLI must also be kept until the new TLLI
has been used by both MS and SGSN.

This commit modifies the TLLI handling of GprsMs accordingly.
set_tlli() is only used with TLLIs derived from MS messages,
confirm_tlli() is used with TLLIs derived from messages received from
the SGSN. tlli() returns the value set by the MS. check_tlli()
matches each of the TLLI used by either MS or SGSN as well as the old
TLLI until it has been confirmed.

Sponsored-by: On-Waves ehf
diff --git a/tests/tbf/TbfTest.cpp b/tests/tbf/TbfTest.cpp
index d5bcb67..2ec715b 100644
--- a/tests/tbf/TbfTest.cpp
+++ b/tests/tbf/TbfTest.cpp
@@ -51,6 +51,8 @@
 static void test_tbf_tlli_update()
 {
 	BTS the_bts;
+	GprsMs *ms, *ms_new;
+
 	the_bts.bts_data()->alloc_algorithm = alloc_algorithm_a;
 	the_bts.bts_data()->trx[0].pdch[2].enable();
 	the_bts.bts_data()->trx[0].pdch[3].enable();
@@ -63,6 +65,7 @@
 						0, 0, 0);
 	dl_tbf->update_tlli(0x2342);
 	dl_tbf->tlli_mark_valid();
+	dl_tbf->update_ms(0x2342);
 	dl_tbf->ta = 4;
 	the_bts.timing_advance()->remember(0x2342, dl_tbf->ta);
 
@@ -71,23 +74,33 @@
 						0, 0, 0);
 	ul_tbf->update_tlli(0x2342);
 	ul_tbf->tlli_mark_valid();
-	
 
-	OSMO_ASSERT(the_bts.dl_tbf_by_tlli(0x2342) == dl_tbf);
-	OSMO_ASSERT(the_bts.ul_tbf_by_tlli(0x2342) == ul_tbf);
+	ms = the_bts.ms_by_tlli(0x2342);
 
+	OSMO_ASSERT(ms != NULL);
+	OSMO_ASSERT(ms->dl_tbf() == dl_tbf);
+	OSMO_ASSERT(ms->ul_tbf() == ul_tbf);
 
 	/*
 	 * Now check.. that DL changes and that the timing advance
 	 * has changed.
 	 */
 	dl_tbf->update_tlli(0x4232);
-	OSMO_ASSERT(!the_bts.dl_tbf_by_tlli(0x2342));
-	OSMO_ASSERT(!the_bts.ul_tbf_by_tlli(0x2342));
+	ms->confirm_tlli(0x4232);
 
-	
-	OSMO_ASSERT(the_bts.dl_tbf_by_tlli(0x4232) == dl_tbf);
-	OSMO_ASSERT(the_bts.ul_tbf_by_tlli(0x4232) == ul_tbf);
+	/* It is still there, since the new TLLI has not been used for UL yet */
+	ms_new = the_bts.ms_by_tlli(0x2342);
+	OSMO_ASSERT(ms == ms_new);
+
+	ms_new = the_bts.ms_by_tlli(0x4232);
+	OSMO_ASSERT(ms == ms_new);
+	OSMO_ASSERT(ms->dl_tbf() == dl_tbf);
+	OSMO_ASSERT(ms->ul_tbf() == ul_tbf);
+
+	/* Now use the new TLLI for UL */
+	ms->set_tlli(0x4232);
+	ms_new = the_bts.ms_by_tlli(0x2342);
+	OSMO_ASSERT(ms_new == NULL);
 
 	OSMO_ASSERT(the_bts.timing_advance()->recall(0x4232) == 4);
 }
@@ -318,7 +331,7 @@
 
 		snprintf(imsi, sizeof(imsi)-1, "001001%9d", i);
 
-		rc = gprs_rlcmac_dl_tbf::handle(bts, tlli, imsi, ms_class,
+		rc = gprs_rlcmac_dl_tbf::handle(bts, tlli, 0, imsi, ms_class,
 			delay_csec, buf, sizeof(buf));
 
 		if (rc < 0)
diff --git a/tests/tbf/TbfTest.err b/tests/tbf/TbfTest.err
index 3a58351..32f9efc 100644
--- a/tests/tbf/TbfTest.err
+++ b/tests/tbf/TbfTest.err
@@ -6,6 +6,8 @@
 - Assign downlink TS=2
 - Setting Control TS 2
 TBF(TFI=0 TLLI=0x00000000 DIR=DL STATE=NULL) changing tlli from TLLI=0x00000000 TLLI=0x00002342 ul_changed=0
+Creating MS object, TLLI = 0x00002342
+Attaching TBF to MS object, TLLI = 0x00002342, TBF = TBF(TFI=0 TLLI=0x00002342 DIR=DL STATE=NULL)
 ********** TBF starts here **********
 Allocating UL TBF: TFI=0 TRX=0 MS_CLASS=0
 Slot Allocation (Algorithm A) for class 0
@@ -13,8 +15,11 @@
 - Skipping TS 1, because not enabled
 - Assign uplink TS=2 USF=0
 - Setting Control TS 2
+Attaching TBF to MS object, TLLI = 0x00002342, TBF = TBF(TFI=0 TLLI=0x00000000 DIR=UL STATE=NULL)
 TBF(TFI=0 TLLI=0x00000000 DIR=UL STATE=NULL) changing tlli from TLLI=0x00000000 TLLI=0x00002342 ul_changed=0
-TBF(TFI=0 TLLI=0x00002342 DIR=DL STATE=NULL) changing tlli from TLLI=0x00002342 TLLI=0x00004232 ul_changed=1
+TBF(TFI=0 TLLI=0x00002342 DIR=DL STATE=NULL) changing tlli from TLLI=0x00002342 TLLI=0x00004232 ul_changed=0
+The MS object cannot fully confirm an unexpected TLLI: 0x00004232, partly confirmed
+Modifying MS object, TLLI: 0x00002342 -> 0x00004232, already confirmed partly
 Searching for first unallocated TFI: TRX=0 first TS=4
  Found TFI=0.
 ********** TBF starts here **********