Skip to content

Commit

Permalink
samples: wifi: sta: Use wifi_cred library
Browse files Browse the repository at this point in the history
Add changes to use wifi_credentials library.

Signed-off-by: Triveni Danda <[email protected]>
  • Loading branch information
D-Triveni committed Feb 19, 2024
1 parent 6f98e9d commit b9928dc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 85 deletions.
35 changes: 0 additions & 35 deletions samples/wifi/sta/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,6 @@ config CONNECTION_IDLE_TIMEOUT
int "Time to be waited for a station to connect"
default 30

config STA_SAMPLE_SSID
string "SSID"
help
Specify the SSID to connect

choice STA_KEY_MGMT_SELECT
prompt "Security Option"
default STA_KEY_MGMT_WPA3

config STA_KEY_MGMT_NONE
bool "Open Security"
help
Enable for Open Security

config STA_KEY_MGMT_WPA2
bool "WPA2 Security"
help
Enable for WPA2 Security

config STA_KEY_MGMT_WPA2_256
bool "WPA2 SHA 256 Security"
help
Enable for WPA2-PSK-256 Security

config STA_KEY_MGMT_WPA3
bool "WPA3 Security"
help
Enable for WPA3 Security
endchoice

config STA_SAMPLE_PASSWORD
string "Passphrase (WPA2) or password (WPA3)"
help
Specify the Password to connect

config NRF700X_QSPI_ENCRYPTION_KEY
string "16 bytes QSPI encryption key, only for testing purposes"
depends on BOARD_NRF7002DK_NRF5340_CPUAPP
Expand Down
12 changes: 5 additions & 7 deletions samples/wifi/sta/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ CONFIG_WIFI_NRF700X=y
# WPA supplicant
CONFIG_WPA_SUPP=y

# Below configs need to be modified based on security
# CONFIG_STA_KEY_MGMT_NONE=y
# CONFIG_STA_KEY_MGMT_WPA2=y
# CONFIG_STA_KEY_MGMT_WPA2_256=y
# CONFIG_STA_KEY_MGMT_WPA3=y
CONFIG_STA_SAMPLE_SSID="Myssid"
CONFIG_STA_SAMPLE_PASSWORD="Mypassword"
CONFIG_WIFI_MGMT_EXT=y
CONFIG_WIFI_CREDENTIALS=y
CONFIG_WIFI_CREDENTIALS_STATIC=y
CONFIG_WIFI_CREDENTIALS_STATIC_SSID="Myssid"
CONFIG_WIFI_CREDENTIALS_STATIC_PASSWORD="Mypassword"

# System settings
CONFIG_NEWLIB_LIBC=y
Expand Down
53 changes: 10 additions & 43 deletions samples/wifi/sta/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ LOG_MODULE_REGISTER(sta, CONFIG_LOG_DEFAULT_LEVEL);
#include <zephyr/net/net_event.h>
#include <zephyr/drivers/gpio.h>

#include <net/wifi_credentials.h>
#include <net/wifi_mgmt_ext.h>

#include <qspi_if.h>

#include "net_private.h"
Expand Down Expand Up @@ -125,6 +128,7 @@ static int cmd_wifi_status(void)
LOG_INF("MFP: %s", wifi_mfp_txt(status.mfp));
LOG_INF("RSSI: %d", status.rssi);
}

return 0;
}

Expand Down Expand Up @@ -195,6 +199,7 @@ static void print_dhcp_ip(struct net_mgmt_event_callback *cb)

LOG_INF("DHCP IP address: %s", dhcp_info);
}

static void net_mgmt_event_handler(struct net_mgmt_event_callback *cb,
uint32_t mgmt_event, struct net_if *iface)
{
Expand All @@ -207,56 +212,18 @@ static void net_mgmt_event_handler(struct net_mgmt_event_callback *cb,
}
}

static int __wifi_args_to_params(struct wifi_connect_req_params *params)
{

params->timeout = CONFIG_STA_CONN_TIMEOUT_SEC * MSEC_PER_SEC;

if (params->timeout == 0) {
params->timeout = SYS_FOREVER_MS;
}

/* Defaults */
params->band = WIFI_FREQ_BAND_UNKNOWN;
params->channel = WIFI_CHANNEL_ANY;
params->security = WIFI_SECURITY_TYPE_NONE;
params->mfp = WIFI_MFP_OPTIONAL;

/* SSID */
params->ssid = CONFIG_STA_SAMPLE_SSID;
params->ssid_length = strlen(params->ssid);

#if defined(CONFIG_STA_KEY_MGMT_WPA2)
params->security = 1;
#elif defined(CONFIG_STA_KEY_MGMT_WPA2_256)
params->security = 2;
#elif defined(CONFIG_STA_KEY_MGMT_WPA3)
params->security = 3;
#else
params->security = 0;
#endif

#if !defined(CONFIG_STA_KEY_MGMT_NONE)
params->psk = CONFIG_STA_SAMPLE_PASSWORD;
params->psk_length = strlen(params->psk);
#endif

return 0;
}

static int wifi_connect(void)
{
struct net_if *iface = net_if_get_default();
static struct wifi_connect_req_params cnx_params;
struct net_if *iface = net_if_get_first_wifi();
int ret;

context.connected = false;
context.connect_result = false;
__wifi_args_to_params(&cnx_params);

if (net_mgmt(NET_REQUEST_WIFI_CONNECT, iface,
&cnx_params, sizeof(struct wifi_connect_req_params))) {
/* Apply stored wifi credential */
ret = net_mgmt(NET_REQUEST_WIFI_CONNECT_STORED, iface, NULL, 0);
if (ret) {
LOG_ERR("Connection request failed");

return -ENOEXEC;
}

Expand Down

0 comments on commit b9928dc

Please sign in to comment.