Skip to content

Commit 2317aec

Browse files
authored
Merge pull request #10631 from dhalbert/lwip-fixes
Further LWIP-related fixes
2 parents 11ecd40 + 4008e14 commit 2317aec

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

ports/espressif/common-hal/socketpool/Socket.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ void common_hal_socketpool_socket_connect(socketpool_socket_obj_t *self,
483483
socklen_t socklen = sizeof(error_code);
484484
result = getsockopt(self->num, SOL_SOCKET, SO_ERROR, &error_code, &socklen);
485485
if (result < 0 || error_code != 0) {
486-
mp_raise_OSError(errno);
486+
mp_raise_OSError(error_code);
487487
}
488488
self->connected = true;
489489
return;

ports/raspberrypi/common-hal/socketpool/Socket.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static mp_obj_t socketpool_ip_addr_and_port_to_tuple(const ip_addr_t *addr, int
8989
// socket API.
9090

9191
// Extension to lwIP error codes
92-
// Matches lwIP 2.0.3
92+
// Matches lwIP 2.2.1
9393
#undef _ERR_BADF
9494
#define _ERR_BADF -17
9595
static const int error_lookup_table[] = {
@@ -473,7 +473,12 @@ static mp_uint_t lwip_tcp_send(socketpool_socket_obj_t *socket, const byte *buf,
473473

474474
MICROPY_PY_LWIP_ENTER
475475

476-
u16_t available = tcp_sndbuf(socket->pcb.tcp);
476+
// If the socket is still connecting then don't let data be written to it.
477+
// Otherwise, get the number of available bytes in the output buffer.
478+
u16_t available = 0;
479+
if (socket->state != STATE_CONNECTING) {
480+
available = tcp_sndbuf(socket->pcb.tcp);
481+
}
477482

478483
if (available == 0) {
479484
// Non-blocking socket
@@ -490,7 +495,8 @@ static mp_uint_t lwip_tcp_send(socketpool_socket_obj_t *socket, const byte *buf,
490495
// If peer fully closed socket, we would have socket->state set to ERR_RST (connection
491496
// reset) by error callback.
492497
// Avoid sending too small packets, so wait until at least 16 bytes available
493-
while (socket->state >= STATE_CONNECTED && (available = tcp_sndbuf(socket->pcb.tcp)) < 16) {
498+
while (socket->state == STATE_CONNECTING
499+
|| (socket->state >= STATE_CONNECTED && (available = tcp_sndbuf(socket->pcb.tcp)) < 16)) {
494500
MICROPY_PY_LWIP_EXIT
495501
if (socket->timeout != (unsigned)-1 && mp_hal_ticks_ms() - start > socket->timeout) {
496502
*_errno = MP_ETIMEDOUT;
@@ -531,9 +537,10 @@ static mp_uint_t lwip_tcp_send(socketpool_socket_obj_t *socket, const byte *buf,
531537
MICROPY_PY_LWIP_REENTER
532538
}
533539

534-
// If the output buffer is getting full then send the data to the lower layers
535-
if (err == ERR_OK && tcp_sndbuf(socket->pcb.tcp) < TCP_SND_BUF / 4) {
536-
err = tcp_output(socket->pcb.tcp);
540+
// Use nagle algorithm to determine when to send segment buffer (can be
541+
// disabled with TCP_NODELAY socket option)
542+
if (err == ERR_OK) {
543+
err = tcp_output_nagle(socket->pcb.tcp);
537544
}
538545

539546
MICROPY_PY_LWIP_EXIT
@@ -551,6 +558,12 @@ static mp_uint_t lwip_tcp_receive(socketpool_socket_obj_t *socket, byte *buf, mp
551558
// Check for any pending errors
552559
STREAM_ERROR_CHECK(socket);
553560

561+
if (socket->state == STATE_LISTENING) {
562+
// original socket in listening state, not the accepted connection.
563+
*_errno = MP_ENOTCONN;
564+
return -1;
565+
}
566+
554567
if (socket->incoming.pbuf == NULL) {
555568

556569
// Non-blocking socket

ports/raspberrypi/lwip_src/lwip_mem.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,12 @@
1212
#include "supervisor/port_heap.h"
1313

1414
void *lwip_heap_malloc(size_t size) {
15-
common_hal_mcu_disable_interrupts();
1615
void *ptr = port_malloc(size, true);
17-
common_hal_mcu_enable_interrupts();
1816
return ptr;
1917
}
2018

2119
void lwip_heap_free(void *ptr) {
22-
common_hal_mcu_disable_interrupts();
2320
port_free(ptr);
24-
common_hal_mcu_enable_interrupts();
2521
}
2622

2723
void *lwip_heap_calloc(size_t num, size_t size) {

ports/raspberrypi/supervisor/port.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,31 +264,42 @@ void port_heap_init(void) {
264264

265265
void *port_malloc(size_t size, bool dma_capable) {
266266
if (!dma_capable && _psram_size > 0) {
267+
common_hal_mcu_disable_interrupts();
267268
void *block = tlsf_malloc(_psram_heap, size);
269+
common_hal_mcu_enable_interrupts();
268270
if (block) {
269271
return block;
270272
}
271273
}
274+
common_hal_mcu_disable_interrupts();
272275
void *block = tlsf_malloc(_heap, size);
276+
common_hal_mcu_enable_interrupts();
273277
return block;
274278
}
275279

276280
void port_free(void *ptr) {
281+
common_hal_mcu_disable_interrupts();
277282
if (((size_t)ptr) < SRAM_BASE) {
278283
tlsf_free(_psram_heap, ptr);
279284
} else {
280285
tlsf_free(_heap, ptr);
281286
}
287+
common_hal_mcu_enable_interrupts();
282288
}
283289

284290
void *port_realloc(void *ptr, size_t size, bool dma_capable) {
285291
if (_psram_size > 0 && ((ptr != NULL && ((size_t)ptr) < SRAM_BASE) || (ptr == NULL && !dma_capable))) {
292+
common_hal_mcu_disable_interrupts();
286293
void *block = tlsf_realloc(_psram_heap, ptr, size);
294+
common_hal_mcu_enable_interrupts();
287295
if (block) {
288296
return block;
289297
}
290298
}
291-
return tlsf_realloc(_heap, ptr, size);
299+
common_hal_mcu_disable_interrupts();
300+
void *new_ptr = tlsf_realloc(_heap, ptr, size);
301+
common_hal_mcu_enable_interrupts();
302+
return new_ptr;
292303
}
293304

294305
static bool max_size_walker(void *ptr, size_t size, int used, void *user) {

0 commit comments

Comments
 (0)