Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 1 | #include <stdio.h> |
Harald Welte | 71d87b2 | 2011-07-18 14:49:56 +0200 | [diff] [blame] | 2 | #include <osmocom/core/talloc.h> |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 3 | #include <string.h> |
Pablo Neira Ayuso | 2ad22b8 | 2011-07-08 18:35:45 +0200 | [diff] [blame] | 4 | #include <unistd.h> |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 5 | #include <osmocom/abis/abis.h> |
| 6 | #include <osmocom/abis/e1_input.h> |
| 7 | #include <osmocom/core/logging.h> |
| 8 | #include <osmocom/core/application.h> |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 9 | #include <osmocom/abis/ipaccess.h> |
Pablo Neira Ayuso | 009573a | 2011-07-08 14:17:15 +0200 | [diff] [blame] | 10 | #include <osmocom/gsm/protocol/gsm_12_21.h> |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 11 | |
| 12 | static void *tall_test; |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 13 | static struct e1inp_sign_link *oml_sign_link, *rsl_sign_link; |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 14 | |
Pablo Neira Ayuso | fe8ab0a | 2011-07-02 18:48:45 +0200 | [diff] [blame] | 15 | #define DBTSTEST 0 |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 16 | |
| 17 | struct log_info_cat bts_test_cat[] = { |
| 18 | [DBTSTEST] = { |
| 19 | .name = "DBTSTEST", |
| 20 | .description = "BTS-mode test", |
| 21 | .color = "\033[1;35m", |
| 22 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 23 | }, |
| 24 | }; |
| 25 | |
| 26 | const struct log_info bts_test_log_info = { |
| 27 | .filter_fn = NULL, |
| 28 | .cat = bts_test_cat, |
| 29 | .num_cat = ARRAY_SIZE(bts_test_cat), |
| 30 | }; |
| 31 | |
Pablo Neira Ayuso | 009573a | 2011-07-08 14:17:15 +0200 | [diff] [blame] | 32 | enum bts_state_machine { |
| 33 | BTS_TEST_OML_SIGN_LINK_DOWN = 0, |
| 34 | BTS_TEST_OML_SIGN_LINK_UP, |
| 35 | BTS_TEST_OML_WAIT_SW_ACT_ACK, |
| 36 | }; |
| 37 | |
Pablo Neira Ayuso | 2ad22b8 | 2011-07-08 18:35:45 +0200 | [diff] [blame] | 38 | static struct osmo_fd bts_eventfd; |
| 39 | static int eventfds[2]; |
| 40 | |
Pablo Neira Ayuso | 009573a | 2011-07-08 14:17:15 +0200 | [diff] [blame] | 41 | static enum bts_state_machine bts_state = BTS_TEST_OML_SIGN_LINK_DOWN; |
| 42 | |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 43 | static struct e1inp_sign_link * |
| 44 | sign_link_up(void *unit, struct e1inp_line *line, enum e1inp_sign_type type) |
| 45 | { |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 46 | struct e1inp_sign_link *sign_link = NULL; |
| 47 | void *dst = NULL; |
| 48 | |
| 49 | switch(type) { |
| 50 | case E1INP_SIGN_OML: |
Pablo Neira Ayuso | 2b35201 | 2011-07-02 17:45:42 +0200 | [diff] [blame] | 51 | LOGP(DBTSTEST, LOGL_NOTICE, "OML link up request received.\n"); |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 52 | |
| 53 | e1inp_ts_config_sign(&line->ts[E1INP_SIGN_OML - 1], line); |
| 54 | sign_link = oml_sign_link = |
| 55 | e1inp_sign_link_create(&line->ts[E1INP_SIGN_OML - 1], |
| 56 | E1INP_SIGN_OML, NULL, 255, 0); |
Pablo Neira Ayuso | 2b35201 | 2011-07-02 17:45:42 +0200 | [diff] [blame] | 57 | if (!oml_sign_link) { |
| 58 | LOGP(DBTSTEST, LOGL_ERROR, |
| 59 | "cannot create OML sign link\n"); |
| 60 | } |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 61 | dst = oml_sign_link; |
Pablo Neira Ayuso | 2ad22b8 | 2011-07-08 18:35:45 +0200 | [diff] [blame] | 62 | if (oml_sign_link) { |
| 63 | unsigned int event_type = 0; |
| 64 | |
| 65 | /* tell GSM 12.21 that we're ready via our eventfd. */ |
| 66 | if (write(eventfds[1], &event_type, |
| 67 | sizeof(unsigned int)) < 0) { |
| 68 | LOGP(DBTSTEST, LOGL_ERROR, "cannot write " |
| 69 | "event fd.\n"); |
| 70 | } |
| 71 | /* Now we can send OML messages to the BSC. */ |
| 72 | bts_state = BTS_TEST_OML_SIGN_LINK_UP; |
| 73 | } |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 74 | break; |
| 75 | case E1INP_SIGN_RSL: |
Pablo Neira Ayuso | 2b35201 | 2011-07-02 17:45:42 +0200 | [diff] [blame] | 76 | LOGP(DBTSTEST, LOGL_NOTICE, "RSL link up request received.\n"); |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 77 | |
| 78 | e1inp_ts_config_sign(&line->ts[E1INP_SIGN_RSL - 1], line); |
| 79 | |
| 80 | sign_link = rsl_sign_link = |
| 81 | e1inp_sign_link_create(&line->ts[E1INP_SIGN_RSL - 1], |
| 82 | E1INP_SIGN_RSL, NULL, 0, 0); |
Pablo Neira Ayuso | 2b35201 | 2011-07-02 17:45:42 +0200 | [diff] [blame] | 83 | if (!rsl_sign_link) { |
| 84 | LOGP(DBTSTEST, LOGL_ERROR, |
| 85 | "cannot create RSL sign link\n"); |
| 86 | } |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 87 | dst = rsl_sign_link; |
| 88 | break; |
| 89 | default: |
| 90 | return NULL; |
| 91 | } |
Pablo Neira Ayuso | 2b35201 | 2011-07-02 17:45:42 +0200 | [diff] [blame] | 92 | if (sign_link) |
| 93 | LOGP(DBTSTEST, LOGL_NOTICE, "signal link has been set up.\n"); |
| 94 | |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 95 | return sign_link; |
| 96 | } |
| 97 | |
| 98 | static void sign_link_down(struct e1inp_line *line) |
| 99 | { |
Pablo Neira Ayuso | 2b35201 | 2011-07-02 17:45:42 +0200 | [diff] [blame] | 100 | LOGP(DBTSTEST, LOGL_NOTICE, "signal link has been closed\n"); |
| 101 | e1inp_sign_link_destroy(oml_sign_link); |
| 102 | e1inp_sign_link_destroy(rsl_sign_link); |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Pablo Neira Ayuso | 009573a | 2011-07-08 14:17:15 +0200 | [diff] [blame] | 105 | static int abis_nm_rcvmsg_fom(struct msgb *msg) |
| 106 | { |
| 107 | struct abis_om_fom_hdr *foh = msgb_l3(msg); |
| 108 | uint8_t mt = foh->msg_type; |
| 109 | int ret = 0; |
| 110 | |
| 111 | switch (mt) { |
| 112 | case NM_MT_SW_ACT_REQ_ACK: /* SW activate request ACK from BSC. */ |
| 113 | LOGP(DBTSTEST, LOGL_NOTICE, "Receive SW Act Req ACK\n"); |
| 114 | break; |
| 115 | default: |
| 116 | LOGP(DBTSTEST, LOGL_ERROR, "unknown OML message\n"); |
| 117 | break; |
| 118 | } |
| 119 | return ret; |
| 120 | } |
| 121 | |
| 122 | static int abis_nm_rcvmsg(struct msgb *msg) |
| 123 | { |
| 124 | int ret = 0; |
| 125 | struct abis_om_hdr *oh = msgb_l2(msg); |
| 126 | |
| 127 | msg->l3h = (unsigned char *)oh + sizeof(*oh); |
| 128 | switch (oh->mdisc) { |
| 129 | case ABIS_OM_MDISC_FOM: |
| 130 | ret = abis_nm_rcvmsg_fom(msg); |
| 131 | break; |
| 132 | default: |
| 133 | LOGP(DBTSTEST, LOGL_ERROR, "unknown OML message\n"); |
| 134 | break; |
| 135 | } |
| 136 | return ret; |
| 137 | } |
| 138 | |
Pablo Neira Ayuso | dbd82fb | 2011-07-05 15:29:23 +0200 | [diff] [blame] | 139 | static int sign_link(struct msgb *msg) |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 140 | { |
Pablo Neira Ayuso | 009573a | 2011-07-08 14:17:15 +0200 | [diff] [blame] | 141 | int ret = 0; |
| 142 | struct e1inp_sign_link *link = msg->dst; |
| 143 | |
| 144 | switch(link->type) { |
| 145 | case E1INP_SIGN_OML: |
| 146 | LOGP(DBTSTEST, LOGL_NOTICE, "OML message received.\n"); |
| 147 | ret = abis_nm_rcvmsg(msg); |
| 148 | break; |
| 149 | case E1INP_SIGN_RSL: |
| 150 | LOGP(DBTSTEST, LOGL_NOTICE, "RSL message received.\n"); |
| 151 | break; |
| 152 | default: |
| 153 | LOGP(DBTSTEST, LOGL_ERROR, "Unknown signalling message.\n"); |
| 154 | break; |
| 155 | } |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | static void fill_om_hdr(struct abis_om_hdr *oh, uint8_t len) |
| 160 | { |
| 161 | oh->mdisc = ABIS_OM_MDISC_FOM; |
| 162 | oh->placement = ABIS_OM_PLACEMENT_ONLY; |
| 163 | oh->sequence = 0; |
| 164 | oh->length = len; |
| 165 | } |
| 166 | |
| 167 | static void fill_om_fom_hdr(struct abis_om_hdr *oh, uint8_t len, |
| 168 | uint8_t msg_type, uint8_t obj_class, |
| 169 | uint8_t bts_nr, uint8_t trx_nr, uint8_t ts_nr) |
| 170 | { |
| 171 | struct abis_om_fom_hdr *foh = (struct abis_om_fom_hdr *) oh->data; |
| 172 | |
| 173 | fill_om_hdr(oh, len+sizeof(*foh)); |
| 174 | foh->msg_type = msg_type; |
| 175 | foh->obj_class = obj_class; |
| 176 | foh->obj_inst.bts_nr = bts_nr; |
| 177 | foh->obj_inst.trx_nr = trx_nr; |
| 178 | foh->obj_inst.ts_nr = ts_nr; |
| 179 | } |
| 180 | |
| 181 | #define OM_ALLOC_SIZE 1024 |
| 182 | #define OM_HEADROOM_SIZE 128 |
| 183 | |
| 184 | static struct msgb *nm_msgb_alloc(void) |
| 185 | { |
| 186 | return msgb_alloc_headroom(OM_ALLOC_SIZE, OM_HEADROOM_SIZE, "BTS/test"); |
| 187 | } |
| 188 | |
| 189 | static int abis_nm_sw_act_req(struct e1inp_sign_link *sign_link, |
| 190 | uint8_t obj_class, |
| 191 | uint8_t i1, uint8_t i2, uint8_t i3, |
| 192 | uint8_t *attr, int att_len) |
| 193 | { |
| 194 | struct abis_om_hdr *oh; |
| 195 | struct msgb *msg = nm_msgb_alloc(); |
| 196 | uint8_t msgtype = NM_MT_SW_ACT_REQ; |
| 197 | |
| 198 | oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE); |
| 199 | fill_om_fom_hdr(oh, att_len, msgtype, obj_class, i1, i2, i3); |
| 200 | |
| 201 | if (attr) { |
| 202 | uint8_t *ptr = msgb_put(msg, att_len); |
| 203 | memcpy(ptr, attr, att_len); |
| 204 | } |
| 205 | msg->dst = sign_link; |
| 206 | return abis_sendmsg(msg); |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 207 | } |
| 208 | |
Pablo Neira Ayuso | 2ad22b8 | 2011-07-08 18:35:45 +0200 | [diff] [blame] | 209 | static int test_bts_gsm_12_21_cb(struct osmo_fd *ofd, unsigned int what) |
| 210 | { |
| 211 | int ret; |
| 212 | struct ipaccess_unit *unit = ofd->data; |
| 213 | |
| 214 | switch(bts_state) { |
| 215 | case BTS_TEST_OML_SIGN_LINK_DOWN: |
| 216 | /* Do nothing until OML link becomes ready. */ |
| 217 | break; |
| 218 | case BTS_TEST_OML_SIGN_LINK_UP: |
| 219 | /* OML link is up, send SW ACT REQ. */ |
| 220 | ret = abis_nm_sw_act_req(oml_sign_link, 0, |
| 221 | unit->bts_id, |
| 222 | unit->trx_id, |
| 223 | 0, NULL, 0); |
| 224 | bts_state = BTS_TEST_OML_WAIT_SW_ACT_ACK; |
| 225 | break; |
| 226 | case BTS_TEST_OML_WAIT_SW_ACT_ACK: |
| 227 | /* ... things should continue after this. */ |
| 228 | break; |
| 229 | } |
| 230 | return 0; |
| 231 | } |
| 232 | |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 233 | int main(void) |
| 234 | { |
Pablo Neira Ayuso | dfafe68 | 2011-07-02 14:32:32 +0200 | [diff] [blame] | 235 | struct ipaccess_unit bts_dev_info = { |
Pablo Neira Ayuso | 6268416 | 2011-08-17 23:25:23 +0200 | [diff] [blame] | 236 | .site_id = 1801, |
Pablo Neira Ayuso | dfafe68 | 2011-07-02 14:32:32 +0200 | [diff] [blame] | 237 | .bts_id = 0, |
| 238 | .trx_id = 0, |
| 239 | .unit_name = "testBTS", |
| 240 | .equipvers = "0.1", |
| 241 | .swversion = "0.1", |
| 242 | .mac_addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, |
| 243 | .location1 = "testBTS", |
| 244 | .location2 = "testBTS", |
| 245 | .serno = "", |
| 246 | }; |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 247 | struct e1inp_line *line; |
| 248 | |
| 249 | tall_test = talloc_named_const(NULL, 1, "e1inp_test"); |
| 250 | libosmo_abis_init(tall_test); |
| 251 | |
| 252 | osmo_init_logging(&bts_test_log_info); |
| 253 | |
| 254 | struct e1inp_line_ops ops = { |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 255 | .role = E1INP_LINE_R_BTS, |
| 256 | .addr = "127.0.0.1", |
Pablo Neira Ayuso | dfafe68 | 2011-07-02 14:32:32 +0200 | [diff] [blame] | 257 | .data = &bts_dev_info, |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 258 | .sign_link_up = sign_link_up, |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 259 | .sign_link_down = sign_link_down, |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 260 | .sign_link = sign_link, |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 261 | }; |
| 262 | |
| 263 | #define LINENR 0 |
| 264 | |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 265 | line = e1inp_line_create(LINENR, "ipa"); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 266 | if (line == NULL) { |
| 267 | LOGP(DBTSTEST, LOGL_ERROR, "problem enabling E1 line\n"); |
| 268 | exit(EXIT_FAILURE); |
| 269 | } |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 270 | e1inp_line_bind_ops(line, &ops); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 271 | |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 272 | if (e1inp_line_update(line) < 0) { |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 273 | LOGP(DBTSTEST, LOGL_ERROR, "problem enabling E1 line\n"); |
| 274 | exit(EXIT_FAILURE); |
| 275 | } |
| 276 | |
| 277 | LOGP(DBTSTEST, LOGL_NOTICE, "entering main loop\n"); |
| 278 | |
Pablo Neira Ayuso | 2ad22b8 | 2011-07-08 18:35:45 +0200 | [diff] [blame] | 279 | if (pipe(eventfds) < 0) { |
| 280 | LOGP(DBTSTEST, LOGL_ERROR, "cannot create pipe fds\n"); |
| 281 | exit(EXIT_FAILURE); |
| 282 | } |
| 283 | bts_eventfd.fd = eventfds[0]; |
| 284 | bts_eventfd.cb = test_bts_gsm_12_21_cb; |
| 285 | bts_eventfd.when = BSC_FD_READ; |
| 286 | bts_eventfd.data = &bts_dev_info; |
| 287 | if (osmo_fd_register(&bts_eventfd) < 0) { |
| 288 | LOGP(DBTSTEST, LOGL_ERROR, "could not register event fd\n"); |
| 289 | exit(EXIT_FAILURE); |
| 290 | } |
| 291 | |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 292 | while (1) { |
| 293 | osmo_select_main(0); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 294 | } |
| 295 | return 0; |
| 296 | } |