Skip to content

Commit e282694

Browse files
committed
implement error mapping from ameba to matter
1 parent 2377844 commit e282694

11 files changed

+352
-210
lines changed

src/platform/Ameba/AmebaConfig.cpp

+143-84
Large diffs are not rendered by default.

src/platform/Ameba/AmebaUtils.cpp

+76-23
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ constexpr char kWiFiCredentialsKeyName[] = "wifi-pass";
4040

4141
CHIP_ERROR AmebaUtils::StartWiFi(void)
4242
{
43-
CHIP_ERROR err = CHIP_NO_ERROR;
4443
// Ensure that the WiFi layer is started.
45-
matter_wifi_on(RTW_MODE_STA);
46-
return err;
44+
int32_t error = matter_wifi_on(RTW_MODE_STA);
45+
CHIP_ERROR err = MapError(error, AmebaErrorType::kWiFiError);
46+
return CHIP_NO_ERROR; // will fail if wifi is already initialized, let it pass
4747
}
4848

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

6161
CHIP_ERROR AmebaUtils::IsStationConnected(bool & connected)
6262
{
63-
CHIP_ERROR err = CHIP_NO_ERROR;
64-
connected = (matter_wifi_is_connected_to_ap() == RTW_SUCCESS) ? 1 : 0;
63+
int32_t error = matter_wifi_is_connected_to_ap();
64+
CHIP_ERROR err = MapError(error, AmebaErrorType::kWiFiError);
65+
connected = (err == CHIP_NO_ERROR) ? true : false;
6566
return err;
6667
}
6768

6869
CHIP_ERROR AmebaUtils::EnableStationMode(void)
6970
{
70-
CHIP_ERROR err = CHIP_NO_ERROR;
7171
// Ensure that station mode is enabled in the WiFi layer.
72-
matter_wifi_set_mode(RTW_MODE_STA);
72+
int32_t error = matter_wifi_set_mode(RTW_MODE_STA);
73+
CHIP_ERROR err = MapError(error, AmebaErrorType::kWiFiError);
7374
return err;
7475
}
7576

