Skip to content

Commit

Permalink
[Ameba] open security, fastconnect, onnetwork fix (#28512)
Browse files Browse the repository at this point in the history
* [wifi] open security, fastconnect, onnetwork fix

* Ran restyle-diff.sh
  • Loading branch information
pankore authored and pull[bot] committed Mar 15, 2024
1 parent b6ea4f4 commit 1373764
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 37 deletions.
56 changes: 53 additions & 3 deletions src/platform/Ameba/AmebaUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,47 @@ constexpr char kWiFiCredentialsKeyName[] = "wifi-pass";

CHIP_ERROR AmebaUtils::StartWiFi(void)
{
// Ensure that the WiFi layer is started.
int32_t error = matter_wifi_on(RTW_MODE_STA);
CHIP_ERROR err = MapError(error, AmebaErrorType::kWiFiError);
bool staEnabled;
int32_t error;
CHIP_ERROR err;

if (wifi_mode == RTW_MODE_AP)
{
ChipLogError(DeviceLayer, "StartWiFi(): Does not support RTW_MODE_AP, change to RTW_MODE_STA_AP");
error = matter_wifi_set_mode(RTW_MODE_STA_AP);
err = MapError(error, AmebaErrorType::kWiFiError);

if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "matter_wifi_on(RTW_MODE_STA_AP) failed");
return err;
}
}

if (!(IsStationInterfaceUp()))
{
ChipLogError(DeviceLayer, "StartWiFi() setting Wi-Fi interface");
error = matter_wifi_set_mode(RTW_MODE_STA);
err = MapError(error, AmebaErrorType::kWiFiError);

if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "matter_wifi_set_mode(RTW_MODE_STA) failed");
return err;
}
}
exit:
return CHIP_NO_ERROR; // will fail if wifi is already initialized, let it pass
}

bool AmebaUtils::IsStationInterfaceUp(void)
{
/* Station mode will only be setup in Interface 0 in both station only and concurrent mode
* Thus, only check Interface 0 for station mode
*/
return matter_wifi_is_up(RTW_STA_INTERFACE);
}

CHIP_ERROR AmebaUtils::IsStationEnabled(bool & staEnabled)
{
staEnabled = (wifi_mode == RTW_MODE_STA || wifi_mode == RTW_MODE_STA_AP);
Expand All @@ -58,6 +93,19 @@ bool AmebaUtils::IsStationProvisioned(void)
return ((GetWiFiConfig(&WiFiConfig) == CHIP_NO_ERROR) && (WiFiConfig.ssid[0] != 0));
}

CHIP_ERROR AmebaUtils::IsStationIPLinked(bool & linked)
{
CHIP_ERROR err = CHIP_NO_ERROR;
linked = (matter_wifi_is_ready_to_transceive(RTW_STA_INTERFACE) == RTW_SUCCESS) ? 1 : 0;
return err;
}

bool AmebaUtils::IsStationOpenSecurity(void)
{
bool is_open_security = (matter_wifi_is_open_security()) ? 1 : 0;
return is_open_security;
}

CHIP_ERROR AmebaUtils::IsStationConnected(bool & connected)
{
int32_t error = matter_wifi_is_connected_to_ap();
Expand Down Expand Up @@ -182,6 +230,7 @@ CHIP_ERROR AmebaUtils::SetCurrentProvisionedNetwork()
if (!memcmp(config.ssid, pSetting.ssid, strlen((const char *) pSetting.ssid) + 1))
{
ChipLogProgress(DeviceLayer, "STA Wi-Fi Info exist, do nothing");
matter_set_autoreconnect(0);
goto exit;
}
else
Expand All @@ -194,6 +243,7 @@ CHIP_ERROR AmebaUtils::SetCurrentProvisionedNetwork()
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "SetWiFiConfig() failed");
matter_set_autoreconnect(0);
goto exit;
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/platform/Ameba/AmebaUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ class AmebaUtils
public:
static CHIP_ERROR StartWiFi(void);
static CHIP_ERROR IsStationEnabled(bool & staEnabled);
static bool IsStationInterfaceUp(void);
static bool IsStationProvisioned(void);
static CHIP_ERROR IsStationConnected(bool & connected);
static CHIP_ERROR IsStationIPLinked(bool & linked);
static bool IsStationOpenSecurity(void);
static CHIP_ERROR EnableStationMode(void);
static CHIP_ERROR SetWiFiConfig(rtw_wifi_config_t * config);
static CHIP_ERROR GetWiFiConfig(rtw_wifi_config_t * config);
Expand Down
1 change: 1 addition & 0 deletions src/platform/Ameba/CHIPDevicePlatformEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ enum InternalPlatformSpecificEventTypes
kRtkWiFiStationConnectedEvent,
kRtkWiFiStationDisconnectedEvent,
kRtkWiFiScanCompletedEvent,
kRtkWiFiDHCPCompletedEvent,
};

} // namespace DeviceEventType
Expand Down
76 changes: 42 additions & 34 deletions src/platform/Ameba/ConnectivityManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,46 +83,23 @@ CHIP_ERROR ConnectivityManagerImpl::_Init()
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_FOURWAY_HANDSHAKE_DONE, ConnectivityManagerImpl::RtkWiFiStationConnectedHandler, NULL);
wifi_reg_event_handler(WIFI_EVENT_DISCONNECT, ConnectivityManagerImpl::RtkWiFiStationDisconnectedHandler, NULL);
wifi_reg_event_handler(WIFI_EVENT_DHCP6_DONE, ConnectivityManagerImpl::RtkWiFiDHCPCompletedHandler, NULL);

