Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 1 | #include <stdio.h> |
Pablo Neira Ayuso | 54b4979 | 2011-06-07 12:15:26 +0200 | [diff] [blame] | 2 | #include <talloc.h> |
| 3 | #include <osmocom/abis/abis.h> |
Pablo Neira Ayuso | 177094b | 2011-06-07 12:21:51 +0200 | [diff] [blame] | 4 | #include <osmocom/abis/e1_input.h> |
Pablo Neira Ayuso | e19c70a | 2011-06-12 15:15:30 +0200 | [diff] [blame] | 5 | #include <osmocom/core/application.h> |
| 6 | #include <osmocom/core/logging.h> |
Pablo Neira Ayuso | 904d8f2 | 2011-07-05 18:32:12 +0200 | [diff] [blame] | 7 | #include <osmocom/gsm/protocol/gsm_12_21.h> |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 8 | |
Pablo Neira Ayuso | 54b4979 | 2011-06-07 12:15:26 +0200 | [diff] [blame] | 9 | static void *tall_test; |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 10 | static struct e1inp_sign_link *oml_sign_link, *rsl_sign_link; |
Pablo Neira Ayuso | 54b4979 | 2011-06-07 12:15:26 +0200 | [diff] [blame] | 11 | |
Pablo Neira Ayuso | fe8ab0a | 2011-07-02 18:48:45 +0200 | [diff] [blame] | 12 | #define DBSCTEST 0 |
Pablo Neira Ayuso | e19c70a | 2011-06-12 15:15:30 +0200 | [diff] [blame] | 13 | |
| 14 | struct log_info_cat bsc_test_cat[] = { |
| 15 | [DBSCTEST] = { |
| 16 | .name = "DBSCTEST", |
| 17 | .description = "BSC-mode test", |
| 18 | .color = "\033[1;35m", |
| 19 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 20 | }, |
| 21 | }; |
| 22 | |
Pablo Neira Ayuso | 2b35201 | 2011-07-02 17:45:42 +0200 | [diff] [blame] | 23 | static struct e1inp_sign_link * |
| 24 | sign_link_up(void *dev, struct e1inp_line *line, enum e1inp_sign_type type) |
| 25 | { |
| 26 | struct e1inp_sign_link *sign_link = NULL; |
| 27 | |
| 28 | switch(type) { |
| 29 | case E1INP_SIGN_OML: |
| 30 | LOGP(DBSCTEST, LOGL_NOTICE, "OML link up request received.\n"); |
| 31 | e1inp_ts_config_sign(&line->ts[E1INP_SIGN_OML - 1], line); |
| 32 | sign_link = oml_sign_link = |
| 33 | e1inp_sign_link_create(&line->ts[E1INP_SIGN_OML - 1], |
| 34 | E1INP_SIGN_OML, NULL, 255, 0); |
| 35 | break; |
| 36 | case E1INP_SIGN_RSL: |
| 37 | LOGP(DBSCTEST, LOGL_NOTICE, "RSL link up request received.\n"); |
| 38 | e1inp_ts_config_sign(&line->ts[E1INP_SIGN_RSL - 1], line); |
| 39 | sign_link = rsl_sign_link = |
| 40 | e1inp_sign_link_create(&line->ts[E1INP_SIGN_RSL - 1], |
| 41 | E1INP_SIGN_OML, NULL, 0, 0); |
| 42 | break; |
| 43 | default: |
| 44 | break; |
| 45 | } |
| 46 | if (sign_link) |
| 47 | LOGP(DBSCTEST, LOGL_NOTICE, "signal link has been set up.\n"); |
| 48 | |
| 49 | return sign_link; |
| 50 | } |
| 51 | |
| 52 | static void sign_link_down(struct e1inp_line *line) |
| 53 | { |
| 54 | LOGP(DBSCTEST, LOGL_NOTICE, "signal link has been closed\n"); |
| 55 | e1inp_sign_link_destroy(oml_sign_link); |
| 56 | e1inp_sign_link_destroy(rsl_sign_link); |
| 57 | } |
| 58 | |
Pablo Neira Ayuso | 904d8f2 | 2011-07-05 18:32:12 +0200 | [diff] [blame] | 59 | static void fill_om_hdr(struct abis_om_hdr *oh, uint8_t len) |
| 60 | { |
| 61 | oh->mdisc = ABIS_OM_MDISC_FOM; |
| 62 | oh->placement = ABIS_OM_PLACEMENT_ONLY; |
| 63 | oh->sequence = 0; |
| 64 | oh->length = len; |
| 65 | } |
| 66 | |
| 67 | static void fill_om_fom_hdr(struct abis_om_hdr *oh, uint8_t len, |
| 68 | uint8_t msg_type, uint8_t obj_class, |
| 69 | uint8_t bts_nr, uint8_t trx_nr, uint8_t ts_nr) |
| 70 | { |
| 71 | struct abis_om_fom_hdr *foh = |
| 72 | (struct abis_om_fom_hdr *) oh->data; |
| 73 | |
| 74 | fill_om_hdr(oh, len+sizeof(*foh)); |
| 75 | foh->msg_type = msg_type; |
| 76 | foh->obj_class = obj_class; |
| 77 | foh->obj_inst.bts_nr = bts_nr; |
| 78 | foh->obj_inst.trx_nr = trx_nr; |
| 79 | foh->obj_inst.ts_nr = ts_nr; |
| 80 | } |
| 81 | |
| 82 | #define OM_ALLOC_SIZE 1024 |
| 83 | #define OM_HEADROOM_SIZE 128 |
| 84 | |
| 85 | static struct msgb *nm_msgb_alloc(void) |
| 86 | { |
| 87 | return msgb_alloc_headroom(OM_ALLOC_SIZE, OM_HEADROOM_SIZE, "OML"); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | static int abis_nm_sw_act_req_ack(struct e1inp_sign_link *sign_link, |
| 92 | uint8_t obj_class, |
| 93 | uint8_t i1, uint8_t i2, uint8_t i3, |
| 94 | uint8_t *attr, int att_len) |
| 95 | { |
| 96 | struct abis_om_hdr *oh; |
| 97 | struct msgb *msg = nm_msgb_alloc(); |
| 98 | uint8_t msgtype = NM_MT_SW_ACT_REQ_ACK; |
| 99 | |
| 100 | oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE); |
| 101 | fill_om_fom_hdr(oh, att_len, msgtype, obj_class, i1, i2, i3); |
| 102 | |
| 103 | if (attr) { |
| 104 | uint8_t *ptr = msgb_put(msg, att_len); |
| 105 | memcpy(ptr, attr, att_len); |
| 106 | } |
| 107 | msg->dst = sign_link; |
| 108 | return abis_sendmsg(msg); |
| 109 | } |
| 110 | |
| 111 | static int abis_nm_rx_sw_act_req(struct msgb *msg) |
| 112 | { |
| 113 | struct abis_om_hdr *oh = msgb_l2(msg); |
| 114 | struct abis_om_fom_hdr *foh = msgb_l3(msg); |
| 115 | int ret; |
| 116 | |
| 117 | ret = abis_nm_sw_act_req_ack(msg->dst, |
| 118 | foh->obj_class, |
| 119 | foh->obj_inst.bts_nr, |
| 120 | foh->obj_inst.trx_nr, |
| 121 | foh->obj_inst.ts_nr, |
| 122 | foh->data, oh->length-sizeof(*foh)); |
| 123 | |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | static int abis_nm_rcvmsg_fom(struct msgb *msg) |
| 128 | { |
| 129 | struct abis_om_fom_hdr *foh = msgb_l3(msg); |
| 130 | uint8_t mt = foh->msg_type; |
| 131 | int ret = 0; |
| 132 | |
| 133 | switch (mt) { |
| 134 | case NM_MT_SW_ACT_REQ: /* Software activate request from BTS. */ |
| 135 | ret = abis_nm_rx_sw_act_req(msg); |
| 136 | break; |
| 137 | default: |
| 138 | break; |
| 139 | } |
| 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | static int abis_nm_rcvmsg(struct msgb *msg) |
| 144 | { |
| 145 | int ret = 0; |
| 146 | struct abis_om_hdr *oh = msgb_l2(msg); |
| 147 | |
| 148 | msg->l3h = (unsigned char *)oh + sizeof(*oh); |
| 149 | switch (oh->mdisc) { |
| 150 | case ABIS_OM_MDISC_FOM: |
| 151 | ret = abis_nm_rcvmsg_fom(msg); |
| 152 | break; |
| 153 | default: |
| 154 | LOGP(DBSCTEST, LOGL_ERROR, "unknown OML message\n"); |
| 155 | break; |
| 156 | } |
| 157 | return ret; |
| 158 | } |
| 159 | |
Pablo Neira Ayuso | dbd82fb | 2011-07-05 15:29:23 +0200 | [diff] [blame] | 160 | static int sign_link(struct msgb *msg) |
Pablo Neira Ayuso | 2b35201 | 2011-07-02 17:45:42 +0200 | [diff] [blame] | 161 | { |
Pablo Neira Ayuso | 904d8f2 | 2011-07-05 18:32:12 +0200 | [diff] [blame] | 162 | int ret = 0; |
| 163 | struct e1inp_sign_link *link = msg->dst; |
| 164 | |
| 165 | switch(link->type) { |
| 166 | case E1INP_SIGN_RSL: |
| 167 | LOGP(DBSCTEST, LOGL_NOTICE, "RSL message received.\n"); |
| 168 | break; |
| 169 | case E1INP_SIGN_OML: |
| 170 | LOGP(DBSCTEST, LOGL_NOTICE, "OML message received.\n"); |
| 171 | ret = abis_nm_rcvmsg(msg); |
| 172 | break; |
| 173 | default: |
| 174 | LOGP(DBSCTEST, LOGL_ERROR, "Unknown signallin message.\n"); |
| 175 | break; |
| 176 | } |
| 177 | return ret; |
Pablo Neira Ayuso | 2b35201 | 2011-07-02 17:45:42 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Pablo Neira Ayuso | e19c70a | 2011-06-12 15:15:30 +0200 | [diff] [blame] | 180 | const struct log_info bsc_test_log_info = { |
| 181 | .filter_fn = NULL, |
| 182 | .cat = bsc_test_cat, |
| 183 | .num_cat = ARRAY_SIZE(bsc_test_cat), |
| 184 | }; |
| 185 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 186 | int main(void) |
| 187 | { |
| 188 | struct e1inp_line *line; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 189 | |
Pablo Neira Ayuso | 54b4979 | 2011-06-07 12:15:26 +0200 | [diff] [blame] | 190 | tall_test = talloc_named_const(NULL, 1, "e1inp_test"); |
| 191 | libosmo_abis_init(tall_test); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 192 | |
Pablo Neira Ayuso | e19c70a | 2011-06-12 15:15:30 +0200 | [diff] [blame] | 193 | osmo_init_logging(&bsc_test_log_info); |
| 194 | |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 195 | struct e1inp_line_ops ops = { |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 196 | .addr = "0.0.0.0", |
| 197 | .role = E1INP_LINE_R_BSC, |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 198 | .sign_link_up = sign_link_up, |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 199 | .sign_link_down = sign_link_down, |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 200 | .sign_link = sign_link, |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 201 | }; |
| 202 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 203 | #define LINENR 0 |
| 204 | |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 205 | line = e1inp_line_create(LINENR, "ipa"); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 206 | if (line == NULL) { |
Pablo Neira Ayuso | e19c70a | 2011-06-12 15:15:30 +0200 | [diff] [blame] | 207 | LOGP(DBSCTEST, LOGL_ERROR, "problem creating E1 line\n"); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 208 | exit(EXIT_FAILURE); |
| 209 | } |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 210 | |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 211 | e1inp_line_bind_ops(line, &ops); |
| 212 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 213 | /* |
| 214 | * Depending if this is a real or virtual E1 lines: |
| 215 | * - real (ISDN): create signal link for OML and RSL before line up. |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 216 | * - vitual (INET): we create it in signal_link_up(...) callback. |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 217 | * |
| 218 | * The signal link is created via e1inp_sign_link_create(...) |
| 219 | * |
| 220 | * See e1_reconfig_trx and e1_reconfig_bts in libbsc/e1_config.c, |
| 221 | * it explains how this is done with ISDN. |
| 222 | */ |
| 223 | |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 224 | if (e1inp_line_update(line) < 0) { |
Pablo Neira Ayuso | e19c70a | 2011-06-12 15:15:30 +0200 | [diff] [blame] | 225 | LOGP(DBSCTEST, LOGL_ERROR, "problem creating E1 line\n"); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 226 | exit(EXIT_FAILURE); |
| 227 | } |
| 228 | |
Pablo Neira Ayuso | e19c70a | 2011-06-12 15:15:30 +0200 | [diff] [blame] | 229 | LOGP(DBSCTEST, LOGL_NOTICE, "entering main loop\n"); |
| 230 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 231 | while (1) { |
| 232 | osmo_select_main(0); |
| 233 | } |
| 234 | return 0; |
| 235 | } |