blob: 1842ab7eef1efbcac79a0a96079c86f9c3742f19 [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
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100243static int dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp,
244 const char *queue_name)
245{
246 int rc;
247 int l2_header_len;
248 int l3_len = 0;
249
250 /* Take message from queue */
251 rc = lapdm_phsap_dequeue_prim(le, pp);
252
253 fprintf(stderr, "dequeue: got rc %d: %s\n", rc,
254 rc <= 0 ? strerror(-rc) : "-");
255
256 if (rc < 0)
257 return rc;
258
259 l2_header_len = msgb_l2len(pp->oph.msg);
260 if (msgb_l3(pp->oph.msg)) {
261 l3_len = msgb_l3len(pp->oph.msg);
262 l2_header_len -= l3_len;
263 } else
264 fprintf(stderr, "MSGB: L3 is undefined\n");
265
266 if (l2_header_len < 0 || l2_header_len > pp->oph.msg->data_len) {
267 fprintf(stderr,
268 "MSGB inconsistent: data = %p, l2 = %p, l3 = %p, tail = %p\n",
269 pp->oph.msg->data,
270 pp->oph.msg->l2h,
271 pp->oph.msg->l3h,
272 pp->oph.msg->tail);
273 l2_header_len = -1;
274 }
275
276 printf("Took message from %s queue: "
277 "L2 header size %d, L3 size %d, "
278 "SAP %#x, %d/%d, Link 0x%02x\n",
279 queue_name,
280 l2_header_len, l3_len,
281 pp->oph.sap, pp->oph.primitive, pp->oph.operation,
282 pp->u.data.link_id);
283 printf("Message: %s\n", msgb_hexdump(pp->oph.msg));
284
285 return rc;
286}
287
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100288static int ms_to_bts_tx_cb(struct msgb *msg, struct lapdm_entity *le, void *_ctx)
289{
290 struct lapdm_polling_state *state = _ctx;
291
292 printf("%s: BTS->MS(us) message %d\n", __func__, msgb_length(msg));
293
294 if (state->ms_read == 0) {
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100295 struct abis_rsl_rll_hdr hdr;
296
297 printf("MS: Verifying incoming primitive.\n");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000298 OSMO_ASSERT(msg->len == sizeof(struct abis_rsl_rll_hdr) + 3);
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100299
300 /* verify the header */
301 memset(&hdr, 0, sizeof(hdr));
302 rsl_init_rll_hdr(&hdr, RSL_MT_EST_CONF);
303 hdr.c.msg_discr |= ABIS_RSL_MDISC_TRANSP;
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000304 OSMO_ASSERT(memcmp(msg->data, &hdr, sizeof(hdr)) == 0);
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100305
306 /* Verify the added RSL_IE_L3_INFO but we have a bug here */
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000307 OSMO_ASSERT(msg->data[6] == RSL_IE_L3_INFO);
Holger Hans Peter Freyther4a075f82011-12-12 00:41:25 +0100308 #warning "RSL_IE_L3_INFO 16 bit length is wrong"
Holger Hans Peter Freyther3cc268c2013-06-19 08:30:59 +0200309 /* This should be okay but it is actually 0x0, 0x9c on ia-32 */
310 /* OSMO_ASSERT(msg->data[7] == 0x0 && msg->data[8] == 0x0); */
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100311 } else if (state->ms_read == 1) {
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100312 printf("MS: Verifying incoming MM message: %d\n", msgb_l3len(msg));
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000313 OSMO_ASSERT(msgb_l3len(msg) == 3);
314 OSMO_ASSERT(memcmp(msg->l3h, &mm[12], msgb_l3len(msg)) == 0);
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100315 } else {
316 printf("MS: Do not know to verify: %d\n", state->ms_read);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100317 }
318
319 state->ms_read += 1;
320 msgb_free(msg);
321 return 0;
322}
323
324static void test_lapdm_polling()
325{
326 printf("I do some very simple LAPDm test.\n");
327
328 int rc;
329 struct lapdm_polling_state test_state;
330 struct osmo_phsap_prim pp;
331
332 /* Configure LAPDm on both sides */
333 struct lapdm_channel bts_to_ms_channel;
334 struct lapdm_channel ms_to_bts_channel;
335 memset(&bts_to_ms_channel, 0, sizeof(bts_to_ms_channel));
336 memset(&ms_to_bts_channel, 0, sizeof(ms_to_bts_channel));
337
338 memset(&test_state, 0, sizeof(test_state));
339 test_state.bts = &bts_to_ms_channel;
340 test_state.ms = &ms_to_bts_channel;
341
342 /* BTS to MS in polling mode */
343 lapdm_channel_init(&bts_to_ms_channel, LAPDM_MODE_BTS);
344 lapdm_channel_set_flags(&bts_to_ms_channel, LAPDM_ENT_F_POLLING_ONLY);
345 lapdm_channel_set_l1(&bts_to_ms_channel, NULL, &test_state);
346 lapdm_channel_set_l3(&bts_to_ms_channel, bts_to_ms_tx_cb, &test_state);
347
348 /* MS to BTS in direct mode */
349 lapdm_channel_init(&ms_to_bts_channel, LAPDM_MODE_MS);
350 lapdm_channel_set_l1(&ms_to_bts_channel, ms_to_bts_l1_cb, &test_state);
351 lapdm_channel_set_l3(&ms_to_bts_channel, ms_to_bts_tx_cb, &test_state);
352
353 /*
354 * We try to send messages from the MS to the BTS to the MS..
355 */
356 /* 1. Start with MS -> BTS, BTS should have a pending message */
357 printf("Establishing link.\n");
358 lapdm_rslms_recvmsg(create_cm_serv_req(), &ms_to_bts_channel);
359
360 /* 2. Poll on the BTS for sending out a confirmation */
361 printf("\nConfirming\n");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000362 OSMO_ASSERT(test_state.bts_read == 1);
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100363 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100364 CHECK_RC(rc);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000365 OSMO_ASSERT(pp.oph.msg->data == pp.oph.msg->l2h);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100366 send(pp.oph.msg, &ms_to_bts_channel);
367 msgb_free(pp.oph.msg);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000368 OSMO_ASSERT(test_state.ms_read == 1);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100369
370 /* 3. Send some data to the MS */
371 printf("\nSending back to MS\n");
372 lapdm_rslms_recvmsg(create_mm_id_req(), &bts_to_ms_channel);
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100373 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100374 CHECK_RC(rc);
375 send(pp.oph.msg, &ms_to_bts_channel);
376 msgb_free(pp.oph.msg);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000377 OSMO_ASSERT(test_state.ms_read == 2);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100378
379 /* verify that there is nothing more to poll */
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100380 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000381 OSMO_ASSERT(rc < 0);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100382
383 /* 3. And back to the BTS */
384 printf("\nSending back to BTS\n");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000385 OSMO_ASSERT(test_state.ms_read == 2);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100386 lapdm_rslms_recvmsg(create_dummy_data_req(), &ms_to_bts_channel);
387
388
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100389 /* 4. And back to the MS, but let's move data/l2h apart */
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000390 OSMO_ASSERT(test_state.bts_read == 2);
391 OSMO_ASSERT(test_state.ms_read == 2);
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100392 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100393 CHECK_RC(rc);
394 send(pp.oph.msg, &ms_to_bts_channel);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000395 OSMO_ASSERT(test_state.ms_read == 2);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100396 msgb_free(pp.oph.msg);
397
398 /* verify that there is nothing more to poll */
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100399 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000400 OSMO_ASSERT(rc < 0);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100401
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +0800402 /* check sending an empty L3 message fails */
403 rc = lapdm_rslms_recvmsg(create_empty_msg(), &bts_to_ms_channel);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000404 OSMO_ASSERT(rc == -1);
405 OSMO_ASSERT(test_state.ms_read == 2);
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +0800406
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100407 /* clean up */
408 lapdm_channel_exit(&bts_to_ms_channel);
409 lapdm_channel_exit(&ms_to_bts_channel);
Holger Hans Peter Freytherce397de2013-10-26 13:35:15 +0200410
411 /* Check if exit is idempotent */
412 lapdm_channel_exit(&bts_to_ms_channel);
413 lapdm_channel_exit(&ms_to_bts_channel);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100414}
415
Andreas Eversberg6e182082013-02-06 14:13:21 +0100416static void test_lapdm_contention_resolution()
417{
418 printf("I test contention resultion by having two mobiles collide and "
419 "first mobile repeating SABM.\n");
420
421 int rc;
422 struct lapdm_polling_state test_state;
423 struct osmo_phsap_prim pp;
424
425 /* Configure LAPDm on both sides */
426 struct lapdm_channel bts_to_ms_channel;
427 memset(&bts_to_ms_channel, 0, sizeof(bts_to_ms_channel));
428
429 memset(&test_state, 0, sizeof(test_state));
430 test_state.bts = &bts_to_ms_channel;
431
432 /* BTS to MS in polling mode */
433 lapdm_channel_init(&bts_to_ms_channel, LAPDM_MODE_BTS);
434 lapdm_channel_set_flags(&bts_to_ms_channel, LAPDM_ENT_F_POLLING_ONLY);
435 lapdm_channel_set_l1(&bts_to_ms_channel, NULL, &test_state);
436 lapdm_channel_set_l3(&bts_to_ms_channel, bts_to_ms_tx_cb, &test_state);
437
438 /* Send SABM MS 1, we must get UA */
439 send_sabm(&bts_to_ms_channel, 0);
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100440 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
Andreas Eversberg6e182082013-02-06 14:13:21 +0100441 CHECK_RC(rc);
442 OSMO_ASSERT(memcmp(pp.oph.msg->l2h, ua, ARRAY_SIZE(ua)) == 0);
443
444 /* Send SABM MS 2, we must get nothing, due to collision */
445 send_sabm(&bts_to_ms_channel, 1);
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100446 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
Andreas Eversberg6e182082013-02-06 14:13:21 +0100447 OSMO_ASSERT(rc == -ENODEV);
448
449 /* Send SABM MS 1 again, we must get UA gain */
450 send_sabm(&bts_to_ms_channel, 0);
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100451 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
Andreas Eversberg6e182082013-02-06 14:13:21 +0100452 CHECK_RC(rc);
453 OSMO_ASSERT(memcmp(pp.oph.msg->l2h, ua, ARRAY_SIZE(ua)) == 0);
454
455 /* clean up */
456 lapdm_channel_exit(&bts_to_ms_channel);
Holger Hans Peter Freytherce397de2013-10-26 13:35:15 +0200457
458 /* idempotent */
459 lapdm_channel_exit(&bts_to_ms_channel);
Andreas Eversberg6e182082013-02-06 14:13:21 +0100460}
461
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100462static void test_lapdm_early_release()
463{
464 printf("I test RF channel release of an unestablished channel.\n");
465
466 int rc;
467 struct lapdm_polling_state test_state;
468
469 /* Configure LAPDm on both sides */
470 struct lapdm_channel bts_to_ms_channel;
471 memset(&bts_to_ms_channel, 0, sizeof(bts_to_ms_channel));
472
473 memset(&test_state, 0, sizeof(test_state));
474 test_state.bts = &bts_to_ms_channel;
475
476 /* BTS to MS in polling mode */
477 lapdm_channel_init(&bts_to_ms_channel, LAPDM_MODE_BTS);
478 lapdm_channel_set_flags(&bts_to_ms_channel, LAPDM_ENT_F_POLLING_ONLY);
479 lapdm_channel_set_l1(&bts_to_ms_channel, NULL, &test_state);
480 lapdm_channel_set_l3(&bts_to_ms_channel, bts_to_ms_tx_cb, &test_state);
481
482 /* Send the release request */
483 rc = lapdm_rslms_recvmsg(create_rel_req(), &bts_to_ms_channel);
484 OSMO_ASSERT(rc == -EINVAL);
485
486 /* clean up */
487 lapdm_channel_exit(&bts_to_ms_channel);
488
489 /* Check if exit is idempotent */
490 lapdm_channel_exit(&bts_to_ms_channel);
491}
492
493static void lapdm_establish(const uint8_t *est_req, size_t est_req_size)
494{
495 int rc;
496 struct lapdm_polling_state test_state;
497 struct osmo_phsap_prim pp;
498 struct msgb *msg;
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100499
500 /* Configure LAPDm on both sides */
501 struct lapdm_channel bts_to_ms_channel;
502 memset(&bts_to_ms_channel, 0, sizeof(bts_to_ms_channel));
503
504 memset(&test_state, 0, sizeof(test_state));
505 test_state.bts = &bts_to_ms_channel;
506
507 /* BTS to MS in polling mode */
508 lapdm_channel_init(&bts_to_ms_channel, LAPDM_MODE_BTS);
509 lapdm_channel_set_flags(&bts_to_ms_channel, LAPDM_ENT_F_POLLING_ONLY);
510 lapdm_channel_set_l1(&bts_to_ms_channel, NULL, &test_state);
511 lapdm_channel_set_l3(&bts_to_ms_channel, bts_to_ms_tx_cb, &test_state);
512
513 /* Send the release request */
514 msg = create_est_req(est_req, est_req_size);
515 rc = lapdm_rslms_recvmsg(msg, &bts_to_ms_channel);
516 fprintf(stderr, "recvmsg: got rc %d: %s\n", rc, rc <= 0 ? strerror(-rc) : "???");
517 OSMO_ASSERT(rc == 0);
518
519 /* Take message from queue */
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100520 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
521 if (rc < 0)
522 rc = dequeue_prim(&bts_to_ms_channel.lapdm_acch, &pp, "ACCH");
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100523
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100524 CHECK_RC(rc);
525
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100526 OSMO_ASSERT(pp.oph.msg->data == msgb_l2(pp.oph.msg));
527
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100528 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100529 OSMO_ASSERT(rc < 0);
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100530 rc = dequeue_prim(&bts_to_ms_channel.lapdm_acch, &pp, "ACCH");
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100531 OSMO_ASSERT(rc < 0);
532
533 /* clean up */
534 lapdm_channel_exit(&bts_to_ms_channel);
535
536 /* idempotent */
537 lapdm_channel_exit(&bts_to_ms_channel);
538}
539
540static void test_lapdm_establishment()
541{
542 printf("I test RF channel establishment.\n");
543 printf("Testing SAPI3/SDCCH\n");
544 lapdm_establish(est_req_sdcch_sapi3, sizeof(est_req_sdcch_sapi3));
545 printf("Testing SAPI3/SACCH\n");
546 lapdm_establish(est_req_sacch_sapi3, sizeof(est_req_sacch_sapi3));
547}
548
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100549int main(int argc, char **argv)
550{
551 osmo_init_logging(&info);
552
553 test_lapdm_polling();
Daniel Willmanne5233922012-12-25 23:15:50 +0100554 test_lapdm_early_release();
Andreas Eversberg6e182082013-02-06 14:13:21 +0100555 test_lapdm_contention_resolution();
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100556 test_lapdm_establishment();
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100557 printf("Success.\n");
558
559 return 0;
560}