Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nut-scanner device naming in sanity-check reports #2317

Merged
merged 5 commits into from
Feb 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 51 additions & 14 deletions tools/nut-scanner/nutscan-display.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,19 @@ static char * nutscan_device_type_string[TYPE_END] = {
"EATON_SERIAL"
};

static int last_nutdev_num = 0;
/* Counter incremented by nutscan_display_ups_conf(); later maybe adding other
* renderer methods which could be interested in uniquely numbering the device
* configurations. Note that this is a monotonously increasing number suffixed
* to "nutdev-<type>". Naming of this sort is NOT part of nutscan_device_t or
* nutscan_options_t, it is defined by/appears in the renderer alone so far.
*/
static size_t last_nutdev_num = 0;

void nutscan_display_ups_conf_with_sanity_check(nutscan_device_t * device)
{
/* Note: while a single device is passed to the method, it is actually
* used to locate the list of related device types and iterate it all.
*/
upsdebugx(2, "%s: %s", __func__, device
? (device->type < TYPE_END ? nutscan_device_type_string[device->type] : "<UNKNOWN>")
: "<NULL>");
Expand All @@ -52,9 +61,12 @@ void nutscan_display_ups_conf_with_sanity_check(nutscan_device_t * device)

void nutscan_display_ups_conf(nutscan_device_t * device)
{
/* Note: while a single device is passed to the method, it is actually
* used to locate the list of related device types and iterate it all.
*/
nutscan_device_t * current_dev = device;
nutscan_options_t * opt;
static int nutdev_num = 1;
static size_t nutdev_num = 1;

upsdebugx(2, "%s: %s", __func__, device
? (device->type < TYPE_END ? nutscan_device_type_string[device->type] : "<UNKNOWN>")
Expand All @@ -71,7 +83,7 @@ void nutscan_display_ups_conf(nutscan_device_t * device)

/* Display each device */
do {
printf("[nutdev-%s%i]\n\tdriver = \"%s\"",
printf("[nutdev-%s%" PRIuSIZE "]\n\tdriver = \"%s\"",
nutscan_device_type_lstrings[current_dev->type],
nutdev_num, current_dev->driver);

Expand Down Expand Up @@ -115,6 +127,9 @@ void nutscan_display_ups_conf(nutscan_device_t * device)

void nutscan_display_parsable(nutscan_device_t * device)
{
/* Note: while a single device is passed to the method, it is actually
* used to locate the list of related device types and iterate it all.
*/
nutscan_device_t * current_dev = device;
nutscan_options_t * opt;

Expand Down Expand Up @@ -174,6 +189,9 @@ typedef struct keyval_strings {

void nutscan_display_sanity_check_serial(nutscan_device_t * device)
{
/* Note: while a single device is passed to the method, it is actually
* used to locate the list of related device types and iterate it all.
*/
/* Some devices have useless serial numbers
* (empty strings, all-zeroes, all-spaces etc.)
* and others have identical serial numbers on
Expand All @@ -196,10 +214,20 @@ void nutscan_display_sanity_check_serial(nutscan_device_t * device)
*/
nutscan_device_t * current_dev = device;
nutscan_options_t * opt;
/* Keep numbering consistent with global entry naming.
* Note its last value is after loop, so "real + 1".
/* Keep numbering consistent with global entry naming
* as we call the device config renderer and the sanity
* check collector for the same device, then iterate.
* Note its last value is after loop, so says "real + 1".
* Starts with "1", so a discovery of 1 device yields a
* "last_nutdev_num==2" value here. We would rewind the
* number to start at the beginning of the list for the
* current device type.
* FIXME: It now feels beneficial to add a "nutdev_name"
* right into the nutscan_device_t or its "opt" and to
* pre-parse all discovered lists of devices before any
* rendering to consistently pre-set these strings.
*/
int nutdev_num = last_nutdev_num - 1;
size_t nutdev_num = last_nutdev_num - 1;
size_t listlen = 0, count = 0, i;
keyval_strings_t *map = NULL, *entry = NULL;

Expand All @@ -211,7 +239,7 @@ void nutscan_display_sanity_check_serial(nutscan_device_t * device)
return;
}

/* At least one entry exists... */
/* At least one entry exists, this one (note the 1-based count!)... */
listlen++;

/* Find end of the list */
Expand All @@ -222,6 +250,7 @@ void nutscan_display_sanity_check_serial(nutscan_device_t * device)
/* Find start of the list and count its size */
while (current_dev->prev != NULL) {
current_dev = current_dev->prev;
nutdev_num--;
listlen++;
}

Expand All @@ -241,9 +270,14 @@ void nutscan_display_sanity_check_serial(nutscan_device_t * device)
upsdebugx(3, "%s: checking serial numbers for %" PRIuSIZE " device configuration(s)",
__func__, listlen);

/* NOTE: we start the loop with current_dev == first device in list */
do {
/* Look for serial option in current device */
/* Look for serial option in current device (iterated) */
char nutdev_name[SMALLBUF];
opt = current_dev->opt;
snprintf(nutdev_name, sizeof(nutdev_name), "nutdev-%s%" PRIuSIZE,
nutscan_device_type_lstrings[current_dev->type],
nutdev_num);

while (NULL != opt) {
if (opt->option != NULL && !strcmp(opt->option, "serial")) {
Expand All @@ -261,18 +295,18 @@ void nutscan_display_sanity_check_serial(nutscan_device_t * device)

if (entry) {
/* Got a hit => append value */
upsdebugx(3, "%s: duplicate entry for serial '%s'",
__func__, keytmp);
upsdebugx(3, "%s: duplicate entry for serial '%s' of '%s': %s",
__func__, keytmp, nutdev_name, entry->val);

/* TODO: If changing from preallocated LARGEBUF to
* dynamic allocation, malloc data for larger "val".
*/
snprintfcat(entry->val, sizeof(entry->val),
",nutdev-serial%i", nutdev_num);
",%s", nutdev_name);
} else {
/* No hit => new key */
upsdebugx(3, "%s: new entry for serial '%s'",
__func__, keytmp);
upsdebugx(3, "%s: new entry for serial '%s' of %s",
__func__, keytmp, nutdev_name);

/* TODO: If changing from preallocated LARGEBUF to
* dynamic allocation, malloc data for new "entry"
Expand All @@ -292,7 +326,7 @@ void nutscan_display_sanity_check_serial(nutscan_device_t * device)
snprintf(entry->key, sizeof(entry->key),
"%s", keytmp);
snprintf(entry->val, sizeof(entry->val),
"nutdev-serial%i", nutdev_num);
"%s", nutdev_name);
}

/* Abort the opt-searching loop for this device */
Expand Down Expand Up @@ -361,6 +395,9 @@ void nutscan_display_sanity_check_serial(nutscan_device_t * device)

void nutscan_display_sanity_check(nutscan_device_t * device)
{
/* Note: while a single device is passed to the method, it is actually
* used to locate the list of related device types and iterate it all.
*/
upsdebugx(2, "%s: %s", __func__, device
? (device->type < TYPE_END ? nutscan_device_type_string[device->type] : "<UNKNOWN>")
: "<NULL>");
Expand Down
Loading