L1CTL Test: Get all the way up to dedicated channel establishment

The code is now a proof of concept to establish a dedicated channel
via virt_um, at which point we can exchange L2 messages with the BTS.
diff --git a/lapd/L1CTL_Test.ttcn b/lapd/L1CTL_Test.ttcn
index faaf4ae..f8c4fdc 100644
--- a/lapd/L1CTL_Test.ttcn
+++ b/lapd/L1CTL_Test.ttcn
@@ -18,6 +18,7 @@
 		port L1CTL_PT L1CTL;
 	};
 
+	/* for generating RESET_REQ */
 	template L1ctlUlMessage t_L1ctlResetReq(template L1ctlResetType rst_type) := {
 		header := t_L1ctlHeader(L1CTL_RESET_REQ),
 		ul_info := omit,
@@ -29,12 +30,130 @@
 		}
 	};
 
-	//template UD_send_data
+	/* for generating FBSB_REQ */
+	template L1ctlUlMessage t_L1CTL_FBSB_REQ(template Arfcn arfcn, template L1ctlFbsbFlags flags, uint8_t sync_info_idx, L1ctlCcchMode ccch_mode, GsmRxLev rxlev_exp) := {
+		header := t_L1ctlHeader(L1CTL_FBSB_REQ),
+		ul_info := omit,
+		payload := {
+			fbsb_req := {
+				arfcn := arfcn,
+				timeout_tdma_frames := 10,
+				freq_err_thresh1 := 10000,
+				freq_err_thresh2 := 800,
+				num_freqerr_avg := 3,
+				flags := flags,
+				sync_info_idx := sync_info_idx,
+				ccch_mode := ccch_mode,
+				rxlev_exp := rxlev_exp
+			}
+		}
+	};
+
+	/* for matching against incoming FBSB_CONF */
+	template L1ctlDlMessage t_L1CTL_FBSB_CONF(template uint8_t result) := {
+		header := t_L1ctlHeader(L1CTL_FBSB_CONF),
+		dl_info := ?,
+		payload := {
+			fbsb_conf := {
+				initial_freq_err := ?,
+				result := result,
+				bsic := ?
+			}
+		}
+	};
+
+	template L1ctlUlMessage t_L1CTL_RACH_REQ(uint8_t ra, uint8_t combined, uint16_t offset) := {
+		header := t_L1ctlHeader(L1CTL_RACH_REQ),
+		ul_info := {
+			chan_nr := t_RslChanNr_RACH(0),
+			link_id := ts_RslLinkID_DCCH(0),
+			padding := '0000'O
+		},
+		payload := {
+			rach_req := {
+				ra := ra,
+				combined := combined,
+				offset := offset
+			}
+		}
+	}
+
+	template L1ctlUlMessage t_L1CTL_DM_EST_REQ(Arfcn arfcn, RslChannelNr chan_nr, GsmTsc tsc) := {
+		header := t_L1ctlHeader(L1CTL_DM_EST_REQ),
+		ul_info := {
+			chan_nr := chan_nr,
+			link_id := ts_RslLinkID_DCCH(0),
+			padding := '0000'O
+		},
+		payload := {
+			dm_est_req := {
+				tsc := tsc,
+				h := 0,
+				arfcn := arfcn,
+				hopping := omit,
+				tch_mode := 0,
+				audio_mode := t_L1CTL_AudioModeNone
+			}
+		}
+	}
+
+	template L1ctlUlMessage t_L1CTL_DM_REL_REQ(RslChannelNr chan_nr) := {
+		header := t_L1ctlHeader(L1CTL_DM_EST_REQ),
+		ul_info := {
+			chan_nr := chan_nr,
+			link_id := ts_RslLinkID_DCCH(0),
+			padding := '0000'O
+		},
+		payload := {
+			other := ''O
+		}
+	}
+
+	template L1ctlUlMessage t_L1CTL_DATA_REQ(template RslChannelNr chan_nr, template RslLinkId link_id, octetstring l2_data) := {
+		header := t_L1ctlHeader(L1CTL_DATA_REQ),
+		ul_info := {
+			chan_nr := chan_nr,
+			link_id := link_id,
+			padding := '0000'O
+		},
+		payload := {
+			other := l2_data
+		}
+	}
+
+	/* for matching against incoming RACH_CONF */
+	template L1ctlDlMessage t_L1CTL_RACH_CONF := {
+		header := t_L1ctlHeader(L1CTL_RACH_CONF),
+		dl_info := ?,
+		payload := ?
+	};
+
+	/* for matching against incoming RACH_CONF */
+	template L1ctlDlMessage t_L1CTL_DATA_IND(template RslChannelNr chan_nr) := {
+		header := t_L1ctlHeader(L1CTL_DATA_IND),
+		dl_info := {
+			chan_nr := chan_nr,
+			link_id := ?,
+			arfcn := ?,
+			frame_nr := ?,
+			rx_level := ?,
+			snr := ?,
+			num_biterr := ?,
+			fire_crc := ?
+		},
+		payload := {
+			data_ind := ?
+		}
+	};
+
 
 	function f_init() runs on dummy_CT {
 		map(self:L1CTL, system:L1CTL);
 		L1CTL.send(L1CTL_connect:{path:=l1ctl_sock_path});
 		L1CTL.receive(L1CTL_connect_result:{result_code := SUCCESS, err:=omit});
+
+		L1CTL.send(t_L1ctlResetReq(L1CTL_RES_T_SCHED));
+		L1CTL.receive;
 	}
 
 	testcase TC_si1() runs on dummy_CT {
@@ -48,10 +167,97 @@
 		setverdict(pass);
 	}
 
