From 5b224dcd3718ff7fa6ea2c3b503cf15e5b6454a7 Mon Sep 17 00:00:00 2001
From: Filip Matusiak <filip.matusiak@iopsys.eu>
Date: Mon, 9 Dec 2024 15:25:43 +0100
Subject: [PATCH] Reuse BH lost code

---
 src/agent.c    | 53 ++++++++++++++++++++++++--------------------------
 src/agent.h    |  1 +
 src/backhaul.c | 12 +-----------
 3 files changed, 27 insertions(+), 39 deletions(-)

diff --git a/src/agent.c b/src/agent.c
index fdd4a535e..fdd34ec19 100644
--- a/src/agent.c
+++ b/src/agent.c
@@ -4690,6 +4690,29 @@ static void wifi_bsta_event_scan_failed(struct agent *a, struct netif_bk *bk)
 	return;
 }
 
+void agent_handle_bh_lost(struct agent *a, struct netif_bk *bk)
+{
+	char ul_ifname[16] = {0};
+
+	if (!bk->cfg->enabled)
+		return;
+
+	dynbh_handle_bh_lost(a, bk);
+
+	if (agent_get_backhaul_ifname(a, ul_ifname)) {
+		if (!strncmp(ul_ifname, bk->ifname, IFNAMSIZ)) {
+			char ev[512] = {0};
+
+			sprintf(ev,
+				"{\"action\":\"unset_uplink\""
+				",\"type\":\"wifi\"}");
+
+			agent_notify_event(a, "map.agent", ev);
+			agent_exec_platform_scripts("unset_uplink wifi");
+		}
+	}
+}
+
 static void wifi_bsta_event_handler(void *agent, struct blob_attr *msg)
 {
 	char ifname[16] = {0}, event[16] = {0};
@@ -4835,24 +4858,7 @@ static void wifi_bsta_event_handler(void *agent, struct blob_attr *msg)
 
 		bk->connected = false;
 		/* Handle link loss if this was supposed to be an active bsta */
-		if (bk->cfg->enabled) {
-			char ul_ifname[16] = {0};
-
-			dynbh_handle_bh_lost(a, bk);
-
-			if (agent_get_backhaul_ifname(a, ul_ifname)) {
-				if (!strncmp(ul_ifname, bk->ifname, IFNAMSIZ)) {
-					char ev[512] = {0};
-
-					sprintf(ev,
-						"{\"action\":\"unset_uplink\""
-						",\"type\":\"wifi\"}");
-
-					agent_notify_event(a, "map.agent", ev);
-					agent_exec_platform_scripts("unset_uplink wifi");
-				}
-			}
-		}
+		agent_handle_bh_lost(a, bk);
 
 		wifi_mod_bridge(a, bk, false);
 		a->connected = agent_is_bsta_connected(a);
@@ -5921,16 +5927,7 @@ static void bk_toggle(struct agent *a, struct netif_bk *bk,
 		/* check if part of the bridge */
 		if (if_isbridge_interface(bk->ifname)) {
 			wifi_mod_bridge(a, bk, false);
-
-			if (bk->cfg->enabled) {
-				char ul_ifname[16] = {0};
-
-				dynbh_handle_bh_lost(a, bk);
-
-				if (agent_get_backhaul_ifname(a, ul_ifname)
-					&& !strncmp(ul_ifname, bk->ifname, IFNAMSIZ))
-					agent_exec_platform_scripts("unset_uplink wifi");
-			}
+			agent_handle_bh_lost(a, bk);
 		}
 	}
 }
diff --git a/src/agent.h b/src/agent.h
index 30a519c38..e225e7c01 100644
--- a/src/agent.h
+++ b/src/agent.h
@@ -1148,6 +1148,7 @@ void agent_set_post_scan_action_pref(struct agent *agent, struct wifi_radio_elem
 bool is_channel_supported_by_radio(struct wifi_radio_element *r,
 				   uint8_t opclass,
 				   uint8_t channel);
+void agent_handle_bh_lost(struct agent *a, struct netif_bk *bk);
 int agent_send_ch_scan_response(struct agent *a, struct wifi_radio_element *ndev,
 		struct wifi_scan_request_radio *req);
 bool agent_ch_scan_succesful(struct agent *a);
diff --git a/src/backhaul.c b/src/backhaul.c
index 50beae76e..ba28f8c81 100644
--- a/src/backhaul.c
+++ b/src/backhaul.c
@@ -619,18 +619,8 @@ void agent_manage_bsta(struct agent *a, struct netif_bk *bk)
 	} else {
 		if (if_isbridge_interface(ifname)) {
 			wifi_mod_bridge(a, bk, false);
-
 			/* Handle bk link loss if this is an active bsta */
-			if (bk->cfg->enabled) {
-				char ul_ifname[16] = {0};
-
-				dynbh_handle_bh_lost(a, bk);
-
-				if (agent_get_backhaul_ifname(a, ul_ifname)) {
-					if (!strncmp(ul_ifname, bk->ifname, IFNAMSIZ))
-						agent_exec_platform_scripts("unset_uplink wifi");
-				}
-			}
+			agent_handle_bh_lost(a, bk);
 		}
 	}
 }
-- 
GitLab