@@ -40,10 +40,10 @@ constexpr char kWiFiCredentialsKeyName[] = "wifi-pass";
40
40
41
41
CHIP_ERROR AmebaUtils::StartWiFi (void )
42
42
{
43
- CHIP_ERROR err = CHIP_NO_ERROR;
44
43
// 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
47
47
}
48
48
49
49
CHIP_ERROR AmebaUtils::IsStationEnabled (bool & staEnabled)
@@ -60,22 +60,23 @@ bool AmebaUtils::IsStationProvisioned(void)
60
60
61
61
CHIP_ERROR AmebaUtils::IsStationConnected (bool & connected)
62
62
{
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 ;
65
66
return err;
66
67
}
67
68
68
69
CHIP_ERROR AmebaUtils::EnableStationMode (void )
69
70
{
70
- CHIP_ERROR err = CHIP_NO_ERROR;
71
71
// 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 );
73
74
return err;
74
75
}
75
76
76
77
CHIP_ERROR AmebaUtils::SetWiFiConfig (rtw_wifi_config_t * config)
77
78
{
78
- CHIP_ERROR err = CHIP_NO_ERROR ;
79
+ CHIP_ERROR err;
79
80
// don't store if ssid is null
80
81
if (config->ssid [0 ] == 0 )
81
82
{
@@ -95,7 +96,7 @@ CHIP_ERROR AmebaUtils::SetWiFiConfig(rtw_wifi_config_t * config)
95
96
96
97
CHIP_ERROR AmebaUtils::GetWiFiConfig (rtw_wifi_config_t * config)
97
98
{
98
- CHIP_ERROR err = CHIP_NO_ERROR ;
99
+ CHIP_ERROR err;
99
100
size_t ssidLen = 0 ;
100
101
size_t credentialsLen = 0 ;
101
102
@@ -117,8 +118,8 @@ CHIP_ERROR AmebaUtils::GetWiFiConfig(rtw_wifi_config_t * config)
117
118
CHIP_ERROR AmebaUtils::ClearWiFiConfig ()
118
119
{
119
120
/* 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 );
122
123
SuccessOrExit (err);
123
124
124
125
err = PersistedStorage::KeyValueStoreMgr ().Delete (kWiFiCredentialsKeyName );
@@ -130,43 +131,46 @@ CHIP_ERROR AmebaUtils::ClearWiFiConfig()
130
131
131
132
CHIP_ERROR AmebaUtils::WiFiDisconnect (void )
132
133
{
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
+ }
136
142
return err;
137
143
}
138
144
139
145
CHIP_ERROR AmebaUtils::WiFiConnectProvisionedNetwork (void )
140
146
{
141
- CHIP_ERROR err = CHIP_NO_ERROR;
142
147
rtw_wifi_config_t * config = (rtw_wifi_config_t *) pvPortMalloc (sizeof (rtw_wifi_config_t ));
143
148
memset (config, 0 , sizeof (rtw_wifi_config_t ));
144
149
GetWiFiConfig (config);
145
150
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 ,
147
152
strlen ((const char *) config->ssid ), strlen ((const char *) config->password ), 0 , nullptr );
153
+ CHIP_ERROR err = MapError (error, AmebaErrorType::kWiFiError );
148
154
149
155
vPortFree (config);
150
- err = (ameba_err == RTW_SUCCESS) ? CHIP_NO_ERROR : CHIP_ERROR_INTERNAL;
151
156
return err;
152
157
}
153
158
154
159
CHIP_ERROR AmebaUtils::WiFiConnect (const char * ssid, const char * password)
155
160
{
156
- CHIP_ERROR err = CHIP_NO_ERROR;
157
161
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),
159
163
strlen (password), 0 , nullptr );
160
- err = (ameba_err == RTW_SUCCESS) ? CHIP_NO_ERROR : CHIP_ERROR_INTERNAL ;
164
+ CHIP_ERROR err = MapError (error, AmebaErrorType:: kWiFiError ) ;
161
165
return err;
162
166
}
163
167
164
168
CHIP_ERROR AmebaUtils::SetCurrentProvisionedNetwork ()
165
169
{
166
- CHIP_ERROR err = CHIP_NO_ERROR;
167
170
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)
170
174
{
171
175
ChipLogProgress (DeviceLayer, " STA No Wi-Fi Info" );
172
176
goto exit ;
@@ -197,3 +201,52 @@ CHIP_ERROR AmebaUtils::SetCurrentProvisionedNetwork()
197
201
exit :
198
202
return err;
199
203
}
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
+ }
0 commit comments