Skip to content

Commit

Permalink
[Ameba] Autonomous connection status networkcommissioning driver (#17534
Browse files Browse the repository at this point in the history
)

* [NetworkCommissioning] Implement callback for wifi disconnect event
- Add new kRtkWiFiStationDisconnectedEvent event
- Add callbacks for wifi disconnect event
- Update network commissioning driver
- Get last disconnect reason

* [NetworkCommissioning] Fix hardfault during commissioning
- Move registration of wifi event handlers to ConnectivityManagerImpl::_Init()
- Set mpStatusChangeCallback at the start of AmebaWiFiDriver::Init()
- Clean up loggings
  • Loading branch information
pankore authored Apr 20, 2022
1 parent 8f6ba74 commit 00e3dfb
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/platform/Ameba/CHIPDevicePlatformEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ enum InternalPlatformSpecificEventTypes
kCHIPoBLERXCharWriteEvent,
kCHIPoBLETXCharWriteEvent,
kRtkWiFiStationConnectedEvent,
kRtkWiFiStationDisconnectedEvent,
kRtkWiFiScanCompletedEvent,
};

Expand Down
23 changes: 22 additions & 1 deletion src/platform/Ameba/ConnectivityManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ CHIP_ERROR ConnectivityManagerImpl::_Init()

// Set callback functions from chip_porting
chip_connmgr_set_callback_func((chip_connmgr_callback)(conn_callback_dispatcher), this);

// Register WiFi event handlers
wifi_reg_event_handler(WIFI_EVENT_CONNECT, ConnectivityManagerImpl::RtkWiFiStationConnectedHandler, NULL);
wifi_reg_event_handler(WIFI_EVENT_DISCONNECT, ConnectivityManagerImpl::RtkWiFiStationDisconnectedHandler, NULL);

// Ensure that station mode is enabled.
wifi_on(RTW_MODE_STA);
Expand Down Expand Up @@ -148,6 +152,15 @@ void ConnectivityManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event)
DriveStationState();
DHCPProcess();
}
if (event->Type == DeviceEventType::kRtkWiFiStationDisconnectedEvent)
{
ChipLogProgress(DeviceLayer, "WiFiStationDisconnected");
NetworkCommissioning::AmebaWiFiDriver::GetInstance().SetLastDisconnectReason(event);
if (mWiFiStationState == kWiFiStationState_Connecting)
{
ChangeWiFiStationState(kWiFiStationState_Connecting_Failed);
}
}
if (event->Type == DeviceEventType::kRtkWiFiScanCompletedEvent)
{
ChipLogProgress(DeviceLayer, "WiFiScanCompleted");
Expand Down Expand Up @@ -540,7 +553,6 @@ void ConnectivityManagerImpl::DriveStationState()
ChipLogProgress(DeviceLayer, "Attempting to connect WiFi station interface");
rtw_wifi_setting_t wifi_info;
CHIP_GetWiFiConfig(&wifi_info);
wifi_reg_event_handler(WIFI_EVENT_CONNECT, ConnectivityManagerImpl::RtkWiFiStationConnectedHandler, NULL);
wifi_connect((char *) wifi_info.ssid, RTW_SECURITY_WPA_WPA2_MIXED, (char *) wifi_info.password,
strlen((const char *) wifi_info.ssid), strlen((const char *) wifi_info.password), 0, NULL);
ChangeWiFiStationState(kWiFiStationState_Connecting);
Expand Down Expand Up @@ -593,6 +605,7 @@ void ConnectivityManagerImpl::ChangeWiFiStationState(WiFiStationState newState)
{
ChipLogProgress(DeviceLayer, "WiFi station state change: %d -> %d", (mWiFiStationState), (newState));
mWiFiStationState = newState;
SystemLayer().ScheduleLambda([]() { NetworkCommissioning::AmebaWiFiDriver::GetInstance().OnNetworkStatusChange(); });
}
}

Expand Down Expand Up @@ -786,6 +799,14 @@ void ConnectivityManagerImpl::RtkWiFiStationConnectedHandler(char * buf, int buf
PlatformMgr().PostEventOrDie(&event);
}

