Harald Welte | 714ded9 | 2017-12-08 14:00:22 +0100 | [diff] [blame] | 1 | module RSL_Emulation { |
| 2 | |
Harald Welte | 35bb716 | 2018-01-03 21:07:52 +0100 | [diff] [blame] | 3 | /* RSL Emulation, runs on top of IPA_Emulation. It multiplexes/demultiplexes |
| 4 | * the individual connections (logical channels), so there can be separate TTCN-3 components |
| 5 | * handling each of the connections. |
| 6 | * |
| 7 | * The RSL_Emulation.main() function processes RSL messages from the IPA demultiplex |
| 8 | * stack via the IPA_RSL_PT, and dispatches them to the per-connection components. |
| 9 | * |
| 10 | * Outbound RSL connections are initiated by sending a RSLDC_ChanRqd primitive |
| 11 | * to the component running the RSL_Emulation.main() function. |
| 12 | * |
| 13 | * (C) 2017 by Harald Welte <laforge@gnumonks.org> |
| 14 | * All rights reserved. |
| 15 | * |
| 16 | * Released under the terms of GNU General Public License, Version 2 or |
| 17 | * (at your option) any later version. |
| 18 | */ |
| 19 | |
Harald Welte | 714ded9 | 2017-12-08 14:00:22 +0100 | [diff] [blame] | 20 | import from General_Types all; |
| 21 | import from Osmocom_Types all; |
| 22 | import from GSM_Types all; |
| 23 | import from GSM_RR_Types all; |
| 24 | import from RSL_Types all; |
| 25 | import from IPA_Types all; |
| 26 | import from IPA_Emulation all; |
| 27 | |
| 28 | |
| 29 | /* General "base class" component definition, of which specific implementations |
| 30 | * derive themselves by means of the "extends" feature */ |
| 31 | type component RSL_DchanHdlr { |
| 32 | /* port facing up towards dedicated channel handler */ |
| 33 | port RSL_DCHAN_PT RSL; |
| 34 | var RslChannelNr g_chan_nr; |
| 35 | }; |
| 36 | |
| 37 | type record RSLDC_ChanRqd { |
| 38 | OCT1 ra, |
| 39 | GsmFrameNumber fn |
| 40 | }; |
| 41 | |
| 42 | template RSLDC_ChanRqd ts_RSLDC_ChanRqd(OCT1 ra, GsmFrameNumber fn) := { |
| 43 | ra := ra, |
| 44 | fn := fn |
| 45 | } |
| 46 | |
| 47 | type port RSL_DCHAN_PT message { |
| 48 | inout RSLDC_ChanRqd, RSL_Message; |
| 49 | } with { extension "internal" }; |
| 50 | |
| 51 | /*********************************************************************** |
| 52 | * Client Component for a single dedicated channel |
| 53 | ***********************************************************************/ |
| 54 | |
| 55 | private function f_rx_or_fail(template RSL_Message exp_rx) runs on RSL_DchanHdlr return RSL_Message |
| 56 | { |
| 57 | var RSL_Message rx_rsl; |
| 58 | timer T := 10.0; |
| 59 | |
| 60 | /* request a channel to be established */ |
| 61 | T.start; |
| 62 | alt { |
| 63 | [] RSL.receive(exp_rx) -> value rx_rsl { |
| 64 | T.stop; |
| 65 | return rx_rsl; |
| 66 | } |
| 67 | [] RSL.receive { |
| 68 | setverdict(fail, "Unexpected RSL message on DCHAN"); |
| 69 | self.stop; |
| 70 | } |
| 71 | [] T.timeout { |
| 72 | setverdict(fail, "Timeout waiting for RSL on DCHAN"); |
| 73 | self.stop; |
| 74 | } |
| 75 | } |
| 76 | /* never reached */ |
| 77 | return rx_rsl; |
| 78 | } |
| 79 | |
| 80 | /* establish a dedicated channel using 'ra' */ |
| 81 | function f_chan_est(OCT1 ra, octetstring est_l3, template RslLinkId link_id, GsmFrameNumber fn := 23) |
| 82 | runs on RSL_DchanHdlr { |
| 83 | var RSL_Message rx_rsl; |
| 84 | var GsmRrMessage rr; |
| 85 | |
| 86 | /* request a channel to be established */ |
| 87 | RSL.send(ts_RSLDC_ChanRqd(ra, fn)); |
| 88 | /* expect immediate assignment */ |
| 89 | rx_rsl := f_rx_or_fail(tr_RSL_IMM_ASSIGN); |
| 90 | rr := dec_GsmRrMessage(rx_rsl.ies[1].body.full_imm_ass_info.payload); |
| 91 | g_chan_nr := rr.payload.imm_ass.chan_desc.chan_nr; |
| 92 | RSL.send(ts_RSL_EST_IND(g_chan_nr, valueof(link_id), est_l3)); |
| 93 | } |
| 94 | |
| 95 | function f_deact_chan(RSL_Cause cause) runs on RSL_DchanHdlr |
| 96 | { |
| 97 | var RSL_Message rx_rsl; |
| 98 | |
| 99 | RSL.send(ts_RSL_CONN_FAIL_IND(g_chan_nr, cause)); |
| 100 | rx_rsl := f_rx_or_fail(tr_RSL_MsgTypeD(RSL_MT_RF_CHAN_REL)); |
| 101 | /* FIXME RSL.send(ts_RSL_RF_CHAN_REL_ACK()) */ |
| 102 | } |
| 103 | |
| 104 | |
| 105 | |
| 106 | /*********************************************************************** |
| 107 | * Main Component |
| 108 | ***********************************************************************/ |
| 109 | |
| 110 | private type record ConnectionData { |
| 111 | /* component reference to the client component */ |
| 112 | RSL_DchanHdlr comp_ref, |
| 113 | /* RSL (dedicated) Channel number we're handling */ |
| 114 | uint8_t trx_nr optional, |
| 115 | IpaStreamId stream_id optional, |
| 116 | RslChannelNr chan_nr optional, |
| 117 | /* Random Reference */ |
| 118 | OCT1 ra optional, |
| 119 | GsmFrameNumber ra_fn optional |
| 120 | }; |
| 121 | |
| 122 | private function f_cid_by_comp_ref(RSL_DchanHdlr comp_ref) |
| 123 | runs on RSL_Emulation_CT return integer { |
| 124 | var integer i; |
| 125 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 126 | if (ispresent(ConnectionTable[i].comp_ref) and |
| 127 | ConnectionTable[i].comp_ref == comp_ref) { |
| 128 | return i; |
| 129 | } |
| 130 | } |
| 131 | log("No Dchan handler for ", comp_ref); |
| 132 | return -1; |
| 133 | } |
| 134 | |
| 135 | private function f_cid_by_chan_nr(uint8_t trx_nr, RslChannelNr chan_nr) |
| 136 | runs on RSL_Emulation_CT return integer { |
| 137 | var integer i; |
| 138 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 139 | if (ispresent(ConnectionTable[i].chan_nr) and |
| 140 | ConnectionTable[i].chan_nr == chan_nr and ConnectionTable[i].trx_nr == trx_nr) { |
| 141 | return i; |
| 142 | } |
| 143 | } |
| 144 | log("No Dchan handler for ", trx_nr, chan_nr); |
| 145 | return -1; |
| 146 | } |
| 147 | |
| 148 | private function f_cid_by_ra_fn(OCT1 ra, GsmFrameNumber fn) |
| 149 | runs on RSL_Emulation_CT return integer { |
| 150 | var integer i; |
| 151 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 152 | if (ispresent(ConnectionTable[i].ra) and |
| 153 | ConnectionTable[i].ra == ra and ConnectionTable[i].ra_fn == fn) { |
| 154 | return i; |
| 155 | } |
| 156 | } |
| 157 | log("No Dchan handler for ", ra, fn); |
| 158 | return -1; |
| 159 | } |
| 160 | |
| 161 | /* create an ew client with given RA and FN */ |
| 162 | private function f_cid_create(OCT1 ra, GsmFrameNumber fn, RSL_DchanHdlr comp_ref) |
| 163 | runs on RSL_Emulation_CT return integer { |
| 164 | var integer i; |
| 165 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 166 | if (not ispresent(ConnectionTable[i].ra)) { |
| 167 | ConnectionTable[i].ra := ra; |
| 168 | ConnectionTable[i].ra_fn := fn; |
| 169 | ConnectionTable[i].comp_ref := comp_ref; |
| 170 | return i; |
| 171 | } |
| 172 | } |
| 173 | log("No free entry in conn table for ", ra, fn); |
| 174 | return -1; |
| 175 | } |
| 176 | |
| 177 | private function f_cid_clear(integer cid) |
| 178 | runs on RSL_Emulation_CT { |
| 179 | ConnectionTable[cid].ra := omit; |
| 180 | ConnectionTable[cid].ra_fn := omit; |
| 181 | ConnectionTable[cid].ra_fn := omit; |
| 182 | ConnectionTable[cid].trx_nr := omit; |
| 183 | ConnectionTable[cid].stream_id := omit; |
| 184 | ConnectionTable[cid].chan_nr := omit; |
| 185 | } |
| 186 | |
| 187 | type component RSL_Emulation_CT { |
| 188 | /* port facing down towards IPA emulation */ |
| 189 | port IPA_RSL_PT IPA_PT; |
| 190 | /* port facing up towards dedicated channel handler */ |
| 191 | port RSL_DCHAN_PT CLIENT_PT; |
| 192 | |
| 193 | /* state of all concurrent connections / dedicated channels */ |
| 194 | var ConnectionData ConnectionTable[64]; |
| 195 | } |
| 196 | |
| 197 | |
| 198 | /* template for an ASP_RSL_Unitdata as we receive it from the IPA_Emulateion component */ |
| 199 | private template ASP_RSL_Unitdata tr_RSL(template RSL_Message rsl, template IpaStreamId sid := ?) := { |
| 200 | streamId := sid, |
| 201 | rsl := rsl |
| 202 | } |
| 203 | |
Harald Welte | 714ded9 | 2017-12-08 14:00:22 +0100 | [diff] [blame] | 204 | private function f_trx_by_streamId(IpaStreamId id) return integer { |
| 205 | return enum2int(id); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | function main() runs on RSL_Emulation_CT { |
| 210 | var ASP_RSL_Unitdata rx_rsl; |
| 211 | var RSL_Message rx_rsl_msg; |
| 212 | var RSLDC_ChanRqd chan_rqd; |
| 213 | var RSL_DchanHdlr vc_conn; |
| 214 | var integer cid; |
| 215 | var integer i; |
| 216 | |
Daniel Willmann | 17f970f | 2018-01-17 12:03:19 +0100 | [diff] [blame] | 217 | f_conn_table_init(); |
| 218 | |
Harald Welte | 714ded9 | 2017-12-08 14:00:22 +0100 | [diff] [blame] | 219 | while (true) { |
| 220 | alt { |
Harald Welte | 624f963 | 2017-12-16 19:26:04 +0100 | [diff] [blame] | 221 | [] IPA_PT.receive(ASP_IPA_Event:{up_down := ASP_IPA_EVENT_UP}) { |
| 222 | } |
Harald Welte | 714ded9 | 2017-12-08 14:00:22 +0100 | [diff] [blame] | 223 | [] IPA_PT.receive(ASP_IPA_Event:{up_down := ASP_IPA_EVENT_ID_ACK}) { |
Harald Welte | 7ae019e | 2017-12-09 00:54:15 +0100 | [diff] [blame] | 224 | IPA_PT.send(ts_ASP_RSL_UD(IPAC_PROTO_RSL_TRX0,ts_RSL_PAGING_LOAD_IND(23))); |
Harald Welte | 714ded9 | 2017-12-08 14:00:22 +0100 | [diff] [blame] | 225 | } |
| 226 | [] IPA_PT.receive(tr_RSL(tr_RSL_IMM_ASSIGN)) -> value rx_rsl { |
| 227 | var GsmRrMessage rr; |
| 228 | var OCT1 ra; |
| 229 | var GsmFrameNumber fn; |
| 230 | log("IMM ASS INFO ", rx_rsl.rsl.ies[1].body); |
| 231 | rr := dec_GsmRrMessage(rx_rsl.rsl.ies[1].body.full_imm_ass_info.payload); |
| 232 | if (ischosen(rr.payload.imm_ass)) { |
| 233 | ra := bit2oct(rr.payload.imm_ass.req_ref.ra); |
| 234 | fn := 23; //FIXME(rr.payload.imm_ass); |
| 235 | /* lookup client based on RA+time, deliver to client */ |
| 236 | cid := f_cid_by_ra_fn(ra, fn); |
| 237 | if (cid == -1) { |
| 238 | setverdict(fail, "IMM ASS for unknown DChan"); |
| 239 | } |
| 240 | /* update client with trx_nr */ |
| 241 | ConnectionTable[cid].trx_nr := f_trx_by_streamId(rx_rsl.streamId); |
| 242 | ConnectionTable[cid].stream_id := rx_rsl.streamId; |
| 243 | /* update client with chan_nr */ |
| 244 | ConnectionTable[cid].chan_nr := rr.payload.imm_ass.chan_desc.chan_nr; |
| 245 | /* TODO: add timer to time-out ConnectionTable entries which |
| 246 | * never get followed-up to */ |
| 247 | CLIENT_PT.send(rx_rsl.rsl) to ConnectionTable[cid].comp_ref; |
| 248 | } else if (ischosen(rr.payload.imm_ass_rej)) { |
| 249 | for (i := 0; i < sizeof(rr.payload.imm_ass_rej.payload); i := i + 1) { |
| 250 | ra := bit2oct(rr.payload.imm_ass_rej.payload[i].req_ref.ra); |
| 251 | fn := 23; //FIXME(); |
| 252 | /* lookup client based on RA+time, deliver to client */ |
| 253 | cid := f_cid_by_ra_fn(ra, fn); |
| 254 | if (cid != -1) { |
| 255 | CLIENT_PT.send(rx_rsl.rsl) to ConnectionTable[cid].comp_ref; |
| 256 | /* delete ClientTable entry, as it failed */ |
| 257 | f_cid_clear(cid); |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | [] IPA_PT.receive(tr_RSL(tr_RSL_PAGING_CMD(?, ?))) -> value rx_rsl { |
| 264 | log("PAGING IDENTITY ", rx_rsl.rsl.ies[2].body.other); |
| 265 | /* broadcast to all clients? */ |
| 266 | for (i := 0; i < sizeof(ConnectionTable); i := i + 1) { |
| 267 | if (ispresent(ConnectionTable[i].comp_ref)) { |
| 268 | CLIENT_PT.send(rx_rsl.rsl) to ConnectionTable[i].comp_ref; |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | [] IPA_PT.receive(tr_RSL(tr_RSL_MsgTypeT(?))) -> value rx_rsl { |
| 274 | log("Ingnoring TRX Mgmt ", rx_rsl.rsl); |
| 275 | } |
| 276 | |
| 277 | [] IPA_PT.receive(tr_RSL(tr_RSL_MsgTypeC(?))) -> value rx_rsl { |
| 278 | log("Ignoring Common Channel Mgmt ", rx_rsl.rsl); |
| 279 | } |
| 280 | |
| 281 | /* blindly acknowledge all channel activations */ |
| 282 | [] IPA_PT.receive(tr_RSL(tr_RSL_MsgTypeD(RSL_MT_CHAN_ACTIV))) -> value rx_rsl { |
| 283 | var RslChannelNr chan_nr := rx_rsl.rsl.ies[0].body.chan_nr; |
Harald Welte | 7ae019e | 2017-12-09 00:54:15 +0100 | [diff] [blame] | 284 | IPA_PT.send(ts_ASP_RSL_UD(rx_rsl.streamId, ts_RSL_CHAN_ACT_ACK(chan_nr, 23))); |
Harald Welte | 714ded9 | 2017-12-08 14:00:22 +0100 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | [] IPA_PT.receive(tr_RSL(tr_RSL_MsgTypeDR(?))) -> value rx_rsl { |
| 288 | /* dispatch to channel based on ChanId */ |
| 289 | cid := f_cid_by_chan_nr(f_trx_by_streamId(rx_rsl.streamId), |
| 290 | rx_rsl.rsl.ies[0].body.chan_nr); |
| 291 | if (cid != -1) { |
| 292 | CLIENT_PT.send(rx_rsl.rsl) to ConnectionTable[cid].comp_ref; |
| 293 | } else { |
| 294 | setverdict(fail, "RSL for unknown Dchan"); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | [] IPA_PT.receive { |
| 299 | setverdict(fail, "Received unknown primitive from IPA"); |
| 300 | self.stop; |
| 301 | } |
| 302 | |
| 303 | [] CLIENT_PT.receive(RSLDC_ChanRqd:?) -> value chan_rqd sender vc_conn { |
| 304 | /* Store the knowledge that this sender has requested a certain RQ+time */ |
| 305 | f_cid_create(chan_rqd.ra, chan_rqd.fn, vc_conn); |
Harald Welte | 7ae019e | 2017-12-09 00:54:15 +0100 | [diff] [blame] | 306 | IPA_PT.send(ts_ASP_RSL_UD(IPAC_PROTO_RSL_TRX0, |
Harald Welte | 714ded9 | 2017-12-08 14:00:22 +0100 | [diff] [blame] | 307 | ts_RSL_CHAN_RQD(chan_rqd.ra, chan_rqd.fn))); |
| 308 | } |
| 309 | |
| 310 | [] CLIENT_PT.receive(tr_RSL_MsgType(?)) -> value rx_rsl_msg sender vc_conn { |
| 311 | /* forward to BSC */ |
| 312 | cid := f_cid_by_comp_ref(vc_conn); |
Harald Welte | 7ae019e | 2017-12-09 00:54:15 +0100 | [diff] [blame] | 313 | IPA_PT.send(ts_ASP_RSL_UD(ConnectionTable[cid].stream_id, rx_rsl_msg)); |
Harald Welte | 714ded9 | 2017-12-08 14:00:22 +0100 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
Daniel Willmann | 17f970f | 2018-01-17 12:03:19 +0100 | [diff] [blame] | 320 | private function f_conn_table_init() |
| 321 | runs on RSL_Emulation_CT { |
| 322 | var integer i; |
| 323 | |
| 324 | /* Initialize the ConnectionTable */ |
| 325 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 326 | f_cid_clear(i); |
| 327 | } |
| 328 | } |
Harald Welte | 714ded9 | 2017-12-08 14:00:22 +0100 | [diff] [blame] | 329 | |
| 330 | } |