nmxptool_getoptlong.c 25.8 KB
Newer Older
Matteo Quintiliani's avatar
Matteo Quintiliani committed
1
2
3
4
5
6
7
8
9
/*! \file
 *
 * \brief Nanometrics Protocol Tool
 *
 * Author:
 * 	Matteo Quintiliani
 * 	Istituto Nazionale di Geofisica e Vulcanologia - Italy
 *	quintiliani@ingv.it
 *
10
 * $Id: nmxptool_getoptlong.c,v 1.61 2008-02-18 09:32:53 mtheo Exp $
Matteo Quintiliani's avatar
Matteo Quintiliani committed
11
12
13
 *
 */

14
15
16
17
18
19
20
21
22
23
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "config.h"
#include "nmxp.h"

#include "nmxptool_getoptlong.h"


24
const NMXPTOOL_PARAMS NMXPTOOL_PARAMS_DEFAULT =
25
26
{
    NULL,
27
28
    DEFAULT_PORT_DAP,
    DEFAULT_PORT_PDS,
29
30
31
    NULL,
    NULL,
    NULL,
32
33
    0.0,
    0.0,
34
    DEFAULT_INTERVAL_NO_VALUE,
35
36
    NULL,
    NULL,
37
38
    DEFAULT_STC,
    DEFAULT_RATE,
39
    NULL,
Matteo Quintiliani's avatar
Matteo Quintiliani committed
40
    DEFAULT_DELAY,
Matteo Quintiliani's avatar
Matteo Quintiliani committed
41
42
    DEFAULT_MAX_TOLERABLE_LATENCY,
    DEFAULT_TIMEOUTRECV,
43
    DEFAULT_VERBOSE_LEVEL,
44
    NULL,
45
    NULL,
46
    DEFAULT_BUFFERED_TIME,
47
    DEFAULT_MAX_TIME_TO_RETRIEVE,
48
    0,
49
50
51
52
    0,
    0,
    0,
    0,
53
    0,
54
    0,
55
56
57
    0
};

58
59

void nmxptool_author_support() {
60
    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "\
61
62
63
64
65
66
67
68
Matteo Quintiliani - Istituto Nazionale di Geofisica e Vulcanologia - Italy\n\
Mail bug reports and suggestions to <%s>.\n",
	    PACKAGE_BUGREPORT
	    );
}


void nmxptool_version() {
69
    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "\
70
%s %s%s, Nanometrics tool based on %s\n\
71
        (Data Access Protocol 1.0, Private Data Stream 1.4)\n",
72
73
	PACKAGE_NAME, PACKAGE_VERSION, PACKAGE_BUILD,
	nmxp_log_version()	
74
	);
Matteo Quintiliani's avatar
Matteo Quintiliani committed
75
76

    nmxptool_supports();
77
78
}

Matteo Quintiliani's avatar
Matteo Quintiliani committed
79
void nmxptool_supports() {
80
    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "\
Matteo Quintiliani's avatar
Matteo Quintiliani committed
81
82
         Support for: libmseed ");
#ifdef HAVE_LIBMSEED
83
    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "YES");
Matteo Quintiliani's avatar
Matteo Quintiliani committed
84
#else
85
    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "NO");
Matteo Quintiliani's avatar
Matteo Quintiliani committed
86
87
#endif

88
    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, ", SeedLink ");
Matteo Quintiliani's avatar
Matteo Quintiliani committed
89
#ifdef HAVE___SRC_SEEDLINK_PLUGIN_C
90
    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "YES");
Matteo Quintiliani's avatar
Matteo Quintiliani committed
91
#else
92
    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "NO");
Matteo Quintiliani's avatar
Matteo Quintiliani committed
93
94
#endif

95
    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, ", Earthworm ");
Matteo Quintiliani's avatar
Matteo Quintiliani committed
96
#ifdef HAVE_EARTHWORMOBJS
97
    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "YES");
Matteo Quintiliani's avatar
Matteo Quintiliani committed
98
#else
99
    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "NO");
Matteo Quintiliani's avatar
Matteo Quintiliani committed
100
#endif
101
    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, ".\n");
Matteo Quintiliani's avatar
Matteo Quintiliani committed
102
103
}

104