err = Internal::AmebaUtils::StartWiFi();
SuccessOrExit(err);
err = Internal::AmebaUtils::EnableStationMode();
SuccessOrExit(err);
bool stationIPLinked;
err = Internal::AmebaUtils::IsStationIPLinked(stationIPLinked);

// If there is no persistent station provision...
if (!IsWiFiStationProvisioned())
if (stationIPLinked)
{
// If the code has been compiled with a default WiFi station provision, configure that now.
#if !defined(CONFIG_DEFAULT_WIFI_SSID)
ChipLogProgress(DeviceLayer, "Please define CONFIG_DEFAULT_WIFI_SSID");
#else
if (CONFIG_DEFAULT_WIFI_SSID[0] != 0)
{
ChipLogProgress(DeviceLayer, "Setting default WiFi station configuration (SSID: %s)", CONFIG_DEFAULT_WIFI_SSID);

// Set a default station configuration.
rtw_wifi_config_t wifiConfig;
memset(&wifiConfig, 0, sizeof(wifiConfig));
memcpy(wifiConfig.ssid, CONFIG_DEFAULT_WIFI_SSID, strlen(CONFIG_DEFAULT_WIFI_SSID) + 1);
memcpy(wifiConfig.password, CONFIG_DEFAULT_WIFI_PASSWORD, strlen(CONFIG_DEFAULT_WIFI_PASSWORD) + 1);
wifiConfig.mode = RTW_MODE_STA;

// Configure the WiFi interface.
err = Internal::AmebaUtils::SetWiFiConfig(&wifiConfig);
SuccessOrExit(err);

// Enable WiFi station mode.
ReturnErrorOnFailure(SetWiFiStationMode(kWiFiStationMode_Enabled));
}

// Otherwise, ensure WiFi station mode is disabled.
else
{
ReturnErrorOnFailure(SetWiFiStationMode(kWiFiStationMode_Disabled));
}
#endif
ChangeWiFiStationState(kWiFiStationState_Connecting_Succeeded);
}
else
{
ChangeWiFiStationState(kWiFiStationState_NotConnected);
}

// Force AP mode off for now.
Expand Down Expand Up @@ -180,6 +157,17 @@ void ConnectivityManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event)
ChipLogProgress(DeviceLayer, "WiFiScanCompleted");
NetworkCommissioning::AmebaWiFiDriver::GetInstance().OnScanWiFiNetworkDone();
}
if (event->Type == DeviceEventType::kRtkWiFiDHCPCompletedEvent)
{
ChipLogProgress(DeviceLayer, "WiFiDHCPCompleted");
const bool hadIPv4Conn = mFlags.Has(ConnectivityFlags::kHaveIPv4InternetConnectivity);
const bool hadIPv6Conn = mFlags.Has(ConnectivityFlags::kHaveIPv6InternetConnectivity);
if (!(hadIPv4Conn || hadIPv6Conn))
{
ChipLogProgress(DeviceLayer, "WiFiDHCPCompleted: UpdateInternetConnectivityState\n");
UpdateInternetConnectivityState();
}
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI
}

Expand Down Expand Up @@ -523,15 +511,16 @@ void ConnectivityManagerImpl::DriveStationState()
if (mWiFiStationState == kWiFiStationState_Disconnecting || mWiFiStationState == kWiFiStationState_Connecting_Failed)
{
WiFiStationState prevState = mWiFiStationState;
ChangeWiFiStationState(kWiFiStationState_NotConnected);
if (prevState == kWiFiStationState_Connecting_Failed)
{
ChipLogProgress(DeviceLayer, "WiFi station failed to connect");
ChangeWiFiStationState(kWiFiStationState_Connecting);
// TODO: check retry count if exceeded, then clearwificonfig
}
else
{
ChipLogProgress(DeviceLayer, "WiFi station disconnected");
ChangeWiFiStationState(kWiFiStationState_NotConnected);
}
mLastStationConnectFailTime = now;
OnStationDisconnected();
Expand Down Expand Up @@ -818,6 +807,17 @@ void ConnectivityManagerImpl::RefreshMessageLayer(void) {}

void ConnectivityManagerImpl::RtkWiFiStationConnectedHandler(char * buf, int buf_len, int flags, void * userdata)
{
bool stationConnected;
Internal::AmebaUtils::IsStationConnected(stationConnected);
if (Internal::AmebaUtils::IsStationOpenSecurity())
{
// continue
}
else if (!stationConnected)
{
return;
}

ChipDeviceEvent event;
memset(&event, 0, sizeof(event));
event.Type = DeviceEventType::kRtkWiFiStationConnectedEvent;
Expand All @@ -840,6 +840,14 @@ void ConnectivityManagerImpl::RtkWiFiScanCompletedHandler(void)
PlatformMgr().PostEventOrDie(&event);
}

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

void ConnectivityManagerImpl::DHCPProcessThread(void * param)
{
matter_lwip_dhcp();
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 @@ -156,6 +156,7 @@ class ConnectivityManagerImpl final : public ConnectivityManager,
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);
static void RtkWiFiDHCPCompletedHandler(char * buf, int buf_len, int flags, void * userdata);
void DHCPProcess(void);
static void DHCPProcessThread(void * param);
static int conn_callback_dispatcher(void * object);
Expand Down

0 comments on commit 1373764

Please sign in to comment.