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