Skip to content

Commit 6dd601c

Browse files
jerome-pouillerdanieldegrasse
authored andcommitted
drivers: wifi: siwx91x: Extract scan related functions
siwx91x_wifi.c starts to contains to much code. Let's simplify it by grouping all the scan related functions in a separated file. Signed-off-by: Jérôme Pouiller <[email protected]>
1 parent f6f3879 commit 6dd601c

File tree

4 files changed

+284
-249
lines changed

4 files changed

+284
-249
lines changed

drivers/wifi/siwx91x/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Copyright (c) 2024 Silicon Laboratories Inc.
33

44
if(CONFIG_WIFI_SILABS_SIWX91X)
5-
zephyr_library_sources(siwx91x_wifi.c)
5+
zephyr_library_sources(siwx91x_wifi.c siwx91x_wifi_scan.c)
66

77
if(CONFIG_WIFI_SILABS_SIWX91X_NET_STACK_OFFLOAD)
88
zephyr_library_sources(siwx91x_wifi_socket.c)

drivers/wifi/siwx91x/siwx91x_wifi.c

Lines changed: 1 addition & 248 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <nwp.h>
1313
#include "siwx91x_wifi.h"
14+
#include "siwx91x_wifi_scan.h"
1415
#include "siwx91x_wifi_socket.h"
1516

1617
#include "sl_rsi_utility.h"
@@ -23,7 +24,6 @@
2324
#include "sl_wifi_constants.h"
2425

2526
#define SIWX91X_DRIVER_VERSION KERNEL_VERSION_STRING
26-
#define SIWX91X_DEFAULT_PASSIVE_SCAN_DWELL_TIME 400
2727

2828
LOG_MODULE_REGISTER(siwx91x_wifi);
2929

@@ -1023,253 +1023,6 @@ static int siwx91x_connect(const struct device *dev, struct wifi_connect_req_par
10231023
return 0;
10241024
}
10251025

