1 /* Main method of the layer2/3 stack */
3 /* (C) 2010 by Holger Hans Peter Freyther
4 * (C) 2010 by Harald Welte <laforge@gnumonks.org>
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.
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.
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.
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>
33 #include <osmocore/msgb.h>
34 #include <osmocore/talloc.h>
35 #include <osmocore/select.h>
36 #include <osmocore/linuxlist.h>
37 #include <osmocore/gsmtap_util.h>
38 #include <osmocore/utils.h>
40 #include <arpa/inet.h>
50 struct log_target *stderr_target;
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;
63 const char *openbsc_copyright =
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";
71 static void print_usage(const char *app)
73 printf("Usage: %s\n", app);
76 static void print_help()
79 struct l23_app_info *app = l23_app_info();
81 if (app && app->cfg_supported != 0)
82 options = app->cfg_supported();
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");
89 if (options & L23_OPT_SAP)
90 printf(" -S --sap /tmp/osmocom_sap. Path to the "
91 "unix domain socket (BTSAP)\n");
93 if (options & L23_OPT_ARFCN)
94 printf(" -a --arfcn NR The ARFCN to be used for layer2.\n");
96 if (options & L23_OPT_TAP)
97 printf(" -i --gsmtap-ip The destination IP used for GSMTAP.\n");
99 if (options & L23_OPT_VTY)
100 printf(" -v --vty-port The VTY port number to telnet "
101 "to. (default %u)\n", vty_port);
103 if (options & L23_OPT_DBG)
104 printf(" -d --debug Change debug flags.\n");
106 if (app && app->cfg_print_help)
107 app->cfg_print_help();
110 static void build_config(char **opt, struct option **option)
112 struct l23_app_info *app;
113 static struct option long_options[] = {
115 {"socket", 1, 0, 's'},
117 {"arfcn", 1, 0, 'a'},
118 {"gsmtap-ip", 1, 0, 'i'},
119 {"vty-port", 1, 0, 'v'},
120 {"debug", 1, 0, 'd'},
124 app = l23_app_info();
125 *opt = talloc_asprintf(l23_ctx, "hs:S:a:i:v:d:%s",
126 app && app->getopt_string ? app->getopt_string : "");
127 *option = talloc_zero_array(l23_ctx, struct option,
128 ARRAY_SIZE(long_options) + 1);
129 memcpy(*option, long_options, sizeof(long_options));
132 static void handle_options(int argc, char **argv)
134 struct sockaddr_in gsmtap;
135 struct l23_app_info *app = l23_app_info();
136 struct option *long_options;
139 build_config(&opt, &long_options);
142 int option_index = 0, c;
144 c = getopt_long(argc, argv, opt,
145 long_options, &option_index);
151 print_usage(argv[0]);
156 layer2_socket_path = talloc_strdup(l23_ctx, optarg);
159 sap_socket_path = talloc_strdup(l23_ctx, optarg);
162 ms->test_arfcn = atoi(optarg);
165 if (!inet_aton(optarg, &gsmtap.sin_addr)) {
169 gsmtap_ip = ntohl(gsmtap.sin_addr.s_addr);
172 vty_port = atoi(optarg);
175 log_parse_category_mask(stderr_target, optarg);
178 if (app && app->cfg_handle_opt)
179 app->cfg_handle_opt(c, optarg);
185 talloc_free(long_options);
188 void sighandler(int sigset)
192 if (sigset == SIGHUP || sigset == SIGPIPE)
195 fprintf(stderr, "Signal %d recevied.\n", sigset);
197 rc = l23_app_exit(ms);
203 static void print_copyright()
205 struct l23_app_info *app;
206 app = l23_app_info();
207 printf(openbsc_copyright,
208 app && app->copyright ? app->copyright : "",
209 app && app->contribution ? app->contribution : "");
212 int main(int argc, char **argv)
216 INIT_LLIST_HEAD(&ms_list);
218 stderr_target = log_target_create_stderr();
219 log_add_target(stderr_target);
220 log_set_all_filter(stderr_target, 1);
222 l23_ctx = talloc_named_const(NULL, 1, "layer2 context");
224 ms = talloc_zero(l23_ctx, struct osmocom_ms);
226 fprintf(stderr, "Failed to allocate MS\n");
232 llist_add_tail(&ms->entity, &ms_list);
234 sprintf(ms->name, "1");
236 ms->test_arfcn = 871;
238 handle_options(argc, argv);
240 rc = layer2_open(ms, layer2_socket_path);
242 fprintf(stderr, "Failed during layer2_open()\n");
246 rc = sap_open(ms, sap_socket_path);
248 fprintf(stderr, "Failed during sap_open(), no SIM reader\n");
250 lapdm_init(&ms->l2_entity.lapdm_dcch, ms);
251 lapdm_init(&ms->l2_entity.lapdm_acch, ms);
253 rc = l23_app_init(ms);
258 rc = gsmtap_init(gsmtap_ip);
260 fprintf(stderr, "Failed during gsmtap_init()\n");
265 signal(SIGINT, sighandler);
266 signal(SIGHUP, sighandler);
267 signal(SIGTERM, sighandler);
268 signal(SIGPIPE, sighandler);