From cbdf8737bd807b6c6bc12376bcd8612ce47bfc92 Mon Sep 17 00:00:00 2001
From: Jakob Olsson <jakob.olsson@iopsys.eu>
Date: Fri, 9 May 2025 15:28:12 +0200
Subject: [PATCH] scan: fix reporting stored scan results

---
 src/agent_cmdu.c | 61 +++++++++++++++++++-----------------------------
 src/agent_map.c  | 19 ++++++++++-----
 2 files changed, 37 insertions(+), 43 deletions(-)

diff --git a/src/agent_cmdu.c b/src/agent_cmdu.c
index c7b2e69ef..49ea9be17 100644
--- a/src/agent_cmdu.c
+++ b/src/agent_cmdu.c
@@ -806,13 +806,8 @@ static int agent_gen_ch_scan_response_all(struct agent *a, struct cmdu_buff *cmd
 {
 	trace("%s --->\n", __func__);
 
-	int i, j, ret;
 	struct wifi_scanres_element *sl;
-	struct wifi_scanres_opclass_element *op;
-	struct wifi_scanres_channel_element *ch;
-	int num_tlv = 0;
-	int num_bw_20 = 0;
-	bool results_missing = false;
+	int i, ret;
 
 	if (!a || !cmdu || !re)
 		return -1;
@@ -828,45 +823,37 @@ static int agent_gen_ch_scan_response_all(struct agent *a, struct cmdu_buff *cmd
 	if (WARN_ON(!sl))
 		return -1;
 
-	do {
-		/* do: second iteration to send empty TLVs for all supported opc/ch
-		 * pairs in case there were no nbrs cached on any opc/ch pair.
-		 */
-		for (i = 0; i < sl->num_opclass; i++) {
-			op = sl->opclass_scanlist + i;
+	for (i = 0; i < sl->num_opclass; i++) {
+		struct wifi_scanres_opclass_element *op;
+		int j;
 
-			if (op->bandwidth != 20) /* caps */
-				continue;
-			else
-				num_bw_20++;
+		op = sl->opclass_scanlist + i;
 
-			for (j = 0; j < op->num_channels; j++) {
-				ch = op->channel_scanlist + j;
+		if (op->bandwidth != 20) /* caps */
+			continue;
 
-				if (!ch->num_neighbors && !results_missing)
-					/* report only non-empty results */
-					continue;
+		for (j = 0; j < op->num_channels; j++) {
+			struct wifi_scanres_channel_element *ch;
 
-				ret = agent_gen_ch_scan_response_tlv(a, cmdu,
-							re->macaddr, op->opclass,
-							ch, status);
+			ch = op->channel_scanlist + j;
 
-				if (ret)
-					return ret;
-
-				dbg("|%s:%d| Added Channel Scan Result TLV.\n",
-				    __func__, __LINE__);
-				num_tlv++;
+			if (!wifi_opclass_id_channel_supported(&re->opclass,
+							       op->opclass,
+							       ch->channel)) {
+				/* skip channel(s) not supported */
+				continue;
 			}
-		}
 
-		if (!num_tlv) {
-			dbg("|%s:%d| No Scan Results found.\n",
-			    __func__, __LINE__);
-			/* do send empty TLVs for all supported opc/ch pairs */
-			results_missing = true;
+			ret = agent_gen_ch_scan_response_tlv(a, cmdu,
+						re->macaddr, op->opclass,
+						ch, status);
+			if (ret)
+				return ret;
+
+			dbg("|%s:%d| Added Channel Scan Result TLV.\n",
+				__func__, __LINE__);
 		}
-	} while (!num_tlv && num_bw_20);
+	}
 
 	return 0;
 }
diff --git a/src/agent_map.c b/src/agent_map.c
index 25a2c6f0e..cedc3f5d5 100644
--- a/src/agent_map.c
+++ b/src/agent_map.c
@@ -5627,6 +5627,18 @@ int handle_channel_scan_request(void *agent, struct cmdu_buff *rx_cmdu,
 			continue;
 		}
 
+		if (!(ch_scan_req.mode & SCAN_REQUEST_FRESH_SCAN)) {
+			dbg("%s: return scan cache for radio:%s\n", __func__, re->name);
+			if (a->cfg.scan_on_boot_only &&
+			    re->scan_state != SCAN_DONE) {
+				/* Some boot scan results missing yet */
+				scan_req->status = CH_SCAN_STATUS_SCAN_NOT_COMPLETED;
+			} else
+				scan_req->status = CH_SCAN_STATUS_SUCCESS;
+			ret = agent_send_ch_scan_response(a, re, scan_req);
+			continue;
+		}
+
 		if (scan_req->num_opclass <= 0) {
 			dbg("%s: no opclass provided, dropping scan req for radio:"MACFMT"\n",
 			    __func__, MAC2STR(scan_req->radio));
@@ -5644,12 +5656,7 @@ int handle_channel_scan_request(void *agent, struct cmdu_buff *rx_cmdu,
 		}
 		/* Do not 'Perform Fresh Scan' */
 		else if (!(ch_scan_req.mode & SCAN_REQUEST_FRESH_SCAN)) {
-			if (a->cfg.scan_on_boot_only
-					&& re->scan_state != SCAN_DONE)
-				/* Some boot scan results missing yet */
-				scan_req->status = CH_SCAN_STATUS_SCAN_NOT_COMPLETED;
-			else
-				scan_req->status = CH_SCAN_STATUS_SUCCESS;
+
 		}
 		/* Check all requested opc/chan pairs supported by radio */
 		else if (!scan_supported(a, scan_req, re)) {
-- 
GitLab