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

[Ameba] Error mapping #26532

Closed
wants to merge 5 commits into from
Closed
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
227 changes: 143 additions & 84 deletions src/platform/Ameba/AmebaConfig.cpp

Large diffs are not rendered by default.

99 changes: 76 additions & 23 deletions src/platform/Ameba/AmebaUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ constexpr char kWiFiCredentialsKeyName[] = "wifi-pass";

CHIP_ERROR AmebaUtils::StartWiFi(void)
{
CHIP_ERROR err = CHIP_NO_ERROR;
// Ensure that the WiFi layer is started.
matter_wifi_on(RTW_MODE_STA);
return err;
int32_t error = matter_wifi_on(RTW_MODE_STA);
CHIP_ERROR err = MapError(error, AmebaErrorType::kWiFiError);
return CHIP_NO_ERROR; // will fail if wifi is already initialized, let it pass
}

CHIP_ERROR AmebaUtils::IsStationEnabled(bool & staEnabled)
Expand All @@ -60,22 +60,23 @@ bool AmebaUtils::IsStationProvisioned(void)

CHIP_ERROR AmebaUtils::IsStationConnected(bool & connected)
{
CHIP_ERROR err = CHIP_NO_ERROR;
connected = (matter_wifi_is_connected_to_ap() == RTW_SUCCESS) ? 1 : 0;
int32_t error = matter_wifi_is_connected_to_ap();
CHIP_ERROR err = MapError(error, AmebaErrorType::kWiFiError);
connected = (err == CHIP_NO_ERROR) ? true : false;
return err;
}

CHIP_ERROR AmebaUtils::EnableStationMode(void)
{
CHIP_ERROR err = CHIP_NO_ERROR;
// Ensure that station mode is enabled in the WiFi layer.
matter_wifi_set_mode(RTW_MODE_STA);
int32_t error = matter_wifi_set_mode(RTW_MODE_STA);
CHIP_ERROR err = MapError(error, AmebaErrorType::kWiFiError);
return err;
}

