blob: 02a4cb3d8e43fa9f1693dea22a7db76a2d151b5f [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>
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020012
13static void *tall_test;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020014static struct e1inp_sign_link *oml_sign_link, *rsl_sign_link;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020015
Pablo Neira Ayusofe8ab0a2011-07-02 18:48:45 +020016#define DBTSTEST 0
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020017
18struct 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
27const 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 Ayuso009573a2011-07-08 14:17:15 +020033enum 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 Ayuso2ad22b82011-07-08 18:35:45 +020039static struct osmo_fd bts_eventfd;
40static int eventfds[2];
41
Pablo Neira Ayuso009573a2011-07-08 14:17:15 +020042static enum bts_state_machine bts_state = BTS_TEST_OML_SIGN_LINK_DOWN;
43
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020044static struct e1inp_sign_link *
45sign_link_up(void *unit, struct e1inp_line *line, enum e1inp_sign_type type)
46{
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020047 struct e1inp_sign_link *sign_link = NULL;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020048
49 switch(type) {
50 case E1INP_SIGN_OML:
Pablo Neira Ayuso2b352012011-07-02 17:45:42 +020051 LOGP(DBTSTEST, LOGL_NOTICE, "OML link up request received.\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020052
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 Ayuso2b352012011-07-02 17:45:42 +020057 if (!oml_sign_link) {
58 LOGP(DBTSTEST, LOGL_ERROR,
59 "cannot create OML sign link\n");
60 }
Pablo Neira Ayuso2ad22b82011-07-08 18:35:45 +020061 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 }
Harald Welte84f67b22013-06-30 13:13:59 +020073 e1inp_ipa_bts_rsl_connect(line, "127.0.0.1", IPA_TCP_PORT_RSL);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020074 break;
75 case E1INP_SIGN_RSL:
Pablo Neira Ayuso2b352012011-07-02 17:45:42 +020076 LOGP(DBTSTEST, LOGL_NOTICE, "RSL link up request received.\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020077
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 Ayuso2b352012011-07-02 17:45:42 +020083 if (!rsl_sign_link) {
84 LOGP(DBTSTEST, LOGL_ERROR,
85 "cannot create RSL sign link\n");
86 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020087 break;
88 default:
89 return NULL;
90 }
Pablo Neira Ayuso2b352012011-07-02 17:45:42 +020091 if (sign_link)
92 LOGP(DBTSTEST, LOGL_NOTICE, "signal link has been set up.\n");
93
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020094 return sign_link;
95}
96
97static void sign_link_down(struct e1inp_line *line)
98{
Pablo Neira Ayuso2b352012011-07-02 17:45:42 +020099 LOGP(DBTSTEST, LOGL_NOTICE, "signal link has been closed\n");
100 e1inp_sign_link_destroy(oml_sign_link);
101 e1inp_sign_link_destroy(rsl_sign_link);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200102}
103
Pablo Neira Ayuso009573a2011-07-08 14:17:15 +0200104static int abis_nm_rcvmsg_fom(struct msgb *msg)
105{
106 struct abis_om_fom_hdr *foh = msgb_l3(msg);
107 uint8_t mt = foh->msg_type;
108 int ret = 0;
109
110 switch (mt) {
111 case NM_MT_SW_ACT_REQ_ACK: /* SW activate request ACK from BSC. */
112 LOGP(DBTSTEST, LOGL_NOTICE, "Receive SW Act Req ACK\n");
113 break;
114 default:
115 LOGP(DBTSTEST, LOGL_ERROR, "unknown OML message\n");
116 break;
117 }
118 return ret;
119}
120
121static int abis_nm_rcvmsg(struct msgb *msg)
122{
123 int ret = 0;
124 struct abis_om_hdr *oh = msgb_l2(msg);
125
126 msg->l3h = (unsigned char *)oh + sizeof(*oh);
127 switch (oh->mdisc) {
128 case ABIS_OM_MDISC_FOM:
129 ret = abis_nm_rcvmsg_fom(msg);
130 break;
131 default:
132 LOGP(DBTSTEST, LOGL_ERROR, "unknown OML message\n");
133 break;
134 }
135 return ret;
136}
137
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200138static int sign_link(struct msgb *msg)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200139{
Pablo Neira Ayuso009573a2011-07-08 14:17:15 +0200140 int ret = 0;
141 struct e1inp_sign_link *link = msg->dst;
142
143 switch(link->type) {
144 case E1INP_SIGN_OML:
145 LOGP(DBTSTEST, LOGL_NOTICE, "OML message received.\n");
146 ret = abis_nm_rcvmsg(msg);
147 break;
148 case E1INP_SIGN_RSL:
149 LOGP(DBTSTEST, LOGL_NOTICE, "RSL message received.\n");
150 break;
151 default:
152 LOGP(DBTSTEST, LOGL_ERROR, "Unknown signalling message.\n");
153 break;
154 }
Pablo Neira Ayuso72516042012-08-23 23:34:05 +0200155 msgb_free(msg);
Pablo Neira Ayuso009573a2011-07-08 14:17:15 +0200156 return ret;
157}
158
159static 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
167static 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
184static struct msgb *nm_msgb_alloc(void)
185{
186 return msgb_alloc_headroom(OM_ALLOC_SIZE, OM_HEADROOM_SIZE, "BTS/test");
187}
188
189static 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 Ayusoc9c4fd32011-06-30 12:19:42 +0200207}
208
Pablo Neira Ayuso2ad22b82011-07-08 18:35:45 +0200209static int test_bts_gsm_12_21_cb(struct osmo_fd *ofd, unsigned int what)
210{
Pablo Neira Ayuso7723b4e2012-08-23 13:51:27 +0200211 int ret, event_type;
Pablo Neira Ayuso2ad22b82011-07-08 18:35:45 +0200212 struct ipaccess_unit *unit = ofd->data;
213
Pablo Neira Ayuso7723b4e2012-08-23 13:51:27 +0200214 if (read(eventfds[0], &event_type, sizeof(unsigned int)) < 0) {
215 LOGP(DBTSTEST, LOGL_ERROR, "error receiving event\n");
216 return 0;
217 }
218
Pablo Neira Ayuso2ad22b82011-07-08 18:35:45 +0200219 switch(bts_state) {
220 case BTS_TEST_OML_SIGN_LINK_DOWN:
221 /* Do nothing until OML link becomes ready. */
222 break;
223 case BTS_TEST_OML_SIGN_LINK_UP:
224 /* OML link is up, send SW ACT REQ. */
225 ret = abis_nm_sw_act_req(oml_sign_link, 0,
226 unit->bts_id,
227 unit->trx_id,
228 0, NULL, 0);
Pablo Neira Ayuso18d62942012-08-22 16:12:47 +0200229 if (ret < 0) {
230 LOGP(DBTSTEST, LOGL_ERROR, "cannot send SW ACT REQ\n");
231 break;
232 }
Pablo Neira Ayuso2ad22b82011-07-08 18:35:45 +0200233 bts_state = BTS_TEST_OML_WAIT_SW_ACT_ACK;
234 break;
235 case BTS_TEST_OML_WAIT_SW_ACT_ACK:
236 /* ... things should continue after this. */
237 break;
238 }
239 return 0;
240}
241
Pablo Neira Ayuso1fd93bb2012-08-23 23:26:19 +0200242static struct e1inp_line *line;
243
244static void sighandler(int foo)
245{
246 e1inp_line_put(line);
247 exit(EXIT_SUCCESS);
248}
249
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200250int main(void)
251{
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200252 struct ipaccess_unit bts_dev_info = {
Pablo Neira Ayuso62684162011-08-17 23:25:23 +0200253 .site_id = 1801,
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200254 .bts_id = 0,
255 .trx_id = 0,
256 .unit_name = "testBTS",
257 .equipvers = "0.1",
258 .swversion = "0.1",
259 .mac_addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
260 .location1 = "testBTS",
261 .location2 = "testBTS",
262 .serno = "",
263 };
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200264
265 tall_test = talloc_named_const(NULL, 1, "e1inp_test");
266 libosmo_abis_init(tall_test);
267
268 osmo_init_logging(&bts_test_log_info);
269
270 struct e1inp_line_ops ops = {
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200271 .cfg = {
272 .ipa = {
273 .role = E1INP_LINE_R_BTS,
274 .addr = "127.0.0.1",
275 .dev = &bts_dev_info,
276 },
277 },
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200278 .sign_link_up = sign_link_up,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200279 .sign_link_down = sign_link_down,
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200280 .sign_link = sign_link,
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200281 };
282
283#define LINENR 0
284
Pablo Neira Ayuso1fd93bb2012-08-23 23:26:19 +0200285 if (signal(SIGINT, sighandler) == SIG_ERR ||
286 signal(SIGTERM, sighandler) == SIG_ERR) {
287 perror("Cannot set sighandler");
288 exit(EXIT_FAILURE);
289 }
290
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200291 line = e1inp_line_create(LINENR, "ipa");
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200292 if (line == NULL) {
293 LOGP(DBTSTEST, LOGL_ERROR, "problem enabling E1 line\n");
294 exit(EXIT_FAILURE);
295 }
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200296 e1inp_line_bind_ops(line, &ops);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200297
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200298 if (e1inp_line_update(line) < 0) {
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200299 LOGP(DBTSTEST, LOGL_ERROR, "problem enabling E1 line\n");
300 exit(EXIT_FAILURE);
301 }
302
303 LOGP(DBTSTEST, LOGL_NOTICE, "entering main loop\n");
304
Pablo Neira Ayuso2ad22b82011-07-08 18:35:45 +0200305 if (pipe(eventfds) < 0) {
306 LOGP(DBTSTEST, LOGL_ERROR, "cannot create pipe fds\n");
307 exit(EXIT_FAILURE);
308 }
309 bts_eventfd.fd = eventfds[0];
310 bts_eventfd.cb = test_bts_gsm_12_21_cb;
311 bts_eventfd.when = BSC_FD_READ;
312 bts_eventfd.data = &bts_dev_info;
313 if (osmo_fd_register(&bts_eventfd) < 0) {
314 LOGP(DBTSTEST, LOGL_ERROR, "could not register event fd\n");
315 exit(EXIT_FAILURE);
316 }
317
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200318 while (1) {
319 osmo_select_main(0);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200320 }
321 return 0;
322}