summaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 066149867e845099c2cdc16144a8bfd3389b9ab3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * (C) 2016 by Holger Hans Peter Freyther
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#define _GNU_SOURCE

#include "evpoll.h"
#include "vty.h"
#include "logging.h"
#include "mncc.h"
#include "app.h"
#include "call.h"

#include <osmocom/core/application.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/stats.h>

#include <osmocom/vty/logging.h>
#include <osmocom/vty/stats.h>
#include <osmocom/vty/ports.h>
#include <osmocom/vty/telnet_interface.h>

#include <talloc.h>

#include <sofia-sip/su_glib.h>

#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>

void *tall_mncc_ctx;

static char *config_file = "osmo-sip-connector.cfg";

static struct log_info_cat mncc_sip_categories[] = {
	[DSIP] = {
		.name		= "DSIP",
		.description	= "SIP interface",
		.enabled = 1, .loglevel = LOGL_NOTICE,
	},
	[DMNCC] = {
		.name		= "DMNCC",
		.description	= "MNCC interface",
		.enabled = 1, .loglevel = LOGL_NOTICE,
	},
	[DAPP] = {
		.name		= "DAPP",
		.description	= "Application interface",
		.enabled = 1, .loglevel = LOGL_NOTICE,
	},
	[DCALL] = {
		.name		= "DCALL",
		.description	= "Call management",
		.enabled = 1, .loglevel = LOGL_NOTICE,
	},
};

static const struct log_info mncc_sip_info = {
	.cat = mncc_sip_categories,
	.num_cat = ARRAY_SIZE(mncc_sip_categories),
};

static void print_help(void)
{
	printf("OsmoSIPcon: MNCC to SIP bridge\n");
	printf("  -h --help\tthis text\n");
	printf("  -c --config-file NAME\tThe config file to use [%s]\n", config_file);
	printf("  -V --version\tPrint the version number\n");
}

static void handle_options(int argc, char **argv)
{
	while (1) {
		int option_index = 0, c;
		static struct option long_options[] = {
			{"help", 0, 0, 'h'},
			{"config-file", 1, 0, 'c'},
			{"version", 0, 0, 'V' },
			{NULL, 0, 0, 0}
		};

		c = getopt_long(argc, argv, "hc:V",
			long_options, &option_index);
		if (c == -1)
			break;

		switch (c) {
		case 'h':
			print_help();
			exit(0);
		case 'c':
			config_file = optarg;
			break;
		case 'V':
			print_version(1);
			exit(EXIT_SUCCESS);
			break;
		}
	}
}

int main(int argc, char **argv)
{
	int rc;
	GMainLoop *loop;

	/* initialize osmocom */
	tall_mncc_ctx = talloc_named_const(NULL, 0, "MNCC CTX");
	osmo_init_ignore_signals();
	osmo_init_logging2(tall_mncc_ctx, &mncc_sip_info);
	osmo_stats_init(tall_mncc_ctx);

	mncc_sip_vty_init();
	logging_vty_add_cmds(&mncc_sip_info);
	osmo_stats_vty_add_cmds(&mncc_sip_info);


	/* parsing and setup */
	g_app.sip.sofia_log_level = 2;
	handle_options(argc, argv);
	rc = vty_read_config_file(config_file, NULL);
	if (rc < 0) {
		LOGP(DAPP, LOGL_ERROR, "Can not parse config: %s %d\n",
			config_file, rc);
		exit(1);
	}

	rc = telnet_init_dynif(tall_mncc_ctx, NULL,
				vty_get_bind_addr(), OSMO_VTY_PORT_MNCC_SIP);
	if (rc < 0)
		exit(1);

	mncc_connection_init(&g_app.mncc.conn, &g_app);
	mncc_connection_start(&g_app.mncc.conn);

	/* sofia sip */
	sip_agent_init(&g_app.sip.agent, &g_app);
	rc = sip_agent_start(&g_app.sip.agent);
	if (rc < 0)
		LOGP(DSIP, LOGL_ERROR,
			"Failed to initialize SIP. Running broken\n");

	calls_init();
	app_setup(&g_app);

	/* marry sofia-sip to glib and glib to libosmocore */
	loop = g_main_loop_new(NULL, FALSE);
	g_source_attach(su_glib_root_gsource(g_app.sip.agent.root),
			g_main_loop_get_context(loop));

	/* prepare integration with osmocom */
	g_main_context_set_poll_func(g_main_loop_get_context(loop),
					(GPollFunc) evpoll);
	g_main_loop_run(loop);
	g_main_loop_unref(loop);

	return EXIT_SUCCESS;
}