Skip to content

Commit

Permalink
[BouffaloLab]BL602&BL702 Support OpenNetwork
Browse files Browse the repository at this point in the history
  • Loading branch information
shchen-Lab committed Sep 5, 2023
1 parent 54038c0 commit f28bf9e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
32 changes: 28 additions & 4 deletions src/platform/bouffalolab/BL602/NetworkCommissioningDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ CHIP_ERROR BLWiFiDriver::Init(NetworkStatusChangeCallback * networkStatusChangeC
err = PersistedStorage::KeyValueStoreMgr().Get(BLConfig::kConfigKey_WiFiPassword, mSavedNetwork.ssid,
sizeof(mSavedNetwork.ssid), &ssidLen);
SuccessOrExit(err);

if(credentialsLen==1)
{
credentialsLen=0;
}
mSavedNetwork.credentialsLen = credentialsLen;
mSavedNetwork.ssidLen = ssidLen;

Expand Down Expand Up @@ -79,8 +82,17 @@ CHIP_ERROR BLWiFiDriver::CommitConfiguration()
ChipLogProgress(NetworkProvisioning, "BLWiFiDriver::CommitConfiguration");
ReturnErrorOnFailure(
PersistedStorage::KeyValueStoreMgr().Put(BLConfig::kConfigKey_WiFiSSID, mStagingNetwork.ssid, mStagingNetwork.ssidLen));
ReturnErrorOnFailure(PersistedStorage::KeyValueStoreMgr().Put(BLConfig::kConfigKey_WiFiPassword, mStagingNetwork.credentials,
if(mStagingNetwork.credentialsLen==0)
{
mStagingNetwork.credentialsLen=1;
ReturnErrorOnFailure(PersistedStorage::KeyValueStoreMgr().Put(BLConfig::kConfigKey_WiFiPassword, mStagingNetwork.credentials,
mStagingNetwork.credentialsLen));
}
else
{
ReturnErrorOnFailure(PersistedStorage::KeyValueStoreMgr().Put(BLConfig::kConfigKey_WiFiPassword, mStagingNetwork.credentials,
mStagingNetwork.credentialsLen));
}
mSavedNetwork = mStagingNetwork;
return CHIP_NO_ERROR;
}
Expand All @@ -106,9 +118,11 @@ Status BLWiFiDriver::AddOrUpdateNetwork(ByteSpan ssid, ByteSpan credentials, Mut
VerifyOrReturnError(credentials.size() <= sizeof(mStagingNetwork.credentials), Status::kOutOfRange);
VerifyOrReturnError(ssid.size() <= sizeof(mStagingNetwork.ssid), Status::kOutOfRange);

memset(mStagingNetwork.credentials, 0, sizeof(mStagingNetwork.credentials));
memcpy(mStagingNetwork.credentials, credentials.data(), credentials.size());
mStagingNetwork.credentialsLen = static_cast<decltype(mStagingNetwork.credentialsLen)>(credentials.size());

memset(mStagingNetwork.ssid, 0, sizeof(mStagingNetwork.ssid));
memcpy(mStagingNetwork.ssid, ssid.data(), ssid.size());
mStagingNetwork.ssidLen = static_cast<decltype(mStagingNetwork.ssidLen)>(ssid.size());

Expand Down Expand Up @@ -157,10 +171,20 @@ CHIP_ERROR BLWiFiDriver::ConnectWiFiNetwork(const char * ssid, uint8_t ssidLen,
}

memcpy(wifi_ssid, ssid, ssidLen);
memcpy(passwd, key, keyLen);
if(keyLen!=0)
{
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);
if(keyLen!=0)
{
wifi_mgmr_sta_connect(&wifi_interface, wifi_ssid, passwd, NULL, NULL, 0, 0);
}
else
{
wifi_mgmr_sta_connect(&wifi_interface, wifi_ssid, NULL, NULL, NULL, 0, 0);
}

return CHIP_NO_ERROR;
}
Expand Down
28 changes: 25 additions & 3 deletions src/platform/bouffalolab/BL702/NetworkCommissioningDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ CHIP_ERROR BLWiFiDriver::Init(NetworkStatusChangeCallback * networkStatusChangeC
err = PersistedStorage::KeyValueStoreMgr().Get(BLConfig::kConfigKey_WiFiPassword, mSavedNetwork.credentials,
sizeof(mSavedNetwork.credentials), &credentialsLen);
SuccessOrExit(err);

if(credentialsLen==1)
{
credentialsLen=0;
}
mSavedNetwork.credentialsLen = credentialsLen;
mSavedNetwork.ssidLen = ssidLen;
mSavedNetwork.credentials[mSavedNetwork.credentialsLen] = '\0';
Expand Down Expand Up @@ -73,8 +76,18 @@ CHIP_ERROR BLWiFiDriver::CommitConfiguration()
ChipLogProgress(NetworkProvisioning, "BLWiFiDriver::CommitConfiguration");
ReturnErrorOnFailure(
PersistedStorage::KeyValueStoreMgr().Put(BLConfig::kConfigKey_WiFiSSID, mStagingNetwork.ssid, mStagingNetwork.ssidLen));
ReturnErrorOnFailure(PersistedStorage::KeyValueStoreMgr().Put(BLConfig::kConfigKey_WiFiPassword, mStagingNetwork.credentials,
if(mStagingNetwork.credentialsLen==0)
{
mStagingNetwork.credentialsLen=1;
ReturnErrorOnFailure(PersistedStorage::KeyValueStoreMgr().Put(BLConfig::kConfigKey_WiFiPassword, mStagingNetwork.credentials,
mStagingNetwork.credentialsLen));
}
else
{
ReturnErrorOnFailure(PersistedStorage::KeyValueStoreMgr().Put(BLConfig::kConfigKey_WiFiPassword, mStagingNetwork.credentials,
mStagingNetwork.credentialsLen));
}

mSavedNetwork = mStagingNetwork;
return CHIP_NO_ERROR;
}
Expand All @@ -100,9 +113,11 @@ Status BLWiFiDriver::AddOrUpdateNetwork(ByteSpan ssid, ByteSpan credentials, Mut
VerifyOrReturnError(credentials.size() <= sizeof(mStagingNetwork.credentials), Status::kOutOfRange);
VerifyOrReturnError(ssid.size() <= sizeof(mStagingNetwork.ssid), Status::kOutOfRange);

memset(mStagingNetwork.credentials, 0, sizeof(mStagingNetwork.credentials));
memcpy(mStagingNetwork.credentials, credentials.data(), credentials.size());
mStagingNetwork.credentialsLen = static_cast<decltype(mStagingNetwork.credentialsLen)>(credentials.size());

memset(mStagingNetwork.ssid, 0, sizeof(mStagingNetwork.ssid));
memcpy(mStagingNetwork.ssid, ssid.data(), ssid.size());
mStagingNetwork.ssidLen = static_cast<decltype(mStagingNetwork.ssidLen)>(ssid.size());

Expand Down Expand Up @@ -132,7 +147,14 @@ 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);
if(keyLen==0)
{
wifiInterface_connect((char *) ssid, NULL);
}
else
{
wifiInterface_connect((char *) ssid, (char *) key);
}
ConnectivityMgrImpl().ChangeWiFiStationState(ConnectivityManager::kWiFiStationState_Connecting);
return CHIP_NO_ERROR;
}
Expand Down
5 changes: 4 additions & 1 deletion src/platform/bouffalolab/common/ConnectivityManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ CHIP_ERROR ConnectivityManagerImpl::ConnectProvisionedWiFiNetwork(void)

ReturnErrorOnFailure(PersistedStorage::KeyValueStoreMgr().Get(BLConfig::kConfigKey_WiFiSSID, (void *) ssid, 64, &ssidLen, 0));
ReturnErrorOnFailure(PersistedStorage::KeyValueStoreMgr().Get(BLConfig::kConfigKey_WiFiPassword, (void *) psk, 64, &pskLen, 0));

if(pskLen==1)
{
pskLen=0;
}
NetworkCommissioning::BLWiFiDriver::GetInstance().ConnectWiFiNetwork(ssid, ssidLen, psk, pskLen);

return CHIP_NO_ERROR;
Expand Down

0 comments on commit f28bf9e

Please sign in to comment.