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