blob: 2423715a31dab2a63d1d0866a9816630ab773a41 [file] [log] [blame]
Harald Welte34b5a952019-05-27 11:54:11 +02001/* dual-faced port that wraps an Unixdomain port and encodes/decodes L1CTL
2 * (C) 2017-2019 Harald Welte <laforge@gnumonks.org>
3 * contributions by sysmocom - s.f.m.c. GmbH
4 * All rights reserved.
5 *
6 * Released under the terms of GNU General Public License, Version 2 or
7 * (at your option) any later version.
8 *
9 * SPDX-License-Identifier: GPL-2.0-or-later
10 */
11
Harald Welte52c713c2017-07-16 15:44:44 +020012module L1CTL_PortType {
13 import from L1CTL_Types all;
14 import from UD_PortType all;
15 import from UD_Types all;
Harald Welte9e4725d2017-07-16 23:18:09 +020016 import from Osmocom_Types all;
Harald Weltee613f962018-04-18 22:38:16 +020017 import from Osmocom_Types all;
Harald Welte9e4725d2017-07-16 23:18:09 +020018 import from GSM_Types all;
Harald Welte9419c8a2017-07-30 04:07:05 +020019 import from GSM_RR_Types all;
Harald Welte6fc784e2018-02-25 23:31:37 +010020 import from L1CTL_PortType_CtrlFunct all;
Harald Welte52c713c2017-07-16 15:44:44 +020021
22 type record L1CTL_connect {
23 charstring path
24 }
25
26 type record L1CTL_connect_result {
27 UD_Result_code result_code optional,
28 charstring err optional
29 }
30
Harald Weltef68765d2017-08-20 22:54:57 +020031 modulepar {
32 charstring m_l1ctl_sock_path := "/tmp/osmocom_l2";
33 }
34
Harald Welte6fc784e2018-02-25 23:31:37 +010035 function f_L1CTL_getMsgLen(in octetstring stream, inout ro_integer args) return integer {
36 var integer stream_len := lengthof(stream);
37 var integer len;
38 if (stream_len < 2) {
Harald Welte6fc784e2018-02-25 23:31:37 +010039 return -1;
40 }
41 len := 2 + oct2int(substr(stream, 0, 2));
Harald Welte6fc784e2018-02-25 23:31:37 +010042 return len;
43 }
44
Pau Espin Pedrol752ffd52018-06-07 13:55:45 +020045 function f_L1CTL_FBSB(L1CTL_PT pt, Arfcn arfcn, L1ctlCcchMode ccch_mode := CCCH_MODE_COMBINED, integer rxlev_exp := 57) {
Harald Welte3757e602018-03-10 17:12:02 +010046 timer T := 15.0;
Harald Welte8fe9eba2018-03-09 17:03:49 +010047 for (var integer i := 0; i < 10; i := i+1) {
Stefan Sperling02585902018-08-30 17:03:50 +020048 var L1ctlDlMessage dl;
Pau Espin Pedrol752ffd52018-06-07 13:55:45 +020049 pt.send(ts_L1CTL_FBSB_REQ(arfcn, valueof(t_L1CTL_FBSB_F_ALL), 0, ccch_mode, rxlev_exp));
Harald Welte8fe9eba2018-03-09 17:03:49 +010050 T.start
51 alt {
Harald Weltef8df4cb2018-03-10 15:15:08 +010052 [] pt.receive(tr_L1CTL_FBSB_CONF(0)) { return; };
Stefan Sperling02585902018-08-30 17:03:50 +020053 [i >= 9] pt.receive(tr_L1CTL_FBSB_CONF(?)) -> value dl {
54 setverdict(fail, "FBSB Failed with non-zero return code ", dl.payload.fbsb_conf.result);
Daniel Willmanne4ff5372018-07-05 17:35:03 +020055 mtc.stop;
Harald Welte7d7d26c2018-02-22 18:52:28 +010056 };
Harald Weltef8df4cb2018-03-10 15:15:08 +010057 [] pt.receive(tr_L1CTL_FBSB_CONF(?)) {
Harald Welte3757e602018-03-10 17:12:02 +010058 f_sleep(1.0);
Harald Weltef8df4cb2018-03-10 15:15:08 +010059 }
Harald Welte9e4725d2017-07-16 23:18:09 +020060 [] pt.receive { repeat; };
Harald Welte3757e602018-03-10 17:12:02 +010061 [] T.timeout {
Vadim Yanitskiyde8af3d2019-06-12 01:34:05 +070062 setverdict(fail, "Timeout waiting for L1CTL_FBSB_CONF");
Daniel Willmanne4ff5372018-07-05 17:35:03 +020063 mtc.stop;
Harald Welte3757e602018-03-10 17:12:02 +010064 };
65 }
66 }
67 }
68
69 function f_L1CTL_CCCH_MODE(L1CTL_PT pt, L1ctlCcchMode ccch_mode) {
70 timer T := 2.0;
71 pt.send(ts_L1CTL_CCCH_MODE_REQ(ccch_mode));
72 T.start;
73 alt {
74 [] pt.receive(tr_L1CTL_CCCH_MODE_CONF) { }
75 [] pt.receive { repeat; }
76 [] T.timeout {
Vadim Yanitskiyde8af3d2019-06-12 01:34:05 +070077 setverdict(fail, "Timeout waiting for L1CTL_CCCH_MODE_CONF");
Daniel Willmanne4ff5372018-07-05 17:35:03 +020078 mtc.stop;
Harald Welte8fe9eba2018-03-09 17:03:49 +010079 }
Harald Welte9e4725d2017-07-16 23:18:09 +020080 }
81 }
82
Vadim Yanitskiye432ba92019-05-31 18:44:13 +070083 function f_L1CTL_RACH(L1CTL_PT pt, uint8_t ra, uint8_t combined := 1, uint16_t offset := 0,
84 template (value) RslChannelNr chan_nr := ts_RslChanNr_RACH(0),
85 template (value) RslLinkId link_id := ts_RslLinkID_DCCH(0))
86 return GsmFrameNumber {
Harald Welte9e4725d2017-07-16 23:18:09 +020087 var L1ctlDlMessage rc;
88 var GsmFrameNumber fn;
89 timer T := 2.0;
90 T.start
Vadim Yanitskiye432ba92019-05-31 18:44:13 +070091 pt.send(ts_L1CTL_RACH_REQ(ra, combined, offset, chan_nr, link_id))
Harald Welte9e4725d2017-07-16 23:18:09 +020092 alt {
Harald Weltef8df4cb2018-03-10 15:15:08 +010093 [] pt.receive(tr_L1CTL_RACH_CONF) -> value rc { fn := rc.dl_info.frame_nr };
Harald Welte9e4725d2017-07-16 23:18:09 +020094 [] pt.receive { repeat; };
Harald Welte3757e602018-03-10 17:12:02 +010095 [] T.timeout {
Vadim Yanitskiyde8af3d2019-06-12 01:34:05 +070096 setverdict(fail, "Timeout waiting for L1CTL_RACH_CONF");
Daniel Willmanne4ff5372018-07-05 17:35:03 +020097 mtc.stop;
Harald Welte3757e602018-03-10 17:12:02 +010098 }
Harald Welte9e4725d2017-07-16 23:18:09 +020099 }
100 return fn;
101 }
102
Vadim Yanitskiy51cbc102019-04-22 06:37:30 +0700103 function f_L1CTL_EXT_RACH(
104 L1CTL_PT pt, uint16_t ra11, L1ctlRachSynchSeq seq,
105 uint8_t combined := 1, uint16_t offset := 0
106 ) return GsmFrameNumber {
107 var L1ctlDlMessage rc;
108 var GsmFrameNumber fn;
109 timer T := 2.0;
110
111 T.start;
112 pt.send(ts_L1CTL_EXT_RACH_REQ(ra11, seq, combined, offset));
113 alt {
114 [] pt.receive(tr_L1CTL_RACH_CONF) -> value rc { fn := rc.dl_info.frame_nr };
115 [] pt.receive { repeat; };
116 [] T.timeout {
Vadim Yanitskiyde8af3d2019-06-12 01:34:05 +0700117 setverdict(fail, "Timeout waiting for (extended) L1CTL_RACH_CONF");
Vadim Yanitskiy51cbc102019-04-22 06:37:30 +0700118 mtc.stop;
119 }
120 }
121
122 return fn;
123 }
124
Harald Welte37052732018-03-09 19:38:46 +0100125 function f_L1CTL_PARAM(L1CTL_PT pt, uint8_t ta, uint8_t tx_power) {
Harald Weltef8df4cb2018-03-10 15:15:08 +0100126 pt.send(ts_L1CTL_PAR_REQ(ta, tx_power));
Harald Welte37052732018-03-09 19:38:46 +0100127 }
128
Harald Welte9e4725d2017-07-16 23:18:09 +0200129 function f_L1CTL_WAIT_IMM_ASS(L1CTL_PT pt, uint8_t ra, GsmFrameNumber rach_fn) return ImmediateAssignment {
130 var L1ctlDlMessage dl;
131 var GsmRrMessage rr;
132 timer T := 10.0;
133 T.start;
134 alt {
Harald Weltef8df4cb2018-03-10 15:15:08 +0100135 [] pt.receive(tr_L1CTL_DATA_IND(t_RslChanNr_PCH_AGCH(0))) -> value dl {
Harald Welte9e4725d2017-07-16 23:18:09 +0200136 rr := dec_GsmRrMessage(dl.payload.data_ind.payload);
Harald Welte0472ab42018-03-12 15:02:26 +0100137 log("PCH/AGCH DL RR: ", rr);
Harald Welte9e4725d2017-07-16 23:18:09 +0200138 if (match(rr, t_RR_IMM_ASS(ra, rach_fn))) {
139 log("Received IMM.ASS for our RACH!");
140 } else {
141 repeat;
142 }
143 };
144 [] pt.receive { repeat };
Harald Welte3757e602018-03-10 17:12:02 +0100145 [] T.timeout {
146 setverdict(fail, "Timeout waiting for IMM ASS");
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200147 mtc.stop;
Harald Welte3757e602018-03-10 17:12:02 +0100148 }
Harald Welte9e4725d2017-07-16 23:18:09 +0200149 }
150 T.stop;
151 return rr.payload.imm_ass;
152 }
153
Harald Welteb669ee02018-03-09 12:50:02 +0100154 function f_L1CTL_WAIT_IMM_ASS_TBF_DL(L1CTL_PT pt, GprsTlli tlli) return ImmediateAssignment {
155 var L1ctlDlMessage dl;
156 var GsmRrMessage rr;
157 timer T := 10.0;
158 T.start;
159 alt {
Harald Weltef8df4cb2018-03-10 15:15:08 +0100160 [] pt.receive(tr_L1CTL_DATA_IND(t_RslChanNr_PCH_AGCH(0))) -> value dl {
Harald Welteb669ee02018-03-09 12:50:02 +0100161 rr := dec_GsmRrMessage(dl.payload.data_ind.payload);
162 log("PCH/AGCN DL RR: ", rr);
163 if (match(rr, t_RR_IMM_ASS_TBF_DL(tlli))) {
164 log("Received IMM.ASS for our TLLI!");
165 } else {
166 repeat;
167 }
168 };
169 [] pt.receive { repeat };
Harald Welte3757e602018-03-10 17:12:02 +0100170 [] T.timeout {
Vadim Yanitskiyde8af3d2019-06-12 01:34:05 +0700171 setverdict(fail, "Timeout waiting for TBF IMM ASS");
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200172 mtc.stop;
Harald Welte3757e602018-03-10 17:12:02 +0100173 }
Harald Welteb669ee02018-03-09 12:50:02 +0100174 }
175 T.stop;
176 return rr.payload.imm_ass;
177 }
178
Harald Welteb3c226e2017-07-30 17:18:01 +0200179 function f_L1CTL_TBF_CFG(L1CTL_PT pt, boolean is_uplink, TfiUsfArr tfi_usf) {
180 timer T := 2.0;
181 T.start;
Harald Weltef8df4cb2018-03-10 15:15:08 +0100182 pt.send(ts_L1CTL_TBF_CFG_REQ(is_uplink, tfi_usf));
Harald Welteb3c226e2017-07-30 17:18:01 +0200183 alt {
Harald Weltef8df4cb2018-03-10 15:15:08 +0100184 [] pt.receive(tr_L1CTL_TBF_CFG_CONF(is_uplink)) {}
Harald Welteb3c226e2017-07-30 17:18:01 +0200185 [] pt.receive { repeat };
Harald Welte3757e602018-03-10 17:12:02 +0100186 [] T.timeout {
Vadim Yanitskiyde8af3d2019-06-12 01:34:05 +0700187 setverdict(fail, "Timeout waiting for L1CTL_TBF_CFG_CONF");
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200188 mtc.stop;
Harald Welte3757e602018-03-10 17:12:02 +0100189 };
Harald Welteb3c226e2017-07-30 17:18:01 +0200190 }
191 T.stop;
192 }
Harald Welteb05fd2d2019-05-19 22:25:38 +0200193 function f_L1CTL_DM_EST_REQ(L1CTL_PT pt, Arfcn arfcn, RslChannelNr chan_nr, GsmTsc tsc) {
194 pt.send(ts_L1CTL_DM_EST_REQ(arfcn, chan_nr, tsc));
195 }
Harald Welteb3c226e2017-07-30 17:18:01 +0200196
Harald Welte9e4725d2017-07-16 23:18:09 +0200197 /* Send DM_EST_REQ from parameters derived from IMM ASS */
198 function f_L1CTL_DM_EST_REQ_IA(L1CTL_PT pt, ImmediateAssignment imm_ass) {
Harald Welteb05fd2d2019-05-19 22:25:38 +0200199 f_L1CTL_DM_EST_REQ(pt, { false, imm_ass.chan_desc.arfcn }, imm_ass.chan_desc.chan_nr,
200 imm_ass.chan_desc.tsc);
Harald Welte9e4725d2017-07-16 23:18:09 +0200201 }
202
Harald Welte3dc20462018-03-10 23:03:38 +0100203 /* Send DM_REL_REQ from parameters derived from IMM ASS */
204 function f_L1CTL_DM_REL_REQ(L1CTL_PT pt, RslChannelNr chan_nr) {
205 pt.send(ts_L1CTL_DM_REL_REQ(chan_nr));
206 }
207
Harald Welteaca6e072018-03-10 17:15:38 +0100208 function f_L1CTL_RESET(L1CTL_PT pt, L1ctlResetType res_type := L1CTL_RES_T_FULL) {
209 timer T := 2.0;
210 pt.send(t_L1ctlResetReq(res_type));
211 T.start;
212 alt {
213 [] pt.receive(tr_L1CTL_MsgType(L1CTL_RESET_CONF)) { }
214 [] pt.receive { repeat; }
215 [] T.timeout {
Vadim Yanitskiyde8af3d2019-06-12 01:34:05 +0700216 setverdict(fail, "Timeout waiting for L1CTL_RESET_CONF");
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200217 mtc.stop;
Harald Welteaca6e072018-03-10 17:15:38 +0100218 }
219 }
Harald Weltee613f962018-04-18 22:38:16 +0200220 }
Harald Welteaca6e072018-03-10 17:15:38 +0100221
Harald Weltee613f962018-04-18 22:38:16 +0200222 function f_L1CTL_CRYPTO_REQ(L1CTL_PT pt, RslChannelNr chan_nr, uint8_t algo, octetstring key) {
223 pt.send(ts_L1CTL_CRYPTO_REQ(chan_nr, algo, key));
Harald Welteaca6e072018-03-10 17:15:38 +0100224 }
225
Harald Weltef68765d2017-08-20 22:54:57 +0200226 function f_connect_reset(L1CTL_PT pt, charstring l1ctl_sock_path := m_l1ctl_sock_path) {
Harald Welte6fc784e2018-02-25 23:31:37 +0100227 var f_UD_getMsgLen vl_f := refers(f_L1CTL_getMsgLen);
228 f_L1CTL_setGetMsgLen(pt, -1, vl_f, {});
Harald Welted1209a62017-07-29 12:55:06 +0200229 pt.send(L1CTL_connect:{path:=l1ctl_sock_path});
230 pt.receive(L1CTL_connect_result:{result_code := SUCCESS, err:=omit});
Harald Welte6fc784e2018-02-25 23:31:37 +0100231 f_L1CTL_setGetMsgLen(pt, 0, vl_f, {});
Harald Welteaca6e072018-03-10 17:15:38 +0100232 f_L1CTL_RESET(pt);
Harald Welted1209a62017-07-29 12:55:06 +0200233 }
Harald Welte9e4725d2017-07-16 23:18:09 +0200234
Harald Welte52c713c2017-07-16 15:44:44 +0200235 private function L1CTL_to_UD_connect(in L1CTL_connect pin, out UD_connect pout) {
236 pout.path := pin.path;
237 pout.id := 0;
238 } with { extension "prototype(fast)" }
239
240 private function UD_to_L1CTL_connect_result(in UD_connect_result pin, out L1CTL_connect_result pout) {
241 pout.result_code := pin.result.result_code;
242 pout.err := pin.result.err;
243 } with { extension "prototype(fast)" }
244
245 private function L1CTL_to_UD_ul(in L1ctlUlMessage pin, out UD_send_data pout) {
246 var L1ctlUlMessageLV msg_lv := { msg := pin };
247 pout.data := enc_L1ctlUlMessageLV(msg_lv);
248 pout.id := 0;
249 } with { extension "prototype(fast)" }
250
251 private function UD_to_L1CTL_dl(in UD_send_data pin, out L1ctlDlMessage pout) {
252 var L1ctlDlMessageLV msg_lv := dec_L1ctlDlMessageLV(pin.data);
253 pout:= msg_lv.msg;
254 } with { extension "prototype(fast)" }
255
256 type port L1CTL_PT message {
257 out L1ctlUlMessage
258 out L1CTL_connect
259 in L1ctlDlMessage
260 in L1CTL_connect_result
261 in UD_listen_result
262 in UD_connected
263 } with { extension "user UD_PT
264 out(L1ctlUlMessage -> UD_send_data: function(L1CTL_to_UD_ul);
265 L1CTL_connect -> UD_connect: function(L1CTL_to_UD_connect))
266 in(UD_send_data -> L1ctlDlMessage: function(UD_to_L1CTL_dl);
267 UD_connect_result -> L1CTL_connect_result: function(UD_to_L1CTL_connect_result);
268 UD_listen_result -> UD_listen_result: simple;
269 UD_connected -> UD_connected: simple
270 )" }
271}