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