Skip to content

Commit 4ec2abb

Browse files
committed
Merge branch 'feature/some_refactor_for_tcpip_adapter' into 'master'
tcpip_adapter: not remove netif when tcpip adapter is stopped See merge request !943
2 parents 5ac0503 + d724cc2 commit 4ec2abb

File tree

4 files changed

+46
-19
lines changed

4 files changed

+46
-19
lines changed

components/lwip/include/lwip/lwip/netif.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ void netif_set_gw(struct netif *netif, const ip4_addr_t *gw);
385385
void netif_set_up(struct netif *netif);
386386
void netif_set_down(struct netif *netif);
387387
/** Ask if an interface is up */
388-
#define netif_is_up(netif) (((netif)->flags & NETIF_FLAG_UP) ? (u8_t)1 : (u8_t)0)
388+
#define netif_is_up(netif) ( ((netif) && ((netif)->flags & NETIF_FLAG_UP)) ? (u8_t)1 : (u8_t)0)
389389

390390
#if LWIP_NETIF_STATUS_CALLBACK
391391
void netif_set_status_callback(struct netif *netif, netif_status_callback_fn status_callback);

components/lwip/port/netif/ethernetif.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,17 @@ ethernetif_input(struct netif *netif, void *buffer, uint16_t len)
153153
{
154154
struct pbuf *p;
155155

156-
if(buffer== NULL || netif == NULL)
157-
goto _exit;
156+
if(buffer== NULL || !netif_is_up(netif)) {
157+
if (buffer) {
158+
esp_eth_free_rx_buf(buffer);
159+
}
160+
return;
161+
}
162+
158163
#ifdef CONFIG_EMAC_L2_TO_L3_RX_BUF_MODE
159164
p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM);
160165
if (p == NULL) {
166+
esp_eth_free_rx_buf(buffer);
161167
return;
162168
}
163169
p->l2_owner = NULL;
@@ -172,6 +178,7 @@ if (netif->input(p, netif) != ERR_OK) {
172178
#else
173179
p = pbuf_alloc(PBUF_RAW, len, PBUF_REF);
174180
if (p == NULL){
181+
esp_eth_free_rx_buf(buffer);
175182
return;
176183
}
177184
p->payload = buffer;
@@ -185,8 +192,6 @@ if (netif->input(p, netif) != ERR_OK) {
185192
pbuf_free(p);
186193
}
187194
#endif
188-
_exit:
189-
;
190195
}
191196

192197
/**

components/lwip/port/netif/wlanif.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,12 @@ wlanif_input(struct netif *netif, void *buffer, u16_t len, void* eb)
144144
{
145145
struct pbuf *p;
146146

147-
if(!buffer || !netif)
148-
goto _exit;
147+
if(!buffer || !netif_is_up(netif)) {
148+
if (eb) {
149+
esp_wifi_internal_free_rx_buffer(eb);
150+
}
151+
return;
152+
}
149153

150154
#if (ESP_L2_TO_L3_COPY == 1)
151155
p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM);
@@ -161,6 +165,7 @@ wlanif_input(struct netif *netif, void *buffer, u16_t len, void* eb)
161165
p = pbuf_alloc(PBUF_RAW, len, PBUF_REF);
162166
if (p == NULL){
163167
ESP_STATS_DROP_INC(esp.wlanif_input_pbuf_fail);
168+
esp_wifi_internal_free_rx_buffer(eb);
164169
return;
165170
}
166171
p->payload = buffer;
@@ -174,8 +179,6 @@ wlanif_input(struct netif *netif, void *buffer, u16_t len, void* eb)
174179
pbuf_free(p);
175180
}
176181

177-
_exit:
178-
;
179182
}
180183

181184
/**

components/tcpip_adapter/tcpip_adapter_lwip.c

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ void tcpip_adapter_init(void)
8585

8686
tcpip_init(NULL, NULL);
8787

88+
memset(esp_ip, 0, sizeof(tcpip_adapter_ip_info_t)*TCPIP_ADAPTER_IF_MAX);
8889
IP4_ADDR(&esp_ip[TCPIP_ADAPTER_IF_AP].ip, 192, 168 , 4, 1);
8990
IP4_ADDR(&esp_ip[TCPIP_ADAPTER_IF_AP].gw, 192, 168 , 4, 1);
9091
IP4_ADDR(&esp_ip[TCPIP_ADAPTER_IF_AP].netmask, 255, 255 , 255, 0);
@@ -140,8 +141,11 @@ esp_err_t tcpip_adapter_start(tcpip_adapter_if_t tcpip_if, uint8_t *mac, tcpip_a
140141
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
141142
}
142143

143-
if (esp_netif[tcpip_if] == NULL) {
144-
esp_netif[tcpip_if] = calloc(1, sizeof(*esp_netif[tcpip_if]));
144+
if (esp_netif[tcpip_if] == NULL || !netif_is_up(esp_netif[tcpip_if])) {
145+
if (esp_netif[tcpip_if] == NULL) {
146+
esp_netif[tcpip_if] = calloc(1, sizeof(*esp_netif[tcpip_if]));
147+
}
148+
145149
if (esp_netif[tcpip_if] == NULL) {
146150
return ESP_ERR_NO_MEM;
147151
}
@@ -166,11 +170,11 @@ esp_err_t tcpip_adapter_start(tcpip_adapter_if_t tcpip_if, uint8_t *mac, tcpip_a
166170
}
167171

168172
/* if ap is on, choose ap as default if */
169-
if (esp_netif[TCPIP_ADAPTER_IF_AP]) {
173+
if (netif_is_up(esp_netif[TCPIP_ADAPTER_IF_AP])) {
170174
netif_set_default(esp_netif[TCPIP_ADAPTER_IF_AP]);
171-
} else if (esp_netif[TCPIP_ADAPTER_IF_STA]) {
175+
} else if (netif_is_up(esp_netif[TCPIP_ADAPTER_IF_STA])) {
172176
netif_set_default(esp_netif[TCPIP_ADAPTER_IF_STA]);
173-
} else if (esp_netif[TCPIP_ADAPTER_IF_ETH] ) {
177+
} else if (netif_is_up(esp_netif[TCPIP_ADAPTER_IF_ETH])) {
174178
netif_set_default(esp_netif[TCPIP_ADAPTER_IF_ETH]);
175179
}
176180

@@ -194,6 +198,11 @@ esp_err_t tcpip_adapter_stop(tcpip_adapter_if_t tcpip_if)
194198
return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY;
195199
}
196200

