Harald Welte | d4ba7ff | 2017-07-17 21:00:48 +0200 | [diff] [blame] | 1 | /* Test Port that stacks on top of L1CTL test port and performs LAPDm encoding/decoding, so the user can send |
| 2 | * and receive LAPDm frames in decoded TTCN-3 data types. This is particularly useful for sending/receiving |
| 3 | * all kinds of hand-crafted LAPDm frames for testing of the remote LAPDm layer */ |
| 4 | module LAPDm_RAW_PT { |
| 5 | import from GSM_Types all; |
| 6 | import from Osmocom_Types all; |
| 7 | import from L1CTL_Types all; |
| 8 | import from L1CTL_PortType all; |
| 9 | import from LAPDm_Types all; |
| 10 | |
| 11 | /* request to tune to a given ARFCN and start BCCH decoding */ |
| 12 | type record BCCH_tune_req { |
| 13 | Arfcn arfcn, |
| 14 | boolean combined_ccch |
| 15 | } |
| 16 | |
| 17 | /* ask for a dedicated channel to be established */ |
| 18 | type record DCCH_establish_req { |
| 19 | uint8_t ra |
| 20 | } |
| 21 | |
| 22 | type record DCCH_establish_res { |
| 23 | ChannelDescription chan_desc optional, |
| 24 | charstring err optional |
| 25 | } |
| 26 | |
| 27 | type record DCCH_release_req { |
| 28 | } |
| 29 | |
| 30 | /* PH-DATA.ind / PH-DATA.req */ |
| 31 | type record LAPDm_ph_data { |
| 32 | boolean sacch, |
| 33 | GsmSapi sapi, |
| 34 | LapdmFrame lapdm |
| 35 | } |
| 36 | |
| 37 | /* port from our (internal) point of view */ |
| 38 | type port LAPDm_SP_PT message { |
| 39 | in BCCH_tune_req, |
| 40 | DCCH_establish_req, |
| 41 | DCCH_release_req, |
| 42 | LAPDm_ph_data; |
| 43 | out DCCH_establish_res, |
| 44 | LAPDm_ph_data; |
| 45 | } with {extension "internal"}; |
| 46 | |
| 47 | /* port from user (external) point of view */ |
| 48 | type port LAPDm_PT message { |
| 49 | in DCCH_establish_res, |
| 50 | LAPDm_ph_data; |
| 51 | out BCCH_tune_req, |
| 52 | DCCH_establish_req, |
| 53 | DCCH_release_req, |
| 54 | LAPDm_ph_data; |
| 55 | } with {extension "internal"}; |
| 56 | |
| 57 | function LAPDmStart() runs on lapdm_CT { |
| 58 | f_init(); |
| 59 | ScanEvents(); |
| 60 | } |
| 61 | |
| 62 | /* TS 44.004 Figure 5.1 */ |
| 63 | type enumerated ph_state_enum { |
| 64 | PH_STATE_NULL, |
| 65 | PH_STATE_BCH, |
| 66 | PH_STATE_SEARCHING_BCH, |
| 67 | PH_STATE_TUNING_DCH, |
| 68 | PH_STATE_DCH |
| 69 | } |
| 70 | |
| 71 | type component lapdm_CT { |
| 72 | var charstring l1ctl_sock_path := "/tmp/osmocom_l2"; |
| 73 | |
| 74 | /* L1CTL port towards the bottom */ |
| 75 | port L1CTL_PT L1CTL; |
| 76 | /* Port towards L2 */ |
| 77 | port LAPDm_SP_PT LAPDM_SP; |
| 78 | |
| 79 | /* physical layer state */ |
| 80 | var ph_state_enum ph_state := PH_STATE_NULL; |
| 81 | |
| 82 | /* channel description of the currently active DCH */ |
| 83 | var ChannelDescription chan_desc; |
| 84 | }; |
| 85 | |
| 86 | /* wrapper function to log state transitions */ |
| 87 | private function set_ph_state(ph_state_enum new_state) runs on lapdm_CT { |
| 88 | log("PH-STATE ", ph_state, " -> ", new_state); |
| 89 | ph_state := new_state; |
| 90 | } |
| 91 | |
| 92 | private function f_init() runs on lapdm_CT { |
Harald Welte | d1209a6 | 2017-07-29 12:55:06 +0200 | [diff] [blame] | 93 | f_connect_reset(L1CTL, l1ctl_sock_path); |
Harald Welte | d4ba7ff | 2017-07-17 21:00:48 +0200 | [diff] [blame] | 94 | set_ph_state(PH_STATE_NULL); |
| 95 | } |
| 96 | |
| 97 | /* release the dedicated radio channel */ |
| 98 | private function f_release_dcch() runs on lapdm_CT { |
| 99 | L1CTL.send(t_L1CTL_DM_REL_REQ(chan_desc.chan_nr)); |
| 100 | set_ph_state(PH_STATE_NULL); |
| 101 | } |
| 102 | |
| 103 | /* tune to given ARFCN and start BCCH/CCCH decoding */ |
| 104 | private function f_tune_bcch(Arfcn arfcn, boolean combined) runs on lapdm_CT { |
| 105 | var L1ctlCcchMode mode := CCCH_MODE_NON_COMBINED; |
| 106 | if (combined) { |
| 107 | mode := CCCH_MODE_COMBINED; |
| 108 | } |
| 109 | |
| 110 | if (ph_state == PH_STATE_DCH) { |
| 111 | /* release any previous DCH */ |
| 112 | f_release_dcch(); |
| 113 | } |
| 114 | |
| 115 | set_ph_state(PH_STATE_SEARCHING_BCH); |
| 116 | |
| 117 | /* send FB/SB req to sync to cell */ |
| 118 | f_L1CTL_FBSB(L1CTL, arfcn, mode); |
| 119 | set_ph_state(PH_STATE_BCH); |
| 120 | } |
| 121 | |
| 122 | /* master function establishing a dedicated radio channel */ |
| 123 | private function f_establish_dcch(uint8_t ra) runs on lapdm_CT { |
| 124 | var ImmediateAssignment imm_ass; |
| 125 | var GsmFrameNumber rach_fn; |
| 126 | |
| 127 | /* send RACH request and obtain FN at which it was sent */ |
| 128 | rach_fn := f_L1CTL_RACH(L1CTL, ra); |
| 129 | //if (not rach_fn) { return; } |
| 130 | |
| 131 | /* wait for receiving matching IMM ASS */ |
| 132 | imm_ass := f_L1CTL_WAIT_IMM_ASS(L1CTL, ra, rach_fn) |
| 133 | //if (not imm_ass) { return; } |
| 134 | set_ph_state(PH_STATE_TUNING_DCH); |
| 135 | |
| 136 | /* store/save channel description */ |
| 137 | chan_desc := imm_ass.chan_desc; |
| 138 | |
| 139 | /* send DM_EST_REQ */ |
| 140 | f_L1CTL_DM_EST_REQ_IA(L1CTL, imm_ass); |
| 141 | set_ph_state(PH_STATE_DCH); |
| 142 | } |
| 143 | |
| 144 | function ScanEvents() runs on lapdm_CT { |
| 145 | var L1ctlDlMessage dl; |
| 146 | var BCCH_tune_req bt; |
| 147 | var LAPDm_ph_data lpd; |
| 148 | var DCCH_establish_req est_req; |
| 149 | var DCCH_establish_res est_res; |
| 150 | |
| 151 | while (true) { |
| 152 | if (ph_state == PH_STATE_NULL) { |
| 153 | alt { |
| 154 | [] LAPDM_SP.receive(BCCH_tune_req:?) -> value bt { |
| 155 | f_tune_bcch(bt.arfcn, bt.combined_ccch); |
| 156 | } |
| 157 | |
| 158 | [] LAPDM_SP.receive {} |
| 159 | [] L1CTL.receive {} |
| 160 | |
| 161 | } |
| 162 | } else if (ph_state == PH_STATE_BCH or ph_state == PH_STATE_SEARCHING_BCH) { |
| 163 | alt { |
| 164 | [] LAPDM_SP.receive(BCCH_tune_req:?) -> value bt { |
| 165 | f_tune_bcch(bt.arfcn, bt.combined_ccch); |
| 166 | } |
| 167 | |
| 168 | /* forward CCCH SAPI from L1CTL to User */ |
| 169 | [] L1CTL.receive(t_L1CTL_DATA_IND(t_RslChanNr_BCCH(0))) -> value dl { |
| 170 | lpd.sacch := false; |
| 171 | lpd.sapi := 0; |
| 172 | lpd.lapdm.bbis := dec_LapdmFrameBbis(dl.payload.data_ind.payload); |
| 173 | LAPDM_SP.send(lpd); |
| 174 | } |
| 175 | |
| 176 | /* forward BCCH SAPI from L1CTL to User */ |
| 177 | [] L1CTL.receive(t_L1CTL_DATA_IND(t_RslChanNr_PCH_AGCH(0))) -> value dl { |
| 178 | lpd.sacch := false; |
| 179 | lpd.sapi := 0; |
| 180 | lpd.lapdm.bbis := dec_LapdmFrameBbis(dl.payload.data_ind.payload); |
| 181 | LAPDM_SP.send(lpd); |
| 182 | } |
| 183 | |
| 184 | /* Establish dedicated channel */ |
| 185 | [] LAPDM_SP.receive(DCCH_establish_req:?) -> value est_req { |
| 186 | var DCCH_establish_res res; |
| 187 | f_establish_dcch(est_req.ra); |
| 188 | if (ph_state == PH_STATE_DCH) { |
| 189 | res := { chan_desc, omit }; |
| 190 | } else { |
| 191 | res := { omit, "Unable to esetablish DCCH" }; |
| 192 | } |
| 193 | LAPDM_SP.send(res); |
| 194 | } |
| 195 | |
| 196 | [] LAPDM_SP.receive {} |
| 197 | [] L1CTL.receive {} |
| 198 | |
| 199 | } |
| 200 | |
| 201 | } else if (ph_state == PH_STATE_TUNING_DCH or ph_state == PH_STATE_DCH) { |
| 202 | alt { |
| 203 | |
| 204 | /* decode any received DATA frames for the dedicated channel and pass them up */ |
| 205 | [] L1CTL.receive(t_L1CTL_DATA_IND(chan_desc.chan_nr)) -> value dl { |
| 206 | if (dl.dl_info.link_id.c == SACCH) { |
| 207 | lpd.sacch := true; |
| 208 | /* FIXME: how to deal with UI frames in B4 format (lo length!) */ |
| 209 | } else { |
| 210 | lpd.sacch := false; |
| 211 | } |
| 212 | lpd.sapi := dl.dl_info.link_id.sapi; |
| 213 | lpd.lapdm.b := dec_LapdmFrameB(dl.payload.data_ind.payload); |
| 214 | LAPDM_SP.send(lpd); |
| 215 | } |
| 216 | |
| 217 | /* encode any LAPDm record from user and pass it on to L1CTL */ |
| 218 | [] LAPDM_SP.receive(LAPDm_ph_data:?) -> value lpd { |
| 219 | var octetstring buf; |
| 220 | var RslLinkId link_id; |
| 221 | if (lpd.sacch) { |
| 222 | link_id := valueof(ts_RslLinkID_SACCH(lpd.sapi)); |
| 223 | } else { |
| 224 | link_id := valueof(ts_RslLinkID_DCCH(lpd.sapi)); |
| 225 | } |
| 226 | buf := enc_LapdmFrame(lpd.lapdm); |
| 227 | L1CTL.send(t_L1CTL_DATA_REQ(chan_desc.chan_nr, link_id, buf)); |
| 228 | } |
| 229 | |
| 230 | /* Release dedicated channel */ |
| 231 | [] LAPDM_SP.receive(DCCH_release_req:?) { |
| 232 | /* go back to BCCH */ |
| 233 | f_release_dcch(); |
| 234 | } |
| 235 | |
| 236 | [] LAPDM_SP.receive {} |
| 237 | [] L1CTL.receive {} |
| 238 | |
| 239 | |
| 240 | } |
| 241 | } |
| 242 | } /* while (1) */ |
| 243 | } |
| 244 | } |