void ConnectivityManagerImpl::RtkWiFiStationDisconnectedHandler(char * buf, int buf_len, int flags, void * userdata)
{
ChipDeviceEvent event;
memset(&event, 0, sizeof(event));
event.Type = DeviceEventType::kRtkWiFiStationDisconnectedEvent;
PlatformMgr().PostEventOrDie(&event);
}

void ConnectivityManagerImpl::RtkWiFiScanCompletedHandler(void)
{
ChipDeviceEvent event;
Expand Down
1 change: 1 addition & 0 deletions src/platform/Ameba/ConnectivityManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class ConnectivityManagerImpl final : public ConnectivityManager,
static ConnectivityManagerImpl sInstance;
static void RefreshMessageLayer(void);
static void RtkWiFiStationConnectedHandler(char * buf, int buf_len, int flags, void * userdata);
static void RtkWiFiStationDisconnectedHandler(char * buf, int buf_len, int flags, void * userdata);
static void RtkWiFiScanCompletedHandler(void);
void DHCPProcess(void);
static void DHCPProcessThread(void * param);
Expand Down
7 changes: 7 additions & 0 deletions src/platform/Ameba/NetworkCommissioningDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ class AmebaWiFiDriver final : public WiFiDriver
CHIP_ERROR ConnectWiFiNetwork(const char * ssid, uint8_t ssidLen, const char * key, uint8_t keyLen);
void OnConnectWiFiNetwork();
void OnScanWiFiNetworkDone();
void OnNetworkStatusChange();

CHIP_ERROR SetLastDisconnectReason(const ChipDeviceEvent * event);
int32_t GetLastDisconnectReason();

static AmebaWiFiDriver & GetInstance()
{
static AmebaWiFiDriver instance;
Expand All @@ -127,6 +132,8 @@ class AmebaWiFiDriver final : public WiFiDriver
WiFiNetwork mStagingNetwork;
ScanCallback * mpScanCallback;
ConnectCallback * mpConnectCallback;
NetworkStatusChangeCallback * mpStatusChangeCallback = nullptr;
int32_t mLastDisconnectedReason;
};

} // namespace NetworkCommissioning
Expand Down
88 changes: 66 additions & 22 deletions src/platform/Ameba/NetworkCommissioningWiFiDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,37 @@ constexpr char kWiFiSSIDKeyName[] = "wifi-ssid";
constexpr char kWiFiCredentialsKeyName[] = "wifi-pass";
} // namespace

CHIP_ERROR AmebaWiFiDriver::Init(NetworkStatusChangeCallback *)
CHIP_ERROR AmebaWiFiDriver::Init(NetworkStatusChangeCallback * networkStatusChangeCallback)
{
CHIP_ERROR err;
size_t ssidLen = 0;
size_t credentialsLen = 0;
mpScanCallback = nullptr;
mpConnectCallback = nullptr;
mpStatusChangeCallback = networkStatusChangeCallback;

err = PersistedStorage::KeyValueStoreMgr().Get(kWiFiCredentialsKeyName, mSavedNetwork.credentials,
sizeof(mSavedNetwork.credentials), &credentialsLen);
if (err == CHIP_ERROR_NOT_FOUND)
if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
{
return CHIP_NO_ERROR;
}

err = PersistedStorage::KeyValueStoreMgr().Get(kWiFiSSIDKeyName, mSavedNetwork.ssid, sizeof(mSavedNetwork.ssid), &ssidLen);
if (err == CHIP_ERROR_NOT_FOUND)
if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
{
return CHIP_NO_ERROR;
}
mSavedNetwork.credentialsLen = credentialsLen;
mSavedNetwork.ssidLen = ssidLen;

mStagingNetwork = mSavedNetwork;
mpScanCallback = nullptr;
mpConnectCallback = nullptr;
return err;
}

CHIP_ERROR AmebaWiFiDriver::Shutdown()
{
mpStatusChangeCallback = nullptr;
return CHIP_NO_ERROR;
}

Expand Down Expand Up @@ -244,6 +246,49 @@ void AmebaWiFiDriver::OnScanWiFiNetworkDone()
vPortFree(ScanResult);
}