-	testcase TC_l1ctl() runs on dummy_CT {
-		f_init();
-		L1CTL.send(t_L1ctlResetReq(L1CTL_RES_T_FULL));
+	function f_L1CTL_FBSB(L1CTL_PT pt, Arfcn arfcn, L1ctlCcchMode ccch_mode := CCCH_MODE_COMBINED) {
+		timer T := 5.0;
+		pt.send(t_L1CTL_FBSB_REQ(arfcn, t_L1CTL_FBSB_F_ALL, 0, ccch_mode, 0));
+		T.start
+		alt {
+			[] pt.receive(t_L1CTL_FBSB_CONF(0)) {};
+			[] pt.receive { repeat; };
+			[] T.timeout { setverdict(fail, "Timeout in FBSB") };
+		}
 	}
+
+	function f_L1CTL_RACH(L1CTL_PT pt, uint8_t ra, uint8_t combined := 1, uint16_t offset := 0) return GsmFrameNumber {
+		var L1ctlDlMessage rc;
+		var GsmFrameNumber fn;
+		timer T := 2.0;
+		T.start
+		pt.send(t_L1CTL_RACH_REQ(ra, 0, 0))
+		alt {
+			[] pt.receive(t_L1CTL_RACH_CONF) -> value rc { fn := rc.dl_info.frame_nr };
+			[] pt.receive { repeat; };
+			[] T.timeout { setverdict(fail, "Timeout in RACH") };
+		}
+		return fn;
+	}
+
+	template GsmRrMessage t_RR_IMM_ASS(uint8_t ra, uint8_t fn) := {
+		header := t_RrHeader(IMMEDIATE_ASSIGNMENT, ?),
+		payload := {
+			imm_ass := {
+				ded_or_tbf := ?,
+				page_mode := ?,
+				chan_desc := ?,
+				pkt_chan_desc := omit,
+				req_ref := f_compute_ReqRef(ra, fn),
+				timing_advance := ?,
+				mobile_allocation := ?
+			}
+		}
+	};
+
+	function f_L1CTL_WAIT_IMM_ASS(L1CTL_PT pt, uint8_t ra, GsmFrameNumber rach_fn) return ImmediateAssignment {
+		var L1ctlDlMessage dl;
+		var GsmRrMessage rr;
+		timer T := 10.0;
+		T.start;
+		alt {
+			[] pt.receive(t_L1CTL_DATA_IND(t_RslChanNr_PCH_AGCH(0))) -> value dl {
+				rr := dec_GsmRrMessage(dl.payload.data_ind.payload);
+				log("PCH/AGCN DL RR: ", rr);
+				if (match(rr, t_RR_IMM_ASS(ra, rach_fn))) {
+					log("Received IMM.ASS for our RACH!");
+				} else {
+					repeat;
+				}
+			};
+			[] pt.receive { repeat };
+			[] T.timeout { setverdict(fail, "Timeout waiting for IMM ASS") };
+		}
+		T.stop;
+		return rr.payload.imm_ass;
+	}
+
+	/* Send DM_EST_REQ from parameters derived from IMM ASS */
+	function f_L1CTL_DM_EST_REQ_IA(L1CTL_PT pt, ImmediateAssignment imm_ass) {
+		pt.send(t_L1CTL_DM_EST_REQ({ false, imm_ass.chan_desc.arfcn }, imm_ass.chan_desc.chan_nr, imm_ass.chan_desc.tsc));
+	}
+
+	testcase TC_l1ctl() runs on dummy_CT {
+		var GsmFrameNumber rach_fn;
+		var uint8_t ra := 23;
+		var ImmediateAssignment imm_ass;
+
+		f_init();
+		/* send FB/SB req to sync to cell */
+		f_L1CTL_FBSB(L1CTL, { false, 871 }, CCCH_MODE_COMBINED);
+		/* send RACH request and obtain FN at which it was sent */
+		rach_fn := f_L1CTL_RACH(L1CTL, ra);
+		/* wait for receiving matching IMM ASS */
+		imm_ass := f_L1CTL_WAIT_IMM_ASS(L1CTL, ra, rach_fn)
+		/* send DM_EST_REQ */
+		f_L1CTL_DM_EST_REQ_IA(L1CTL, imm_ass);
+
+		L1CTL.send(t_L1CTL_DATA_REQ(imm_ass.chan_desc.chan_nr, ts_RslLinkID_DCCH(0), '000102030405060708090a0b0c0d0e0f10111213141516'O));
+		L1CTL.send(t_L1CTL_DATA_REQ(imm_ass.chan_desc.chan_nr, ts_RslLinkID_DCCH(0), '000102030405060708090a0b0c0d0e0f10111213141516'O));
+		L1CTL.send(t_L1CTL_DATA_REQ(imm_ass.chan_desc.chan_nr, ts_RslLinkID_DCCH(0), '000102030405060708090a0b0c0d0e0f10111213141516'O));
+
+		L1CTL.send(t_L1CTL_DM_REL_REQ(imm_ass.chan_desc.chan_nr));
+
+		setverdict(pass);
+	}
+
 	control {
 		execute(TC_si1());
 		execute(TC_l1ctl());
diff --git a/lapd/L1CTL_Types.ttcn b/lapd/L1CTL_Types.ttcn
index a5a6bf8..2b9219c 100644
--- a/lapd/L1CTL_Types.ttcn
+++ b/lapd/L1CTL_Types.ttcn
@@ -78,8 +78,6 @@
 		padding := '0000'O
 	};
 
-	type uint8_t RslLinkId;
-
 	type record L1ctlDlInfo {
 		RslChannelNr	chan_nr,
 		RslLinkId	link_id,
@@ -113,6 +111,8 @@
 		boolean		rx_traffic_ind
 	} with { variant "" };
 
+	template L1ctlAudioMode t_L1CTL_AudioModeNone := { '0000'B, false, false, false, false };
+
 	type record L1ctlTchModeConf {
 		L1ctlTchMode	tch_mode,
 		L1ctlAudioMode	audio_mode,
@@ -168,6 +168,13 @@
 		boolean		fb0
 	} with { variant "" };
 
+	template L1ctlFbsbFlags t_L1CTL_FBSB_F_ALL := {
+		padding := '00000'B,
+		sb := true,
+		fb1 := true,
+		fb0 := true
+	};
+
 	type record L1ctlFbsbReq {
 		Arfcn		arfcn,
 		uint16_t	timeout_tdma_frames,