blob: 1a06cb1cd22cc866f208afc9e67f7d9ebd1c252f [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
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010083static struct msgb *create_cm_serv_req(void)
84{
85 struct msgb *msg;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010086
87 msg = msgb_from_array(cm, sizeof(cm));
88 rsl_rll_push_l3(msg, RSL_MT_EST_REQ, 0, 0, 1);
89 return msg;
90}
91
92static struct msgb *create_mm_id_req(void)
93{
94 struct msgb *msg;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010095
96 msg = msgb_from_array(mm, sizeof(mm));
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +010097 msg->l2h = msg->data + 3;
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +000098 OSMO_ASSERT(msgb_l2len(msg) == 12);
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +010099 msg->l3h = msg->l2h + 6;
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000100 OSMO_ASSERT(msgb_l3len(msg) == 6);
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100101
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100102 return msg;
103}
104
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +0800105static struct msgb *create_empty_msg(void)
106{
107 struct msgb *msg;
108
109 msg = msgb_from_array(NULL, 0);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000110 OSMO_ASSERT(msgb_l3len(msg) == 0);
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +0800111 rsl_rll_push_l3(msg, RSL_MT_DATA_REQ, 0, 0, 1);
112 return msg;
113}
114
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100115static struct msgb *create_dummy_data_req(void)
116{
117 struct msgb *msg;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100118
119 msg = msgb_from_array(dummy1, sizeof(dummy1));
120 rsl_rll_push_l3(msg, RSL_MT_DATA_REQ, 0, 0, 1);
121 return msg;
122}
123
Daniel Willmanne5233922012-12-25 23:15:50 +0100124static struct msgb *create_rel_req(void)
125{
126 struct msgb *msg;
127
128 msg = msgb_from_array(rel_req, sizeof(rel_req));
129 msg->l2h = msg->data;
130 msg->l3h = msg->l2h + sizeof(struct abis_rsl_rll_hdr);
131 return msg;
132}
133
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100134static int send(struct msgb *in_msg, struct lapdm_channel *chan)
135{
136 struct osmo_phsap_prim pp;
137 struct msgb *msg;
138 int rc;
139
140 msg = msgb_alloc_headroom(128, 64, "PH-DATA.ind");
141 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
142 PRIM_OP_INDICATION, msg);
143 /* copy over actual MAC block */
144 msg->l2h = msgb_put(msg, msgb_l2len(in_msg));
145 memcpy(msg->l2h, in_msg->l2h, msgb_l2len(in_msg));
146
147 /* LAPDm requires those... */
148 pp.u.data.chan_nr = 0;
149 pp.u.data.link_id = 0;
150 /* feed into the LAPDm code of libosmogsm */
151 rc = lapdm_phsap_up(&pp.oph, &chan->lapdm_dcch);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000152 OSMO_ASSERT(rc == 0 || rc == -EBUSY);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100153 return 0;
154}
155
Andreas Eversberg6e182082013-02-06 14:13:21 +0100156static int send_sabm(struct lapdm_channel *chan, int second_ms)
157{
158 struct osmo_phsap_prim pp;
159 struct msgb *msg;
160 int rc;
161
162 msg = msgb_alloc_headroom(128, 64, "PH-DATA.ind");
163 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
164 PRIM_OP_INDICATION, msg);
165 /* copy over actual MAC block */
166 msg->l2h = msgb_put(msg, 23);
167 msg->l2h[0] = 0x01;
168 msg->l2h[1] = 0x3f;
169 msg->l2h[2] = 0x01 | (sizeof(cm) << 2);
Holger Hans Peter Freytherdd34ed52013-06-19 08:16:56 +0200170 memcpy(msg->l2h + 3, cm, sizeof(cm));
Andreas Eversberg6e182082013-02-06 14:13:21 +0100171 msg->l2h[3] += second_ms; /* alter message, for second mobile */
172
173 /* LAPDm requires those... */
174 pp.u.data.chan_nr = 0;
175 pp.u.data.link_id = 0;
176 /* feed into the LAPDm code of libosmogsm */
177 rc = lapdm_phsap_up(&pp.oph, &chan->lapdm_dcch);
178 OSMO_ASSERT(rc == 0 || rc == -EBUSY);
179 return 0;
180}
181
Andreas Eversberga3de4df2013-02-06 14:13:21 +0100182static int send_sabm(struct lapdm_channel *chan, int second_ms)
183{
184 struct osmo_phsap_prim pp;
185 struct msgb *msg;
186 int rc;
187
188 msg = msgb_alloc_headroom(128, 64, "PH-DATA.ind");
189 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
190 PRIM_OP_INDICATION, msg);
191 /* copy over actual MAC block */
192 msg->l2h = msgb_put(msg, 23);
193 msg->l2h[0] = 0x01;
194 msg->l2h[1] = 0x3f;
195 msg->l2h[2] = 0x01 | (sizeof(cm) << 2);
196 memcpy(msg->l2h + 3, cm_padded, sizeof(cm_padded));
197 msg->l2h[3] += second_ms; /* alter message, for second mobile */
198
199 /* LAPDm requires those... */
200 pp.u.data.chan_nr = 0;
201 pp.u.data.link_id = 0;
202 /* feed into the LAPDm code of libosmogsm */
203 rc = lapdm_phsap_up(&pp.oph, &chan->lapdm_dcch);
204 ASSERT(rc == 0 || rc == -EBUSY);
205 return 0;
206}
207
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100208/*
209 * I get called from the LAPDm code when something was sent my way...
210 */
211static int bts_to_ms_tx_cb(struct msgb *in_msg, struct lapdm_entity *le, void *_ctx)
212{
213 struct lapdm_polling_state *state = _ctx;
214
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100215
216 printf("%s: MS->BTS(us) message %d\n", __func__, msgb_length(in_msg));
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100217
218
219 if (state->bts_read == 0) {
220 printf("BTS: Verifying CM request.\n");
Holger Hans Peter Freytherdd34ed52013-06-19 08:16:56 +0200221 OSMO_ASSERT(msgb_l3len(in_msg) == ARRAY_SIZE(cm));
222 OSMO_ASSERT(memcmp(in_msg->l3h, cm,
223 ARRAY_SIZE(cm)) == 0);
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100224 } else if (state->bts_read == 1) {
225 printf("BTS: Verifying dummy message.\n");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000226 OSMO_ASSERT(msgb_l3len(in_msg) == ARRAY_SIZE(dummy1));
227 OSMO_ASSERT(memcmp(in_msg->l3h, dummy1,
228 ARRAY_SIZE(dummy1)) == 0);
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100229 } else {
230 printf("BTS: Do not know to verify: %d\n", state->bts_read);
231 }
232
233 state->bts_read += 1;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100234 msgb_free(in_msg);
235
236 return 0;
237}
238
239static int ms_to_bts_l1_cb(struct osmo_prim_hdr *oph, void *_ctx)
240{
241 int rc;
242 struct lapdm_polling_state *state = _ctx;
243 printf("%s: MS(us) -> BTS prim message\n", __func__);
244
245 /* i stuff it into the LAPDm channel of the BTS */
246 rc = send(oph->msg, state->bts);
247 msgb_free(oph->msg);
Holger Hans Peter Freytheraf723a42012-12-26 10:51:00 +0100248 return rc;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100249}
250
251static int ms_to_bts_tx_cb(struct msgb *msg, struct lapdm_entity *le, void *_ctx)
252{
253 struct lapdm_polling_state *state = _ctx;
254
255 printf("%s: BTS->MS(us) message %d\n", __func__, msgb_length(msg));
256
257 if (state->ms_read == 0) {
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100258 struct abis_rsl_rll_hdr hdr;
259
260 printf("MS: Verifying incoming primitive.\n");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000261 OSMO_ASSERT(msg->len == sizeof(struct abis_rsl_rll_hdr) + 3);
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100262
263 /* verify the header */
264 memset(&hdr, 0, sizeof(hdr));
265 rsl_init_rll_hdr(&hdr, RSL_MT_EST_CONF);
266 hdr.c.msg_discr |= ABIS_RSL_MDISC_TRANSP;
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000267 OSMO_ASSERT(memcmp(msg->data, &hdr, sizeof(hdr)) == 0);
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100268
269 /* Verify the added RSL_IE_L3_INFO but we have a bug here */
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000270 OSMO_ASSERT(msg->data[6] == RSL_IE_L3_INFO);
Holger Hans Peter Freyther4a075f82011-12-12 00:41:25 +0100271 #warning "RSL_IE_L3_INFO 16 bit length is wrong"
Holger Hans Peter Freyther3cc268c2013-06-19 08:30:59 +0200272 /* This should be okay but it is actually 0x0, 0x9c on ia-32 */
273 /* OSMO_ASSERT(msg->data[7] == 0x0 && msg->data[8] == 0x0); */
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100274 } else if (state->ms_read == 1) {
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100275 printf("MS: Verifying incoming MM message: %d\n", msgb_l3len(msg));
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000276 OSMO_ASSERT(msgb_l3len(msg) == 3);
277 OSMO_ASSERT(memcmp(msg->l3h, &mm[12], msgb_l3len(msg)) == 0);
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100278 } else {
279 printf("MS: Do not know to verify: %d\n", state->ms_read);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100280 }
281
282 state->ms_read += 1;
283 msgb_free(msg);
284 return 0;
285}
286
287static void test_lapdm_polling()
288{
289 printf("I do some very simple LAPDm test.\n");
290
291 int rc;
292 struct lapdm_polling_state test_state;
293 struct osmo_phsap_prim pp;
294
295 /* Configure LAPDm on both sides */
296 struct lapdm_channel bts_to_ms_channel;
297 struct lapdm_channel ms_to_bts_channel;
298 memset(&bts_to_ms_channel, 0, sizeof(bts_to_ms_channel));
299 memset(&ms_to_bts_channel, 0, sizeof(ms_to_bts_channel));
300
301 memset(&test_state, 0, sizeof(test_state));
302 test_state.bts = &bts_to_ms_channel;
303 test_state.ms = &ms_to_bts_channel;
304
305 /* BTS to MS in polling mode */
306 lapdm_channel_init(&bts_to_ms_channel, LAPDM_MODE_BTS);
307 lapdm_channel_set_flags(&bts_to_ms_channel, LAPDM_ENT_F_POLLING_ONLY);
308 lapdm_channel_set_l1(&bts_to_ms_channel, NULL, &test_state);
309 lapdm_channel_set_l3(&bts_to_ms_channel, bts_to_ms_tx_cb, &test_state);
310
311 /* MS to BTS in direct mode */
312 lapdm_channel_init(&ms_to_bts_channel, LAPDM_MODE_MS);
313 lapdm_channel_set_l1(&ms_to_bts_channel, ms_to_bts_l1_cb, &test_state);
314 lapdm_channel_set_l3(&ms_to_bts_channel, ms_to_bts_tx_cb, &test_state);
315
316 /*
317 * We try to send messages from the MS to the BTS to the MS..
318 */
319 /* 1. Start with MS -> BTS, BTS should have a pending message */
320 printf("Establishing link.\n");
321 lapdm_rslms_recvmsg(create_cm_serv_req(), &ms_to_bts_channel);
322
323 /* 2. Poll on the BTS for sending out a confirmation */
324 printf("\nConfirming\n");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000325 OSMO_ASSERT(test_state.bts_read == 1);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100326 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
327 CHECK_RC(rc);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000328 OSMO_ASSERT(pp.oph.msg->data == pp.oph.msg->l2h);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100329 send(pp.oph.msg, &ms_to_bts_channel);
330 msgb_free(pp.oph.msg);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000331 OSMO_ASSERT(test_state.ms_read == 1);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100332
333 /* 3. Send some data to the MS */
334 printf("\nSending back to MS\n");
335 lapdm_rslms_recvmsg(create_mm_id_req(), &bts_to_ms_channel);
336 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
337 CHECK_RC(rc);
338 send(pp.oph.msg, &ms_to_bts_channel);
339 msgb_free(pp.oph.msg);
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
342 /* verify that there is nothing more to poll */
343 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000344 OSMO_ASSERT(rc < 0);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100345
346 /* 3. And back to the BTS */
347 printf("\nSending back to BTS\n");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000348 OSMO_ASSERT(test_state.ms_read == 2);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100349 lapdm_rslms_recvmsg(create_dummy_data_req(), &ms_to_bts_channel);
350
351
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100352 /* 4. And back to the MS, but let's move data/l2h apart */
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000353 OSMO_ASSERT(test_state.bts_read == 2);
354 OSMO_ASSERT(test_state.ms_read == 2);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100355 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
356 CHECK_RC(rc);
357 send(pp.oph.msg, &ms_to_bts_channel);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000358 OSMO_ASSERT(test_state.ms_read == 2);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100359 msgb_free(pp.oph.msg);
360
361 /* verify that there is nothing more to poll */
362 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000363 OSMO_ASSERT(rc < 0);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100364
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +0800365 /* check sending an empty L3 message fails */
366 rc = lapdm_rslms_recvmsg(create_empty_msg(), &bts_to_ms_channel);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000367 OSMO_ASSERT(rc == -1);
368 OSMO_ASSERT(test_state.ms_read == 2);
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +0800369
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100370 /* clean up */
371 lapdm_channel_exit(&bts_to_ms_channel);
372 lapdm_channel_exit(&ms_to_bts_channel);
373}
374
Daniel Willmanne5233922012-12-25 23:15:50 +0100375static void test_lapdm_early_release()
376{
377 printf("I test RF channel release of an unestablished channel.\n");
378
379 int rc;
380 struct lapdm_polling_state test_state;
381
382 /* Configure LAPDm on both sides */
383 struct lapdm_channel bts_to_ms_channel;
384 memset(&bts_to_ms_channel, 0, sizeof(bts_to_ms_channel));
385
386 memset(&test_state, 0, sizeof(test_state));
387 test_state.bts = &bts_to_ms_channel;
388
389 /* BTS to MS in polling mode */
390 lapdm_channel_init(&bts_to_ms_channel, LAPDM_MODE_BTS);
391 lapdm_channel_set_flags(&bts_to_ms_channel, LAPDM_ENT_F_POLLING_ONLY);
392 lapdm_channel_set_l1(&bts_to_ms_channel, NULL, &test_state);
393 lapdm_channel_set_l3(&bts_to_ms_channel, bts_to_ms_tx_cb, &test_state);
394
395 /* Send the release request */
396 rc = lapdm_rslms_recvmsg(create_rel_req(), &bts_to_ms_channel);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000397 OSMO_ASSERT(rc == -EINVAL);
Daniel Willmanne5233922012-12-25 23:15:50 +0100398
399 /* clean up */
400 lapdm_channel_exit(&bts_to_ms_channel);
401}
402
Andreas Eversberg6e182082013-02-06 14:13:21 +0100403static void test_lapdm_contention_resolution()
404{
405 printf("I test contention resultion by having two mobiles collide and "
406 "first mobile repeating SABM.\n");
407
408 int rc;
409 struct lapdm_polling_state test_state;
410 struct osmo_phsap_prim pp;
411
412 /* Configure LAPDm on both sides */
413 struct lapdm_channel bts_to_ms_channel;
414 memset(&bts_to_ms_channel, 0, sizeof(bts_to_ms_channel));
415
416 memset(&test_state, 0, sizeof(test_state));
417 test_state.bts = &bts_to_ms_channel;
418
419 /* BTS to MS in polling mode */
420 lapdm_channel_init(&bts_to_ms_channel, LAPDM_MODE_BTS);
421 lapdm_channel_set_flags(&bts_to_ms_channel, LAPDM_ENT_F_POLLING_ONLY);
422 lapdm_channel_set_l1(&bts_to_ms_channel, NULL, &test_state);
423 lapdm_channel_set_l3(&bts_to_ms_channel, bts_to_ms_tx_cb, &test_state);
424
425 /* Send SABM MS 1, we must get UA */
426 send_sabm(&bts_to_ms_channel, 0);
427 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
428 CHECK_RC(rc);
429 OSMO_ASSERT(memcmp(pp.oph.msg->l2h, ua, ARRAY_SIZE(ua)) == 0);
430
431 /* Send SABM MS 2, we must get nothing, due to collision */
432 send_sabm(&bts_to_ms_channel, 1);
433 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
434 OSMO_ASSERT(rc == -ENODEV);
435
436 /* Send SABM MS 1 again, we must get UA gain */
437 send_sabm(&bts_to_ms_channel, 0);
438 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
439 CHECK_RC(rc);
440 OSMO_ASSERT(memcmp(pp.oph.msg->l2h, ua, ARRAY_SIZE(ua)) == 0);
441
442 /* clean up */
443 lapdm_channel_exit(&bts_to_ms_channel);
444}
445
Andreas Eversberga3de4df2013-02-06 14:13:21 +0100446static void test_lapdm_contention_resolution()
447{
448 printf("I test contention resultion by having two mobiles collide and "
449 "first mobile repeating SABM.\n");
450
451 int rc;
452 struct lapdm_polling_state test_state;
453 struct osmo_phsap_prim pp;
454
455 /* Configure LAPDm on both sides */
456 struct lapdm_channel bts_to_ms_channel;
457 memset(&bts_to_ms_channel, 0, sizeof(bts_to_ms_channel));
458
459 memset(&test_state, 0, sizeof(test_state));
460 test_state.bts = &bts_to_ms_channel;
461
462 /* BTS to MS in polling mode */
463 lapdm_channel_init(&bts_to_ms_channel, LAPDM_MODE_BTS);
464 lapdm_channel_set_flags(&bts_to_ms_channel, LAPDM_ENT_F_POLLING_ONLY);
465 lapdm_channel_set_l1(&bts_to_ms_channel, NULL, &test_state);
466 lapdm_channel_set_l3(&bts_to_ms_channel, bts_to_ms_tx_cb, &test_state);
467
468 /* Send SABM MS 1, we must get UA */
469 send_sabm(&bts_to_ms_channel, 0);
470 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
471 CHECK_RC(rc);
472 ASSERT(memcmp(pp.oph.msg->l2h, ua, ARRAY_SIZE(ua)) == 0);
473
474 /* Send SABM MS 2, we must get nothing, due to collision */
475 send_sabm(&bts_to_ms_channel, 1);
476 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
477 ASSERT(rc == -ENODEV);
478
479 /* Send SABM MS 1 again, we must get UA gain */
480 send_sabm(&bts_to_ms_channel, 0);
481 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
482 CHECK_RC(rc);
483 ASSERT(memcmp(pp.oph.msg->l2h, ua, ARRAY_SIZE(ua)) == 0);
484
485 /* clean up */
486 lapdm_channel_exit(&bts_to_ms_channel);
487}
488
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100489int main(int argc, char **argv)
490{
491 osmo_init_logging(&info);
492
493 test_lapdm_polling();
Daniel Willmanne5233922012-12-25 23:15:50 +0100494 test_lapdm_early_release();
Andreas Eversberg6e182082013-02-06 14:13:21 +0100495 test_lapdm_contention_resolution();
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100496 printf("Success.\n");
497
498 return 0;
499}