CHIP_ERROR GetConfiguredNetwork(Network & network)
{
rtw_wifi_setting_t wifi_setting;
CHIP_GetWiFiConfig(&wifi_setting);

uint8_t length = strnlen(reinterpret_cast<const char *>(wifi_setting.ssid), DeviceLayer::Internal::kMaxWiFiSSIDLength);
if (length > sizeof(network.networkID))
{
ChipLogError(DeviceLayer, "SSID too long");
return CHIP_ERROR_INTERNAL;
}

memcpy(network.networkID, wifi_setting.ssid, length);
network.networkIDLen = length;
return CHIP_NO_ERROR;
}

void AmebaWiFiDriver::OnNetworkStatusChange()
{
Network configuredNetwork;
rtw_wifi_setting_t mWiFiSetting;
CHIP_GetWiFiConfig(&mWiFiSetting);
bool staEnable = (mWiFiSetting.mode == RTW_MODE_STA || mWiFiSetting.mode == RTW_MODE_STA_AP);
bool staConnected = (mWiFiSetting.ssid[0] != 0);

CHIP_ERROR err = GetConfiguredNetwork(configuredNetwork);
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Failed to get configured network when updating network status: %s", err.AsString());
return;
}

if (staConnected)
{
mpStatusChangeCallback->OnNetworkingStatusChange(
Status::kSuccess, MakeOptional(ByteSpan(configuredNetwork.networkID, configuredNetwork.networkIDLen)), NullOptional);
return;
}
mpStatusChangeCallback->OnNetworkingStatusChange(
Status::kUnknownError, MakeOptional(ByteSpan(configuredNetwork.networkID, configuredNetwork.networkIDLen)),
MakeOptional(GetLastDisconnectReason()));
}

void AmebaWiFiDriver::ScanNetworks(ByteSpan ssid, WiFiDriver::ScanCallback * callback)
{
if (callback != nullptr)
Expand All @@ -257,23 +302,18 @@ void AmebaWiFiDriver::ScanNetworks(ByteSpan ssid, WiFiDriver::ScanCallback * cal
}
}

CHIP_ERROR GetConnectedNetwork(Network & network)
CHIP_ERROR AmebaWiFiDriver::SetLastDisconnectReason(const ChipDeviceEvent * event)
{
rtw_wifi_setting_t wifi_setting;
CHIP_GetWiFiConfig(&wifi_setting);

uint8_t length = strnlen(reinterpret_cast<const char *>(wifi_setting.ssid), DeviceLayer::Internal::kMaxWiFiSSIDLength);
if (length > sizeof(network.networkID))
{
ChipLogError(DeviceLayer, "SSID too long");
return CHIP_ERROR_INTERNAL;
}

memcpy(network.networkID, wifi_setting.ssid, length);
network.networkIDLen = length;
VerifyOrReturnError(event->Type == DeviceEventType::kRtkWiFiStationDisconnectedEvent, CHIP_ERROR_INVALID_ARGUMENT);
mLastDisconnectedReason = wifi_get_last_error();
return CHIP_NO_ERROR;
}

int32_t AmebaWiFiDriver::GetLastDisconnectReason()
{
return mLastDisconnectedReason;
}

size_t AmebaWiFiDriver::WiFiNetworkIterator::Count()
{
return mDriver->mStagingNetwork.ssidLen == 0 ? 0 : 1;
Expand All @@ -290,12 +330,16 @@ bool AmebaWiFiDriver::WiFiNetworkIterator::Next(Network & item)
item.connected = false;
mExhausted = true;

Network connectedNetwork;
CHIP_ERROR err = GetConnectedNetwork(connectedNetwork);
Network configuredNetwork;
CHIP_ERROR err = GetConfiguredNetwork(configuredNetwork);
if (err == CHIP_NO_ERROR)
{
if (connectedNetwork.networkIDLen == item.networkIDLen &&
memcmp(connectedNetwork.networkID, item.networkID, item.networkIDLen) == 0)
rtw_wifi_setting_t mWiFiSetting;
CHIP_GetWiFiConfig(&mWiFiSetting);
bool isConnected = (mWiFiSetting.ssid[0] != 0);

if (isConnected && configuredNetwork.networkIDLen == item.networkIDLen &&
memcmp(configuredNetwork.networkID, item.networkID, item.networkIDLen) == 0)
{
item.connected = true;
}
Expand Down

0 comments on commit 00e3dfb

Please sign in to comment.