Skip to content
Snippets Groups Projects
Commit 1132a34c authored by Suvendhu Hansa's avatar Suvendhu Hansa :speech_balloon:
Browse files

Remove host instance from dmmap

parent 20402c9f
No related branches found
No related tags found
1 merge request!27Remove host instance from dmmap
Pipeline #154864 passed
...@@ -21,95 +21,6 @@ DM_MAP_OBJ tDynamicObj[] = { ...@@ -21,95 +21,6 @@ DM_MAP_OBJ tDynamicObj[] = {
{0} {0}
}; };
static int host_get_last_instance(void)
{
struct uci_section *s = is_dmmap_section_exist("dmmap_hosts", "global");
int last_inst = 0;
if (s == NULL) {
dmuci_add_section_bbfdm("dmmap_hosts", "global", &s);
} else {
char *last_instance = NULL;
dmuci_get_option_value_string_bbfdm("dmmap_hosts", "@global[0]", "last_instance", &last_instance);
last_inst = DM_STRTOUL(last_instance);
}
return last_inst;
}
static void synchronize_hosts_array_with_dmmap(json_object *res)
{
struct uci_section *s = NULL, *stmp = NULL;
json_object *hosts_arr = NULL;
char *mac = NULL, *host_mac = NULL;
bool host_read_idx = false;
int host_idx = 0;
if (!res)
return;
json_object_object_get_ex(res, "hosts", &hosts_arr);
int nbre_hosts = (hosts_arr) ? json_object_array_length(hosts_arr) : 0;
uci_path_foreach_sections_safe(bbfdm, "dmmap_hosts", "host", stmp, s) {
dmuci_get_value_by_section_string(s, "mac", &mac);
if (host_idx < nbre_hosts) {
if (!host_read_idx) {
json_object *host_obj = json_object_array_get_idx(hosts_arr, host_idx);
host_mac = dmjson_get_value(host_obj, 1, "macaddr");
host_read_idx = true;
}
if (DM_STRCMP(mac, host_mac) == 0) {
host_read_idx = false;
host_idx++;
continue;
}
}
// Remove host from dmmap
dmuci_delete_by_section(s, NULL, NULL);
}
}
static char *host_get_instance(char *mac, int idx, int *last_instance)
{
struct uci_section *s = NULL;
char *inst = NULL;
int i = 0;
uci_path_foreach_sections(bbfdm, "dmmap_hosts", "host", s) {
char *curr_mac = NULL;
if (i > idx)
goto create_host;
i++;
dmuci_get_value_by_section_string(s, "mac", &curr_mac);
if (DM_STRCMP(curr_mac, mac) == 0) {
dmuci_get_value_by_section_string(s, "instance", &inst);
return inst;
}
}
create_host:
{
dmasprintf(&inst, "%lu", ++(*last_instance));
dmuci_add_section_bbfdm("dmmap_hosts", "host", &s);
dmuci_set_value_by_section(s, "mac", mac);
dmuci_set_value_by_section(s, "instance", inst);
dmuci_set_value_bbfdm("dmmap_hosts", "@global[0]", "last_instance", inst);
}
return inst;
}
/************************************************************* /*************************************************************
* ENTRY METHOD * ENTRY METHOD
**************************************************************/ **************************************************************/
...@@ -117,20 +28,14 @@ create_host: ...@@ -117,20 +28,14 @@ create_host:
static int browseHostsHostInst(struct dmctx *dmctx, DMNODE *parent_node, void *prev_data, char *prev_instance) static int browseHostsHostInst(struct dmctx *dmctx, DMNODE *parent_node, void *prev_data, char *prev_instance)
{ {
json_object *res = NULL, *host_obj = NULL, *arrobj = NULL; json_object *res = NULL, *host_obj = NULL, *arrobj = NULL;
int idx = 0, last_inst = 0; int idx = 0, i = 0;
char *inst = NULL; char *inst = NULL;
last_inst = host_get_last_instance();
dmubus_call("hosts", "show", UBUS_ARGS{0}, 0, &res); dmubus_call("hosts", "show", UBUS_ARGS{0}, 0, &res);
synchronize_hosts_array_with_dmmap(res);
dmjson_foreach_obj_in_array(res, arrobj, host_obj, idx, 1, "hosts") { dmjson_foreach_obj_in_array(res, arrobj, host_obj, idx, 1, "hosts") {
char *mac = dmjson_get_value(host_obj, 1, "macaddr"); inst = handle_instance_without_section(dmctx, parent_node, ++i);
inst = host_get_instance(mac, idx, &last_inst);
if (DM_LINK_INST_OBJ(dmctx, parent_node, (void *)host_obj, inst) == DM_STOP) if (DM_LINK_INST_OBJ(dmctx, parent_node, (void *)host_obj, inst) == DM_STOP)
break; break;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment