From eae7aa9c0a6339b5ac1b94872915ca0335984f2f Mon Sep 17 00:00:00 2001 From: shchen-Lab <75290921+shchen-Lab@users.noreply.github.com> Date: Fri, 8 Sep 2023 01:08:15 +0800 Subject: [PATCH] [BouffaloLab]update sdk to support open network (#29070) * BL702&BL602 Support OpenNetWork * update sdk to support open network on bl706-wifi * Restyled by whitespace * BLCONFIG Bytes alignment * remove repo openthread misc * code style * Restyled by gn * remove otPlatCAlloc/otPlatFree * Restyled by gn * add comment and check credentials length * Restyled by clang-format --------- Co-authored-by: wyhong Co-authored-by: Restyled.io --- .../BL602/NetworkCommissioningDriver.cpp | 34 ++++++++++++++++--- .../BL602/NetworkCommissioningDriver.h | 9 ++++- .../BL702/NetworkCommissioningDriver.cpp | 28 ++++++++++++++- .../BL702/NetworkCommissioningDriver.h | 9 ++++- .../BL702/ThreadStackManagerImpl.cpp | 10 ------ .../BL702L/ThreadStackManagerImpl.cpp | 10 ------ src/platform/bouffalolab/common/BLConfig.cpp | 33 +++++++++++++++--- third_party/bouffalolab/repo | 2 +- 8 files changed, 102 insertions(+), 33 deletions(-) diff --git a/src/platform/bouffalolab/BL602/NetworkCommissioningDriver.cpp b/src/platform/bouffalolab/BL602/NetworkCommissioningDriver.cpp index d9dd83c923cd57..b051a44a44a0d5 100644 --- a/src/platform/bouffalolab/BL602/NetworkCommissioningDriver.cpp +++ b/src/platform/bouffalolab/BL602/NetworkCommissioningDriver.cpp @@ -44,11 +44,11 @@ CHIP_ERROR BLWiFiDriver::Init(NetworkStatusChangeCallback * networkStatusChangeC size_t ssidLen = 0; size_t credentialsLen = 0; - err = PersistedStorage::KeyValueStoreMgr().Get(BLConfig::kConfigKey_WiFiSSID, mSavedNetwork.credentials, + err = PersistedStorage::KeyValueStoreMgr().Get(BLConfig::kConfigKey_WiFiPassword, mSavedNetwork.credentials, sizeof(mSavedNetwork.credentials), &credentialsLen); SuccessOrExit(err); - err = PersistedStorage::KeyValueStoreMgr().Get(BLConfig::kConfigKey_WiFiPassword, mSavedNetwork.ssid, - sizeof(mSavedNetwork.ssid), &ssidLen); + err = PersistedStorage::KeyValueStoreMgr().Get(BLConfig::kConfigKey_WiFiSSID, mSavedNetwork.ssid, sizeof(mSavedNetwork.ssid), + &ssidLen); SuccessOrExit(err); mSavedNetwork.credentialsLen = credentialsLen; @@ -160,7 +160,33 @@ CHIP_ERROR BLWiFiDriver::ConnectWiFiNetwork(const char * ssid, uint8_t ssidLen, memcpy(passwd, key, keyLen); wifi_interface_t wifi_interface; wifi_interface = wifi_mgmr_sta_enable(); - wifi_mgmr_sta_connect(&wifi_interface, wifi_ssid, passwd, NULL, NULL, 0, 0); + // Valid Credentials length are: + // - 0 bytes: Unsecured (open) connection + // - 5 bytes: WEP-64 passphrase + // - 10 hexadecimal ASCII characters: WEP-64 40-bit hex raw PSK + // - 13 bytes: WEP-128 passphrase + // - 26 hexadecimal ASCII characters: WEP-128 104-bit hex raw PSK + // - 8..63 bytes: WPA/WPA2/WPA3 passphrase + // - 64 bytes: WPA/WPA2/WPA3 raw hex PSK + // Note 10 hex WEP64 and 13 bytes / 26 hex WEP128 passphrase are covered by 8~63 bytes WPA passphrase, so we don't check WEP64 + // hex and WEP128 passphrase. + if (keyLen == BLWiFiDriver::WiFiCredentialLength::kOpen || keyLen == BLWiFiDriver::WiFiCredentialLength::kWEP64 || + (keyLen >= BLWiFiDriver::WiFiCredentialLength::kMinWPAPSK && keyLen <= BLWiFiDriver::WiFiCredentialLength::kMaxWPAPSK)) + { + + if (keyLen == BLWiFiDriver::WiFiCredentialLength::kOpen) + { + wifi_mgmr_sta_connect(&wifi_interface, wifi_ssid, NULL, NULL, NULL, 0, 0); + } + else + { + wifi_mgmr_sta_connect(&wifi_interface, wifi_ssid, passwd, NULL, NULL, 0, 0); + } + } + else + { + return CHIP_ERROR_INVALID_STRING_LENGTH; + } return CHIP_NO_ERROR; } diff --git a/src/platform/bouffalolab/BL602/NetworkCommissioningDriver.h b/src/platform/bouffalolab/BL602/NetworkCommissioningDriver.h index 7e0fd1b30727a1..83416a534988ac 100644 --- a/src/platform/bouffalolab/BL602/NetworkCommissioningDriver.h +++ b/src/platform/bouffalolab/BL602/NetworkCommissioningDriver.h @@ -86,6 +86,14 @@ class BLWiFiDriver final : public WiFiDriver char credentials[DeviceLayer::Internal::kMaxWiFiKeyLength]; uint8_t credentialsLen = 0; }; + enum WiFiCredentialLength + { + kOpen = 0, + kWEP64 = 5, + kMinWPAPSK = 8, + kMaxWPAPSK = 63, + kWPAPSKHex = 64, + }; // BaseDriver NetworkIterator * GetNetworks() override { return new WiFiNetworkIterator(this); } @@ -116,7 +124,6 @@ class BLWiFiDriver final : public WiFiDriver CHIP_ERROR SetLastDisconnectReason(const ChipDeviceEvent * event); int32_t GetLastDisconnectReason(); - static BLWiFiDriver & GetInstance() { static BLWiFiDriver instance; diff --git a/src/platform/bouffalolab/BL702/NetworkCommissioningDriver.cpp b/src/platform/bouffalolab/BL702/NetworkCommissioningDriver.cpp index 6087fce9d94f66..252a1eba7f2475 100644 --- a/src/platform/bouffalolab/BL702/NetworkCommissioningDriver.cpp +++ b/src/platform/bouffalolab/BL702/NetworkCommissioningDriver.cpp @@ -132,7 +132,33 @@ Status BLWiFiDriver::ReorderNetwork(ByteSpan networkId, uint8_t index, MutableCh CHIP_ERROR BLWiFiDriver::ConnectWiFiNetwork(const char * ssid, uint8_t ssidLen, const char * key, uint8_t keyLen) { ChipLogProgress(NetworkProvisioning, "ConnectWiFiNetwork"); - wifiInterface_connect((char *) ssid, (char *) key); + // Valid Credentials length are: + // - 0 bytes: Unsecured (open) connection + // - 5 bytes: WEP-64 passphrase + // - 10 hexadecimal ASCII characters: WEP-64 40-bit hex raw PSK + // - 13 bytes: WEP-128 passphrase + // - 26 hexadecimal ASCII characters: WEP-128 104-bit hex raw PSK + // - 8..63 bytes: WPA/WPA2/WPA3 passphrase + // - 64 bytes: WPA/WPA2/WPA3 raw hex PSK + // Note 10 hex WEP64 and 13 bytes / 26 hex WEP128 passphrase are covered by 8~63 bytes WPA passphrase, so we don't check WEP64 + // hex and WEP128 passphrase. + if (keyLen == BLWiFiDriver::WiFiCredentialLength::kOpen || keyLen == BLWiFiDriver::WiFiCredentialLength::kWEP64 || + (keyLen >= BLWiFiDriver::WiFiCredentialLength::kMinWPAPSK && keyLen <= BLWiFiDriver::WiFiCredentialLength::kMaxWPAPSK)) + { + + if (keyLen == BLWiFiDriver::WiFiCredentialLength::kOpen) + { + wifiInterface_connect((char *) ssid, NULL); + } + else + { + wifiInterface_connect((char *) ssid, (char *) key); + } + } + else + { + return CHIP_ERROR_INVALID_STRING_LENGTH; + } ConnectivityMgrImpl().ChangeWiFiStationState(ConnectivityManager::kWiFiStationState_Connecting); return CHIP_NO_ERROR; } diff --git a/src/platform/bouffalolab/BL702/NetworkCommissioningDriver.h b/src/platform/bouffalolab/BL702/NetworkCommissioningDriver.h index 48bf7d098c869f..caa071ddd467b2 100644 --- a/src/platform/bouffalolab/BL702/NetworkCommissioningDriver.h +++ b/src/platform/bouffalolab/BL702/NetworkCommissioningDriver.h @@ -78,7 +78,14 @@ class BLWiFiDriver final : public WiFiDriver char credentials[DeviceLayer::Internal::kMaxWiFiKeyLength + 1]; uint8_t credentialsLen = 0; }; - + enum WiFiCredentialLength + { + kOpen = 0, + kWEP64 = 5, + kMinWPAPSK = 8, + kMaxWPAPSK = 63, + kWPAPSKHex = 64, + }; // BaseDriver NetworkIterator * GetNetworks() override { return new WiFiNetworkIterator(this); } CHIP_ERROR Init(NetworkStatusChangeCallback * networkStatusChangeCallback) override; diff --git a/src/platform/bouffalolab/BL702/ThreadStackManagerImpl.cpp b/src/platform/bouffalolab/BL702/ThreadStackManagerImpl.cpp index c4981afe84a656..94932a9febb8c8 100644 --- a/src/platform/bouffalolab/BL702/ThreadStackManagerImpl.cpp +++ b/src/platform/bouffalolab/BL702/ThreadStackManagerImpl.cpp @@ -103,16 +103,6 @@ extern "C" otInstance * otrGetInstance() return ThreadStackMgrImpl().OTInstance(); } -extern "C" void * otPlatCAlloc(size_t aNum, size_t aSize) -{ - return calloc(aNum, aSize); -} - -extern "C" void otPlatFree(void * aPtr) -{ - free(aPtr); -} - extern "C" uint32_t otrEnterCrit(void) { if (xPortIsInsideInterrupt()) diff --git a/src/platform/bouffalolab/BL702L/ThreadStackManagerImpl.cpp b/src/platform/bouffalolab/BL702L/ThreadStackManagerImpl.cpp index 1b1ebf197e006f..f2996526e84445 100644 --- a/src/platform/bouffalolab/BL702L/ThreadStackManagerImpl.cpp +++ b/src/platform/bouffalolab/BL702L/ThreadStackManagerImpl.cpp @@ -106,16 +106,6 @@ extern "C" otInstance * otrGetInstance() return ThreadStackMgrImpl().OTInstance(); } -extern "C" void * otPlatCAlloc(size_t aNum, size_t aSize) -{ - return calloc(aNum, aSize); -} - -extern "C" void otPlatFree(void * aPtr) -{ - free(aPtr); -} - extern "C" ot_system_event_t otrGetNotifyEvent(void) { ot_system_event_t sevent = OT_SYSTEM_EVENT_NONE; diff --git a/src/platform/bouffalolab/common/BLConfig.cpp b/src/platform/bouffalolab/common/BLConfig.cpp index de1cc24d139508..ff03eb662a1bb7 100644 --- a/src/platform/bouffalolab/common/BLConfig.cpp +++ b/src/platform/bouffalolab/common/BLConfig.cpp @@ -111,9 +111,21 @@ CHIP_ERROR BLConfig::WriteConfigValue(const char * key, uint8_t * val, size_t si ef_port_env_lock(); - if (size && val) + if (size) { - ret = ef_set_env_blob(key, val, size); + if (val) + { + ret = ef_set_env_blob(key, val, size); + } + else + { + ret = EF_ENV_ARG_ERR; + } + } + else + { + uint32_t value_null = 0; + ret = ef_set_env_blob(key, &value_null, size); } ef_port_env_unlock(); @@ -240,11 +252,22 @@ CHIP_ERROR BLConfig::WriteKVS(const char * key, const void * value, size_t value ef_port_env_lock(); - if (value && value_size) + if (value_size) { - ret = ef_set_env_blob(key, value, value_size); + if (value) + { + ret = ef_set_env_blob(key, value, value_size); + } + else + { + ret = EF_ENV_ARG_ERR; + } + } + else + { + uint32_t value_null = 0; + ret = ef_set_env_blob(key, &value_null, value_size); } - ef_port_env_unlock(); if (ret == EF_NO_ERR) diff --git a/third_party/bouffalolab/repo b/third_party/bouffalolab/repo index dbca8b07013852..0c5772fb31ad08 160000 --- a/third_party/bouffalolab/repo +++ b/third_party/bouffalolab/repo @@ -1 +1 @@ -Subproject commit dbca8b07013852985a8bcea9cc56f486f53c77bd +Subproject commit 0c5772fb31ad083cafdedc9a9de578d3abd5a234