blob: d58bec655f232230bd9c5b8406bcd0c4609c2f87 [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
84static struct msgb *create_cm_serv_req(void)
85{
86 struct msgb *msg;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010087
88 msg = msgb_from_array(cm, sizeof(cm));
89 rsl_rll_push_l3(msg, RSL_MT_EST_REQ, 0, 0, 1);
90 return msg;
91}
92
93static struct msgb *create_mm_id_req(void)
94{
95 struct msgb *msg;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010096
97 msg = msgb_from_array(mm, sizeof(mm));
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +010098 msg->l2h = msg->data + 3;
99 ASSERT(msgb_l2len(msg) == 12);
100 msg->l3h = msg->l2h + 6;
101 ASSERT(msgb_l3len(msg) == 6);
102
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100103 return msg;
104}
105
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +0800106static struct msgb *create_empty_msg(void)
107{
108 struct msgb *msg;
109
110 msg = msgb_from_array(NULL, 0);
111 ASSERT(msgb_l3len(msg) == 0);
112 rsl_rll_push_l3(msg, RSL_MT_DATA_REQ, 0, 0, 1);
113 return msg;
114}
115
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100116static struct msgb *create_dummy_data_req(void)
117{
118 struct msgb *msg;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100119
120 msg = msgb_from_array(dummy1, sizeof(dummy1));
121 rsl_rll_push_l3(msg, RSL_MT_DATA_REQ, 0, 0, 1);
122 return msg;
123}
124
125static int send(struct msgb *in_msg, struct lapdm_channel *chan)
126{
127 struct osmo_phsap_prim pp;
128 struct msgb *msg;
129 int rc;
130
131 msg = msgb_alloc_headroom(128, 64, "PH-DATA.ind");
132 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
133 PRIM_OP_INDICATION, msg);
134 /* copy over actual MAC block */
135 msg->l2h = msgb_put(msg, msgb_l2len(in_msg));
136 memcpy(msg->l2h, in_msg->l2h, msgb_l2len(in_msg));
137
138 /* LAPDm requires those... */
139 pp.u.data.chan_nr = 0;
140 pp.u.data.link_id = 0;
141 /* feed into the LAPDm code of libosmogsm */
142 rc = lapdm_phsap_up(&pp.oph, &chan->lapdm_dcch);
143 ASSERT(rc == 0 || rc == -EBUSY);
144 return 0;
145}
146
147/*
148 * I get called from the LAPDm code when something was sent my way...
149 */
150static int bts_to_ms_tx_cb(struct msgb *in_msg, struct lapdm_entity *le, void *_ctx)
151{
152 struct lapdm_polling_state *state = _ctx;
153
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100154
155 printf("%s: MS->BTS(us) message %d\n", __func__, msgb_length(in_msg));
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100156
157
158 if (state->bts_read == 0) {
159 printf("BTS: Verifying CM request.\n");
160 ASSERT(msgb_l3len(in_msg) == ARRAY_SIZE(cm_padded));
161 ASSERT(memcmp(in_msg->l3h, cm_padded, ARRAY_SIZE(cm_padded)) == 0);
162 } else if (state->bts_read == 1) {
163 printf("BTS: Verifying dummy message.\n");
164 ASSERT(msgb_l3len(in_msg) == ARRAY_SIZE(dummy1));
165 ASSERT(memcmp(in_msg->l3h, dummy1, ARRAY_SIZE(dummy1)) == 0);
166 } else {
167 printf("BTS: Do not know to verify: %d\n", state->bts_read);
168 }
169
170 state->bts_read += 1;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100171 msgb_free(in_msg);
172
173 return 0;
174}
175
176static int ms_to_bts_l1_cb(struct osmo_prim_hdr *oph, void *_ctx)
177{
178 int rc;
179 struct lapdm_polling_state *state = _ctx;
180 printf("%s: MS(us) -> BTS prim message\n", __func__);
181
182 /* i stuff it into the LAPDm channel of the BTS */
183 rc = send(oph->msg, state->bts);
184 msgb_free(oph->msg);
185}
186
187static int ms_to_bts_tx_cb(struct msgb *msg, struct lapdm_entity *le, void *_ctx)
188{
189 struct lapdm_polling_state *state = _ctx;
190
191 printf("%s: BTS->MS(us) message %d\n", __func__, msgb_length(msg));
192
193 if (state->ms_read == 0) {
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100194 struct abis_rsl_rll_hdr hdr;
195
196 printf("MS: Verifying incoming primitive.\n");
197 ASSERT(msg->len == sizeof(struct abis_rsl_rll_hdr) + 3);
198
199 /* verify the header */
200 memset(&hdr, 0, sizeof(hdr));
201 rsl_init_rll_hdr(&hdr, RSL_MT_EST_CONF);
202 hdr.c.msg_discr |= ABIS_RSL_MDISC_TRANSP;
203 ASSERT(memcmp(msg->data, &hdr, sizeof(hdr)) == 0);
204
205 /* Verify the added RSL_IE_L3_INFO but we have a bug here */
206 ASSERT(msg->data[6] == RSL_IE_L3_INFO);
Holger Hans Peter Freyther4a075f82011-12-12 00:41:25 +0100207 #warning "RSL_IE_L3_INFO 16 bit length is wrong"
208 /* ASSERT(msg->data[7] == 0x0 && msg->data[8] == 0x9c); */
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100209 /* this should be 0x0 and 0x0... but we have a bug */
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100210 } else if (state->ms_read == 1) {
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100211 printf("MS: Verifying incoming MM message: %d\n", msgb_l3len(msg));
212 ASSERT(msgb_l3len(msg) == 3);
213 ASSERT(memcmp(msg->l3h, &mm[12], msgb_l3len(msg)) == 0);
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100214 } else {
215 printf("MS: Do not know to verify: %d\n", state->ms_read);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100216 }
217
218 state->ms_read += 1;
219 msgb_free(msg);
220 return 0;
221}
222
223static void test_lapdm_polling()
224{
225 printf("I do some very simple LAPDm test.\n");
226
227 int rc;
228 struct lapdm_polling_state test_state;
229 struct osmo_phsap_prim pp;
230
231 /* Configure LAPDm on both sides */
232 struct lapdm_channel bts_to_ms_channel;
233 struct lapdm_channel ms_to_bts_channel;
234 memset(&bts_to_ms_channel, 0, sizeof(bts_to_ms_channel));
235 memset(&ms_to_bts_channel, 0, sizeof(ms_to_bts_channel));
236
237 memset(&test_state, 0, sizeof(test_state));
238 test_state.bts = &bts_to_ms_channel;
239 test_state.ms = &ms_to_bts_channel;
240
241 /* BTS to MS in polling mode */
242 lapdm_channel_init(&bts_to_ms_channel, LAPDM_MODE_BTS);
243 lapdm_channel_set_flags(&bts_to_ms_channel, LAPDM_ENT_F_POLLING_ONLY);
244 lapdm_channel_set_l1(&bts_to_ms_channel, NULL, &test_state);
245 lapdm_channel_set_l3(&bts_to_ms_channel, bts_to_ms_tx_cb, &test_state);
246
247 /* MS to BTS in direct mode */
248 lapdm_channel_init(&ms_to_bts_channel, LAPDM_MODE_MS);
249 lapdm_channel_set_l1(&ms_to_bts_channel, ms_to_bts_l1_cb, &test_state);
250 lapdm_channel_set_l3(&ms_to_bts_channel, ms_to_bts_tx_cb, &test_state);
251
252 /*
253 * We try to send messages from the MS to the BTS to the MS..
254 */
255 /* 1. Start with MS -> BTS, BTS should have a pending message */
256 printf("Establishing link.\n");
257 lapdm_rslms_recvmsg(create_cm_serv_req(), &ms_to_bts_channel);
258
259 /* 2. Poll on the BTS for sending out a confirmation */
260 printf("\nConfirming\n");
261 ASSERT(test_state.bts_read == 1)
262 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
263 CHECK_RC(rc);
264 ASSERT(pp.oph.msg->data == pp.oph.msg->l2h);
265 send(pp.oph.msg, &ms_to_bts_channel);
266 msgb_free(pp.oph.msg);
267 ASSERT(test_state.ms_read == 1);
268
269 /* 3. Send some data to the MS */
270 printf("\nSending back to MS\n");
271 lapdm_rslms_recvmsg(create_mm_id_req(), &bts_to_ms_channel);
272 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
273 CHECK_RC(rc);
274 send(pp.oph.msg, &ms_to_bts_channel);
275 msgb_free(pp.oph.msg);
276 ASSERT(test_state.ms_read == 2);
277
278 /* verify that there is nothing more to poll */
279 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
280 ASSERT(rc < 0);
281
282 /* 3. And back to the BTS */
283 printf("\nSending back to BTS\n");
284 ASSERT(test_state.ms_read == 2);
285 lapdm_rslms_recvmsg(create_dummy_data_req(), &ms_to_bts_channel);
286
287
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100288 /* 4. And back to the MS, but let's move data/l2h apart */
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100289 ASSERT(test_state.bts_read == 2)
290 ASSERT(test_state.ms_read == 2);
291 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
292 CHECK_RC(rc);
293 send(pp.oph.msg, &ms_to_bts_channel);
294 ASSERT(test_state.ms_read == 2);
295 msgb_free(pp.oph.msg);
296
297 /* verify that there is nothing more to poll */
298 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
299 ASSERT(rc < 0);
300
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +0800301 /* check sending an empty L3 message fails */
302 rc = lapdm_rslms_recvmsg(create_empty_msg(), &bts_to_ms_channel);
303 ASSERT(rc == -1);
304 ASSERT(test_state.ms_read == 2);
305
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100306 /* clean up */
307 lapdm_channel_exit(&bts_to_ms_channel);
308 lapdm_channel_exit(&ms_to_bts_channel);
309}
310
311int main(int argc, char **argv)
312{
313 osmo_init_logging(&info);
314
315 test_lapdm_polling();
316 printf("Success.\n");
317
318 return 0;
319}