Merge commit 'e476442cf0e84c65565ace545f5b73602b5f0ffc'
[osmocom-bb.git] / src / host / layer23 / src / common / main.c
1 /* Main method of the layer2/3 stack */
2
3 /* (C) 2010 by Holger Hans Peter Freyther
4  * (C) 2010 by Harald Welte <laforge@gnumonks.org>
5  *
6  * All Rights Reserved
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  */
23
24 #include <osmocom/bb/common/osmocom_data.h>
25 #include <osmocom/bb/common/l1ctl.h>
26 #include <osmocom/bb/common/l1l2_interface.h>
27 #include <osmocom/bb/common/sap_interface.h>
28 #include <osmocom/bb/misc/layer3.h>
29 #include <osmocom/bb/common/lapdm.h>
30 #include <osmocom/bb/common/logging.h>
31 #include <osmocom/bb/common/l23_app.h>
32
33 #include <osmocom/core/msgb.h>
34 #include <osmocom/core/talloc.h>
35 #include <osmocom/core/select.h>
36 #include <osmocom/core/linuxlist.h>
37 #include <osmocom/core/gsmtap_util.h>
38 #include <osmocom/core/utils.h>
39
40 #include <arpa/inet.h>
41
42 #define _GNU_SOURCE
43 #include <getopt.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <signal.h>
49
50 struct log_target *stderr_target;
51
52 void *l23_ctx = NULL;
53 static char *layer2_socket_path = "/tmp/osmocom_l2";
54 static char *sap_socket_path = "/tmp/osmocom_sap";
55 struct llist_head ms_list;
56 static struct osmocom_ms *ms = NULL;
57 static uint32_t gsmtap_ip = 0;
58 unsigned short vty_port = 4247;
59 int (*l23_app_work) (struct osmocom_ms *ms) = NULL;
60 int (*l23_app_exit) (struct osmocom_ms *ms) = NULL;
61 int quit = 0;
62
63 const char *openbsc_copyright =
64         "%s"
65         "%s\n"
66         "License GPLv2+: GNU GPL version 2 or later "
67                 "<http://gnu.org/licenses/gpl.html>\n"
68         "This is free software: you are free to change and redistribute it.\n"
69         "There is NO WARRANTY, to the extent permitted by law.\n\n";
70
71 static void print_usage(const char *app)
72 {
73         printf("Usage: %s\n", app);
74 }
75
76 static void print_help()
77 {
78         int options = 0xff;
79         struct l23_app_info *app = l23_app_info();
80
81         if (app && app->cfg_supported != 0)
82                 options = app->cfg_supported();
83
84         printf(" Some help...\n");
85         printf("  -h --help             this text\n");
86         printf("  -s --socket           /tmp/osmocom_l2. Path to the unix "
87                 "domain socket (l2)\n");
88
89         if (options & L23_OPT_SAP)
90                 printf("  -S --sap              /tmp/osmocom_sap. Path to the "
91                         "unix domain socket (BTSAP)\n");
92
93         if (options & L23_OPT_ARFCN)
94                 printf("  -a --arfcn NR         The ARFCN to be used for layer2.\n");
95
96         if (options & L23_OPT_TAP)
97                 printf("  -i --gsmtap-ip        The destination IP used for GSMTAP.\n");
98
99         if (options & L23_OPT_VTY)
100                 printf("  -v --vty-port         The VTY port number to telnet "
101                         "to. (default %u)\n", vty_port);
102
103         if (options & L23_OPT_DBG)
104                 printf("  -d --debug            Change debug flags.\n");
105
106         if (app && app->cfg_print_help)
107                 app->cfg_print_help();
108 }
109
110 static void build_config(char **opt, struct option **option)
111 {
112         struct l23_app_info *app;
113         struct option *app_opp = NULL;
114         int app_len = 0, len;
115
116         static struct option long_options[] = {
117                 {"help", 0, 0, 'h'},
118                 {"socket", 1, 0, 's'},
119                 {"sap", 1, 0, 'S'},
120                 {"arfcn", 1, 0, 'a'},
121                 {"gsmtap-ip", 1, 0, 'i'},
122                 {"vty-port", 1, 0, 'v'},
123                 {"debug", 1, 0, 'd'},
124         };
125
126
127         app = l23_app_info();
128         *opt = talloc_asprintf(l23_ctx, "hs:S:a:i:v:d:%s",
129                                app && app->getopt_string ? app->getopt_string : "");
130
131         len = ARRAY_SIZE(long_options);
132         if (app && app->cfg_getopt_opt)
133                 app_len = app->cfg_getopt_opt(&app_opp);
134
135         *option = talloc_zero_array(l23_ctx, struct option, len + app_len + 1);
136         memcpy(*option, long_options, sizeof(long_options));
137         memcpy(*option + len, app_opp, app_len * sizeof(struct option));
138 }
139
140 static void handle_options(int argc, char **argv)
141 {
142         struct sockaddr_in gsmtap;
143         struct l23_app_info *app = l23_app_info();
144         struct option *long_options;
145         char *opt;
146
147         build_config(&opt, &long_options);
148
149         while (1) {
150                 int option_index = 0, c;
151
152                 c = getopt_long(argc, argv, opt,
153                                 long_options, &option_index);
154                 if (c == -1)
155                         break;
156
157                 switch (c) {
158                 case 'h':
159                         print_usage(argv[0]);
160                         print_help();
161                         exit(0);
162                         break;
163                 case 's':
164                         layer2_socket_path = talloc_strdup(l23_ctx, optarg);
165                         break;
166                 case 'S':
167                         sap_socket_path = talloc_strdup(l23_ctx, optarg);
168                         break;
169                 case 'a':
170                         ms->test_arfcn = atoi(optarg);
171                         break;
172                 case 'i':
173                         if (!inet_aton(optarg, &gsmtap.sin_addr)) {
174                                 perror("inet_aton");
175                                 exit(2);
176                         }
177                         gsmtap_ip = ntohl(gsmtap.sin_addr.s_addr);
178                         break;
179                 case 'v':
180                         vty_port = atoi(optarg);
181                         break;
182                 case 'd':
183                         log_parse_category_mask(stderr_target, optarg);
184                         break;
185                 default:
186                         if (app && app->cfg_handle_opt)
187                                 app->cfg_handle_opt(c, optarg);
188                         break;
189                 }
190         }
191
192         talloc_free(opt);
193         talloc_free(long_options);
194 }
195
196 void sighandler(int sigset)
197 {
198         int rc = 0;
199
200         if (sigset == SIGHUP || sigset == SIGPIPE)
201                 return;
202
203         fprintf(stderr, "Signal %d recevied.\n", sigset);
204         if (l23_app_exit)
205                 rc = l23_app_exit(ms);
206
207         if (rc != -EBUSY)
208                 exit (0);
209 }
210
211 static void print_copyright()
212 {
213         struct l23_app_info *app;
214         app = l23_app_info();
215         printf(openbsc_copyright,
216                app && app->copyright ? app->copyright : "",
217                app && app->contribution ? app->contribution : "");
218 }
219
220 int main(int argc, char **argv)
221 {
222         int rc;
223
224         INIT_LLIST_HEAD(&ms_list);
225         log_init(&log_info);
226         stderr_target = log_target_create_stderr();
227         log_add_target(stderr_target);
228         log_set_all_filter(stderr_target, 1);
229
230         l23_ctx = talloc_named_const(NULL, 1, "layer2 context");
231
232         ms = talloc_zero(l23_ctx, struct osmocom_ms);
233         if (!ms) {
234                 fprintf(stderr, "Failed to allocate MS\n");
235                 exit(1);
236         }
237
238         print_copyright();
239
240         llist_add_tail(&ms->entity, &ms_list);
241
242         sprintf(ms->name, "1");
243
244         ms->test_arfcn = 871;
245
246         handle_options(argc, argv);
247
248         rc = layer2_open(ms, layer2_socket_path);
249         if (rc < 0) {
250                 fprintf(stderr, "Failed during layer2_open()\n");
251                 exit(1);
252         }
253
254         rc = sap_open(ms, sap_socket_path);
255         if (rc < 0)
256                 fprintf(stderr, "Failed during sap_open(), no SIM reader\n");
257
258         lapdm_init(&ms->l2_entity.lapdm_dcch, ms);
259         lapdm_init(&ms->l2_entity.lapdm_acch, ms);
260
261         rc = l23_app_init(ms);
262         if (rc < 0)
263                 exit(1);
264
265         if (gsmtap_ip) {
266                 rc = gsmtap_init(gsmtap_ip);
267                 if (rc < 0) {
268                         fprintf(stderr, "Failed during gsmtap_init()\n");
269                         exit(1);
270                 }
271         }
272
273         signal(SIGINT, sighandler);
274         signal(SIGHUP, sighandler);
275         signal(SIGTERM, sighandler);
276         signal(SIGPIPE, sighandler);
277
278         while (!quit) {
279                 if (l23_app_work)
280                         l23_app_work(ms);
281                 osmo_select_main(0);
282         }
283
284         return 0;
285 }