Merge commit 'af5ee34c353ea2868a4b04b227bc1b511e1ac42b'
[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/misc/layer3.h>
28 #include <osmocom/bb/common/lapdm.h>
29 #include <osmocom/bb/common/logging.h>
30 #include <osmocom/bb/common/l23_app.h>
31
32 #include <osmocore/msgb.h>
33 #include <osmocore/talloc.h>
34 #include <osmocore/select.h>
35 #include <osmocore/linuxlist.h>
36 #include <osmocore/gsmtap_util.h>
37
38 #include <arpa/inet.h>
39
40 #define _GNU_SOURCE
41 #include <getopt.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <signal.h>
47
48 struct log_target *stderr_target;
49
50 void *l23_ctx = NULL;
51 static char *socket_path = "/tmp/osmocom_l2";
52 struct llist_head ms_list;
53 static struct osmocom_ms *ms = NULL;
54 static uint32_t gsmtap_ip = 0;
55 unsigned short vty_port = 4247;
56 int (*l23_app_work) (struct osmocom_ms *ms) = NULL;
57 int (*l23_app_exit) (struct osmocom_ms *ms) = NULL;
58 int quit = 0;
59
60 const char *openbsc_copyright =
61         "Copyright (C) 2008-2010 ...\n"
62         "Contributions by ...\n\n"
63         "License GPLv2+: GNU GPL version 2 or later "
64                 "<http://gnu.org/licenses/gpl.html>\n"
65         "This is free software: you are free to change and redistribute it.\n"
66         "There is NO WARRANTY, to the extent permitted by law.\n";
67
68 static void print_usage(const char *app)
69 {
70         printf("Usage: %s\n", app);
71 }
72
73 static void print_help()
74 {
75         printf(" Some help...\n");
76         printf("  -h --help             this text\n");
77         printf("  -s --socket           /tmp/osmocom_l2. Path to the unix "
78                 "domain socket\n");
79         printf("  -a --arfcn NR         The ARFCN to be used for layer2.\n");
80         printf("  -i --gsmtap-ip        The destination IP used for GSMTAP.\n");
81         printf("  -v --vty-port         The VTY port number to telnet to. "
82                 "(default %u)\n", vty_port);
83         printf("  -d --debug            Change debug flags.\n");
84 }
85
86 static void handle_options(int argc, char **argv)
87 {
88         struct sockaddr_in gsmtap;
89         while (1) {
90                 int option_index = 0, c;
91                 static struct option long_options[] = {
92                         {"help", 0, 0, 'h'},
93                         {"socket", 1, 0, 's'},
94                         {"arfcn", 1, 0, 'a'},
95                         {"gsmtap-ip", 1, 0, 'i'},
96                         {"vty-port", 1, 0, 'v'},
97                         {"debug", 1, 0, 'd'},
98                         {0, 0, 0, 0},
99                 };
100
101                 c = getopt_long(argc, argv, "hs:a:i:v:d:",
102                                 long_options, &option_index);
103                 if (c == -1)
104                         break;
105
106                 switch (c) {
107                 case 'h':
108                         print_usage(argv[0]);
109                         print_help();
110                         exit(0);
111                         break;
112                 case 's':
113                         socket_path = talloc_strdup(l23_ctx, optarg);
114                         break;
115                 case 'a':
116                         ms->test_arfcn = atoi(optarg);
117                         break;
118                 case 'i':
119                         if (!inet_aton(optarg, &gsmtap.sin_addr)) {
120                                 perror("inet_aton");
121                                 exit(2);
122                         }
123                         gsmtap_ip = ntohl(gsmtap.sin_addr.s_addr);
124                         break;
125                 case 'v':
126                         vty_port = atoi(optarg);
127                         break;
128                 case 'd':
129                         log_parse_category_mask(stderr_target, optarg);
130                         break;
131                 default:
132                         break;
133                 }
134         }
135 }
136
137 void sighandler(int sigset)
138 {
139         int rc = 0;
140
141         if (sigset == SIGHUP || sigset == SIGPIPE)
142                 return;
143
144         fprintf(stderr, "Signal %d recevied.\n", sigset);
145         if (l23_app_exit)
146                 rc = l23_app_exit(ms);
147
148         if (rc != -EBUSY)
149                 exit (0);
150 }
151
152 int main(int argc, char **argv)
153 {
154         int rc;
155
156         printf("%s\n", openbsc_copyright);
157
158         INIT_LLIST_HEAD(&ms_list);
159         log_init(&log_info);
160         stderr_target = log_target_create_stderr();
161         log_add_target(stderr_target);
162         log_set_all_filter(stderr_target, 1);
163
164         l23_ctx = talloc_named_const(NULL, 1, "layer2 context");
165
166         ms = talloc_zero(l23_ctx, struct osmocom_ms);
167         if (!ms) {
168                 fprintf(stderr, "Failed to allocate MS\n");
169                 exit(1);
170         }
171         llist_add_tail(&ms->entity, &ms_list);
172
173         sprintf(ms->name, "1");
174
175         ms->test_arfcn = 871;
176
177         handle_options(argc, argv);
178
179         rc = layer2_open(ms, socket_path);
180         if (rc < 0) {
181                 fprintf(stderr, "Failed during layer2_open()\n");
182                 exit(1);
183         }
184
185         lapdm_init(&ms->l2_entity.lapdm_dcch, ms);
186         lapdm_init(&ms->l2_entity.lapdm_acch, ms);
187
188         rc = l23_app_init(ms);
189         if (rc < 0)
190                 exit(1);
191
192         if (gsmtap_ip) {
193                 rc = gsmtap_init(gsmtap_ip);
194                 if (rc < 0) {
195                         fprintf(stderr, "Failed during gsmtap_init()\n");
196                         exit(1);
197                 }
198         }
199
200         signal(SIGINT, sighandler);
201         signal(SIGHUP, sighandler);
202         signal(SIGTERM, sighandler);
203         signal(SIGPIPE, sighandler);
204
205         while (!quit) {
206                 if (l23_app_work)
207                         l23_app_work(ms);
208                 bsc_select_main(0);
209         }
210
211         return 0;
212 }