diff --git a/.github/workflows/examples-ameba.yaml b/.github/workflows/examples-ameba.yaml index e188826740668f..0e3d2e867ac431 100644 --- a/.github/workflows/examples-ameba.yaml +++ b/.github/workflows/examples-ameba.yaml @@ -38,7 +38,7 @@ jobs: if: github.actor != 'restyled-io[bot]' container: - image: connectedhomeip/chip-build-ameba:0.7.3 + image: connectedhomeip/chip-build-ameba:0.7.14 options: --user root steps: diff --git a/src/platform/Ameba/AmebaConfig.cpp b/src/platform/Ameba/AmebaConfig.cpp index 08d552f440fd76..e91905ef17fd10 100644 --- a/src/platform/Ameba/AmebaConfig.cpp +++ b/src/platform/Ameba/AmebaConfig.cpp @@ -21,6 +21,8 @@ #include #include +#include +#include #include #include #include @@ -35,6 +37,8 @@ enum kPrefsTypeBinary = 5 }; +using namespace chip::DeviceLayer::PersistedStorage; + namespace chip { namespace DeviceLayer { namespace Internal { @@ -84,114 +88,112 @@ const AmebaConfig::Key AmebaConfig::kCounterKey_BootReason = { kConfi CHIP_ERROR AmebaConfig::ReadConfigValue(Key key, bool & val) { + CHIP_ERROR err; + int32_t error; uint8_t intVal; - int32_t success = 0; - success = getPref_bool_new(key.Namespace, key.Name, &intVal); - if (success != 0) + error = getPref_bool_new(key.Namespace, key.Name, &intVal); + err = AmebaUtils::MapError(error, AmebaErrorType::kDctError); + if (err != CHIP_NO_ERROR) { ChipLogProgress(DeviceLayer, "getPref_bool_new: %s/%s failed\n", StringOrNullMarker(key.Namespace), StringOrNullMarker(key.Name)); } val = (intVal != 0); - - if (success == 0) - return CHIP_NO_ERROR; - else - return CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND; + return err; } CHIP_ERROR AmebaConfig::ReadConfigValue(Key key, uint32_t & val) { - int32_t success = 0; + CHIP_ERROR err; + int32_t error; - success = getPref_u32_new(key.Namespace, key.Name, &val); - if (success != 0) + error = getPref_u32_new(key.Namespace, key.Name, &val); + err = AmebaUtils::MapError(error, AmebaErrorType::kDctError); + if (err != CHIP_NO_ERROR) { ChipLogProgress(DeviceLayer, "getPref_u32_new: %s/%s failed\n", StringOrNullMarker(key.Namespace), StringOrNullMarker(key.Name)); } - if (success == 0) - return CHIP_NO_ERROR; - else - return CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND; + return err; } CHIP_ERROR AmebaConfig::ReadConfigValue(Key key, uint64_t & val) { - int32_t success = 0; + CHIP_ERROR err; + int32_t error; - success = getPref_u64_new(key.Namespace, key.Name, &val); - if (success != 0) + error = getPref_u64_new(key.Namespace, key.Name, &val); + err = AmebaUtils::MapError(error, AmebaErrorType::kDctError); + if (err != CHIP_NO_ERROR) { - ChipLogProgress(DeviceLayer, "getPref_u32_new: %s/%s failed\n", StringOrNullMarker(key.Namespace), + ChipLogProgress(DeviceLayer, "getPref_u64: %s/%s failed\n", StringOrNullMarker(key.Namespace), StringOrNullMarker(key.Name)); } - if (success == 0) - return CHIP_NO_ERROR; - else - return CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND; + return err; } CHIP_ERROR AmebaConfig::ReadConfigValueStr(Key key, char * buf, size_t bufSize, size_t & outLen) { - int32_t success = 0; + CHIP_ERROR err; + int32_t error; - success = getPref_str_new(key.Namespace, key.Name, buf, bufSize, &outLen); - if (success != 0) + error = getPref_str_new(key.Namespace, key.Name, buf, bufSize, &outLen); + err = AmebaUtils::MapError(error, AmebaErrorType::kDctError); + outLen -= 1; // Don't count trailing null + if (err != CHIP_NO_ERROR) { ChipLogProgress(DeviceLayer, "getPref_str_new: %s/%s failed\n", StringOrNullMarker(key.Namespace), StringOrNullMarker(key.Name)); - } - - if (success == 0) - { - outLen -= 1; // Don't count trailing null - return CHIP_NO_ERROR; - } - else - { outLen = 0; - return CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND; } + + return err; } CHIP_ERROR AmebaConfig::ReadConfigValueBin(Key key, uint8_t * buf, size_t bufSize, size_t & outLen) { - int32_t success = 0; + CHIP_ERROR err; + int32_t error; - success = getPref_bin_new(key.Namespace, key.Name, buf, bufSize, &outLen); - if (success != 0) + error = getPref_bin_new(key.Namespace, key.Name, buf, bufSize, &outLen); + err = AmebaUtils::MapError(error, AmebaErrorType::kDctError); + if (err != CHIP_NO_ERROR) { ChipLogProgress(DeviceLayer, "getPref_bin_new: %s/%s failed\n", StringOrNullMarker(key.Namespace), StringOrNullMarker(key.Name)); - } - - if (success == 0) - { - return CHIP_NO_ERROR; - } - else - { outLen = 0; - return CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND; } + + return err; } CHIP_ERROR AmebaConfig::WriteConfigValue(Key key, bool val) { - int32_t success; + CHIP_ERROR err; + int32_t error; uint8_t value; if (val == 1) value = 1; else value = 0; - success = setPref_new(key.Namespace, key.Name, &value, 1); - if (!success) + + if (checkExist(key.Name, key.Name)) + { + err = ClearConfigValue(key); + if (err != CHIP_NO_ERROR) + { + ChipLogError(DeviceLayer, "Warning, KVS leakage, failed to remove old KVS value"); + } + } + + error = setPref_new(key.Namespace, key.Name, &value, 1); + err = AmebaUtils::MapError(error, AmebaErrorType::kDctError); + if (err != CHIP_NO_ERROR) { ChipLogError(DeviceLayer, "setPref: %s/%s = %s failed\n", StringOrNullMarker(key.Namespace), StringOrNullMarker(key.Name), value ? "true" : "false"); @@ -202,15 +204,26 @@ CHIP_ERROR AmebaConfig::WriteConfigValue(Key key, bool val) val ? "true" : "false"); } - return CHIP_NO_ERROR; + return err; } CHIP_ERROR AmebaConfig::WriteConfigValue(Key key, uint32_t val) { - int32_t success; + CHIP_ERROR err; + int32_t error; - success = setPref_new(key.Namespace, key.Name, (uint8_t *) &val, sizeof(uint32_t)); - if (!success) + if (checkExist(key.Name, key.Name)) + { + err = ClearConfigValue(key); + if (err != CHIP_NO_ERROR) + { + ChipLogError(DeviceLayer, "Warning, KVS leakage, failed to remove old KVS value"); + } + } + + error = setPref_new(key.Namespace, key.Name, (uint8_t *) &val, sizeof(uint32_t)); + err = AmebaUtils::MapError(error, AmebaErrorType::kDctError); + if (err != CHIP_NO_ERROR) { ChipLogError(DeviceLayer, "setPref: %s/%s = %d(0x%x) failed\n", StringOrNullMarker(key.Namespace), StringOrNullMarker(key.Name), val, val); @@ -221,15 +234,26 @@ CHIP_ERROR AmebaConfig::WriteConfigValue(Key key, uint32_t val) StringOrNullMarker(key.Name), val, val); } - return CHIP_NO_ERROR; + return err; } CHIP_ERROR AmebaConfig::WriteConfigValue(Key key, uint64_t val) { - int32_t success; + CHIP_ERROR err; + int32_t error; + + if (checkExist(key.Name, key.Name)) + { + err = ClearConfigValue(key); + if (err != CHIP_NO_ERROR) + { + ChipLogError(DeviceLayer, "Warning, KVS leakage, failed to remove old KVS value"); + } + } - success = setPref_new(key.Namespace, key.Name, (uint8_t *) &val, sizeof(uint64_t)); - if (!success) + error = setPref_new(key.Namespace, key.Name, (uint8_t *) &val, sizeof(uint64_t)); + err = AmebaUtils::MapError(error, AmebaErrorType::kDctError); + if (err != CHIP_NO_ERROR) { ChipLogError(DeviceLayer, "setPref: %s/%s = %d(0x%x) failed\n", StringOrNullMarker(key.Namespace), StringOrNullMarker(key.Name), val, val); @@ -240,15 +264,26 @@ CHIP_ERROR AmebaConfig::WriteConfigValue(Key key, uint64_t val) StringOrNullMarker(key.Name), val, val); } - return CHIP_NO_ERROR; + return err; } CHIP_ERROR AmebaConfig::WriteConfigValueStr(Key key, const char * str) { - int32_t success; + CHIP_ERROR err; + int32_t error; + + if (checkExist(key.Name, key.Name)) + { + err = ClearConfigValue(key); + if (err != CHIP_NO_ERROR) + { + ChipLogError(DeviceLayer, "Warning, KVS leakage, failed to remove old KVS value"); + } + } - success = setPref_new(key.Namespace, key.Name, (uint8_t *) str, strlen(str) + 1); - if (!success) + error = setPref_new(key.Namespace, key.Name, (uint8_t *) str, strlen(str) + 1); + err = AmebaUtils::MapError(error, AmebaErrorType::kDctError); + if (err != CHIP_NO_ERROR) { ChipLogError(DeviceLayer, "setPref: %s/%s = %s failed\n", StringOrNullMarker(key.Namespace), StringOrNullMarker(key.Name), StringOrNullMarker(str)); @@ -258,7 +293,8 @@ CHIP_ERROR AmebaConfig::WriteConfigValueStr(Key key, const char * str) ChipLogProgress(DeviceLayer, "NVS set: %s/%s = \"%s\"", StringOrNullMarker(key.Namespace), StringOrNullMarker(key.Name), StringOrNullMarker(str)); } - return CHIP_NO_ERROR; + + return err; } CHIP_ERROR AmebaConfig::WriteConfigValueStr(Key key, const char * str, size_t strLen) @@ -279,10 +315,21 @@ CHIP_ERROR AmebaConfig::WriteConfigValueStr(Key key, const char * str, size_t st CHIP_ERROR AmebaConfig::WriteConfigValueBin(Key key, const uint8_t * data, size_t dataLen) { - int32_t success; + CHIP_ERROR err; + int32_t error; - success = setPref_new(key.Namespace, key.Name, (uint8_t *) data, dataLen); - if (!success) + if (checkExist(key.Name, key.Name)) + { + err = ClearConfigValue(key); + if (err != CHIP_NO_ERROR) + { + ChipLogError(DeviceLayer, "Warning, KVS leakage, failed to remove old KVS value"); + } + } + + error = setPref_new(key.Namespace, key.Name, (uint8_t *) data, dataLen); + err = AmebaUtils::MapError(error, AmebaErrorType::kDctError); + if (err != CHIP_NO_ERROR) { ChipLogError(DeviceLayer, "setPref: %s/%s failed\n", StringOrNullMarker(key.Namespace), StringOrNullMarker(key.Name)); } @@ -292,15 +339,17 @@ CHIP_ERROR AmebaConfig::WriteConfigValueBin(Key key, const uint8_t * data, size_ StringOrNullMarker(key.Name), dataLen); } - return CHIP_NO_ERROR; + return err; } CHIP_ERROR AmebaConfig::ClearConfigValue(Key key) { - int32_t success; + CHIP_ERROR err; + int32_t error; - success = deleteKey(key.Namespace, key.Name); - if (!success) + error = deleteKey(key.Namespace, key.Name); + err = AmebaUtils::MapError(error, AmebaErrorType::kDctError); + if (err != CHIP_NO_ERROR) { ChipLogProgress(DeviceLayer, "%s : %s/%s failed\n", __FUNCTION__, StringOrNullMarker(key.Namespace), StringOrNullMarker(key.Name)); @@ -310,7 +359,7 @@ CHIP_ERROR AmebaConfig::ClearConfigValue(Key key) ChipLogProgress(DeviceLayer, "NVS erase: %s/%s", StringOrNullMarker(key.Namespace), StringOrNullMarker(key.Name)); } - return CHIP_NO_ERROR; + return err; } bool AmebaConfig::ConfigValueExists(Key key) @@ -320,40 +369,50 @@ bool AmebaConfig::ConfigValueExists(Key key) CHIP_ERROR AmebaConfig::InitNamespace() { - int32_t success = -1; + CHIP_ERROR err; + int32_t error; - success = registerPref(); - if (success != 0) + error = registerPref(); + err = AmebaUtils::MapError(error, AmebaErrorType::kDctError); + if (err != CHIP_NO_ERROR) { ChipLogError(DeviceLayer, "DCT modules registration failed"); + goto exit; } - success = registerPref2(); - if (success != 0) + error = registerPref2(); + err = AmebaUtils::MapError(error, AmebaErrorType::kDctError); + if (err != CHIP_NO_ERROR) { ChipLogError(DeviceLayer, "DCT2 modules registration failed"); } - return CHIP_NO_ERROR; +exit: + return err; } CHIP_ERROR AmebaConfig::ClearNamespace() { - int32_t success = -1; + CHIP_ERROR err; + int32_t error; - success = clearPref(); - if (success != 0) + error = clearPref(); + err = AmebaUtils::MapError(error, AmebaErrorType::kDctError); + if (err != CHIP_NO_ERROR) { ChipLogError(DeviceLayer, "DCT modules unregistration failed\n"); + goto exit; } - success = clearPref2(); - if (success != 0) + error = clearPref2(); + err = AmebaUtils::MapError(error, AmebaErrorType::kDctError); + if (err != CHIP_NO_ERROR) { ChipLogError(DeviceLayer, "DCT2 modules unregistration failed\n"); } - return CHIP_NO_ERROR; +exit: + return err; } void AmebaConfig::RunConfigUnitTest() {} diff --git a/src/platform/Ameba/AmebaUtils.cpp b/src/platform/Ameba/AmebaUtils.cpp index 578ffab2cd5816..12118dfb14e9f3 100644 --- a/src/platform/Ameba/AmebaUtils.cpp +++ b/src/platform/Ameba/AmebaUtils.cpp @@ -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) @@ -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) { @@ -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; @@ -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); @@ -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; @@ -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; +} diff --git a/src/platform/Ameba/AmebaUtils.h b/src/platform/Ameba/AmebaUtils.h index 5fcc5c9c2ef487..c1fa81b163580e 100644 --- a/src/platform/Ameba/AmebaUtils.h +++ b/src/platform/Ameba/AmebaUtils.h @@ -24,6 +24,13 @@ namespace chip { namespace DeviceLayer { namespace Internal { +enum class AmebaErrorType +{ + kDctError, + kFlashError, + kWiFiError, +}; + class AmebaUtils { public: @@ -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 diff --git a/src/platform/Ameba/ConfigurationManagerImpl.cpp b/src/platform/Ameba/ConfigurationManagerImpl.cpp index 16d7a8f45e75f7..b3c6cbfbb66db6 100644 --- a/src/platform/Ameba/ConfigurationManagerImpl.cpp +++ b/src/platform/Ameba/ConfigurationManagerImpl.cpp @@ -25,6 +25,7 @@ #include #include +#include #include #include #include @@ -152,13 +153,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); @@ -169,7 +179,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() @@ -188,10 +199,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; } diff --git a/src/platform/Ameba/ConnectivityManagerImpl.cpp b/src/platform/Ameba/ConnectivityManagerImpl.cpp index ba289b115c0bab..d42886b0f75ac3 100644 --- a/src/platform/Ameba/ConnectivityManagerImpl.cpp +++ b/src/platform/Ameba/ConnectivityManagerImpl.cpp @@ -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 @@ -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(); diff --git a/src/platform/Ameba/DiagnosticDataProviderImpl.cpp b/src/platform/Ameba/DiagnosticDataProviderImpl.cpp index b9f2f018e6987d..ab1f2438639675 100644 --- a/src/platform/Ameba/DiagnosticDataProviderImpl.cpp +++ b/src/platform/Ameba/DiagnosticDataProviderImpl.cpp @@ -25,10 +25,13 @@ #include #include +#include #include #include +using namespace chip::DeviceLayer::Internal; + namespace chip { namespace DeviceLayer { @@ -248,19 +251,25 @@ void DiagnosticDataProviderImpl::ReleaseNetworkInterfaces(NetworkInterface * net #if CHIP_DEVICE_CONFIG_ENABLE_WIFI CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiBssId(MutableByteSpan & BssId) { + CHIP_ERROR err; + int32_t error; + constexpr size_t bssIdSize = 6; VerifyOrReturnError(BssId.size() >= bssIdSize, CHIP_ERROR_BUFFER_TOO_SMALL); - if (wifi_get_ap_bssid(BssId.data()) != 0) + error = matter_wifi_get_ap_bssid(BssId.data()); + err = AmebaUtils::MapError(error, AmebaErrorType::kWiFiError); + + if (err != CHIP_NO_ERROR) { - return CHIP_ERROR_READ_FAILED; + return err; } BssId.reduce_size(bssIdSize); ChipLogProgress(DeviceLayer, "%02x,%02x,%02x,%02x,%02x,%02x\n", BssId.data()[0], BssId.data()[1], BssId.data()[2], BssId.data()[3], BssId.data()[4], BssId.data()[5]); - return CHIP_NO_ERROR; + return err; } CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiVersion(app::Clusters::WiFiNetworkDiagnostics::WiFiVersionEnum & wifiVersion) @@ -273,20 +282,25 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiVersion(app::Clusters::WiFiNetwork CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiSecurityType(app::Clusters::WiFiNetworkDiagnostics::SecurityTypeEnum & securityType) { + CHIP_ERROR err; + int32_t error; + using app::Clusters::WiFiNetworkDiagnostics::SecurityTypeEnum; unsigned int _auth_type; - unsigned short _security = 0; + unsigned short security = 0; rtw_wifi_setting_t setting; -#ifdef CONFIG_PLATFORM_8721D - if (wext_get_enc_ext("wlan0", &_security, &setting.key_idx, setting.password) < 0) + error = matter_wifi_get_security_type("wlan0", &security, &setting.key_idx, setting.password); + err = AmebaUtils::MapError(error, AmebaErrorType::kWiFiError); + if (err != CHIP_NO_ERROR) { securityType = SecurityTypeEnum::kUnspecified; } +#ifdef CONFIG_PLATFORM_8721D else { - switch (_security) + switch (security) { case IW_ENCODE_ALG_NONE: securityType = SecurityTypeEnum::kNone; @@ -306,14 +320,9 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiSecurityType(app::Clusters::WiFiNe } } #else - wext_get_enc_ext("wlan0", &_security, &setting.key_idx, setting.password); - if (wext_get_auth_type("wlan0", &_auth_type) < 0) - { - securityType = SecurityTypeEnum::kUnspecified; - } else { - switch (_security) + switch (security) { case IW_ENCODE_ALG_NONE: securityType = SecurityTypeEnum::kNone; @@ -342,30 +351,40 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiSecurityType(app::Clusters::WiFiNe } #endif - return CHIP_NO_ERROR; + return err; } CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiChannelNumber(uint16_t & channelNumber) { + CHIP_ERROR err; + int32_t error; unsigned char channel; - if (wext_get_channel("wlan0", &channel) < 0) + error = matter_wifi_get_wifi_channel_number("wlan0", &channel); + err = AmebaUtils::MapError(error, AmebaErrorType::kWiFiError); + if (err != CHIP_NO_ERROR) channelNumber = 0; else channelNumber = (uint16_t) channel; - return CHIP_NO_ERROR; + return err; } CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiRssi(int8_t & rssi) { - int _rssi = 0; - if (wifi_get_rssi(&_rssi) < 0) + CHIP_ERROR err; + int32_t error; + + error = matter_wifi_get_rssi((int *) &rssi); + err = AmebaUtils::MapError(error, AmebaErrorType::kWiFiError); + + if (err != CHIP_NO_ERROR) + { + // set rssi to 0 upon error rssi = 0; - else - rssi = _rssi; + } - return CHIP_NO_ERROR; + return err; } CHIP_ERROR DiagnosticDataProviderImpl::GetWiFiBeaconLostCount(uint32_t & beaconLostCount) diff --git a/src/platform/Ameba/FactoryDataDecoder.cpp b/src/platform/Ameba/FactoryDataDecoder.cpp index 3c6874b5207863..4e3536e3de1455 100644 --- a/src/platform/Ameba/FactoryDataDecoder.cpp +++ b/src/platform/Ameba/FactoryDataDecoder.cpp @@ -17,29 +17,28 @@ #include "FactoryDataDecoder.h" #include "chip_porting.h" +#include #include +using namespace chip::DeviceLayer::Internal; + namespace chip { namespace DeviceLayer { CHIP_ERROR FactoryDataDecoder::ReadFactoryData(uint8_t * buffer, uint16_t * pfactorydata_len) { - uint32_t ret = 0; - ret = ReadFactory(buffer, pfactorydata_len); - if (ret != 1) - return CHIP_ERROR_INTERNAL; + int32_t error = ReadFactory(buffer, pfactorydata_len); + CHIP_ERROR err = AmebaUtils::MapError(error, AmebaErrorType::kFlashError); - return CHIP_NO_ERROR; + return err; } CHIP_ERROR FactoryDataDecoder::DecodeFactoryData(uint8_t * buffer, FactoryData * fdata, uint16_t factorydata_len) { - uint32_t ret = 0; - ret = DecodeFactory(buffer, fdata, factorydata_len); - if (ret != 0) - return CHIP_ERROR_INTERNAL; + int32_t error = DecodeFactory(buffer, fdata, factorydata_len); + CHIP_ERROR err = AmebaUtils::MapError(error, AmebaErrorType::kFlashError); - return CHIP_NO_ERROR; + return err; } } // namespace DeviceLayer diff --git a/src/platform/Ameba/KeyValueStoreManagerImpl.cpp b/src/platform/Ameba/KeyValueStoreManagerImpl.cpp index 1fede31febb7c1..282d58c47c1f65 100644 --- a/src/platform/Ameba/KeyValueStoreManagerImpl.cpp +++ b/src/platform/Ameba/KeyValueStoreManagerImpl.cpp @@ -23,9 +23,12 @@ /* this file behaves like a config.h, comes first */ #include "FreeRTOS.h" #include "chip_porting.h" +#include #include #include +using namespace chip::DeviceLayer::Internal; + namespace chip { namespace DeviceLayer { namespace PersistedStorage { @@ -35,99 +38,79 @@ KeyValueStoreManagerImpl KeyValueStoreManagerImpl::sInstance; CHIP_ERROR KeyValueStoreManagerImpl::_Get(const char * key, void * value, size_t value_size, size_t * read_bytes_size, size_t offset_bytes) { - CHIP_ERROR err = CHIP_NO_ERROR; - int32_t ret = -1; + CHIP_ERROR err; + int32_t error; if (!value) { - return (err = CHIP_ERROR_INVALID_ARGUMENT); + err = CHIP_ERROR_INVALID_ARGUMENT; + goto exit; } if (offset_bytes > 0) { // Offset and partial reads are not supported in nvs, for now just return NOT_IMPLEMENTED. Support can be added in the // future if this is needed. - return (err = CHIP_ERROR_NOT_IMPLEMENTED); + err = CHIP_ERROR_NOT_IMPLEMENTED; + goto exit; } if (read_bytes_size) { - ret = getPref_bin_new(key, key, (uint8_t *) value, value_size, read_bytes_size); + error = getPref_bin_new(key, key, (uint8_t *) value, value_size, read_bytes_size); + err = AmebaUtils::MapError(error, AmebaErrorType::kDctError); } else { size_t * dummy_read_bytes_size = (size_t *) pvPortMalloc(sizeof(size_t)); if (!dummy_read_bytes_size) { - return CHIP_ERROR_INTERNAL; + err = CHIP_ERROR_INTERNAL; + goto exit; } - ret = getPref_bin_new(key, key, (uint8_t *) value, value_size, dummy_read_bytes_size); + error = getPref_bin_new(key, key, (uint8_t *) value, value_size, dummy_read_bytes_size); + err = AmebaUtils::MapError(error, AmebaErrorType::kDctError); vPortFree(dummy_read_bytes_size); } - switch (ret) - { - case 0: - return CHIP_NO_ERROR; - case -6: - return CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND; - case -7: - return CHIP_ERROR_INVALID_ARGUMENT; - case -8: - return CHIP_ERROR_BUFFER_TOO_SMALL; - default: - break; - } - return CHIP_ERROR_INTERNAL; +exit: + return err; } CHIP_ERROR KeyValueStoreManagerImpl::_Put(const char * key, const void * value, size_t value_size) { - CHIP_ERROR err = CHIP_NO_ERROR; - int32_t ret = -1; + CHIP_ERROR err; + int32_t error; if (!value) { - return (err = CHIP_ERROR_INVALID_ARGUMENT); + err = CHIP_ERROR_INVALID_ARGUMENT; + goto exit; } if (checkExist(key, key)) { - ret = deleteKey(key, key); - if (ret != 0) + error = deleteKey(key, key); + err = AmebaUtils::MapError(error, AmebaErrorType::kDctError); + if (err != CHIP_NO_ERROR) { ChipLogError(DeviceLayer, "Warning, KVS leakage, failed to remove old kvs value"); } } - ret = setPref_new(key, key, (uint8_t *) value, value_size); - - if (TRUE == ret) - err = CHIP_NO_ERROR; - else - err = CHIP_ERROR_INTERNAL; + error = setPref_new(key, key, (uint8_t *) value, value_size); + err = AmebaUtils::MapError(error, AmebaErrorType::kDctError); +exit: return err; } CHIP_ERROR KeyValueStoreManagerImpl::_Delete(const char * key) { - int32_t ret = deleteKey(key, key); - switch (ret) - { - case 0: - return CHIP_NO_ERROR; - case -6: - return CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND; - case -7: - return CHIP_ERROR_INVALID_ARGUMENT; - case -8: - return CHIP_ERROR_BUFFER_TOO_SMALL; - default: - break; - } + int32_t error = deleteKey(key, key); + CHIP_ERROR err = AmebaUtils::MapError(error, AmebaErrorType::kDctError); - return CHIP_ERROR_INTERNAL; + return err; } } // namespace PersistedStorage diff --git a/src/platform/Ameba/NetworkCommissioningWiFiDriver.cpp b/src/platform/Ameba/NetworkCommissioningWiFiDriver.cpp index 38be3f2080f0b5..914e4f337f6688 100644 --- a/src/platform/Ameba/NetworkCommissioningWiFiDriver.cpp +++ b/src/platform/Ameba/NetworkCommissioningWiFiDriver.cpp @@ -127,10 +127,13 @@ Status AmebaWiFiDriver::ReorderNetwork(ByteSpan networkId, uint8_t index, Mutabl CHIP_ERROR AmebaWiFiDriver::ConnectWiFiNetwork(const char * ssid, uint8_t ssidLen, const char * key, uint8_t keyLen) { CHIP_ERROR err = CHIP_NO_ERROR; + bool connected; + // If device is already connected to WiFi, then disconnect the WiFi, - // clear the WiFi configurations and add the newly provided WiFi configurations. - if (chip::DeviceLayer::Internal::AmebaUtils::IsStationProvisioned()) + chip::DeviceLayer::Internal::AmebaUtils::IsStationConnected(connected); + if (connected) { + ConnectivityMgrImpl().ChangeWiFiStationState(ConnectivityManager::kWiFiStationState_Disconnecting); ChipLogProgress(DeviceLayer, "Disconnecting WiFi station interface"); err = chip::DeviceLayer::Internal::AmebaUtils::WiFiDisconnect(); if (err != CHIP_NO_ERROR) @@ -138,6 +141,11 @@ CHIP_ERROR AmebaWiFiDriver::ConnectWiFiNetwork(const char * ssid, uint8_t ssidLe ChipLogError(DeviceLayer, "WiFiDisconnect() failed"); return err; } + } + + // clear the WiFi configurations and add the newly provided WiFi configurations. + if (chip::DeviceLayer::Internal::AmebaUtils::IsStationProvisioned()) + { err = chip::DeviceLayer::Internal::AmebaUtils::ClearWiFiConfig(); if (err != CHIP_NO_ERROR) { @@ -146,9 +154,12 @@ CHIP_ERROR AmebaWiFiDriver::ConnectWiFiNetwork(const char * ssid, uint8_t ssidLe } } - ConnectivityMgrImpl().ChangeWiFiStationState(ConnectivityManager::kWiFiStationState_Connecting); + DeviceLayer::ConnectivityManager::WiFiStationState state = DeviceLayer::ConnectivityManager::kWiFiStationState_Connecting; + DeviceLayer::SystemLayer().ScheduleLambda([state, ssid, key]() { + ConnectivityMgrImpl().ChangeWiFiStationState(state); + chip::DeviceLayer::Internal::AmebaUtils::WiFiConnect(ssid, key); + }); - err = chip::DeviceLayer::Internal::AmebaUtils::WiFiConnect(ssid, key); return err; } @@ -304,7 +315,7 @@ void AmebaWiFiDriver::ScanNetworks(ByteSpan ssid, WiFiDriver::ScanCallback * cal CHIP_ERROR AmebaWiFiDriver::SetLastDisconnectReason(const ChipDeviceEvent * event) { VerifyOrReturnError(event->Type == DeviceEventType::kRtkWiFiStationDisconnectedEvent, CHIP_ERROR_INVALID_ARGUMENT); - mLastDisconnectedReason = wifi_get_last_error(); // TODO: change this to wrapper + mLastDisconnectedReason = matter_wifi_get_last_error(); return CHIP_NO_ERROR; } diff --git a/src/platform/Ameba/PlatformManagerImpl.cpp b/src/platform/Ameba/PlatformManagerImpl.cpp index 0b89c4a8c24971..33ad2a657f3e10 100644 --- a/src/platform/Ameba/PlatformManagerImpl.cpp +++ b/src/platform/Ameba/PlatformManagerImpl.cpp @@ -39,7 +39,7 @@ CHIP_ERROR InitLwIPCoreLock(void); PlatformManagerImpl PlatformManagerImpl::sInstance; extern "C" { -extern int rtw_get_random_bytes(void * dst, size_t size); +extern int rtw_get_random_bytes(void * dst, uint32_t size); } static int app_entropy_source(void * data, unsigned char * output, size_t len, size_t * olen) { diff --git a/src/platform/Ameba/SystemTimeSupport.cpp b/src/platform/Ameba/SystemTimeSupport.cpp index 57e5d863b059f9..5b7d03b212d672 100644 --- a/src/platform/Ameba/SystemTimeSupport.cpp +++ b/src/platform/Ameba/SystemTimeSupport.cpp @@ -28,14 +28,10 @@ #include #include -#include "rtc_api.h" #include "task.h" +#include #include -extern void rtc_init(void); -extern time_t rtc_read(void); -extern void rtc_write(time_t t); - struct rtkTimeVal { uint32_t tv_sec; /* seconds */ @@ -69,7 +65,7 @@ CHIP_ERROR ClockImpl::GetClock_RealTime(Clock::Microseconds64 & curTime) time_t seconds; struct rtkTimeVal tv; - seconds = rtc_read(); + seconds = matter_rtc_read(); tv.tv_sec = (uint32_t) seconds; tv.tv_usec = 0; @@ -98,8 +94,8 @@ CHIP_ERROR ClockImpl::SetClock_RealTime(Microseconds64 aNewCurTime) struct rtkTimeVal tv; tv.tv_sec = static_cast(aNewCurTime.count() / UINT64_C(1000000)); tv.tv_usec = static_cast(aNewCurTime.count() % UINT64_C(1000000)); - rtc_init(); - rtc_write(tv.tv_sec); + matter_rtc_init(); + matter_rtc_write(tv.tv_sec); return CHIP_NO_ERROR; }