105
106
void nmxptool_usage(struct option long_options[])
{
107
    nmxptool_version();
Matteo Quintiliani's avatar
Matteo Quintiliani committed
108

109
    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "\
110
\n\
111
Usage: %s -H hostname   -l | -L\n\
112
             Print list of available channels on DataServer or NaqsServer.\n\
113
114
\n\
       %s -H hostname -C channellist [...]\n\
115
             Receive data in near real-time from NaqsServer by PDS.\n\
116
117
\n\
       %s -H hostname -F statefile [...]\n\
118
             Receive data from NaqsServer, and from DataServer if is necessary.\n\
119
120
\n\
       %s -H hostname -C channellist -s DATE -e DATE [...]\n\
Matteo Quintiliani's avatar
Matteo Quintiliani committed
121
       %s -H hostname -C channellist -s DATE -t SECs [...]\n\
Matteo Quintiliani's avatar
Matteo Quintiliani committed
122
             Receive data from DataServer by DAP.\n\
123
\n", PACKAGE_NAME, PACKAGE_NAME, PACKAGE_NAME, PACKAGE_NAME, PACKAGE_NAME);
Matteo Quintiliani's avatar
Matteo Quintiliani committed
124
125

#ifdef HAVE_EARTHWORMOBJS
126
    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "\
Matteo Quintiliani's avatar
Matteo Quintiliani committed
127
       %s nmxptool.d\n\
128
129
130
131
132
133
134
135
             Launched as Earthworm module to redirect data into the EW-Rings.\n\
\n", PACKAGE_NAME);
#endif

#ifdef HAVE___SRC_SEEDLINK_PLUGIN_C
    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "\
       %s <option ... option> -k\n\
             Launched as SeedLink plug-in to feed the SL-Server.\n\
Matteo Quintiliani's avatar
Matteo Quintiliani committed
136
137
138
\n", PACKAGE_NAME);
#endif

139
    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "\
140
141
142
143
144
       %s --help\n\
             Print this help.\n\
\n", PACKAGE_NAME);

    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "\
145
Arguments:\n\
146
147
  -H, --hostname=HOST     NaqsServer or DataServer hostname.\n\
  -C, --channels=LIST     List of NET.STA.CHAN separated by comma.\n\
148
                          NET  is optional and used only for output.\n\
149
150
                          STA  can be '*', it stands for all stations.\n\
                          CHAN can contain '?', it stands for any character.\n\
151
                          Example:  *.HH?,N1.STA2.??Z,STA3.?H?\n\
152
  -F, --statefile=FILE    Load/Save time of last sample of each channel.\n\
153
154
155
156
157
                          Allow data continuity between program restarts.\n\
                          Related to -A and it enables -b.\n\
                          DO NOT USE with -C.\n");

    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "\
Matteo Quintiliani's avatar
Matteo Quintiliani committed
158
  -A, --maxdataretr=SECs  Max amount of data of the past to retrieve from the\n\
Matteo Quintiliani's avatar
Matteo Quintiliani committed
159
                          DataServer when program restarts (default %d) [%d..%d].\n\
160
                          0 to disable connection to DataServer.\n\
161
162
163
164
165
166
167
168
\n",
	    DEFAULT_MAX_TIME_TO_RETRIEVE,
	    DEFAULT_MAX_TIME_TO_RETRIEVE_MINIMUM,
	    DEFAULT_MAX_TIME_TO_RETRIEVE_MAXIMUM
);

    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "\
PDS arguments:\n\
169
170
171
172
173
174
175
176
177
  -S, --stc=SECs          Short-Term-Completion (default %d).\n\
                          -1 is for Raw Stream, no Short-Term-Completion.\n\
                             Packet are compressed. Related to -M, -T.\n\
                             It enables --rate=-1.\n\
                           0 decompressed packets are received in chronological\n\
                             order without waiting for missing data.\n\
                          [1..300] decompressed packets are received in\n\
                             chronological order but waiting for missing data\n\
                             at most SECs seconds.\n\
178
179
180
181
182
183
184
185
186
187
  -R, --rate=Hz           Receive data with specified sample rate (default %d).\n\
                          -1 for original sample rate and compressed data.\n\
                           0 for original sample rate and decompressed data.\n\
                          >0 for specified sample rate and decompressed data.\n\
  -b, --buffered          Request also recent packets into the past.\n\
  -B, --buff_date=DATE    Request also recent packets into the past\n\
                          but consider only samples after DATE.\n\
  -L, --listchannelsnaqs  Print list of available channels on NaqsServer.\n\
  -M, --maxlatency=SECs   Max tolerable latency (default %d) [%d..%d].\n\
  -T, --timeoutrecv=SECs  Time-out for flushing buffered packets.\n\
188
                          (default %d, no time-out) [%d..%d].\n\
189
                          -T is useful for retrieving Data On Demand.\n\
190
                          -M, -T are usable only with Raw Stream, -S=-1.\n\
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
                          In general, -M and -T are not used together.\n\
