Vadim Yanitskiy | f7d9c0f | 2019-09-06 00:08:17 +0200 | [diff] [blame] | 1 | module PCUIF_RAW_Components { |
| 2 | |
| 3 | /* |
| 4 | * Components for (RAW) PCU test cases. |
| 5 | * |
| 6 | * (C) 2019 Vadim Yanitskiy <axilirator@gmail.com> |
| 7 | * |
| 8 | * All rights reserved. |
| 9 | * |
| 10 | * Released under the terms of GNU General Public License, Version 2 or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 14 | */ |
| 15 | |
| 16 | import from IPL4asp_Types all; |
| 17 | import from UD_Types all; |
| 18 | |
| 19 | import from PCUIF_Types all; |
| 20 | import from PCUIF_CodecPort all; |
| 21 | |
| 22 | /* Component communication diagram: |
| 23 | * |
| 24 | * +-----+ +----------+ +---------+ |
| 25 | * | MTC +---------------+ PCUIF_CT +------+ OsmoPCU | |
| 26 | * +--+--+ +----+-----+ +---------+ |
| 27 | * | | |
| 28 | * | | |
| 29 | * | | |
| 30 | * | +-----------+ | +---------------+ |
| 31 | * +----+ BTS_CT #1 +------+ | ClckGen_CT #1 | |
| 32 | * | +-----+-----+ | +-------+-------+ |
| 33 | * | | | | |
| 34 | * | +---------------------------+ |
| 35 | * | | |
| 36 | * | +-----------+ | +---------------+ |
| 37 | * +----+ BTS_CT #2 +------+ | ClckGen_CT #2 | |
| 38 | * | +-----+-----+ | +-------+-------+ |
| 39 | * | | | | |
| 40 | * | +---------------------------+ |
| 41 | * | | |
| 42 | * | +-----------+ | +---------------+ |
| 43 | * +----+ BTS_CT #N +------+ | ClckGen_CT #N | |
| 44 | * +-----+-----+ +-------+-------+ |
| 45 | * | | |
| 46 | * +---------------------------+ |
| 47 | */ |
| 48 | |
| 49 | /* Events are used by the components to indicate that something |
| 50 | * has happened, e.g. we have got a connection from the PCU. */ |
| 51 | type enumerated RAW_PCU_EventType { |
| 52 | /* Events related to RAW_PCUIF_CT */ |
| 53 | PCU_EV_DISCONNECT, /*!< OsmoPCU has disconnected */ |
| 54 | PCU_EV_CONNECT, /*!< OsmoPCU is now connected */ |
| 55 | |
| 56 | /* Events related to RAW_PCU_BTS_CT */ |
| 57 | BTS_EV_SI13_NEGO, /*!< SI13 negotiation complete */ |
| 58 | |
| 59 | /* TDMA clock related events (TDMA frame-number in parameters) */ |
| 60 | TDMA_EV_PDTCH_BLOCK_BEG, /*!< 1/4 bursts of a PDTCH block on both Uplink and Downlink */ |
| 61 | TDMA_EV_PDTCH_BLOCK_END, /*!< 4/4 bursts of a PDTCH block on both Uplink and Downlink */ |
| 62 | TDMA_EV_PTCCH_DL_BLOCK, /*!< 4/4 bursts of a PTCCH block on Downlink */ |
| 63 | TDMA_EV_PTCCH_UL_BURST, /*!< One Access Burst on PTCCH/U */ |
| 64 | |
| 65 | TDMA_EV_PDTCH_BLOCK_SENT /*!< A PDTCH block has been sent to the PCU */ |
| 66 | }; |
| 67 | |
| 68 | /* Union of all possible parameters of the events */ |
| 69 | type union RAW_PCU_EventParam { |
| 70 | integer tdma_fn |
| 71 | }; |
| 72 | |
| 73 | type record RAW_PCU_Event { |
| 74 | RAW_PCU_EventType event, |
| 75 | /* TODO: can we use 'anytype' here? */ |
| 76 | RAW_PCU_EventParam data optional |
| 77 | }; |
| 78 | |
| 79 | template (value) RAW_PCU_Event ts_RAW_PCU_EV(RAW_PCU_EventType event) := { |
| 80 | event := event, |
| 81 | data := omit |
| 82 | } |
| 83 | template RAW_PCU_Event tr_RAW_PCU_EV(template RAW_PCU_EventType event := ?, |
| 84 | template RAW_PCU_EventParam data := *) := { |
| 85 | event := event, |
| 86 | data := data |
| 87 | } |
| 88 | |
| 89 | template (value) RAW_PCU_Event ts_RAW_PCU_CLCK_EV(RAW_PCU_EventType event, integer fn) := { |
| 90 | event := event, |
| 91 | data := { tdma_fn := fn } |
| 92 | } |
| 93 | template RAW_PCU_Event tr_RAW_PCU_CLCK_EV := { |
| 94 | event := (TDMA_EV_PDTCH_BLOCK_BEG, TDMA_EV_PDTCH_BLOCK_END, |
| 95 | TDMA_EV_PTCCH_DL_BLOCK, TDMA_EV_PTCCH_UL_BURST, |
| 96 | TDMA_EV_PDTCH_BLOCK_SENT), |
| 97 | data := { tdma_fn := ? } |
| 98 | } |
| 99 | |
| 100 | /* Generic port for messages and events */ |
| 101 | type port RAW_PCU_MSG_PT message { |
| 102 | inout RAW_PCU_Event; |
| 103 | inout PCUIF_Message; |
| 104 | } with { extension "internal" }; |
| 105 | |
| 106 | /* TDMA frame clock generator */ |
| 107 | type component RAW_PCU_ClckGen_CT { |
| 108 | /* One TDMA frame is 4.615 ms long */ |
| 109 | timer T_TDMAClock := 4.615 / 1000.0; |
| 110 | port RAW_PCU_MSG_PT CLCK; |
| 111 | var integer fn := 0; |
| 112 | } |
| 113 | |
| 114 | function f_ClckGen_CT_handler() |
| 115 | runs on RAW_PCU_ClckGen_CT { |
| 116 | var integer fn104, fn52, fn13; |
| 117 | |
| 118 | while (true) { |
| 119 | fn104 := fn mod 104; |
| 120 | fn52 := fn mod 52; |
| 121 | fn13 := fn mod 13; |
| 122 | |
| 123 | if (fn13 == 0 or fn13 == 4 or fn13 == 8) { |
| 124 | /* 1/4 bursts of a PDTCH block on both Uplink and Downlink */ |
| 125 | CLCK.send(ts_RAW_PCU_CLCK_EV(TDMA_EV_PDTCH_BLOCK_BEG, fn)); |
| 126 | } else if (fn13 == 3 or fn13 == 7 or fn13 == 11) { |
| 127 | /* 4/4 bursts of a PDTCH block on both Uplink and Downlink */ |
| 128 | CLCK.send(ts_RAW_PCU_CLCK_EV(TDMA_EV_PDTCH_BLOCK_END, fn)); |
| 129 | } else if (fn104 == 90) { |
| 130 | /* 4/4 bursts of a PTCCH (Timing Advance Control) block on Downlink */ |
| 131 | CLCK.send(ts_RAW_PCU_CLCK_EV(TDMA_EV_PTCCH_DL_BLOCK, fn)); |
| 132 | } else if (fn52 == 12 or fn52 == 38) { |
| 133 | /* One Access Burst on PTCCH/U */ |
| 134 | CLCK.send(ts_RAW_PCU_CLCK_EV(TDMA_EV_PTCCH_UL_BURST, fn)); |
| 135 | } |
| 136 | |
| 137 | /* TDMA hyperframe period is (2048 * 51 * 26) frames */ |
| 138 | fn := (fn + 1) mod (2048 * 51 * 26); |
| 139 | |
| 140 | /* (Re)start TDMA clock timer and wait */ |
| 141 | T_TDMAClock.start; |
| 142 | T_TDMAClock.timeout; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | type record of PCUIF_Message PCUIF_MsgQueue; |
| 147 | |
| 148 | /* Enqueue a given message to the end of a given queue */ |
| 149 | private function f_PCUIF_MsgQueue_enqueue(inout PCUIF_MsgQueue queue, |
| 150 | in PCUIF_Message msg) |
| 151 | { |
| 152 | queue := queue & { msg }; |
| 153 | } |
| 154 | |
| 155 | /* Dequeue the first message of a given queue */ |
| 156 | private function f_PCUIF_MsgQueue_dequeue(inout PCUIF_MsgQueue queue, |
| 157 | out PCUIF_Message msg) |
| 158 | { |
| 159 | var integer len := lengthof(queue); |
| 160 | |
| 161 | if (len == 0) { |
| 162 | setverdict(fail, "Failed to dequeue a message: the queue is empty!"); |
| 163 | mtc.stop; |
| 164 | } |
| 165 | |
| 166 | /* Store the first message */ |
| 167 | msg := queue[0]; |
| 168 | |
| 169 | /* Remove the first message from queue */ |
| 170 | if (len > 1) { |
| 171 | queue := substr(queue, 1, len - 1); |
| 172 | } else { |
| 173 | queue := { }; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /* Multiple base stations can be connected to the PCU. This component |
| 178 | * represents one BTS with an associated TDMA clock generator. */ |
| 179 | type component RAW_PCU_BTS_CT { |
| 180 | /* TDMA clock generator */ |
| 181 | var RAW_PCU_ClckGen_CT vc_CLCK_GEN; |
| 182 | port RAW_PCU_MSG_PT CLCK; |
| 183 | |
| 184 | /* Queues of PCUIF messages to be sent |
| 185 | * TODO: we may have multiple PDCH time-slots */ |
| 186 | var PCUIF_MsgQueue pdtch_data_queue := { }; |
| 187 | var PCUIF_MsgQueue pdtch_rts_queue := { }; |
| 188 | var PCUIF_MsgQueue ptcch_rts_queue := { }; |
| 189 | |
| 190 | /* Connection towards the PCU interface */ |
| 191 | port RAW_PCU_MSG_PT PCUIF; |
| 192 | /* Connection towards the test case */ |
| 193 | port RAW_PCU_MSG_PT TC; |
| 194 | } |
| 195 | |
| 196 | private altstep as_BTS_CT_MsgQueue(integer bts_nr) |
| 197 | runs on RAW_PCU_BTS_CT { |
| 198 | var PCUIF_Message pcu_msg; |
| 199 | |
| 200 | /* Enqueue DATA.ind and RTS.req messages */ |
| 201 | [] TC.receive(tr_PCUIF_MSG(PCU_IF_MSG_DATA_IND, bts_nr)) -> value pcu_msg { |
| 202 | f_PCUIF_MsgQueue_enqueue(pdtch_data_queue, pcu_msg); |
| 203 | repeat; |
| 204 | } |
| 205 | [] TC.receive(tr_PCUIF_RTS_REQ(bts_nr, sapi := PCU_IF_SAPI_PDTCH)) -> value pcu_msg { |
| 206 | f_PCUIF_MsgQueue_enqueue(pdtch_rts_queue, pcu_msg); |
| 207 | repeat; |
| 208 | } |
| 209 | [] TC.receive(tr_PCUIF_RTS_REQ(bts_nr, sapi := PCU_IF_SAPI_PTCCH)) -> value pcu_msg { |
| 210 | f_PCUIF_MsgQueue_enqueue(ptcch_rts_queue, pcu_msg); |
| 211 | repeat; |
| 212 | } |
| 213 | /* Forward other messages directly to the PCU */ |
| 214 | [] TC.receive(tr_PCUIF_MSG(?, bts_nr)) -> value pcu_msg { |
| 215 | PCUIF.send(pcu_msg); |
| 216 | repeat; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | private altstep as_BTS_CT_TDMASched(integer bts_nr) |
| 221 | runs on RAW_PCU_BTS_CT { |
| 222 | var PCUIF_Message pcu_msg; |
| 223 | var RAW_PCU_Event event; |
| 224 | |
| 225 | [] CLCK.receive(tr_RAW_PCU_EV(TDMA_EV_PDTCH_BLOCK_BEG)) -> value event { |
| 226 | /* If the RTS queue for PDTCH is not empty, send a message */ |
| 227 | if (lengthof(pdtch_rts_queue) > 0) { |
| 228 | f_PCUIF_MsgQueue_dequeue(pdtch_rts_queue, pcu_msg); |
| 229 | |
| 230 | /* Patch TDMA frame / block number and send */ |
| 231 | pcu_msg.u.rts_req.fn := event.data.tdma_fn; |
| 232 | pcu_msg.u.rts_req.block_nr := 0; /* FIXME! */ |
| 233 | PCUIF.send(pcu_msg); |
| 234 | } |
| 235 | |
| 236 | /* We don't really need to send every frame to OsmoPCU, because |
| 237 | * it omits frame numbers not starting at a MAC block. */ |
| 238 | PCUIF.send(ts_PCUIF_TIME_IND(bts_nr, event.data.tdma_fn)); |
| 239 | repeat; |
| 240 | } |
| 241 | [lengthof(pdtch_data_queue) > 0] CLCK.receive(tr_RAW_PCU_EV(TDMA_EV_PDTCH_BLOCK_END)) -> value event { |
| 242 | /* Dequeue a DATA.ind message */ |
| 243 | f_PCUIF_MsgQueue_dequeue(pdtch_data_queue, pcu_msg); |
| 244 | |
| 245 | /* Patch TDMA frame / block number */ |
| 246 | pcu_msg.u.data_ind.fn := event.data.tdma_fn; |
| 247 | pcu_msg.u.data_ind.block_nr := 0; /* FIXME! */ |
| 248 | |
| 249 | PCUIF.send(pcu_msg); /* Send to the PCU and notify the TC */ |
| 250 | TC.send(ts_RAW_PCU_CLCK_EV(TDMA_EV_PDTCH_BLOCK_SENT, event.data.tdma_fn)); |
| 251 | repeat; |
| 252 | } |
| 253 | [lengthof(ptcch_rts_queue) > 0] CLCK.receive(tr_RAW_PCU_EV(TDMA_EV_PTCCH_DL_BLOCK)) -> value event { |
| 254 | /* Dequeue an RTS.req message for PTCCH */ |
| 255 | f_PCUIF_MsgQueue_dequeue(ptcch_rts_queue, pcu_msg); |
| 256 | |
| 257 | /* Patch TDMA frame / block number and send */ |
| 258 | pcu_msg.u.rts_req.fn := event.data.tdma_fn; |
| 259 | pcu_msg.u.rts_req.block_nr := 0; /* FIXME! */ |
| 260 | PCUIF.send(pcu_msg); |
| 261 | repeat; |
| 262 | } |
| 263 | /* Ignore other clock events (and guard against an empty queue) */ |
| 264 | [] CLCK.receive(tr_RAW_PCU_CLCK_EV) { repeat; } |
| 265 | } |
| 266 | |
| 267 | function f_BTS_CT_handler(integer bts_nr, PCUIF_info_ind info_ind) |
| 268 | runs on RAW_PCU_BTS_CT { |
| 269 | var PCUIF_Message pcu_msg; |
| 270 | var RAW_PCU_Event event; |
| 271 | |
| 272 | /* Init TDMA clock generator (so we can stop and start it) */ |
| 273 | vc_CLCK_GEN := RAW_PCU_ClckGen_CT.create("ClckGen-" & int2str(bts_nr)) alive; |
| 274 | connect(vc_CLCK_GEN:CLCK, self:CLCK); |
| 275 | |
| 276 | /* Wait until the PCU is connected */ |
| 277 | PCUIF.receive(tr_RAW_PCU_EV(PCU_EV_CONNECT)); |
| 278 | |
| 279 | /* TODO: implement ACT.req handling */ |
| 280 | alt { |
| 281 | /* Wait for TXT.ind (PCU_VERSION) and respond with INFO.ind (SI13) */ |
| 282 | [] PCUIF.receive(tr_PCUIF_TXT_IND(bts_nr, PCU_VERSION, ?)) -> value pcu_msg { |
| 283 | log("Rx TXT.ind from the PCU, version is ", pcu_msg.u.txt_ind.text); |
| 284 | |
| 285 | /* Send System Information 13 to the PCU */ |
| 286 | PCUIF.send(PCUIF_Message:{ |
| 287 | msg_type := PCU_IF_MSG_INFO_IND, |
| 288 | bts_nr := bts_nr, |
| 289 | spare := '0000'O, |
| 290 | u := { info_ind := info_ind } |
| 291 | }); |
| 292 | |
| 293 | /* Notify the test case that we're done with SI13 */ |
| 294 | TC.send(ts_RAW_PCU_EV(BTS_EV_SI13_NEGO)); |
| 295 | |
| 296 | /* Start feeding clock to the PCU */ |
| 297 | vc_CLCK_GEN.start(f_ClckGen_CT_handler()); |
| 298 | repeat; |
| 299 | } |
| 300 | /* PCU -> test case forwarding (filter by the BTS number) */ |
| 301 | [] PCUIF.receive(tr_PCUIF_MSG(?, bts_nr)) -> value pcu_msg { |
| 302 | TC.send(pcu_msg); |
| 303 | repeat; |
| 304 | } |
| 305 | |
| 306 | /* TC -> [Queue] -> PCU forwarding */ |
| 307 | [] as_BTS_CT_MsgQueue(bts_nr); |
| 308 | /* TDMA scheduler (clock and queue handling) */ |
| 309 | [] as_BTS_CT_TDMASched(bts_nr); |
| 310 | |
| 311 | /* TODO: handle events (e.g. disconnection) from the PCU interface */ |
| 312 | [] PCUIF.receive(tr_RAW_PCU_EV) -> value event { |
| 313 | log("Ignore unhandled event: ", event); |
| 314 | repeat; |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | /* PCU interface (UNIX domain socket) handler */ |
| 320 | type component RAW_PCUIF_CT { |
| 321 | var ConnectionId g_pcu_conn_id := -1; |
| 322 | var boolean g_pcu_connected := false; |
| 323 | port PCUIF_CODEC_PT PCU; |
| 324 | |
| 325 | /* Connection towards BTS component(s) */ |
| 326 | port RAW_PCU_MSG_PT BTS; |
| 327 | /* Connection to the MTC (test case) */ |
| 328 | port RAW_PCU_MSG_PT MTC; |
| 329 | }; |
| 330 | |
| 331 | /* All received messages and events from OsmoPCU can be additionally sent |
| 332 | * directly to the MTC component. Pass direct := true to enable this mode. */ |
| 333 | function f_PCUIF_CT_handler(charstring pcu_sock_path, boolean direct := false) |
| 334 | runs on RAW_PCUIF_CT { |
| 335 | var PCUIF_send_data pcu_sd_msg; |
| 336 | var PCUIF_Message pcu_msg; |
| 337 | timer T_Conn := 10.0; |
| 338 | |
| 339 | log("Init PCU interface on '" & pcu_sock_path & "', waiting for connection..."); |
| 340 | g_pcu_conn_id := f_pcuif_listen(PCU, pcu_sock_path); |
| 341 | |
| 342 | /* Wait for connection */ |
| 343 | T_Conn.start; |
| 344 | alt { |
| 345 | [not g_pcu_connected] PCU.receive(UD_connected:?) { |
| 346 | log("OsmoPCU is now connected"); |
| 347 | /* Duplicate this event to the MTC if requested */ |
| 348 | if (direct) { MTC.send(ts_RAW_PCU_EV(PCU_EV_CONNECT)); } |
| 349 | BTS.send(ts_RAW_PCU_EV(PCU_EV_CONNECT)); |
| 350 | g_pcu_connected := true; |
| 351 | setverdict(pass); |
| 352 | T_Conn.stop; |
| 353 | repeat; |
| 354 | } |
| 355 | /* PCU -> BTS / MTC message forwarding */ |
| 356 | [g_pcu_connected] PCU.receive(PCUIF_send_data:?) -> value pcu_sd_msg { |
| 357 | /* Send to the BTSes if at least one is connected */ |
| 358 | if (BTS.checkstate("Connected")) { BTS.send(pcu_sd_msg.data); } |
| 359 | /* Duplicate this message to the MTC if requested */ |
| 360 | if (direct) { MTC.send(pcu_sd_msg.data); } |
| 361 | repeat; |
| 362 | } |
| 363 | /* MTC -> PCU message forwarding */ |
| 364 | [g_pcu_connected] MTC.receive(PCUIF_Message:?) -> value pcu_msg { |
| 365 | PCU.send(t_SD_PCUIF(g_pcu_conn_id, pcu_msg)); |
| 366 | repeat; |
| 367 | } |
| 368 | /* BTS -> PCU message forwarding */ |
| 369 | [g_pcu_connected] BTS.receive(PCUIF_Message:?) -> value pcu_msg { |
| 370 | PCU.send(t_SD_PCUIF(g_pcu_conn_id, pcu_msg)); |
| 371 | repeat; |
| 372 | } |
| 373 | [not g_pcu_connected] MTC.receive(PCUIF_Message:?) -> value pcu_msg { |
| 374 | log("PCU is not connected, dropping ", pcu_msg); |
| 375 | repeat; |
| 376 | } |
| 377 | [not g_pcu_connected] BTS.receive(PCUIF_Message:?) -> value pcu_msg { |
| 378 | log("PCU is not connected, dropping ", pcu_msg); |
| 379 | repeat; |
| 380 | } |
| 381 | /* TODO: handle disconnect and reconnect of the PCU */ |
| 382 | [] PCU.receive { |
| 383 | log("Unhandled message on PCU interface"); |
| 384 | repeat; |
| 385 | } |
| 386 | [not g_pcu_connected] T_Conn.timeout { |
| 387 | setverdict(fail, "Timeout waiting for PCU connection"); |
| 388 | mtc.stop; |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | } |