Commit 12497f73 authored by Federico Vaga's avatar Federico Vaga

tools: optimize code

Signed-off-by: Federico Vaga's avatarFederico Vaga <federico.vaga@cern.ch>
parent 146d18ad
...@@ -56,7 +56,8 @@ void dump(unsigned int ch, struct fmctdc_time *ts, int diff_mode) ...@@ -56,7 +56,8 @@ void dump(unsigned int ch, struct fmctdc_time *ts, int diff_mode)
uint64_t ns; uint64_t ns;
double s, hz; double s, hz;
fprintf(stdout, "channel %d | channel seq %-12u | board seq %-12u\n ts ", fprintf(stdout,
"channel %d | channel seq %-12u | board seq %-12u\n ts ",
ch, ts->seq_id, ts->gseq_id); ch, ts->seq_id, ts->gseq_id);
dump_timestamp(*ts); dump_timestamp(*ts);
fprintf(stdout, "\n"); fprintf(stdout, "\n");
...@@ -65,7 +66,8 @@ void dump(unsigned int ch, struct fmctdc_time *ts, int diff_mode) ...@@ -65,7 +66,8 @@ void dump(unsigned int ch, struct fmctdc_time *ts, int diff_mode)
fmctdc_ts_sub(&ts_tmp, &ts_prev[ch]); fmctdc_ts_sub(&ts_tmp, &ts_prev[ch]);
if (diff_mode) { if (diff_mode) {
fprintf(stdout, " refer to board seq %-12u\n", ts->ref_gseq_id); fprintf(stdout, " refer to board seq %-12u\n",
ts->ref_gseq_id);
return; return;
} }
...@@ -101,19 +103,6 @@ static void help(char *name) ...@@ -101,19 +103,6 @@ static void help(char *name)
FMCTDC_CH_1, FMCTDC_CH_LAST); FMCTDC_CH_1, FMCTDC_CH_LAST);
} }
static void tstamp_flush(struct fmctdc_board *brd, int ch, int flush)
{
int ret;
if (!flush)
return;
ret = fmctdc_flush(brd, ch);
if (ret)
fprintf(stderr,
"fmc-tdc-tstamp: failed to flush channel %d: %s\n",
ch, fmctdc_strerror(errno));
}
static void termination_handler(int signum) static void termination_handler(int signum)
{ {
...@@ -136,6 +125,7 @@ int main(int argc, char **argv) ...@@ -136,6 +125,7 @@ int main(int argc, char **argv)
char opt; char opt;
fd_set rfds; fd_set rfds;
struct sigaction new_action, old_action; struct sigaction new_action, old_action;
int ch_valid[FMCTDC_NUM_CHANNELS] = {0, 1, 2, 3, 4};
/* Set up the structure to specify the new action. */ /* Set up the structure to specify the new action. */
new_action.sa_handler = termination_handler; new_action.sa_handler = termination_handler;
...@@ -159,7 +149,7 @@ int main(int argc, char **argv) ...@@ -159,7 +149,7 @@ int main(int argc, char **argv)
ref[i] = -1; ref[i] = -1;
/* Parse Options */ /* Parse Options */
while ((opt = getopt(argc, argv, "hwns:d:frm:l:")) != -1) { while ((opt = getopt(argc, argv, "hwns:d:frm:l:c:")) != -1) {
switch (opt) { switch (opt) {
case 'h': case 'h':
case '?': case '?':
...@@ -212,6 +202,15 @@ int main(int argc, char **argv) ...@@ -212,6 +202,15 @@ int main(int argc, char **argv)
} }
ref[b] = a; ref[b] = a;
break; break;
case 'c':
if (chan_count >= FMCTDC_NUM_CHANNELS) {
fprintf(stderr,
"%s: too many channels, maximum %d\n",
argv[0], FMCTDC_NUM_CHANNELS);
break;
}
sscanf(optarg, "%i", &ch_valid[chan_count++]);
break;
} }
} }
if (optind >= argc) { if (optind >= argc) {
...@@ -236,48 +235,20 @@ int main(int argc, char **argv) ...@@ -236,48 +235,20 @@ int main(int argc, char **argv)
/* Open Channels from command line */ /* Open Channels from command line */
memset(channels, 0, sizeof(channels)); memset(channels, 0, sizeof(channels));
while (optind < argc) { if (!chan_count)
ch = atoi(argv[optind]); chan_count = FMCTDC_NUM_CHANNELS;
for (i = 0; i < chan_count; i++) {
if (ch < FMCTDC_CH_1 || ch > FMCTDC_CH_LAST) { ch = ch_valid[i];
fprintf(stderr, "%s: invalid channel.\n", argv[0]); if (flush) {
fmctdc_close(brd); ret = fmctdc_flush(brd, ch);
exit(EXIT_FAILURE);
}
channels[ch] = fmctdc_fileno_channel(brd, ch);
tstamp_flush(brd, ch, flush);
/* set buffer mode */
ret = fmctdc_set_buffer_mode(brd, ch, bufmode);
if (ret) {
fprintf(stderr,
"%s: chan %d: cannot set buffer mode: %s. Use default\n",
argv[0], ch, fmctdc_strerror(errno));
}
/* set buffer lenght */
ret = fmctdc_set_buffer_len(brd, ch, buflen);
if (ret) {
fprintf(stderr,
"%s: chan %d: cannot set buffer lenght: %s. Use default\n",
argv[0], ch, fmctdc_strerror(errno));
}
if (!read)
ret = fmctdc_channel_enable(brd, i);
if (ret) if (ret)
fprintf(stderr, fprintf(stderr,
"%s: chan %d: cannot enable acquisition: %s.\n", "fmc-tdc-tstamp: failed to flush channel %d: %s\n",
argv[0], i, fmctdc_strerror(errno)); ch, fmctdc_strerror(errno));
chan_count++;
optind++;
} }
/* If there are not channels, then dump them all */
if (!chan_count) { channels[ch] = fmctdc_fileno_channel(brd, ch);
for (i = 0; i < FMCTDC_NUM_CHANNELS; i++) { ret = fmctdc_reference_set(brd, ch, ref[ch]);
tstamp_flush(brd, ch, flush);
channels[i] =
fmctdc_fileno_channel(brd, i);
ret = fmctdc_reference_set(brd, i, ref[i]);
if (ret) { if (ret) {
fprintf(stderr, fprintf(stderr,
"%s: cannot set reference mode: %s\n", "%s: cannot set reference mode: %s\n",
...@@ -285,39 +256,38 @@ int main(int argc, char **argv) ...@@ -285,39 +256,38 @@ int main(int argc, char **argv)
fprintf(stderr, fprintf(stderr,
"%s: continue in normal mode: %s\n", "%s: continue in normal mode: %s\n",
argv[0], fmctdc_strerror(errno)); argv[0], fmctdc_strerror(errno));
ref[i] = -1; ref[ch] = -1;
} }
/* set buffer mode */ /* set buffer mode */
ret = fmctdc_set_buffer_mode(brd, i, bufmode); ret = fmctdc_set_buffer_mode(brd, ch, bufmode);
if (ret) { if (ret) {
fprintf(stderr, fprintf(stderr,
"%s: chan %d: cannot set buffer mode: %s. Use default\n", "%s: chan %d: cannot set buffer mode: %s. Use default\n",
argv[0], i, fmctdc_strerror(errno)); argv[0], ch, fmctdc_strerror(errno));
} }
/* set buffer lenght */ /* set buffer lenght */
ret = fmctdc_set_buffer_len(brd, i, buflen); ret = fmctdc_set_buffer_len(brd, ch, buflen);
if (ret) { if (ret) {
fprintf(stderr, fprintf(stderr,
"%s: chan %d: cannot set buffer lenght: %s. Use default\n", "%s: chan %d: cannot set buffer lenght: %s. Use default\n",
argv[0], i, fmctdc_strerror(errno)); argv[0], ch, fmctdc_strerror(errno));
} }
if (!read) if (!read)
ret = fmctdc_channel_enable(brd, i); ret = fmctdc_channel_enable(brd, ch);
if (ret) if (ret)
fprintf(stderr, fprintf(stderr,
"%s: chan %d: cannot enable acquisition: %s.\n", "%s: chan %d: cannot enable acquisition: %s.\n",
argv[0], i, fmctdc_strerror(errno)); argv[0], i, fmctdc_strerror(errno));
} }
chan_count = i;
}
/* Read Time-Stamps */ /* Read Time-Stamps */
n = 0; n = 0;
while ((n < n_samples || n_samples <= 0) && (!stop)) { while ((n < n_samples || n_samples <= 0) && (!stop)) {
/* Check for pending signal */ /* Check for pending signal */
nfds = 0; nfds = 0;
...@@ -346,8 +316,8 @@ int main(int argc, char **argv) ...@@ -346,8 +316,8 @@ int main(int argc, char **argv)
} }
/* Now we can read the timestamp */ /* Now we can read the timestamp */
for (i = 0; i <= FMCTDC_CH_LAST; i++) { for (i = 0; i < chan_count; i++) {
fd = channels[i]; fd = channels[ch_valid[i]];
if (fd < 0) if (fd < 0)
continue; continue;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment