Skip to content
Snippets Groups Projects
config.c 56.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • Anjan Chanda's avatar
    Anjan Chanda committed
    		if (r->init_config)
    			r->init_config(r, &pol->policy);
    		list_add(&pol->list, &new->steer_policylist);
    	}
    
    	INIT_LIST_HEAD(&new->assoc_ctrllist);
    
    	/* f->cfg = new; */
    	dbg("%s: %s netif_fh->cfg = %p\n", __func__, new->name, new);
    
    	list_add(&new->list, &cfg->fhlist);
    
    	return new;
    
    err_oom:
    	list_flush(&new->steer_policylist, struct steer_policy, list);
    	free(new);
    	return NULL;
    }
    
    
    /* create fh-iface config and initialize with default values */
    struct netif_bkcfg *create_backhaul_iface_config(struct agent_config *cfg,
    							const char *ifname)
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    {
    
    	struct netif_bkcfg *new;
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    
    
    	new = calloc(1, sizeof(struct netif_bkcfg));
    	if (!new) {
    		warn("OOM! config\n");
    		return NULL;
    	}
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    
    
    	snprintf(new->name, 16, "%s", ifname);
    	new->enabled = true;
    	new->onboarded = false;
    
    	new->band = BAND_UNKNOWN;
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    
    
    	/* f->cfg = new; */
    	dbg("%s: %s netif_fh->cfg = %p\n", __func__, new->name, new);
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    
    
    	list_add(&new->list, &cfg->bklist);
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    }
    
    static void config_update_entry(struct uci_context *ctx, struct uci_package *p,
    				struct uci_section *s, const char *optname,
    				int add, void *val, int len)
    {
    	struct uci_ptr ptr;
    
    	memset(&ptr, 0, sizeof(struct uci_ptr));
    	ptr.p = p;
    	ptr.s = s;
    	ptr.package = p->e.name;
    	ptr.section = s->e.name;
    	ptr.option = optname;
    	ptr.target = UCI_TYPE_OPTION;
    	ptr.flags |= UCI_LOOKUP_EXTENDED;
    	ptr.value = (char *)val;
    
    	if (add) {
    		dbg("config: add list option: %s\n", (char *)val);
    		uci_add_list(ctx, &ptr);
    	} else {
    		dbg("config: del list option: %s\n", (char *)val);
    		uci_del_list(ctx, &ptr);
    	}
    	uci_commit(ctx, &p, false);
    }
    
    int config_update(const char *confname, struct agent_config *cfg,
    			const char *section, const char *option,
    			int add,
    			void *value, int len)
    {
    	struct uci_context *ctx = NULL;
    	struct uci_package *pkg = NULL;
    	struct uci_element *e;
    
    	ctx = uci_alloc_context();
    	if (ctx && uci_load(ctx, confname, &pkg) != UCI_OK) {
    		dbg("config file '%s' not found!\n", confname);
    		free(ctx);
    		return -1;
    	}
    
    	uci_foreach_element(&pkg->sections, e) {
    		struct uci_section *s = uci_to_section(e);
    		struct uci_element *x, *tmp;
    		struct uci_option *op;
    
    		if (strcmp(s->type, section))
    			continue;
    
    		/* iter through matched 'section' for the 'option' */
    		uci_foreach_element_safe(&s->options, tmp, x) {
    			if (strcmp(x->name, option))
    				continue;
    
    			op = uci_to_option(x);
    			if (op->type == UCI_TYPE_LIST) {
    				uci_foreach_element(&op->v.list, x) {
    					if (!strncmp(x->name, value, len)) {
    						if (!add)
    							config_update_entry(ctx,
    
    Jakob Olsson's avatar
    Jakob Olsson committed
    								pkg, s,
    								option, 0,
    								value, len);
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    
    						goto out_exit;
    					}
    				}
    				/* add new exclude at end of list */
    				if (add)
    
    Jakob Olsson's avatar
    Jakob Olsson committed
    					config_update_entry(ctx, pkg, s, option,
    							1, value, len);
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    
    				goto out_exit;
    			}
    		}
    		/* 'option' name not present in 'section'
    		 * Create a new one at end of 'section'.
    		 */
    		if (add)
    			config_update_entry(ctx, pkg, s, option, 1, value, len);
    
    		goto out_exit;
    	}
    out_exit:
    	uci_free_context(ctx);
    	return 0;
    }
    
    int config_update2(const char *confname, struct agent_config *cfg,
    		const char *section_type,
    		const char *match_option,
    		const char *match_option_value,
    		const char *option,
    		int add,
    		void *value, int len)
    {
    	struct uci_context *ctx = NULL;
    	struct uci_package *pkg = NULL;
    	struct uci_element *e;
    
    	ctx = uci_alloc_context();
    	if (ctx && uci_load(ctx, confname, &pkg) != UCI_OK) {
    		dbg("config file '%s' not found!\n", confname);
    		free(ctx);
    		return -1;
    	}
    
    	uci_foreach_element(&pkg->sections, e) {
    		struct uci_section *s = uci_to_section(e);
    		struct uci_element *x, *tmp;
    		struct uci_option *op;
    		const char *optstring;
    
    		if (strcmp(s->type, section_type))
    			continue;
    
    		if (match_option && match_option_value) {
    
    Jakob Olsson's avatar
    Jakob Olsson committed
    			optstring = uci_lookup_option_string(ctx, s,
    				match_option);
    
    Anjan Chanda's avatar
    Anjan Chanda committed
    			if (!optstring || strcmp(optstring, match_option_value))
    				continue;
    		}
    
    		/* iter through matched 'section' for the 'option' */
    		uci_foreach_element_safe(&s->options, tmp, x) {
    			if (strcmp(x->name, option))
    				continue;
    
    			op = uci_to_option(x);
    			if (op->type == UCI_TYPE_LIST) {
    				uci_foreach_element(&op->v.list, x) {
    					if (!strncmp(x->name, value, len)) {
    						if (!add) {
    							config_update_entry(ctx,
    								pkg, s, option,
    								0, value, len);
    						}
    
    						goto out_exit;
    					}
    				}
    				/* add new 'option' at end of list */
    				if (add) {
    					config_update_entry(ctx, pkg, s, option,
    								1, value, len);
    				}
    
    				goto out_exit;
    			}
    		}
    		/* 'option' name not present in 'section'
    		 * Create a new one at end of 'section'.
    		 */
    		if (add)
    			config_update_entry(ctx, pkg, s, option,
    							1, value, len);
    
    		goto out_exit;
    	}
    out_exit:
    	uci_free_context(ctx);
    	return 0;
    }
    
    
    #if 0
    struct config uci_config = {
    	.name = "uci",
    	.priv = uci_ctx;
    	.get = uci_get_config,
    	.set = uci_set_config,
    	.init = uci_setup,
    	.exit = uci_exit,
    };
    
    #define priv_get_config(priv)	container_of(priv, struct config, priv)
    
    void register_config(struct config *c)
    {
    	static struct uci_context *ctx;
    	static struct uci_package *pkg;
    	struct uci_element *e;
    	int ret = 0;
    
    	if (uci_ctx)
    		return priv_get_config(uci_ctx);
    
    	ctx = uci_alloc_context();
    	if (ctx) {
    		uci_ctx = ctx;
    		memcpy(c, &uci_config, sizeof(*cfg));
    	}
    	if (uci_load(ctx, "wifiagent", &pkg))
    		return -1;
    
    	uci_foreach_element(&pkg->sections, e) {
    		struct uci_section *s = uci_to_section(e);
    		const char *option_val;
    
    		if (strcmp(s->type, "wifiagent"))
    			continue;
    
    		option_val = uci_lookup_option_string(ctx, s, name);
    		if (option_val)
    			sprintf(val, "%s", option_val);
    		else
    			ret = -1;
    	}
    	uci_free_context(ctx);
    	return ret;
    
    }
    #endif
    
    static int agent_config_get_wifi_agent(struct agent_config *a,
    
    		A_PROFILE,
    
    		A_DISCOVERY_PROTO,
    
    		NUM_POLICIES
    	};
    	const struct uci_parse_option opts[] = {
    		{ .name = "enabled", .type = UCI_TYPE_STRING },
    
    		{ .name = "debug", .type = UCI_TYPE_STRING },
    
    		{ .name = "profile", .type = UCI_TYPE_STRING },
    
    		{ .name = "brcm_setup", .type = UCI_TYPE_STRING },
    		/*{ .name = "configured", .type = UCI_TYPE_STRING },*/
    		{ .name = "controller_macaddr", .type = UCI_TYPE_STRING },
    
    		{ .name = "exclude", .type = UCI_TYPE_LIST },
    		{ .name = "exclude_btm", .type = UCI_TYPE_LIST },
    
    		{ .name = "al_bridge", .type = UCI_TYPE_STRING },
    
    		{ .name = "netdev", .type = UCI_TYPE_STRING },
    
    		{ .name = "discovery_proto", .type = UCI_TYPE_STRING }
    
    	};
    	struct uci_option *tb[NUM_POLICIES];
    
    	uci_parse_section(s, opts, NUM_POLICIES, tb);
    
    	if (tb[A_ENABLED])
    		a->enabled = atoi(tb[A_ENABLED]->v.string) == 1 ? true : false;
    
    
    	if (tb[A_DEBUG]) {
    		a->debug_level = atoi(tb[A_DEBUG]->v.string);
    		if (verbose < a->debug_level)
    			verbose = a->debug_level;
    	}
    
    
    	if (tb[A_PROFILE])
    		a->profile = atoi(tb[A_PROFILE]->v.string);
    
    
    	if (tb[A_BRCM_SETUP])
    		a->brcm_setup = atoi(tb[A_BRCM_SETUP]->v.string);
    
    	/*if (tb[A_CONFIGURED])
    		a->configured = atoi(tb[A_CONFIGURED]->v.string);*/
    
    	if (tb[A_CNTLR_MAC])
    		hwaddr_aton(tb[A_CNTLR_MAC]->v.string, a->cntlr_almac);
    
    
    	if (tb[A_EXCLUDE]) {
    		struct uci_element *xi;
    
    		dbg("Steer: exclude: ");
    		uci_foreach_element(&tb[A_EXCLUDE]->v.list, xi) {
    			dbg("%s ", xi->name);
    			stax_add_entry(&a->steer_excludelist, xi->name);
    		}
    		dbg("\n");
    	}
    
    	if (tb[A_EXCLUDE_BTM]) {
    		struct uci_element *xi;
    
    		dbg("Steer: exclude_btm: ");
    		uci_foreach_element(&tb[A_EXCLUDE_BTM]->v.list, xi) {
    			dbg("%s ", xi->name);
    			stax_add_entry(&a->steer_btm_excludelist, xi->name);
    		}
    		dbg("\n");
    	}
    
    
    	if (tb[A_AL_BRIDGE]) {
    		const char *iface;
    
    		iface = tb[A_AL_BRIDGE]->v.string;
    		strncpy(a->al_bridge, iface, sizeof(a->al_bridge) - 1);
    	} else /* Default to br-lan if non-specfied */
    		strncpy(a->al_bridge, "br-lan", sizeof(a->al_bridge) - 1);
    
    
    	if (tb[A_NETDEV]) {
    		const char *ifname;
    		ifname = tb[A_NETDEV]->v.string;
    		strncpy(a->netdev, ifname, sizeof(a->netdev) - 1);
    	} else { /* Default to wl/wlan if not specfied */
    		strncpy(a->netdev, (a->brcm_setup)?"wl":"wlan", sizeof(a->netdev) - 1);
    	}
    
    
    	if (tb[A_DISCOVERY_PROTO]) {
    		const char *proto = tb[A_DISCOVERY_PROTO]->v.string;
    
    		if (!strncmp(proto, "static", 6))
    			a->discovery_proto = DISCOVERY_STATIC;
    		if (!strncmp(proto, "auto", 4)) /* defaults to auto */
    			a->discovery_proto = DISCOVERY_AUTOMATIC;
    	}
    
    
    static int agent_config_get_wifi_radio(struct agent_config *a,
    				       struct uci_section *s)
    {
    	enum {
    		WIFI_RADIO_DEVICE,
    		WIFI_RADIO_BAND,
    		WIFI_RADIO_CONFIGURED,
    		WIFI_RADIO_ONBOARDED,
    		NUM_WIFI_RADIO_POLICIES,
    	};
    	const struct uci_parse_option opts[] = {
    		{ .name = "device", .type = UCI_TYPE_STRING },
    		{ .name = "band", .type = UCI_TYPE_STRING },
    		{ .name = "configured", .type = UCI_TYPE_STRING },
    		{ .name = "onboarded", .type = UCI_TYPE_STRING },
    	};
    	struct uci_option *tb[NUM_WIFI_RADIO_POLICIES];
    	const char *ifname = NULL;
    	uint32_t band = 0;
    	struct agent_config_radio *n;
    
    	uci_parse_section(s, opts, NUM_WIFI_RADIO_POLICIES, tb);
    
    	if (!tb[WIFI_RADIO_DEVICE] || !tb[WIFI_RADIO_BAND]) {
    		warn("No radio name or band option found!\n");
    		return -1;
    	}
    
    	if (tb[WIFI_RADIO_DEVICE])
    		ifname = tb[WIFI_RADIO_DEVICE]->v.string;
    
    	if (tb[WIFI_RADIO_BAND]) {
    		band = atoi(tb[WIFI_RADIO_BAND]->v.string);
    		if (band != 2 && band != 5) {
    			warn("Incorrect band '%d' in config\n", band);
    			return -1;
    		}
    	}
    
    	if (ifname && band) {
    		n = calloc(1, sizeof(*n));
    		if (!n) {
    			warn("-ENOMEM!\n");
    			return -1;
    		}
    		strncpy(n->name, ifname, 16);
    		n->name[15] = '\0';
    		n->band = band;
    	}
    
    	if (tb[WIFI_RADIO_CONFIGURED]) {
    		n->configured = atoi(tb[WIFI_RADIO_CONFIGURED]->v.string) == 1 ?
    								true : false;
    	}
    
    	if (tb[WIFI_RADIO_ONBOARDED]) {
    		n->onboarded = atoi(tb[WIFI_RADIO_ONBOARDED]->v.string) == 1 ?
    								true : false;
    	}
    
    	list_add_tail(&n->list, &a->radiolist);
    
    	return 0;
    }
    
    
    static int agent_config_get_bk_iface(struct agent_config *a,
    
    		BK_BAND,
    
    		BK_ENABLED,
    		BK_ONBOARDED,
    
    		BK_DISALLOWED_BSTA_P1,
    		BK_DISALLOWED_BSTA_P2,
    
    		NUM_POLICIES
    	};
    	const struct uci_parse_option opts[] = {
    		{ .name = "ifname", .type = UCI_TYPE_STRING },
    
    		{ .name = "device", .type = UCI_TYPE_STRING },
    
    		{ .name = "band", .type = UCI_TYPE_STRING },
    
    		{ .name = "enabled", .type = UCI_TYPE_STRING },
    
    		{ .name = "onboarded", .type = UCI_TYPE_STRING },
    		{ .name = "ssid", .type = UCI_TYPE_STRING },
    		{ .name = "key", .type = UCI_TYPE_STRING },
    
    		{ .name = "encryption", .type = UCI_TYPE_STRING },
    		{ .name = "disallow_bsta_p1", .type = UCI_TYPE_STRING },
    		{ .name = "disallow_bsta_p2", .type = UCI_TYPE_STRING }
    
    	};
    	struct uci_option *tb[NUM_POLICIES];
    	struct netif_bkcfg *bk;
    	const char *ifname;
    
    	uci_parse_section(s, opts, NUM_POLICIES, tb);
    
    	if (tb[BK_IFNAME]) {
    		ifname = tb[BK_IFNAME]->v.string;
    		bk = get_netif_bkcfg_by_name(a, ifname);
    		if (!bk) {
    			bk = create_backhaul_iface_config(a, ifname);
    			if (!bk) {
    				warn("%s: OOM!\n", __func__);
    				return -1;
    			}
    		} else {
    			warn("Duplicate 'bk-iface %s' config!! ignore\n",
    					ifname);
    		}
    	} else {
    		warn("No ifname in bk-iface section!\n");
    		return -1;
    	}
    
    
    	if (tb[BK_DEVICE]) {
    		const char *device;
    
    		device = tb[BK_DEVICE]->v.string;
    		strncpy(bk->device, device, sizeof(bk->device) - 1);
    	}
    
    
    	if (tb[BK_ENABLED])
    		bk->enabled = atoi(tb[BK_ENABLED]->v.string);
    
    	if (tb[BK_ONBOARDED])
    		bk->onboarded = atoi(tb[BK_ONBOARDED]->v.string);
    
    
    	if (tb[BK_BAND]) {
    		int band = atoi(tb[BK_BAND]->v.string);
    
    		if (band == 2)
    			bk->band = BAND_2;
    		else if (band == 5)
    			bk->band = BAND_5;
    	}
    
    
    	if (tb[BK_SSID]) {
    		const char *ssid;
    
    		ssid = tb[BK_SSID]->v.string;
    		strncpy(bk->ssid, ssid, sizeof(bk->ssid) - 1);
    	}
    
    	if (tb[BK_KEY]) {
    		const char *key;
    
    		key = tb[BK_KEY]->v.string;
    		strncpy(bk->key, key, sizeof(bk->key) - 1);
    	}
    
    	if (tb[BK_ENCRYPTION]) {
    		const char *encryption;
    
    		encryption = tb[BK_ENCRYPTION]->v.string;
    		strncpy(bk->encryption, encryption, sizeof(bk->encryption) - 1);
    	}
    
    
    	if (tb[BK_DISALLOWED_BSTA_P1])
    		bk->disallowed_bsta_p1 =
    			atoi(tb[BK_DISALLOWED_BSTA_P1]->v.string);
    
    	if (tb[BK_DISALLOWED_BSTA_P2])
    		bk->disallowed_bsta_p2 =
    			atoi(tb[BK_DISALLOWED_BSTA_P2]->v.string);
    
    
    static int agent_config_get_fh_iface(struct agent_config *a,
    
    Jakob Olsson's avatar
    Jakob Olsson committed
    		struct uci_section *s)
    
    		FH_BAND,
    
    		FH_ASSOC_CTRL,
    		FH_BTM_RETRY,
    		FH_BTM_RETRY_SECS,
    		FH_FLBK_LEGACY,
    		FH_STEER_LEGACY_RASSOC_SECS,
    		FH_STEER_LEGACY_RETRY_SECS,
    		FH_ASSOC_CTRL_SECS,
    
    		FH_POLICY,
    		FH_UTIL_THRESHOLD,
    		FH_RCPI_THRESHOLD,
    		FH_REPORT_RCPI_THRESHOLD,
    		FH_RCPI_HYSTERESIS_MARGIN,
    		FH_REPORT_UTIL_THRESHOLD,
    		FH_INCLUDE_STA_STATS,
    		FH_INCLUDE_STA_METRIC,
    
    		NUM_POLICIES,
    	};
    	const struct uci_parse_option opts[] = {
    		{ .name = "ifname", .type = UCI_TYPE_STRING },
    
    		{ .name = "band", .type = UCI_TYPE_STRING },
    
    		{ .name = "steer", .type = UCI_TYPE_LIST },
    
    		{ .name = "device", .type = UCI_TYPE_STRING },
    
    		{ .name = "assoc_ctrl", .type = UCI_TYPE_LIST },
    
    		{ .name = "btm_retry", .type = UCI_TYPE_STRING },
    		{ .name = "btm_retry_secs", .type = UCI_TYPE_STRING },
    		{ .name = "fallback_legacy", .type = UCI_TYPE_STRING },
    		{ .name = "steer_legacy_reassoc_secs", .type = UCI_TYPE_STRING },
    		{ .name = "steer_legacy_retry_secs", .type = UCI_TYPE_STRING },
    
    		{ .name = "assoc_ctrl_secs", .type = UCI_TYPE_STRING },
    		{ .name = "ssid", .type = UCI_TYPE_STRING },
    		{ .name = "key", .type = UCI_TYPE_STRING },
    
    		{ .name = "encryption", .type = UCI_TYPE_STRING },
    		{ .name = "policy", .type = UCI_TYPE_STRING },
    		{ .name = "util_threshold", .type = UCI_TYPE_STRING },
    		{ .name = "rcpi_threshold", .type = UCI_TYPE_STRING },
    		{ .name = "report_rcpi_threshold", .type = UCI_TYPE_STRING },
    		{ .name = "rcpi_hysteresis_margin", .type = UCI_TYPE_STRING },
    		{ .name = "report_util_threshold", .type = UCI_TYPE_STRING },
    		{ .name = "include_sta_stats", .type = UCI_TYPE_STRING },
    		{ .name = "include_sta_metric", .type = UCI_TYPE_STRING }
    
    	};
    	struct uci_option *tb[NUM_POLICIES];
    	struct netif_fhcfg *fh;
    
    	uci_parse_section(s, opts, NUM_POLICIES, tb);
    
    	if (tb[FH_IFNAME]) {
    
    		const char *ifname;
    
    
    		ifname = tb[FH_IFNAME]->v.string;
    
    		fh = get_netif_fhcfg_by_name(a, ifname);
    		if (!fh) {
    			fh = create_fronthaul_iface_config(a, ifname);
    			if (!fh) {
    				warn("%s: OOM!\n", __func__);
    				return -1;
    			}
    		} else {
    			warn("Duplicate 'fh-iface %s' config!! ignore\n",
    					ifname);
    		}
    	} else {
    		warn("No ifname in fh-iface section!\n");
    		return -1;
    	}
    
    
    	if (tb[FH_BAND]) {
    		int band = atoi(tb[FH_BAND]->v.string);
    
    		if (band == 2)
    			fh->band = BAND_2;
    		else if (band == 5)
    			fh->band = BAND_5;
    	}
    
    
    	if (tb[FH_STEER]) {
    		struct uci_element *xi;
    
    		dbg("Steer: param: ");
    		uci_foreach_element(&tb[FH_STEER]->v.list, xi) {
    			struct steer_policy *p = NULL;
    			struct steer_rule *r;
    
    			dbg("%s ", xi->name);
    			p = get_steer_policy_by_name(fh, xi->name);
    			if (!p) {
    				/* TODO? */
    				dbg("TODO!! steer before ifname\n");
    				continue;
    			}
    			p->enabled = true;
    			r = get_steer_rule_by_name(xi->name);
    			if (r)
    				r->enabled = true;
    		}
    		dbg("\n");
    	}
    
    
    	if (tb[FH_DEVICE]) {
    		const char *device;
    
    		device = tb[FH_DEVICE]->v.string;
    
    		strncpy(fh->device, device, sizeof(fh->device) - 1);
    	}
    
    
    	if (tb[FH_BTM_RETRY])
    		fh->steer_btm_retry = atoi(tb[FH_BTM_RETRY]->v.string);
    
    	if (tb[FH_BTM_RETRY_SECS])
    		fh->steer_btm_retry_secs = atoi(tb[FH_BTM_RETRY_SECS]->v.string);
    
    	if (tb[FH_FLBK_LEGACY])
    		fh->fallback_legacy = atoi(tb[FH_FLBK_LEGACY]->v.string);
    
    	if (tb[FH_STEER_LEGACY_RASSOC_SECS])
    		fh->steer_legacy_reassoc_secs =
    				atoi(tb[FH_STEER_LEGACY_RASSOC_SECS]->v.string);
    
    	if (tb[FH_STEER_LEGACY_RETRY_SECS])
    		fh->steer_legacy_retry_secs =
    				atoi(tb[FH_STEER_LEGACY_RETRY_SECS]->v.string);
    
    	if (tb[FH_ASSOC_CTRL_SECS])
    		fh->assoc_control_time =
    				atoi(tb[FH_ASSOC_CTRL_SECS]->v.string);
    
    
    	if (tb[FH_SSID]) {
    		const char *ssid;
    
    		ssid = tb[FH_SSID]->v.string;
    		strncpy(fh->ssid, ssid, sizeof(fh->ssid) - 1);
    	}
    
    	if (tb[FH_KEY]) {
    		const char *key;
    
    		key = tb[FH_KEY]->v.string;
    		strncpy(fh->key, key, sizeof(fh->key) - 1);
    	}
    
    	if (tb[FH_ENCRYPTION]) {
    		const char *encryption;
    
    		encryption = tb[FH_ENCRYPTION]->v.string;
    		strncpy(fh->encryption, encryption, sizeof(fh->encryption) - 1);
    	}
    
    
    	if (tb[FH_POLICY])
    		fh->policy = atoi(tb[FH_POLICY]->v.string);
    
    	if (tb[FH_UTIL_THRESHOLD])
    		fh->util_threshold = atoi(tb[FH_UTIL_THRESHOLD]->v.string);
    
    	if (tb[FH_RCPI_THRESHOLD])
    		fh->rcpi_threshold = atoi(tb[FH_RCPI_THRESHOLD]->v.string);
    
    	if (tb[FH_REPORT_RCPI_THRESHOLD])
    		fh->report_rcpi_threshold =
    			atoi(tb[FH_REPORT_RCPI_THRESHOLD]->v.string);
    
    	if (tb[FH_RCPI_HYSTERESIS_MARGIN])
    		fh->rcpi_hysteresis_margin =
    			atoi(tb[FH_RCPI_HYSTERESIS_MARGIN]->v.string);
    
    	if (tb[FH_REPORT_UTIL_THRESHOLD])
    		fh->report_util_threshold =
    			atoi(tb[FH_REPORT_UTIL_THRESHOLD]->v.string);
    
    	if (tb[FH_INCLUDE_STA_STATS])
    		fh->include_sta_stats =
    			atoi(tb[FH_INCLUDE_STA_STATS]->v.string);
    
    	if (tb[FH_INCLUDE_STA_METRIC])
    		fh->include_sta_metric =
    			atoi(tb[FH_INCLUDE_STA_METRIC]->v.string);
    
    
    static int agent_config_get_steer_param(struct agent_config *a,
    		struct uci_section *s)
    
    {
    	struct steer_rule *r;
    
    	dbg("Steer-param: %s\n", s->e.name);
    	r = get_steer_rule_by_name(s->e.name);
    	if (!r)
    		return -1;
    
    	dbg("Rule to handle steer-param '%s' available\n", s->e.name);
    	r->config(r, a, s);
    
    	return 0;
    }
    
    
    static int agent_config_get_policy_param(struct agent_config *a,
    		struct uci_section *s)
    {
    	enum {
    		POL_REPORT_INTERVAL,
    		POL_PVID,
    		POL_PCP_DEFAULT,
    		POL_REPORT_SCAN,
    		POL_REPORT_STA_ASSOCFAILS,
    		POL_REPORT_STA_ASSOCFAILS_RATE,
    		NUM_POLICIES
    	};
    	const struct uci_parse_option opts[] = {
    		{ .name = "report_interval", .type = UCI_TYPE_STRING },
    		{ .name = "pvid", .type = UCI_TYPE_STRING },
    		{ .name = "pcp_default", .type = UCI_TYPE_STRING },
    		{ .name = "repost_scan", .type = UCI_TYPE_STRING },
    		{ .name = "report_sta_assocfails", .type = UCI_TYPE_STRING },
    		{ .name = "report_sta_assocfails_rate", .type = UCI_TYPE_STRING },
    	};
    
    	struct uci_option *tb[NUM_POLICIES];
    	struct policy_cfg *cfg;
    
    	uci_parse_section(s, opts, NUM_POLICIES, tb);
    
    	cfg = (struct policy_cfg *)calloc(1, sizeof(struct policy_cfg));
    	if (!cfg)
    		return -1;
    
    	if (tb[POL_REPORT_INTERVAL])
    		cfg->report_interval = atoi(tb[POL_REPORT_INTERVAL]->v.string);
    
    	if (tb[POL_PVID])
    		cfg->pvid = atoi(tb[POL_PVID]->v.string);
    
    	if (tb[POL_PCP_DEFAULT])
    		cfg->pcp_default = atoi(tb[POL_PCP_DEFAULT]->v.string);
    
    	if (tb[POL_REPORT_SCAN])
    		cfg->report_scan = atoi(tb[POL_REPORT_SCAN]->v.string);
    
    	if (tb[POL_REPORT_STA_ASSOCFAILS])
    		cfg->report_sta_assocfails =
    			atoi(tb[POL_REPORT_STA_ASSOCFAILS]->v.string);
    
    	if (tb[POL_REPORT_STA_ASSOCFAILS_RATE])
    		cfg->report_sta_assocfails_rate =
    			atoi(tb[POL_REPORT_STA_ASSOCFAILS_RATE]->v.string);
    
    	if (a->pcfg)
    		free(a->pcfg);
    
    	a->pcfg = cfg;
    
    	return 0;
    }
    
    
    int agent_config_reload(struct agent_config *cfg)
    
    {
    	struct uci_context *ctx;
    	struct uci_package *pkg;
    	struct uci_element *e;
    
    
    	cfg->enabled = false;
    	cfg->runfreq = AGENT_RUN_AUTO;
    
    
    	ctx = uci_alloc_context();
    	if (!ctx)
    		return -1;
    
    
    		uci_free_context(ctx);
    		return -1;
    	}
    
    	uci_foreach_element(&pkg->sections, e) {
    		struct uci_section *s = uci_to_section(e);
    
    
    			agent_config_get_wifi_agent(cfg, s);
    
    		else if (!strcmp(s->type, "wifi-radio"))
    			agent_config_get_wifi_radio(cfg, s);
    
    Jakob Olsson's avatar
    Jakob Olsson committed
    		else if (!strcmp(s->type, "fh-iface"))
    
    			agent_config_get_fh_iface(cfg, s);
    
    Jakob Olsson's avatar
    Jakob Olsson committed
    		else if (!strcmp(s->type, "bk-iface"))
    
    			agent_config_get_bk_iface(cfg, s);
    
    Jakob Olsson's avatar
    Jakob Olsson committed
    		else if (!strcmp(s->type, "steer"))
    
    			agent_config_get_steer_param(cfg, s);
    
    		else if (!strcmp(s->type, "policy"))
    			agent_config_get_policy_param(cfg, s);
    
    Jakob Olsson's avatar
    Jakob Olsson committed
    }
    
    int config_generate_radio(struct agent_config *cfg, struct uci_context *ctx,
    		char *device, uint8_t band)
    {
    	struct uci_package *pkg;
    	struct uci_section *s;
    	int rv;
    
    	rv = uci_load(ctx, UCI_AGENT, &pkg);
    	if (rv)
    		return -1;
    
    	s = config_add_section(ctx, pkg, UCI_AGENT, "wifi-radio", "device",
    			device);
    	if (!s)
    		return -1;
    
    	if (band == BAND_2)
    		set_value(ctx, pkg, s, "band", "2", UCI_TYPE_STRING);
    	else if (band == BAND_5)
    		set_value(ctx, pkg, s, "band", "5", UCI_TYPE_STRING);
    
    	uci_commit(ctx, &pkg, false);
    	uci_unload(ctx, pkg);
    	return 0;
    }
    
    int config_generate_bsta_agent(struct agent_config *cfg, struct uci_context *ctx,
    
    		const char *device, const char *ifname,
    
    		uint8_t band)
    {
    	struct uci_section *s;
    	struct uci_package *pkg;
    	int rv;
    
    	rv = uci_load(ctx, UCI_AGENT, &pkg);
    	if (rv)
    		return -1;
    
    	s = config_add_section(ctx, pkg, UCI_AGENT, UCI_BK_AGENT, "device",
    			device);
    	if (!s)
    		return -1;
    
    	if (band == BAND_2)
    		set_value(ctx, pkg, s, "band", "2", UCI_TYPE_STRING);
    	else if (band == BAND_5)
    		set_value(ctx, pkg, s, "band", "5", UCI_TYPE_STRING);
    
    	set_value(ctx, pkg, s, "ifname", ifname, UCI_TYPE_STRING);
    	uci_commit(ctx, &pkg, false);
    	uci_unload(ctx, pkg);
    	return 0;
    }
    
    int config_find_radio(struct agent_config *cfg, struct uci_context *ctx,
    		char *radio)
    {
    	struct uci_package *pkg;
    	struct uci_section *s;
    	struct uci_element *e;
    	bool found = false;
    	int rv;
    
    	rv = uci_load(ctx, UCI_AGENT, &pkg);
    	if (rv)
    		return found;
    
    	s = config_get_section(ctx, pkg, "wifi-radio", "device", radio);
    	if (s)
    		found = true;
    
    	uci_unload(ctx, pkg);
    	return found;
    }
    
    static void wifi_radio_status_cb(struct ubus_request *req, int type,
    		struct blob_attr *msg)
    {
    	struct blob_attr *tb[1];
    	static const struct blobmsg_policy ap_attr[1] = {
    		[0] = { .name = "band", .type = BLOBMSG_TYPE_STRING },
    	};
    	char band_str[8] = {0};
    	uint8_t *band = req->priv;
    
    	blobmsg_parse(ap_attr, 1, tb, blob_data(msg), blob_len(msg));
    
    	if (!tb[0])
    		return;
    
    	strncpy(band_str, blobmsg_data(tb[0]), sizeof(band_str));
    
    	if (!strncmp(band_str, "5GHz", sizeof(band_str)))
    		*band = BAND_5;
    	else if (!strncmp(band_str, "2.4GHz", sizeof(band_str)))
    		*band = BAND_2;
    	else
    		*band = BAND_UNKNOWN;
    }
    
    
    void config_disable_bstas(struct agent_config *cfg)
    {
    	struct uci_context *ctx = NULL;
    	struct uci_package *pkg;
    	struct uci_section *section;
    	struct netif_bkcfg *bk;
    	bool reload = false;
    
    
    	list_for_each_entry(bk, &cfg->bklist, list) {
    		/* only disable onboarded bsta */
    //		if (!bk->cfg->onboarded)
    //			continue;
    
    		/* disable from wireless config */
    		pkg = uci_load_pkg(&ctx, UCI_WIRELESS);
    		if (!pkg)
    			continue;
    
    		section = config_get_section(ctx, pkg, UCI_WLAN_IFACE, "ifname", bk->name);
    		if (!section) {
    			uci_unload(ctx, pkg);
    			continue;
    		}
    
    		set_value(ctx, pkg, section, "disabled", "1", UCI_TYPE_STRING);
    		uci_save(ctx, pkg);
    		reload = true;
    	}
    
    	if (reload) {
    		uci_commit(ctx, &pkg, false);
    		uci_unload(ctx, pkg);
    	}
    
    
    	list_for_each_entry(bk, &cfg->bklist, list) {
    		pkg = uci_load_pkg(&ctx, UCI_AGENT);
    		if (!pkg)
    			continue;
    
    		section = config_get_section(ctx, pkg, UCI_BK_AGENT, "ifname", bk->name);
    		if (!section) {
    			uci_unload(ctx, pkg);
    			continue;
    		}
    
    		set_value(ctx, pkg, section, "enabled", "0", UCI_TYPE_STRING);
    		uci_save(ctx, pkg);
    		bk->enabled = 0;
    	}
    
    	if (reload) {
    		uci_commit(ctx, &pkg, false);
    		uci_reload_services("wireless");
    	}