blob: 1b9a2dbae4bff7cc4dc5e4dd885afffdaa207f5b [file] [log] [blame]
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001#include <stdio.h>
Harald Welte71d87b22011-07-18 14:49:56 +02002#include <osmocom/core/talloc.h>
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +02003#include <string.h>
Pablo Neira Ayuso2ad22b82011-07-08 18:35:45 +02004#include <unistd.h>
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02005#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 Ayusoc9c4fd32011-06-30 12:19:42 +02009#include <osmocom/abis/ipaccess.h>
Pablo Neira Ayuso009573a2011-07-08 14:17:15 +020010#include <osmocom/gsm/protocol/gsm_12_21.h>
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020011
12static void *tall_test;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020013static struct e1inp_sign_link *oml_sign_link, *rsl_sign_link;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020014
Pablo Neira Ayusofe8ab0a2011-07-02 18:48:45 +020015#define DBTSTEST 0
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020016
17struct 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
26const 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 Ayuso009573a2011-07-08 14:17:15 +020032enum 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 Ayuso2ad22b82011-07-08 18:35:45 +020038static struct osmo_fd bts_eventfd;
39static int eventfds[2];
40
Pablo Neira Ayuso009573a2011-07-08 14:17:15 +020041static enum bts_state_machine bts_state = BTS_TEST_OML_SIGN_LINK_DOWN;
42
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020043static struct e1inp_sign_link *
44sign_link_up(void *unit, struct e1inp_line *line, enum e1inp_sign_type type)
45{
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020046 struct e1inp_sign_link *sign_link = NULL;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020047
48 switch(type) {
49 case E1INP_SIGN_OML:
Pablo Neira Ayuso2b352012011-07-02 17:45:42 +020050 LOGP(DBTSTEST, LOGL_NOTICE, "OML link up request received.\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020051
52 e1inp_ts_config_sign(&line->ts[E1INP_SIGN_OML - 1], line);
53 sign_link = oml_sign_link =
54 e1inp_sign_link_create(&line->ts[E1INP_SIGN_OML - 1],
55 E1INP_SIGN_OML, NULL, 255, 0);
Pablo Neira Ayuso2b352012011-07-02 17:45:42 +020056 if (!oml_sign_link) {
57 LOGP(DBTSTEST, LOGL_ERROR,
58 "cannot create OML sign link\n");
59 }
Pablo Neira Ayuso2ad22b82011-07-08 18:35:45 +020060 if (oml_sign_link) {
61 unsigned int event_type = 0;
62
63 /* tell GSM 12.21 that we're ready via our eventfd. */
64 if (write(eventfds[1], &event_type,
65 sizeof(unsigned int)) < 0) {
66 LOGP(DBTSTEST, LOGL_ERROR, "cannot write "
67 "event fd.\n");
68 }
69 /* Now we can send OML messages to the BSC. */
70 bts_state = BTS_TEST_OML_SIGN_LINK_UP;
71 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020072 break;
73 case E1INP_SIGN_RSL:
Pablo Neira Ayuso2b352012011-07-02 17:45:42 +020074 LOGP(DBTSTEST, LOGL_NOTICE, "RSL link up request received.\n");
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020075
76 e1inp_ts_config_sign(&line->ts[E1INP_SIGN_RSL - 1], line);
77
78 sign_link = rsl_sign_link =
79 e1inp_sign_link_create(&line->ts[E1INP_SIGN_RSL - 1],
80 E1INP_SIGN_RSL, NULL, 0, 0);
Pablo Neira Ayuso2b352012011-07-02 17:45:42 +020081 if (!rsl_sign_link) {
82 LOGP(DBTSTEST, LOGL_ERROR,
83 "cannot create RSL sign link\n");
84 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020085 break;
86 default:
87 return NULL;
88 }
Pablo Neira Ayuso2b352012011-07-02 17:45:42 +020089 if (sign_link)
90 LOGP(DBTSTEST, LOGL_NOTICE, "signal link has been set up.\n");
91
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020092 return sign_link;
93}
94
95static void sign_link_down(struct e1inp_line *line)
96{
Pablo Neira Ayuso2b352012011-07-02 17:45:42 +020097 LOGP(DBTSTEST, LOGL_NOTICE, "signal link has been closed\n");
98 e1inp_sign_link_destroy(oml_sign_link);
99 e1inp_sign_link_destroy(rsl_sign_link);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200100}
101
Pablo Neira Ayuso009573a2011-07-08 14:17:15 +0200102static int abis_nm_rcvmsg_fom(struct msgb *msg)
103{
104 struct abis_om_fom_hdr *foh = msgb_l3(msg);
105 uint8_t mt = foh->msg_type;
106 int ret = 0;
107
108 switch (mt) {
109 case NM_MT_SW_ACT_REQ_ACK: /* SW activate request ACK from BSC. */
110 LOGP(DBTSTEST, LOGL_NOTICE, "Receive SW Act Req ACK\n");
111 break;
112 default:
113 LOGP(DBTSTEST, LOGL_ERROR, "unknown OML message\n");
114 break;
115 }
116 return ret;
117}
118
119static int abis_nm_rcvmsg(struct msgb *msg)
120{
121 int ret = 0;
122 struct abis_om_hdr *oh = msgb_l2(msg);
123
124 msg->l3h = (unsigned char *)oh + sizeof(*oh);
125 switch (oh->mdisc) {
126 case ABIS_OM_MDISC_FOM:
127 ret = abis_nm_rcvmsg_fom(msg);
128 break;
129 default:
130 LOGP(DBTSTEST, LOGL_ERROR, "unknown OML message\n");
131 break;
132 }
133 return ret;
134}
135
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200136static int sign_link(struct msgb *msg)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200137{
Pablo Neira Ayuso009573a2011-07-08 14:17:15 +0200138 int ret = 0;
139 struct e1inp_sign_link *link = msg->dst;
140
141 switch(link->type) {
142 case E1INP_SIGN_OML:
143 LOGP(DBTSTEST, LOGL_NOTICE, "OML message received.\n");
144 ret = abis_nm_rcvmsg(msg);
145 break;
146 case E1INP_SIGN_RSL:
147 LOGP(DBTSTEST, LOGL_NOTICE, "RSL message received.\n");
148 break;
149 default:
150 LOGP(DBTSTEST, LOGL_ERROR, "Unknown signalling message.\n");
151 break;
152 }
153 return ret;
154}
155
156static void fill_om_hdr(struct abis_om_hdr *oh, uint8_t len)
157{
158 oh->mdisc = ABIS_OM_MDISC_FOM;
159 oh->placement = ABIS_OM_PLACEMENT_ONLY;
160 oh->sequence = 0;
161 oh->length = len;
162}
163
164static void fill_om_fom_hdr(struct abis_om_hdr *oh, uint8_t len,
165 uint8_t msg_type, uint8_t obj_class,
166 uint8_t bts_nr, uint8_t trx_nr, uint8_t ts_nr)
167{
168 struct abis_om_fom_hdr *foh = (struct abis_om_fom_hdr *) oh->data;
169
170 fill_om_hdr(oh, len+sizeof(*foh));
171 foh->msg_type = msg_type;
172 foh->obj_class = obj_class;
173 foh->obj_inst.bts_nr = bts_nr;
174 foh->obj_inst.trx_nr = trx_nr;
175 foh->obj_inst.ts_nr = ts_nr;
176}
177
178#define OM_ALLOC_SIZE 1024
179#define OM_HEADROOM_SIZE 128
180
181static struct msgb *nm_msgb_alloc(void)
182{
183 return msgb_alloc_headroom(OM_ALLOC_SIZE, OM_HEADROOM_SIZE, "BTS/test");
184}
185
186static int abis_nm_sw_act_req(struct e1inp_sign_link *sign_link,
187 uint8_t obj_class,
188 uint8_t i1, uint8_t i2, uint8_t i3,
189 uint8_t *attr, int att_len)
190{
191 struct abis_om_hdr *oh;
192 struct msgb *msg = nm_msgb_alloc();
193 uint8_t msgtype = NM_MT_SW_ACT_REQ;
194
195 oh = (struct abis_om_hdr *) msgb_put(msg, ABIS_OM_FOM_HDR_SIZE);
196 fill_om_fom_hdr(oh, att_len, msgtype, obj_class, i1, i2, i3);
197
198 if (attr) {
199 uint8_t *ptr = msgb_put(msg, att_len);
200 memcpy(ptr, attr, att_len);
201 }
202 msg->dst = sign_link;
203 return abis_sendmsg(msg);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200204}
205
Pablo Neira Ayuso2ad22b82011-07-08 18:35:45 +0200206static int test_bts_gsm_12_21_cb(struct osmo_fd *ofd, unsigned int what)
207{
Pablo Neira Ayuso7723b4e2012-08-23 13:51:27 +0200208 int ret, event_type;
Pablo Neira Ayuso2ad22b82011-07-08 18:35:45 +0200209 struct ipaccess_unit *unit = ofd->data;
210
Pablo Neira Ayuso7723b4e2012-08-23 13:51:27 +0200211 if (read(eventfds[0], &event_type, sizeof(unsigned int)) < 0) {
212 LOGP(DBTSTEST, LOGL_ERROR, "error receiving event\n");
213 return 0;
214 }
215
Pablo Neira Ayuso2ad22b82011-07-08 18:35:45 +0200216 switch(bts_state) {
217 case BTS_TEST_OML_SIGN_LINK_DOWN:
218 /* Do nothing until OML link becomes ready. */
219 break;
220 case BTS_TEST_OML_SIGN_LINK_UP:
221 /* OML link is up, send SW ACT REQ. */
222 ret = abis_nm_sw_act_req(oml_sign_link, 0,
223 unit->bts_id,
224 unit->trx_id,
225 0, NULL, 0);
Pablo Neira Ayuso18d62942012-08-22 16:12:47 +0200226 if (ret < 0) {
227 LOGP(DBTSTEST, LOGL_ERROR, "cannot send SW ACT REQ\n");
228 break;
229 }
Pablo Neira Ayuso2ad22b82011-07-08 18:35:45 +0200230 bts_state = BTS_TEST_OML_WAIT_SW_ACT_ACK;
231 break;
232 case BTS_TEST_OML_WAIT_SW_ACT_ACK:
233 /* ... things should continue after this. */
234 break;
235 }
236 return 0;
237}
238
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200239int main(void)
240{
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200241 struct ipaccess_unit bts_dev_info = {
Pablo Neira Ayuso62684162011-08-17 23:25:23 +0200242 .site_id = 1801,
Pablo Neira Ayusodfafe682011-07-02 14:32:32 +0200243 .bts_id = 0,
244 .trx_id = 0,
245 .unit_name = "testBTS",
246 .equipvers = "0.1",
247 .swversion = "0.1",
248 .mac_addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
249 .location1 = "testBTS",
250 .location2 = "testBTS",
251 .serno = "",
252 };
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200253 struct e1inp_line *line;
254
255 tall_test = talloc_named_const(NULL, 1, "e1inp_test");
256 libosmo_abis_init(tall_test);
257
258 osmo_init_logging(&bts_test_log_info);
259
260 struct e1inp_line_ops ops = {
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200261 .cfg = {
262 .ipa = {
263 .role = E1INP_LINE_R_BTS,
264 .addr = "127.0.0.1",
265 .dev = &bts_dev_info,
266 },
267 },
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200268 .sign_link_up = sign_link_up,
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200269 .sign_link_down = sign_link_down,
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200270 .sign_link = sign_link,
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200271 };
272
273#define LINENR 0
274
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200275 line = e1inp_line_create(LINENR, "ipa");
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200276 if (line == NULL) {
277 LOGP(DBTSTEST, LOGL_ERROR, "problem enabling E1 line\n");
278 exit(EXIT_FAILURE);
279 }
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200280 e1inp_line_bind_ops(line, &ops);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200281
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200282 if (e1inp_line_update(line) < 0) {
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200283 LOGP(DBTSTEST, LOGL_ERROR, "problem enabling E1 line\n");
284 exit(EXIT_FAILURE);
285 }
286
287 LOGP(DBTSTEST, LOGL_NOTICE, "entering main loop\n");
288
Pablo Neira Ayuso2ad22b82011-07-08 18:35:45 +0200289 if (pipe(eventfds) < 0) {
290 LOGP(DBTSTEST, LOGL_ERROR, "cannot create pipe fds\n");
291 exit(EXIT_FAILURE);
292 }
293 bts_eventfd.fd = eventfds[0];
294 bts_eventfd.cb = test_bts_gsm_12_21_cb;
295 bts_eventfd.when = BSC_FD_READ;
296 bts_eventfd.data = &bts_dev_info;
297 if (osmo_fd_register(&bts_eventfd) < 0) {
298 LOGP(DBTSTEST, LOGL_ERROR, "could not register event fd\n");
299 exit(EXIT_FAILURE);
300 }
301
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200302 while (1) {
303 osmo_select_main(0);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200304 }
305 return 0;
306}