esp32: Support building with ESP IDF 4.0-beta1.

This commit adds support for a second supported hash (currently set to the
4.0-beta1 tag).  When this hash is detected, the relevant changes are
applied.

This allows to start using v4 features (e.g. BLE with Nimble), and also
start doing testing, while still supporting the original, stable, v3.3 IDF.

Note: this feature is experimental, not well tested, and network.LAN and
network.PPP are currently unsupported.
This commit is contained in:
Jim Mussared
2019-09-13 23:04:13 +10:00
committed by Damien George
parent b45f9de809
commit 96008ff59a
15 changed files with 266 additions and 57 deletions

View File

@@ -56,6 +56,18 @@
#include "lwip/igmp.h"
#include "esp_log.h"
#if !MICROPY_ESP_IDF_4
#define lwip_bind lwip_bind_r
#define lwip_listen lwip_listen_r
#define lwip_accept lwip_accept_r
#define lwip_setsockopt lwip_setsockopt_r
#define lwip_fnctl lwip_fnctl_r
#define lwip_recvfrom lwip_recvfrom_r
#define lwip_write lwip_write_r
#define lwip_sendto lwip_sendto_r
#define lwip_close lwip_close_r
#endif
#define SOCKET_POLL_US (100000)
#define MDNS_QUERY_TIMEOUT_MS (5000)
#define MDNS_LOCAL_SUFFIX ".local"
@@ -277,17 +289,17 @@ STATIC mp_obj_t socket_bind(const mp_obj_t arg0, const mp_obj_t arg1) {
socket_obj_t *self = MP_OBJ_TO_PTR(arg0);
struct addrinfo *res;
_socket_getaddrinfo(arg1, &res);
int r = lwip_bind_r(self->fd, res->ai_addr, res->ai_addrlen);
int r = lwip_bind(self->fd, res->ai_addr, res->ai_addrlen);
lwip_freeaddrinfo(res);
if (r < 0) exception_from_errno(errno);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_bind_obj, socket_bind);
STATIC mp_obj_t socket_listen(const mp_obj_t arg0, const mp_obj_t arg1) {
socket_obj_t *self = MP_OBJ_TO_PTR(arg0);
int backlog = mp_obj_get_int(arg1);
int r = lwip_listen_r(self->fd, backlog);
int r = lwip_listen(self->fd, backlog);
if (r < 0) exception_from_errno(errno);
return mp_const_none;
}
@@ -302,7 +314,7 @@ STATIC mp_obj_t socket_accept(const mp_obj_t arg0) {
int new_fd = -1;
for (int i=0; i<=self->retries; i++) {
MP_THREAD_GIL_EXIT();
new_fd = lwip_accept_r(self->fd, &addr, &addr_len);
new_fd = lwip_accept(self->fd, &addr, &addr_len);
MP_THREAD_GIL_ENTER();
if (new_fd >= 0) break;
if (errno != EAGAIN) exception_from_errno(errno);
@@ -342,7 +354,7 @@ STATIC mp_obj_t socket_connect(const mp_obj_t arg0, const mp_obj_t arg1) {
struct addrinfo *res;
_socket_getaddrinfo(arg1, &res);
MP_THREAD_GIL_EXIT();
int r = lwip_connect_r(self->fd, res->ai_addr, res->ai_addrlen);
int r = lwip_connect(self->fd, res->ai_addr, res->ai_addrlen);
MP_THREAD_GIL_ENTER();
lwip_freeaddrinfo(res);
if (r != 0) {
@@ -363,7 +375,7 @@ STATIC mp_obj_t socket_setsockopt(size_t n_args, const mp_obj_t *args) {
// level: SOL_SOCKET
case SO_REUSEADDR: {
int val = mp_obj_get_int(args[3]);
int ret = lwip_setsockopt_r(self->fd, SOL_SOCKET, opt, &val, sizeof(int));
int ret = lwip_setsockopt(self->fd, SOL_SOCKET, opt, &val, sizeof(int));
if (ret != 0) {
exception_from_errno(errno);
}
@@ -424,9 +436,9 @@ void _socket_settimeout(socket_obj_t *sock, uint64_t timeout_ms) {
.tv_sec = 0,
.tv_usec = timeout_ms ? SOCKET_POLL_US : 0
};
lwip_setsockopt_r(sock->fd, SOL_SOCKET, SO_SNDTIMEO, (const void *)&timeout, sizeof(timeout));
lwip_setsockopt_r(sock->fd, SOL_SOCKET, SO_RCVTIMEO, (const void *)&timeout, sizeof(timeout));
lwip_fcntl_r(sock->fd, F_SETFL, timeout_ms ? 0 : O_NONBLOCK);
lwip_setsockopt(sock->fd, SOL_SOCKET, SO_SNDTIMEO, (const void *)&timeout, sizeof(timeout));
lwip_setsockopt(sock->fd, SOL_SOCKET, SO_RCVTIMEO, (const void *)&timeout, sizeof(timeout));
lwip_fcntl(sock->fd, F_SETFL, timeout_ms ? 0 : O_NONBLOCK);
}
STATIC mp_obj_t socket_settimeout(const mp_obj_t arg0, const mp_obj_t arg1) {
@@ -459,7 +471,7 @@ STATIC mp_uint_t _socket_read_data(mp_obj_t self_in, void *buf, size_t size,
socket_obj_t *sock = MP_OBJ_TO_PTR(self_in);
// If the peer closed the connection then the lwIP socket API will only return "0" once
// from lwip_recvfrom_r and then block on subsequent calls. To emulate POSIX behaviour,
// from lwip_recvfrom and then block on subsequent calls. To emulate POSIX behaviour,
// which continues to return "0" for each call on a closed socket, we set a flag when
// the peer closed the socket.
if (sock->peer_closed) {
@@ -482,7 +494,7 @@ STATIC mp_uint_t _socket_read_data(mp_obj_t self_in, void *buf, size_t size,
if (release_gil) {
MP_THREAD_GIL_EXIT();
}
int r = lwip_recvfrom_r(sock->fd, buf, size, 0, from, from_len);
int r = lwip_recvfrom(sock->fd, buf, size, 0, from, from_len);
if (release_gil) {
MP_THREAD_GIL_ENTER();
}
@@ -543,13 +555,13 @@ int _socket_send(socket_obj_t *sock, const char *data, size_t datalen) {
int sentlen = 0;
for (int i=0; i<=sock->retries && sentlen < datalen; i++) {
MP_THREAD_GIL_EXIT();
int r = lwip_write_r(sock->fd, data+sentlen, datalen-sentlen);
int r = lwip_write(sock->fd, data+sentlen, datalen-sentlen);
MP_THREAD_GIL_ENTER();
if (r < 0 && errno != EWOULDBLOCK) exception_from_errno(errno);
if (r > 0) sentlen += r;
check_for_exceptions();
}
if (sentlen == 0) mp_raise_OSError(MP_ETIMEDOUT);
if (sentlen == 0) mp_raise_OSError(MP_ETIMEDOUT);
return sentlen;
}
@@ -590,7 +602,7 @@ STATIC mp_obj_t socket_sendto(mp_obj_t self_in, mp_obj_t data_in, mp_obj_t addr_
// send the data
for (int i=0; i<=self->retries; i++) {
MP_THREAD_GIL_EXIT();
int ret = lwip_sendto_r(self->fd, bufinfo.buf, bufinfo.len, 0, (struct sockaddr*)&to, sizeof(to));
int ret = lwip_sendto(self->fd, bufinfo.buf, bufinfo.len, 0, (struct sockaddr*)&to, sizeof(to));
MP_THREAD_GIL_ENTER();
if (ret > 0) return mp_obj_new_int_from_uint(ret);
if (ret == -1 && errno != EWOULDBLOCK) {
@@ -598,7 +610,7 @@ STATIC mp_obj_t socket_sendto(mp_obj_t self_in, mp_obj_t data_in, mp_obj_t addr_
}
check_for_exceptions();
}
mp_raise_OSError(MP_ETIMEDOUT);
mp_raise_OSError(MP_ETIMEDOUT);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(socket_sendto_obj, socket_sendto);
@@ -622,7 +634,7 @@ STATIC mp_uint_t socket_stream_write(mp_obj_t self_in, const void *buf, mp_uint_
socket_obj_t *sock = self_in;
for (int i=0; i<=sock->retries; i++) {
MP_THREAD_GIL_EXIT();
int r = lwip_write_r(sock->fd, buf, size);
int r = lwip_write(sock->fd, buf, size);
MP_THREAD_GIL_ENTER();
if (r > 0) return r;
if (r < 0 && errno != EWOULDBLOCK) { *errcode = errno; return MP_STREAM_ERROR; }
@@ -663,7 +675,7 @@ STATIC mp_uint_t socket_stream_ioctl(mp_obj_t self_in, mp_uint_t request, uintpt
socket->events_callback = MP_OBJ_NULL;
}
#endif
int ret = lwip_close_r(socket->fd);
int ret = lwip_close(socket->fd);
if (ret != 0) {
*errcode = errno;
return MP_STREAM_ERROR;
@@ -731,7 +743,7 @@ STATIC mp_obj_t esp_socket_getaddrinfo(size_t n_args, const mp_obj_t *args) {
mp_obj_new_str(resi->ai_canonname, strlen(resi->ai_canonname)),
mp_const_none
};
if (resi->ai_family == AF_INET) {
struct sockaddr_in *addr = (struct sockaddr_in *)resi->ai_addr;
// This looks odd, but it's really just a u32_t