7677
CHIP_ERROR AmebaUtils::SetWiFiConfig(rtw_wifi_config_t * config)
7778
{
78-
CHIP_ERROR err = CHIP_NO_ERROR;
79+
CHIP_ERROR err;
7980
// don't store if ssid is null
8081
if (config->ssid[0] == 0)
8182
{
@@ -95,7 +96,7 @@ CHIP_ERROR AmebaUtils::SetWiFiConfig(rtw_wifi_config_t * config)
9596

9697
CHIP_ERROR AmebaUtils::GetWiFiConfig(rtw_wifi_config_t * config)
9798
{
98-
CHIP_ERROR err = CHIP_NO_ERROR;
99+
CHIP_ERROR err;
99100
size_t ssidLen = 0;
100101
size_t credentialsLen = 0;
101102

@@ -117,8 +118,8 @@ CHIP_ERROR AmebaUtils::GetWiFiConfig(rtw_wifi_config_t * config)
117118
CHIP_ERROR AmebaUtils::ClearWiFiConfig()
118119
{
119120
/* Clear Wi-Fi Configurations in Storage */
120-
CHIP_ERROR err = CHIP_NO_ERROR;
121-
err = PersistedStorage::KeyValueStoreMgr().Delete(kWiFiSSIDKeyName);
121+
CHIP_ERROR err;
122+
err = PersistedStorage::KeyValueStoreMgr().Delete(kWiFiSSIDKeyName);
122123
SuccessOrExit(err);
123124

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

131132
CHIP_ERROR AmebaUtils::WiFiDisconnect(void)
132133
{
133-
CHIP_ERROR err = CHIP_NO_ERROR;
134-
ChipLogProgress(DeviceLayer, "matter_wifi_disconnect");
135-
err = (matter_wifi_disconnect() == RTW_SUCCESS) ? CHIP_NO_ERROR : CHIP_ERROR_INTERNAL;
134+
ChipLogProgress(DeviceLayer, "Disconnecting WiFi");
135+
int32_t error = matter_wifi_disconnect();
136+
CHIP_ERROR err = MapError(error, AmebaErrorType::kWiFiError);
137+
if (err == CHIP_NO_ERROR)
138+
{
139+
ChipLogProgress(DeviceLayer, "matter_lwip_releaseip");
140+
matter_lwip_releaseip();
141+
}
136142
return err;
137143
}
138144

139145
CHIP_ERROR AmebaUtils::WiFiConnectProvisionedNetwork(void)
140146
{
141-
CHIP_ERROR err = CHIP_NO_ERROR;
142147
rtw_wifi_config_t * config = (rtw_wifi_config_t *) pvPortMalloc(sizeof(rtw_wifi_config_t));
143148
memset(config, 0, sizeof(rtw_wifi_config_t));
144149
GetWiFiConfig(config);
145150
ChipLogProgress(DeviceLayer, "Connecting to AP : [%s]", (char *) config->ssid);
146-
int ameba_err = matter_wifi_connect((char *) config->ssid, RTW_SECURITY_WPA_WPA2_MIXED, (char *) config->password,
151+
int32_t error = matter_wifi_connect((char *) config->ssid, RTW_SECURITY_WPA_WPA2_MIXED, (char *) config->password,
147152
strlen((const char *) config->ssid), strlen((const char *) config->password), 0, nullptr);
153+
CHIP_ERROR err = MapError(error, AmebaErrorType::kWiFiError);
148154

149155
vPortFree(config);
150-
err = (ameba_err == RTW_SUCCESS) ? CHIP_NO_ERROR : CHIP_ERROR_INTERNAL;
151156
return err;
152157
}
153158

154159
CHIP_ERROR AmebaUtils::WiFiConnect(const char * ssid, const char * password)
155160
{
156-
CHIP_ERROR err = CHIP_NO_ERROR;
157161
ChipLogProgress(DeviceLayer, "Connecting to AP : [%s]", (char *) ssid);
158-
int ameba_err = matter_wifi_connect((char *) ssid, RTW_SECURITY_WPA_WPA2_MIXED, (char *) password, strlen(ssid),
162+
int32_t error = matter_wifi_connect((char *) ssid, RTW_SECURITY_WPA_WPA2_MIXED, (char *) password, strlen(ssid),
159163
strlen(password), 0, nullptr);
160-
err = (ameba_err == RTW_SUCCESS) ? CHIP_NO_ERROR : CHIP_ERROR_INTERNAL;
164+
CHIP_ERROR err = MapError(error, AmebaErrorType::kWiFiError);
161165
return err;
162166
}
163167

164168
CHIP_ERROR AmebaUtils::SetCurrentProvisionedNetwork()
165169
{
166-
CHIP_ERROR err = CHIP_NO_ERROR;
167170
rtw_wifi_setting_t pSetting;
168-
int ret = matter_get_sta_wifi_info(&pSetting);
169-
if (ret < 0)
171+
int32_t error = matter_get_sta_wifi_info(&pSetting);
172+
CHIP_ERROR err = MapError(error, AmebaErrorType::kWiFiError);
173+
if (err != CHIP_NO_ERROR)
170174
{
171175
ChipLogProgress(DeviceLayer, "STA No Wi-Fi Info");
172176
goto exit;
@@ -197,3 +201,52 @@ CHIP_ERROR AmebaUtils::SetCurrentProvisionedNetwork()
197201
exit:
198202
return err;
199203
}
204+
205+
CHIP_ERROR AmebaUtils::MapError(int32_t error, AmebaErrorType type)
206+
{
207+
if (type == AmebaErrorType::kDctError)
208+
{
209+
return MapDctError(error);
210+
}
211+
if (type == AmebaErrorType::kFlashError)
212+
{
213+
return MapFlashError(error);
214+
}
215+
if (type == AmebaErrorType::kWiFiError)
216+
{
217+
return MapWiFiError(error);
218+
}
219+
return CHIP_ERROR_INTERNAL;
220+
}
221+
222+
CHIP_ERROR AmebaUtils::MapDctError(int32_t error)
223+
{
224+
if (error == DCT_SUCCESS)
225+
return CHIP_NO_ERROR;
226+
if (error == DCT_ERR_NO_MEMORY)
227+
return CHIP_ERROR_NO_MEMORY;
228+
if (error == DCT_ERR_NOT_FIND)
229+
return CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
230+
if (error == DCT_ERR_SIZE_OVER)
231+
return CHIP_ERROR_INVALID_ARGUMENT;
232+
if (error == DCT_ERR_MODULE_BUSY)
233+
return CHIP_ERROR_BUSY;
234+
235+
return CHIP_ERROR_INTERNAL;
236+
}
237+
238+
CHIP_ERROR AmebaUtils::MapFlashError(int32_t error)
239+
{
240+
if (error == 1)
241+
return CHIP_NO_ERROR;
242+
243+
return CHIP_ERROR_INTERNAL;
244+
}
245+
246+
CHIP_ERROR AmebaUtils::MapWiFiError(int32_t error)
247+
{
248+
if (error == RTW_SUCCESS)
249+
return CHIP_NO_ERROR;
250+
251+
return CHIP_ERROR_INTERNAL;
252+
}

src/platform/Ameba/AmebaUtils.h

+15
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ namespace chip {
2424
namespace DeviceLayer {
2525
namespace Internal {
2626

27+
enum class AmebaErrorType
28+
{
29+
kDctError,
30+
kFlashError,
31+
kWiFiError,
32+
};
33+
2734
class AmebaUtils
2835
{
2936
public:
@@ -39,6 +46,14 @@ class AmebaUtils
3946
static CHIP_ERROR WiFiConnectProvisionedNetwork(void);
4047
static CHIP_ERROR WiFiConnect(const char * ssid, const char * password);
4148
static CHIP_ERROR SetCurrentProvisionedNetwork(void);
49+
static CHIP_ERROR WiFiConnect(void);
50+
51+
static CHIP_ERROR MapError(int32_t error, AmebaErrorType type);
52+
53+
private:
54+
static CHIP_ERROR MapDctError(int32_t error);
55+
static CHIP_ERROR MapFlashError(int32_t error);
56+
static CHIP_ERROR MapWiFiError(int32_t error);
4257
};
4358

4459
} // namespace Internal

src/platform/Ameba/ConfigurationManagerImpl.cpp

+15-8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <platform/internal/CHIPDeviceLayerInternal.h>
2626

2727
#include <platform/Ameba/AmebaConfig.h>
28+
#include <platform/Ameba/AmebaUtils.h>
2829
#include <platform/ConfigurationManager.h>
2930
#include <platform/DiagnosticDataProvider.h>
3031
#include <platform/internal/GenericConfigurationManagerImpl.ipp>
@@ -152,13 +153,22 @@ CHIP_ERROR ConfigurationManagerImpl::GetLocationCapability(uint8_t & location)
152153

153154
CHIP_ERROR ConfigurationManagerImpl::GetPrimaryWiFiMACAddress(uint8_t * buf)
154155
{
156+
CHIP_ERROR err;
157+
int32_t error;
158+
155159
char temp[32];
156160
uint32_t mac[ETH_ALEN];
157-
int i = 0;
161+
char * token = strtok(temp, ":");
162+
int i = 0;
158163

159-
wifi_get_mac_address(temp);
164+
error = matter_wifi_get_mac_address(temp);
165+
err = AmebaUtils::MapError(error, AmebaErrorType::kWiFiError);
166+
if (err != CHIP_NO_ERROR)
167+
{
168+
ChipLogError(DeviceLayer, "Failed to get mac address");
169+
goto exit;
170+
}
160171

161-
char * token = strtok(temp, ":");
162172
while (token != NULL)
163173
{
164174
mac[i] = (uint32_t) strtol(token, NULL, 16);
@@ -169,7 +179,8 @@ CHIP_ERROR ConfigurationManagerImpl::GetPrimaryWiFiMACAddress(uint8_t * buf)
169179
for (i = 0; i < ETH_ALEN; i++)
170180
buf[i] = mac[i] & 0xFF;
171181

172-
return CHIP_NO_ERROR;
182+
exit:
183+
return err;
173184
}
174185

175186
bool ConfigurationManagerImpl::CanFactoryReset()
@@ -188,10 +199,6 @@ CHIP_ERROR ConfigurationManagerImpl::ReadPersistedStorageValue(::chip::Platform:
188199
AmebaConfig::Key configKey{ AmebaConfig::kConfigNamespace_ChipCounters, key };
189200

190201
CHIP_ERROR err = ReadConfigValue(configKey, value);
191-
if (err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND)
192-
{
193-
err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
194-
}
195202
return err;
196203
}
197204

src/platform/Ameba/ConnectivityManagerImpl.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ void ConnectivityManagerImpl::DriveStationState()
524524
{
525525
WiFiStationState prevState = mWiFiStationState;
526526
ChangeWiFiStationState(kWiFiStationState_NotConnected);
527-
if (prevState != kWiFiStationState_Connecting_Failed)
527+
if (prevState == kWiFiStationState_Connecting_Failed)
528528
{
529529
ChipLogProgress(DeviceLayer, "WiFi station failed to connect");
530530
// TODO: check retry count if exceeded, then clearwificonfig
@@ -858,13 +858,13 @@ void ConnectivityManagerImpl::RtkWiFiScanCompletedHandler(void)
858858

859859
void ConnectivityManagerImpl::DHCPProcessThread(void * param)
860860
{
861-
matter_lwip_dhcp(0, DHCP_START);
861+
matter_lwip_dhcp();
862862
PlatformMgr().LockChipStack();
863863
sInstance.OnStationIPv4AddressAvailable();
864864
PlatformMgr().UnlockChipStack();
865865
#if LWIP_VERSION_MAJOR > 2 || LWIP_VERSION_MINOR > 0
866866
#if LWIP_IPV6
867-
matter_lwip_dhcp(0, DHCP6_START);
867+
matter_lwip_dhcp6();
868868
PlatformMgr().LockChipStack();
869869
sInstance.OnIPv6AddressAvailable();
870870
PlatformMgr().UnlockChipStack();

0 commit comments

Comments
 (0)