blob: 68dfefb397e0392e41ac4b852c6957f14434e1ec [file] [log] [blame]
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001#include <stdio.h>
Pablo Neira Ayuso1fd93bb2012-08-23 23:26:19 +02002#include <signal.h>
Harald Welte71d87b22011-07-18 14:49:56 +02003#include <osmocom/core/talloc.h>
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +02004#include <string.h>
Pablo Neira Ayuso2ad22b82011-07-08 18:35:45 +02005#include <unistd.h>
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02006#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 Ayusoc9c4fd32011-06-30 12:19:42 +020010#include <osmocom/abis/ipaccess.h>
Pablo Neira Ayuso009573a2011-07-08 14:17:15 +020011#include <osmocom/gsm/protocol/gsm_12_21.h>
Harald Welteb65f58f2014-08-20 22:04:11 +020012#include <osmocom/gsm/ipa.h>
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020013
14static void *tall_test;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020015static struct e1inp_sign_link *oml_sign_link, *rsl_sign_link;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020016
Pablo Neira Ayusofe8ab0a2011-07-02 18:48:45 +020017#define DBTSTEST 0
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020018
19struct log_info_cat bts_test_cat[] = {
20 [DBTSTEST] = {
21 .name = "DBTSTEST",
22 .description = "BTS-mode test",
23 .color = "\033[1;35m",
24 .enabled = 1, .loglevel = LOGL_NOTICE,
25 },
26};
27
28const struct log_info bts_test_log_info = {
29 .filter_fn = NULL,
30 .cat = bts_test_cat,
31 .num_cat = ARRAY_SIZE(bts_test_cat),
32};
33
Pablo Neira Ayuso009573a2011-07-08 14:17:15 +020034enum bts_state_machine {
35 BTS_TEST_OML_SIGN_LINK_DOWN = 0,
36 BTS_TEST_OML_SIGN_LINK_UP,
37 BTS_TEST_OML_WAIT_SW_ACT_ACK,
38};
39
Pablo Neira Ayuso2ad22b82011-07-08 18:35:45 +020040static struct osmo_fd bts_eventfd;
41static int eventfds[2];
42
Pablo Neira Ayuso009573a2011-07-08 14:17:15 +020043static enum bts_state_machine bts_state = BTS_TEST_OML_SIGN_LINK_DOWN;
44
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020045static struct e1inp_sign_link *
46sign_link_up(void *unit, struct e1inp_line *line, enum e1inp_sign_type type)
47{
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020048 struct e1inp_sign_link *sign_link = NULL;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020049
50 switch(type) {
51 case E1INP_SIGN_OML:
Pablo Neira Ayuso2b352012011-07-02 17:45:42 +020052 LOGP(DBTSTEST, LOGL_NOTICE, "OML link up request received.\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020053
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +020054 e1inp_ts_config_sign(e1inp_line_ipa_oml_ts(line), line);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020055 sign_link = oml_sign_link =
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +020056 e1inp_sign_link_create(e1inp_line_ipa_oml_ts(line),
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020057 E1INP_SIGN_OML, NULL, 255, 0);
Pablo Neira Ayuso2b352012011-07-02 17:45:42 +020058 if (!oml_sign_link) {
59 LOGP(DBTSTEST, LOGL_ERROR,
60 "cannot create OML sign link\n");
61 }
Pablo Neira Ayuso2ad22b82011-07-08 18:35:45 +020062 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 }
Holger Hans Peter Freythere6ad7942014-08-29 09:07:05 +020074 e1inp_ipa_bts_rsl_connect_n(line, "127.0.0.1", IPA_TCP_PORT_RSL, 0);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020075 break;
76 case E1INP_SIGN_RSL:
Pablo Neira Ayuso2b352012011-07-02 17:45:42 +020077 LOGP(DBTSTEST, LOGL_NOTICE, "RSL link up request received.\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020078
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +020079 e1inp_ts_config_sign(e1inp_line_ipa_rsl_ts(line, 0), line);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020080
81 sign_link = rsl_sign_link =
Pau Espin Pedrolb5cfc6b2018-10-02 21:22:18 +020082 e1inp_sign_link_create(e1inp_line_ipa_rsl_ts(line, 0),
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020083 E1INP_SIGN_RSL, NULL, 0, 0);
Pablo Neira Ayuso2b352012011-07-02 17:45:42 +020084 if (!rsl_sign_link) {
85 LOGP(DBTSTEST, LOGL_ERROR,
86 "cannot create RSL sign link\n");
87 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020088 break;
89 default:
90 return NULL;
91 }
Pablo Neira Ayuso2b352012011-07-02 17:45:42 +020092 if (sign_link)
93 LOGP(DBTSTEST, LOGL_NOTICE, "signal link has been set up.\n");
94
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020095 return sign_link;
96}
97
98static void sign_link_down(struct e1inp_line *line)
99{
Pablo Neira Ayuso2b352012011-07-02 17:45:42 +0200100 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 Ayusoc9c4fd32011-06-30 12:19:42 +0200103}
104
Pablo Neira Ayuso009573a2011-07-08 14:17:15 +0200105static 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
122static 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 Ayusodbd82fb2011-07-05 15:29:23 +0200139static int sign_link(struct msgb *msg)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200140{
Pablo Neira Ayuso009573a2011-07-08 14:17:15 +0200141 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 }
Pablo Neira Ayuso72516042012-08-23 23:34:05 +0200156 msgb_free(msg);
Pablo Neira Ayuso009573a2011-07-08 14:17:15 +0200157 return ret;
158}
159
160static void fill_om_hdr(struct abis_om_hdr *oh, uint8_t len)
161{
162 oh->mdisc = ABIS_OM_MDISC_FOM;
163 oh->placement = ABIS_OM_PLACEMENT_ONLY;
164 oh->sequence = 0;
165 oh->length = len;
166}
167
168static void fill_om_fom_hdr(struct abis_om_hdr *oh, uint8_t len,
169 uint8_t msg_type, uint8_t obj_class,
170 uint8_t bts_nr, uint8_t trx_nr, uint8_t ts_nr)
171{
172 struct abis_om_fom_hdr *foh = (struct abis_om_fom_hdr *) oh->data;
173
174 fill_om_hdr(oh, len+sizeof(*foh));
175 foh->msg_type = msg_type;
176 foh->obj_class = obj_class;
177 foh->obj_inst.bts_nr = bts_nr;
178 foh->obj_inst.trx_nr = trx_nr;
179 foh->obj_inst.ts_nr = ts_nr;
180}
181
182#define OM_ALLOC_SIZE 1024
183#define OM_HEADROOM_SIZE 128
184
185static struct msgb *nm_msgb_alloc(void)
186{
187 return msgb_alloc_headroom(OM_ALLOC_SIZE, OM_HEADROOM_SIZE, "BTS/test");
188}
189
190static int abis_nm_sw_act_req(struct e1inp_sign_link *sign_link,
191 uint8_t obj_class,
192 uint8_t i1, uint8_t i2, uint8_t i3,
193 uint8_t *attr, int att_len)
194{
195 struct abis_om_hdr *oh;
196 struct msgb *msg = nm_msgb_alloc();
197 uint8_t msgtype = NM_MT_SW_ACT_REQ;
198
199 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
200 fill_om_fom_hdr(oh, att_len, msgtype, obj_class, i1, i2, i3);
201
202 if (attr) {
203 uint8_t *ptr = msgb_put(msg, att_len);
204 memcpy(ptr, attr, att_len);
205 }
206 msg->dst = sign_link;
207 return abis_sendmsg(msg);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200208}
209
Pablo Neira Ayuso2ad22b82011-07-08 18:35:45 +0200210static int test_bts_gsm_12_21_cb(struct osmo_fd *ofd, unsigned int what)
211{
Pablo Neira Ayuso7723b4e2012-08-23 13:51:27 +0200212 int ret, event_type;
Pablo Neira Ayuso2ad22b82011-07-08 18:35:45 +0200213 struct ipaccess_unit *unit = ofd->data;
214
Pablo Neira Ayuso7723b4e2012-08-23 13:51:27 +0200215 if (read(eventfds[0], &event_type, sizeof(unsigned int)) < 0) {
216 LOGP(DBTSTEST, LOGL_ERROR, "error receiving event\n");
217 return 0;
218 }
219
Pablo Neira Ayuso2ad22b82011-07-08 18:35:45 +0200220 switch(bts_state) {
221 case BTS_TEST_OML_SIGN_LINK_DOWN:
222 /* Do nothing until OML link becomes ready. */
223 break;
224 case BTS_TEST_OML_SIGN_LINK_UP:
225 /* OML link is up, send SW ACT REQ. */
226 ret = abis_nm_sw_act_req(oml_sign_link, 0,
227 unit->bts_id,
228 unit->trx_id,
229 0, NULL, 0);
Pablo Neira Ayuso18d62942012-08-22 16:12:47 +0200230 if (ret < 0) {
231 LOGP(DBTSTEST, LOGL_ERROR, "cannot send SW ACT REQ\n");
232 break;
233 }
Pablo Neira Ayuso2ad22b82011-07-08 18:35:45 +0200234 bts_state = BTS_TEST_OML_WAIT_SW_ACT_ACK;
235 break;
236 case BTS_TEST_OML_WAIT_SW_ACT_ACK:
237 /* ... things should continue after this. */
238 break;
239 }
240 return 0;
241}
242
Pablo Neira Ayuso1fd93bb2012-08-23 23:26:19 +0200243static struct e1inp_line *line;
244
245static void sighandler(int foo)
246{
Pau Espin Pedrol3b9bebf2020-07-15 15:49:44 +0200247 e1inp_line_put2(line, "ctor");
Pablo Neira Ayuso1fd93bb2012-08-23 23:26:19 +0200248 exit(EXIT_SUCCESS);
249}
250
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200251int main(void)
252{
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200253 struct ipaccess_unit bts_dev_info = {
Pablo Neira Ayuso62684162011-08-17 23:25:23 +0200254 .site_id = 1801,
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200255 .bts_id = 0,
256 .trx_id = 0,
257 .unit_name = "testBTS",
258 .equipvers = "0.1",
259 .swversion = "0.1",
260 .mac_addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
261 .location1 = "testBTS",
262 .location2 = "testBTS",
263 .serno = "",
264 };
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200265
266 tall_test = talloc_named_const(NULL, 1, "e1inp_test");
267 libosmo_abis_init(tall_test);
Pau Espin Pedrolc04d8d22018-04-17 14:42:44 +0200268 msgb_talloc_ctx_init(tall_test, 0);
269 osmo_init_logging2(tall_test, &bts_test_log_info);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200270
271 struct e1inp_line_ops ops = {
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200272 .cfg = {
273 .ipa = {
274 .role = E1INP_LINE_R_BTS,
275 .addr = "127.0.0.1",
276 .dev = &bts_dev_info,
277 },
278 },
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200279 .sign_link_up = sign_link_up,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200280 .sign_link_down = sign_link_down,
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200281 .sign_link = sign_link,
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200282 };
283
284#define LINENR 0
285
Pablo Neira Ayuso1fd93bb2012-08-23 23:26:19 +0200286 if (signal(SIGINT, sighandler) == SIG_ERR ||
287 signal(SIGTERM, sighandler) == SIG_ERR) {
288 perror("Cannot set sighandler");
289 exit(EXIT_FAILURE);
290 }
291
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200292 line = e1inp_line_create(LINENR, "ipa");
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200293 if (line == NULL) {
294 LOGP(DBTSTEST, LOGL_ERROR, "problem enabling E1 line\n");
295 exit(EXIT_FAILURE);
296 }
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200297 e1inp_line_bind_ops(line, &ops);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200298
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200299 if (e1inp_line_update(line) < 0) {
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200300 LOGP(DBTSTEST, LOGL_ERROR, "problem enabling E1 line\n");
301 exit(EXIT_FAILURE);
302 }
303
304 LOGP(DBTSTEST, LOGL_NOTICE, "entering main loop\n");
305
Pablo Neira Ayuso2ad22b82011-07-08 18:35:45 +0200306 if (pipe(eventfds) < 0) {
307 LOGP(DBTSTEST, LOGL_ERROR, "cannot create pipe fds\n");
308 exit(EXIT_FAILURE);
309 }
Harald Welte6e831b72020-10-18 22:59:58 +0200310 osmo_fd_setup(&bts_eventfd, eventfds[0], OSMO_FD_READ, test_bts_gsm_12_21_cb, &bts_dev_info, 0);
Pablo Neira Ayuso2ad22b82011-07-08 18:35:45 +0200311 if (osmo_fd_register(&bts_eventfd) < 0) {
312 LOGP(DBTSTEST, LOGL_ERROR, "could not register event fd\n");
313 exit(EXIT_FAILURE);
314 }
315
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200316 while (1) {
317 osmo_select_main(0);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200318 }
319 return 0;
320}