blob: 9d3268fb6d86627f6764fc59fe4a08e5a7ad4464 [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"
36 );
37}
38
39static void handle_options(int argc, char **argv)
40{
41 while (1) {
42 int option_index = 0, c;
43 static struct option long_options[] = {
44 { "help", 0, 0, 'h' },
45 { "version", 0, 0, 'V' },
46 {0, 0, 0, 0}
47 };
48
49 c = getopt_long(argc, argv, "hV", long_options, &option_index);
50 if (c == -1)
51 break;
52
53 switch (c) {
54 case 'h':
55 print_help();
56 exit(0);
57 break;
58 case 'V':
59 printf("osmo-resmim-server version %s\n", VERSION);
60 exit(0);
61 break;
62 default:
63 /* ignore */
64 break;
65 }
66 }
67
68 if (argc > optind) {
69 fprintf(stderr, "Unsupported extra positional arguments in command line\n");
70 exit(2);
71 }
72}
73
Harald Weltef5a0fa32019-03-03 15:44:18 +010074int main(int argc, char **argv)
75{
Harald Welte0c50c342019-07-21 20:16:29 +020076 void *talloc_rest_ctx;
Harald Weltef5a0fa32019-03-03 15:44:18 +010077 int rc;
78
79 g_tall_ctx = talloc_named_const(NULL, 0, "global");
Harald Welteb54a51e2019-03-31 15:57:59 +020080 talloc_asn1_ctx = talloc_named_const(g_tall_ctx, 0, "asn1");
Harald Welte0c50c342019-07-21 20:16:29 +020081 talloc_rest_ctx = talloc_named_const(g_tall_ctx, 0, "rest");
Harald Welte92fd7342019-07-18 18:46:48 +020082 msgb_talloc_ctx_init(g_tall_ctx, 0);
Harald Weltef5a0fa32019-03-03 15:44:18 +010083
84 osmo_init_logging2(g_tall_ctx, &log_info);
85
Harald Weltecd7fcd72019-12-03 21:29:07 +010086 handle_options(argc, argv);
87
Harald Weltef5a0fa32019-03-03 15:44:18 +010088 g_rps = rspro_server_create(g_tall_ctx, "0.0.0.0", 9998);
89 if (!g_rps)
90 exit(1);
91 g_rps->slotmaps = slotmap_init(g_rps);
92 if (!g_rps->slotmaps)
93 goto out_rspro;
94
95 g_rps->comp_id.type = ComponentType_remsimServer;
96 OSMO_STRLCPY_ARRAY(g_rps->comp_id.name, "fixme-name");
97 OSMO_STRLCPY_ARRAY(g_rps->comp_id.software, "remsim-server");
98 OSMO_STRLCPY_ARRAY(g_rps->comp_id.sw_version, PACKAGE_VERSION);
99 /* FIXME: other members of app_comp_id */
100
101 rc = eventfd(0, 0);
102 if (rc < 0)
103 goto out_rps;
104 osmo_fd_setup(&g_event_ofd, rc, BSC_FD_READ, event_fd_cb, g_rps, 0);
105 osmo_fd_register(&g_event_ofd);
106
Harald Weltece638d82019-03-17 09:36:04 +0100107 signal(SIGUSR1, handle_sig_usr1);
108
Harald Welte0c50c342019-07-21 20:16:29 +0200109 rc = rest_api_init(talloc_rest_ctx, 9997);
Harald Weltef5a0fa32019-03-03 15:44:18 +0100110 if (rc < 0)
111 goto out_eventfd;
112
113 while (1) {
114 osmo_select_main(0);
115 }
116
117 rest_api_fini();
118
119 exit(0);
120
121out_eventfd:
122 close(g_event_ofd.fd);
123out_rps:
124 talloc_free(g_rps->slotmaps);
125 talloc_free(g_rps);
126out_rspro:
127 rspro_server_destroy(g_rps);
128
129 exit(1);
130}