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