blob: e6b10b64f3eacaa7cf2282fa88b906d81f136892 [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
61
62static struct msgb *create_cm_serv_req(void)
63{
64 struct msgb *msg;
65 static const uint8_t cm[] = {
66 0x05, 0x24, 0x31, 0x03, 0x50, 0x18, 0x93, 0x08,
67 0x29, 0x47, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80,
68 };
69
70 msg = msgb_from_array(cm, sizeof(cm));
71 rsl_rll_push_l3(msg, RSL_MT_EST_REQ, 0, 0, 1);
72 return msg;
73}
74
75static struct msgb *create_mm_id_req(void)
76{
77 struct msgb *msg;
78 /* okay... not an identity request */
79 static const uint8_t mm[] = {
80 0x05, 0x24, 0x31, 0x03, 0x50, 0x18, 0x93, 0x08,
81 0x29, 0x47, 0x80, 0x00,
82 };
83
84 msg = msgb_from_array(mm, sizeof(mm));
85 rsl_rll_push_l3(msg, RSL_MT_DATA_REQ, 0, 0, 1);
86 return msg;
87}
88
89static struct msgb *create_dummy_data_req(void)
90{
91 struct msgb *msg;
92 /* okay... not an identity request */
93 static const uint8_t dummy1[] = {
94 0xab, 0x03, 0x30, 0x60, 0x06,
95 };
96
97 msg = msgb_from_array(dummy1, sizeof(dummy1));
98 rsl_rll_push_l3(msg, RSL_MT_DATA_REQ, 0, 0, 1);
99 return msg;
100}
101
102static int send(struct msgb *in_msg, struct lapdm_channel *chan)
103{
104 struct osmo_phsap_prim pp;
105 struct msgb *msg;
106 int rc;
107
108 msg = msgb_alloc_headroom(128, 64, "PH-DATA.ind");
109 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
110 PRIM_OP_INDICATION, msg);
111 /* copy over actual MAC block */
112 msg->l2h = msgb_put(msg, msgb_l2len(in_msg));
113 memcpy(msg->l2h, in_msg->l2h, msgb_l2len(in_msg));
114
115 /* LAPDm requires those... */
116 pp.u.data.chan_nr = 0;
117 pp.u.data.link_id = 0;
118 /* feed into the LAPDm code of libosmogsm */
119 rc = lapdm_phsap_up(&pp.oph, &chan->lapdm_dcch);
120 ASSERT(rc == 0 || rc == -EBUSY);
121 return 0;
122}
123
124/*
125 * I get called from the LAPDm code when something was sent my way...
126 */
127static int bts_to_ms_tx_cb(struct msgb *in_msg, struct lapdm_entity *le, void *_ctx)
128{
129 struct lapdm_polling_state *state = _ctx;
130
131 state->bts_read += 1;
132
133 printf("%s: MS->BTS(us) message %d\n", __func__, msgb_length(in_msg));
134 msgb_free(in_msg);
135
136 return 0;
137}
138
139static int ms_to_bts_l1_cb(struct osmo_prim_hdr *oph, void *_ctx)
140{
141 int rc;
142 struct lapdm_polling_state *state = _ctx;
143 printf("%s: MS(us) -> BTS prim message\n", __func__);
144
145 /* i stuff it into the LAPDm channel of the BTS */
146 rc = send(oph->msg, state->bts);
147 msgb_free(oph->msg);
148}
149
150static int ms_to_bts_tx_cb(struct msgb *msg, struct lapdm_entity *le, void *_ctx)
151{
152 struct lapdm_polling_state *state = _ctx;
153
154 printf("%s: BTS->MS(us) message %d\n", __func__, msgb_length(msg));
155
156 if (state->ms_read == 0) {
157 /* TODO: Verify that this is a: PRIM_DL_EST, PRIM_OP_CONFIRM */
158 } else if (state->ms_read == 1) {
159 /* TODO: Verify that this is: create_mm_id_req() */
160 }
161
162 state->ms_read += 1;
163 msgb_free(msg);
164 return 0;
165}
166
167static void test_lapdm_polling()
168{
169 printf("I do some very simple LAPDm test.\n");
170
171 int rc;
172 struct lapdm_polling_state test_state;
173 struct osmo_phsap_prim pp;
174
175 /* Configure LAPDm on both sides */
176 struct lapdm_channel bts_to_ms_channel;
177 struct lapdm_channel ms_to_bts_channel;
178 memset(&bts_to_ms_channel, 0, sizeof(bts_to_ms_channel));
179 memset(&ms_to_bts_channel, 0, sizeof(ms_to_bts_channel));
180
181 memset(&test_state, 0, sizeof(test_state));
182 test_state.bts = &bts_to_ms_channel;
183 test_state.ms = &ms_to_bts_channel;
184
185 /* BTS to MS in polling mode */
186 lapdm_channel_init(&bts_to_ms_channel, LAPDM_MODE_BTS);
187 lapdm_channel_set_flags(&bts_to_ms_channel, LAPDM_ENT_F_POLLING_ONLY);
188 lapdm_channel_set_l1(&bts_to_ms_channel, NULL, &test_state);
189 lapdm_channel_set_l3(&bts_to_ms_channel, bts_to_ms_tx_cb, &test_state);
190
191 /* MS to BTS in direct mode */
192 lapdm_channel_init(&ms_to_bts_channel, LAPDM_MODE_MS);
193 lapdm_channel_set_l1(&ms_to_bts_channel, ms_to_bts_l1_cb, &test_state);
194 lapdm_channel_set_l3(&ms_to_bts_channel, ms_to_bts_tx_cb, &test_state);
195
196 /*
197 * We try to send messages from the MS to the BTS to the MS..
198 */
199 /* 1. Start with MS -> BTS, BTS should have a pending message */
200 printf("Establishing link.\n");
201 lapdm_rslms_recvmsg(create_cm_serv_req(), &ms_to_bts_channel);
202
203 /* 2. Poll on the BTS for sending out a confirmation */
204 printf("\nConfirming\n");
205 ASSERT(test_state.bts_read == 1)
206 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
207 CHECK_RC(rc);
208 ASSERT(pp.oph.msg->data == pp.oph.msg->l2h);
209 send(pp.oph.msg, &ms_to_bts_channel);
210 msgb_free(pp.oph.msg);
211 ASSERT(test_state.ms_read == 1);
212
213 /* 3. Send some data to the MS */
214 printf("\nSending back to MS\n");
215 lapdm_rslms_recvmsg(create_mm_id_req(), &bts_to_ms_channel);
216 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
217 CHECK_RC(rc);
218 send(pp.oph.msg, &ms_to_bts_channel);
219 msgb_free(pp.oph.msg);
220 ASSERT(test_state.ms_read == 2);
221
222 /* verify that there is nothing more to poll */
223 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
224 ASSERT(rc < 0);
225
226 /* 3. And back to the BTS */
227 printf("\nSending back to BTS\n");
228 ASSERT(test_state.ms_read == 2);
229 lapdm_rslms_recvmsg(create_dummy_data_req(), &ms_to_bts_channel);
230
231
232 /* 4. And back to the MS */
233 ASSERT(test_state.bts_read == 2)
234 ASSERT(test_state.ms_read == 2);
235 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
236 CHECK_RC(rc);
237 send(pp.oph.msg, &ms_to_bts_channel);
238 ASSERT(test_state.ms_read == 2);
239 msgb_free(pp.oph.msg);
240
241 /* verify that there is nothing more to poll */
242 rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp);
243 ASSERT(rc < 0);
244
245 /* clean up */
246 lapdm_channel_exit(&bts_to_ms_channel);
247 lapdm_channel_exit(&ms_to_bts_channel);
248}
249
250int main(int argc, char **argv)
251{
252 osmo_init_logging(&info);
253
254 test_lapdm_polling();
255 printf("Success.\n");
256
257 return 0;
258}