blob: bc916b99f1e0174f850089c86212574ad96b4b08 [file] [log] [blame]
Harald Weltea9caad82020-02-14 14:32:02 +01001
2#include <signal.h>
3#include <unistd.h>
4#define _GNU_SOURCE
5#include <getopt.h>
6
7#include <osmocom/core/msgb.h>
8#include <osmocom/core/application.h>
9
10#include "client.h"
11
12static void *g_tall_ctx;
13void __thread *talloc_asn1_ctx;
14int asn_debug;
15
16static void handle_sig_usr1(int signal)
17{
18 OSMO_ASSERT(signal == SIGUSR1);
19 talloc_report_full(g_tall_ctx, stderr);
20}
21
22static void printf_help()
23{
24 printf(
25 " -h --help Print this help message\n"
Harald Welte0e968cc2020-02-22 18:16:16 +010026 " -v --version Print program version\n"
Harald Welte80a01102020-05-25 14:55:12 +020027 " -d --debug option Enable debug logging (e.g. DMAIN:DST2)\n"
Harald Weltea9caad82020-02-14 14:32:02 +010028 " -i --server-ip A.B.C.D remsim-server IP address\n"
29 " -p --server-port 13245 remsim-server TCP port\n"
Harald Welte0e968cc2020-02-22 18:16:16 +010030 " -c --client-id <0-65535> RSPRO ClientId of this client\n"
Harald Weltea9caad82020-02-14 14:32:02 +010031 " -n --client-slot <0-65535> RSPRO SlotNr of this client\n"
Harald Welte0e968cc2020-02-22 18:16:16 +010032 " -e --event-script <path> event script to be called by client\n"
33#ifdef USB_SUPPORT
34 " -V --usb-vendor VENDOR_ID\n"
35 " -P --usb-product PRODUCT_ID\n"
36 " -C --usb-config CONFIG_ID\n"
37 " -I --usb-interface INTERFACE_ID\n"
38 " -S --usb-altsetting ALTSETTING_ID\n"
39 " -A --usb-address ADDRESS\n"
40 " -H --usb-path PATH\n"
41#endif
Harald Weltea9caad82020-02-14 14:32:02 +010042 );
43}
44
Harald Welte0e968cc2020-02-22 18:16:16 +010045static void handle_options(struct client_config *cfg, int argc, char **argv)
Harald Weltea9caad82020-02-14 14:32:02 +010046{
Harald Welte0e968cc2020-02-22 18:16:16 +010047 int rc;
48
Harald Weltea9caad82020-02-14 14:32:02 +010049 while (1) {
50 int option_index = 0, c;
51 static const struct option long_options[] = {
52 { "help", 0, 0, 'h' },
Harald Welte0e968cc2020-02-22 18:16:16 +010053 { "version", 0, 0, 'v' },
Harald Welte80a01102020-05-25 14:55:12 +020054 { "debug", 1, 0, 'd' },
Harald Weltea9caad82020-02-14 14:32:02 +010055 { "server-ip", 1, 0, 'i' },
56 { "server-port", 1, 0, 'p' },
57 { "client-id", 1, 0, 'c' },
58 { "client-slot", 1, 0, 'n' },
Harald Welte0e968cc2020-02-22 18:16:16 +010059 { "atr", 1, 0, 'a' },
60 { "event-script", 1, 0, 'e' },
61#ifdef USB_SUPPORT
62 { "usb-vendor", 1, 0, 'V' },
63 { "usb-product", 1, 0, 'P' },
64 { "usb-config", 1, 0, 'C' },
65 { "usb-interface", 1, 0, 'I' },
66 { "usb-altsetting", 1, 0, 'S' },
67 { "usb-address", 1, 0, 'A' },
68 { "usb-path", 1, 0, 'H' },
69#endif
Harald Weltea9caad82020-02-14 14:32:02 +010070 { 0, 0, 0, 0 }
71 };
72
Harald Welte80a01102020-05-25 14:55:12 +020073 c = getopt_long(argc, argv, "hvd:i:p:c:n:e:"
Harald Welte0e968cc2020-02-22 18:16:16 +010074#ifdef USB_SUPPORT
75 "V:P:C:I:S:A:H:"
76#endif
77 ,
Harald Weltea9caad82020-02-14 14:32:02 +010078 long_options, &option_index);
79 if (c == -1)
80 break;
81
82 switch (c) {
83 case 'h':
84 printf_help();
85 exit(0);
86 break;
Harald Welte0e968cc2020-02-22 18:16:16 +010087 case 'v':
88 printf("osmo-remsim-client version %s\n", VERSION);
89 exit(0);
90 break;
Harald Welte80a01102020-05-25 14:55:12 +020091 case 'd':
92 log_parse_category_mask(osmo_stderr_target, optarg);
93 break;
Harald Weltea9caad82020-02-14 14:32:02 +010094 case 'i':
Harald Welte0e968cc2020-02-22 18:16:16 +010095 osmo_talloc_replace_string(cfg, &cfg->server_host, optarg);
Harald Weltea9caad82020-02-14 14:32:02 +010096 break;
97 case 'p':
Harald Welte0e968cc2020-02-22 18:16:16 +010098 cfg->server_port = atoi(optarg);
Harald Weltea9caad82020-02-14 14:32:02 +010099 break;
100 case 'c':
Harald Welte0e968cc2020-02-22 18:16:16 +0100101 cfg->client_id = atoi(optarg);
Harald Weltea9caad82020-02-14 14:32:02 +0100102 break;
103 case 'n':
Harald Welte0e968cc2020-02-22 18:16:16 +0100104 cfg->client_slot = atoi(optarg);
Harald Weltea9caad82020-02-14 14:32:02 +0100105 break;
Harald Welte0e968cc2020-02-22 18:16:16 +0100106 case 'a':
107 rc = osmo_hexparse(optarg, cfg->atr.data, ARRAY_SIZE(cfg->atr.data));
108 if (rc < 2 || rc > ARRAY_SIZE(cfg->atr.data)) {
109 fprintf(stderr, "ATR malformed\n");
110 exit(2);
111 }
112 break;
113 case 'e':
114 osmo_talloc_replace_string(cfg, &cfg->event_script, optarg);
115 break;
116#ifdef USB_SUPPORT
117 case 'V':
118 cfg->usb.vendor_id = strtol(optarg, NULL, 16);
119 break;
120 case 'P':
121 cfg->usb.product_id = strtol(optarg, NULL, 16);
122 break;
123 case 'C':
124 cfg->usb.config_id = atoi(optarg);
125 break;
126 case 'I':
127 cfg->usb.if_num = atoi(optarg);
128 break;
129 case 'S':
130 cfg->usb.altsetting = atoi(optarg);
131 break;
132 case 'A':
133 cfg->usb.addr = atoi(optarg);
134 break;
135 case 'H':
136 cfg->usb.path = optarg;
137 break;
138#endif
Harald Weltea9caad82020-02-14 14:32:02 +0100139 default:
140 break;
141 }
142 }
143}
144
145int main(int argc, char **argv)
146{
147 struct bankd_client *g_client;
Harald Welte0e968cc2020-02-22 18:16:16 +0100148 struct client_config *cfg;
Harald Weltea9caad82020-02-14 14:32:02 +0100149 char hostname[256];
150
151 gethostname(hostname, sizeof(hostname));
152
153 g_tall_ctx = talloc_named_const(NULL, 0, "global");
154 talloc_asn1_ctx = talloc_named_const(g_tall_ctx, 0, "asn1");
155 msgb_talloc_ctx_init(g_tall_ctx, 0);
156
157 osmo_init_logging2(g_tall_ctx, &log_info);
158
Harald Welte0e968cc2020-02-22 18:16:16 +0100159 cfg = client_config_init(g_tall_ctx);
160 OSMO_ASSERT(cfg);
161 handle_options(cfg, argc, argv);
Harald Weltea9caad82020-02-14 14:32:02 +0100162
Harald Welte0e968cc2020-02-22 18:16:16 +0100163 g_client = remsim_client_create(g_tall_ctx, hostname, "remsim-client",cfg);
Harald Weltea9caad82020-02-14 14:32:02 +0100164
165 osmo_fsm_inst_dispatch(g_client->srv_conn.fi, SRVC_E_ESTABLISH, NULL);
166
167 signal(SIGUSR1, handle_sig_usr1);
168
169 asn_debug = 0;
170
171 client_user_main(g_client);
172}