\n\
",
	    DEFAULT_STC,
	    DEFAULT_RATE,
	    DEFAULT_MAX_TOLERABLE_LATENCY,
	    DEFAULT_MAX_TOLERABLE_LATENCY_MINIMUM,
	    DEFAULT_MAX_TOLERABLE_LATENCY_MAXIMUM,
	    DEFAULT_TIMEOUTRECV,
	    DEFAULT_TIMEOUTRECV_MINIMUM,
	    DEFAULT_TIMEOUTRECV_MAXIMUM
	  );

    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "\
DAP Arguments:\n\
  -s, --start_time=DATE   Start time in date format.\n\
  -e, --end_time=DATE     End time in date format.\n\
                          DATE can be in formats:\n\
                              <date>,<time> | <date>\n\
                          where:\n\
                              <date> = yyyy/mm/dd | yyy.jjj\n\
                              <time> = hh:mm:ss | hh:mm:ss.dddd | hh:mm\n\
213
  -t, --interval=SECs     Time interval from start_time (greater than zero).\n\
214
215
216
217
218
219
220
221
222
  -d, --delay=SECs        Receive continuosly data with delay [%d..%d].\n\
  -u, --username=USER     DataServer username.\n\
  -p, --password=PASS     DataServer password.\n\
  -l, --listchannels      Print list of available channels on DataServer.\n\
  -i, --channelinfo       Print list of available channels and channelinfo.\n\
\n\
",
DEFAULT_DELAY_MINIMUM,
DEFAULT_DELAY_MAXIMUM);
223

224
    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "\
Matteo Quintiliani's avatar
Matteo Quintiliani committed
225
Other arguments:\n\
226
227
  -P, --portpds=PORT      NaqsServer port number (default %d).\n\
  -D, --portdap=PORT      DataServer port number (default %d).\n\
228
229
  -N, --network=NET       Default output Network code. (default '%s').\n\
  -L, --location=LOC      Default output Location code. DISABLED!\n\
230
231
232
233
234
  -v, --verbose=level     Be verbose. level is a bitmap:\n\
                          %d Packet, %d Channel, %d Raw Stream,\n\
                          %d CRC32, %d Connection flow,\n\
                          %d Packet Management, %d Extra, %d Date,\n\
                          %d Gap, %d DOD, %d All messages.\n\
Matteo Quintiliani's avatar
Matteo Quintiliani committed
235
  -g, --logdata           Print info about data.\n\
Matteo Quintiliani's avatar
Matteo Quintiliani committed
236
",
237
238
	    DEFAULT_PORT_PDS,
	    DEFAULT_PORT_DAP,
239
240
241
242
243
244
245
246
247
248
249
250
	    DEFAULT_NETWORK,
	    NMXP_LOG_D_PACKET,
	    NMXP_LOG_D_CHANNEL,
	    NMXP_LOG_D_RAWSTREAM,
	    NMXP_LOG_D_CRC,
	    NMXP_LOG_D_CONNFLOW,
	    NMXP_LOG_D_PACKETMAN,
	    NMXP_LOG_D_EXTRA,
	    NMXP_LOG_D_DATE,
	    NMXP_LOG_D_GAP,
	    NMXP_LOG_D_DOD,
	    NMXP_LOG_D_ANY
251
252
253
		);

#ifdef HAVE_LIBMSEED
254
    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "\
Matteo Quintiliani's avatar
Matteo Quintiliani committed
255
256
  -m, --writeseed         Pack received data in Mini-SEED records\n\
                          and write to a file.\n");
257
258
#endif

259
    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "\
Matteo Quintiliani's avatar
Matteo Quintiliani committed
260
  -w, --writefile         Dump received data to a file.\n");
261

262
#ifdef HAVE___SRC_SEEDLINK_PLUGIN_C
263
    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "\
Matteo Quintiliani's avatar
Matteo Quintiliani committed
264
  -k, --slink=plug_name   Send received data to SeedLink as a plug-in.\n\
Matteo Quintiliani's avatar
Matteo Quintiliani committed
265
                          plug_name is set by SeisComP daemon.\n\
Matteo Quintiliani's avatar
Matteo Quintiliani committed
266
267
                          INTO THE FILE seedlink.in, THIS OPTION MUST BE\n\
                          THE LAST WITHOUT ADDING VALUE FOR plug_name!\n");
268
#endif
269

270
    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "\
271
  -V, --version           Print tool version.\n\
272
  -h, --help              Print this help.\n\
Matteo Quintiliani's avatar
Matteo Quintiliani committed
273
274
\n");

275
    nmxptool_author_support();
276
277
278
279
280

    /*
    if(long_options) {
	int i=0;
	while(long_options[i].name) {
281
	    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_EXTRA, "%s %d %d %d %c\n", long_options[i].name, long_options[i].has_arg, (long_options[i].flag)? *(long_options[i].flag) : 0, long_options[i].val, long_options[i].val);
282
283
284
285
286
287
288
	    i++;
	}
    }
    */
}


289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#define MAXSIZE_LINE_CHAN_STATE 2048
#define MAXSIZECHANNELSTRINGARGUMENT 8000
#define MAXSIZE_CHANNEL_STRING 64

