Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* SPDX-License-Identifier: LGPL-3.0-or-later
* Copyright (C) 2020 CERN (www.cern.ch)
* Author: Federico Vaga <federico.vaga@cern.ch>
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <libgen.h>
#include <getopt.h>
#include <limits.h>
#include <errno.h>
#include <fmc/core.h>
static const char git_version[] = "git version: " GIT_VERSION;
static char *name;
static void help(void)
{
fprintf(stderr, "%s: Lists FMC carriers and Mezzanines\n"
"\t-V print version\n"
"\t-h print help\n"
"\t-c carrier number\n"
"\t-s slot number\n"
"\t-t EEPROM type to set\n",
name);
}
static void print_version(void)
{
printf("%s version %s\nFMC library version: %s\n",
name, git_version, fmc_version_get());
}
int main(int argc, char *argv[])
{
struct fmc_tkn *fmc;
unsigned int carr_n = FMC_ID_INVALID;
unsigned int slot_n = FMC_ID_INVALID;
char *eeprom_type = NULL;
char eeprom_type_rb[16];
int err = 0;
char opt;
name = strndup(basename(argv[0]), 64);
if (!name) {
err = -1;
goto out_strdup;
}
while ((opt = getopt(argc, argv, "h?Vc:s:t:")) != -1) {
switch (opt) {
case 'h':
case '?':
help();
exit(EXIT_SUCCESS);
break;
case 'V':
print_version();
exit(EXIT_SUCCESS);
case 'c':
carr_n = strtoul(optarg, NULL, 10);
if (errno == ERANGE || errno == EINVAL)
goto out_opt;
break;
case 's':
slot_n = strtoul(optarg, NULL, 10);
if (errno == ERANGE || errno == EINVAL)
goto out_opt;
break;
case 't':
eeprom_type = optarg;
break;
}
}
if (carr_n == FMC_ID_INVALID || slot_n == FMC_ID_INVALID) {
fputs("Option '-c' and '-s' are mandatory\n", stderr);
goto out_invalid;
}
fmc = fmc_carrier_open(carr_n);
if (!fmc)
goto out_open;
if (eeprom_type) {
err = fmc_slot_eeprom_type_set(fmc, slot_n,
eeprom_type,
sizeof(eeprom_type));
if (err < 0)
fputs("Can't set EEPROM\n", stderr);
}
fmc_slot_eeprom_type_get(fmc, slot_n,
eeprom_type_rb, sizeof(eeprom_type_rb));
fprintf(stdout, "fmc-slot.%u.%u EEPROM type %s\n",
carr_n, slot_n, eeprom_type_rb);
fmc_carrier_close(fmc);
out_open:
out_invalid:
out_opt:
free(name);
out_strdup:
exit(err ? EXIT_FAILURE : EXIT_SUCCESS);
}