From aaed6aef448cb6777a0fffeb1a058fb5d6a848c7 Mon Sep 17 00:00:00 2001
From: Jakob Olsson <jakob.olsson@iopsys.eu>
Date: Tue, 10 Jun 2025 17:09:43 +0200
Subject: [PATCH] dynbh: wip - hacky working dump

---
 src/agent.c            |  28 ++++----
 src/agent.h            |   3 +
 src/agent_cmdu.c       |   7 +-
 src/agent_map.c        |  28 ++++++--
 src/agent_map.h        |   1 -
 src/dynbh/dbh_config.c |  27 --------
 src/dynbh/dbh_config.h |   3 +-
 src/dynbh/dbh_nl.c     |  32 ++++-----
 src/dynbh/dbh_nl.h     |   4 +-
 src/dynbh/dynbh.c      | 150 +++++++++++------------------------------
 src/dynbh/dynbh.h      |   5 +-
 src/script/multiap     |  53 ++++++++++++---
 12 files changed, 151 insertions(+), 190 deletions(-)

diff --git a/src/agent.c b/src/agent.c
index d109f4dde..409fab218 100644
--- a/src/agent.c
+++ b/src/agent.c
@@ -815,15 +815,16 @@ int agent_config_reload(struct agent *a)
 	ret = agent_config_load(&a->cfg);
 	agent_config_load_post_action(a);
 
-	snprintf(ev, sizeof(ev), "{"\
-			"\"event\":\"configuration\","\
-			"\"data\": {"\
-				"\"action\":\"%s\","\
-				"\"status\":\"%s\","\
-			"}"\
-		"}", "reload", "success");
-
-
+#ifdef PERSIST_CONTROLLER
+	if (is_local_cntlr_available()
+	    && !is_local_cntlr_running()
+	    && a->priv.discovery_mode == DYNAMIC_CNTLR_MODE_CONTROLLER) {
+		set_value_by_string("mapcontroller",
+					"controller", "enabled",
+					"1", UCI_TYPE_STRING);
+		runCmd("/etc/init.d/mapcontroller reload");
+	}
+#endif
 	agent_notify_event(a, "map.agent", ev);
 	return ret;
 }
@@ -7432,9 +7433,9 @@ static void agent_dispatch_autoconfig(atimer_t *t)
 		if (!cmdu)
 			continue;
 
-		trace("|%s:%d| Sending Autoconfig Search for radio %s(%s)\n",
+		trace("|%s:%d| Sending Autoconfig Search for radio %s(%sGHz), ieee1905band:%d\n",
 				__func__, __LINE__, re->name,
-				(re->band == BAND_2 ? "2.4GHz" : "5GHz"));
+				band_to_str(re->band), ieee1905band);
 		re->mid = agent_send_cmdu(a, cmdu);
 
 		cmdu_free(cmdu);
@@ -8127,7 +8128,7 @@ void run_agent(void *opts)
 
 #ifdef DYNBH
 #ifdef PERSIST_CONTROLLER
-	dbh_config_load(priv);
+	dbh_config_load(&w->priv);
 #endif
 	int num = 0;
 	char ifs[16][16] = {0};
@@ -8135,7 +8136,10 @@ void run_agent(void *opts)
 
 	memcpy(w->priv.almac, w->almac, 6);
 	w->priv.ubus_ctx = ctx;
+	dynbh_publish_object(&w->priv, "dynbh");
+
 	strncpy(w->priv.al_bridge, w->cfg.al_bridge, sizeof(w->priv.al_bridge) - 1);
+	dynbh_register_nlevents(&w->priv);
 	INIT_LIST_HEAD(&w->priv.ethportlist);
 	get_ethportslist(&num, ifs);
 	for (i = 0; i < num; i++)
diff --git a/src/agent.h b/src/agent.h
index 758b5b2bb..f64b23f31 100644
--- a/src/agent.h
+++ b/src/agent.h
@@ -24,7 +24,10 @@
 #include <stdbool.h>
 #include <wifidefs.h>
 
+#ifdef DYNBH
 #include "dynbh/dynbh.h"