char *get_channel_list_argument_from_state_file(const char *filename) {
    char *ret_channel_string = NULL;
    char line[MAXSIZE_LINE_CHAN_STATE];
    char str_chan[MAXSIZE_CHANNEL_STRING];
    int k;
    FILE *fstatefile = NULL;

    fstatefile = fopen(filename, "r");

    /* Read only channel names from state file */
    if(fstatefile) {
	ret_channel_string = (char *) malloc (MAXSIZECHANNELSTRINGARGUMENT);
	ret_channel_string[0] = 0;
	while(fgets(line, MAXSIZE_LINE_CHAN_STATE, fstatefile) != NULL) {
	    k = 0;
	    while(line[k] != 0
		    &&  line[k] != ' '
		    &&  line[k] != 10
		    &&  line[k] != 13
		    &&  k < MAXSIZE_CHANNEL_STRING) {
		str_chan[k] = line[k];
		k++;
	    }
	    str_chan[k] = 0;
	    if(ret_channel_string[0] == 0) {
		strncpy(ret_channel_string, str_chan, MAXSIZECHANNELSTRINGARGUMENT);
	    } else {
		strncat(ret_channel_string, ",", MAXSIZECHANNELSTRINGARGUMENT);
		strncat(ret_channel_string, str_chan, MAXSIZECHANNELSTRINGARGUMENT);
	    }
	}
	fclose(fstatefile);
    }
    return ret_channel_string;
}

329
int nmxptool_getopt_long(int argc, char **argv, NMXPTOOL_PARAMS *params)
330
{
331
332
    int ret_errors = 0;

333
    NMXP_TM_T tmp_tmt;
334
335
336
337
    int i;
    char one_time_option[255];
    int c;

338
339
340
341
342
343
344
345
    struct option long_options[] =
    {
	/* These options set a flag. */
	/* It is not safe use reference to params in this way */
	/* {"verbose",        no_argument,       &(params->flag_verbose), 1}, */
	/* {"quiet",          no_argument,       &(params->flag_verbose), 0}, */
	/* These options don't set a flag.
	 *                   We distinguish them by their indices. */
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
	{"hostname",     required_argument, NULL, 'H'},
	{"portpds",      required_argument, NULL, 'P'},
	{"portdap",      required_argument, NULL, 'D'},
	{"channels",     required_argument, NULL, 'C'},
	{"network",      required_argument, NULL, 'N'},
	{"location",     required_argument, NULL, 'n'},
	{"stc",          required_argument, NULL, 'S'},
	{"rate",         required_argument, NULL, 'R'},
	{"start_time",   required_argument, NULL, 's'},
	{"end_time",     required_argument, NULL, 'e'},
	{"interval",     required_argument, NULL, 't'},
	{"delay",        required_argument, NULL, 'd'},
	{"username",     required_argument, NULL, 'u'},
	{"password",     required_argument, NULL, 'p'},
	{"maxlatency",   required_argument, NULL, 'M'},
	{"timeoutrecv",  required_argument, NULL, 'T'},
	{"verbose",      required_argument, NULL, 'v'},
	{"bufferedt",    required_argument, NULL, 'B'},
364
	{"maxdataretr",  required_argument, NULL, 'A'},
365
	/* Following are flags */
366
367
368
369
370
	{"logdata",      no_argument,       NULL, 'g'},
	{"buffered",     no_argument,       NULL, 'b'},
	{"listchannels", no_argument,       NULL, 'l'},
	{"listchannelsnaqs", no_argument,   NULL, 'L'},
	{"channelinfo",  no_argument,       NULL, 'i'},
371
#ifdef HAVE_LIBMSEED
372
	{"writeseed",    no_argument,       NULL, 'm'},
373
#endif
374
	{"writefile",    no_argument,       NULL, 'w'},
375
#ifdef HAVE___SRC_SEEDLINK_PLUGIN_C
376
	{"slink",        required_argument, NULL, 'k'},
377
#endif
378
379
380
	{"statefile",    required_argument, NULL, 'F'},
	{"help",         no_argument,       NULL, 'h'},
	{"version",      no_argument,       NULL, 'V'},
381
382
383
	{0, 0, 0, 0}
    };

384
    char optstr[300] = "H:P:D:C:N:n:S:R:s:e:t:d:u:p:M:T:v:B:A:F:gblLiwhV";
385

386
387
388
#ifdef HAVE_LIBMSEED
    strcat(optstr, "m");
#endif
389

390
391
392
#ifdef HAVE___SRC_SEEDLINK_PLUGIN_C
    strcat(optstr, "k:");
#endif
393
394
395
396
397
398
399
400
401
402
403
404


    /* getopt_long stores the option index here. */
    int option_index = 0;

    /* init array for checking one time option */
    for(i=0; i<255; i++) {
	one_time_option[i] = 0;
    }


    /* init params */
405
    memcpy(params, &NMXPTOOL_PARAMS_DEFAULT, sizeof(NMXPTOOL_PARAMS_DEFAULT));
406

407
408
409
410
411
412
413
414
415
416
417
418
419
420
    /* Check number of command line arguments for earthworm */
    if (argc == 2)
    {
	int l = strlen(argv[1]);
	if(l >= 3) {
	    if(argv[1][0] != '-') {
		if(argv[1][l-2] == '.'  &&  argv[1][l-1] == 'd') {
		    params->ew_configuration_file = argv[1];
		    return 0;
		}
	    }
	}
    }

421
422
423
424
425
426
427
    while ( (c = getopt_long (argc, argv, optstr, long_options, &option_index)) != -1) {

	/* BE CAREFUL if use synonym options !!! */
	one_time_option[c]++;

	if(one_time_option[c] > 1) {
	    ret_errors++;
428
	    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "Replicated option -%c (value %s)\n", c, optarg);
429
430
431
432
433
434
435
	} else {
	    switch (c)
	    {
		case 0:
		    /* If this option set a flag, do nothing else now. */
		    if (long_options[option_index].flag != 0)
			break;
436
		    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "option %s", long_options[option_index].name);
437
		    if (optarg)
438
439
			nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, " with arg %s", optarg);
		    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "\n");
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
		    break;

		case 'H':
		    params->hostname = optarg;
		    break;

		case 'P':
		    params->portnumberpds = atoi(optarg);
		    break;

		case 'D':
		    params->portnumberdap = atoi(optarg);
		    break;

		case 'C':