201+
if (!netif_is_up(esp_netif[tcpip_if])) {
202+
netif_remove(esp_netif[tcpip_if]);
203+
return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY;
204+
}
205+
197206
if (tcpip_if == TCPIP_ADAPTER_IF_AP) {
198207
dhcps_stop(esp_netif[tcpip_if]); // TODO: dhcps checks status by its self
199208
if (TCPIP_ADAPTER_DHCP_STOPPED != dhcps_status) {
@@ -211,14 +220,16 @@ esp_err_t tcpip_adapter_stop(tcpip_adapter_if_t tcpip_if)
211220
ip4_addr_set_zero(&esp_ip[tcpip_if].netmask);
212221
}
213222

223+
netif_set_down(esp_netif[tcpip_if]);
214224
netif_remove(esp_netif[tcpip_if]);
215225

216-
free(esp_netif[tcpip_if]);
217-
esp_netif[tcpip_if] = NULL;
218-
219226
/* in ap + sta mode, if stop ap, choose sta as default if */
220-
if (esp_netif[TCPIP_ADAPTER_IF_STA] && tcpip_if == TCPIP_ADAPTER_IF_AP) {
221-
netif_set_default(esp_netif[TCPIP_ADAPTER_IF_STA]);
227+
if (tcpip_if == TCPIP_ADAPTER_IF_AP) {
228+
if (netif_is_up(esp_netif[TCPIP_ADAPTER_IF_STA])) {
229+
netif_set_default(esp_netif[TCPIP_ADAPTER_IF_STA]);
230+
} else if (netif_is_up(esp_netif[TCPIP_ADAPTER_IF_ETH])) {
231+
netif_set_default(esp_netif[TCPIP_ADAPTER_IF_ETH]);
232+
}
222233
}
223234

224235
return ESP_OK;
@@ -244,6 +255,14 @@ esp_err_t tcpip_adapter_up(tcpip_adapter_if_t tcpip_if)
244255
netif_set_up(esp_netif[tcpip_if]);
245256
}
246257

258+
if (netif_is_up(esp_netif[TCPIP_ADAPTER_IF_AP])) {
259+
netif_set_default(esp_netif[TCPIP_ADAPTER_IF_AP]);
260+
} else if (netif_is_up(esp_netif[TCPIP_ADAPTER_IF_STA])) {
261+
netif_set_default(esp_netif[TCPIP_ADAPTER_IF_STA]);
262+
} else if (netif_is_up(esp_netif[TCPIP_ADAPTER_IF_ETH])) {
263+
netif_set_default(esp_netif[TCPIP_ADAPTER_IF_ETH]);
264+
}
265+
247266
return ESP_OK;
248267
}
249268

0 commit comments

Comments
 (0)