misc: Fix crash in cell_log due missing l1_prim_cb
[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/gsmtap.h>
39 #include <osmocom/core/utils.h>
40
41 #include <arpa/inet.h>
42
43 #define _GNU_SOURCE
44 #include <getopt.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <signal.h>
50
51 struct log_target *stderr_target;
52
53 void *l23_ctx = NULL;
54
55 static char *layer2_socket_path = "/tmp/osmocom_l2";
56 static char *sap_socket_path = "/tmp/osmocom_sap";
57 struct llist_head ms_list;
58 static struct osmocom_ms *ms = NULL;
59 static char *gsmtap_ip = NULL;
60
61 unsigned short vty_port = 4247;
62 int (*l23_app_work) (struct osmocom_ms *ms) = NULL;
63 int (*l23_app_exit) (struct osmocom_ms *ms) = NULL;
64 int quit = 0;
65 struct gsmtap_inst *gsmtap_inst;
66
67 const char *openbsc_copyright =
68         "%s"
69         "%s\n"
70         "License GPLv2+: GNU GPL version 2 or later "
71                 "<http://gnu.org/licenses/gpl.html>\n"
72         "This is free software: you are free to change and redistribute it.\n"
73         "There is NO WARRANTY, to the extent permitted by law.\n\n";
74
75 static void print_usage(const char *app)
76 {
77         printf("Usage: %s\n", app);
78 }
79
80 static void print_help()
81 {
82         int options = 0xff;
83         struct l23_app_info *app = l23_app_info();
84
85         if (app && app->cfg_supported != 0)
86                 options = app->cfg_supported();
87
88         printf(" Some help...\n");
89         printf("  -h --help             this text\n");
90         printf("  -s --socket           /tmp/osmocom_l2. Path to the unix "
91                 "domain socket (l2)\n");
92
93         if (options & L23_OPT_SAP)
94                 printf("  -S --sap              /tmp/osmocom_sap. Path to the "
95                         "unix domain socket (BTSAP)\n");
96
97         if (options & L23_OPT_ARFCN)
98                 printf("  -a --arfcn NR         The ARFCN to be used for layer2.\n");
99
100         if (options & L23_OPT_TAP)
101                 printf("  -i --gsmtap-ip        The destination IP used for GSMTAP.\n");
102
103         if (options & L23_OPT_VTY)
104                 printf("  -v --vty-port         The VTY port number to telnet "
105                         "to. (default %u)\n", vty_port);
106
107         if (options & L23_OPT_DBG)
108                 printf("  -d --debug            Change debug flags.\n");
109
110         if (app && app->cfg_print_help)
111                 app->cfg_print_help();
112 }
113
114 static void build_config(char **opt, struct option **option)
115 {
116         struct l23_app_info *app;
117         struct option *app_opp = NULL;
118         int app_len = 0, len;
119
120         static struct option long_options[] = {
121                 {"help", 0, 0, 'h'},
122                 {"socket", 1, 0, 's'},
123                 {"sap", 1, 0, 'S'},
124                 {"arfcn", 1, 0, 'a'},
125                 {"gsmtap-ip", 1, 0, 'i'},
126                 {"vty-port", 1, 0, 'v'},
127                 {"debug", 1, 0, 'd'},
128         };
129
130
131         app = l23_app_info();
132         *opt = talloc_asprintf(l23_ctx, "hs:S:a:i:v:d:%s",
133                                app && app->getopt_string ? app->getopt_string : "");
134
135         len = ARRAY_SIZE(long_options);
136         if (app && app->cfg_getopt_opt)
137                 app_len = app->cfg_getopt_opt(&app_opp);
138
139         *option = talloc_zero_array(l23_ctx, struct option, len + app_len + 1);
140         memcpy(*option, long_options, sizeof(long_options));
141         memcpy(*option + len, app_opp, app_len * sizeof(struct option));
142 }
143
144 static void handle_options(int argc, char **argv)
145 {
146         struct l23_app_info *app = l23_app_info();
147         struct option *long_options;
148         char *opt;
149
150         build_config(&opt, &long_options);
151
152         while (1) {
153                 int option_index = 0, c;
154
155                 c = getopt_long(argc, argv, opt,
156                                 long_options, &option_index);
157                 if (c == -1)
158                         break;
159
160                 switch (c) {
161                 case 'h':
162                         print_usage(argv[0]);
163                         print_help();
164                         exit(0);
165                         break;
166                 case 's':
167                         layer2_socket_path = talloc_strdup(l23_ctx, optarg);
168                         break;
169                 case 'S':
170                         sap_socket_path = talloc_strdup(l23_ctx, optarg);
171                         break;
172                 case 'a':
173                         ms->test_arfcn = atoi(optarg);
174                         break;
175                 case 'i':
176                         gsmtap_ip = optarg;
177                         break;
178                 case 'v':
179                         vty_port = atoi(optarg);
180                         break;
181                 case 'd':
182                         log_parse_category_mask(stderr_target, optarg);
183                         break;
184                 default:
185                         if (app && app->cfg_handle_opt)
186                                 app->cfg_handle_opt(c, optarg);
187                         break;
188                 }
189         }
190
191         talloc_free(opt);
192         talloc_free(long_options);
193 }
194
195 void sighandler(int sigset)
196 {
197         int rc = 0;
198
199         if (sigset == SIGHUP || sigset == SIGPIPE)
200                 return;
201
202         fprintf(stderr, "Signal %d recevied.\n", sigset);
203         if (l23_app_exit)
204                 rc = l23_app_exit(ms);
205
206         if (rc != -EBUSY)
207                 exit (0);
208 }
209
210 static void print_copyright()
211 {
212         struct l23_app_info *app;
213         app = l23_app_info();
214         printf(openbsc_copyright,
215                app && app->copyright ? app->copyright : "",
216                app && app->contribution ? app->contribution : "");
217 }
218
219 int main(int argc, char **argv)
220 {
221         int rc;
222
223         INIT_LLIST_HEAD(&ms_list);
224         log_init(&log_info);
225         stderr_target = log_target_create_stderr();
226         log_add_target(stderr_target);
227         log_set_all_filter(stderr_target, 1);
228
229         l23_ctx = talloc_named_const(NULL, 1, "layer2 context");
230
231         ms = talloc_zero(l23_ctx, struct osmocom_ms);
232         if (!ms) {
233                 fprintf(stderr, "Failed to allocate MS\n");
234                 exit(1);
235         }
236
237         print_copyright();
238
239         llist_add_tail(&ms->entity, &ms_list);
240
241         sprintf(ms->name, "1");
242
243         ms->test_arfcn = 871;
244
245         handle_options(argc, argv);
246
247         rc = layer2_open(ms, layer2_socket_path);
248         if (rc < 0) {
249                 fprintf(stderr, "Failed during layer2_open()\n");
250                 exit(1);
251         }
252
253         rc = sap_open(ms, sap_socket_path);
254         if (rc < 0)
255                 fprintf(stderr, "Failed during sap_open(), no SIM reader\n");
256
257         ms->lapdm_channel.lapdm_dcch.l1_ctx = ms;
258         ms->lapdm_channel.lapdm_dcch.l3_ctx = ms;
259         ms->lapdm_channel.lapdm_acch.l1_ctx = ms;
260         ms->lapdm_channel.lapdm_acch.l3_ctx = ms;
261         lapdm_channel_init(&ms->lapdm_channel, LAPDM_MODE_MS);
262         lapdm_channel_set_l1(&ms->lapdm_channel, l1ctl_ph_prim_cb, ms);
263
264         rc = l23_app_init(ms);
265         if (rc < 0)
266                 exit(1);
267
268         if (gsmtap_ip) {
269                 gsmtap_inst = gsmtap_source_init(gsmtap_ip, GSMTAP_UDP_PORT, 1);
270                 if (!gsmtap_inst) {
271                         fprintf(stderr, "Failed during gsmtap_init()\n");
272                         exit(1);
273                 }
274                 gsmtap_source_add_sink(gsmtap_inst);
275         }
276
277         signal(SIGINT, sighandler);
278         signal(SIGHUP, sighandler);
279         signal(SIGTERM, sighandler);
280         signal(SIGPIPE, sighandler);
281
282         while (!quit) {
283                 if (l23_app_work)
284                         l23_app_work(ms);
285                 osmo_select_main(0);
286         }
287
288         return 0;
289 }