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