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