blob: b4594dea5bc2b17bb729fdc6a97bdafcb0da468a [file] [log] [blame]
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +01001/*
2 * (C) 2011 by Holger Hans Peter Freyther
3 * (C) 2011 by On-Waves
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 */
21
Holger Hans Peter Freytheraf723a42012-12-26 10:51:00 +010022#include <osmocom/core/application.h>
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010023#include <osmocom/core/logging.h>
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +000024#include <osmocom/core/utils.h>
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010025#include <osmocom/gsm/lapdm.h>
26#include <osmocom/gsm/rsl.h>
27
28#include <errno.h>
29
30#include <string.h>
31
32#define CHECK_RC(rc) \
33 if (rc != 0) { \
34 printf("Operation failed rc=%d on %s:%d\n", rc, __FILE__, __LINE__); \
35 abort(); \
36 }
37
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010038static struct log_info info = {};
39
40struct lapdm_polling_state {
41 struct lapdm_channel *bts;
42 int bts_read;
43
44 struct lapdm_channel *ms;
45 int ms_read;
46};
47
48static struct msgb *msgb_from_array(const uint8_t *data, int len)
49{
50 struct msgb *msg = msgb_alloc_headroom(4096, 128, "data");
51 msg->l3h = msgb_put(msg, len);
52 memcpy(msg->l3h, data, len);
53 return msg;
54}
55
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +010056/*
57 * Test data is below...
58 */
59static const uint8_t cm[] = {
60 0x05, 0x24, 0x31, 0x03, 0x50, 0x18, 0x93, 0x08,
61 0x29, 0x47, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80,
62};
63
Andreas Eversberg6e182082013-02-06 14:13:21 +010064static const uint8_t ua[] = {
65 0x01, 0x73, 0x41, 0x05, 0x24, 0x31, 0x03, 0x50,
66 0x18, 0x93, 0x08, 0x29, 0x47, 0x80, 0x00, 0x00,
67 0x00, 0x00, 0x80, 0x2b, 0x2b, 0x2b, 0x2b
68};
69
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +010070static const uint8_t mm[] = {
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +010071 0x00, 0x0c, 0x00, 0x03, 0x01, 0x01, 0x20, 0x02,
72 0x00, 0x0b, 0x00, 0x03, 0x05, 0x04, 0x0d
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +010073};
74
75static const uint8_t dummy1[] = {
76 0xab, 0x03, 0x30, 0x60, 0x06,
77};
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010078
Daniel Willmanne5233922012-12-25 23:15:50 +010079static const uint8_t rel_req[] = {
80 0x02, 0x07, 0x01, 0x0a, 0x02, 0x40, 0x14, 0x01
81};
82
Jacob Erlbeck90937fe2014-01-24 13:48:19 +010083static const uint8_t est_req_sdcch_sapi3[] = {
84 0x02, 0x04, 0x01, 0x20, 0x02, 0x03
85};
86
87static const uint8_t est_req_sacch_sapi3[] = {
88 0x02, 0x04, 0x01, 0x0b, 0x02, 0x43
89};
90
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010091static struct msgb *create_cm_serv_req(void)
92{
93 struct msgb *msg;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010094
95 msg = msgb_from_array(cm, sizeof(cm));
96 rsl_rll_push_l3(msg, RSL_MT_EST_REQ, 0, 0, 1);
97 return msg;
98}
99
100static struct msgb *create_mm_id_req(void)
101{
102 struct msgb *msg;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100103
104 msg = msgb_from_array(mm, sizeof(mm));
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100105 msg->l2h = msg->data + 3;
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000106 OSMO_ASSERT(msgb_l2len(msg) == 12);
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100107 msg->l3h = msg->l2h + 6;
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000108 OSMO_ASSERT(msgb_l3len(msg) == 6);
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100109
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100110 return msg;
111}
112
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +0800113static struct msgb *create_empty_msg(void)
114{
115 struct msgb *msg;
116
117 msg = msgb_from_array(NULL, 0);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000118 OSMO_ASSERT(msgb_l3len(msg) == 0);
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +0800119 rsl_rll_push_l3(msg, RSL_MT_DATA_REQ, 0, 0, 1);
120 return msg;
121}
122
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100123static struct msgb *create_dummy_data_req(void)
124{
125 struct msgb *msg;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100126
127 msg = msgb_from_array(dummy1, sizeof(dummy1));
128 rsl_rll_push_l3(msg, RSL_MT_DATA_REQ, 0, 0, 1);
129 return msg;
130}
131
Daniel Willmanne5233922012-12-25 23:15:50 +0100132static struct msgb *create_rel_req(void)
133{
134 struct msgb *msg;
135
136 msg = msgb_from_array(rel_req, sizeof(rel_req));
137 msg->l2h = msg->data;
138 msg->l3h = msg->l2h + sizeof(struct abis_rsl_rll_hdr);
139 return msg;
140}
141
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100142static struct msgb *create_est_req(const uint8_t *est_req, size_t est_req_size)
143{
144 struct msgb *msg;
145
146 msg = msgb_from_array(est_req, est_req_size);
147 msg->l2h = msg->data;
148 msg->l3h = msg->l2h + sizeof(struct abis_rsl_rll_hdr);
149 return msg;
150}
151
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100152static int send(struct msgb *in_msg, struct lapdm_channel *chan)
153{
154 struct osmo_phsap_prim pp;
155 struct msgb *msg;
156 int rc;
157
158 msg = msgb_alloc_headroom(128, 64, "PH-DATA.ind");
159 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
160 PRIM_OP_INDICATION, msg);
161 /* copy over actual MAC block */
162 msg->l2h = msgb_put(msg, msgb_l2len(in_msg));
163 memcpy(msg->l2h, in_msg->l2h, msgb_l2len(in_msg));
164
165 /* LAPDm requires those... */
166 pp.u.data.chan_nr = 0;
167 pp.u.data.link_id = 0;
168 /* feed into the LAPDm code of libosmogsm */
169 rc = lapdm_phsap_up(&pp.oph, &chan->lapdm_dcch);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000170 OSMO_ASSERT(rc == 0 || rc == -EBUSY);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100171 return 0;
172}
173
Andreas Eversberg6e182082013-02-06 14:13:21 +0100174static int send_sabm(struct lapdm_channel *chan, int second_ms)
175{
176 struct osmo_phsap_prim pp;
177 struct msgb *msg;
178 int rc;
179
180 msg = msgb_alloc_headroom(128, 64, "PH-DATA.ind");
181 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
182 PRIM_OP_INDICATION, msg);
183 /* copy over actual MAC block */
184 msg->l2h = msgb_put(msg, 23);
185 msg->l2h[0] = 0x01;
186 msg->l2h[1] = 0x3f;
187 msg->l2h[2] = 0x01 | (sizeof(cm) << 2);
Holger Hans Peter Freytherdd34ed52013-06-19 08:16:56 +0200188 memcpy(msg->l2h + 3, cm, sizeof(cm));
Andreas Eversberg6e182082013-02-06 14:13:21 +0100189 msg->l2h[3] += second_ms; /* alter message, for second mobile */
190
191 /* LAPDm requires those... */
192 pp.u.data.chan_nr = 0;
193 pp.u.data.link_id = 0;
194 /* feed into the LAPDm code of libosmogsm */
195 rc = lapdm_phsap_up(&pp.oph, &chan->lapdm_dcch);
196 OSMO_ASSERT(rc == 0 || rc == -EBUSY);
197 return 0;
198}
199
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100200/*
201 * I get called from the LAPDm code when something was sent my way...
202 */
203static int bts_to_ms_tx_cb(struct msgb *in_msg, struct lapdm_entity *le, void *_ctx)
204{
205 struct lapdm_polling_state *state = _ctx;
206
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100207
208 printf("%s: MS->BTS(us) message %d\n", __func__, msgb_length(in_msg));
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100209
210
211 if (state->bts_read == 0) {
212 printf("BTS: Verifying CM request.\n");
Holger Hans Peter Freytherdd34ed52013-06-19 08:16:56 +0200213 OSMO_ASSERT(msgb_l3len(in_msg) == ARRAY_SIZE(cm));
214 OSMO_ASSERT(memcmp(in_msg->l3h, cm,
215 ARRAY_SIZE(cm)) == 0);
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100216 } else if (state->bts_read == 1) {
217 printf("BTS: Verifying dummy message.\n");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000218 OSMO_ASSERT(msgb_l3len(in_msg) == ARRAY_SIZE(dummy1));
219 OSMO_ASSERT(memcmp(in_msg->l3h, dummy1,
220 ARRAY_SIZE(dummy1)) == 0);
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100221 } else {
222 printf("BTS: Do not know to verify: %d\n", state->bts_read);
223 }
224
225 state->bts_read += 1;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100226 msgb_free(in_msg);
227
228 return 0;
229}
230
231static int ms_to_bts_l1_cb(struct osmo_prim_hdr *oph, void *_ctx)
232{
233 int rc;
234 struct lapdm_polling_state *state = _ctx;
235 printf("%s: MS(us) -> BTS prim message\n", __func__);
236
237 /* i stuff it into the LAPDm channel of the BTS */
238 rc = send(oph->msg, state->bts);
239 msgb_free(oph->msg);
Holger Hans Peter Freytheraf723a42012-12-26 10:51:00 +0100240 return rc;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100241}
242
243static int ms_to_bts_tx_cb(struct msgb *msg, struct lapdm_entity *le, void *_ctx)
244{
245 struct lapdm_polling_state *state = _ctx;
246
247 printf("%s: BTS->MS(us) message %d\n", __func__, msgb_length(msg));
248
249 if (state->ms_read == 0) {
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100250 struct abis_rsl_rll_hdr hdr;
251
252 printf("MS: Verifying incoming primitive.\n");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000253 OSMO_ASSERT(msg->len == sizeof(struct abis_rsl_rll_hdr) + 3);
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100254
255 /* verify the header */
256 memset(&hdr, 0, sizeof(hdr));
257 rsl_init_rll_hdr(&hdr, RSL_MT_EST_CONF);
258 hdr.c.msg_discr |= ABIS_RSL_MDISC_TRANSP;
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000259 OSMO_ASSERT(memcmp(msg->data, &hdr, sizeof(hdr)) == 0);
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100260
261 /* Verify the added RSL_IE_L3_INFO but we have a bug here */
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000262 OSMO_ASSERT(msg->data[6] == RSL_IE_L3_INFO);
Holger Hans Peter Freyther4a075f82011-12-12 00:41:25 +0100263 #warning "RSL_IE_L3_INFO 16 bit length is wrong"
Holger Hans Peter Freyther3cc268c2013-06-19 08:30:59 +0200264 /* This should be okay but it is actually 0x0, 0x9c on ia-32 */
265 /* OSMO_ASSERT(msg->data[7] == 0x0 && msg->data[8] == 0x0); */
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100266 } else if (state->ms_read == 1) {
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100267 printf("MS: Verifying incoming MM message: %d\n", msgb_l3len(msg));
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000268 OSMO_ASSERT(msgb_l3len(msg) == 3);
269 OSMO_ASSERT(memcmp(msg->l3h, &mm[12], msgb_l3len(msg)) == 0);
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100270 } else {
271 printf("MS: Do not know to verify: %d\n", state->ms_read);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100272 }
273
274 state->ms_read += 1;
275 msgb_free(msg);
276 return 0;
277}
278
279static void test_lapdm_polling()
280{
281 printf("I do some very simple LAPDm test.\n");
282
283 int rc;
284 struct lapdm_polling_state test_state;
285 struct osmo_phsap_prim pp;
286
287 /* Configure LAPDm on both sides */
288 struct lapdm_channel bts_to_ms_channel;
289 struct lapdm_channel ms_to_bts_channel;
290 memset(&bts_to_ms_channel, 0, sizeof(bts_to_ms_channel));
291 memset(&ms_to_bts_channel, 0, sizeof(ms_to_bts_channel));
292
293 memset(&test_state, 0, sizeof(test_state));
294 test_state.bts = &bts_to_ms_channel;
295 test_state.ms = &ms_to_bts_channel;
296
297 /* BTS to MS in polling mode */
298 lapdm_channel_init(&bts_to_ms_channel, LAPDM_MODE_BTS);
299 lapdm_channel_set_flags(&bts_to_ms_channel, LAPDM_ENT_F_POLLING_ONLY);
300 lapdm_channel_set_l1(&bts_to_ms_channel, NULL, &test_state);
301 lapdm_channel_set_l3(&bts_to_ms_channel, bts_to_ms_tx_cb, &test_state);
302
303 /* MS to BTS in direct mode */
304 lapdm_channel_init(&ms_to_bts_channel, LAPDM_MODE_MS);
305 lapdm_channel_set_l1(&ms_to_bts_channel, ms_to_bts_l1_cb, &test_state);
306 lapdm_channel_set_l3(&ms_to_bts_channel, ms_to_bts_tx_cb, &test_state);
307
308 /*
309 * We try to send messages from the MS to the BTS to the MS..
310 */
311 /* 1. Start with MS -> BTS, BTS should have a pending message */
312 printf("Establishing link.\n");
313 lapdm_rslms_recvmsg(create_cm_serv_req(), &ms_to_bts_channel);
314
315 /* 2. Poll on the BTS for sending out a confirmation */
316 printf("\nConfirming\n");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000317 OSMO_ASSERT(test_state.bts_read == 1);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100318 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
319 CHECK_RC(rc);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000320 OSMO_ASSERT(pp.oph.msg->data == pp.oph.msg->l2h);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100321 send(pp.oph.msg, &ms_to_bts_channel);
322 msgb_free(pp.oph.msg);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000323 OSMO_ASSERT(test_state.ms_read == 1);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100324
325 /* 3. Send some data to the MS */
326 printf("\nSending back to MS\n");
327 lapdm_rslms_recvmsg(create_mm_id_req(), &bts_to_ms_channel);
328 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
329 CHECK_RC(rc);
330 send(pp.oph.msg, &ms_to_bts_channel);
331 msgb_free(pp.oph.msg);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000332 OSMO_ASSERT(test_state.ms_read == 2);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100333
334 /* verify that there is nothing more to poll */
335 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000336 OSMO_ASSERT(rc < 0);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100337
338 /* 3. And back to the BTS */
339 printf("\nSending back to BTS\n");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000340 OSMO_ASSERT(test_state.ms_read == 2);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100341 lapdm_rslms_recvmsg(create_dummy_data_req(), &ms_to_bts_channel);
342
343
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100344 /* 4. And back to the MS, but let's move data/l2h apart */
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000345 OSMO_ASSERT(test_state.bts_read == 2);
346 OSMO_ASSERT(test_state.ms_read == 2);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100347 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
348 CHECK_RC(rc);
349 send(pp.oph.msg, &ms_to_bts_channel);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000350 OSMO_ASSERT(test_state.ms_read == 2);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100351 msgb_free(pp.oph.msg);
352
353 /* verify that there is nothing more to poll */
354 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000355 OSMO_ASSERT(rc < 0);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100356
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +0800357 /* check sending an empty L3 message fails */
358 rc = lapdm_rslms_recvmsg(create_empty_msg(), &bts_to_ms_channel);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000359 OSMO_ASSERT(rc == -1);
360 OSMO_ASSERT(test_state.ms_read == 2);
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +0800361
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100362 /* clean up */
363 lapdm_channel_exit(&bts_to_ms_channel);
364 lapdm_channel_exit(&ms_to_bts_channel);
Holger Hans Peter Freytherce397de2013-10-26 13:35:15 +0200365
366 /* Check if exit is idempotent */
367 lapdm_channel_exit(&bts_to_ms_channel);
368 lapdm_channel_exit(&ms_to_bts_channel);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100369}
370
Andreas Eversberg6e182082013-02-06 14:13:21 +0100371static void test_lapdm_contention_resolution()
372{
373 printf("I test contention resultion by having two mobiles collide and "
374 "first mobile repeating SABM.\n");
375
376 int rc;
377 struct lapdm_polling_state test_state;
378 struct osmo_phsap_prim pp;
379
380 /* Configure LAPDm on both sides */
381 struct lapdm_channel bts_to_ms_channel;
382 memset(&bts_to_ms_channel, 0, sizeof(bts_to_ms_channel));
383
384 memset(&test_state, 0, sizeof(test_state));
385 test_state.bts = &bts_to_ms_channel;
386
387 /* BTS to MS in polling mode */
388 lapdm_channel_init(&bts_to_ms_channel, LAPDM_MODE_BTS);
389 lapdm_channel_set_flags(&bts_to_ms_channel, LAPDM_ENT_F_POLLING_ONLY);
390 lapdm_channel_set_l1(&bts_to_ms_channel, NULL, &test_state);
391 lapdm_channel_set_l3(&bts_to_ms_channel, bts_to_ms_tx_cb, &test_state);
392
393 /* Send SABM MS 1, we must get UA */
394 send_sabm(&bts_to_ms_channel, 0);
395 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
396 CHECK_RC(rc);
397 OSMO_ASSERT(memcmp(pp.oph.msg->l2h, ua, ARRAY_SIZE(ua)) == 0);
398
399 /* Send SABM MS 2, we must get nothing, due to collision */
400 send_sabm(&bts_to_ms_channel, 1);
401 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
402 OSMO_ASSERT(rc == -ENODEV);
403
404 /* Send SABM MS 1 again, we must get UA gain */
405 send_sabm(&bts_to_ms_channel, 0);
406 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
407 CHECK_RC(rc);
408 OSMO_ASSERT(memcmp(pp.oph.msg->l2h, ua, ARRAY_SIZE(ua)) == 0);
409
410 /* clean up */
411 lapdm_channel_exit(&bts_to_ms_channel);
Holger Hans Peter Freytherce397de2013-10-26 13:35:15 +0200412
413 /* idempotent */
414 lapdm_channel_exit(&bts_to_ms_channel);
Andreas Eversberg6e182082013-02-06 14:13:21 +0100415}
416
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100417static void test_lapdm_early_release()
418{
419 printf("I test RF channel release of an unestablished channel.\n");
420
421 int rc;
422 struct lapdm_polling_state test_state;
423
424 /* Configure LAPDm on both sides */
425 struct lapdm_channel bts_to_ms_channel;
426 memset(&bts_to_ms_channel, 0, sizeof(bts_to_ms_channel));
427
428 memset(&test_state, 0, sizeof(test_state));
429 test_state.bts = &bts_to_ms_channel;
430
431 /* BTS to MS in polling mode */
432 lapdm_channel_init(&bts_to_ms_channel, LAPDM_MODE_BTS);
433 lapdm_channel_set_flags(&bts_to_ms_channel, LAPDM_ENT_F_POLLING_ONLY);
434 lapdm_channel_set_l1(&bts_to_ms_channel, NULL, &test_state);
435 lapdm_channel_set_l3(&bts_to_ms_channel, bts_to_ms_tx_cb, &test_state);
436
437 /* Send the release request */
438 rc = lapdm_rslms_recvmsg(create_rel_req(), &bts_to_ms_channel);
439 OSMO_ASSERT(rc == -EINVAL);
440
441 /* clean up */
442 lapdm_channel_exit(&bts_to_ms_channel);
443
444 /* Check if exit is idempotent */
445 lapdm_channel_exit(&bts_to_ms_channel);
446}
447
448static void lapdm_establish(const uint8_t *est_req, size_t est_req_size)
449{
450 int rc;
451 struct lapdm_polling_state test_state;
452 struct osmo_phsap_prim pp;
453 struct msgb *msg;
454 const char *queue_name;
455
456 /* Configure LAPDm on both sides */
457 struct lapdm_channel bts_to_ms_channel;
458 memset(&bts_to_ms_channel, 0, sizeof(bts_to_ms_channel));
459
460 memset(&test_state, 0, sizeof(test_state));
461 test_state.bts = &bts_to_ms_channel;
462
463 /* BTS to MS in polling mode */
464 lapdm_channel_init(&bts_to_ms_channel, LAPDM_MODE_BTS);
465 lapdm_channel_set_flags(&bts_to_ms_channel, LAPDM_ENT_F_POLLING_ONLY);
466 lapdm_channel_set_l1(&bts_to_ms_channel, NULL, &test_state);
467 lapdm_channel_set_l3(&bts_to_ms_channel, bts_to_ms_tx_cb, &test_state);
468
469 /* Send the release request */
470 msg = create_est_req(est_req, est_req_size);
471 rc = lapdm_rslms_recvmsg(msg, &bts_to_ms_channel);
472 fprintf(stderr, "recvmsg: got rc %d: %s\n", rc, rc <= 0 ? strerror(-rc) : "???");
473 OSMO_ASSERT(rc == 0);
474
475 /* Take message from queue */
476 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
477 if (rc >= 0)
478 queue_name = "DCCH";
479 else {
480 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_acch, &pp);
481 if (rc >= 0)
482 queue_name = "ACCH";
483 }
484
485 fprintf(stderr, "dequeue: got rc %d: %s\n", rc,
486 rc <= 0 ? strerror(-rc) : "-");
487 CHECK_RC(rc);
488
489 printf("Took message from %s queue: L2 header size %d, "
490 "SAP %#x, %d/%d, Link 0x%02x\n",
491 queue_name, msgb_l2len(pp.oph.msg) - msgb_l3len(pp.oph.msg),
492 pp.oph.sap, pp.oph.primitive, pp.oph.operation,
493 pp.u.data.link_id);
494 printf("Message: %s\n", osmo_hexdump(pp.oph.msg->data, pp.oph.msg->len));
495
496 OSMO_ASSERT(pp.oph.msg->data == msgb_l2(pp.oph.msg));
497
498 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
499 OSMO_ASSERT(rc < 0);
500 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_acch, &pp);
501 OSMO_ASSERT(rc < 0);
502
503 /* clean up */
504 lapdm_channel_exit(&bts_to_ms_channel);
505
506 /* idempotent */
507 lapdm_channel_exit(&bts_to_ms_channel);
508}
509
510static void test_lapdm_establishment()
511{
512 printf("I test RF channel establishment.\n");
513 printf("Testing SAPI3/SDCCH\n");
514 lapdm_establish(est_req_sdcch_sapi3, sizeof(est_req_sdcch_sapi3));
515 printf("Testing SAPI3/SACCH\n");
516 lapdm_establish(est_req_sacch_sapi3, sizeof(est_req_sacch_sapi3));
517}
518
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100519int main(int argc, char **argv)
520{
521 osmo_init_logging(&info);
522
523 test_lapdm_polling();
Daniel Willmanne5233922012-12-25 23:15:50 +0100524 test_lapdm_early_release();
Andreas Eversberg6e182082013-02-06 14:13:21 +0100525 test_lapdm_contention_resolution();
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100526 test_lapdm_establishment();
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100527 printf("Success.\n");
528
529 return 0;
530}