blob: acd3cadbd910fb9c780826b73d957e2e1bd1e78a [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>
24#include <osmocom/gsm/lapdm.h>
25#include <osmocom/gsm/rsl.h>
26
27#include <errno.h>
28
29#include <string.h>
30
31#define CHECK_RC(rc) \
32 if (rc != 0) { \
33 printf("Operation failed rc=%d on %s:%d\n", rc, __FILE__, __LINE__); \
34 abort(); \
35 }
36
37#define ASSERT(exp) \
38 if (!(exp)) { \
39 printf("Assert failed %s %s:%d\n", #exp, __FILE__, __LINE__); \
40 abort(); \
41 }
42
43
44static struct log_info info = {};
45
46struct lapdm_polling_state {
47 struct lapdm_channel *bts;
48 int bts_read;
49
50 struct lapdm_channel *ms;
51 int ms_read;
52};
53
54static struct msgb *msgb_from_array(const uint8_t *data, int len)
55{
56 struct msgb *msg = msgb_alloc_headroom(4096, 128, "data");
57 msg->l3h = msgb_put(msg, len);
58 memcpy(msg->l3h, data, len);
59 return msg;
60}
61
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +010062/*
63 * Test data is below...
64 */
65static const uint8_t cm[] = {
66 0x05, 0x24, 0x31, 0x03, 0x50, 0x18, 0x93, 0x08,
67 0x29, 0x47, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80,
68};
69
70static const uint8_t cm_padded[] = {
71 0x05, 0x24, 0x31, 0x03, 0x50, 0x18, 0x93, 0x08,
72 0x29, 0x47, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80,
73 0x2b, 0x2b, 0x2b, 0x2b
74};
75
76static const uint8_t mm[] = {
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +010077 0x00, 0x0c, 0x00, 0x03, 0x01, 0x01, 0x20, 0x02,
78 0x00, 0x0b, 0x00, 0x03, 0x05, 0x04, 0x0d
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +010079};
80
81static const uint8_t dummy1[] = {
82 0xab, 0x03, 0x30, 0x60, 0x06,
83};
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010084
Daniel Willmanne5233922012-12-25 23:15:50 +010085static const uint8_t rel_req[] = {
86 0x02, 0x07, 0x01, 0x0a, 0x02, 0x40, 0x14, 0x01
87};
88
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010089static struct msgb *create_cm_serv_req(void)
90{
91 struct msgb *msg;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010092
93 msg = msgb_from_array(cm, sizeof(cm));
94 rsl_rll_push_l3(msg, RSL_MT_EST_REQ, 0, 0, 1);
95 return msg;
96}
97
98static struct msgb *create_mm_id_req(void)
99{
100 struct msgb *msg;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100101
102 msg = msgb_from_array(mm, sizeof(mm));
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100103 msg->l2h = msg->data + 3;
104 ASSERT(msgb_l2len(msg) == 12);
105 msg->l3h = msg->l2h + 6;
106 ASSERT(msgb_l3len(msg) == 6);
107
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100108 return msg;
109}
110
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +0800111static struct msgb *create_empty_msg(void)
112{
113 struct msgb *msg;
114
115 msg = msgb_from_array(NULL, 0);
116 ASSERT(msgb_l3len(msg) == 0);
117 rsl_rll_push_l3(msg, RSL_MT_DATA_REQ, 0, 0, 1);
118 return msg;
119}
120
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100121static struct msgb *create_dummy_data_req(void)
122{
123 struct msgb *msg;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100124
125 msg = msgb_from_array(dummy1, sizeof(dummy1));
126 rsl_rll_push_l3(msg, RSL_MT_DATA_REQ, 0, 0, 1);
127 return msg;
128}
129
Daniel Willmanne5233922012-12-25 23:15:50 +0100130static struct msgb *create_rel_req(void)
131{
132 struct msgb *msg;
133
134 msg = msgb_from_array(rel_req, sizeof(rel_req));
135 msg->l2h = msg->data;
136 msg->l3h = msg->l2h + sizeof(struct abis_rsl_rll_hdr);
137 return msg;
138}
139
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100140static int send(struct msgb *in_msg, struct lapdm_channel *chan)
141{
142 struct osmo_phsap_prim pp;
143 struct msgb *msg;
144 int rc;
145
146 msg = msgb_alloc_headroom(128, 64, "PH-DATA.ind");
147 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
148 PRIM_OP_INDICATION, msg);
149 /* copy over actual MAC block */
150 msg->l2h = msgb_put(msg, msgb_l2len(in_msg));
151 memcpy(msg->l2h, in_msg->l2h, msgb_l2len(in_msg));
152
153 /* LAPDm requires those... */
154 pp.u.data.chan_nr = 0;
155 pp.u.data.link_id = 0;
156 /* feed into the LAPDm code of libosmogsm */
157 rc = lapdm_phsap_up(&pp.oph, &chan->lapdm_dcch);
158 ASSERT(rc == 0 || rc == -EBUSY);
159 return 0;
160}
161
162/*
163 * I get called from the LAPDm code when something was sent my way...
164 */
165static int bts_to_ms_tx_cb(struct msgb *in_msg, struct lapdm_entity *le, void *_ctx)
166{
167 struct lapdm_polling_state *state = _ctx;
168
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100169
170 printf("%s: MS->BTS(us) message %d\n", __func__, msgb_length(in_msg));
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100171
172
173 if (state->bts_read == 0) {
174 printf("BTS: Verifying CM request.\n");
175 ASSERT(msgb_l3len(in_msg) == ARRAY_SIZE(cm_padded));
176 ASSERT(memcmp(in_msg->l3h, cm_padded, ARRAY_SIZE(cm_padded)) == 0);
177 } else if (state->bts_read == 1) {
178 printf("BTS: Verifying dummy message.\n");
179 ASSERT(msgb_l3len(in_msg) == ARRAY_SIZE(dummy1));
180 ASSERT(memcmp(in_msg->l3h, dummy1, ARRAY_SIZE(dummy1)) == 0);
181 } else {
182 printf("BTS: Do not know to verify: %d\n", state->bts_read);
183 }
184
185 state->bts_read += 1;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100186 msgb_free(in_msg);
187
188 return 0;
189}
190
191static int ms_to_bts_l1_cb(struct osmo_prim_hdr *oph, void *_ctx)
192{
193 int rc;
194 struct lapdm_polling_state *state = _ctx;
195 printf("%s: MS(us) -> BTS prim message\n", __func__);
196
197 /* i stuff it into the LAPDm channel of the BTS */
198 rc = send(oph->msg, state->bts);
199 msgb_free(oph->msg);
Holger Hans Peter Freytheraf723a42012-12-26 10:51:00 +0100200 return rc;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100201}
202
203static int ms_to_bts_tx_cb(struct msgb *msg, struct lapdm_entity *le, void *_ctx)
204{
205 struct lapdm_polling_state *state = _ctx;
206
207 printf("%s: BTS->MS(us) message %d\n", __func__, msgb_length(msg));
208
209 if (state->ms_read == 0) {
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100210 struct abis_rsl_rll_hdr hdr;
211
212 printf("MS: Verifying incoming primitive.\n");
213 ASSERT(msg->len == sizeof(struct abis_rsl_rll_hdr) + 3);
214
215 /* verify the header */
216 memset(&hdr, 0, sizeof(hdr));
217 rsl_init_rll_hdr(&hdr, RSL_MT_EST_CONF);
218 hdr.c.msg_discr |= ABIS_RSL_MDISC_TRANSP;
219 ASSERT(memcmp(msg->data, &hdr, sizeof(hdr)) == 0);
220
221 /* Verify the added RSL_IE_L3_INFO but we have a bug here */
222 ASSERT(msg->data[6] == RSL_IE_L3_INFO);
Holger Hans Peter Freyther4a075f82011-12-12 00:41:25 +0100223 #warning "RSL_IE_L3_INFO 16 bit length is wrong"
224 /* ASSERT(msg->data[7] == 0x0 && msg->data[8] == 0x9c); */
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100225 /* this should be 0x0 and 0x0... but we have a bug */
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100226 } else if (state->ms_read == 1) {
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100227 printf("MS: Verifying incoming MM message: %d\n", msgb_l3len(msg));
228 ASSERT(msgb_l3len(msg) == 3);
229 ASSERT(memcmp(msg->l3h, &mm[12], msgb_l3len(msg)) == 0);
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100230 } else {
231 printf("MS: Do not know to verify: %d\n", state->ms_read);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100232 }
233
234 state->ms_read += 1;
235 msgb_free(msg);
236 return 0;
237}
238
239static void test_lapdm_polling()
240{
241 printf("I do some very simple LAPDm test.\n");
242
243 int rc;
244 struct lapdm_polling_state test_state;
245 struct osmo_phsap_prim pp;
246
247 /* Configure LAPDm on both sides */
248 struct lapdm_channel bts_to_ms_channel;
249 struct lapdm_channel ms_to_bts_channel;
250 memset(&bts_to_ms_channel, 0, sizeof(bts_to_ms_channel));
251 memset(&ms_to_bts_channel, 0, sizeof(ms_to_bts_channel));
252
253 memset(&test_state, 0, sizeof(test_state));
254 test_state.bts = &bts_to_ms_channel;
255 test_state.ms = &ms_to_bts_channel;
256
257 /* BTS to MS in polling mode */
258 lapdm_channel_init(&bts_to_ms_channel, LAPDM_MODE_BTS);
259 lapdm_channel_set_flags(&bts_to_ms_channel, LAPDM_ENT_F_POLLING_ONLY);
260 lapdm_channel_set_l1(&bts_to_ms_channel, NULL, &test_state);
261 lapdm_channel_set_l3(&bts_to_ms_channel, bts_to_ms_tx_cb, &test_state);
262
263 /* MS to BTS in direct mode */
264 lapdm_channel_init(&ms_to_bts_channel, LAPDM_MODE_MS);
265 lapdm_channel_set_l1(&ms_to_bts_channel, ms_to_bts_l1_cb, &test_state);
266 lapdm_channel_set_l3(&ms_to_bts_channel, ms_to_bts_tx_cb, &test_state);
267
268 /*
269 * We try to send messages from the MS to the BTS to the MS..
270 */
271 /* 1. Start with MS -> BTS, BTS should have a pending message */
272 printf("Establishing link.\n");
273 lapdm_rslms_recvmsg(create_cm_serv_req(), &ms_to_bts_channel);
274
275 /* 2. Poll on the BTS for sending out a confirmation */
276 printf("\nConfirming\n");
277 ASSERT(test_state.bts_read == 1)
278 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
279 CHECK_RC(rc);
280 ASSERT(pp.oph.msg->data == pp.oph.msg->l2h);
281 send(pp.oph.msg, &ms_to_bts_channel);
282 msgb_free(pp.oph.msg);
283 ASSERT(test_state.ms_read == 1);
284
285 /* 3. Send some data to the MS */
286 printf("\nSending back to MS\n");
287 lapdm_rslms_recvmsg(create_mm_id_req(), &bts_to_ms_channel);
288 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
289 CHECK_RC(rc);
290 send(pp.oph.msg, &ms_to_bts_channel);
291 msgb_free(pp.oph.msg);
292 ASSERT(test_state.ms_read == 2);
293
294 /* verify that there is nothing more to poll */
295 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
296 ASSERT(rc < 0);
297
298 /* 3. And back to the BTS */
299 printf("\nSending back to BTS\n");
300 ASSERT(test_state.ms_read == 2);
301 lapdm_rslms_recvmsg(create_dummy_data_req(), &ms_to_bts_channel);
302
303
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100304 /* 4. And back to the MS, but let's move data/l2h apart */
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100305 ASSERT(test_state.bts_read == 2)
306 ASSERT(test_state.ms_read == 2);
307 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
308 CHECK_RC(rc);
309 send(pp.oph.msg, &ms_to_bts_channel);
310 ASSERT(test_state.ms_read == 2);
311 msgb_free(pp.oph.msg);
312
313 /* verify that there is nothing more to poll */
314 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
315 ASSERT(rc < 0);
316
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +0800317 /* check sending an empty L3 message fails */
318 rc = lapdm_rslms_recvmsg(create_empty_msg(), &bts_to_ms_channel);
319 ASSERT(rc == -1);
320 ASSERT(test_state.ms_read == 2);
321
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100322 /* clean up */
323 lapdm_channel_exit(&bts_to_ms_channel);
324 lapdm_channel_exit(&ms_to_bts_channel);
325}
326
Daniel Willmanne5233922012-12-25 23:15:50 +0100327static void test_lapdm_early_release()
328{
329 printf("I test RF channel release of an unestablished channel.\n");
330
331 int rc;
332 struct lapdm_polling_state test_state;
333
334 /* Configure LAPDm on both sides */
335 struct lapdm_channel bts_to_ms_channel;
336 memset(&bts_to_ms_channel, 0, sizeof(bts_to_ms_channel));
337
338 memset(&test_state, 0, sizeof(test_state));
339 test_state.bts = &bts_to_ms_channel;
340
341 /* BTS to MS in polling mode */
342 lapdm_channel_init(&bts_to_ms_channel, LAPDM_MODE_BTS);
343 lapdm_channel_set_flags(&bts_to_ms_channel, LAPDM_ENT_F_POLLING_ONLY);
344 lapdm_channel_set_l1(&bts_to_ms_channel, NULL, &test_state);
345 lapdm_channel_set_l3(&bts_to_ms_channel, bts_to_ms_tx_cb, &test_state);
346
347 /* Send the release request */
348 rc = lapdm_rslms_recvmsg(create_rel_req(), &bts_to_ms_channel);
349 ASSERT(rc == -EINVAL);
350
351 /* clean up */
352 lapdm_channel_exit(&bts_to_ms_channel);
353}
354
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100355int main(int argc, char **argv)
356{
357 osmo_init_logging(&info);
358
359 test_lapdm_polling();
Daniel Willmanne5233922012-12-25 23:15:50 +0100360 test_lapdm_early_release();
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100361 printf("Success.\n");
362
363 return 0;
364}