blob: c2d54e5d46bc238eaf7dade2690663c52e31cf7c [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>
Harald Welte73bbd542021-12-08 20:39:55 +010011#include <osmocom/core/logging.h>
12#include <osmocom/core/fsm.h>
Harald Weltef5a0fa32019-03-03 15:44:18 +010013#include <osmocom/core/application.h>
14#include <osmocom/core/select.h>
15
16#include "debug.h"
17#include "slotmap.h"
18#include "rest_api.h"
19#include "rspro_server.h"
20
21struct rspro_server *g_rps;
22void *g_tall_ctx;
Harald Welteb54a51e2019-03-31 15:57:59 +020023__thread void *talloc_asn1_ctx;
24
Harald Weltef5a0fa32019-03-03 15:44:18 +010025struct osmo_fd g_event_ofd;
26
Harald Weltece638d82019-03-17 09:36:04 +010027static void handle_sig_usr1(int signal)
28{
29 OSMO_ASSERT(signal == SIGUSR1);
Harald Welteb54a51e2019-03-31 15:57:59 +020030 talloc_report_full(g_tall_ctx, stderr);
Harald Weltece638d82019-03-17 09:36:04 +010031}
32
Harald Weltecd7fcd72019-12-03 21:29:07 +010033static void print_help()
34{
35 printf( " Some useful help...\n"
36 " -h --help This text\n"
37 " -V --version Print version of the program\n"
Harald Welte80a01102020-05-25 14:55:12 +020038 " -d --debug option Enable debug logging (e.g. DMAIN:DST2)\n"
Harald Weltecd7fcd72019-12-03 21:29:07 +010039 );
40}
41
42static void handle_options(int argc, char **argv)
43{
44 while (1) {
45 int option_index = 0, c;
46 static struct option long_options[] = {
47 { "help", 0, 0, 'h' },
48 { "version", 0, 0, 'V' },
Harald Welte80a01102020-05-25 14:55:12 +020049 { "debug", 1, 0, 'd' },
Harald Weltecd7fcd72019-12-03 21:29:07 +010050 {0, 0, 0, 0}
51 };
52
Harald Welte80a01102020-05-25 14:55:12 +020053 c = getopt_long(argc, argv, "hVd:", long_options, &option_index);
Harald Weltecd7fcd72019-12-03 21:29:07 +010054 if (c == -1)
55 break;
56
57 switch (c) {
58 case 'h':
59 print_help();
60 exit(0);
61 break;
Harald Welte80a01102020-05-25 14:55:12 +020062 case 'd':
63 log_parse_category_mask(osmo_stderr_target, optarg);
64 break;
Harald Weltecd7fcd72019-12-03 21:29:07 +010065 case 'V':
66 printf("osmo-resmim-server version %s\n", VERSION);
67 exit(0);
68 break;
69 default:
70 /* ignore */
71 break;
72 }
73 }
74
75 if (argc > optind) {
76 fprintf(stderr, "Unsupported extra positional arguments in command line\n");
77 exit(2);
78 }
79}
80
Harald Weltef5a0fa32019-03-03 15:44:18 +010081int main(int argc, char **argv)
82{
Harald Welte0c50c342019-07-21 20:16:29 +020083 void *talloc_rest_ctx;
Harald Weltef5a0fa32019-03-03 15:44:18 +010084 int rc;
85
86 g_tall_ctx = talloc_named_const(NULL, 0, "global");
Harald Welteb54a51e2019-03-31 15:57:59 +020087 talloc_asn1_ctx = talloc_named_const(g_tall_ctx, 0, "asn1");
Harald Welte0c50c342019-07-21 20:16:29 +020088 talloc_rest_ctx = talloc_named_const(g_tall_ctx, 0, "rest");
Harald Welte92fd7342019-07-18 18:46:48 +020089 msgb_talloc_ctx_init(g_tall_ctx, 0);
Harald Weltef5a0fa32019-03-03 15:44:18 +010090
91 osmo_init_logging2(g_tall_ctx, &log_info);
Harald Welte73bbd542021-12-08 20:39:55 +010092 log_set_print_level(osmo_stderr_target, 1);
93 log_set_print_category(osmo_stderr_target, 1);
94 log_set_print_category_hex(osmo_stderr_target, 0);
95 osmo_fsm_log_addr(0);
Harald Weltea9d7ad12021-12-08 21:09:12 +010096 log_set_print_tid(osmo_stderr_target, 1);
97 log_enable_multithread();
Harald Weltef5a0fa32019-03-03 15:44:18 +010098
Harald Weltecd7fcd72019-12-03 21:29:07 +010099 handle_options(argc, argv);
100
Harald Weltef5a0fa32019-03-03 15:44:18 +0100101 g_rps = rspro_server_create(g_tall_ctx, "0.0.0.0", 9998);
102 if (!g_rps)
103 exit(1);
104 g_rps->slotmaps = slotmap_init(g_rps);
105 if (!g_rps->slotmaps)
106 goto out_rspro;
107
108 g_rps->comp_id.type = ComponentType_remsimServer;
109 OSMO_STRLCPY_ARRAY(g_rps->comp_id.name, "fixme-name");
110 OSMO_STRLCPY_ARRAY(g_rps->comp_id.software, "remsim-server");
111 OSMO_STRLCPY_ARRAY(g_rps->comp_id.sw_version, PACKAGE_VERSION);
112 /* FIXME: other members of app_comp_id */
113
114 rc = eventfd(0, 0);
115 if (rc < 0)
116 goto out_rps;
Harald Weltea5daec42020-10-18 22:40:28 +0200117 osmo_fd_setup(&g_event_ofd, rc, OSMO_FD_READ, event_fd_cb, g_rps, 0);
Harald Weltef5a0fa32019-03-03 15:44:18 +0100118 osmo_fd_register(&g_event_ofd);
119
Harald Weltece638d82019-03-17 09:36:04 +0100120 signal(SIGUSR1, handle_sig_usr1);
121
Harald Welte0c50c342019-07-21 20:16:29 +0200122 rc = rest_api_init(talloc_rest_ctx, 9997);
Harald Weltef5a0fa32019-03-03 15:44:18 +0100123 if (rc < 0)
124 goto out_eventfd;
125
126 while (1) {
127 osmo_select_main(0);
128 }
129
130 rest_api_fini();
131
132 exit(0);
133
134out_eventfd:
135 close(g_event_ofd.fd);
136out_rps:
137 talloc_free(g_rps->slotmaps);
138 talloc_free(g_rps);
139out_rspro:
140 rspro_server_destroy(g_rps);
141
142 exit(1);
143}