define WIFI_CACHE_SCANRESULTS to cache scanresults

This commit is contained in:
Anjan Chanda
2025-05-27 20:01:11 +02:00
parent 788ca18653
commit c12a4de290
4 changed files with 70 additions and 58 deletions

View File

@@ -6,6 +6,7 @@ BINDIR ?= /usr/sbin
PROG_CFLAGS = $(CFLAGS) -DHAS_UBUS -Wall -Werror -fstrict-aliasing -ggdb
#PROG_CFLAGS += -DDONOT_CREATE_AFFILIATED_IFACE_OBJECT
#PROG_CFLAGS += -DWIFI_CACHE_SCANRESULTS
CFLAGS += -Wno-address-of-packed-member
PROG_LDFLAGS = $(LDFLAGS)
PROG_LIBS += -lssl -lcrypto -leasy -lwifiutils -lwifi-7 -lm -ldl

View File

@@ -2695,25 +2695,6 @@ struct wifimngr_channel *wifimngr_device_lookup_freq(struct wifimngr_device *wde
return NULL;
}
int wifimngr_update_scanresults(struct wifimngr_device *wdev, int num_scanres,
struct wifi_bss *scanres)
{
int i;
for (i = 0; i < num_scanres; i++) {
struct wifimngr_channel *ch =
wifimngr_device_lookup_channel(wdev, scanres[i].channel);
if (ch) {
memcpy(&ch->scanres[ch->num_scanres++], &scanres[i],
sizeof(struct wifi_bss));
time(&ch->scanres_ts);
}
}
return 0;
}
static void wl_scanresult_print(struct blob_buf *bb, void *buf, bool detail)
{
struct wifi_bss *b = (struct wifi_bss *)buf;
@@ -2786,7 +2767,7 @@ int wl_scanresults(struct ubus_context *ctx, struct ubus_object *obj,
bool cache = false;
const char *radio;
void *a, *t;
int i, j;
int i;
blobmsg_parse(wl_scanres_policy, __SCANRES_MAX, tb, blob_data(msg),
@@ -2822,11 +2803,28 @@ int wl_scanresults(struct ubus_context *ctx, struct ubus_object *obj,
return UBUS_STATUS_OK;
}
blob_buf_init(&bb, 0);
a = blobmsg_open_array(&bb, "accesspoints");
#if WIFI_CACHE_SCANRESULTS
if (tb[SCANRES_CACHE])
cache = blobmsg_get_bool(tb[SCANRES_CACHE]);
blob_buf_init(&bb, 0);
a = blobmsg_open_array(&bb, "accesspoints");
if (cache) {
for (i = 0; i < wdev->num_ch; i++) {
if (!wdev->ch[i].num_scanres)
continue;
for (int j = 0; j < wdev->ch[i].num_scanres; j++) {
t = blobmsg_open_table(&bb, "");
wl_scanresult_print(&bb, &wdev->ch[i].scanres[j], detail);
blobmsg_add_u32(&bb, "elapsed", difftime(time(NULL), wdev->ch[i].scanres_ts));
blobmsg_close_table(&bb, t);
}
}
}
#endif /* WIFI_CACHE_SCANRESULTS */
if (!cache) {
int ret;
@@ -2839,18 +2837,6 @@ int wl_scanresults(struct ubus_context *ctx, struct ubus_object *obj,
wl_scanresult_print(&bb, &bsss[i], detail);
blobmsg_close_table(&bb, t);
}
} else {
for (i = 0; i < wdev->num_ch; i++) {
if (!wdev->ch[i].num_scanres)
continue;
for (j = 0; j < wdev->ch[i].num_scanres; j++) {
t = blobmsg_open_table(&bb, "");
wl_scanresult_print(&bb, &wdev->ch[i].scanres[j], detail);
blobmsg_add_u32(&bb, "elapsed", difftime(time(NULL), wdev->ch[i].scanres_ts));
blobmsg_close_table(&bb, t);
}
}
}
blobmsg_close_array(&bb, a);
@@ -5907,6 +5893,48 @@ int wifimngr_reconfig(struct wifimngr *w)
return 0;
}
#ifdef WIFI_CACHE_SCANRESULTS
int wifimngr_update_scanresults(struct wifimngr_device *wdev, int num_scanres,
struct wifi_bss *scanres)
{
int i;
for (i = 0; i < num_scanres; i++) {
struct wifimngr_channel *ch =
wifimngr_device_lookup_channel(wdev, scanres[i].channel);
if (ch) {
memcpy(&ch->scanres[ch->num_scanres++], &scanres[i],
sizeof(struct wifi_bss));
time(&ch->scanres_ts);
}
}
return 0;
}
int wifimngr_get_initial_scanresults(struct wifimngr *w)
{
int i;
for (i = 0; i < w->num_wifi_device; i++) {
struct wifi_bss scanres[NUM_SCANRES] = {0};
int num = ARRAY_SIZE(scanres);
int ret;
wifimngr_device_init_channels(&w->wdev[i]);
ret = wifi_get_band_scan_results(w->wdev[i].phy,
w->wdev[i].band,
scanres,
&num);
if (!ret && num > 0)
wifimngr_update_scanresults(&w->wdev[i], num, scanres);
}
return 0;
}
/* scan event data */
enum scan_event_data_attr {
SCAN_EVENT_ATTR_FREQ,
@@ -6000,6 +6028,7 @@ int wifimngr_update_scanresults_cache(struct wifimngr_device *wdev, int sz,
return 0;
}
#endif /* WIFI_CACHE_SCANRESULTS */
int wifimngr_device_init_channels(struct wifimngr_device *wdev)
{
@@ -6034,28 +6063,6 @@ int wifimngr_device_init_channels(struct wifimngr_device *wdev)
return 0;
}
int wifimngr_get_initial_scanresults(struct wifimngr *w)
{
int i;
for (i = 0; i < w->num_wifi_device; i++) {
struct wifi_bss scanres[NUM_SCANRES] = {0};
int num = ARRAY_SIZE(scanres);
int ret;
wifimngr_device_init_channels(&w->wdev[i]);
ret = wifi_get_band_scan_results(w->wdev[i].phy,
w->wdev[i].band,
scanres,
&num);
if (!ret && num > 0)
wifimngr_update_scanresults(&w->wdev[i], num, scanres);
}
return 0;
}
int wl_sta_ratings_recalc(void *libctx)
{
struct radio_entry radios[4] = {0};
@@ -6181,8 +6188,9 @@ int wifimngr_init(struct wifimngr **w, struct wifimngr_cmdline_opts *opts)
goto out_error;
}
#ifdef WIFI_CACHE_SCANRESULTS
wifimngr_get_initial_scanresults(wm);
#endif
uloop_timeout_set(&wm->hbtimer, 1000);
return 0;

View File

@@ -52,10 +52,12 @@ struct wifimngr_channel {
uint32_t channel; /* 20MHz ctrl-channel */
uint32_t freq; /* frequency in MHz */
#ifdef WIFI_CACHE_SCANRESULTS
/* cached scanresults from latest scan */
time_t scanres_ts;
int num_scanres;
struct wifi_bss scanres[MAX_NUM_PER_CHANNEL_SCANRES];
#endif
};
struct wifimngr_device {

View File

@@ -103,7 +103,7 @@ int wifimngr_event_cb(struct event_struct *e)
WIFI_RADIO_OBJECT, e->ifname, evtype);
}
wifimngr_ubus_event(w->ubus_ctx, evtbuf);
#ifdef WIFI_CACHE_SCANRESULTS
if (resp->type == WIFI_EVENT_SCAN_END) {
struct wifimngr_device *wdev;
@@ -114,6 +114,7 @@ int wifimngr_event_cb(struct event_struct *e)
(char *)resp->data);
}
}
#endif
break;
case WIFI_EVENT_SCAN_ABORT:
snprintf(evtbuf, MAX_EVENT_RESPONSE_LEN - 1,