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

[rtl872x] WPA3 and hidden SSID support #2673

Merged
merged 4 commits into from
Aug 15, 2023
Merged
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
2 changes: 2 additions & 0 deletions hal/inc/wlan_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ typedef enum {
WLAN_SEC_WPA2,
WLAN_SEC_WPA_ENTERPRISE,
WLAN_SEC_WPA2_ENTERPRISE,
WLAN_SEC_WPA3,
WLAN_SEC_NOT_SET = 0xFF
} WLanSecurityType;

Expand Down Expand Up @@ -279,6 +280,7 @@ typedef struct {
#define WLAN_CREDENTIALS_CURRENT_VERSION (WLAN_CREDENTIALS_VERSION_3)

#define WLAN_SET_CREDENTIALS_FLAGS_DRY_RUN (1<<0)
#define WLAN_SET_CREDENTIALS_FLAGS_HIDDEN (1<<1)

#define WLAN_SET_CREDENTIALS_UNKNOWN_SECURITY_TYPE (-1)
#define WLAN_SET_CREDENTIALS_CIPHER_REQUIRED (-2)
Expand Down
22 changes: 19 additions & 3 deletions hal/network/ncp/wifi/wifi_network_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ int loadConfig(Vector<WifiNetworkConfig>* networks) {
.ssid(dSsid.data)
.bssid(bssid)
.security((WifiSecurity)pbConf.security)
.credentials(std::move(cred));
.credentials(std::move(cred))
.hidden(pbConf.hidden);
if (!networks->append(std::move(conf))) {
return false;
}
Expand Down Expand Up @@ -153,6 +154,7 @@ int saveConfig(const Vector<WifiNetworkConfig>& networks) {
ePwd.data = s;
ePwd.size = strlen(s);
}
pbConf.hidden = conf.hidden();
if (!pb_encode_tag_for_field(strm, field)) {
return false;
}
Expand Down Expand Up @@ -234,7 +236,7 @@ int WifiNetworkManager::connect(const char* ssid) {
index = 0;
for (; index < networks.size(); ++index) {
network = &networks.at(index);
if (strcmp(network->ssid(), ap.ssid()) == 0) {
if (strcmp(network->ssid(), ap.ssid()) == 0 && !network->hidden()) {
break;
}
}
Expand All @@ -256,7 +258,21 @@ int WifiNetworkManager::connect(const char* ssid) {
}
}
if (!connected) {
return SYSTEM_ERROR_NOT_FOUND;
// Attempt to connect to hidden SSIDs
index = 0;
for (; index < networks.size(); ++index) {
network = &networks.at(index);
if (network->hidden()) {
r = client_->connect(network->ssid(), network->bssid(), network->security(), network->credentials());
if (r == 0) {
connected = true;
break;
}
}
}
if (!connected) {
return SYSTEM_ERROR_NOT_FOUND;
}
}
} else if (network->bssid() == INVALID_MAC_ADDRESS) {
// Update BSSID
Expand Down
21 changes: 19 additions & 2 deletions hal/network/ncp/wifi/wifi_network_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ enum class WifiSecurity {
WEP = 1,
WPA_PSK = 2,
WPA2_PSK = 3,
WPA_WPA2_PSK = 4
WPA_WPA2_PSK = 4,
WPA3_PSK = 5,
WPA2_WPA3_PSK = 6
};

class WifiCredentials {
Expand Down Expand Up @@ -70,6 +72,9 @@ class WifiNetworkConfig {
WifiNetworkConfig& ssid(const char* ssid);
const char* ssid() const;

WifiNetworkConfig& hidden(bool hidden);
bool hidden() const;

WifiNetworkConfig& bssid(const MacAddress& bssid);
const MacAddress& bssid() const;

Expand All @@ -84,6 +89,7 @@ class WifiNetworkConfig {
MacAddress bssid_;
WifiCredentials cred_;
WifiSecurity sec_;
bool hidden_;
};

class WifiNetworkInfo {
Expand Down Expand Up @@ -185,7 +191,8 @@ inline WifiCredentials::Type WifiCredentials::type() const {

inline WifiNetworkConfig::WifiNetworkConfig() :
bssid_(INVALID_MAC_ADDRESS),
sec_(WifiSecurity::NONE) {
sec_(WifiSecurity::NONE),
hidden_(false) {
}

inline WifiNetworkConfig& WifiNetworkConfig::ssid(const char* ssid) {
Expand All @@ -197,6 +204,16 @@ inline const char* WifiNetworkConfig::ssid() const {
return ssid_;
}


inline WifiNetworkConfig& WifiNetworkConfig::hidden(bool state) {
hidden_ = state;
return *this;
}

inline bool WifiNetworkConfig::hidden() const {
return hidden_;
}

inline WifiNetworkConfig& WifiNetworkConfig::bssid(const MacAddress& bssid) {
bssid_ = bssid;
return *this;
Expand Down
19 changes: 15 additions & 4 deletions hal/network/ncp/wifi/wlan_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ bool isSupportedSecurityType(WLanSecurityType type) {
case WLanSecurityType::WLAN_SEC_WEP:
case WLanSecurityType::WLAN_SEC_WPA:
case WLanSecurityType::WLAN_SEC_WPA2:
case WLanSecurityType::WLAN_SEC_WPA3:
return true;
default:
return false;
Expand All @@ -63,6 +64,8 @@ WifiSecurity toWifiSecurity(WLanSecurityType type) {
return WifiSecurity::WPA_PSK;
case WLanSecurityType::WLAN_SEC_WPA2:
return WifiSecurity::WPA2_PSK;
case WLanSecurityType::WLAN_SEC_WPA3:
return WifiSecurity::WPA3_PSK;
default:
return WifiSecurity::NONE;
}
Expand All @@ -79,6 +82,9 @@ WLanSecurityType fromWifiSecurity(WifiSecurity sec) {
case WifiSecurity::WPA2_PSK:
case WifiSecurity::WPA_WPA2_PSK:
return WLanSecurityType::WLAN_SEC_WPA2;
case WifiSecurity::WPA3_PSK:
case WifiSecurity::WPA2_WPA3_PSK:
return WLanSecurityType::WLAN_SEC_WPA3;
default:
return WLanSecurityType::WLAN_SEC_UNSEC;
}
Expand Down Expand Up @@ -169,6 +175,9 @@ int wlan_set_credentials(WLanCredentials* halCred) {
const auto ssid = CString::wrap(strndup(halCred->ssid, halCred->ssid_len));
WifiNetworkConfig conf;
conf.ssid(ssid);
if (halCred->flags & WLAN_SET_CREDENTIALS_FLAGS_HIDDEN) {
conf.hidden(true);
}
conf.security(toWifiSecurity(halCred->security));
conf.credentials(std::move(cred));
const auto mgr = wifiNetworkManager();
Expand Down Expand Up @@ -370,8 +379,9 @@ int wlan_scan(wlan_scan_result_t callback, void* cookie) {
// FIXME: As the ESP32 doesn't return the cipher type of an AP, we manually set it here.
if (ap.security == WLAN_SEC_WPA || ap.security == WLAN_SEC_WPA2) {
ap.cipher = WLAN_CIPHER_AES_TKIP;
}
else {
} else if (ap.security == WLAN_SEC_WPA3) {
ap.cipher = WLAN_CIPHER_AES;
} else {
ap.cipher = WLAN_CIPHER_NOT_SET;
}
ap.channel = result.channel();
Expand Down Expand Up @@ -414,8 +424,9 @@ int wlan_get_credentials(wlan_scan_result_t callback, void* callback_data) {
// FIXME: As the ESP32 doesn't return the cipher type of an AP, we manually set it here.
if (ap.security == WLAN_SEC_WPA || ap.security == WLAN_SEC_WPA2) {
ap.cipher = WLAN_CIPHER_AES_TKIP;
}
else {
} else if (ap.security == WLAN_SEC_WPA3) {
ap.cipher = WLAN_CIPHER_AES;
} else {
ap.cipher = WLAN_CIPHER_NOT_SET;
}
const auto d = (Data*)data;
Expand Down
9 changes: 8 additions & 1 deletion hal/network/ncp_client/realtek/rtl_ncp_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ rtw_security_t wifiSecurityToRtlSecurity(WifiSecurity sec) {
// FIXME
return RTW_SECURITY_WPA_WPA2_MIXED_PSK;
}
case WifiSecurity::WPA2_WPA3_PSK: {
return RTW_SECURITY_WPA2_WPA3_MIXED;
}
case WifiSecurity::WPA3_PSK: {
return RTW_SECURITY_WPA3_AES_PSK;
}
}
return RTW_SECURITY_UNKNOWN;
}
Expand Down Expand Up @@ -408,7 +414,8 @@ int RealtekNcpClient::scan(WifiScanCallback callback, void* data) {
( record->security == RTW_SECURITY_WPA_WPA2_AES_PSK) ? "WPA/WPA2 AES" :
( record->security == RTW_SECURITY_WPA_WPA2_MIXED_PSK) ? "WPA/WPA2 Mixed" :
#ifdef CONFIG_SAE_SUPPORT
( record->security == RTW_SECURITY_WPA3_AES_PSK) ? "WP3-SAE AES" :
( record->security == RTW_SECURITY_WPA3_AES_PSK) ? "WPA3-SAE AES" :
( record->security == RTW_SECURITY_WPA2_WPA3_MIXED) ? "WPA2/WPA3 Mixed" :
#endif
"Unknown" );

Expand Down
1 change: 1 addition & 0 deletions proto_defs/internal/network_config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ message WifiConfig {
bytes bssid = 2 [(nanopb).max_size = 6];
ctrl.wifi.Security security = 3;
ctrl.wifi.Credentials credentials = 4;
bool hidden = 5;
}

repeated Network networks = 1;
Expand Down
2 changes: 1 addition & 1 deletion proto_defs/shared
Submodule shared updated 1 files
+3 −0 control/wifi_new.proto
17 changes: 11 additions & 6 deletions proto_defs/src/control/wifi_new.pb.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ typedef enum _particle_ctrl_wifi_Security {
particle_ctrl_wifi_Security_WEP = 1, /* WEP */
particle_ctrl_wifi_Security_WPA_PSK = 2, /* WPA PSK */
particle_ctrl_wifi_Security_WPA2_PSK = 3, /* WPA2 PSK */
particle_ctrl_wifi_Security_WPA_WPA2_PSK = 4 /* WPA/WPA2 PSK */
particle_ctrl_wifi_Security_WPA_WPA2_PSK = 4, /* WPA/WPA2 PSK */
particle_ctrl_wifi_Security_WPA3_PSK = 5, /* WPA3 PSK */
particle_ctrl_wifi_Security_WPA2_WPA3_PSK = 6 /* WPA2/WPA3 PSK */
} particle_ctrl_wifi_Security;

/* *
Expand Down Expand Up @@ -139,13 +141,14 @@ typedef struct _particle_ctrl_wifi_JoinNewNetworkRequest {
particle_ctrl_wifi_Security security; /* Network security */
particle_ctrl_wifi_Credentials credentials; /* Network credentials */
particle_ctrl_Interface interface_config; /* Network interface configuration (IP, mask, DNS etc) */
bool hidden;
} particle_ctrl_wifi_JoinNewNetworkRequest;


/* Helper constants for enums */
#define _particle_ctrl_wifi_Security_MIN particle_ctrl_wifi_Security_NO_SECURITY
#define _particle_ctrl_wifi_Security_MAX particle_ctrl_wifi_Security_WPA_WPA2_PSK
#define _particle_ctrl_wifi_Security_ARRAYSIZE ((particle_ctrl_wifi_Security)(particle_ctrl_wifi_Security_WPA_WPA2_PSK+1))
#define _particle_ctrl_wifi_Security_MAX particle_ctrl_wifi_Security_WPA2_WPA3_PSK
#define _particle_ctrl_wifi_Security_ARRAYSIZE ((particle_ctrl_wifi_Security)(particle_ctrl_wifi_Security_WPA2_WPA3_PSK+1))

#define _particle_ctrl_wifi_CredentialsType_MIN particle_ctrl_wifi_CredentialsType_NO_CREDENTIALS
#define _particle_ctrl_wifi_CredentialsType_MAX particle_ctrl_wifi_CredentialsType_PASSWORD
Expand All @@ -158,7 +161,7 @@ extern "C" {

/* Initializer values for message structs */
#define particle_ctrl_wifi_Credentials_init_default {_particle_ctrl_wifi_CredentialsType_MIN, {{NULL}, NULL}}
#define particle_ctrl_wifi_JoinNewNetworkRequest_init_default {{{NULL}, NULL}, {0, {0}}, _particle_ctrl_wifi_Security_MIN, particle_ctrl_wifi_Credentials_init_default, particle_ctrl_Interface_init_default}
#define particle_ctrl_wifi_JoinNewNetworkRequest_init_default {{{NULL}, NULL}, {0, {0}}, _particle_ctrl_wifi_Security_MIN, particle_ctrl_wifi_Credentials_init_default, particle_ctrl_Interface_init_default, 0}
#define particle_ctrl_wifi_JoinNewNetworkReply_init_default {0}
#define particle_ctrl_wifi_JoinKnownNetworkRequest_init_default {{{NULL}, NULL}}
#define particle_ctrl_wifi_JoinKnownNetworkReply_init_default {0}
Expand All @@ -175,7 +178,7 @@ extern "C" {
#define particle_ctrl_wifi_ScanNetworksReply_init_default {{{NULL}, NULL}}
#define particle_ctrl_wifi_ScanNetworksReply_Network_init_default {{{NULL}, NULL}, {0, {0}}, _particle_ctrl_wifi_Security_MIN, 0, 0}
#define particle_ctrl_wifi_Credentials_init_zero {_particle_ctrl_wifi_CredentialsType_MIN, {{NULL}, NULL}}
#define particle_ctrl_wifi_JoinNewNetworkRequest_init_zero {{{NULL}, NULL}, {0, {0}}, _particle_ctrl_wifi_Security_MIN, particle_ctrl_wifi_Credentials_init_zero, particle_ctrl_Interface_init_zero}
#define particle_ctrl_wifi_JoinNewNetworkRequest_init_zero {{{NULL}, NULL}, {0, {0}}, _particle_ctrl_wifi_Security_MIN, particle_ctrl_wifi_Credentials_init_zero, particle_ctrl_Interface_init_zero, 0}
#define particle_ctrl_wifi_JoinNewNetworkReply_init_zero {0}
#define particle_ctrl_wifi_JoinKnownNetworkRequest_init_zero {{{NULL}, NULL}}
#define particle_ctrl_wifi_JoinKnownNetworkReply_init_zero {0}
Expand Down Expand Up @@ -216,6 +219,7 @@ extern "C" {
#define particle_ctrl_wifi_JoinNewNetworkRequest_security_tag 3
#define particle_ctrl_wifi_JoinNewNetworkRequest_credentials_tag 4
#define particle_ctrl_wifi_JoinNewNetworkRequest_interface_config_tag 5
#define particle_ctrl_wifi_JoinNewNetworkRequest_hidden_tag 6

/* Struct field encoding specification for nanopb */
#define particle_ctrl_wifi_Credentials_FIELDLIST(X, a) \
Expand All @@ -229,7 +233,8 @@ X(a, CALLBACK, SINGULAR, STRING, ssid, 1) \
X(a, STATIC, SINGULAR, BYTES, bssid, 2) \
X(a, STATIC, SINGULAR, UENUM, security, 3) \
X(a, STATIC, SINGULAR, MESSAGE, credentials, 4) \
X(a, STATIC, SINGULAR, MESSAGE, interface_config, 5)
X(a, STATIC, SINGULAR, MESSAGE, interface_config, 5) \
X(a, STATIC, SINGULAR, BOOL, hidden, 6)
#define particle_ctrl_wifi_JoinNewNetworkRequest_CALLBACK pb_default_field_callback
#define particle_ctrl_wifi_JoinNewNetworkRequest_DEFAULT NULL
#define particle_ctrl_wifi_JoinNewNetworkRequest_credentials_MSGTYPE particle_ctrl_wifi_Credentials
Expand Down
9 changes: 6 additions & 3 deletions proto_defs/src/network_config.pb.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ typedef struct _particle_firmware_WifiConfig_Network {
particle_firmware_WifiConfig_Network_bssid_t bssid;
particle_ctrl_wifi_Security security;
particle_ctrl_wifi_Credentials credentials;
bool hidden;
} particle_firmware_WifiConfig_Network;


Expand All @@ -42,11 +43,11 @@ extern "C" {

/* Initializer values for message structs */
#define particle_firmware_WifiConfig_init_default {{{NULL}, NULL}}
#define particle_firmware_WifiConfig_Network_init_default {{{NULL}, NULL}, {0, {0}}, _particle_ctrl_wifi_Security_MIN, particle_ctrl_wifi_Credentials_init_default}
#define particle_firmware_WifiConfig_Network_init_default {{{NULL}, NULL}, {0, {0}}, _particle_ctrl_wifi_Security_MIN, particle_ctrl_wifi_Credentials_init_default, 0}
#define particle_firmware_CellularConfig_init_default {particle_ctrl_cellular_AccessPoint_init_default, particle_ctrl_cellular_AccessPoint_init_default, _particle_ctrl_cellular_SimType_MIN}
#define particle_firmware_NetworkConfig_init_default {{{NULL}, NULL}}
#define particle_firmware_WifiConfig_init_zero {{{NULL}, NULL}}
#define particle_firmware_WifiConfig_Network_init_zero {{{NULL}, NULL}, {0, {0}}, _particle_ctrl_wifi_Security_MIN, particle_ctrl_wifi_Credentials_init_zero}
#define particle_firmware_WifiConfig_Network_init_zero {{{NULL}, NULL}, {0, {0}}, _particle_ctrl_wifi_Security_MIN, particle_ctrl_wifi_Credentials_init_zero, 0}
#define particle_firmware_CellularConfig_init_zero {particle_ctrl_cellular_AccessPoint_init_zero, particle_ctrl_cellular_AccessPoint_init_zero, _particle_ctrl_cellular_SimType_MIN}
#define particle_firmware_NetworkConfig_init_zero {{{NULL}, NULL}}

Expand All @@ -60,6 +61,7 @@ extern "C" {
#define particle_firmware_WifiConfig_Network_bssid_tag 2
#define particle_firmware_WifiConfig_Network_security_tag 3
#define particle_firmware_WifiConfig_Network_credentials_tag 4
#define particle_firmware_WifiConfig_Network_hidden_tag 5

/* Struct field encoding specification for nanopb */
#define particle_firmware_WifiConfig_FIELDLIST(X, a) \
Expand All @@ -72,7 +74,8 @@ X(a, CALLBACK, REPEATED, MESSAGE, networks, 1)
X(a, CALLBACK, SINGULAR, STRING, ssid, 1) \
X(a, STATIC, SINGULAR, BYTES, bssid, 2) \
X(a, STATIC, SINGULAR, UENUM, security, 3) \
X(a, STATIC, SINGULAR, MESSAGE, credentials, 4)
X(a, STATIC, SINGULAR, MESSAGE, credentials, 4) \
X(a, STATIC, SINGULAR, BOOL, hidden, 5)
#define particle_firmware_WifiConfig_Network_CALLBACK pb_default_field_callback
#define particle_firmware_WifiConfig_Network_DEFAULT NULL
#define particle_firmware_WifiConfig_Network_credentials_MSGTYPE particle_ctrl_wifi_Credentials
Expand Down
1 change: 1 addition & 0 deletions system/src/control/wifi_new.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ int joinNewNetwork(ctrl_request* req) {
MacAddress bssid = INVALID_MAC_ADDRESS;
bssidFromPb(&bssid, pbReq.bssid);
conf.bssid(bssid);
conf.hidden(pbReq.hidden);
const auto wifiMgr = wifiNetworkManager();
CHECK_TRUE(wifiMgr, SYSTEM_ERROR_UNKNOWN);
const auto ncpClient = wifiMgr->ncpClient();
Expand Down
6 changes: 3 additions & 3 deletions system/src/system_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,12 +427,12 @@ void WiFiSetupConsole::handle(char c)
print("Security 0=unsecured, 1=WEP, 2=WPA, 3=WPA2: ");
read_line(security_type_string, 1);
}
while ('0' > security_type_string[0] || '3' < security_type_string[0]);
while ('0' > security_type_string[0] || ('3' < security_type_string[0] && security_type_string[0] != '6' /* WPA3 */));
#else
print("Security 0=unsecured, 1=WEP, 2=WPA, 3=WPA2, 4=WPA Enterprise, 5=WPA2 Enterprise: ");
read_line(security_type_string, 1);
}
while ('0' > security_type_string[0] || '5' < security_type_string[0]);
while ('0' > security_type_string[0] || '6' < security_type_string[0]);
#endif
security_ = (WLanSecurityType)(security_type_string[0] - '0');
}
Expand Down Expand Up @@ -464,7 +464,7 @@ void WiFiSetupConsole::handle(char c)
credentials.setCipher(cipher_);
}

if (0 < security_ && security_ <= 3)
if ((0 < security_ && security_ <= 3) || security_ == WLAN_SEC_WPA3)
{
print("Password: ");
read_line(password, sizeof(password) - 1);
Expand Down
12 changes: 12 additions & 0 deletions user/tests/wiring/api/wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ test(api_wifi_set_credentials)
(void)ok; // avoid unused warning
}

test(api_wifi_set_credentials_obj)
{
WiFiCredentials cred;
API_COMPILE(cred.setSsid("ssid)"));
API_COMPILE(cred.setPassword("password"));
API_COMPILE(cred.setSecurity(WPA3));
API_COMPILE(cred.setCipher(WLAN_CIPHER_AES));
API_COMPILE(cred.setHidden(true));
API_COMPILE(WiFi.setCredentials(cred));
}


test(api_wifi_set_security)
{
WiFiCredentials credentials;
Expand Down
12 changes: 11 additions & 1 deletion wiring/inc/spark_wiring_wifi_credentials.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ enum SecurityType {
WPA = WLAN_SEC_WPA,
WPA2 = WLAN_SEC_WPA2,
WPA_ENTERPRISE = WLAN_SEC_WPA_ENTERPRISE,
WPA2_ENTERPRISE = WLAN_SEC_WPA2_ENTERPRISE
WPA2_ENTERPRISE = WLAN_SEC_WPA2_ENTERPRISE,
WPA3 = WLAN_SEC_WPA3
};

#define SET_STRING_WITH_LEN_FIELD(field, value, len_field, len_value) \
Expand Down Expand Up @@ -143,6 +144,15 @@ class WiFiCredentials {
return *this;
}

virtual WiFiCredentials& setHidden(bool hidden = true) {
if (hidden) {
creds_.flags |= WLAN_SET_CREDENTIALS_FLAGS_HIDDEN;
} else {
creds_.flags &= ~(WLAN_SET_CREDENTIALS_FLAGS_HIDDEN);
}
return *this;
}

operator WLanCredentials() {
return getHalCredentials();
}
Expand Down