blob: bbc29908a68cdf5d9375893b99250d093637416d [file] [log] [blame]
Harald Weltea9caad82020-02-14 14:32:02 +01001
Harald Weltedc347642020-10-20 19:14:55 +02002#include <errno.h>
Harald Weltea9caad82020-02-14 14:32:02 +01003#include <signal.h>
4#include <unistd.h>
5#define _GNU_SOURCE
6#include <getopt.h>
7
8#include <osmocom/core/msgb.h>
9#include <osmocom/core/application.h>
10
11#include "client.h"
12
13static void *g_tall_ctx;
14void __thread *talloc_asn1_ctx;
15int asn_debug;
16
17static void handle_sig_usr1(int signal)
18{
19 OSMO_ASSERT(signal == SIGUSR1);
20 talloc_report_full(g_tall_ctx, stderr);
21}
22
23static void printf_help()
24{
25 printf(
26 " -h --help Print this help message\n"
Harald Welte0e968cc2020-02-22 18:16:16 +010027 " -v --version Print program version\n"
Harald Welte80a01102020-05-25 14:55:12 +020028 " -d --debug option Enable debug logging (e.g. DMAIN:DST2)\n"
Harald Weltea9caad82020-02-14 14:32:02 +010029 " -i --server-ip A.B.C.D remsim-server IP address\n"
30 " -p --server-port 13245 remsim-server TCP port\n"
Harald Welte0e968cc2020-02-22 18:16:16 +010031 " -c --client-id <0-65535> RSPRO ClientId of this client\n"
Harald Weltea9caad82020-02-14 14:32:02 +010032 " -n --client-slot <0-65535> RSPRO SlotNr of this client\n"
Harald Welte0e968cc2020-02-22 18:16:16 +010033 " -e --event-script <path> event script to be called by client\n"
34#ifdef USB_SUPPORT
35 " -V --usb-vendor VENDOR_ID\n"
36 " -P --usb-product PRODUCT_ID\n"
37 " -C --usb-config CONFIG_ID\n"
38 " -I --usb-interface INTERFACE_ID\n"
39 " -S --usb-altsetting ALTSETTING_ID\n"
40 " -A --usb-address ADDRESS\n"
41 " -H --usb-path PATH\n"
42#endif
Harald Weltea9caad82020-02-14 14:32:02 +010043 );
44}
45
Harald Welte0e968cc2020-02-22 18:16:16 +010046static void handle_options(struct client_config *cfg, int argc, char **argv)
Harald Weltea9caad82020-02-14 14:32:02 +010047{
Harald Welte0e968cc2020-02-22 18:16:16 +010048 int rc;
49
Harald Weltea9caad82020-02-14 14:32:02 +010050 while (1) {
51 int option_index = 0, c;
52 static const struct option long_options[] = {
53 { "help", 0, 0, 'h' },
Harald Welte0e968cc2020-02-22 18:16:16 +010054 { "version", 0, 0, 'v' },
Harald Welte80a01102020-05-25 14:55:12 +020055 { "debug", 1, 0, 'd' },
Harald Weltea9caad82020-02-14 14:32:02 +010056 { "server-ip", 1, 0, 'i' },
57 { "server-port", 1, 0, 'p' },
58 { "client-id", 1, 0, 'c' },
59 { "client-slot", 1, 0, 'n' },
Harald Welte0e968cc2020-02-22 18:16:16 +010060 { "atr", 1, 0, 'a' },
61 { "event-script", 1, 0, 'e' },
62#ifdef USB_SUPPORT
63 { "usb-vendor", 1, 0, 'V' },
64 { "usb-product", 1, 0, 'P' },
65 { "usb-config", 1, 0, 'C' },
66 { "usb-interface", 1, 0, 'I' },
67 { "usb-altsetting", 1, 0, 'S' },
68 { "usb-address", 1, 0, 'A' },
69 { "usb-path", 1, 0, 'H' },
70#endif
Harald Weltea9caad82020-02-14 14:32:02 +010071 { 0, 0, 0, 0 }
72 };
73
Harald Welte80a01102020-05-25 14:55:12 +020074 c = getopt_long(argc, argv, "hvd:i:p:c:n:e:"
Harald Welte0e968cc2020-02-22 18:16:16 +010075#ifdef USB_SUPPORT
76 "V:P:C:I:S:A:H:"
77#endif
78 ,
Harald Weltea9caad82020-02-14 14:32:02 +010079 long_options, &option_index);
80 if (c == -1)
81 break;
82
83 switch (c) {
84 case 'h':
85 printf_help();
86 exit(0);
87 break;
Harald Welte0e968cc2020-02-22 18:16:16 +010088 case 'v':
89 printf("osmo-remsim-client version %s\n", VERSION);
90 exit(0);
91 break;
Harald Welte80a01102020-05-25 14:55:12 +020092 case 'd':
93 log_parse_category_mask(osmo_stderr_target, optarg);
94 break;
Harald Weltea9caad82020-02-14 14:32:02 +010095 case 'i':
Harald Welte0e968cc2020-02-22 18:16:16 +010096 osmo_talloc_replace_string(cfg, &cfg->server_host, optarg);
Harald Weltea9caad82020-02-14 14:32:02 +010097 break;
98 case 'p':
Harald Welte0e968cc2020-02-22 18:16:16 +010099 cfg->server_port = atoi(optarg);
Harald Weltea9caad82020-02-14 14:32:02 +0100100 break;
101 case 'c':
Harald Welte0e968cc2020-02-22 18:16:16 +0100102 cfg->client_id = atoi(optarg);
Harald Weltea9caad82020-02-14 14:32:02 +0100103 break;
104 case 'n':
Harald Welte0e968cc2020-02-22 18:16:16 +0100105 cfg->client_slot = atoi(optarg);
Harald Weltea9caad82020-02-14 14:32:02 +0100106 break;
Harald Welte0e968cc2020-02-22 18:16:16 +0100107 case 'a':
108 rc = osmo_hexparse(optarg, cfg->atr.data, ARRAY_SIZE(cfg->atr.data));
109 if (rc < 2 || rc > ARRAY_SIZE(cfg->atr.data)) {
110 fprintf(stderr, "ATR malformed\n");
111 exit(2);
112 }
113 break;
114 case 'e':
115 osmo_talloc_replace_string(cfg, &cfg->event_script, optarg);
116 break;
117#ifdef USB_SUPPORT
118 case 'V':
119 cfg->usb.vendor_id = strtol(optarg, NULL, 16);
120 break;
121 case 'P':
122 cfg->usb.product_id = strtol(optarg, NULL, 16);
123 break;
124 case 'C':
125 cfg->usb.config_id = atoi(optarg);
126 break;
127 case 'I':
128 cfg->usb.if_num = atoi(optarg);
129 break;
130 case 'S':
131 cfg->usb.altsetting = atoi(optarg);
132 break;
133 case 'A':
134 cfg->usb.addr = atoi(optarg);
135 break;
136 case 'H':
137 cfg->usb.path = optarg;
138 break;
139#endif
Harald Weltea9caad82020-02-14 14:32:02 +0100140 default:
141 break;
142 }
143 }
144}
145
Harald Weltedc347642020-10-20 19:14:55 +0200146
147static int avoid_zombies(void)
148{
149 static struct sigaction sa_chld;
150
151 sa_chld.sa_handler = SIG_IGN;
152 sigemptyset(&sa_chld.sa_mask);
153 sa_chld.sa_flags = SA_NOCLDWAIT;
154 sa_chld.sa_restorer = NULL;
155
156 return sigaction(SIGCHLD, &sa_chld, NULL);
157}
158
Harald Weltea9caad82020-02-14 14:32:02 +0100159int main(int argc, char **argv)
160{
161 struct bankd_client *g_client;
Harald Welte0e968cc2020-02-22 18:16:16 +0100162 struct client_config *cfg;
Harald Weltea9caad82020-02-14 14:32:02 +0100163 char hostname[256];
164
165 gethostname(hostname, sizeof(hostname));
166
167 g_tall_ctx = talloc_named_const(NULL, 0, "global");
168 talloc_asn1_ctx = talloc_named_const(g_tall_ctx, 0, "asn1");
169 msgb_talloc_ctx_init(g_tall_ctx, 0);
170
171 osmo_init_logging2(g_tall_ctx, &log_info);
172
Harald Welte0e968cc2020-02-22 18:16:16 +0100173 cfg = client_config_init(g_tall_ctx);
174 OSMO_ASSERT(cfg);
175 handle_options(cfg, argc, argv);
Harald Weltea9caad82020-02-14 14:32:02 +0100176
Harald Welte0e968cc2020-02-22 18:16:16 +0100177 g_client = remsim_client_create(g_tall_ctx, hostname, "remsim-client",cfg);
Harald Weltea9caad82020-02-14 14:32:02 +0100178
179 osmo_fsm_inst_dispatch(g_client->srv_conn.fi, SRVC_E_ESTABLISH, NULL);
180
181 signal(SIGUSR1, handle_sig_usr1);
182
Harald Weltedc347642020-10-20 19:14:55 +0200183 /* Silently (and portably) reap children. */
184 if (avoid_zombies() < 0) {
185 fprintf(stderr, "Unable to silently reap children: %s\n", strerror(errno));
186 exit(1);
187 }
188
Harald Weltea9caad82020-02-14 14:32:02 +0100189 asn_debug = 0;
190
191 client_user_main(g_client);
192}