Remove duplicate and inactive hosts from manageable device table

This commit is contained in:
suvendhu 2022-11-28 13:29:26 +05:30
parent 0842dabc9a
commit e62604bfb8

View file

@ -28,6 +28,12 @@ struct manageable_device_args {
char host[1025];
};
struct manageable_device_node
{
struct list_head list;
struct manageable_device_args dev;
};
static void get_option125_suboption(char *data, int option, char *dst, int dst_len)
{
int data_len = 0, len = 0;
@ -107,6 +113,23 @@ static void get_option125_suboption(char *data, int option, char *dst, int dst_l
}
}
static bool is_active_host(const char *mac, json_object *res)
{
json_object *host_obj = NULL, *arrobj = NULL;
int i = 0;
bool active = false;
dmjson_foreach_obj_in_array(res, arrobj, host_obj, i, 1, "hosts") {
if (strcmp(dmjson_get_value(host_obj, 1, "macaddr"), mac) == 0) {
char *val = dmjson_get_value(host_obj, 1, "active");
string_to_bool(val, &active);
break;
}
}
return active;
}
static int browseManageableDevice(struct dmctx *dmctx, DMNODE *parent_node, void *prev_data, char *prev_instance)
{
FILE *f = fopen(DHCP_CLIENT_OPTIONS_FILE, "r");
@ -114,9 +137,14 @@ static int browseManageableDevice(struct dmctx *dmctx, DMNODE *parent_node, void
return 0;
struct manageable_device_args device;
struct manageable_device_node *dev_p = NULL;
char line[2048];
char *inst = NULL;
int id = 0;
json_object *res = NULL;
LIST_HEAD(dev_list);
dmubus_call("topology", "hosts", UBUS_ARGS{0}, 0, &res);
while (fgets(line, sizeof(line), f) != NULL) {
remove_new_line(line);
@ -133,6 +161,10 @@ static int browseManageableDevice(struct dmctx *dmctx, DMNODE *parent_node, void
if (DM_STRLEN(linker) == 0)
continue;
/* check that the host is still active or not */
if (!is_active_host(device.mac, res))
continue;
strncpy(device.host, linker, 1024);
get_option125_suboption(line, OPT_OUI, device.oui, sizeof(device.oui));
get_option125_suboption(line, OPT_SERIAL, device.serial, sizeof(device.serial));
@ -141,11 +173,43 @@ static int browseManageableDevice(struct dmctx *dmctx, DMNODE *parent_node, void
if (DM_STRCMP(device.oui, "-") == 0 || DM_STRCMP(device.serial, "-") == 0)
continue;
/* check if already added in the list */
bool found = false;
list_for_each_entry(dev_p, &dev_list, list) {
if (strcmp(dev_p->dev.oui, device.oui) == 0 && strcmp(dev_p->dev.serial, device.serial) == 0 &&
strcmp(dev_p->dev.class, device.class) == 0) {
found = true;
break;
}
}
if (found == true)
continue;
/* add device in device list */
struct manageable_device_node *node = dmcalloc(1, sizeof(struct manageable_device_node));
if (node == NULL)
continue;
list_add_tail(&node->list, &dev_list);
snprintf(node->dev.class, sizeof(node->dev.class), "%s", device.class);
snprintf(node->dev.serial, sizeof(node->dev.serial), "%s", device.serial);
snprintf(node->dev.oui, sizeof(node->dev.oui), "%s", device.oui);
/* add device instance */
inst = handle_instance_without_section(dmctx, parent_node, ++id);
if (DM_LINK_INST_OBJ(dmctx, parent_node, (void *)&device, inst) == DM_STOP)
break;
}
/* free device list */
dev_p = NULL;
while (dev_list.next != &dev_list) {
dev_p = list_entry(dev_list.next, struct manageable_device_node, list);
list_del(&dev_p->list);
}
fclose(f);
return 0;