blob: be853a1c447dda5e32f2f0044193b2fc13a4febf [file] [log] [blame]
Harald Weltef5a0fa32019-03-03 15:44:18 +01001#include <unistd.h>
Harald Weltece638d82019-03-17 09:36:04 +01002#include <signal.h>
Harald Weltef5a0fa32019-03-03 15:44:18 +01003
Harald Weltecd7fcd72019-12-03 21:29:07 +01004#define _GNU_SOURCE
5#include <getopt.h>
6
Harald Weltef5a0fa32019-03-03 15:44:18 +01007#include <sys/eventfd.h>
8
9#include <osmocom/core/utils.h>
10#include <osmocom/core/talloc.h>
11#include <osmocom/core/application.h>
12#include <osmocom/core/select.h>
13
14#include "debug.h"
15#include "slotmap.h"
16#include "rest_api.h"
17#include "rspro_server.h"
18
19struct rspro_server *g_rps;
20void *g_tall_ctx;
Harald Welteb54a51e2019-03-31 15:57:59 +020021__thread void *talloc_asn1_ctx;
22
Harald Weltef5a0fa32019-03-03 15:44:18 +010023struct osmo_fd g_event_ofd;
24
Harald Weltece638d82019-03-17 09:36:04 +010025static void handle_sig_usr1(int signal)
26{
27 OSMO_ASSERT(signal == SIGUSR1);
Harald Welteb54a51e2019-03-31 15:57:59 +020028 talloc_report_full(g_tall_ctx, stderr);
Harald Weltece638d82019-03-17 09:36:04 +010029}
30
Harald Weltecd7fcd72019-12-03 21:29:07 +010031static void print_help()
32{
33 printf( " Some useful help...\n"
34 " -h --help This text\n"
35 " -V --version Print version of the program\n"
Harald Welte80a01102020-05-25 14:55:12 +020036 " -d --debug option Enable debug logging (e.g. DMAIN:DST2)\n"
Harald Weltecd7fcd72019-12-03 21:29:07 +010037 );
38}
39
40static void handle_options(int argc, char **argv)
41{
42 while (1) {
43 int option_index = 0, c;
44 static struct option long_options[] = {
45 { "help", 0, 0, 'h' },
46 { "version", 0, 0, 'V' },
Harald Welte80a01102020-05-25 14:55:12 +020047 { "debug", 1, 0, 'd' },
Harald Weltecd7fcd72019-12-03 21:29:07 +010048 {0, 0, 0, 0}
49 };
50
Harald Welte80a01102020-05-25 14:55:12 +020051 c = getopt_long(argc, argv, "hVd:", long_options, &option_index);
Harald Weltecd7fcd72019-12-03 21:29:07 +010052 if (c == -1)
53 break;
54
55 switch (c) {
56 case 'h':
57 print_help();
58 exit(0);
59 break;
Harald Welte80a01102020-05-25 14:55:12 +020060 case 'd':
61 log_parse_category_mask(osmo_stderr_target, optarg);
62 break;
Harald Weltecd7fcd72019-12-03 21:29:07 +010063 case 'V':
64 printf("osmo-resmim-server version %s\n", VERSION);
65 exit(0);
66 break;
67 default:
68 /* ignore */
69 break;
70 }
71 }
72
73 if (argc > optind) {
74 fprintf(stderr, "Unsupported extra positional arguments in command line\n");
75 exit(2);
76 }
77}
78
Harald Weltef5a0fa32019-03-03 15:44:18 +010079int main(int argc, char **argv)
80{
Harald Welte0c50c342019-07-21 20:16:29 +020081 void *talloc_rest_ctx;
Harald Weltef5a0fa32019-03-03 15:44:18 +010082 int rc;
83
84 g_tall_ctx = talloc_named_const(NULL, 0, "global");
Harald Welteb54a51e2019-03-31 15:57:59 +020085 talloc_asn1_ctx = talloc_named_const(g_tall_ctx, 0, "asn1");
Harald Welte0c50c342019-07-21 20:16:29 +020086 talloc_rest_ctx = talloc_named_const(g_tall_ctx, 0, "rest");
Harald Welte92fd7342019-07-18 18:46:48 +020087 msgb_talloc_ctx_init(g_tall_ctx, 0);
Harald Weltef5a0fa32019-03-03 15:44:18 +010088
89 osmo_init_logging2(g_tall_ctx, &log_info);
90
Harald Weltecd7fcd72019-12-03 21:29:07 +010091 handle_options(argc, argv);
92
Harald Weltef5a0fa32019-03-03 15:44:18 +010093 g_rps = rspro_server_create(g_tall_ctx, "0.0.0.0", 9998);
94 if (!g_rps)
95 exit(1);
96 g_rps->slotmaps = slotmap_init(g_rps);
97 if (!g_rps->slotmaps)
98 goto out_rspro;
99
100 g_rps->comp_id.type = ComponentType_remsimServer;
101 OSMO_STRLCPY_ARRAY(g_rps->comp_id.name, "fixme-name");
102 OSMO_STRLCPY_ARRAY(g_rps->comp_id.software, "remsim-server");
103 OSMO_STRLCPY_ARRAY(g_rps->comp_id.sw_version, PACKAGE_VERSION);
104 /* FIXME: other members of app_comp_id */
105
106 rc = eventfd(0, 0);
107 if (rc < 0)
108 goto out_rps;
109 osmo_fd_setup(&g_event_ofd, rc, BSC_FD_READ, event_fd_cb, g_rps, 0);
110 osmo_fd_register(&g_event_ofd);
111
Harald Weltece638d82019-03-17 09:36:04 +0100112 signal(SIGUSR1, handle_sig_usr1);
113
Harald Welte0c50c342019-07-21 20:16:29 +0200114 rc = rest_api_init(talloc_rest_ctx, 9997);
Harald Weltef5a0fa32019-03-03 15:44:18 +0100115 if (rc < 0)
116 goto out_eventfd;
117
118 while (1) {
119 osmo_select_main(0);
120 }
121
122 rest_api_fini();
123
124 exit(0);
125
126out_eventfd:
127 close(g_event_ofd.fd);
128out_rps:
129 talloc_free(g_rps->slotmaps);
130 talloc_free(g_rps);
131out_rspro:
132 rspro_server_destroy(g_rps);
133
134 exit(1);
135}