CHIP_ERROR AmebaUtils::SetWiFiConfig(rtw_wifi_config_t * config)
{
CHIP_ERROR err = CHIP_NO_ERROR;
CHIP_ERROR err;
// don't store if ssid is null
if (config->ssid[0] == 0)
{
Expand All @@ -95,7 +96,7 @@ CHIP_ERROR AmebaUtils::SetWiFiConfig(rtw_wifi_config_t * config)

CHIP_ERROR AmebaUtils::GetWiFiConfig(rtw_wifi_config_t * config)
{
CHIP_ERROR err = CHIP_NO_ERROR;
CHIP_ERROR err;
size_t ssidLen = 0;
size_t credentialsLen = 0;

Expand All @@ -117,8 +118,8 @@ CHIP_ERROR AmebaUtils::GetWiFiConfig(rtw_wifi_config_t * config)
CHIP_ERROR AmebaUtils::ClearWiFiConfig()
{
/* Clear Wi-Fi Configurations in Storage */
CHIP_ERROR err = CHIP_NO_ERROR;
err = PersistedStorage::KeyValueStoreMgr().Delete(kWiFiSSIDKeyName);
CHIP_ERROR err;
err = PersistedStorage::KeyValueStoreMgr().Delete(kWiFiSSIDKeyName);
SuccessOrExit(err);

err = PersistedStorage::KeyValueStoreMgr().Delete(kWiFiCredentialsKeyName);
Expand All @@ -130,43 +131,46 @@ CHIP_ERROR AmebaUtils::ClearWiFiConfig()

CHIP_ERROR AmebaUtils::WiFiDisconnect(void)
{
CHIP_ERROR err = CHIP_NO_ERROR;
ChipLogProgress(DeviceLayer, "matter_wifi_disconnect");
err = (matter_wifi_disconnect() == RTW_SUCCESS) ? CHIP_NO_ERROR : CHIP_ERROR_INTERNAL;
ChipLogProgress(DeviceLayer, "Disconnecting WiFi");
int32_t error = matter_wifi_disconnect();
CHIP_ERROR err = MapError(error, AmebaErrorType::kWiFiError);
if (err == CHIP_NO_ERROR)
{
ChipLogProgress(DeviceLayer, "matter_lwip_releaseip");
matter_lwip_releaseip();
}
return err;
}

CHIP_ERROR AmebaUtils::WiFiConnectProvisionedNetwork(void)
{
CHIP_ERROR err = CHIP_NO_ERROR;
rtw_wifi_config_t * config = (rtw_wifi_config_t *) pvPortMalloc(sizeof(rtw_wifi_config_t));
memset(config, 0, sizeof(rtw_wifi_config_t));
GetWiFiConfig(config);
ChipLogProgress(DeviceLayer, "Connecting to AP : [%s]", (char *) config->ssid);
int ameba_err = matter_wifi_connect((char *) config->ssid, RTW_SECURITY_WPA_WPA2_MIXED, (char *) config->password,
int32_t error = matter_wifi_connect((char *) config->ssid, RTW_SECURITY_WPA_WPA2_MIXED, (char *) config->password,
strlen((const char *) config->ssid), strlen((const char *) config->password), 0, nullptr);
CHIP_ERROR err = MapError(error, AmebaErrorType::kWiFiError);

vPortFree(config);
err = (ameba_err == RTW_SUCCESS) ? CHIP_NO_ERROR : CHIP_ERROR_INTERNAL;
return err;
}

CHIP_ERROR AmebaUtils::WiFiConnect(const char * ssid, const char * password)
{
CHIP_ERROR err = CHIP_NO_ERROR;
ChipLogProgress(DeviceLayer, "Connecting to AP : [%s]", (char *) ssid);
int ameba_err = matter_wifi_connect((char *) ssid, RTW_SECURITY_WPA_WPA2_MIXED, (char *) password, strlen(ssid),
int32_t error = matter_wifi_connect((char *) ssid, RTW_SECURITY_WPA_WPA2_MIXED, (char *) password, strlen(ssid),
strlen(password), 0, nullptr);
err = (ameba_err == RTW_SUCCESS) ? CHIP_NO_ERROR : CHIP_ERROR_INTERNAL;
CHIP_ERROR err = MapError(error, AmebaErrorType::kWiFiError);
return err;
}

CHIP_ERROR AmebaUtils::SetCurrentProvisionedNetwork()
{
CHIP_ERROR err = CHIP_NO_ERROR;
rtw_wifi_setting_t pSetting;
int ret = matter_get_sta_wifi_info(&pSetting);
if (ret < 0)
int32_t error = matter_get_sta_wifi_info(&pSetting);
CHIP_ERROR err = MapError(error, AmebaErrorType::kWiFiError);
if (err != CHIP_NO_ERROR)
{
ChipLogProgress(DeviceLayer, "STA No Wi-Fi Info");
goto exit;
Expand Down Expand Up @@ -197,3 +201,52 @@ CHIP_ERROR AmebaUtils::SetCurrentProvisionedNetwork()
exit:
return err;
}

CHIP_ERROR AmebaUtils::MapError(int32_t error, AmebaErrorType type)
{
if (type == AmebaErrorType::kDctError)
{
return MapDctError(error);
}
if (type == AmebaErrorType::kFlashError)
{
return MapFlashError(error);
}
if (type == AmebaErrorType::kWiFiError)
{
return MapWiFiError(error);
}
return CHIP_ERROR_INTERNAL;
}

CHIP_ERROR AmebaUtils::MapDctError(int32_t error)
{
if (error == DCT_SUCCESS)
return CHIP_NO_ERROR;
if (error == DCT_ERR_NO_MEMORY)
return CHIP_ERROR_NO_MEMORY;
if (error == DCT_ERR_NOT_FIND)
return CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
if (error == DCT_ERR_SIZE_OVER)
return CHIP_ERROR_INVALID_ARGUMENT;
if (error == DCT_ERR_MODULE_BUSY)
return CHIP_ERROR_BUSY;

return CHIP_ERROR_INTERNAL;
}

CHIP_ERROR AmebaUtils::MapFlashError(int32_t error)
{
if (error == 1)
return CHIP_NO_ERROR;

return CHIP_ERROR_INTERNAL;
}

CHIP_ERROR AmebaUtils::MapWiFiError(int32_t error)
{
if (error == RTW_SUCCESS)
return CHIP_NO_ERROR;

return CHIP_ERROR_INTERNAL;
}
15 changes: 15 additions & 0 deletions src/platform/Ameba/AmebaUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ namespace chip {
namespace DeviceLayer {
namespace Internal {

enum class AmebaErrorType
{
kDctError,
kFlashError,
kWiFiError,
};

class AmebaUtils
{
public:
Expand All @@ -39,6 +46,14 @@ class AmebaUtils
static CHIP_ERROR WiFiConnectProvisionedNetwork(void);
static CHIP_ERROR WiFiConnect(const char * ssid, const char * password);
static CHIP_ERROR SetCurrentProvisionedNetwork(void);
static CHIP_ERROR WiFiConnect(void);

static CHIP_ERROR MapError(int32_t error, AmebaErrorType type);

private:
static CHIP_ERROR MapDctError(int32_t error);
static CHIP_ERROR MapFlashError(int32_t error);
static CHIP_ERROR MapWiFiError(int32_t error);
};

} // namespace Internal
Expand Down
23 changes: 15 additions & 8 deletions src/platform/Ameba/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <platform/internal/CHIPDeviceLayerInternal.h>

#include <platform/Ameba/AmebaConfig.h>
#include <platform/Ameba/AmebaUtils.h>
#include <platform/ConfigurationManager.h>
#include <platform/DiagnosticDataProvider.h>
#include <platform/internal/GenericConfigurationManagerImpl.ipp>
Expand Down Expand Up @@ -151,13 +152,22 @@ CHIP_ERROR ConfigurationManagerImpl::GetLocationCapability(uint8_t & location)

CHIP_ERROR ConfigurationManagerImpl::GetPrimaryWiFiMACAddress(uint8_t * buf)
{
CHIP_ERROR err;
int32_t error;

char temp[32];
uint32_t mac[ETH_ALEN];
int i = 0;
char * token = strtok(temp, ":");
int i = 0;

wifi_get_mac_address(temp);
error = matter_wifi_get_mac_address(temp);
err = AmebaUtils::MapError(error, AmebaErrorType::kWiFiError);
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Failed to get mac address");
goto exit;
}

char * token = strtok(temp, ":");
while (token != NULL)
{
mac[i] = (uint32_t) strtol(token, NULL, 16);
Expand All @@ -168,7 +178,8 @@ CHIP_ERROR ConfigurationManagerImpl::GetPrimaryWiFiMACAddress(uint8_t * buf)
for (i = 0; i < ETH_ALEN; i++)
buf[i] = mac[i] & 0xFF;

return CHIP_NO_ERROR;
exit:
return err;
}

bool ConfigurationManagerImpl::CanFactoryReset()
Expand All @@ -187,10 +198,6 @@ CHIP_ERROR ConfigurationManagerImpl::ReadPersistedStorageValue(::chip::Platform:
AmebaConfig::Key configKey{ AmebaConfig::kConfigNamespace_ChipCounters, key };

CHIP_ERROR err = ReadConfigValue(configKey, value);
if (err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND)
{
err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
}
return err;
}

Expand Down
6 changes: 3 additions & 3 deletions src/platform/Ameba/ConnectivityManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ void ConnectivityManagerImpl::DriveStationState()
{
WiFiStationState prevState = mWiFiStationState;
ChangeWiFiStationState(kWiFiStationState_NotConnected);
if (prevState != kWiFiStationState_Connecting_Failed)
if (prevState == kWiFiStationState_Connecting_Failed)
{
ChipLogProgress(DeviceLayer, "WiFi station failed to connect");
// TODO: check retry count if exceeded, then clearwificonfig
Expand Down Expand Up @@ -858,13 +858,13 @@ void ConnectivityManagerImpl::RtkWiFiScanCompletedHandler(void)

void ConnectivityManagerImpl::DHCPProcessThread(void * param)
{
matter_lwip_dhcp(0, DHCP_START);
matter_lwip_dhcp();
PlatformMgr().LockChipStack();
sInstance.OnStationIPv4AddressAvailable();
PlatformMgr().UnlockChipStack();
#if LWIP_VERSION_MAJOR > 2 || LWIP_VERSION_MINOR > 0
#if LWIP_IPV6
matter_lwip_dhcp(0, DHCP6_START);
matter_lwip_dhcp6();
PlatformMgr().LockChipStack();
sInstance.OnIPv6AddressAvailable();
PlatformMgr().UnlockChipStack();
Expand Down
Loading