Inter-Layer intergration work
[osmocom-bb.git] / src / host / layer2 / src / layer2_main.c
1 /* Main method of the layer2 stack */
2 /* (C) 2010 by Holger Hans Peter Freyther
3  * (C) 2010 by Harald Welte <laforge@gnumonks.org>
4  *
5  * All Rights Reserved
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  */
22
23 #include <osmocom/osmocom_data.h>
24 #include <osmocom/osmocom_layer2.h>
25 #include <osmocom/lapdm.h>
26
27 #include <osmocom/debug.h>
28 #include <osmocore/msgb.h>
29 #include <osmocore/talloc.h>
30 #include <osmocore/select.h>
31
32 #include <arpa/inet.h>
33
34 #include <sys/socket.h>
35 #include <sys/un.h>
36
37 #define _GNU_SOURCE
38 #include <getopt.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <errno.h>
42 #include <fcntl.h>
43
44 #include "gsmtap_util.h"
45
46 #define GSM_L2_LENGTH 256
47
48 static void *l2_ctx = NULL;
49 static char *socket_path = "/tmp/osmocom_l2";
50 static struct osmocom_ms *ms = NULL;
51
52 static int layer2_read(struct bsc_fd *fd, unsigned int flags)
53 {
54         struct msgb *msg;
55         u_int16_t len;
56         int rc;
57
58         msg = msgb_alloc(GSM_L2_LENGTH, "Layer2");
59         if (!msg) {
60                 fprintf(stderr, "Failed to allocate msg.\n");
61                 return -1;
62         }
63
64         rc = read(fd->fd, &len, sizeof(len));
65         if (rc < sizeof(len)) {
66                 fprintf(stderr, "Short read. Error.\n");
67                 exit(2);
68         }
69
70         len = ntohs(len);
71         if (len > GSM_L2_LENGTH) {
72                 fprintf(stderr, "Length is too big: %u\n", len);
73                 msgb_free(msg);
74                 return -1;
75         }
76
77
78         /* blocking read for the poor... we can starve in here... */
79         msg->l1h = msgb_put(msg, len);
80         rc = read(fd->fd, msg->l1h, msgb_l1len(msg));
81         if (rc != msgb_l1len(msg)) {
82                 fprintf(stderr, "Can not read data: len=%d rc=%d errno=%d\n", len, rc, errno);
83                 msgb_free(msg);
84                 return -1;
85         }
86
87         osmo_recv((struct osmocom_ms *) fd->data, msg);
88         msgb_free(msg);
89         return 0;
90 }
91
92 int osmo_send_l1(struct osmocom_ms *ms, struct msgb *msg)
93 {
94         int rc;
95         uint16_t *len;
96
97         LOGP(DMUX, LOGL_INFO, "Sending: '%s'\n", hexdump(msg->data, msg->len));
98
99         
100         len = (uint16_t *) msgb_push(msg, sizeof(*len));
101         *len = htons(msg->len - sizeof(*len));
102
103         /* TODO: just enqueue the message and wait for ready write.. */
104         rc = write(ms->bfd.fd, msg->data, msg->len);
105         if (rc != msg->len) {
106                 fprintf(stderr, "Failed to write data: rc: %d\n", rc);
107                 msgb_free(msg);
108                 return -1;
109         }
110
111         msgb_free(msg);
112         return 0;
113 }
114
115 static void print_usage()
116 {
117         printf("Usage: ./layer2\n");
118 }
119
120 static void print_help()
121 {
122         printf(" Some help...\n");
123         printf("  -h --help this text\n");
124         printf("  -s --socket /tmp/osmocom_l2. Path to the unix domain socket\n");
125         printf("  -a --arfcn NR. The ARFCN to be used for layer2.\n");
126 }
127
128 static void handle_options(int argc, char **argv)
129 {
130         while (1) {
131                 int option_index = 0, c;
132                 static struct option long_options[] = {
133                         {"help", 0, 0, 'h'},
134                         {"socket", 1, 0, 's'},
135                         {"arfcn", 1, 0, 'a'},
136                         {0, 0, 0, 0},
137                 };
138
139                 c = getopt_long(argc, argv, "hs:a:",
140                                 long_options, &option_index);
141                 if (c == -1)
142                         break;
143
144                 switch (c) {
145                 case 'h':
146                         print_usage();
147                         print_help();
148                         exit(0);
149                         break;
150                 case 's':
151                         socket_path = talloc_strdup(l2_ctx, optarg);
152                         break;
153                 case 'a':
154                         ms->arfcn = atoi(optarg);
155                         break;
156                 default:
157                         break;
158                 }
159         }
160 }
161
162 int main(int argc, char **argv)
163 {
164         int rc;
165         struct sockaddr_un local;
166         struct sockaddr_in gsmtap;
167
168         l2_ctx = talloc_named_const(NULL, 1, "layer2 context");
169
170         ms = talloc_zero(l2_ctx, struct osmocom_ms);
171         if (!ms) {
172                 fprintf(stderr, "Failed to allocate MS\n");
173                 exit(1);
174         }
175
176         ms->arfcn = 871;
177
178         handle_options(argc, argv);
179
180         ms->bfd.fd = socket(AF_UNIX, SOCK_STREAM, 0);
181         if (ms->bfd.fd < 0) {
182                 fprintf(stderr, "Failed to create unix domain socket.\n");
183                 exit(1);
184         }
185
186         local.sun_family = AF_UNIX;
187         strncpy(local.sun_path, socket_path, sizeof(local.sun_path));
188         local.sun_path[sizeof(local.sun_path) - 1] = '\0';
189
190         rc = connect(ms->bfd.fd, (struct sockaddr *) &local,
191                      sizeof(local.sun_family) + strlen(local.sun_path));
192         if (rc < 0) {
193                 fprintf(stderr, "Failed to connect to '%s'.\n", local.sun_path);
194                 exit(1);
195         }
196
197         ms->bfd.when = BSC_FD_READ;
198         ms->bfd.cb = layer2_read;
199         ms->bfd.data = ms;
200
201         lapdm_init(&ms->lapdm_dcch, ms);
202         lapdm_init(&ms->lapdm_acch, ms);
203
204         if (bsc_register_fd(&ms->bfd) != 0) {
205                 fprintf(stderr, "Failed to register fd.\n");
206                 exit(1);
207         }
208
209         rc = gsmtap_init();
210         if (rc < 0) {
211                 fprintf(stderr, "Failed during gsmtap_init()\n");
212                 exit(1);
213         }
214
215         while (1) {
216                 bsc_select_main(0);
217         }
218
219
220         return 0;
221 }