1026-
static void siwx91x_report_scan_res(struct siwx91x_dev *sidev, sl_wifi_scan_result_t *result,
1027-
int item)
1028-
{
1029-
static const struct {
1030-
int sl_val;
1031-
int z_val;
1032-
} security_convert[] = {
1033-
{ SL_WIFI_OPEN, WIFI_SECURITY_TYPE_NONE },
1034-
{ SL_WIFI_WEP, WIFI_SECURITY_TYPE_WEP },
1035-
{ SL_WIFI_WPA, WIFI_SECURITY_TYPE_WPA_PSK },
1036-
{ SL_WIFI_WPA2, WIFI_SECURITY_TYPE_PSK },
1037-
{ SL_WIFI_WPA3, WIFI_SECURITY_TYPE_SAE },
1038-
{ SL_WIFI_WPA3_TRANSITION, WIFI_SECURITY_TYPE_SAE },
1039-
{ SL_WIFI_WPA_ENTERPRISE, WIFI_SECURITY_TYPE_EAP },
1040-
{ SL_WIFI_WPA2_ENTERPRISE, WIFI_SECURITY_TYPE_EAP },
1041-
};
1042-
1043-
struct wifi_scan_result tmp = {
1044-
.channel = result->scan_info[item].rf_channel,
1045-
.rssi = result->scan_info[item].rssi_val,
1046-
.ssid_length = strlen(result->scan_info[item].ssid),
1047-
.mac_length = sizeof(result->scan_info[item].bssid),
1048-
.security = WIFI_SECURITY_TYPE_UNKNOWN,
1049-
.mfp = WIFI_MFP_UNKNOWN,
1050-
.band = WIFI_FREQ_BAND_2_4_GHZ,
1051-
};
1052-
1053-
if (result->scan_count == 0) {
1054-
return;
1055-
}
1056-
1057-
if (result->scan_info[item].rf_channel <= 0 || result->scan_info[item].rf_channel > 14) {
1058-
LOG_WRN("Unexpected scan result");
1059-
tmp.band = WIFI_FREQ_BAND_UNKNOWN;
1060-
}
1061-
1062-
memcpy(tmp.ssid, result->scan_info[item].ssid, tmp.ssid_length);
1063-
memcpy(tmp.mac, result->scan_info[item].bssid, tmp.mac_length);
1064-
1065-
ARRAY_FOR_EACH(security_convert, i) {
1066-
if (security_convert[i].sl_val == result->scan_info[item].security_mode) {
1067-
tmp.security = security_convert[i].z_val;
1068-
}
1069-
}
1070-
1071-
sidev->scan_res_cb(sidev->iface, 0, &tmp);
1072-
}
1073-
1074-
static unsigned int siwx91x_on_scan(sl_wifi_event_t event, sl_wifi_scan_result_t *result,
1075-
uint32_t result_size, void *arg)
1076-
{
1077-
struct siwx91x_dev *sidev = arg;
1078-
int i, scan_count;
1079-
1080-
if (!sidev->scan_res_cb) {
1081-
return -EFAULT;
1082-
}
1083-
1084-
if (event & SL_WIFI_EVENT_FAIL_INDICATION) {
1085-
memset(result, 0, sizeof(*result));
1086-
}
1087-
1088-
if (sidev->scan_max_bss_cnt) {
1089-
scan_count = MIN(result->scan_count, sidev->scan_max_bss_cnt);
1090-
} else {
1091-
scan_count = result->scan_count;
1092-
}
1093-
1094-
for (i = 0; i < scan_count; i++) {
1095-
siwx91x_report_scan_res(sidev, result, i);
1096-
}
1097-
1098-
sidev->scan_res_cb(sidev->iface, 0, NULL);
1099-
sidev->state = sidev->scan_prev_state;
1100-
1101-
return 0;
1102-
}
1103-
1104-
static int
1105-
siwx91x_configure_scan_dwell_time(sl_wifi_scan_type_t scan_type, uint16_t dwell_time_active,
1106-
uint16_t dwell_time_passive,
1107-
sl_wifi_advanced_scan_configuration_t *advanced_scan_config)
1108-
{
1109-
int ret = 0;
1110-
1111-
if (dwell_time_active && (dwell_time_active < 5 || dwell_time_active > 1000)) {
1112-
LOG_ERR("Invalid active scan dwell time");
1113-
return -EINVAL;
1114-
}
1115-
1116-
if (dwell_time_passive && (dwell_time_passive < 10 || dwell_time_passive > 1000)) {
1117-
LOG_ERR("Invalid passive scan dwell time");
1118-
return -EINVAL;
1119-
}
1120-
1121-
switch (scan_type) {
1122-
case SL_WIFI_SCAN_TYPE_ACTIVE:
1123-
ret = sl_si91x_configure_timeout(SL_SI91X_CHANNEL_ACTIVE_SCAN_TIMEOUT,
1124-
dwell_time_active);
1125-
break;
1126-
case SL_WIFI_SCAN_TYPE_PASSIVE:
1127-
if (!dwell_time_passive) {
1128-
dwell_time_passive = SIWX91X_DEFAULT_PASSIVE_SCAN_DWELL_TIME;
1129-
}
1130-
ret = sl_si91x_configure_timeout(SL_SI91X_CHANNEL_PASSIVE_SCAN_TIMEOUT,
1131-
dwell_time_passive);
1132-
break;
1133-
case SL_WIFI_SCAN_TYPE_ADV_SCAN:
1134-
__ASSERT(advanced_scan_config, "advanced_scan_config cannot be NULL");
1135-
1136-
if (!dwell_time_active) {
1137-
dwell_time_active = CONFIG_WIFI_SILABS_SIWX91X_ADV_ACTIVE_SCAN_DURATION;
1138-
}
1139-
advanced_scan_config->active_channel_time = dwell_time_active;
1140-
1141-
if (!dwell_time_passive) {
1142-
dwell_time_passive = CONFIG_WIFI_SILABS_SIWX91X_ADV_PASSIVE_SCAN_DURATION;
1143-
}
1144-
advanced_scan_config->passive_channel_time = dwell_time_passive;
1145-
break;
1146-
default:
1147-
break;
1148-
}
1149-
1150-
return ret;
1151-
}
1152-
1153-
static int siwx91x_scan(const struct device *dev, struct wifi_scan_params *z_scan_config,
1154-
scan_result_cb_t cb)
1155-
{
1156-
sl_wifi_interface_t interface = sl_wifi_get_default_interface();
1157-
sl_wifi_scan_configuration_t sl_scan_config = { };
1158-
sl_wifi_advanced_scan_configuration_t advanced_scan_config = {
1159-
.trigger_level = CONFIG_WIFI_SILABS_SIWX91X_ADV_SCAN_THRESHOLD,
1160-
.trigger_level_change = CONFIG_WIFI_SILABS_SIWX91X_ADV_RSSI_TOLERANCE_THRESHOLD,
1161-
.enable_multi_probe = CONFIG_WIFI_SILABS_SIWX91X_ADV_MULTIPROBE,
1162-
.enable_instant_scan = CONFIG_WIFI_SILABS_SIWX91X_ENABLE_INSTANT_SCAN,
1163-
};
1164-
sl_wifi_roam_configuration_t roam_configuration = {
1165-
#ifdef CONFIG_WIFI_SILABS_SIWX91X_ENABLE_ROAMING
1166-
.trigger_level = CONFIG_WIFI_SILABS_SIWX91X_ROAMING_TRIGGER_LEVEL,
1167-
.trigger_level_change = CONFIG_WIFI_SILABS_SIWX91X_ROAMING_TRIGGER_LEVEL_CHANGE,
1168-
#else
1169-
.trigger_level = SL_WIFI_NEVER_ROAM,
1170-
.trigger_level_change = 0,
1171-
#endif
1172-
};
1173-
struct siwx91x_dev *sidev = dev->data;
1174-
sl_wifi_ssid_t ssid = { };
1175-
int ret;
1176-
1177-
__ASSERT(z_scan_config, "z_scan_config cannot be NULL");
1178-
1179-
if (FIELD_GET(SIWX91X_INTERFACE_MASK, interface) != SL_WIFI_CLIENT_INTERFACE) {
1180-
LOG_ERR("Interface not in STA mode");
1181-
return -EINVAL;
1182-
}
1183-
1184-
if (sidev->state != WIFI_STATE_DISCONNECTED && sidev->state != WIFI_STATE_INACTIVE &&
1185-
sidev->state != WIFI_STATE_COMPLETED) {
1186-
LOG_ERR("Command given in invalid state");
1187-
return -EBUSY;
1188-
}
1189-
1190-
if (z_scan_config->bands & ~(BIT(WIFI_FREQ_BAND_UNKNOWN) | BIT(WIFI_FREQ_BAND_2_4_GHZ))) {
1191-
LOG_ERR("Invalid band entered");
1192-
return -EINVAL;
1193-
}
1194-
1195-
if (sidev->state == WIFI_STATE_COMPLETED) {
1196-
siwx91x_configure_scan_dwell_time(SL_WIFI_SCAN_TYPE_ADV_SCAN,
1197-
z_scan_config->dwell_time_active,
1198-
z_scan_config->dwell_time_passive,
1199-
&advanced_scan_config);
1200-
1201-
ret = sl_wifi_set_advanced_scan_configuration(&advanced_scan_config);
1202-
if (ret != SL_STATUS_OK) {
1203-
LOG_ERR("advanced scan configuration failed with status %x", ret);
1204-
return -EINVAL;
1205-
}
1206-
1207-
ret = sl_wifi_set_roam_configuration(interface, &roam_configuration);
1208-
if (ret != SL_STATUS_OK) {
1209-
LOG_ERR("roaming configuration failed with status %x", ret);
1210-
return -EINVAL;
1211-
}
1212-
1213-
sl_scan_config.type = SL_WIFI_SCAN_TYPE_ADV_SCAN;
1214-
sl_scan_config.periodic_scan_interval =
1215-
CONFIG_WIFI_SILABS_SIWX91X_ADV_SCAN_PERIODICITY;
1216-
} else {
1217-
if (z_scan_config->scan_type == WIFI_SCAN_TYPE_ACTIVE) {
1218-
sl_scan_config.type = SL_WIFI_SCAN_TYPE_ACTIVE;
1219-
ret = siwx91x_configure_scan_dwell_time(SL_WIFI_SCAN_TYPE_ACTIVE,
1220-
z_scan_config->dwell_time_active,
1221-
z_scan_config->dwell_time_passive,
1222-
NULL);
1223-
} else {
1224-
sl_scan_config.type = SL_WIFI_SCAN_TYPE_PASSIVE;
1225-
ret = siwx91x_configure_scan_dwell_time(SL_WIFI_SCAN_TYPE_PASSIVE,
1226-
z_scan_config->dwell_time_active,
1227-
z_scan_config->dwell_time_passive,
1228-
NULL);
1229-
}
1230-
if (ret != SL_STATUS_OK) {
1231-
LOG_ERR("Failed to configure timeout");
1232-
return -EINVAL;
1233-
}
1234-
}
1235-
1236-
for (int i = 0; i < ARRAY_SIZE(z_scan_config->band_chan); i++) {
1237-
/* End of channel list */
1238-
if (z_scan_config->band_chan[i].channel == 0) {
1239-
break;
1240-
}
1241-
1242-
if (z_scan_config->band_chan[i].band == WIFI_FREQ_BAND_2_4_GHZ) {
1243-
sl_scan_config.channel_bitmap_2g4 |=
1244-
BIT(z_scan_config->band_chan[i].channel - 1);
1245-
}
1246-
}
1247-
1248-
if (z_scan_config->band_chan[0].channel && !sl_scan_config.channel_bitmap_2g4) {
1249-
LOG_ERR("No supported channels in the request");
1250-
return -EINVAL;
1251-
}
1252-
1253-
if (IS_ENABLED(CONFIG_WIFI_MGMT_SCAN_SSID_FILT_MAX)) {
1254-
if (z_scan_config->ssids[0]) {
1255-
strncpy(ssid.value, z_scan_config->ssids[0], WIFI_SSID_MAX_LEN);
1256-
ssid.length = strlen(z_scan_config->ssids[0]);
1257-
}
1258-
}
1259-
1260-
sidev->scan_max_bss_cnt = z_scan_config->max_bss_cnt;
1261-
sidev->scan_res_cb = cb;
1262-
ret = sl_wifi_start_scan(SL_WIFI_CLIENT_2_4GHZ_INTERFACE, (ssid.length > 0) ? &ssid : NULL,
1263-
&sl_scan_config);
1264-
if (ret != SL_STATUS_IN_PROGRESS) {
1265-
return -EIO;
1266-
}
1267-
sidev->scan_prev_state = sidev->state;
1268-
sidev->state = WIFI_STATE_SCANNING;
1269-
1270-
return 0;
1271-
}
1272-
12731026
static int siwx91x_mode(const struct device *dev, struct wifi_mode_info *mode)
12741027
{
12751028
sl_wifi_interface_t interface = sl_wifi_get_default_interface();

0 commit comments

Comments
 (0)