From e62604bfb85e32af01604fea86ded8de65d00d79 Mon Sep 17 00:00:00 2001 From: suvendhu Date: Mon, 28 Nov 2022 13:29:26 +0530 Subject: [PATCH] Remove duplicate and inactive hosts from manageable device table --- dmtree/tr181/managementserver.c | 64 +++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/dmtree/tr181/managementserver.c b/dmtree/tr181/managementserver.c index 22f2c811..3f863629 100644 --- a/dmtree/tr181/managementserver.c +++ b/dmtree/tr181/managementserver.c @@ -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;