Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

samples: wifi: sta: Use wifi_cred library #11

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed? As we are only calling the Wi-Fi mgmt ext API?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed.

#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);
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary whitespace

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
Loading