455
456
457
458
459
460
		    if(params->channels) {
			nmxp_log(NMXP_LOG_ERR, NMXP_LOG_D_ANY, "Channels have been already defined by State File (option -F)!\n");
			ret_errors++;
		    } else {
			params->channels = optarg;
		    }
461
462
463
464
465
466
		    break;

		case 'N':
		    params->network = optarg;
		    break;

467
		case 'n':
468
469
470
471
472
		    if(1) {
			nmxp_log(NMXP_LOG_WARN, NMXP_LOG_D_ANY, "Location is currently disabled!\n");
		    } else {
			params->location = optarg;
		    }
473
474
		    break;

475
476
		case 'S':
		    params->stc = atoi(optarg);
477
		    nmxp_log(NMXP_LOG_NORM, NMXP_LOG_D_ANY, "Short-Term-Completion %d.\n", params->stc);
478
479
480
481
482
483
		    break;

		case 'R':
		    params->rate = atoi(optarg);
		    break;

484
		case 's':
485
		    if(nmxp_data_parse_date(optarg, &tmp_tmt) == -1) {
486
			// MESSAGE ERROR
Matteo Quintiliani's avatar
Matteo Quintiliani committed
487
			ret_errors++;
488
		    } else {
489
			params->start_time = nmxp_data_tm_to_time(&tmp_tmt);
490
491
492
493
		    }
		    break;

		case 'e':
494
		    if(nmxp_data_parse_date(optarg, &tmp_tmt) == -1) {
495
			// MESSAGE ERROR
Matteo Quintiliani's avatar
Matteo Quintiliani committed
496
			ret_errors++;
497
		    } else {
498
			params->end_time = nmxp_data_tm_to_time(&tmp_tmt);
499
500
501
		    }
		    break;

Matteo Quintiliani's avatar
Matteo Quintiliani committed
502
503
504
505
		case 't':
		    params->interval = atoi(optarg);
		    break;

Matteo Quintiliani's avatar
Matteo Quintiliani committed
506
507
508
509
		case 'd':
		    params->delay = atoi(optarg);
		    break;

510
511
512
513
514
515
516
517
		case 'u':
		    params->datas_username = optarg;
		    break;

		case 'p':
		    params->datas_password = optarg;
		    break;

518
		case 'M':
Matteo Quintiliani's avatar
Matteo Quintiliani committed
519
		    params->max_tolerable_latency = atoi(optarg);
520
		    nmxp_log(NMXP_LOG_NORM, NMXP_LOG_D_ANY, "Max_tolerable_latency %d\n", params->max_tolerable_latency);
Matteo Quintiliani's avatar
Matteo Quintiliani committed
521
522
		    break;

Matteo Quintiliani's avatar
Matteo Quintiliani committed
523
		case 'T':
524
525
		    params->timeoutrecv = atoi(optarg);
		    nmxp_log(NMXP_LOG_NORM, NMXP_LOG_D_ANY, "Time-out receiving %d\n", params->timeoutrecv);
Matteo Quintiliani's avatar
Matteo Quintiliani committed
526
527
		    break;

