blob: 6587e6bbc96efe3d7ef6715e355d6e40d9d1c9bf [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 Weltea9caad82020-02-14 14:32:02 +010027 " -i --server-ip A.B.C.D remsim-server IP address\n"
28 " -p --server-port 13245 remsim-server TCP port\n"
Harald Welte0e968cc2020-02-22 18:16:16 +010029 " -c --client-id <0-65535> RSPRO ClientId of this client\n"
Harald Weltea9caad82020-02-14 14:32:02 +010030 " -n --client-slot <0-65535> RSPRO SlotNr of this client\n"
Harald Welte0e968cc2020-02-22 18:16:16 +010031 " -e --event-script <path> event script to be called by client\n"
32#ifdef USB_SUPPORT
33 " -V --usb-vendor VENDOR_ID\n"
34 " -P --usb-product PRODUCT_ID\n"
35 " -C --usb-config CONFIG_ID\n"
36 " -I --usb-interface INTERFACE_ID\n"
37 " -S --usb-altsetting ALTSETTING_ID\n"
38 " -A --usb-address ADDRESS\n"
39 " -H --usb-path PATH\n"
40#endif
Harald Weltea9caad82020-02-14 14:32:02 +010041 );
42}
43
Harald Welte0e968cc2020-02-22 18:16:16 +010044static void handle_options(struct client_config *cfg, int argc, char **argv)
Harald Weltea9caad82020-02-14 14:32:02 +010045{
Harald Welte0e968cc2020-02-22 18:16:16 +010046 int rc;
47
Harald Weltea9caad82020-02-14 14:32:02 +010048 while (1) {
49 int option_index = 0, c;
50 static const struct option long_options[] = {
51 { "help", 0, 0, 'h' },
Harald Welte0e968cc2020-02-22 18:16:16 +010052 { "version", 0, 0, 'v' },
Harald Weltea9caad82020-02-14 14:32:02 +010053 { "server-ip", 1, 0, 'i' },
54 { "server-port", 1, 0, 'p' },
55 { "client-id", 1, 0, 'c' },
56 { "client-slot", 1, 0, 'n' },
Harald Welte0e968cc2020-02-22 18:16:16 +010057 { "atr", 1, 0, 'a' },
58 { "event-script", 1, 0, 'e' },
59#ifdef USB_SUPPORT
60 { "usb-vendor", 1, 0, 'V' },
61 { "usb-product", 1, 0, 'P' },
62 { "usb-config", 1, 0, 'C' },
63 { "usb-interface", 1, 0, 'I' },
64 { "usb-altsetting", 1, 0, 'S' },
65 { "usb-address", 1, 0, 'A' },
66 { "usb-path", 1, 0, 'H' },
67#endif
Harald Weltea9caad82020-02-14 14:32:02 +010068 { 0, 0, 0, 0 }
69 };
70
Harald Welte0e968cc2020-02-22 18:16:16 +010071 c = getopt_long(argc, argv, "hvi:p:c:n:e:"
72#ifdef USB_SUPPORT
73 "V:P:C:I:S:A:H:"
74#endif
75 ,
Harald Weltea9caad82020-02-14 14:32:02 +010076 long_options, &option_index);
77 if (c == -1)
78 break;
79
80 switch (c) {
81 case 'h':
82 printf_help();
83 exit(0);
84 break;
Harald Welte0e968cc2020-02-22 18:16:16 +010085 case 'v':
86 printf("osmo-remsim-client version %s\n", VERSION);
87 exit(0);
88 break;
Harald Weltea9caad82020-02-14 14:32:02 +010089 case 'i':
Harald Welte0e968cc2020-02-22 18:16:16 +010090 osmo_talloc_replace_string(cfg, &cfg->server_host, optarg);
Harald Weltea9caad82020-02-14 14:32:02 +010091 break;
92 case 'p':
Harald Welte0e968cc2020-02-22 18:16:16 +010093 cfg->server_port = atoi(optarg);
Harald Weltea9caad82020-02-14 14:32:02 +010094 break;
95 case 'c':
Harald Welte0e968cc2020-02-22 18:16:16 +010096 cfg->client_id = atoi(optarg);
Harald Weltea9caad82020-02-14 14:32:02 +010097 break;
98 case 'n':
Harald Welte0e968cc2020-02-22 18:16:16 +010099 cfg->client_slot = atoi(optarg);
Harald Weltea9caad82020-02-14 14:32:02 +0100100 break;
Harald Welte0e968cc2020-02-22 18:16:16 +0100101 case 'a':
102 rc = osmo_hexparse(optarg, cfg->atr.data, ARRAY_SIZE(cfg->atr.data));
103 if (rc < 2 || rc > ARRAY_SIZE(cfg->atr.data)) {
104 fprintf(stderr, "ATR malformed\n");
105 exit(2);
106 }
107 break;
108 case 'e':
109 osmo_talloc_replace_string(cfg, &cfg->event_script, optarg);
110 break;
111#ifdef USB_SUPPORT
112 case 'V':
113 cfg->usb.vendor_id = strtol(optarg, NULL, 16);
114 break;
115 case 'P':
116 cfg->usb.product_id = strtol(optarg, NULL, 16);
117 break;
118 case 'C':
119 cfg->usb.config_id = atoi(optarg);
120 break;
121 case 'I':
122 cfg->usb.if_num = atoi(optarg);
123 break;
124 case 'S':
125 cfg->usb.altsetting = atoi(optarg);
126 break;
127 case 'A':
128 cfg->usb.addr = atoi(optarg);
129 break;
130 case 'H':
131 cfg->usb.path = optarg;
132 break;
133#endif
Harald Weltea9caad82020-02-14 14:32:02 +0100134 default:
135 break;
136 }
137 }
138}
139
140int main(int argc, char **argv)
141{
142 struct bankd_client *g_client;
Harald Welte0e968cc2020-02-22 18:16:16 +0100143 struct client_config *cfg;
Harald Weltea9caad82020-02-14 14:32:02 +0100144 char hostname[256];
145
146 gethostname(hostname, sizeof(hostname));
147
148 g_tall_ctx = talloc_named_const(NULL, 0, "global");
149 talloc_asn1_ctx = talloc_named_const(g_tall_ctx, 0, "asn1");
150 msgb_talloc_ctx_init(g_tall_ctx, 0);
151
152 osmo_init_logging2(g_tall_ctx, &log_info);
153
Harald Welte0e968cc2020-02-22 18:16:16 +0100154 cfg = client_config_init(g_tall_ctx);
155 OSMO_ASSERT(cfg);
156 handle_options(cfg, argc, argv);
Harald Weltea9caad82020-02-14 14:32:02 +0100157
Harald Welte0e968cc2020-02-22 18:16:16 +0100158 g_client = remsim_client_create(g_tall_ctx, hostname, "remsim-client",cfg);
Harald Weltea9caad82020-02-14 14:32:02 +0100159
160 osmo_fsm_inst_dispatch(g_client->srv_conn.fi, SRVC_E_ESTABLISH, NULL);
161
162 signal(SIGUSR1, handle_sig_usr1);
163
164 asn_debug = 0;
165
166 client_user_main(g_client);
167}