blob: e3d3cf8b420b8ce1b24e3b35337838c886cc2007 [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>
Harald Welte73bbd542021-12-08 20:39:55 +01009#include <osmocom/core/logging.h>
10#include <osmocom/core/fsm.h>
Harald Weltea9caad82020-02-14 14:32:02 +010011#include <osmocom/core/application.h>
12
13#include "client.h"
14
15static void *g_tall_ctx;
16void __thread *talloc_asn1_ctx;
17int asn_debug;
18
19static void handle_sig_usr1(int signal)
20{
21 OSMO_ASSERT(signal == SIGUSR1);
22 talloc_report_full(g_tall_ctx, stderr);
23}
24
25static void printf_help()
26{
27 printf(
28 " -h --help Print this help message\n"
Harald Welte0e968cc2020-02-22 18:16:16 +010029 " -v --version Print program version\n"
Harald Welte80a01102020-05-25 14:55:12 +020030 " -d --debug option Enable debug logging (e.g. DMAIN:DST2)\n"
Harald Weltea9caad82020-02-14 14:32:02 +010031 " -i --server-ip A.B.C.D remsim-server IP address\n"
32 " -p --server-port 13245 remsim-server TCP port\n"
Harald Welte55f12b82022-01-16 14:34:12 +010033 " -c --client-id <0-1023> RSPRO ClientId of this client\n"
34 " -n --client-slot <0-1023> RSPRO SlotNr of this client\n"
Harald Welte0e968cc2020-02-22 18:16:16 +010035 " -e --event-script <path> event script to be called by client\n"
36#ifdef USB_SUPPORT
37 " -V --usb-vendor VENDOR_ID\n"
38 " -P --usb-product PRODUCT_ID\n"
39 " -C --usb-config CONFIG_ID\n"
40 " -I --usb-interface INTERFACE_ID\n"
41 " -S --usb-altsetting ALTSETTING_ID\n"
42 " -A --usb-address ADDRESS\n"
43 " -H --usb-path PATH\n"
44#endif
Harald Weltea9caad82020-02-14 14:32:02 +010045 );
46}
47
Harald Welte0e968cc2020-02-22 18:16:16 +010048static void handle_options(struct client_config *cfg, int argc, char **argv)
Harald Weltea9caad82020-02-14 14:32:02 +010049{
Harald Welte0e968cc2020-02-22 18:16:16 +010050 int rc;
51
Harald Weltea9caad82020-02-14 14:32:02 +010052 while (1) {
53 int option_index = 0, c;
54 static const struct option long_options[] = {
55 { "help", 0, 0, 'h' },
Harald Welte0e968cc2020-02-22 18:16:16 +010056 { "version", 0, 0, 'v' },
Harald Welte80a01102020-05-25 14:55:12 +020057 { "debug", 1, 0, 'd' },
Harald Weltea9caad82020-02-14 14:32:02 +010058 { "server-ip", 1, 0, 'i' },
59 { "server-port", 1, 0, 'p' },
60 { "client-id", 1, 0, 'c' },
61 { "client-slot", 1, 0, 'n' },
Harald Welte0e968cc2020-02-22 18:16:16 +010062 { "atr", 1, 0, 'a' },
63 { "event-script", 1, 0, 'e' },
64#ifdef USB_SUPPORT
65 { "usb-vendor", 1, 0, 'V' },
66 { "usb-product", 1, 0, 'P' },
67 { "usb-config", 1, 0, 'C' },
68 { "usb-interface", 1, 0, 'I' },
69 { "usb-altsetting", 1, 0, 'S' },
70 { "usb-address", 1, 0, 'A' },
71 { "usb-path", 1, 0, 'H' },
72#endif
Harald Weltea9caad82020-02-14 14:32:02 +010073 { 0, 0, 0, 0 }
74 };
75
Harald Welte80a01102020-05-25 14:55:12 +020076 c = getopt_long(argc, argv, "hvd:i:p:c:n:e:"
Harald Welte0e968cc2020-02-22 18:16:16 +010077#ifdef USB_SUPPORT
78 "V:P:C:I:S:A:H:"
79#endif
80 ,
Harald Weltea9caad82020-02-14 14:32:02 +010081 long_options, &option_index);
82 if (c == -1)
83 break;
84
85 switch (c) {
86 case 'h':
87 printf_help();
88 exit(0);
89 break;
Harald Welte0e968cc2020-02-22 18:16:16 +010090 case 'v':
91 printf("osmo-remsim-client version %s\n", VERSION);
92 exit(0);
93 break;
Harald Welte80a01102020-05-25 14:55:12 +020094 case 'd':
95 log_parse_category_mask(osmo_stderr_target, optarg);
96 break;
Harald Weltea9caad82020-02-14 14:32:02 +010097 case 'i':
Harald Welte0e968cc2020-02-22 18:16:16 +010098 osmo_talloc_replace_string(cfg, &cfg->server_host, optarg);
Harald Weltea9caad82020-02-14 14:32:02 +010099 break;
100 case 'p':
Harald Welte0e968cc2020-02-22 18:16:16 +0100101 cfg->server_port = atoi(optarg);
Harald Weltea9caad82020-02-14 14:32:02 +0100102 break;
103 case 'c':
Harald Welte0e968cc2020-02-22 18:16:16 +0100104 cfg->client_id = atoi(optarg);
Harald Weltea9caad82020-02-14 14:32:02 +0100105 break;
106 case 'n':
Harald Welte0e968cc2020-02-22 18:16:16 +0100107 cfg->client_slot = atoi(optarg);
Harald Weltea9caad82020-02-14 14:32:02 +0100108 break;
Harald Welte0e968cc2020-02-22 18:16:16 +0100109 case 'a':
110 rc = osmo_hexparse(optarg, cfg->atr.data, ARRAY_SIZE(cfg->atr.data));
111 if (rc < 2 || rc > ARRAY_SIZE(cfg->atr.data)) {
112 fprintf(stderr, "ATR malformed\n");
113 exit(2);
114 }
115 break;
116 case 'e':
117 osmo_talloc_replace_string(cfg, &cfg->event_script, optarg);
118 break;
119#ifdef USB_SUPPORT
120 case 'V':
121 cfg->usb.vendor_id = strtol(optarg, NULL, 16);
122 break;
123 case 'P':
124 cfg->usb.product_id = strtol(optarg, NULL, 16);
125 break;
126 case 'C':
127 cfg->usb.config_id = atoi(optarg);
128 break;
129 case 'I':
130 cfg->usb.if_num = atoi(optarg);
131 break;
132 case 'S':
133 cfg->usb.altsetting = atoi(optarg);
134 break;
135 case 'A':
136 cfg->usb.addr = atoi(optarg);
137 break;
138 case 'H':
139 cfg->usb.path = optarg;
140 break;
141#endif
Harald Weltea9caad82020-02-14 14:32:02 +0100142 default:
143 break;
144 }
145 }
146}
147
Harald Weltedc347642020-10-20 19:14:55 +0200148
149static int avoid_zombies(void)
150{
151 static struct sigaction sa_chld;
152
153 sa_chld.sa_handler = SIG_IGN;
154 sigemptyset(&sa_chld.sa_mask);
155 sa_chld.sa_flags = SA_NOCLDWAIT;
156 sa_chld.sa_restorer = NULL;
157
158 return sigaction(SIGCHLD, &sa_chld, NULL);
159}
160
Harald Weltea9caad82020-02-14 14:32:02 +0100161int main(int argc, char **argv)
162{
163 struct bankd_client *g_client;
Harald Welte0e968cc2020-02-22 18:16:16 +0100164 struct client_config *cfg;
Harald Weltea9caad82020-02-14 14:32:02 +0100165 char hostname[256];
166
167 gethostname(hostname, sizeof(hostname));
168
169 g_tall_ctx = talloc_named_const(NULL, 0, "global");
170 talloc_asn1_ctx = talloc_named_const(g_tall_ctx, 0, "asn1");
171 msgb_talloc_ctx_init(g_tall_ctx, 0);
172
173 osmo_init_logging2(g_tall_ctx, &log_info);
Harald Welte73bbd542021-12-08 20:39:55 +0100174 log_set_print_level(osmo_stderr_target, 1);
175 log_set_print_category(osmo_stderr_target, 1);
176 log_set_print_category_hex(osmo_stderr_target, 0);
177 osmo_fsm_log_addr(0);
Harald Weltea9caad82020-02-14 14:32:02 +0100178
Harald Welte0e968cc2020-02-22 18:16:16 +0100179 cfg = client_config_init(g_tall_ctx);
180 OSMO_ASSERT(cfg);
181 handle_options(cfg, argc, argv);
Harald Weltea9caad82020-02-14 14:32:02 +0100182
Harald Welte0e968cc2020-02-22 18:16:16 +0100183 g_client = remsim_client_create(g_tall_ctx, hostname, "remsim-client",cfg);
Harald Weltea9caad82020-02-14 14:32:02 +0100184
185 osmo_fsm_inst_dispatch(g_client->srv_conn.fi, SRVC_E_ESTABLISH, NULL);
186
187 signal(SIGUSR1, handle_sig_usr1);
188
Harald Weltedc347642020-10-20 19:14:55 +0200189 /* Silently (and portably) reap children. */
190 if (avoid_zombies() < 0) {
Harald Welte7293e7b2021-12-08 21:29:11 +0100191 LOGP(DMAIN, LOGL_FATAL, "Unable to silently reap children: %s\n", strerror(errno));
Harald Weltedc347642020-10-20 19:14:55 +0200192 exit(1);
193 }
194
Harald Weltea9caad82020-02-14 14:32:02 +0100195 asn_debug = 0;
196
197 client_user_main(g_client);
198}