528
529
530
531
		case 'v':
		    params->verbose_level = atoi(optarg);
		    break;

532
533
534
535
536
537
538
539
540
541
		case 'B':
		    params->flag_buffered = 1;
		    if(nmxp_data_parse_date(optarg, &tmp_tmt) == -1) {
			// MESSAGE ERROR
			ret_errors++;
		    } else {
			params->buffered_time = nmxp_data_tm_to_time(&tmp_tmt);
		    }
		    break;

542
543
		case 'A':
		    if(optarg) {
544
			params->max_data_to_retrieve = atoi(optarg);
545
		    }
546
		    nmxp_log(NMXP_LOG_NORM, NMXP_LOG_D_ANY, "Max_time_to_retrieve %d\n", params->max_data_to_retrieve);
547
		    break;
548

549
550
551
552
553
554
555
#ifdef HAVE___SRC_SEEDLINK_PLUGIN_C
		case 'k':
		    params->flag_slink = 1;
		    params->plugin_slink = optarg;
		    break;
#endif

556
		case 'F':
557
		    params->flag_buffered = 1;
558
		    params->statefile = optarg;
559
560
561
562
563
564
565
566
		    if(params->channels == NULL) {
			params->channels = get_channel_list_argument_from_state_file(params->statefile);
			if(params->channels) {
			    /* Do nothing */
			} else {
			    ret_errors++;
			    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "State file %s not found or unable to read!\n", params->statefile);
			}
567
568
		    } else {
			ret_errors++;
569
			nmxp_log(NMXP_LOG_ERR, NMXP_LOG_D_ANY, "Channels have been already defined by option -C!\n");
570
571
572
		    }
		    break;

Matteo Quintiliani's avatar
Matteo Quintiliani committed
573
		case 'g':
574
575
576
		    params->flag_logdata = 1;
		    break;

577
		case 'b':
578
		    params->flag_buffered = 1;
579
580
		    break;

581
582
583
584
		case 'l':
		    params->flag_listchannels = 1;
		    break;

585
586
587
588
		case 'L':
		    params->flag_listchannelsnaqs = 1;
		    break;

589
590
591
592
		case 'i':
		    params->flag_request_channelinfo = 1;
		    break;

593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
#ifdef HAVE_LIBMSEED
		case 'm':
		    params->flag_writeseed = 1;
		    break;
#endif

		case 'w':
		    params->flag_writefile = 1;
		    break;

		case 'h':
		    nmxptool_usage(long_options);
		    exit (1);
		    break;

608
609
		case 'V':
		    nmxptool_version();
Matteo Quintiliani's avatar
Matteo Quintiliani committed
610
		    nmxptool_author_support();
611
612
613
		    exit (1);
		    break;

614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
		case '?':
		    /* getopt_long already printed an error message. */
		    ret_errors++;
		    break;

		default:
		    nmxptool_usage(long_options);
		    exit (1);
	    }
	}
    }

    /* Print any remaining command line arguments (not options). */
    if (optind < argc)
    {
	ret_errors += optind;

631
	nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "non-option ARGV-elements: ");
632
	while (optind < argc)
633
	    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "%s ", argv[optind++]);
634
635
636
637
638
639
640
	putchar ('\n');
    }

    return ret_errors;
}


641
642
#define RETURN_STR(s) (s == NULL)? "<null>" : s

643
644
645
646
647
void nmxptool_log_params(NMXPTOOL_PARAMS *params) {
    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_EXTRA, "\
    char *hostname: %s\n\
    int portnumberdap: %d\n\
    int portnumberpds: %d\n\
648
649
650
651
652
653
654
",
    RETURN_STR(params->hostname),
    params->portnumberdap,
    params->portnumberpds
);

    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_EXTRA, "\
655
    char *channels: %s\n\
656
657
658
659
660
",
    RETURN_STR(params->channels)
);

    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_EXTRA, "\
661
662
663
664
665
    char *network: %s\n\
    char *location: %s\n\
    double start_time: %f\n\
    double end_time: %f\n\
    int32_t interval: %d\n\
666
667
668
669
670
671
672
673
674
675
",
    RETURN_STR(params->network),
    RETURN_STR(params->location),
    params->start_time,
    params->end_time,
    params->interval
);


    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_EXTRA, "\
676
677
    char *datas_username: %s\n\
    char *datas_password: %s\n\
678
679
680
681
682
683
",
    RETURN_STR(params->datas_username),
    RETURN_STR(params->datas_password)
);

    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_EXTRA, "\
684
685
686
687
688
689
690
    int32_t stc: %d\n\
    int32_t rate: %d\n\
    char *plugin_slink: %s\n\
    int32_t delay: %d\n\
    int32_t max_tolerable_latency: %d\n\
    int32_t timeoutrecv: %d\n\
    int32_t verbose_level: %d\n\