+#include "dynbh/dbh_nl.h"
+#endif
 #include "utils/allmac.h"
 #include "utils/debug.h"
 #include "config.h"
diff --git a/src/agent_cmdu.c b/src/agent_cmdu.c
index 9d32c252c..ec7949eb2 100644
--- a/src/agent_cmdu.c
+++ b/src/agent_cmdu.c
@@ -41,11 +41,10 @@
 struct cmdu_buff *agent_gen_ap_autoconfig_search(struct agent *a,
 		uint8_t ieee1905band, uint8_t profile)
 {
+	uint8_t origin[6] = {0x01, 0x80, 0xc2, 0x00, 0x00, 0x13};
 	struct cmdu_buff *frm = NULL;
-	int ret = 0;
-	uint8_t band = 0;
 	uint16_t mid = 0;
-	uint8_t origin[6] = {0x01, 0x80, 0xc2, 0x00, 0x00, 0x13};
+	int ret = 0;
 
 	frm = cmdu_alloc_simple(CMDU_TYPE_AP_AUTOCONFIGURATION_SEARCH, &mid);
 	if (!frm) {
@@ -63,7 +62,7 @@ struct cmdu_buff *agent_gen_ap_autoconfig_search(struct agent *a,
 	if (ret)
 		goto out;
 
-	ret = agent_gen_autoconf_freq_band(a, frm, band);
+	ret = agent_gen_autoconf_freq_band(a, frm, ieee1905band);
 	if (ret)
 		goto out;
 
diff --git a/src/agent_map.c b/src/agent_map.c
index 868d638f6..6ca28ad01 100644
--- a/src/agent_map.c
+++ b/src/agent_map.c
@@ -1330,6 +1330,10 @@ int handle_ap_autoconfig_search(void *agent, struct cmdu_buff *rx_cmdu,
 		return -1;
 	}
 
+#ifdef DYNBH
+	dbh_handle_autoconfig_search(&a->priv, rx_cmdu->dev_ifname, cmdu_get_mid(rx_cmdu), tv);
+#endif
+
 	/* Discard autoconfig search in case it's been sent by ourself */
 	list_for_each_entry(re, &a->radiolist, list) {
 		if (re->mid == cmdu_get_mid(rx_cmdu)) {
@@ -1375,7 +1379,7 @@ int handle_ap_autoconfig_search(void *agent, struct cmdu_buff *rx_cmdu,
 			}
 		}
 	}
-	dbh_handle_autoconfig_search(&a->priv, rx_cmdu->dev_ifname, cmdu_get_mid(rx_cmdu), tv);
+
 	return 0;
 }
 
@@ -1431,6 +1435,22 @@ int handle_ap_autoconfig_response(void *agent, struct cmdu_buff *rx_cmdu,
 	uint8_t *hash;
 #endif
 
+#ifdef DYNBH
+	struct tlv *t;
+	t = map_cmdu_get_tlv(rx_cmdu, MAP_TLV_SUPPORTED_SERVICE);
+	if (!t) {
+		dbg("|%s:%d| Malformed topology notification!\n", __func__,
+		    __LINE__);
+		return -1;
+	}
+
+
+	dbh_handle_autoconfig_response(&a->priv, rx_cmdu->dev_ifname, cmdu_get_mid(rx_cmdu), t);
+#endif
+
+	if (!n)
+		return -1;
+
 	agent_set_link_profile(a, n, rx_cmdu);
 
 	if (!map_cmdu_validate_parse(rx_cmdu, tv, ARRAY_SIZE(tv), n->map_profile)) {
@@ -1619,7 +1639,6 @@ int handle_ap_autoconfig_response(void *agent, struct cmdu_buff *rx_cmdu,
 		agent_reschedule_heartbeat_autocfg(a, HEARTBEAT_AUTOCFG_INTERVAL);
 	}
 
-	dbh_handle_autoconfig_response(&a->priv, rx_cmdu->dev_ifname, cmdu_get_mid(rx_cmdu), tv);
 	return 0;
 }
 
@@ -7131,16 +7150,15 @@ int agent_handle_map_event(struct agent *a, uint16_t cmdutype, uint16_t mid,
 	if (!n) {
 		if (cmdutype != CMDU_TYPE_TOPOLOGY_DISCOVERY &&
 		    cmdutype != CMDU_TYPE_TOPOLOGY_NOTIFICATION &&
-		    cmdutype != CMDU_TYPE_AP_AUTOCONFIGURATION_SEARCH)
+		    cmdutype != CMDU_TYPE_AP_AUTOCONFIGURATION_SEARCH &&
+		    cmdutype != CMDU_TYPE_AP_AUTOCONFIGURATION_RESPONSE)
 			return -1;
 	}
 
 	if (f[idx]) {
-		dbg("mid pointer prev = %u src = "MACFMT"\n", mid, MAC2STR(src));
 		cmdu = cmdu_alloc_custom(cmdutype, &mid, rxif, src, tlvs, len);
 		if (cmdu) {
 			memcpy(cmdu->origin, origin, 6);
-			dbg("mid pointer post = %u src = "MACFMT"\n", mid, MAC2STR(cmdu->origin));
 			dbg("%s: cmdu_alloc_custom() succeeded! cmdu->cdata->hdr.mid %u\n", __func__, cmdu_get_mid(cmdu));
 			ret = f[idx](a, cmdu, n);
 			cmdu_free(cmdu);
diff --git a/src/agent_map.h b/src/agent_map.h
index 80143fc80..3462322cf 100644
--- a/src/agent_map.h
+++ b/src/agent_map.h
@@ -522,7 +522,6 @@ struct netif_ap *wifi_get_first_available_ifname(struct agent *a, struct wifi_ra
 
 
 uint16_t agent_send_cmdu(struct agent *a, struct cmdu_buff *cmdu);
-int agent_send_cmdu_ubus(struct agent *a, struct cmdu_buff *cmdu);
 int send_channel_sel_response(void *agent, struct cmdu_buff *cmdu,
 		struct channel_response *rec_cmdu, uint32_t channel_response_nr);
 int agent_fill_radio_max_preference(void *agent,
diff --git a/src/dynbh/dbh_config.c b/src/dynbh/dbh_config.c
index 6d35b04fa..770368930 100644
--- a/src/dynbh/dbh_config.c
+++ b/src/dynbh/dbh_config.c
@@ -7,33 +7,6 @@
 #include "dynbh.h"
 
 #if PERSIST_CONTROLLER
-int set_value_by_string(const char *package, const char *section,
-		const char *key, const char *value, enum uci_option_type type)
-{
-	struct uci_ptr ptr = {0};
-	struct uci_context *ctx;
-	int rv = 0;
-
-	ctx = uci_alloc_context();
-	if (!ctx)
-		return -1;
-
-	ptr.package = package;
-	ptr.section = section;
-	ptr.option = key;
-	ptr.value = value;
-
-	if (type == UCI_TYPE_STRING)
-		rv = uci_set(ctx, &ptr);
-
-	if (type == UCI_TYPE_LIST)
-		rv = uci_add_list(ctx, &ptr);
-
-	uci_commit(ctx, &ptr.p, false);
-
-	uci_free_context(ctx);
-	return rv;
-}
 
 static int dbh_config_get_controller_select(struct dynbh_ctx *ctx,
 				       struct uci_section *s)
diff --git a/src/dynbh/dbh_config.h b/src/dynbh/dbh_config.h
index ff6ea7383..f6d192b64 100644
--- a/src/dynbh/dbh_config.h
+++ b/src/dynbh/dbh_config.h
@@ -6,8 +6,7 @@
 
 struct dynbh_ctx;
 #if PERSIST_CONTROLLER
-int set_value_by_string(const char *package, const char *section,
-		const char *key, const char *value, enum uci_option_type type);
+
 int dbh_config_load(struct dynbh_ctx *cfg);
 #endif
 #endif
diff --git a/src/dynbh/dbh_nl.c b/src/dynbh/dbh_nl.c
index 5d31ff2e0..9b3d5866b 100644
--- a/src/dynbh/dbh_nl.c
+++ b/src/dynbh/dbh_nl.c
@@ -38,21 +38,21 @@
 struct nl_msg;
 
 
-struct i1905_nlevent {
+struct dynbh_nlevent {
 	struct uloop_fd uloop;
-	void (*error_cb)(struct i1905_nlevent *e, int error);
-	void (*event_cb)(struct i1905_nlevent *e);
+	void (*error_cb)(struct dynbh_nlevent *e, int error);
+	void (*event_cb)(struct dynbh_nlevent *e);
 };
 
 struct event_socket {
-	struct i1905_nlevent ev;
+	struct dynbh_nlevent ev;
 	struct nl_sock *sock;
 	int sock_bufsize;
 };
 
-static int i1905_nlevents_cb(struct nl_msg *msg, void *arg);
+static int dynbh_nlevents_cb(struct nl_msg *msg, void *arg);
 
-static void handle_error(struct i1905_nlevent *e, int error)
+static void handle_error(struct dynbh_nlevent *e, int error)
 {
 	struct event_socket *ev_sock = container_of(e, struct event_socket, ev);
 
@@ -70,7 +70,7 @@ err:
 	uloop_fd_delete(&e->uloop);
 }
 
-static void recv_nlevents(struct i1905_nlevent *e)
+static void recv_nlevents(struct dynbh_nlevent *e)
 {
 	struct event_socket *ev_sock = container_of(e, struct event_socket, ev);
 
@@ -88,7 +88,7 @@ static struct event_socket rtnl_event = {
 };
 
 
-static int i1905_handle_nlevents_link(struct dynbh_ctx *priv,
+static int dynbh_handle_nlevents_link(struct dynbh_ctx *priv,
 				      struct nlmsghdr *hdr, bool add)
 {
 	struct ifinfomsg *ifi = nlmsg_data(hdr);
@@ -148,7 +148,7 @@ static int i1905_handle_nlevents_link(struct dynbh_ctx *priv,
 	return NL_OK;
 }
 
-static int i1905_nlevents_cb(struct nl_msg *msg, void *arg)
+static int dynbh_nlevents_cb(struct nl_msg *msg, void *arg)
 {
 	struct nlmsghdr *hdr = nlmsg_hdr(msg);
 	struct dynbh_ctx *priv = arg;
@@ -160,7 +160,7 @@ static int i1905_nlevents_cb(struct nl_msg *msg, void *arg)
 		add = true;
 	//case RTM_DELLINK:
 		fprintf(stderr, "%s!\n", (add ? "newlink":"dellink"));
-		ret = i1905_handle_nlevents_link(priv, hdr, add);
+		ret = dynbh_handle_nlevents_link(priv, hdr, add);
 		break;
 	default:
 		break;
@@ -170,9 +170,9 @@ static int i1905_nlevents_cb(struct nl_msg *msg, void *arg)
 }
 
 
-static void i1905_receive_nlevents(struct uloop_fd *u, unsigned int events)
+static void dynbh_receive_nlevents(struct uloop_fd *u, unsigned int events)
 {
-	struct i1905_nlevent *e = container_of(u, struct i1905_nlevent, uloop);
+	struct dynbh_nlevent *e = container_of(u, struct dynbh_nlevent, uloop);
 
 	if (u->error) {
 		int ret = -1;
@@ -191,7 +191,7 @@ static void i1905_receive_nlevents(struct uloop_fd *u, unsigned int events)
 	}
 }
 
-int i1905_register_nlevents(struct dynbh_ctx *priv)
+int dynbh_register_nlevents(struct dynbh_ctx *priv)
 {
 	struct nl_sock *sk;
 
@@ -218,14 +218,14 @@ int i1905_register_nlevents(struct dynbh_ctx *priv)
 	nl_socket_disable_seq_check(rtnl_event.sock);
 
 	nl_socket_modify_cb(rtnl_event.sock, NL_CB_VALID, NL_CB_CUSTOM,
-			    i1905_nlevents_cb, priv);
+			    dynbh_nlevents_cb, priv);
 
 	if (nl_socket_add_memberships(rtnl_event.sock,
 				      RTNLGRP_NEIGH, RTNLGRP_LINK, 0))
 		goto out_err;
 
 	rtnl_event.ev.uloop.fd = nl_socket_get_fd(rtnl_event.sock);
-	rtnl_event.ev.uloop.cb = i1905_receive_nlevents;
+	rtnl_event.ev.uloop.cb = dynbh_receive_nlevents;
 	uloop_fd_add(&rtnl_event.ev.uloop, ULOOP_READ |
 		     ((rtnl_event.ev.error_cb) ? ULOOP_ERROR_CB : 0));
 	fprintf(stderr, "netlink success!\n");
@@ -242,7 +242,7 @@ out_err:
 	return -1;
 }
 
-void i1905_unregister_nlevents(struct dynbh_ctx *priv)
+void dynbh_unregister_nlevents(struct dynbh_ctx *priv)
 {
 	UNUSED(priv);
 
diff --git a/src/dynbh/dbh_nl.h b/src/dynbh/dbh_nl.h
index ead974e78..617b45c1f 100644
--- a/src/dynbh/dbh_nl.h
+++ b/src/dynbh/dbh_nl.h
@@ -4,8 +4,8 @@
 
 struct dynbh_ctx;
 
-int i1905_register_nlevents(struct dynbh_ctx *priv);
-void i1905_unregister_nlevents(struct dynbh_ctx *priv);
+int dynbh_register_nlevents(struct dynbh_ctx *priv);
+void dynbh_unregister_nlevents(struct dynbh_ctx *priv);
 
 #endif
 #endif
diff --git a/src/dynbh/dynbh.c b/src/dynbh/dynbh.c
index e3dc26e2c..25499c921 100644
--- a/src/dynbh/dynbh.c
+++ b/src/dynbh/dynbh.c
@@ -22,6 +22,8 @@
 
 #include "agent.h"
 #include "agent_cmdu.h"
+#include "agent_map.h"
+//#include "agent_map.h"
 #include "backhaul.h"
 #include "1905_ubus.h"
 #include "dbh_config.h"
@@ -35,8 +37,8 @@
 #endif
 
 #define map_plugin			IEEE1905_OBJECT_MULTIAP
-#define HEARTBEAT_PROBE_TIMEOUT 	30
-#define APCONF_MAX_RETRIES 		5
+#define HEARTBEAT_PROBE_TIMEOUT 	60
+#define APCONF_MAX_RETRIES 		3
 #define APCONF_INTERVAL 		2
 
 
@@ -67,8 +69,10 @@ static void issue_discovery(struct dynbh_ctx *priv, struct ethport *port)
 
 	resp = agent_gen_topology_discovery(a, priv->almac, port->macaddr);
 	if (resp) {
-	//	agent_send_cmdu(a, resp);
-	//	cmdu_free(resp);
+		uint16_t mid = 0;
+
+		ieee1905_ubus_send_al_cmdu(priv->ubus_ctx, resp, &mid, port->ifname, a->pvid);
+		cmdu_free(resp);
 	}
 }
 
@@ -90,21 +94,23 @@ static void i1905_modif(struct dynbh_ctx *priv, struct ethport *port, bool add)
 	const char *fn = (add ? "add_interface" : "del_interface");
 	int ret;
 	struct blob_buf bb = {0};
-	//char objname[32];
+	char objname[32];
 	//TODO: IF OBJECT ALREADY EXISTS AND ADDIF NOP
 
-	//snprintf(objname, sizeof(objname), "ieee1905.al.%s", port->ifname);
+	snprintf(objname, sizeof(objname), "ieee1905.al.%s", port->ifname);
+	uobj_id = ubus_get_object(priv->ubus_ctx, objname);
+	if ((uobj_id == WIFI_OBJECT_INVALID && !add) ||
+	    (uobj_id != WIFI_OBJECT_INVALID && add)) {
+		err("%s: error case, objid:%u add:%d objname:%s\n", __func__, uobj_id, add, objname);
+		return;
+	}
 
 	uobj_id = ubus_get_object(priv->ubus_ctx, "ieee1905");
 	if (uobj_id == WIFI_OBJECT_INVALID) {
 		err("%s: ieee1905 not present\n", __func__);
 		return;
 	}
-	//if (uobj_id == WIFI_OBJECT_INVALID && add
-	//    || uobj_id != WIFI_OBJECT_INVALID && !del) {
-	//	err("%s not present! skipping '%s' from config\n",
-	//					objname, f->name);
-	//	continue;
+
 	blob_buf_init(&bb, 0);
 	blobmsg_add_string(&bb, "ifname", port->ifname);
 	ret = ubus_call_object_args(priv->ubus_ctx, uobj_id, &bb, fn, NULL, NULL);
@@ -132,19 +138,28 @@ static void i1905_delif(struct dynbh_ctx *priv, struct ethport *port)
  */
 static void link_up(struct ethport *port)
 {
+	char fmt[64] = {0};
+	err("|%s:%d|\n", __func__, __LINE__);
+	snprintf(fmt, sizeof(fmt), "/lib/wifi/multiap bstas_down");
 	err("|%s:%d|\n", __func__, __LINE__);
-	runCmd("/lib/wifi/dynbhd/api up %s", port->ifname);
+	//runCmd("/lib/wifi/dynbhd/api up %s", port->ifname);
+	runCmd(fmt);
+	err("|%s:%d| port:%s\n", __func__, __LINE__, port->ifname);
 	runCmd("/lib/wifi/multiap set_uplink eth %s", port->ifname);
+	err("|%s:%d|\n", __func__, __LINE__);
 }
 
 /**
  * remove /var/run/multiap/map.agent.bsta_global_disable
  * enable all bstas
  */
-static void link_down(void)
+static void link_down(struct ethport *port)
 {
+	char fmt[64] = {0};
+
 	err("|%s:%d|\n", __func__, __LINE__);
-	runCmd("/lib/wifi/dynbhd/api down");
+	snprintf(fmt, sizeof(fmt), "/lib/wifi/multiap bsta_up %s", port->ifname);
+	runCmd(fmt);
 }
 
 #ifdef PERSIST_CONTROLLER
@@ -233,7 +248,7 @@ static void bridge_readd(atimer_t *t)
 	int timeout = 5 * port->retries;
 
 	if (port->active_uplink)
-		link_down();
+		link_down(port);
 
 	err("|%s:%d| link timed out for iface:%s mid:%d, %d, add back to bridge\n", __func__, __LINE__, port->ifname, port->mid[0], port->mid[1]);
 	port->loop = false;
@@ -361,7 +376,7 @@ void dynbh_handle_port_down(struct dynbh_ctx *priv, struct ethport *port)
 		}
 
 		if (!found) {
-			link_down();
+			link_down(port);
 			runCmd("/lib/wifi/multiap unset_uplink eth");
 		}
 	}
@@ -419,108 +434,21 @@ void free_ethport_search(struct ethport *port)
 	free(port);
 }
 
-#ifdef PERSIST_CONTROLLER
-/* { "map.agent": {"event":"configuration","data":{"action":"reload","status":"success"}} }
- */
-/* mapagent event handler */
-static void mapagent_event_handler(void *agent, struct blob_attr *msg)
-{
-	char event[32] = {0};
-	struct dynbh_ctx *a = (struct dynbh_ctx *) agent;
-	struct blob_attr *tb[2];
-	static const struct blobmsg_policy ev_attr[2] = {
-		[0] = { .name = "event", .type = BLOBMSG_TYPE_STRING },
-		[1] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
-	};
-
-	blobmsg_parse(ev_attr, 2, tb, blob_data(msg), blob_len(msg));
-
-	if (!tb[0] || !tb[1])
-		return;
-
-	strncpy(event, blobmsg_data(tb[0]), sizeof(event) - 1);
-	if (!strncmp(event, "configuration", sizeof(event))) {
-		struct blob_attr *tb2[2];
-		char action[16] = {0};
-		static const struct blobmsg_policy conf_attrs[2] = {
-			[0] = { .name = "action", .type = BLOBMSG_TYPE_STRING },
-			[1] = { .name = "status", .type = BLOBMSG_TYPE_STRING },
-		};
-
-		blobmsg_parse(conf_attrs, 2, tb2,
-			      blobmsg_data(tb[1]), blobmsg_data_len(tb[1]));
-
-		if (!tb2[0] || !tb2[1]) {
-			err("|%s:%d| event parse error.\n", __func__, __LINE__);
-			return;
-		}
-
-		strncpy(action, blobmsg_data(tb2[0]), sizeof(action) - 1);
-		if (!strncmp(action, "reload", sizeof(action))) {
-			if (is_local_cntlr_available()
-			    && !is_local_cntlr_running()
-			    && a->discovery_mode == DYNAMIC_CNTLR_MODE_CONTROLLER) {
-				set_value_by_string("mapcontroller",
-						    "controller", "enabled",
-						    "1", UCI_TYPE_STRING);
-				runCmd("/etc/init.d/mapcontroller reload");
-			}
-		}
-	}
-}
-#endif
-
-#ifdef PERSIST_CONTROLLER
-static void agent_event_handler(struct ubus_context *ctx,
-		struct ubus_event_handler *ev,
-		const char *type, struct blob_attr *msg)
-{
-	int i;
-	char *str;
-	struct dynbh_ctx *a = container_of(ev, struct dynbh_ctx, evh);
-	struct wifi_ev_handler {
-		const char *ev_type;
-		void (*handler)(void *ctx, struct blob_attr *ev_data);
-	} evs[] = {
-		{ "map.agent", mapagent_event_handler },
-	};
-
-
-	str = blobmsg_format_json(msg, true);
-	if (!str)
-		return;
-
-	err("[ &agent = %p ] Received [event = %s]  [val = %s]\n",
-			a, type, str);
-	for (i = 0; i < ARRAY_SIZE(evs); i++) {
-		if (!strcmp(type, evs[i].ev_type)) {
-			evs[i].handler(a, msg);
-			break;
-		}
-	}
-
-	free(str);
-}
-#endif
-
-
 int dbh_handle_autoconfig_response(struct dynbh_ctx *priv, char *ifname,
-				      uint16_t mid, struct tlv *tv[][TLV_MAXNUM])
+				      uint16_t mid, struct tlv *tv)
 {
 	struct agent *a = container_of(priv, struct agent, priv);
-	char almac_str[18] = {0};
 	struct ethport *port;
 	bool has_cntlr = false;
 	int timeout = HEARTBEAT_PROBE_TIMEOUT;
 
 	err("mapclient: %s ===>\n", __func__);
 
-	if (tv[1][0]->data[0] > 0) {
+	if (tv->data[0] > 0) {
 		int i;
 
-		for (i = 0; i < tv[1][0]->data[0]; i++) {
-			if (tv[1][0]->data[(i+1)] ==
-					SUPPORTED_SERVICE_MULTIAP_CONTROLLER) {
+		for (i = 0; i < tv->data[0]; i++) {
+			if (tv->data[(i+1)] == SUPPORTED_SERVICE_MULTIAP_CONTROLLER) {
 				has_cntlr = true;
 				break;
 			}
@@ -603,11 +531,11 @@ int dbh_handle_autoconfig_search(struct dynbh_ctx *priv, char *ifname, uint16_t
 
 	err("mapclient: %s ===>\n", __func__);
 
-	if (tv[3][0]->data[0] > 0) {
+	if (tv[AP_AUTOCONFIGURATION_SEARCH_SUPPORTED_SERVICE_IDX][0]->data[0] > 0) {
 		int i;
 
-		for (i = 0; i < tv[3][0]->data[0]; i++) {
-			if (tv[3][0]->data[(i+1)] ==
+		for (i = 0; i < tv[AP_AUTOCONFIGURATION_SEARCH_SUPPORTED_SERVICE_IDX][0]->data[0]; i++) {
+			if (tv[AP_AUTOCONFIGURATION_SEARCH_SUPPORTED_SERVICE_IDX][0]->data[(i+1)] ==
 					SUPPORTED_SERVICE_MULTIAP_CONTROLLER) {
 				has_cntlr = true;
 				break;
@@ -731,7 +659,7 @@ static int dynbhd_status(struct ubus_context *ctx, struct ubus_object *obj,
 }
 
 
-int cntlr_publish_object(struct dynbh_ctx *c, const char *objname)
+int dynbh_publish_object(struct dynbh_ctx *c, const char *objname)
 {
 	struct ubus_object *obj;
 	struct ubus_object_type *obj_type;
diff --git a/src/dynbh/dynbh.h b/src/dynbh/dynbh.h
index 1d381aaa5..3dfc107d8 100644
--- a/src/dynbh/dynbh.h
+++ b/src/dynbh/dynbh.h
@@ -51,6 +51,9 @@ struct dynbh_ctx {
 
 };
 
+int dynbh_publish_object(struct dynbh_ctx *c, const char *objname);
+
+
 void delif(struct dynbh_ctx *priv, struct ethport *port);
 void addif(struct ethport *port);
 
@@ -66,7 +69,7 @@ void dynbh_handle_port_down(struct dynbh_ctx *priv, struct ethport *port);
 void free_ethport_search(struct ethport *port);
 
 int dbh_handle_autoconfig_response(struct dynbh_ctx *priv, char *ifname,
-				      uint16_t mid, struct tlv *tv[][TLV_MAXNUM]);
+				      uint16_t mid, struct tlv *tv);
 int dbh_handle_autoconfig_search(struct dynbh_ctx *priv, char *ifname, uint16_t mid, struct tlv *tv[][TLV_MAXNUM]);
 
 #endif
diff --git a/src/script/multiap b/src/script/multiap
index bdba28822..5fb965c2a 100755
--- a/src/script/multiap
+++ b/src/script/multiap
@@ -70,15 +70,22 @@ setup_conf() {
 	map_genconf
 }
 
-bsta_disable() {
-	local ifname=$1
-
-	[ -n "$ifname" ] || return
-
+bsta_updown() {
+	local ifname="$1"
+	local action="$2"
+	local enabled="$3"
+	echo "3 $@" > /dev/console
 	network_id=$(get_network_id $ifname)
-	wpa_cli -i "$ifname" disconnect > /dev/console
-	wpa_cli -i "$ifname" disable_network $network_id > /dev/console
-	wpa_cli -i "$ifname" save_config > /dev/console
+	if [ "$action" = "down" ]; then
+		echo "4" > /dev/console
+		wpa_cli -i "$ifname" disconnect > /dev/null 2>&1
+		wpa_cli -i "$ifname" disable_network $network_id  > /dev/null 2>&1
+	elif [ "$action" = "up" ]; then
+		[ "$enabled" -eq 0 ] && return
+		echo "5" > /dev/console
+		wpa_cli -i "$ifname" reconnect > /dev/null 2>&1
+		wpa_cli -i "$ifname" enable_network $network_id  > /dev/null 2>&1
+	fi
 }
 
 sync_credentials() {
@@ -521,7 +528,7 @@ bsta_steer_with_disassoc() {
 		json_cleanup
 		echo $bk_ifname
 	) 200>/var/lock/map.backhaul.lock
-	bsta_disable $bk_ifname
+	bsta_updown "$bk_ifname" down
 	bsta_steer $ifname $bssid
 }
 
@@ -1537,6 +1544,32 @@ reset() {
 	/etc/init.d/mapagent start > /dev/null 2>&1
 }
 
+bstas_up() {
+	update_bstas() {
+		local section="$1"
+
+		config_get ifname "$section" ifname
+		config_get_bool enabled "$section" enabled 0
+		bsta_updown "$ifname" "up" $enabled
+	}
+	config_load "mapagent"
+	config_foreach update_bstas bsta
+}
+
+bstas_down() {
+	update_bstas() {
+		local section="$1"
+		echo "2" > /dev/console
+		config_get ifname "$section" ifname
+		config_get_bool enabled "$section" enabled 0
+		bsta_updown "$ifname" "down" $enabled
+	}
+	echo "1" > /dev/console
+	rm -f "$MAPFILE"
+	config_load "mapagent"
+	config_foreach update_bstas bsta
+}
+
 
 func=$1
 shift
@@ -1580,6 +1613,8 @@ case "$func" in
 	set_ap_psk) set_ap_psk $@;;
 	config_reorder) config_reorder $@;;
 	reset) reset $@;;
+	bstas_up) bstas_up $@;;
+	bstas_down) bstas_down $@;;
 	--help|help) usage;;
 	*) usage; exit 1;;
 esac
-- 
GitLab