691
692
693
694
695
696
697
698
699
700
701
",
    params->stc,
    params->rate,
    RETURN_STR(params->plugin_slink),
    params->delay,
    params->max_tolerable_latency,
    params->timeoutrecv,
    params->verbose_level
);

    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_EXTRA, "\
702
703
    char *ew_configuration_file: %s\n\
    char *statefile: %s\n\
Matteo Quintiliani's avatar
Matteo Quintiliani committed
704
    int32_t max_data_to_retrieve: %d\n\
705
706
",
    RETURN_STR(params->ew_configuration_file),
Matteo Quintiliani's avatar
Matteo Quintiliani committed
707
708
    RETURN_STR(params->statefile),
    params->max_data_to_retrieve
709
710
711
);

    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_EXTRA, "\
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
    double buffered_time: %f\n\
    int flag_writeseed: %d\n\
    int flag_listchannels: %d\n\
    int flag_listchannelsnaqs: %d\n\
    int flag_request_channelinfo: %d\n\
    int flag_writefile: %d\n\
    int flag_slink: %d\n\
    int flag_buffered: %d\n\
    int flag_logdata: %d\n\
",
    params->buffered_time,
    params->flag_writeseed,
    params->flag_listchannels,
    params->flag_listchannelsnaqs,
    params->flag_request_channelinfo,
    params->flag_writefile,
    params->flag_slink,
    params->flag_buffered,
    params->flag_logdata
    );
}

734
735


736
int nmxptool_check_params(NMXPTOOL_PARAMS *params) {
737
738
    int ret = 0;

739
740
741
    if(params->ew_configuration_file != NULL) {
	/* Do nothing */
    } else if(params->hostname == NULL) {
742
	ret = -1;
743
	nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "<hostname> is required!\n");
744
    } else if(params->flag_listchannels) {
745
746
747
748
749
	if(params->flag_listchannelsnaqs) {
	    ret = -1;
	    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "-l and -L can not be used together!\n");
	}
    } else if(params->flag_listchannelsnaqs) {
750
751
752
	/* Do nothing */
    } else if(params->hostname == NULL) {
	ret = -1;
753
	nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "<hostname> is required!\n");
754
755
    } else if(params->channels == NULL) {
	ret = -1;
Matteo Quintiliani's avatar
Matteo Quintiliani committed
756
	nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "Channel list is required!\n");
757
    } else if(params->start_time == 0.0 &&  params->end_time != 0.0) {
Matteo Quintiliani's avatar
Matteo Quintiliani committed
758
	ret = -1;
Matteo Quintiliani's avatar
Matteo Quintiliani committed
759
	nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "<end_time> is required when declaring <start_time>!\n");
760
    } else if(params->start_time != 0.0  &&  params->end_time != 0.0  && params->interval != DEFAULT_INTERVAL_NO_VALUE) {
Matteo Quintiliani's avatar
Matteo Quintiliani committed
761
	ret = -1;
762
	nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "<start_time> has to be used with either <end_time> or <interval>!\n");
763
764
765
    } else if(params->interval != DEFAULT_INTERVAL_NO_VALUE && params->interval <= 0) {
	ret = -1;
	nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "<interval> has to be greater than zero!\n");
766
    } else if(params->start_time != 0.0   &&   params->end_time != 0.0
767
	    && params->start_time >= params->end_time) {
768
	ret = -1;
769
	nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "<start_time> is less than <end_time>!\n");
Matteo Quintiliani's avatar
Matteo Quintiliani committed
770
    } else if(params->stc < DEFAULT_STC_MINIMUM   ||   params->stc > DEFAULT_STC_MAXIMUM) {
771
	ret = -1;
772
	nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "<stc> has to be in the interval [%d..%d] secs.\n",
Matteo Quintiliani's avatar
Matteo Quintiliani committed
773
		DEFAULT_STC_MINIMUM, DEFAULT_STC_MAXIMUM);
774
775
    } else if(params->stc == -1   &&   params->rate != DEFAULT_RATE) {
	ret = -1;
776
	nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "<rate> has to be equal to -1 when <stc> is equal to -1 (Raw Stream).\n");
777
    } else if(params->delay > 0 && params->start_time != 0.0   &&   params->end_time != 0.0) {
778
	ret = -1;
779
	nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "<delay> can not be used with options <start_time> and <end_time>.\n");
Matteo Quintiliani's avatar
Matteo Quintiliani committed
780
781
782
    } else if( params->delay != DEFAULT_DELAY &&
	    (params->delay < DEFAULT_DELAY_MINIMUM  || params->delay > DEFAULT_DELAY_MAXIMUM) ) {
	ret = -1;
783
	nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "<delay> has to be in the interval [%d..%d] secs.\n",
Matteo Quintiliani's avatar
Matteo Quintiliani committed
784
		DEFAULT_DELAY_MINIMUM, DEFAULT_DELAY_MAXIMUM);
785
786
787
788
    } else if(params->verbose_level < DEFAULT_VERBOSE_LEVEL_MINIMUM  ||  params->verbose_level > DEFAULT_VERBOSE_LEVEL_MAXIMUM) {
	ret = -1;
	nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "<verbose_level> has to be in the interval [%d..%d].\n",
		DEFAULT_VERBOSE_LEVEL_MINIMUM, DEFAULT_VERBOSE_LEVEL_MAXIMUM);
Matteo Quintiliani's avatar
Matteo Quintiliani committed
789
790
    } else if(params->rate < DEFAULT_RATE_MINIMUM  ||  params->rate > DEFAULT_RATE_MAXIMUM) {
	ret = -1;
791
	nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "<rate> has to be in the interval [%d..%d].\n",
Matteo Quintiliani's avatar
Matteo Quintiliani committed
792
		DEFAULT_RATE_MINIMUM, DEFAULT_RATE_MAXIMUM);
793
    } else if(params->rate != -1 && params->start_time != 0.0   &&   params->end_time != 0.0) {
794
	ret = -1;
795
	nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "<rate> can not be used with options <start_time> and <end_time>.\n");
796
    } else if(params->flag_buffered != 0 && params->start_time != 0.0   &&   params->end_time != 0.0) {
797
	ret = -1;
798
	nmxp_log(NMXP_LOG_ERR, NMXP_LOG_D_ANY, "<buffered> can not be used with options <start_time> and <end_time>.\n");
799
800
    } else if( (params->max_data_to_retrieve < DEFAULT_MAX_TIME_TO_RETRIEVE_MINIMUM  ||
		params->max_data_to_retrieve > DEFAULT_MAX_TIME_TO_RETRIEVE_MAXIMUM)) {
801
	ret = -1;
802
	nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "<maxdataretr> has to be within [%d..%d].\n",
803
804
		DEFAULT_MAX_TIME_TO_RETRIEVE_MINIMUM,
		DEFAULT_MAX_TIME_TO_RETRIEVE_MAXIMUM);
805
    } else if( params->stc == -1
Matteo Quintiliani's avatar
Matteo Quintiliani committed
806
807
	    && (params->max_tolerable_latency < DEFAULT_MAX_TOLERABLE_LATENCY_MINIMUM  ||
		params->max_tolerable_latency > DEFAULT_MAX_TOLERABLE_LATENCY_MAXIMUM)) {
Matteo Quintiliani's avatar
Matteo Quintiliani committed
808
	ret = -1;
809
	nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "<maxlatency> has to be within [%d..%d].\n",
Matteo Quintiliani's avatar
Matteo Quintiliani committed
810
811
812
813
814
815
816
		DEFAULT_MAX_TOLERABLE_LATENCY_MINIMUM,
		DEFAULT_MAX_TOLERABLE_LATENCY_MAXIMUM);
    } else if( params->stc == -1
	    && (params->timeoutrecv < DEFAULT_TIMEOUTRECV_MINIMUM  ||
		params->timeoutrecv > DEFAULT_TIMEOUTRECV_MAXIMUM)) {
	if(params->timeoutrecv != 0) {
	    ret = -1;
817
	    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "<timeoutrecv> has to be within [%d..%d] or equal to zero for not time-out.\n",
Matteo Quintiliani's avatar
Matteo Quintiliani committed
818
819
820
821
		    DEFAULT_TIMEOUTRECV_MINIMUM,
		    DEFAULT_TIMEOUTRECV_MAXIMUM);
	}
    } else if( params->stc != -1 && params->max_tolerable_latency > 0 ){
822
	nmxp_log(NMXP_LOG_WARN, NMXP_LOG_D_ANY, "<maxlatency> ignored since not defined --stc=-1.\n");
Matteo Quintiliani's avatar
Matteo Quintiliani committed
823
824
    } else if(params->stc != -1 && params->timeoutrecv > 0) {
	params->timeoutrecv = 0;
825
	nmxp_log(NMXP_LOG_WARN, NMXP_LOG_D_ANY, "<timeoutrecv> ignored since not defined --stc=-1.\n");
826
    }
827

Matteo Quintiliani's avatar
Matteo Quintiliani committed
828
    /*
829
    if( params->stc == -1 ) {
830
	nmxp_log(NMXP_LOG_WARN, NMXP_LOG_D_ANY, "<maxlatency> is equal to %d sec.\n", params->max_tolerable_latency);
831
    }
Matteo Quintiliani's avatar
Matteo Quintiliani committed
832
    */
833

834
835
    return ret;
}